lesson MDL-23525 more bugs fixed to pagetypes.
[moodle.git] / mod / lesson / pagetypes / truefalse.php
CommitLineData
0a4abb73
SH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * True/false
20 *
cc3dbaaa
PS
21 * @package mod
22 * @subpackage lesson
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a4abb73
SH
25 **/
26
1e7f8ea2
PS
27defined('MOODLE_INTERNAL') || die();
28
29/** True/False question type */
0a4abb73
SH
30define("LESSON_PAGE_TRUEFALSE", "2");
31
32class lesson_page_type_truefalse extends lesson_page {
33
34 protected $type = lesson_page::TYPE_QUESTION;
35 protected $typeidstring = 'truefalse';
36 protected $typeid = LESSON_PAGE_TRUEFALSE;
37 protected $string = null;
38
39 public function get_typeid() {
40 return $this->typeid;
41 }
42 public function get_typestring() {
43 if ($this->string===null) {
44 $this->string = get_string($this->typeidstring, 'lesson');
45 }
46 return $this->string;
47 }
48 public function get_idstring() {
49 return $this->typeidstring;
50 }
51 public function display($renderer, $attempt) {
52 global $USER, $CFG, $PAGE;
53 $answers = $this->get_answers();
54 shuffle($answers);
55
1112c9ed 56 $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt);
0a4abb73
SH
57 $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params);
58 $data = new stdClass;
59 $data->id = $PAGE->cm->id;
60 $data->pageid = $this->properties->id;
61 $mform->set_data($data);
62 return $mform->display();
63 }
64 public function check_answer() {
65 global $DB, $CFG;
01c37ef1
PS
66 $formattextdefoptions = new object();
67 $formattextdefoptions->noclean = true;
68 $formattextdefoptions->para = false;
69
0a4abb73
SH
70 $answers = $this->get_answers();
71 shuffle($answers);
72 $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
73 $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params);
74 $data = $mform->get_data();
75 require_sesskey();
76
77 $result = parent::check_answer();
78
79 $answerid = $data->answerid;
80 if ($answerid === false) {
81 $result->noanswer = true;
82 return $result;
83 }
84 $result->answerid = $answerid;
01c37ef1 85 $answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST);
0a4abb73
SH
86 if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
87 $result->correctanswer = true;
88 }
89 if ($this->lesson->custom) {
90 if ($answer->score > 0) {
91 $result->correctanswer = true;
92 } else {
93 $result->correctanswer = false;
94 }
95 }
96 $result->newpageid = $answer->jumpto;
01c37ef1
PS
97 $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
98 $result->studentanswer = $result->userresponse = $result->response;
0a4abb73
SH
99 return $result;
100 }
101
102 public function display_answers(html_table $table) {
103 $answers = $this->get_answers();
01c37ef1 104 $options = new object();
0a4abb73
SH
105 $options->noclean = true;
106 $options->para = false;
107 $i = 1;
108 foreach ($answers as $answer) {
109 $cells = array();
110 if ($this->lesson->custom && $answer->score > 0) {
111 // if the score is > 0, then it is correct
112 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
113 } else if ($this->lesson->custom) {
114 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
115 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
116 // underline correct answers
117 $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
118 } else {
119 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
120 }
01c37ef1 121 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
8cea545e 122 $table->data[] = new html_table_row($cells);
0a4abb73
SH
123
124 $cells = array();
125 $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
01c37ef1 126 $cells[] = format_text($answer->response, $answer->responseformat, $options);
8cea545e 127 $table->data[] = new html_table_row($cells);
0a4abb73
SH
128
129 $cells = array();
130 $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
131 $cells[] = $answer->score;
8cea545e 132 $table->data[] = new html_table_row($cells);
0a4abb73
SH
133
134 $cells = array();
135 $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
136 $cells[] = $this->get_jump_name($answer->jumpto);
8cea545e 137 $table->data[] = new html_table_row($cells);
2f67a9b3 138
0a4abb73
SH
139 if ($i === 1){
140 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
141 }
142
143 $i++;
144 }
145 return $table;
146 }
147 public function stats(array &$pagestats, $tries) {
148 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
149 $temp = $tries[$this->lesson->maxattempts - 1];
150 } else {
151 // else, user attempted the question less than the max, so grab the last one
152 $temp = end($tries);
153 }
154 if ($this->properties->qoption) {
155 $userresponse = explode(",", $temp->useranswer);
156 foreach ($userresponse as $response) {
157 if (isset($pagestats[$temp->pageid][$response])) {
158 $pagestats[$temp->pageid][$response]++;
159 } else {
160 $pagestats[$temp->pageid][$response] = 1;
161 }
162 }
163 } else {
164 if (isset($pagestats[$temp->pageid][$temp->answerid])) {
165 $pagestats[$temp->pageid][$temp->answerid]++;
166 } else {
167 $pagestats[$temp->pageid][$temp->answerid] = 1;
168 }
169 }
170 if (isset($pagestats[$temp->pageid]["total"])) {
171 $pagestats[$temp->pageid]["total"]++;
172 } else {
173 $pagestats[$temp->pageid]["total"] = 1;
174 }
175 return true;
176 }
177
178 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
179 $answers = $this->get_answers();
1112c9ed 180 $formattextdefoptions = new object(); //I'll use it widely in this page
01c37ef1
PS
181 $formattextdefoptions->para = false;
182 $formattextdefoptions->noclean = true;
0a4abb73
SH
183 foreach ($answers as $answer) {
184 if ($this->properties->qoption) {
185 if ($useranswer == NULL) {
186 $userresponse = array();
187 } else {
188 $userresponse = explode(",", $useranswer->useranswer);
189 }
190 if (in_array($answer->id, $userresponse)) {
191 // make checked
192 $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />";
193 if (!isset($answerdata->response)) {
194 if ($answer->response == NULL) {
195 if ($useranswer->correct) {
196 $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
197 } else {
198 $answerdata->response = get_string("thatsthewronganswer", "lesson");
199 }
200 } else {
01c37ef1 201 $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
0a4abb73
SH
202 }
203 }
204 if (!isset($answerdata->score)) {
205 if ($this->lesson->custom) {
206 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
207 } elseif ($useranswer->correct) {
208 $answerdata->score = get_string("receivedcredit", "lesson");
209 } else {
210 $answerdata->score = get_string("didnotreceivecredit", "lesson");
211 }
212 }
213 } else {
214 // unchecked
215 $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />";
216 }
217 if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) {
01c37ef1 218 $data .= "<div class=highlight>".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>";
0a4abb73 219 } else {
01c37ef1 220 $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
0a4abb73
SH
221 }
222 } else {
223 if ($useranswer != NULL and $answer->id == $useranswer->answerid) {
224 // make checked
225 $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />";
226 if ($answer->response == NULL) {
227 if ($useranswer->correct) {
228 $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
229 } else {
230 $answerdata->response = get_string("thatsthewronganswer", "lesson");
231 }
232 } else {
01c37ef1 233 $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
0a4abb73
SH
234 }
235 if ($this->lesson->custom) {
236 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
237 } elseif ($useranswer->correct) {
238 $answerdata->score = get_string("receivedcredit", "lesson");
239 } else {
240 $answerdata->score = get_string("didnotreceivecredit", "lesson");
241 }
242 } else {
243 // unchecked
244 $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />";
245 }
246 if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) {
01c37ef1 247 $data .= "<div class=\"highlight\">".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>";
0a4abb73 248 } else {
01c37ef1 249 $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
0a4abb73
SH
250 }
251 }
252 if (isset($pagestats[$this->properties->id][$answer->id])) {
253 $percent = $pagestats[$this->properties->id][$answer->id] / $pagestats[$this->properties->id]["total"] * 100;
254 $percent = round($percent, 2);
255 $percent .= "% ".get_string("checkedthisone", "lesson");
256 } else {
257 $percent = get_string("noonecheckedthis", "lesson");
258 }
259
260 $answerdata->answers[] = array($data, $percent);
261 $answerpage->answerdata = $answerdata;
262 }
263 return $answerpage;
264 }
265}
266
267class lesson_add_page_form_truefalse extends lesson_add_page_form_base {
268
269 public $qtype = 'truefalse';
270 public $qtypestring = 'truefalse';
271
272 public function custom_definition() {
273 $this->_form->addElement('header', 'answertitle0', get_string('correctresponse', 'lesson'));
10b8fefd 274 $this->add_answer(0, NULL, true);
0a4abb73 275 $this->add_response(0);
4d5ab13e 276 $this->add_jumpto(0, get_string('correctanswerjump', 'lesson'), LESSON_NEXTPAGE);
0a4abb73
SH
277 $this->add_score(0, get_string('correctanswerscore', 'lesson'), 1);
278
279 $this->_form->addElement('header', 'answertitle1', get_string('wrongresponse', 'lesson'));
10b8fefd 280 $this->add_answer(1, NULL, true);
0a4abb73 281 $this->add_response(1);
4d5ab13e 282 $this->add_jumpto(1, get_string('wronganswerjump', 'lesson'), LESSON_THISPAGE);
0a4abb73
SH
283 $this->add_score(1, get_string('wronganswerscore', 'lesson'), 0);
284 }
285}
286
287class lesson_display_answer_form_truefalse extends moodleform {
288
289 public function definition() {
290 global $USER, $OUTPUT;
291 $mform = $this->_form;
292 $answers = $this->_customdata['answers'];
293 $lessonid = $this->_customdata['lessonid'];
294 $contents = $this->_customdata['contents'];
1112c9ed
AB
295 if (array_key_exists('attempt', $this->_customdata)) {
296 $attempt = $this->_customdata['attempt'];
297 } else {
298 $attempt = new stdClass();
299 $attempt->answerid = null;
300 }
0a4abb73
SH
301
302 $mform->addElement('header', 'pageheader', $OUTPUT->box($contents, 'contents'));
303
01c37ef1 304 $options = new object();
0a4abb73
SH
305 $options->para = false;
306 $options->noclean = true;
307
308 $mform->addElement('hidden', 'id');
309 $mform->setType('id', PARAM_INT);
310
311 $mform->addElement('hidden', 'pageid');
312 $mform->setType('pageid', PARAM_INT);
313
314 $i = 0;
315 foreach ($answers as $answer) {
316 $mform->addElement('html', '<div class="answeroption">');
01c37ef1 317 $mform->addElement('radio', 'answerid', null, format_text($answer->answer, $answer->answerformat, $options), $answer->id);
0a4abb73
SH
318 $mform->setType('answerid', PARAM_INT);
319 if (isset($USER->modattempts[$lessonid]) && $answer->id == $attempt->answerid) {
320 $mform->setDefault('answerid', true);
321 }
322 $mform->addElement('html', '</div>');
323 $i++;
324 }
325
326 $this->add_action_buttons(null, get_string("pleasecheckoneanswer", "lesson"));
327 }
2f67a9b3 328
0a4abb73 329}