11e81cce8ac2ba01c7bf3ad879bdca89f2610410
[moodle.git] / mod / lesson / pagetypes / truefalse.php
1 <?php
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/>.
18 /**
19  * True/false
20  *
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
25  **/
27 defined('MOODLE_INTERNAL') || die();
29 /** True/False question type */
30 define("LESSON_PAGE_TRUEFALSE",     "2");
32 class lesson_page_type_truefalse extends lesson_page {
34     protected $type = lesson_page::TYPE_QUESTION;
35     protected $typeidstring = 'truefalse';
36     protected $typeid = LESSON_PAGE_TRUEFALSE;
37     protected $string = null;
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);
56         $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt);
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;
66         $formattextdefoptions = new object();
67         $formattextdefoptions->noclean = true;
68         $formattextdefoptions->para = false;
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();
77         $result = parent::check_answer();
79         $answerid = $data->answerid;
80         if ($answerid === false) {
81             $result->noanswer = true;
82             return $result;
83         }
84         $result->answerid = $answerid;
85         $answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST);
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;
97         $result->response  = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
98         $result->studentanswer = $result->userresponse = $result->response;
99         return $result;
100     }
102     public function display_answers(html_table $table) {
103         $answers = $this->get_answers();
104         $options = new object();
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             }
121             $cells[] = format_text($answer->answer, $answer->answerformat, $options);
122             $table->data[] = new html_table_row($cells);
124             $cells = array();
125             $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
126             $cells[] = format_text($answer->response, $answer->responseformat, $options);
127             $table->data[] = new html_table_row($cells);
129             $cells = array();
130             $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
131             $cells[] = $answer->score;
132             $table->data[] = new html_table_row($cells);
134             $cells = array();
135             $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
136             $cells[] = $this->get_jump_name($answer->jumpto);
137             $table->data[] = new html_table_row($cells);
139             if ($i === 1){
140                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
141             }
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     }
178     public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
179         $answers = $this->get_answers();
180         $formattextdefoptions = new object(); //I'll use it widely in this page
181         $formattextdefoptions->para = false;
182         $formattextdefoptions->noclean = true;
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 {
201                             $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
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)) {
218                     $data .= "<div class=highlight>".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>";
219                 } else {
220                     $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
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 {
233                         $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
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)) {
247                     $data .= "<div class=\"highlight\">".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>";
248                 } else {
249                     $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
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             }
260             $answerdata->answers[] = array($data, $percent);
261             $answerpage->answerdata = $answerdata;
262         }
263         return $answerpage;
264     }
267 class lesson_add_page_form_truefalse extends lesson_add_page_form_base {
269     public $qtype = 'truefalse';
270     public $qtypestring = 'truefalse';
272     public function custom_definition() {
273         $this->_form->addElement('header', 'answertitle0', get_string('correctresponse', 'lesson'));
274         $this->add_answer(0, NULL, true);
275         $this->add_response(0);
276         $this->add_jumpto(0, get_string('correctanswerjump', 'lesson'), LESSON_NEXTPAGE);
277         $this->add_score(0, get_string('correctanswerscore', 'lesson'), 1);
279         $this->_form->addElement('header', 'answertitle1', get_string('wrongresponse', 'lesson'));
280         $this->add_answer(1, NULL, true);
281         $this->add_response(1);
282         $this->add_jumpto(1, get_string('wronganswerjump', 'lesson'), LESSON_THISPAGE);
283         $this->add_score(1, get_string('wronganswerscore', 'lesson'), 0);
284     }
287 class lesson_display_answer_form_truefalse extends moodleform {
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'];
295         if (array_key_exists('attempt', $this->_customdata)) {
296             $attempt = $this->_customdata['attempt'];
297         } else {
298             $attempt = new stdClass();
299             $attempt->answerid = null;
300         }
302         $mform->addElement('header', 'pageheader', $OUTPUT->box($contents, 'contents'));
304         $options = new object();
305         $options->para = false;
306         $options->noclean = true;
308         $mform->addElement('hidden', 'id');
309         $mform->setType('id', PARAM_INT);
311         $mform->addElement('hidden', 'pageid');
312         $mform->setType('pageid', PARAM_INT);
314         $i = 0;
315         foreach ($answers as $answer) {
316             $mform->addElement('html', '<div class="answeroption">');
317             $mform->addElement('radio', 'answerid', null, format_text($answer->answer, $answer->answerformat, $options), $answer->id);
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         }
326         $this->add_action_buttons(null, get_string("pleasecheckoneanswer", "lesson"));
327     }