Skip to content

Commit

Permalink
The calculated question wizard has been restructured/rewritten. A new…
Browse files Browse the repository at this point in the history
… question is now only saved when the first dataset item is added. And hopefully the code is somewhat clearer and easier to understand. Additionally it all works through the (slightly adapted) standard question.php mechanisms and doesn't hack around it.
  • Loading branch information
mindforge committed Jun 11, 2005
1 parent eceda46 commit 3ed1ef5
Show file tree
Hide file tree
Showing 8 changed files with 700 additions and 608 deletions.
6 changes: 2 additions & 4 deletions mod/quiz/questiontypes/calculated/calculated.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<form name="theform" method="post" action="question.php">
<center>
<input type="hidden" name="nextwizardpage" value="<?php p($nextwizardpage)?>" />
<?php foreach ($calculatedmessages as $message) {formerr("$message<br/>");} ?>
<table cellpadding="5">
<tr valign="top">
<td align="right"><b><?php print_string("category", "quiz") ?>:</b></td>
Expand Down Expand Up @@ -77,7 +75,7 @@
<tr valign="top">
<td align="right"><b><?php print_string("correctanswerformula", "quiz") ?>:</b></td>
<td>
<input align="left" type="text" id="formula0" name="answer[]" size="20" value="<?php p($answers[0]->answer) ?>" alt="<?php print_string("correctanswerformula", "quiz") ?>" />&nbsp;&nbsp;
<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" />
</td>
</tr>
Expand Down Expand Up @@ -153,10 +151,10 @@
} /// END for
?>
</table>

<input type="hidden" name="sesskey" value="<?php echo $USER->sesskey; ?>" />
<input type="hidden" name="id" value="<?php p($question->id) ?>" />
<input type="hidden" name="qtype" value="<?php p($question->qtype) ?>" />
<input type="hidden" name="wizardpage" value="question" />
<input type="submit" onclick="return determineMinAndMax();" value="<?php print_string("savechanges") ?>" />
</center>
</form>
Expand Down
183 changes: 18 additions & 165 deletions mod/quiz/questiontypes/calculated/editquestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,163 +2,12 @@

// Get a handle to the question type we are dealing with here
$qtypeobj = $QUIZ_QTYPES[CALCULATED];
if (isset($form->editdatasets) && $form->editdatasets) {
require("$CFG->dirroot/mod/quiz/questiontypes/datasetdependent/datasetitems.php");
exit();
}

$calculatedmessages = array();
if ($form) {

// Verify the quality of the question properties
if (empty($question->name)) {
$calculatedmessages[] = get_string('missingname', 'quiz');
}
if (empty($question->questiontext)) {
$calculatedmessages[] = get_string('missingquestiontext', 'quiz');
}

// Formula stuff (verify some of them)
$answers[0]->answer = trim(array_shift($form->answer))
and false===($formulaerrors =
quiz_qtype_calculated_find_formula_errors($answers[0]->answer))
or $answers[0]->answer
and $calculatedmessages[] = $formulaerrors
or $calculatedmesages[] = get_string('missingformula', 'quiz');

$answers[0]->tolerance = array_shift($form->tolerance)
or $answers[0]->tolerance = 0.0;
is_numeric($answers[0]->tolerance)
or $calculatedmessages[] = get_string('tolerancemustbenumeric', 'quiz');

$answers[0]->feedback = array_shift($form->feedback);

// Let's trust the drop down menus.

$answers[0]->tolerancetype = array_shift($form->tolerancetype);
$answers[0]->fraction = array_shift($form->fraction);

$answers[0]->correctanswerlength = array_shift($form->correctanswerlength);
$answers[0]->correctanswerformat = array_shift($form->correctanswerformat);

2 == $answers[0]->correctanswerformat
and 0 == $answers[0]->correctanswerlength
and $calculatedmessages[]=get_string('zerosignificantfiguresnotallowed','quiz');

// Fill with remaining answers, in case calculated.html
// supports multiple formulas.
$i = 1;
foreach ($form->answer as $key => $answer) {
if (trim($answer)) {
$answers[$i]->answer = trim($answer);
$answers[$i]->tolerance = $form->tolerance[$key]
or $answers[$i]->tolerance = 0.0;
$answers[$i]->tolerancetype = $form->tolerancetype[$key];
$answers[$i]->correctanswerlength =
$form->correctanswerlength[$key];
$answers[$i]->correctanswerformat =
$form->correctanswerformat[$key];

$answers[$i]->fraction = $form->fraction[$key];
$answers[$i]->feedback = $form->feedback[$key];

// Check for errors:
false === ($formulaerrors =
quiz_qtype_calculated_find_formula_errors($answer))
or $calculatedmessages[] = $formulaerrors;
is_numeric($answers[$i]->tolerance)
or $calculatedmessages[] = get_string('tolerancemustbenumeric',
'quiz');
// Increase answer count
++$i;
}
}

// Finally the units:

// Start with the default units...
$units[0]->unit = array_shift($form->unit);
array_shift($form->multiplier); // In case it is not 1.0
$units[0]->multiplier = 1.0; // Must!

// Accept other units if they have legal multipliers
$i = 1;
foreach ($form->multiplier as $key => $multiplier) {
if ($multiplier && is_numeric($multiplier)) {
$units[$i]->multiplier = $multiplier;
$units[$i]->unit = $form->unit[$key];
++$i;
}
}


if (empty($calculatedmessages)) {
/*First page in the question wizard (calculated.html) passed all right!*/

if (!empty($form->dataset)) {
/* Second page in the question wizard has also passed all right */

/***** Save question! ... ****/
$subtypeoptions->answers = $answers;
$subtypeoptions->units = $units;
$question = $qtypeobj->save_question
($question, $form, $course, $subtypeoptions);

/***** ... and continue to the dataset item editing: *****/
require("$CFG->dirroot/mod/quiz/questiontypes/datasetdependent/datasetitems.php");
exit();
}

/***** Now continue by preparing for the second page ******
***** in the question wizard: "questiondatasets.html" ******/
$datasetmessage = '';

/*** Answer information is not to be shown ***/
$hiddeninputnames= array();
$hiddeninputvalues= array();
foreach ($answers as $answer) {
$hiddeninputnames[] = 'answer[]';
$hiddeninputvalues[] = $answer->answer;
$hiddeninputnames[] = 'fraction[]';
$hiddeninputvalues[] = $answer->fraction;
$hiddeninputnames[] = 'feedback[]';
$hiddeninputvalues[] = $answer->feedback;
$hiddeninputnames[] = 'tolerance[]';
$hiddeninputvalues[] = $answer->tolerance;
$hiddeninputnames[] = 'tolerancetype[]';
$hiddeninputvalues[] = $answer->tolerancetype;
$hiddeninputnames[] = 'correctanswerlength[]';
$hiddeninputvalues[] = $answer->correctanswerlength;
$hiddeninputnames[] = 'correctanswerformat[]';
$hiddeninputvalues[] = $answer->correctanswerformat;
}
foreach ($units as $unit) {
$hiddeninputnames[] = 'unit[]';
$hiddeninputvalues[] = $unit->unit;
$hiddeninputnames[] = 'multiplier[]';
$hiddeninputvalues[] = $unit->multiplier;
}

/*** Determine possible and mandatory datasets... ***/
$possibledatasets = $qtypeobj->find_dataset_names($question->questiontext);
$mandatorydatasets = array();
foreach ($answers as $answer) {
$mandatorydatasets += $qtypeobj
->find_dataset_names($answer->answer);
}
$datasets = $qtypeobj->construct_dataset_menus(
$question, $mandatorydatasets, $possibledatasets);

/*** Print the page ***/
print_heading_with_help(get_string("choosedatasetproperties", "quiz"), "questiondatasets", "quiz");
require("$CFG->dirroot/mod/quiz/questiontypes/datasetdependent/questiondatasets.html");
exit();
}

} else {
if (empty($form)) {
/*********************************************************/
/***** First page in question wizard - calculated.html! **/
/*********************************************************/
// Inside this block everything works as for non-wizardquestions

// The layout of the editing page will only support
// one formula alternative for calculated questions.
Expand All @@ -167,7 +16,7 @@
// does not have any limit.
$answers= array();
for ($i=0; $i<6; $i++) {
/*** Make answer slots with default values ***/
// Make answer slots with default values
$answers[$i]->answer = "";
$answers[$i]->feedback = "";
$answers[$i]->fraction = "1.0";
Expand All @@ -176,16 +25,16 @@
$answers[$i]->correctanswerlength = "2"; // Defaults to two ...
$answers[$i]->correctanswerformat = "1"; // ... decimals
}

if (!empty($question->id)) {
$QUIZ_QTYPES[$question->qtype]->get_question_options($question);
$answersraw = $question->options->answers;
if (!empty($answersraw)) {
/*** Overwrite the default valued answer slots ***
*** with correct values from database ***/
$i=0;
foreach ($answersraw as $answer) {
$answers[$i] = $answer;
$i++;
if (!empty($question->options->answers)) {
// Overwrite the default valued answer slots
// with correct values from database
$answersraw = array_values($question->options->answers);
$n = count($answersraw);
for ($i = 0; $i < $n; $i++) {
$answers[$i] = $answersraw[$i];
}
}
}
Expand Down Expand Up @@ -233,8 +82,12 @@
}
}
}
print_heading_with_help(get_string("editingcalculated", "quiz"), "calculated", "quiz");
require("$CFG->dirroot/mod/quiz/questiontypes/calculated/calculated.html");
} else { // $form is not empty
/*********************************************************/
/***** Any subsequent page of the question wizard **/
/*********************************************************/
$qtypeobj->print_next_wizard_page($question, $form, $course);
}
print_heading_with_help(get_string("editingcalculated", "quiz"), "calculated", "quiz");
require("$CFG->dirroot/mod/quiz/questiontypes/calculated/calculated.html");

?>
Loading

0 comments on commit 3ed1ef5

Please sign in to comment.