Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge from head MDL-14054 Display of correct answers in the popup window

already use for feedback and MDL-14625 Make multianswer questions more tolerant if one of the subquestions
is missing
  • Loading branch information...
commit 9f13517f8feb2c61b5a63ccb3197c9f292e48237 1 parent 0654809
pichetp authored
View
4 lang/en_utf8/qtype_multianswer.php
@@ -1,3 +1,7 @@
<?php
+$string['correctanswer'] = 'Correct Answer';
+$string['correctanswerandfeedback'] = 'Correct Answer and Feedback';
$string['decodeverifyquestiontext'] = 'Decode and Verify the Question Text';
+$string['questionnotfound'] = 'Unable to find question of question part #$a';
+$string['questionsmissing'] = 'No valid questions, create at least one question ';
?>
View
17 question/type/multianswer/edit_multianswer_form.php
@@ -33,8 +33,13 @@ function definition_inner(&$mform) {
$this->questiondisplay = "";
}
- if ( isset($this->questiondisplay->options->questions) && count($this->questiondisplay->options->questions) > 0 ) {
- $countsubquestions = count($this->questiondisplay->options->questions);
+ if ( isset($this->questiondisplay->options->questions) && is_array($this->questiondisplay->options->questions) ) {
+ $countsubquestions =0;
+ foreach($this->questiondisplay->options->questions as $subquestion){
+ if ($subquestion != ''){
+ $countsubquestions++;
+ }
+ }
} else {
$countsubquestions =0;
}
@@ -84,6 +89,7 @@ function set_data($question) {
if (isset($question->id) and $question->id and $question->qtype and $question->questiontext) {
foreach ($question->options->questions as $key => $wrapped) {
+ if($wrapped != ''){
// The old way of restoring the definitions is kept to gradually
// update all multianswer questions
if (empty($wrapped->questiontext)) {
@@ -99,7 +105,7 @@ function set_data($question) {
$parsableanswerdef .= 'NUMERICAL:';
break;
default:
- error("questiontype $wrapped->qtype not recognized");
+ print_error('unknownquestiontype', 'question', '', $wrapped->qtype);
}
$separator= '';
foreach ($wrapped->options->answers as $subanswer) {
@@ -127,6 +133,7 @@ function set_data($question) {
$question->questiontext = str_replace("{#$key}", $parsableanswerdef, $question->questiontext);
}
}
+ }
// set default to $questiondisplay questions elements
if (isset($this->questiondisplay->options->questions)) {
@@ -231,7 +238,7 @@ function validation($data, $files) {
$sub++;
}
} else {
- $errors['questiontext']=get_string('questions missing', 'question');
+ $errors['questiontext']=get_string('questionsmissing', 'qtype_multianswer');
}
}
@@ -242,4 +249,4 @@ function qtype() {
return 'multianswer';
}
}
-?>
+?>
View
94 question/type/multianswer/questiontype.php
@@ -37,7 +37,11 @@ function get_question_options(&$question) {
// 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
+ foreach($sequence as $seq){
+ $question->options->questions[$seq]= '';
+ }
+ if (isset($wrappedquestions) && is_array($wrappedquestions)){
foreach ($wrappedquestions as $wrapped) {
if (!$QTYPES[$wrapped->qtype]->get_question_options($wrapped)) {
notify("Unable to get options for questiontype {$wrapped->qtype} (id={$wrapped->id})");
@@ -48,6 +52,7 @@ function get_question_options(&$question) {
$question->options->questions[$sequence[$wrapped->id]] = clone($wrapped); // ??? Why do we need a clone here?
}
+ }
return true;
}
@@ -67,28 +72,38 @@ function save_question_options($question) {
if (!$oldwrappedids = get_field('question_multianswer', 'sequence', 'question', $question->id)) {
$oldwrappedids = array();
} else {
- $oldwrappedids = explode(',', $oldwrappedids);
+ $oldwrappedids = get_records_list('question', 'id', $oldwrappedids, 'id ASC','id');
}
$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 ($oldwrappedid = array_shift($oldwrappedids)) {
- $wrapped->id = $oldwrappedid;
- $oldqtype = get_field('question', 'qtype', 'id',$oldwrappedid) ;
+ if (is_array($oldwrappedids) && $oldwrappedid = array_shift($oldwrappedids)) {
+
+ if( $oldqtype = get_field('question', 'qtype', 'id',$oldwrappedid->id)){
+ $wrapped->id = $oldwrappedid->id;
if($oldqtype != $wrapped->qtype ) {
switch ($oldqtype) {
case 'multichoice':
delete_records('question_multichoice', 'question', $oldwrappedid);
+ $wrapped->id = $oldwrappedid;
break;
case 'shortanswer':
delete_records('question_shortanswer', 'question', $oldwrappedid);
+ $wrapped->id = $oldwrappedid;
break;
case 'numerical':
delete_records('question_numerical', 'question', $oldwrappedid);
+ $wrapped->id = $oldwrappedid;
break;
default:
error("questiontype $wrapped->qtype not recognized");
+ $wrapped->id = 0 ;
+ }
}
+ }
+ }else {
+ $wrapped->id = 0 ;
}
}
$wrapped->name = $question->name;
@@ -197,6 +212,7 @@ function get_correct_responses(&$question, &$state) {
global $QTYPES;
$responses = array();
foreach($question->options->questions as $key => $wrapped) {
+ if ($wrapped != ''){
if ($correct = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $state)) {
$responses[$key] = $correct[''];
} else {
@@ -206,6 +222,7 @@ function get_correct_responses(&$question, &$state) {
return null;
}
}
+ }
return $responses;
}
@@ -247,9 +264,10 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
$qtextremaining = $qtextsplits[1];
$positionkey = $regs[1];
+ if (isset($question->options->questions[$positionkey]) && $question->options->questions[$positionkey] != ''){
$wrapped = &$question->options->questions[$positionkey];
$answers = &$wrapped->options->answers;
- $correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $state);
+ // $correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $state);
$inputname = $nameprefix.$positionkey;
if (isset($state->responses[$positionkey])) {
@@ -262,6 +280,38 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
$popup = '';
$style = '';
$feedbackimg = '';
+ $feedback = '' ;
+ $correctanswer = '';
+ $strfeedbackwrapped = $strfeedback;
+ // if($wrapped->qtype == 'numerical' ||$wrapped->qtype == 'shortanswer'){
+ $testedstate = clone($state);
+ if ($correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $testedstate)) {
+ if ($options->readonly && $options->correct_responses) {
+ $delimiter = '';
+ if ($correctanswers) {
+ foreach ($correctanswers as $ca) {
+ switch($wrapped->qtype){
+ case 'numerical':
+ case 'shortanswer':
+ $correctanswer .= $delimiter.$ca;
+ break ;
+ case 'multichoice':
+ if (isset($answers[$ca])){
+ $correctanswer .= $delimiter.$answers[$ca]->answer;
+ }
+ break ;
+ }
+ $delimiter = ', ';
+ }
+ }
+ }
+ if ($correctanswer) {
+ $feedback = '<div class="correctness">';
+ $feedback .= get_string('correctansweris', 'quiz', s($correctanswer, true));
+ $feedback .= '</div>';
+ // $strfeedbackwrapped = get_string('correctanswer and', 'quiz').get_string('feedback', 'quiz');
+ }
+ }
if ($options->feedback) {
$chosenanswer = null;
switch ($wrapped->qtype) {
@@ -294,8 +344,13 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
}
if (!empty($chosenanswer->feedback)) {
- $feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $chosenanswer->feedback));
- $popup = " onmouseover=\"return overlib('$feedback', STICKY, MOUSEOFF, CAPTION, '$strfeedback', FGCOLOR, '#FFFFFF');\" ".
+ $feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $feedback.$chosenanswer->feedback));
+ if ($options->readonly && $options->correct_responses) {
+ $strfeedbackwrapped = get_string('correctanswerandfeedback', 'qtype_multianswer');
+ }else {
+ $strfeedbackwrapped = get_string('feedback', 'quiz');
+ }
+ $popup = " onmouseover=\"return overlib('$feedback', STICKY, MOUSEOFF, CAPTION, '$strfeedbackwrapped', FGCOLOR, '#FFFFFF');\" ".
" onmouseout=\"return nd();\" ";
}
@@ -308,6 +363,12 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
$feedbackimg = '';
}
}
+ if ($feedback !='' && $popup == ''){
+ $strfeedbackwrapped = get_string('correctanswer', 'qtype_multianswer');
+ $feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $feedback));
+ $popup = " onmouseover=\"return overlib('$feedback', STICKY, MOUSEOFF, CAPTION, '$strfeedbackwrapped', FGCOLOR, '#FFFFFF');\" ".
+ " onmouseout=\"return nd();\" ";
+ }
// Print the input control
switch ($wrapped->qtype) {
@@ -378,12 +439,19 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
echo $feedbackimg;
break;
default:
- error("Unable to recognize questiontype ($wrapped->qtype) of
- question part $positionkey.");
+ print_error('unknownquestiontype', 'question');
break;
}
echo "</label>"; // MDL-7497
}
+ else {
+ if(! isset($question->options->questions[$positionkey])){
+ echo $regs[0];
+ }else {
+ echo '<div class="error" >'.get_string('questionnotfound','qtype_multianswer',$positionkey).'</div>';
+ }
+ }
+ }
// Print the final piece of question text:
echo $qtextremaining;
@@ -396,6 +464,7 @@ function grade_responses(&$question, &$state, $cmoptions) {
$teststate = clone($state);
$state->raw_grade = 0;
foreach($question->options->questions as $key => $wrapped) {
+ if ($wrapped != ''){
$state->responses[$key] = $state->responses[$key];
$teststate->responses = array('' => $state->responses[$key]);
$teststate->raw_grade = 0;
@@ -405,6 +474,7 @@ function grade_responses(&$question, &$state, $cmoptions) {
}
$state->raw_grade += $teststate->raw_grade;
}
+ }
$state->raw_grade /= $question->defaultgrade;
$state->raw_grade = min(max((float) $state->raw_grade, 0.0), 1.0)
* $question->maxgrade;
@@ -602,7 +672,7 @@ function restore_recode_answer($state, $restore) {
$answer = $exploded[1];
// $sequence is an ordered array of the question ids.
if (!$sequence = get_field('question_multianswer', 'sequence', 'question', $state->question)) {
- error("The cloze question $state->question is missing its options");
+ print_error('missingoption', 'question', '', $state->question);
}
$sequence = explode(',', $sequence);
// The id of the current question.
@@ -729,7 +799,7 @@ function qtype_multianswer_extract_question($text) {
$wrapped->partiallycorrectfeedback = '';
$wrapped->incorrectfeedback = '';
} else {
- error("Cannot identify qtype $answerregs[2]");
+ print_error('unknownquestiontype', 'question', '', $answerregs[2]);
return false;
}
Please sign in to comment.
Something went wrong with that request. Please try again.