}
function save_question_options($question) {
-
+ $result = new stdClass;
+
if (!$oldsubquestions = get_records("question_match_sub", "question", $question->id, "id ASC")) {
$oldsubquestions = array();
}
- // following hack to check at least three answers exist
- $answercount = 0;
- foreach ($question->subquestions as $key=>$questiontext) {
- $answertext = $question->subanswers[$key];
- if (!empty($questiontext) or !empty($answertext)) {
- $answercount++;
- }
- }
- $answercount += count($oldsubquestions);
- if ($answercount < 3) { // check there are at lest 3 answers for matching type questions
- $result->notice = get_string("notenoughanswers", "quiz", "3");
- return $result;
- }
-
// $subquestions will be an array with subquestion ids
$subquestions = array();
return $result;
}
} else {
- unset($subquestion);
+ $subquestion = new stdClass;
// Determine a unique random code
$subquestion->code = rand(1,999999999);
while (record_exists('question_match_sub', 'code', $subquestion->code, 'question', $question->id)) {
}
$subquestions[] = $subquestion->id;
}
+ if (!empty($questiontext) && empty($answertext)) {
+ $result->notice = get_string('nomatchinganswer', 'quiz', $questiontext);
+ }
}
// delete old subquestions records
}
}
- if (count($subquestions) < 3) {
- $result->noticeyesno = get_string("notenoughsubquestions", "quiz");
- return $result;
- }
-
if ($options = get_record("question_match", "question", $question->id)) {
$options->subquestions = implode(",",$subquestions);
$options->shuffleanswers = $question->shuffleanswers;
return $result;
}
}
+
+ if (!empty($result->notice)) {
+ return $result;
+ }
+
+ if (count($subquestions) < 3) {
+ $result->notice = get_string('notenoughanswers', 'quiz', 3);
+ return $result;
+ }
+
return true;
}
$responses = array();
foreach ($state->options->subquestions as $sub) {
foreach ($sub->options->answers as $answer) {
- if (1 == $answer->fraction) {
+ if (1 == $answer->fraction && $sub->questiontext) {
$responses[$sub->id] = $answer->id;
}
}
$answers = array();
$responses = &$state->responses;
+ $formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->para = false;
///// Print the input controls //////
foreach ($subquestions as $key => $subquestion) {
- /// Subquestion text:
- $a->text = format_text($subquestion->questiontext,
- $question->questiontextformat, $formatoptions, $cmoptions->course);
-
- /// Drop-down list:
- $menuname = $nameprefix.$subquestion->id;
- $response = isset($state->responses[$subquestion->id])
- ? $state->responses[$subquestion->id] : '0';
- if ($options->readonly
- and $options->correct_responses
- and isset($correctanswers[$subquestion->id])
- and ($correctanswers[$subquestion->id] == $response)) {
- $a->class = ' highlight ';
- } else {
- $a->class = '';
+ if ($subquestion->questiontext) {
+ /// Subquestion text:
+ $a = new stdClass;
+ $a->text = format_text($subquestion->questiontext,
+ $question->questiontextformat, $formatoptions, $cmoptions->course);
+
+ /// Drop-down list:
+ $menuname = $nameprefix.$subquestion->id;
+ $response = isset($state->responses[$subquestion->id])
+ ? $state->responses[$subquestion->id] : '0';
+ if ($options->readonly
+ and $options->correct_responses
+ and isset($correctanswers[$subquestion->id])
+ and ($correctanswers[$subquestion->id] == $response)) {
+ $a->class = ' highlight ';
+ } else {
+ $a->class = '';
+ }
+
+ $a->control = choose_from_menu($answers, $menuname, $response, 'choose', '', 0,
+ true, $options->readonly);
+
+ // Neither the editing interface or the database allow to provide
+ // fedback for this question type.
+ // However (as was pointed out in bug bug 3294) the randomsamatch
+ // type which reuses this method can have feedback defined for
+ // the wrapped shortanswer questions.
+ //if ($options->feedback
+ // && !empty($subquestion->options->answers[$responses[$key]]->feedback)) {
+ // print_comment($subquestion->options->answers[$responses[$key]]->feedback);
+ //}
+
+ $anss[] = $a;
}
-
- $a->control = choose_from_menu($answers, $menuname, $response, 'choose', '', 0,
- true, $options->readonly);
-
- // Neither the editing interface or the database allow to provide
- // fedback for this question type.
- // However (as was pointed out in bug bug 3294) the randomsamatch
- // type which reuses this method can have feedback defined for
- // the wrapped shortanswer questions.
- //if ($options->feedback
- // && !empty($subquestion->options->answers[$responses[$key]]->feedback)) {
- // print_comment($subquestion->options->answers[$responses[$key]]->feedback);
- //}
-
- $anss[] = clone($a);
}
include("$CFG->dirroot/question/type/match/display.html");
}
$responses = &$state->responses;
$sumgrade = 0;
+ $totalgrade = 0;
foreach ($subquestions as $key => $sub) {
- if (isset($sub->options->answers[$responses[$key]])) {
- $sumgrade += $sub->options->answers[$responses[$key]]->fraction;
+ if ($sub->questiontext) {
+ $totalgrade += 1;
+ if (isset($sub->options->answers[$responses[$key]])) {
+ $sumgrade += $sub->options->answers[$responses[$key]]->fraction;
+ }
}
}
- $state->raw_grade = $sumgrade/count($subquestions);
+ $state->raw_grade = $sumgrade/$totalgrade;
if (empty($state->raw_grade)) {
$state->raw_grade = 0;
}
// ULPGC ecastro for stats report
function get_all_responses($question, $state) {
- unset($answers);
+ $answers = new stdClass;
if (is_array($question->options->subquestions)) {
foreach ($question->options->subquestions as $aid=>$answer) {
- unset ($r);
+ $r = new stdClass;
$r->answer = $answer->questiontext." : ".$answer->answertext;
$r->credit = 1;
$answers[$aid] = $r;
} else {
$answers[]="error"; // just for debugging, eliminate
}
+ $result = new stdClass;
$result->id = $question->id;
$result->responses = $answers;
return $result;
$oldid = backup_todb($mat_info['#']['ID']['0']['#']);
//Now, build the question_match_SUB record structure
+ $match_sub = new stdClass;
$match_sub->question = $new_question_id;
$match_sub->code = backup_todb($mat_info['#']['CODE']['0']['#']);
if (!$match_sub->code) {