Пример #1
0
 public function check_answer()
 {
     global $DB, $CFG;
     $formattextdefoptions = new stdClass();
     $formattextdefoptions->noclean = true;
     $formattextdefoptions->para = false;
     $answers = $this->get_answers();
     shuffle($answers);
     $params = array('answers' => $answers, 'lessonid' => $this->lesson->id, 'contents' => $this->get_contents());
     $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot . '/mod/lesson/continue.php', $params);
     $data = $mform->get_data();
     require_sesskey();
     $result = parent::check_answer();
     if (empty($data->answerid)) {
         $result->noanswer = true;
         return $result;
     }
     $result->answerid = $data->answerid;
     $answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST);
     if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
         $result->correctanswer = true;
     }
     if ($this->lesson->custom) {
         if ($answer->score > 0) {
             $result->correctanswer = true;
         } else {
             $result->correctanswer = false;
         }
     }
     $result->newpageid = $answer->jumpto;
     $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
     $result->studentanswer = $result->userresponse = $answer->answer;
     return $result;
 }
Пример #2
0
 public function check_answer()
 {
     global $PAGE, $CFG;
     $result = parent::check_answer();
     $result->isessayquestion = true;
     $mform = new lesson_display_answer_form_essay($CFG->wwwroot . '/mod/lesson/continue.php', array('contents' => $this->get_contents()));
     $data = $mform->get_data();
     require_sesskey();
     if (!$data) {
         redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $this->properties->id)));
     }
     $studentanswer = $data->answer['text'];
     if (trim($studentanswer) === '') {
         $result->noanswer = true;
         return $result;
     }
     $answers = $this->get_answers();
     foreach ($answers as $answer) {
         $result->answerid = $answer->id;
         $result->newpageid = $answer->jumpto;
     }
     $userresponse = new stdClass();
     $userresponse->sent = 0;
     $userresponse->graded = 0;
     $userresponse->score = 0;
     $userresponse->answer = $studentanswer;
     $userresponse->response = "";
     $result->userresponse = serialize($userresponse);
     $result->studentanswer = s($studentanswer);
     return $result;
 }
Пример #3
0
    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;
    }
Пример #4
0
 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;
 }
Пример #5
0
 public function check_answer()
 {
     global $CFG, $PAGE;
     $formattextdefoptions = new stdClass();
     $formattextdefoptions->noclean = true;
     $formattextdefoptions->para = false;
     $result = parent::check_answer();
     $mform = $this->make_answer_form();
     $data = $mform->get_data();
     require_sesskey();
     if (!$data) {
         redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $this->properties->id)));
     }
     $response = $data->response;
     $getanswers = $this->get_answers();
     foreach ($getanswers as $key => $answer) {
         $getanswers[$key] = parent::rewrite_answers_urls($answer);
     }
     $correct = array_shift($getanswers);
     $wrong = array_shift($getanswers);
     $answers = array();
     foreach ($getanswers as $key => $answer) {
         if ($answer->answer !== '' or $answer->response !== '') {
             $answers[$answer->id] = $answer;
         }
     }
     // get the user's exact responses for record keeping
     $hits = 0;
     $userresponse = array();
     foreach ($response as $id => $value) {
         if ($value == '') {
             $result->noanswer = true;
             return $result;
         }
         $value = htmlspecialchars_decode($value);
         $userresponse[] = $value;
         // Make sure the user's answer exists in question's answer
         if (array_key_exists($id, $answers)) {
             $answer = $answers[$id];
             $result->studentanswer .= '<br />' . format_text($answer->answer, $answer->answerformat, $formattextdefoptions) . ' = ' . $value;
             if (trim($answer->response) == trim($value)) {
                 $hits++;
             }
         }
     }
     $result->userresponse = implode(",", $userresponse);
     if ($hits == count($answers)) {
         $result->correctanswer = true;
         $result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions);
         $result->answerid = $correct->id;
         $result->newpageid = $correct->jumpto;
     } else {
         $result->correctanswer = false;
         $result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions);
         $result->answerid = $wrong->id;
         $result->newpageid = $wrong->jumpto;
     }
     return $result;
 }
Пример #6
0
 public function check_answer()
 {
     global $DB, $CFG, $PAGE;
     $result = parent::check_answer();
     $formattextdefoptions = new stdClass();
     $formattextdefoptions->noclean = true;
     $formattextdefoptions->para = false;
     $answers = $this->get_used_answers();
     shuffle($answers);
     $action = $CFG->wwwroot . '/mod/lesson/continue.php';
     $params = array('answers' => $answers, 'lessonid' => $this->lesson->id, 'contents' => $this->get_contents());
     if ($this->properties->qoption) {
         $mform = new lesson_display_answer_form_multichoice_multianswer($action, $params);
     } else {
         $mform = new lesson_display_answer_form_multichoice_singleanswer($action, $params);
     }
     $data = $mform->get_data();
     require_sesskey();
     if (!$data) {
         redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $this->properties->id)));
     }
     if ($this->properties->qoption) {
         // MULTIANSWER allowed, user's answer is an array
         if (empty($data->answer) || !is_array($data->answer)) {
             $result->noanswer = true;
             return $result;
         }
         $studentanswers = array();
         foreach ($data->answer as $key => $value) {
             $studentanswers[] = (int) $key;
         }
         // get what the user answered
         $result->userresponse = implode(",", $studentanswers);
         // get the answers in a set order, the id order
         $answers = $this->get_used_answers();
         $ncorrect = 0;
         $nhits = 0;
         $correctresponse = '';
         $wrongresponse = '';
         $correctanswerid = 0;
         $wronganswerid = 0;
         // store student's answers for displaying on feedback page
         $result->studentanswer = '';
         foreach ($answers as $answer) {
             foreach ($studentanswers as $answerid) {
                 if ($answerid == $answer->id) {
                     $result->studentanswer .= '<br />' . format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
                 }
             }
         }
         $correctpageid = null;
         $wrongpageid = null;
         // this is for custom scores.  If score on answer is positive, it is correct
         if ($this->lesson->custom) {
             $ncorrect = 0;
             $nhits = 0;
             foreach ($answers as $answer) {
                 if ($answer->score > 0) {
                     $ncorrect++;
                     foreach ($studentanswers as $answerid) {
                         if ($answerid == $answer->id) {
                             $nhits++;
                         }
                     }
                     // save the first jumpto page id, may be needed!...
                     if (!isset($correctpageid)) {
                         // leave in its "raw" state - will converted into a proper page id later
                         $correctpageid = $answer->jumpto;
                     }
                     // save the answer id for scoring
                     if ($correctanswerid == 0) {
                         $correctanswerid = $answer->id;
                     }
                     // ...also save any response from the correct answers...
                     if (trim(strip_tags($answer->response))) {
                         $correctresponse = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
                     }
                 } else {
                     // save the first jumpto page id, may be needed!...
                     if (!isset($wrongpageid)) {
                         // leave in its "raw" state - will converted into a proper page id later
                         $wrongpageid = $answer->jumpto;
                     }
                     // save the answer id for scoring
                     if ($wronganswerid == 0) {
                         $wronganswerid = $answer->id;
                     }
                     // ...and from the incorrect ones, don't know which to use at this stage
                     if (trim(strip_tags($answer->response))) {
                         $wrongresponse = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
                     }
                 }
             }
         } else {
             foreach ($answers as $answer) {
                 if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
                     $ncorrect++;
                     foreach ($studentanswers as $answerid) {
                         if ($answerid == $answer->id) {
                             $nhits++;
                         }
                     }
                     // save the first jumpto page id, may be needed!...
                     if (!isset($correctpageid)) {
                         // leave in its "raw" state - will converted into a proper page id later
                         $correctpageid = $answer->jumpto;
                     }
                     // save the answer id for scoring
                     if ($correctanswerid == 0) {
                         $correctanswerid = $answer->id;
                     }
                     // ...also save any response from the correct answers...
                     if (trim(strip_tags($answer->response))) {
                         $correctresponse = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
                     }
                 } else {
                     // save the first jumpto page id, may be needed!...
                     if (!isset($wrongpageid)) {
                         // leave in its "raw" state - will converted into a proper page id later
                         $wrongpageid = $answer->jumpto;
                     }
                     // save the answer id for scoring
                     if ($wronganswerid == 0) {
                         $wronganswerid = $answer->id;
                     }
                     // ...and from the incorrect ones, don't know which to use at this stage
                     if (trim(strip_tags($answer->response))) {
                         $wrongresponse = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
                     }
                 }
             }
         }
         if (count($studentanswers) == $ncorrect and $nhits == $ncorrect) {
             $result->correctanswer = true;
             $result->response = $correctresponse;
             $result->newpageid = $correctpageid;
             $result->answerid = $correctanswerid;
         } else {
             $result->response = $wrongresponse;
             $result->newpageid = $wrongpageid;
             $result->answerid = $wronganswerid;
         }
     } else {
         // only one answer allowed
         if (empty($data->answerid) && !is_int($data->answerid)) {
             $result->noanswer = true;
             return $result;
         }
         $result->answerid = $data->answerid;
         if (!($answer = $DB->get_record("lesson_answers", array("id" => $result->answerid)))) {
             print_error("Continue: answer record not found");
         }
         if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
             $result->correctanswer = true;
         }
         if ($this->lesson->custom) {
             if ($answer->score > 0) {
                 $result->correctanswer = true;
             } else {
                 $result->correctanswer = false;
             }
         }
         $result->newpageid = $answer->jumpto;
         $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
         $result->userresponse = format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
         $result->studentanswer = $result->userresponse;
     }
     return $result;
 }
Пример #7
0
 public function check_answer()
 {
     global $CFG, $PAGE;
     $formattextdefoptions = new stdClass();
     $formattextdefoptions->noclean = true;
     $formattextdefoptions->para = false;
     $result = parent::check_answer();
     $mform = $this->make_answer_form();
     $data = $mform->get_data();
     require_sesskey();
     if (!$data) {
         redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $this->properties->id)));
     }
     $response = $data->response;
     if (!is_array($response)) {
         $result->noanswer = true;
         return $result;
     }
     $answers = $this->get_answers();
     $correct = array_shift($answers);
     $wrong = array_shift($answers);
     foreach ($answers as $key => $answer) {
         if ($answer->answer === '' or $answer->response === '') {
             // incomplete option!
             unset($answers[$key]);
         }
     }
     // get he users exact responses for record keeping
     $hits = 0;
     $userresponse = array();
     foreach ($response as $key => $value) {
         foreach ($answers as $answer) {
             if ($value === $answer->response) {
                 $userresponse[] = $answer->id;
             }
             if ((int) $answer->id === (int) $key) {
                 $result->studentanswer .= '<br />' . format_text($answer->answer, $answer->answerformat, $formattextdefoptions) . ' = ' . $value;
             }
             if ((int) $answer->id === (int) $key and $value === $answer->response) {
                 $hits++;
             }
         }
     }
     $result->userresponse = implode(",", $userresponse);
     if ($hits == count($answers)) {
         $result->correctanswer = true;
         $result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions);
         $result->answerid = $correct->id;
         $result->newpageid = $correct->jumpto;
     } else {
         $result->correctanswer = false;
         $result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions);
         $result->answerid = $wrong->id;
         $result->newpageid = $wrong->jumpto;
     }
     return $result;
 }