PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/lesson/pagetypes/numerical.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 325 lines | 259 code | 33 blank | 33 comment | 61 complexity | 8726bb503f13a6528939546470525157 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Numerical
  18. *
  19. * @package mod_lesson
  20. * @copyright 2009 Sam Hemelryk
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. **/
  23. defined('MOODLE_INTERNAL') || die();
  24. /** Numerical question type */
  25. define("LESSON_PAGE_NUMERICAL", "8");
  26. class lesson_page_type_numerical extends lesson_page {
  27. protected $type = lesson_page::TYPE_QUESTION;
  28. protected $typeidstring = 'numerical';
  29. protected $typeid = LESSON_PAGE_NUMERICAL;
  30. protected $string = null;
  31. public function get_typeid() {
  32. return $this->typeid;
  33. }
  34. public function get_typestring() {
  35. if ($this->string===null) {
  36. $this->string = get_string($this->typeidstring, 'lesson');
  37. }
  38. return $this->string;
  39. }
  40. public function get_idstring() {
  41. return $this->typeidstring;
  42. }
  43. public function display($renderer, $attempt) {
  44. global $USER, $CFG, $PAGE;
  45. $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
  46. $data = new stdClass;
  47. $data->id = $PAGE->cm->id;
  48. $data->pageid = $this->properties->id;
  49. if (isset($USER->modattempts[$this->lesson->id])) {
  50. $data->answer = s($attempt->useranswer);
  51. }
  52. $mform->set_data($data);
  53. // Trigger an event question viewed.
  54. $eventparams = array(
  55. 'context' => context_module::instance($PAGE->cm->id),
  56. 'objectid' => $this->properties->id,
  57. 'other' => array(
  58. 'pagetype' => $this->get_typestring()
  59. )
  60. );
  61. $event = \mod_lesson\event\question_viewed::create($eventparams);
  62. $event->trigger();
  63. return $mform->display();
  64. }
  65. public function check_answer() {
  66. global $CFG;
  67. $result = parent::check_answer();
  68. $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
  69. $data = $mform->get_data();
  70. require_sesskey();
  71. $formattextdefoptions = new stdClass();
  72. $formattextdefoptions->noclean = true;
  73. $formattextdefoptions->para = false;
  74. // set defaults
  75. $result->response = '';
  76. $result->newpageid = 0;
  77. if (!isset($data->answer) || !is_numeric($data->answer)) {
  78. $result->noanswer = true;
  79. return $result;
  80. } else {
  81. // Just doing default PARAM_RAW, not doing PARAM_INT because it could be a float.
  82. $result->useranswer = (float)$data->answer;
  83. }
  84. $result->studentanswer = $result->userresponse = $result->useranswer;
  85. $answers = $this->get_answers();
  86. foreach ($answers as $answer) {
  87. $answer = parent::rewrite_answers_urls($answer);
  88. if (strpos($answer->answer, ':')) {
  89. // there's a pairs of values
  90. list($min, $max) = explode(':', $answer->answer);
  91. $minimum = (float) $min;
  92. $maximum = (float) $max;
  93. } else {
  94. // there's only one value
  95. $minimum = (float) $answer->answer;
  96. $maximum = $minimum;
  97. }
  98. if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) {
  99. $result->newpageid = $answer->jumpto;
  100. $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
  101. if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) {
  102. $result->correctanswer = true;
  103. }
  104. if ($this->lesson->custom) {
  105. if ($answer->score > 0) {
  106. $result->correctanswer = true;
  107. } else {
  108. $result->correctanswer = false;
  109. }
  110. }
  111. $result->answerid = $answer->id;
  112. return $result;
  113. }
  114. }
  115. return $result;
  116. }
  117. public function display_answers(html_table $table) {
  118. $answers = $this->get_answers();
  119. $options = new stdClass;
  120. $options->noclean = true;
  121. $options->para = false;
  122. $i = 1;
  123. foreach ($answers as $answer) {
  124. $answer = parent::rewrite_answers_urls($answer, false);
  125. $cells = array();
  126. if ($this->lesson->custom && $answer->score > 0) {
  127. // if the score is > 0, then it is correct
  128. $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
  129. } else if ($this->lesson->custom) {
  130. $cells[] = '<label>' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
  131. } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
  132. // underline correct answers
  133. $cells[] = '<span class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</span>:' . "\n";
  134. } else {
  135. $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
  136. }
  137. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  138. $table->data[] = new html_table_row($cells);
  139. $cells = array();
  140. $cells[] = '<label>' . get_string('response', 'lesson') . ' ' . $i . '</label>:';
  141. $cells[] = format_text($answer->response, $answer->responseformat, $options);
  142. $table->data[] = new html_table_row($cells);
  143. $cells = array();
  144. $cells[] = '<label>' . get_string('score', 'lesson') . '</label>:';
  145. $cells[] = $answer->score;
  146. $table->data[] = new html_table_row($cells);
  147. $cells = array();
  148. $cells[] = '<label>' . get_string('jump', 'lesson') . '</label>:';
  149. $cells[] = $this->get_jump_name($answer->jumpto);
  150. $table->data[] = new html_table_row($cells);
  151. if ($i === 1){
  152. $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
  153. }
  154. $i++;
  155. }
  156. return $table;
  157. }
  158. public function stats(array &$pagestats, $tries) {
  159. if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
  160. $temp = $tries[$this->lesson->maxattempts - 1];
  161. } else {
  162. // else, user attempted the question less than the max, so grab the last one
  163. $temp = end($tries);
  164. }
  165. if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
  166. $pagestats[$temp->pageid][$temp->useranswer]++;
  167. } else {
  168. $pagestats[$temp->pageid][$temp->useranswer] = 1;
  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. public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
  178. $answers = $this->get_answers();
  179. $formattextdefoptions = new stdClass;
  180. $formattextdefoptions->para = false; //I'll use it widely in this page
  181. foreach ($answers as $answer) {
  182. if ($useranswer == null && $i == 0) {
  183. // I have the $i == 0 because it is easier to blast through it all at once.
  184. if (isset($pagestats[$this->properties->id])) {
  185. $stats = $pagestats[$this->properties->id];
  186. $total = $stats["total"];
  187. unset($stats["total"]);
  188. foreach ($stats as $valentered => $ntimes) {
  189. $data = '<input class="form-control" type="text" size="50" ' .
  190. 'disabled="disabled" readonly="readonly" value="'.s($valentered).'" />';
  191. $percent = $ntimes / $total * 100;
  192. $percent = round($percent, 2);
  193. $percent .= "% ".get_string("enteredthis", "lesson");
  194. $answerdata->answers[] = array($data, $percent);
  195. }
  196. } else {
  197. $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
  198. }
  199. $i++;
  200. } else if ($useranswer != null && ($answer->id == $useranswer->answerid || ($answer == end($answers) &&
  201. empty($answerdata->answers)))) {
  202. // Get in here when the user answered or for the last answer.
  203. $data = '<input class="form-control" type="text" size="50" ' .
  204. 'disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">';
  205. if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
  206. $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
  207. $percent = round($percent, 2);
  208. $percent .= "% ".get_string("enteredthis", "lesson");
  209. } else {
  210. $percent = get_string("nooneenteredthis", "lesson");
  211. }
  212. $answerdata->answers[] = array($data, $percent);
  213. if ($answer->id == $useranswer->answerid) {
  214. if ($answer->response == null) {
  215. if ($useranswer->correct) {
  216. $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
  217. } else {
  218. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  219. }
  220. } else {
  221. $answerdata->response = $answer->response;
  222. }
  223. if ($this->lesson->custom) {
  224. $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
  225. } elseif ($useranswer->correct) {
  226. $answerdata->score = get_string("receivedcredit", "lesson");
  227. } else {
  228. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  229. }
  230. } else {
  231. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  232. if ($this->lesson->custom) {
  233. $answerdata->score = get_string("pointsearned", "lesson").": 0";
  234. } else {
  235. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  236. }
  237. }
  238. }
  239. $answerpage->answerdata = $answerdata;
  240. }
  241. return $answerpage;
  242. }
  243. }
  244. class lesson_add_page_form_numerical extends lesson_add_page_form_base {
  245. public $qtype = 'numerical';
  246. public $qtypestring = 'numerical';
  247. protected $answerformat = '';
  248. protected $responseformat = LESSON_ANSWER_HTML;
  249. public function custom_definition() {
  250. for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
  251. $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
  252. $this->add_answer($i, null, ($i < 1));
  253. $this->add_response($i);
  254. $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
  255. $this->add_score($i, null, ($i===0)?1:0);
  256. }
  257. }
  258. }
  259. class lesson_display_answer_form_numerical extends moodleform {
  260. public function definition() {
  261. global $USER, $OUTPUT;
  262. $mform = $this->_form;
  263. $contents = $this->_customdata['contents'];
  264. // Disable shortforms.
  265. $mform->setDisableShortforms();
  266. $mform->addElement('header', 'pageheader');
  267. $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
  268. $hasattempt = false;
  269. $attrs = array('size'=>'50', 'maxlength'=>'200');
  270. if (isset($this->_customdata['lessonid'])) {
  271. $lessonid = $this->_customdata['lessonid'];
  272. if (isset($USER->modattempts[$lessonid]->useranswer)) {
  273. $attrs['readonly'] = 'readonly';
  274. $hasattempt = true;
  275. }
  276. }
  277. $options = new stdClass;
  278. $options->para = false;
  279. $options->noclean = true;
  280. $mform->addElement('hidden', 'id');
  281. $mform->setType('id', PARAM_INT);
  282. $mform->addElement('hidden', 'pageid');
  283. $mform->setType('pageid', PARAM_INT);
  284. $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
  285. $mform->setType('answer', PARAM_FLOAT);
  286. if ($hasattempt) {
  287. $this->add_action_buttons(null, get_string("nextpage", "lesson"));
  288. } else {
  289. $this->add_action_buttons(null, get_string("submit", "lesson"));
  290. }
  291. }
  292. }