From: tjhunt Date: Thu, 11 Dec 2008 04:33:41 +0000 (+0000) Subject: numerical qtype: MDL-15159 Error moving a numerical question type with no units from... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=26b266625bd74f9794e24d9cbfdffe76bb998240;p=moodle.git numerical qtype: MDL-15159 Error moving a numerical question type with no units from one category to another. --- diff --git a/question/type/numerical/edit_numerical_form.php b/question/type/numerical/edit_numerical_form.php index ceb3376b49..2d91d9890e 100644 --- a/question/type/numerical/edit_numerical_form.php +++ b/question/type/numerical/edit_numerical_form.php @@ -77,10 +77,14 @@ class question_edit_numerical_form extends question_edit_form { } $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_numerical')); - $firstunit =& $mform->getElement('multiplier[0]'); - $firstunit->freeze(); - $firstunit->setValue('1.0'); - $firstunit->setPersistantFreeze(true); + if ($mform->elementExists('multiplier[0]')) { + /// Does not exist when this form is used in 'move to another category' + /// mode with a qusetion that has no units. This was leading to errors. + $firstunit =& $mform->getElement('multiplier[0]'); + $firstunit->freeze(); + $firstunit->setValue('1.0'); + $firstunit->setPersistantFreeze(true); + } } function set_data($question) { @@ -129,7 +133,7 @@ class question_edit_numerical_form extends question_edit_form { $answercount++; } } - if ($answercount==0){ + if ($answercount == 0) { $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical'); } if ($maxgrade == false) { @@ -138,15 +142,17 @@ class question_edit_numerical_form extends question_edit_form { // Check units. $alreadyseenunits = array(); - foreach ($data['unit'] as $key => $unit) { - $trimmedunit = trim($unit); - if ($trimmedunit!='' && in_array($trimmedunit, $alreadyseenunits)) { - $errors["unit[$key]"] = get_string('errorrepeatedunit', 'qtype_numerical'); - if (trim($data['multiplier'][$key]) == '') { - $errors["multiplier[$key]"] = get_string('errornomultiplier', 'qtype_numerical'); + if (isset($data['unit'])) { + foreach ($data['unit'] as $key => $unit) { + $trimmedunit = trim($unit); + if ($trimmedunit!='' && in_array($trimmedunit, $alreadyseenunits)) { + $errors["unit[$key]"] = get_string('errorrepeatedunit', 'qtype_numerical'); + if (trim($data['multiplier'][$key]) == '') { + $errors["multiplier[$key]"] = get_string('errornomultiplier', 'qtype_numerical'); + } + } else { + $alreadyseenunits[] = $trimmedunit; } - } else { - $alreadyseenunits[] = $trimmedunit; } } diff --git a/question/type/numerical/questiontype.php b/question/type/numerical/questiontype.php index 806a626c10..1308b3f0cc 100644 --- a/question/type/numerical/questiontype.php +++ b/question/type/numerical/questiontype.php @@ -204,6 +204,12 @@ class question_numerical_qtype extends question_shortanswer_qtype { // Delete the units previously saved for this question. $DB->delete_records('question_numerical_units', array('question' => $question->id)); + // Nothing to do. + if (!isset($question->multiplier)) { + $result->units = array(); + return $result; + } + // Save the new units. $units = array(); foreach ($question->multiplier as $i => $multiplier) {