Exemplo n.º 1
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;
 }