From 857caf3b69ae2cd946bc72739c2d6e196c45167c Mon Sep 17 00:00:00 2001 From: pichetp Date: Sun, 15 Jun 2008 03:47:04 +0000 Subject: [PATCH] MDL-14625 more tolerant to missing subquestions --- question/type/multianswer/questiontype.php | 50 +++++++++++++--------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/question/type/multianswer/questiontype.php b/question/type/multianswer/questiontype.php index a860943566c..b92324f8a60 100644 --- a/question/type/multianswer/questiontype.php +++ b/question/type/multianswer/questiontype.php @@ -28,8 +28,9 @@ class embedded_cloze_qtype extends default_questiontype { // Get relevant data indexed by positionkey from the multianswers table if (!$sequence = $DB->get_field('question_multianswer', 'sequence', array('question' => $question->id))) { - notify('Error: Cloze question '.$question->id.' is missing question options!'); - return false; + notify(get_string('noquestions','qtype_multianswer',$question->name)); + $question->options->questions['1']= ''; + return true ; } $wrappedquestions = $DB->get_records_list('question', 'id', explode(',', $sequence), 'id ASC'); @@ -37,7 +38,12 @@ class embedded_cloze_qtype extends default_questiontype { // We want an array with question ids as index and the positions as values $sequence = array_flip(explode(',', $sequence)); array_walk($sequence, create_function('&$val', '$val++;')); - //si une question est manquante l,indice est nul + //If a question is lost, the corresponding index is null + // so this null convention is used to test $question->options->questions + // before using the values. + // first all possible questions from sequence are nulled + // then filled with the data if available in $wrappedquestions + $nbvaliquestion = 0 ; foreach($sequence as $seq){ $question->options->questions[$seq]= ''; } @@ -45,14 +51,18 @@ class embedded_cloze_qtype extends default_questiontype { foreach ($wrappedquestions as $wrapped) { if (!$QTYPES[$wrapped->qtype]->get_question_options($wrapped)) { notify("Unable to get options for questiontype {$wrapped->qtype} (id={$wrapped->id})"); - } + }else { // for wrapped questions the maxgrade is always equal to the defaultgrade, // there is no entry in the question_instances table for them $wrapped->maxgrade = $wrapped->defaultgrade; - + $nbvaliquestion++ ; $question->options->questions[$sequence[$wrapped->id]] = clone($wrapped); // ??? Why do we need a clone here? } } + } + if ($nbvaliquestion == 0 ) { + notify(get_string('noquestions','qtype_multianswer',$question->name)); + } return true; } @@ -70,36 +80,31 @@ class embedded_cloze_qtype extends default_questiontype { // First we get all the existing wrapped questions if (!$oldwrappedids = $DB->get_field('question_multianswer', 'sequence', array('question' => $question->id))) { - $oldwrappedids = array(); + $oldwrappedquestions = array(); } else { - $oldwrappedids = $DB->get_records_list('question', 'id', explode(',', $oldwrappedids), 'id ASC','id'); + $oldwrappedquestions = $DB->get_records_list('question', 'id', explode(',', $oldwrappedids), 'id ASC'); } $sequence = array(); foreach($question->options->questions as $wrapped) { if ($wrapped != ''){ // if we still have some old wrapped question ids, reuse the next of them - if (is_array($oldwrappedids) && $oldwrappedid = array_shift($oldwrappedids)) { - if( $oldqtype = $DB->get_field('question', 'qtype', array('id' =>$oldwrappedid->id))){ - $wrapped->id = $oldwrappedid->id; - if($oldqtype != $wrapped->qtype ) { - switch ($oldqtype) { + if (is_array($oldwrappedquestions) && $oldwrappedquestion = array_shift($oldwrappedquestions)) { + $wrapped->id = $oldwrappedquestion->id; + if($oldwrappedquestion->qtype != $wrapped->qtype ) { + switch ($oldwrappedquestion->qtype) { case 'multichoice': - $DB->delete_records('question_multichoice', array('question' => $oldwrappedid)); - $wrapped->id = $oldwrappedid; + $DB->delete_records('question_multichoice', array('question' => $oldwrappedquestion->id)); break; case 'shortanswer': - $DB->delete_records('question_shortanswer', array('question' => $oldwrappedid)); - $wrapped->id = $oldwrappedid; + $DB->delete_records('question_shortanswer', array('question' => $oldwrappedquestion->id)); break; case 'numerical': - $DB->delete_records('question_numerical', array('question' => $oldwrappedid)); - $wrapped->id = $oldwrappedid; + $DB->delete_records('question_numerical', array('question' => $oldwrappedquestion->id)); break; default: - error("questiontype $wrapped->qtype not recognized"); + print_error('qtypenotrecognized', 'qtype_multianswer','',$oldwrappedquestion->qtype); $wrapped->id = 0 ; - } } } }else { @@ -441,7 +446,10 @@ class embedded_cloze_qtype extends default_questiontype { echo $feedbackimg; break; default: - print_error('unknownquestiontype', 'question'); + $a = new stdClass; + $a->type = $wrapped->qtype ; + $a->sub = $positionkey; + print_error('unknownquestiontypeofsubquestion', 'qtype_multianswer','',$a); break; } echo ""; // MDL-7497