.
/**
* Multichoice
*
* @package mod_lesson
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
defined('MOODLE_INTERNAL') || die();
/** Multichoice question type */
define("LESSON_PAGE_MULTICHOICE", "3");
class lesson_page_type_multichoice extends lesson_page {
protected $type = lesson_page::TYPE_QUESTION;
protected $typeidstring = 'multichoice';
protected $typeid = LESSON_PAGE_MULTICHOICE;
protected $string = null;
public function get_typeid() {
return $this->typeid;
}
public function get_typestring() {
if ($this->string===null) {
$this->string = get_string($this->typeidstring, 'lesson');
}
return $this->string;
}
public function get_idstring() {
return $this->typeidstring;
}
/**
* Gets an array of the jumps used by the answers of this page
*
* @return array
*/
public function get_jumps() {
global $DB;
$jumps = array();
if ($answers = $this->get_answers()) {
foreach ($answers as $answer) {
if ($answer->answer === '') {
// show only jumps for real branches (==have description)
continue;
}
$jumps[] = $this->get_jump_name($answer->jumpto);
}
} else {
// We get here is the lesson was created on a Moodle 1.9 site and
// the lesson contains question pages without any answers.
$jumps[] = $this->get_jump_name($this->properties->nextpageid);
}
return $jumps;
}
public function get_used_answers() {
$answers = $this->get_answers();
foreach ($answers as $key=>$answer) {
if ($answer->answer === '') {
unset($answers[$key]);
} else {
$answers[$key] = parent::rewrite_answers_urls($answer);
}
}
return $answers;
}
public function display($renderer, $attempt) {
global $CFG, $PAGE;
$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(), 'attempt'=>$attempt);
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 = new stdClass;
$data->id = $PAGE->cm->id;
$data->pageid = $this->properties->id;
$mform->set_data($data);
return $mform->display();
}
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;
$responses = array();
$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 .= '
'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
if (trim(strip_tags($answer->response))) {
$responses[$answerid] = format_text($answer->response, $answer->responseformat, $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;
}
} 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;
}
}
}
} 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;
}
} 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;
}
}
}
}
if ((count($studentanswers) == $ncorrect) and ($nhits == $ncorrect)) {
$result->correctanswer = true;
$result->response = implode('
', $responses);
$result->newpageid = $correctpageid;
$result->answerid = $correctanswerid;
} else {
$result->response = implode('
', $responses);
$result->newpageid = $wrongpageid;
$result->answerid = $wronganswerid;
}
} else {
// only one answer allowed
if (!isset($data->answerid) || (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");
}
$answer = parent::rewrite_answers_urls($answer);
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;
}
public function option_description_string() {
if ($this->properties->qoption) {
return " - ".get_string("multianswer", "lesson");
}
return parent::option_description_string();
}
public function display_answers(html_table $table) {
$answers = $this->get_used_answers();
$options = new stdClass;
$options->noclean = true;
$options->para = false;
$i = 1;
foreach ($answers as $answer) {
$answer = parent::rewrite_answers_urls($answer);
$cells = array();
if ($this->lesson->custom && $answer->score > 0) {
// if the score is > 0, then it is correct
$cells[] = ''.get_string("answer", "lesson")." $i: \n";
} else if ($this->lesson->custom) {
$cells[] = ''.get_string("answer", "lesson")." $i: \n";
} else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
// underline correct answers
$cells[] = ''.get_string("answer", "lesson")." $i: \n";
} else {
$cells[] = ''.get_string("answer", "lesson")." $i: \n";
}
$cells[] = format_text($answer->answer, $answer->answerformat, $options);
$table->data[] = new html_table_row($cells);
$cells = array();
$cells[] = "".get_string("response", "lesson")." $i";
$cells[] = format_text($answer->response, $answer->responseformat, $options);
$table->data[] = new html_table_row($cells);
$cells = array();
$cells[] = "".get_string("score", "lesson").'';
$cells[] = $answer->score;
$table->data[] = new html_table_row($cells);
$cells = array();
$cells[] = "".get_string("jump", "lesson").'';
$cells[] = $this->get_jump_name($answer->jumpto);
$table->data[] = new html_table_row($cells);
if ($i === 1){
$table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
}
$i++;
}
return $table;
}
public function stats(array &$pagestats, $tries) {
if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
$temp = $tries[$this->lesson->maxattempts - 1];
} else {
// else, user attempted the question less than the max, so grab the last one
$temp = end($tries);
}
if ($this->properties->qoption) {
$userresponse = explode(",", $temp->useranswer);
foreach ($userresponse as $response) {
if (isset($pagestats[$temp->pageid][$response])) {
$pagestats[$temp->pageid][$response]++;
} else {
$pagestats[$temp->pageid][$response] = 1;
}
}
} else {
if (isset($pagestats[$temp->pageid][$temp->answerid])) {
$pagestats[$temp->pageid][$temp->answerid]++;
} else {
$pagestats[$temp->pageid][$temp->answerid] = 1;
}
}
if (isset($pagestats[$temp->pageid]["total"])) {
$pagestats[$temp->pageid]["total"]++;
} else {
$pagestats[$temp->pageid]["total"] = 1;
}
return true;
}
public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
$answers = $this->get_used_answers();
$formattextdefoptions = new stdClass;
$formattextdefoptions->para = false; //I'll use it widely in this page
foreach ($answers as $answer) {
if ($this->properties->qoption) {
if ($useranswer == null) {
$userresponse = array();
} else {
$userresponse = explode(",", $useranswer->useranswer);
}
if (in_array($answer->id, $userresponse)) {
// make checked
$data = "";
if (!isset($answerdata->response)) {
if ($answer->response == null) {
if ($useranswer->correct) {
$answerdata->response = get_string("thatsthecorrectanswer", "lesson");
} else {
$answerdata->response = get_string("thatsthewronganswer", "lesson");
}
} else {
$answerdata->response = $answer->response;
}
}
if (!isset($answerdata->score)) {
if ($this->lesson->custom) {
$answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
} elseif ($useranswer->correct) {
$answerdata->score = get_string("receivedcredit", "lesson");
} else {
$answerdata->score = get_string("didnotreceivecredit", "lesson");
}
}
} else {
// unchecked
$data = "";
}
if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) {
$data = "