diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index 1c818bd2da2..6be1bbe1365 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -188,64 +188,53 @@ class qtype_calculated extends question_type { if (isset($question->answer) && !isset($question->answers)) { $question->answers = $question->answer; } - foreach ($question->answers as $key => $dataanswer) { - if (is_array($dataanswer)) { - $dataanswer = $dataanswer['text']; + foreach ($question->answers as $key => $answerdata) { + if (is_array($answerdata)) { + $answerdata = $answerdata['text']; } - if (trim($dataanswer) != '') { + if (trim($answerdata) == '') { + continue; + } + + // Update an existing answer if possible. + $answer = array_shift($oldanswers); + if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; - $answer->answer = trim($dataanswer); - $answer->fraction = $question->fraction[$key]; - $answer->feedbackformat = $question->feedback[$key]['format']; - if (isset($question->feedback[$key]['files'])) { - $files = $question->feedback[$key]['files']; - } + $answer->answer = ''; + $answer->feedback = ''; + $answer->id = $DB->insert_record('question_answers', $answer); + } - if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it - $answer->id = $oldanswer->id; - $answer->feedback = file_save_draft_area_files( - $question->feedback[$key]['itemid'], $context->id, 'question', - 'answerfeedback', $oldanswer->id, $this->fileoptionsa, - trim($question->feedback[$key]['text'])); - $DB->update_record('question_answers', $answer); - } else { // This is a completely new answer - $answer->feedback = trim($question->feedback[$key]['text']); - $answer->id = $DB->insert_record('question_answers', $answer); - if (isset($files)) { - foreach ($files as $file) { - $this->import_file($context, 'question', 'answerfeedback', - $answer->id, $file); - } - } else { - $answer->feedback = file_save_draft_area_files( - $question->feedback[$key]['itemid'], $context->id, 'question', - 'answerfeedback', $answer->id, $this->fileoptionsa , - trim($question->feedback[$key]['text'])); - } - $DB->set_field('question_answers', 'feedback', $answer->feedback, - array('id' => $answer->id)); - } + $answer->answer = trim($answerdata); + $answer->fraction = $question->fraction[$key]; + $answer->feedback = $this->import_or_save_files($question->feedback[$key], + $context, 'question', 'answerfeedback', $answer->id); + $answer->feedbackformat = $question->feedback[$key]['format']; - // Set up the options object - if (!$options = array_shift($oldoptions)) { - $options = new stdClass(); - } - $options->question = $question->id; - $options->answer = $answer->id; - $options->tolerance = trim($question->tolerance[$key]); - $options->tolerancetype = trim($question->tolerancetype[$key]); - $options->correctanswerlength = trim($question->correctanswerlength[$key]); - $options->correctanswerformat = trim($question->correctanswerformat[$key]); + $DB->update_record("question_answers", $answer); - // Save options - if (isset($options->id)) { // reusing existing record - $DB->update_record('question_calculated', $options); - } else { // new options - $DB->insert_record('question_calculated', $options); - } + // Set up the options object + if (!$options = array_shift($oldoptions)) { + $options = new stdClass(); + } + $options->question = $question->id; + $options->answer = $answer->id; + $options->tolerance = trim($question->tolerance[$key]); + $options->tolerancetype = trim($question->tolerancetype[$key]); + $options->correctanswerlength = trim($question->correctanswerlength[$key]); + $options->correctanswerformat = trim($question->correctanswerformat[$key]); + + // Save options + if (isset($options->id)) { + // reusing existing record + $DB->update_record('question_calculated', $options); + } else { + // new options + $DB->insert_record('question_calculated', $options); } } + // delete old answer records if (!empty($oldanswers)) { foreach ($oldanswers as $oa) { @@ -1268,7 +1257,8 @@ class qtype_calculated extends question_type { global $CFG; $sql = "SELECT i.* FROM {question_datasets} d, {question_dataset_definitions} i - WHERE d.question = ? AND d.datasetdefinition = i.id"; + WHERE d.question = ? AND d.datasetdefinition = i.id + ORDER BY i.id"; if ($records = $DB->get_records_sql($sql, array($questionid))) { foreach ($records as $r) { $datasetdefs["$r->type-$r->category-$r->name"] = $r; diff --git a/question/type/calculatedmulti/questiontype.php b/question/type/calculatedmulti/questiontype.php index 91c668db3e5..63914d0f446 100644 --- a/question/type/calculatedmulti/questiontype.php +++ b/question/type/calculatedmulti/questiontype.php @@ -45,55 +45,31 @@ class qtype_calculatedmulti extends qtype_calculated { public function save_question_options($question) { global $CFG, $DB; $context = $question->context; - if (isset($question->answer) && !isset($question->answers)) { - $question->answers = $question->answer; - } - // calculated options + + // Calculated options $update = true; $options = $DB->get_record('question_calculated_options', array('question' => $question->id)); if (!$options) { - $update = false; $options = new stdClass(); $options->question = $question->id; + $options->correctfeedback = ''; + $options->partiallycorrectfeedback = ''; + $options->incorrectfeedback = ''; + $options->id = $DB->insert_record('question_calculated_options', $options); } $options->synchronize = $question->synchronize; $options->single = $question->single; $options->answernumbering = $question->answernumbering; $options->shuffleanswers = $question->shuffleanswers; - - // save question feedback files - foreach (array('correct', 'partiallycorrect', 'incorrect') as $feedbacktype) { - $feedbackname = $feedbacktype . 'feedback'; - $feedbackformat = $feedbackname . 'format'; - $feedback = $question->$feedbackname; - $options->$feedbackformat = $feedback['format']; - if (isset($feedback['files'])) { - $options->$feedbackname = trim($feedback['text']); - $files = $feedback['files']; - foreach ($files as $file) { - $this->import_file($question->context, 'qtype_calculatedmulti', - $feedbackname, $question->id, $file); - } - } else { - $options->$feedbackname = file_save_draft_area_files($feedback['itemid'], - $context->id, 'qtype_calculatedmulti', $feedbackname, - $question->id, $this->fileoptionsa, trim($feedback['text'])); - } - } - - if ($update) { - $DB->update_record('question_calculated_options', $options); - } else { - $DB->insert_record('question_calculated_options', $options); - } + $options = $this->save_combined_feedback_helper($options, $question, $context, true); + $DB->update_record('question_calculated_options', $options); // Get old versions of the objects if (!$oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC')) { $oldanswers = array(); } - if (!$oldoptions = $DB->get_records('question_calculated', array('question' => $question->id), 'answer ASC')) { $oldoptions = array(); @@ -103,80 +79,76 @@ class qtype_calculatedmulti extends qtype_calculated { if (isset($question->answer) && !isset($question->answers)) { $question->answers = $question->answer; } - foreach ($question->answers as $key => $dataanswer) { - if (is_array($dataanswer)) { - $dataanswer = $dataanswer['text']; + foreach ($question->answers as $key => $answerdata) { + if (is_array($answerdata)) { + $answerdata = $answerdata['text']; } - if (trim($dataanswer) != '') { + if (trim($answerdata) == '') { + continue; + } + + // Update an existing answer if possible. + $answer = array_shift($oldanswers); + if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; - $answer->answer = trim($dataanswer); - $answer->fraction = $question->fraction[$key]; - $answer->feedback = trim($question->feedback[$key]['text']); - $answer->feedbackformat = $question->feedback[$key]['format']; - if (isset($question->feedback[$key]['files'])) { - $files = $question->feedback[$key]['files']; - } + $answer->answer = ''; + $answer->feedback = ''; + $answer->id = $DB->insert_record('question_answers', $answer); + } - if ($oldanswer = array_shift($oldanswers)) { - // Existing answer, so reuse it - $answer->id = $oldanswer->id; - $answer->feedback = file_save_draft_area_files( - $question->feedback[$key]['itemid'], $context->id, 'question', - 'answerfeedback', $answer->id, $this->fileoptionsa, $answer->feedback); - $DB->update_record('question_answers', $answer); - } else { - // This is a completely new answer - $answer->id = $DB->insert_record('question_answers', $answer); - if (isset($files)) { - $feedbacktext = $answer->feedback; - foreach ($files as $file) { - $this->import_file($context, 'question', 'answerfeedback', - $answer->id, $file); - } - } else { - $feedbacktext = file_save_draft_area_files( - $question->feedback[$key]['itemid'], $context->id, - 'question', 'answerfeedback', $answer->id, - $this->fileoptionsa, $answer->feedback); - } - $DB->set_field('question_answers', 'feedback', $feedbacktext, - array('id'=>$answer->id)); - } + if (is_array($answerdata)) { + // Doing an import + $answer->answer = $this->import_or_save_files($answerdata, + $context, 'question', 'answer', $answer->id); + $answer->answerformat = $answerdata['format']; + } else { + // Saving the form + $answer->answer = $answerdata; + $answer->answerformat = FORMAT_HTML; + } + $answer->fraction = $question->fraction[$key]; + $answer->feedback = $this->import_or_save_files($question->feedback[$key], + $context, 'question', 'answerfeedback', $answer->id); + $answer->feedbackformat = $question->feedback[$key]['format']; - // Set up the options object - if (!$options = array_shift($oldoptions)) { - $options = new stdClass(); - } - $options->question = $question->id; - $options->answer = $answer->id; - $options->tolerance = trim($question->tolerance[$key]); - $options->tolerancetype = trim($question->tolerancetype[$key]); - $options->correctanswerlength = trim($question->correctanswerlength[$key]); - $options->correctanswerformat = trim($question->correctanswerformat[$key]); + $DB->update_record("question_answers", $answer); - // Save options - if (isset($options->id)) { // reusing existing record - $DB->update_record('question_calculated', $options); - } else { // new options - $DB->insert_record('question_calculated', $options); - } + // Set up the options object + if (!$options = array_shift($oldoptions)) { + $options = new stdClass(); + } + $options->question = $question->id; + $options->answer = $answer->id; + $options->tolerance = trim($question->tolerance[$key]); + $options->tolerancetype = trim($question->tolerancetype[$key]); + $options->correctanswerlength = trim($question->correctanswerlength[$key]); + $options->correctanswerformat = trim($question->correctanswerformat[$key]); + + // Save options + if (isset($options->id)) { + // reusing existing record + $DB->update_record('question_calculated', $options); + } else { + // new options + $DB->insert_record('question_calculated', $options); } } + // delete old answer records if (!empty($oldanswers)) { foreach ($oldanswers as $oa) { $DB->delete_records('question_answers', array('id' => $oa->id)); } } - - // delete old answer records if (!empty($oldoptions)) { foreach ($oldoptions as $oo) { $DB->delete_records('question_calculated', array('id' => $oo->id)); } } + $this->save_hints($question, true); + if (isset($question->import_process) && $question->import_process) { $this->import_datasets($question); } @@ -185,8 +157,6 @@ class qtype_calculatedmulti extends qtype_calculated { return $result; } - $this->save_hints($question, true); - return true; } diff --git a/question/type/calculatedsimple/edit_calculatedsimple_form.php b/question/type/calculatedsimple/edit_calculatedsimple_form.php index ad18e37cee1..f5f5bcd2c85 100644 --- a/question/type/calculatedsimple/edit_calculatedsimple_form.php +++ b/question/type/calculatedsimple/edit_calculatedsimple_form.php @@ -658,6 +658,10 @@ class qtype_calculatedsimple_edit_form extends qtype_calculated_edit_form { public function validation($data, $files) { $errors = parent::validation($data, $files); + if (empty($data['definition'])) { + $errors['selectadd'] = get_string('youmustaddatleastonevalue', 'qtype_calculatedsimple'); + } + return $errors; } } diff --git a/question/type/calculatedsimple/questiontype.php b/question/type/calculatedsimple/questiontype.php index 860274bd08f..8235bda47d9 100644 --- a/question/type/calculatedsimple/questiontype.php +++ b/question/type/calculatedsimple/questiontype.php @@ -72,66 +72,53 @@ class qtype_calculatedsimple extends qtype_calculated { if (isset($question->answer) && !isset($question->answers)) { $question->answers = $question->answer; } - foreach ($question->answers as $key => $dataanswer) { - if (is_array($dataanswer)) { - $dataanswer = $dataanswer['text']; + foreach ($question->answers as $key => $answerdata) { + if (is_array($answerdata)) { + $answerdata = $answerdata['text']; } - if (trim($dataanswer) != '') { + if (trim($answerdata) == '') { + continue; + } + + // Update an existing answer if possible. + $answer = array_shift($oldanswers); + if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; - $answer->answer = trim($dataanswer); - $answer->fraction = $question->fraction[$key]; - $answer->feedbackformat = $question->feedback[$key]['format']; - if (isset($question->feedback[$key]['files'])) { - $files = $question->feedback[$key]['files']; - } + $answer->answer = ''; + $answer->feedback = ''; + $answer->id = $DB->insert_record('question_answers', $answer); + } - if ($oldanswer = array_shift($oldanswers)) { - // Existing answer, so reuse it - $answer->id = $oldanswer->id; - $answer->feedback = file_save_draft_area_files( - $question->feedback[$key]['itemid'], $context->id, 'question', - 'answerfeedback', $answer->id, $this->fileoptionsa, - trim($question->feedback[$key]['text'])); - $DB->update_record("question_answers", $answer); - } else { - // This is a completely new answer - $answer->feedback = trim($question->feedback[$key]['text']); - $answer->id = $DB->insert_record("question_answers", $answer); - if (isset($files)) { - foreach ($files as $file) { - $this->import_file($context, 'question', 'answerfeedback', - $answer->id, $file); - } - } else { - $answer->feedback = file_save_draft_area_files( - $question->feedback[$key]['itemid'], $context->id, 'question', - 'answerfeedback', $answer->id, $this->fileoptionsa, - trim($question->feedback[$key]['text'])); - } - $DB->set_field('question_answers', 'feedback', $answer->feedback, - array('id'=>$answer->id)); - } + $answer->answer = trim($answerdata); + $answer->fraction = $question->fraction[$key]; + $answer->feedback = $this->import_or_save_files($question->feedback[$key], + $context, 'question', 'answerfeedback', $answer->id); + $answer->feedbackformat = $question->feedback[$key]['format']; - // Set up the options object - if (!$options = array_shift($oldoptions)) { - $options = new stdClass(); - } - $options->question = $question->id; - $options->answer = $answer->id; - $options->tolerance = trim($question->tolerance[$key]); - $options->tolerancetype = trim($question->tolerancetype[$key]); - $options->correctanswerlength = trim($question->correctanswerlength[$key]); - $options->correctanswerformat = trim($question->correctanswerformat[$key]); + $DB->update_record("question_answers", $answer); - // Save options - if (isset($options->id)) { // reusing existing record - $DB->update_record('question_calculated', $options); - } else { // new options - $DB->insert_record('question_calculated', $options); - } + // Set up the options object + if (!$options = array_shift($oldoptions)) { + $options = new stdClass(); + } + $options->question = $question->id; + $options->answer = $answer->id; + $options->tolerance = trim($question->tolerance[$key]); + $options->tolerancetype = trim($question->tolerancetype[$key]); + $options->correctanswerlength = trim($question->correctanswerlength[$key]); + $options->correctanswerformat = trim($question->correctanswerformat[$key]); + + // Save options + if (isset($options->id)) { + // reusing existing record + $DB->update_record('question_calculated', $options); + } else { + // new options + $DB->insert_record('question_calculated', $options); } } + // delete old answer records if (!empty($oldanswers)) { foreach ($oldanswers as $oa) { @@ -146,7 +133,7 @@ class qtype_calculatedsimple extends qtype_calculated { } } - if (isset($question->import_process)&&$question->import_process) { + if (isset($question->import_process) && $question->import_process) { $this->import_datasets($question); } else { //save datasets and datatitems from form i.e in question @@ -174,7 +161,7 @@ class qtype_calculatedsimple extends qtype_calculated { continue; } $datasetdef->id = $DB->insert_record('question_dataset_definitions', $datasetdef); - $datasetdefs[]= clone($datasetdef); + $datasetdefs[] = clone($datasetdef); $questiondataset = new stdClass(); $questiondataset->question = $question->id; $questiondataset->datasetdefinition = $datasetdef->id; @@ -225,10 +212,12 @@ class qtype_calculatedsimple extends qtype_calculated { } } } + + $this->save_hints($question); + // Report any problems. - //convert to calculated if (!empty($question->makecopy) && !empty($question->convert)) { - $DB->set_field('question', 'qtype', 'calculated', array('id'=> $question->id)); + $DB->set_field('question', 'qtype', 'calculated', array('id' => $question->id)); } $result = $virtualqtype->save_unit_options($question); @@ -240,8 +229,6 @@ class qtype_calculatedsimple extends qtype_calculated { return $result; } - $this->save_hints($question); - return true; } diff --git a/question/type/multichoice/questiontype.php b/question/type/multichoice/questiontype.php index ae77b50a726..68d00e8de14 100644 --- a/question/type/multichoice/questiontype.php +++ b/question/type/multichoice/questiontype.php @@ -135,7 +135,7 @@ class qtype_multichoice extends question_type { $this->save_hints($question, true); - /// Perform sanity checks on fractional grades + // Perform sanity checks on fractional grades if ($options->single) { if ($maxfraction != 1) { $result->noticeyesno = get_string('fractionsnomax', 'qtype_multichoice',