public function check_answer() { global $CFG; $result = parent::check_answer(); $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents())); $data = $mform->get_data(); require_sesskey(); $formattextdefoptions = new stdClass(); $formattextdefoptions->noclean = true; $formattextdefoptions->para = false; // set defaults $result->response = ''; $result->newpageid = 0; if (!isset($data->answer) || !is_numeric($data->answer)) { $result->noanswer = true; return $result; } else { // Just doing default PARAM_RAW, not doing PARAM_INT because it could be a float. $result->useranswer = (float)$data->answer; } $result->studentanswer = $result->userresponse = $result->useranswer; $answers = $this->get_answers(); foreach ($answers as $answer) { $answer = parent::rewrite_answers_urls($answer); if (strpos($answer->answer, ':')) { // there's a pairs of values list($min, $max) = explode(':', $answer->answer); $minimum = (float) $min; $maximum = (float) $max; } else { // there's only one value $minimum = (float) $answer->answer; $maximum = $minimum; } if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) { $result->newpageid = $answer->jumpto; $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions); if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) { $result->correctanswer = true; } if ($this->lesson->custom) { if ($answer->score > 0) { $result->correctanswer = true; } else { $result->correctanswer = false; } } $result->answerid = $answer->id; return $result; } } return $result; }
public function check_answer() { global $CFG; $result = parent::check_answer(); $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot . '/mod/lesson/continue.php', array('contents' => $this->get_contents())); $data = $mform->get_data(); require_sesskey(); $studentanswer = trim($data->answer); if ($studentanswer === '') { $result->noanswer = true; return $result; } $i = 0; $answers = $this->get_answers(); foreach ($answers as $answer) { $answer = parent::rewrite_answers_urls($answer, false); $i++; // Applying PARAM_TEXT as it is applied to the answer submitted by the user. $expectedanswer = clean_param($answer->answer, PARAM_TEXT); $ismatch = false; $markit = false; $useregexp = $this->qoption; if ($useregexp) { //we are using 'normal analysis', which ignores case $ignorecase = ''; if (substr($expectedanswer, -2) == '/i') { $expectedanswer = substr($expectedanswer, 0, -2); $ignorecase = 'i'; } } else { $expectedanswer = str_replace('*', '#####', $expectedanswer); $expectedanswer = preg_quote($expectedanswer, '/'); $expectedanswer = str_replace('#####', '.*', $expectedanswer); } // see if user typed in any of the correct answers if (!$this->lesson->custom && $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) or $this->lesson->custom && $answer->score > 0) { if (!$useregexp) { // we are using 'normal analysis', which ignores case if (preg_match('/^' . $expectedanswer . '$/i', $studentanswer)) { $ismatch = true; } } else { if (preg_match('/^' . $expectedanswer . '$/' . $ignorecase, $studentanswer)) { $ismatch = true; } } if ($ismatch == true) { $result->correctanswer = true; } } else { if (!$useregexp) { //we are using 'normal analysis' // see if user typed in any of the wrong answers; don't worry about case if (preg_match('/^' . $expectedanswer . '$/i', $studentanswer)) { $ismatch = true; } } else { // we are using regular expressions analysis $startcode = substr($expectedanswer, 0, 2); switch ($startcode) { //1- check for absence of required string in $studentanswer (coded by initial '--') case "--": $expectedanswer = substr($expectedanswer, 2); if (!preg_match('/^' . $expectedanswer . '$/' . $ignorecase, $studentanswer)) { $ismatch = true; } break; //2- check for code for marking wrong strings (coded by initial '++') //2- check for code for marking wrong strings (coded by initial '++') case "++": $expectedanswer = substr($expectedanswer, 2); $markit = true; //check for one or several matches if (preg_match_all('/' . $expectedanswer . '/' . $ignorecase, $studentanswer, $matches)) { $ismatch = true; $nb = count($matches[0]); $original = array(); $marked = array(); $fontStart = '<span class="incorrect matches">'; $fontEnd = '</span>'; for ($i = 0; $i < $nb; $i++) { array_push($original, $matches[0][$i]); array_push($marked, $fontStart . $matches[0][$i] . $fontEnd); } $studentanswer = str_replace($original, $marked, $studentanswer); } break; //3- check for wrong answers belonging neither to -- nor to ++ categories //3- check for wrong answers belonging neither to -- nor to ++ categories default: if (preg_match('/^' . $expectedanswer . '$/' . $ignorecase, $studentanswer, $matches)) { $ismatch = true; } break; } $result->correctanswer = false; } } if ($ismatch) { $result->newpageid = $answer->jumpto; $options = new stdClass(); $options->para = false; $result->response = format_text($answer->response, $answer->responseformat, $options); $result->answerid = $answer->id; break; // quit answer analysis immediately after a match has been found } } $result->userresponse = $studentanswer; //clean student answer as it goes to output. $result->studentanswer = s($studentanswer); return $result; }