Permalink
Browse files

Adding various improvements (MDL-8565, MDL-9496, MDL10070 etc.)to cal…

…culated question
  • Loading branch information...
1 parent 158c4b5 commit aaf33485898ef81f3d310c6b88eaa35ad38e7d2c pichetp committed Jun 10, 2007
@@ -1,24 +1,30 @@
<?php
$QTYPES[$question->qtype]->print_question_form_start($question, array(), $course, $usehtmleditor);
+
+// Answers.
+ for ($i=1; $i<=count($answers); $i++) {
+ $answer = $answers[$i-1];
?>
<tr valign="top">
<td align="right"><b><?php print_string("correctanswerformula", "quiz") ?>:</b></td>
<td align="left">
- <input align="left" type="text" id="formula0" name="answers[]" size="20" value="<?php p($answers[0]->answer) ?>" alt="<?php print_string("correctanswerformula", "quiz") ?>" />&nbsp;&nbsp;
- <input type="hidden" name="fraction[]" value="1.0" />
+ <input align="left" type="text" id="formula0" name="answers[]" size="20" value="<?php p($answer->answer) ?>" alt="<?php print_string("correctanswerformula", "quiz") ?>" />&nbsp;&nbsp;
+ <?php print_string("grade");
+ echo ":&nbsp;";
+ choose_from_menu($gradeoptions, "fraction[]", $answer->fraction,""); ?>
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("tolerance", "quiz"); ?>:</b></td>
<td align="left">
- <input align="left" type="text" id="tolerance0" name="tolerance[]" size="15" value="<?php p($answers[0]->tolerance) ?>" alt="<?php print_string("tolerance", "quiz"); ?>" />&plusmn;
+ <input align="left" type="text" id="tolerance0" name="tolerance[]" size="15" value="<?php p($answer->tolerance) ?>" alt="<?php print_string("tolerance", "quiz"); ?>" />&plusmn;
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("tolerancetype", "quiz"); ?>:</b></td>
<td align="left">
<?php choose_from_menu($qtypeobj->tolerance_types(),
- 'tolerancetype[]', $answers[0]->tolerancetype, false); ?>
+ 'tolerancetype[]', $answer->tolerancetype, false); ?>
</td>
</tr>
<tr valign="top">
@@ -29,17 +35,27 @@
'4' => ' 4 ', '5' => ' 5 ', '6' => ' 6 ',
'7' => ' 7 ', '8' => ' 8 ', '9' => ' 9 '),
'correctanswerlength[]',
- $answers[0]->correctanswerlength, false); ?>
+ $answer->correctanswerlength, false); ?>
<?php choose_from_menu(array('1' => get_string('decimalformat', 'quiz'),
'2' => get_string('significantfiguresformat', 'quiz')),
'correctanswerformat[]',
- $answers[0]->correctanswerformat, false); ?>
+ $answer->correctanswerformat, false); ?>
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("feedback", "quiz") ?>:</b></td>
<td align="left">
- <textarea name="feedback[]" rows="2" cols="50"><?php p($answers[0]->feedback) ?></textarea>
+ <textarea name="feedback[]" rows="2" cols="50"><?php p($answer->feedback) ?></textarea>
+ </td>
+</tr>
+<?php
+ }
+// Units.
+$addmoreanswerblanks=get_string('addmoreanswerblanks', 'qtype_calculated');
+?>
+<tr valign="top">
+ <td align="center" colspan="2">
+ <input name ='addanswers' type="submit" value="<?php p($addmoreanswerblanks) ?>" />
</td>
</tr>
<tr valign="top">
@@ -78,37 +94,17 @@
</tr>
<?php
} /// END for
-
+$addmoreanswerblanks=get_string('addmoreunitblanks', 'qtype_calculated');
+?>
+<tr valign="top">
+ <td align="center" colspan="2">
+ <input name ='addunits' type="submit" value="<?php p($addmoreanswerblanks) ?>" />
+ </td>
+</tr>
+<?php
$QTYPES[$question->qtype]->print_replacement_options($question, $course, $contextquiz);
$QTYPES[$question->qtype]->print_question_form_end($question,
- 'onclick="return determineMinAndMax();"',
+ '',
'<input type="hidden" name="wizardpage" value="question" />');
?>
-<script language="javascript" type="text/javascript">
-function determineMinAndMax() {
- // This client-side script will determine the values for min and max
- // based on the input for answer and acceptederror.
- with(document.theform) {
- if (formula0.value=='') {
- alert('<?php print_string("missingformula","quiz") ?>');
- return false;
- /* It could perhaps be an idea to parse the formula here
- * but as it is necessary at the server anyway, we can
- * it leave like this for the moment. */
- } else if (''!=defaultunit.value && !isNaN(defaultunit.value)) {
- alert('<?php print_string("unitmustnotbenumeric","quiz") ?>');
- return false;
- } else if (isNaN(tolerance0.value)) {
- alert('<?php print_string("tolerancemustbenumeric","quiz") ?>');
- return false;
- } else if ('2' == document.theform['correctanswerformat[]'].value
- && '0' == document.theform['correctanswerlength[]'].value) {
- alert('<?php print_string("zerosignificantfiguresnotallowed","quiz") ?>');
- return false;
- } else {
- return true;
- }
- }
-}
-</script>
@@ -3,7 +3,7 @@
// Get a handle to the question type we are dealing with here
$qtypeobj = $QTYPES['calculated'];
-if (empty($form)) {
+if (empty($form)|| isset($form->addanswers) || isset($form->addunits)) {
/****************************************************************/
/***** First page in question wizard - editquestion.html.html! **/
/****************************************************************/
@@ -14,7 +14,7 @@
// However, the code behind supports up to six formulas
// and the database store and attempt/review framework
// does not have any limit.
- $answers= array();
+/* $answers= array();
for ($i=0; $i<6; $i++) {
// Make answer slots with default values
$answers[$i]->answer = "";
@@ -38,15 +38,52 @@
}
}
}
+ */
+ $emptyanswer = new stdClass;
+ $emptyanswer->answer = '';
+ $emptyanswer->feedback = "";
+ $emptyanswer->fraction = "1.0";
+ $emptyanswer->tolerance = "0.01";
+ $emptyanswer->tolerancetype = "1";
+ $emptyanswer->correctanswerlength = "2"; // Defaults to two ...
+ $emptyanswer->correctanswerformat = "1"; // ... decimals
+
+ if (!empty($question->id) && isset($question->qtype) &&
+ $QTYPES[$question->qtype]->get_question_options($question)) {
+
+ $answers = array_values($question->options->answers);
+ /* $units = array_values($question->options->units);
+ usort($units, create_function('$a, $b', // make sure the default unit is at index 0
+ 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
+ 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
+ */
+ } else {
+ $answers = array();
+ $answers[] = $emptyanswer;
+ // $units = array();
+ }
+
+ // Add blank answers to make the number up to QUESTION_NUMANS
+ // or one more than current, if there are already lots.
+ $emptyanswer = new stdClass;
+ $emptyanswer->answer = '';
+ $emptyanswer->feedback = "";
+ $emptyanswer->fraction = "1.0";
+ $emptyanswer->tolerance = "0.01";
+ $emptyanswer->tolerancetype = "1";
+ $emptyanswer->correctanswerlength = "2"; // Defaults to two ...
+ $emptyanswer->correctanswerformat = "1"; // ... decimals
+ // $i = count($answers);
+ // $limit = QUESTION_NUMANS;
+// $limit = $limit <= $i ? $i+1 : $limit;
+// for (; $i < $limit; $i++) {
+ $answers[] = $emptyanswer;
+ // }
// Units are handled the same way
// as for numerical questions
$units = array();
- for ($i=0 ; $i<6 ; $i++) {
- // Make unit slots, default as blank...
- $units[$i]->multiplier = '';
- $units[$i]->unit = '';
- }
+
if (!empty($question->id) and $unitsraw = get_records(
'question_numerical_units', 'question', $question->id)) {
/// Find default unit and have it put in the zero slot
@@ -70,8 +107,9 @@
}
} else {
$units[0]->multiplier = 1.0;
+ $units[0]->unit= '';
}
-
+
// Strip trailing zeros from multipliers
foreach ($units as $i => $unit) {
if (ereg('^(.*\\..(.*[^0])?)0+$', $unit->multiplier, $regs1)) {
@@ -82,6 +120,13 @@
}
}
}
+ if (isset($form->addunits)){
+ $emptyunit = new stdClass;
+ $emptyunit->multiplier = '';
+ $emptyunit->unit = '';
+ $units[]=$emptyunit;
+ $units[]=$emptyunit;
+ }
print_heading_with_help(get_string("editingcalculated", "quiz"), "calculated", "quiz");
require("$CFG->dirroot/question/type/calculated/editquestion.html");
} else { // $form is not empty
Oops, something went wrong.

0 comments on commit aaf3348

Please sign in to comment.