MDL-55154 mod_lesson: Lesson numerical response shows images.
[moodle.git] / mod / lesson / pagetypes / numerical.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 * Numerical
20 *
9b24f68b 21 * @package mod_lesson
cc3dbaaa
PS
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a4abb73
SH
24 **/
25
1e7f8ea2
PS
26defined('MOODLE_INTERNAL') || die();
27
0a4abb73
SH
28/** Numerical question type */
29define("LESSON_PAGE_NUMERICAL", "8");
30
31class lesson_page_type_numerical extends lesson_page {
32
33 protected $type = lesson_page::TYPE_QUESTION;
34 protected $typeidstring = 'numerical';
35 protected $typeid = LESSON_PAGE_NUMERICAL;
36 protected $string = null;
37
38 public function get_typeid() {
39 return $this->typeid;
40 }
41 public function get_typestring() {
42 if ($this->string===null) {
43 $this->string = get_string($this->typeidstring, 'lesson');
44 }
45 return $this->string;
46 }
47 public function get_idstring() {
48 return $this->typeidstring;
49 }
50 public function display($renderer, $attempt) {
51 global $USER, $CFG, $PAGE;
54fd7cd9 52 $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
0a4abb73
SH
53 $data = new stdClass;
54 $data->id = $PAGE->cm->id;
55 $data->pageid = $this->properties->id;
56 if (isset($USER->modattempts[$this->lesson->id])) {
57 $data->answer = s($attempt->useranswer);
58 }
59 $mform->set_data($data);
8101328a
SB
60
61 // Trigger an event question viewed.
62 $eventparams = array(
63 'context' => context_module::instance($PAGE->cm->id),
64 'objectid' => $this->properties->id,
65 'other' => array(
66 'pagetype' => $this->get_typestring()
67 )
68 );
69
70 $event = \mod_lesson\event\question_viewed::create($eventparams);
71 $event->trigger();
0a4abb73
SH
72 return $mform->display();
73 }
74 public function check_answer() {
75 global $CFG;
76 $result = parent::check_answer();
77
78 $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
79 $data = $mform->get_data();
80 require_sesskey();
81
9582e530
AG
82 $formattextdefoptions = new stdClass();
83 $formattextdefoptions->noclean = true;
84 $formattextdefoptions->para = false;
85
0a4abb73
SH
86 // set defaults
87 $result->response = '';
88 $result->newpageid = 0;
89
90 if (isset($data->answer)) {
91 // just doing default PARAM_RAW, not doing PARAM_INT because it could be a float
92 $result->useranswer = (float)$data->answer;
93 } else {
94 $result->noanswer = true;
95 return $result;
96 }
97 $result->studentanswer = $result->userresponse = $result->useranswer;
98 $answers = $this->get_answers();
99 foreach ($answers as $answer) {
9582e530 100 $answer = parent::rewrite_answers_urls($answer);
0a4abb73
SH
101 if (strpos($answer->answer, ':')) {
102 // there's a pairs of values
103 list($min, $max) = explode(':', $answer->answer);
104 $minimum = (float) $min;
105 $maximum = (float) $max;
106 } else {
107 // there's only one value
108 $minimum = (float) $answer->answer;
109 $maximum = $minimum;
110 }
111 if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) {
112 $result->newpageid = $answer->jumpto;
9582e530 113 $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
0a4abb73
SH
114 if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) {
115 $result->correctanswer = true;
116 }
117 if ($this->lesson->custom) {
118 if ($answer->score > 0) {
119 $result->correctanswer = true;
120 } else {
121 $result->correctanswer = false;
122 }
123 }
124 $result->answerid = $answer->id;
125 return $result;
126 }
127 }
128 return $result;
129 }
130
131 public function display_answers(html_table $table) {
132 $answers = $this->get_answers();
133 $options = new stdClass;
134 $options->noclean = true;
135 $options->para = false;
136 $i = 1;
137 foreach ($answers as $answer) {
0abc18cf 138 $answer = parent::rewrite_answers_urls($answer, false);
0a4abb73
SH
139 $cells = array();
140 if ($this->lesson->custom && $answer->score > 0) {
141 // if the score is > 0, then it is correct
142 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
143 } else if ($this->lesson->custom) {
144 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
145 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
146 // underline correct answers
147 $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
148 } else {
149 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
150 }
01c37ef1 151 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
8cea545e 152 $table->data[] = new html_table_row($cells);
0a4abb73
SH
153
154 $cells = array();
155 $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
01c37ef1 156 $cells[] = format_text($answer->response, $answer->responseformat, $options);
8cea545e 157 $table->data[] = new html_table_row($cells);
0a4abb73
SH
158
159 $cells = array();
160 $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
161 $cells[] = $answer->score;
8cea545e 162 $table->data[] = new html_table_row($cells);
0a4abb73
SH
163
164 $cells = array();
165 $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
166 $cells[] = $this->get_jump_name($answer->jumpto);
8cea545e 167 $table->data[] = new html_table_row($cells);
0a4abb73
SH
168 if ($i === 1){
169 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
170 }
171 $i++;
172 }
173 return $table;
174 }
175 public function stats(array &$pagestats, $tries) {
176 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
177 $temp = $tries[$this->lesson->maxattempts - 1];
178 } else {
179 // else, user attempted the question less than the max, so grab the last one
180 $temp = end($tries);
181 }
182 if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
183 $pagestats[$temp->pageid][$temp->useranswer]++;
184 } else {
185 $pagestats[$temp->pageid][$temp->useranswer] = 1;
186 }
187 if (isset($pagestats[$temp->pageid]["total"])) {
188 $pagestats[$temp->pageid]["total"]++;
189 } else {
190 $pagestats[$temp->pageid]["total"] = 1;
191 }
192 return true;
193 }
194
195 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
196 $answers = $this->get_answers();
197 $formattextdefoptions = new stdClass;
198 $formattextdefoptions->para = false; //I'll use it widely in this page
199 foreach ($answers as $answer) {
200 if ($useranswer == null && $i == 0) {
201 // I have the $i == 0 because it is easier to blast through it all at once.
202 if (isset($pagestats[$this->properties->id])) {
203 $stats = $pagestats[$this->properties->id];
204 $total = $stats["total"];
205 unset($stats["total"]);
206 foreach ($stats as $valentered => $ntimes) {
207 $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($valentered).'" />';
208 $percent = $ntimes / $total * 100;
209 $percent = round($percent, 2);
210 $percent .= "% ".get_string("enteredthis", "lesson");
211 $answerdata->answers[] = array($data, $percent);
212 }
213 } else {
214 $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
215 }
216 $i++;
217 } else if ($useranswer != null && ($answer->id == $useranswer->answerid || ($answer == end($answers) && empty($answerdata)))) {
218 // get in here when what the user entered is not one of the answers
219 $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">';
220 if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
221 $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
222 $percent = round($percent, 2);
223 $percent .= "% ".get_string("enteredthis", "lesson");
224 } else {
225 $percent = get_string("nooneenteredthis", "lesson");
226 }
227 $answerdata->answers[] = array($data, $percent);
228
229 if ($answer->id == $useranswer->answerid) {
ecea65ca 230 if ($answer->response == null) {
0a4abb73
SH
231 if ($useranswer->correct) {
232 $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
233 } else {
234 $answerdata->response = get_string("thatsthewronganswer", "lesson");
235 }
236 } else {
237 $answerdata->response = $answer->response;
238 }
239 if ($this->lesson->custom) {
240 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
241 } elseif ($useranswer->correct) {
242 $answerdata->score = get_string("receivedcredit", "lesson");
243 } else {
244 $answerdata->score = get_string("didnotreceivecredit", "lesson");
245 }
246 } else {
247 $answerdata->response = get_string("thatsthewronganswer", "lesson");
248 if ($this->lesson->custom) {
249 $answerdata->score = get_string("pointsearned", "lesson").": 0";
250 } else {
251 $answerdata->score = get_string("didnotreceivecredit", "lesson");
252 }
253 }
254 }
255 $answerpage->answerdata = $answerdata;
256 }
257 return $answerpage;
258 }
259}
260
261class lesson_add_page_form_numerical extends lesson_add_page_form_base {
262
263 public $qtype = 'numerical';
264 public $qtypestring = 'numerical';
ceeab150
RT
265 protected $answerformat = '';
266 protected $responseformat = LESSON_ANSWER_HTML;
0a4abb73
SH
267
268 public function custom_definition() {
269 for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
270 $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
ecea65ca 271 $this->add_answer($i, null, ($i < 1));
0a4abb73 272 $this->add_response($i);
ecea65ca 273 $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
0a4abb73
SH
274 $this->add_score($i, null, ($i===0)?1:0);
275 }
276 }
277}
278
279class lesson_display_answer_form_numerical extends moodleform {
280
281 public function definition() {
282 global $USER, $OUTPUT;
283 $mform = $this->_form;
284 $contents = $this->_customdata['contents'];
285
5b0af6e4
JMV
286 // Disable shortforms.
287 $mform->setDisableShortforms();
288
ffdf7f8a
DM
289 $mform->addElement('header', 'pageheader');
290
291 $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
0a4abb73 292
54fd7cd9
RW
293 $hasattempt = false;
294 $attrs = array('size'=>'50', 'maxlength'=>'200');
295 if (isset($this->_customdata['lessonid'])) {
296 $lessonid = $this->_customdata['lessonid'];
297 if (isset($USER->modattempts[$lessonid]->useranswer)) {
298 $attrs['readonly'] = 'readonly';
299 $hasattempt = true;
300 }
301 }
0a4abb73
SH
302 $options = new stdClass;
303 $options->para = false;
304 $options->noclean = true;
305
306 $mform->addElement('hidden', 'id');
307 $mform->setType('id', PARAM_INT);
308
309 $mform->addElement('hidden', 'pageid');
310 $mform->setType('pageid', PARAM_INT);
311
54fd7cd9 312 $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
0a4abb73
SH
313 $mform->setType('answer', PARAM_FLOAT);
314
54fd7cd9
RW
315 if ($hasattempt) {
316 $this->add_action_buttons(null, get_string("nextpage", "lesson"));
317 } else {
318 $this->add_action_buttons(null, get_string("submit", "lesson"));
319 }
0a4abb73
SH
320 }
321
ffdf7f8a 322}