PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/lesson/pagetypes/numerical.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 300 lines | 239 code | 29 blank | 32 comment | 61 complexity | f6113c60cc59d52118b0416a10cd36cb MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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
  20. * @subpackage lesson
  21. * @copyright 2009 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. **/
  24. defined('MOODLE_INTERNAL') || die();
  25. /** Numerical question type */
  26. define("LESSON_PAGE_NUMERICAL", "8");
  27. class lesson_page_type_numerical extends lesson_page {
  28. protected $type = lesson_page::TYPE_QUESTION;
  29. protected $typeidstring = 'numerical';
  30. protected $typeid = LESSON_PAGE_NUMERICAL;
  31. protected $string = null;
  32. public function get_typeid() {
  33. return $this->typeid;
  34. }
  35. public function get_typestring() {
  36. if ($this->string===null) {
  37. $this->string = get_string($this->typeidstring, 'lesson');
  38. }
  39. return $this->string;
  40. }
  41. public function get_idstring() {
  42. return $this->typeidstring;
  43. }
  44. public function display($renderer, $attempt) {
  45. global $USER, $CFG, $PAGE;
  46. $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
  47. $data = new stdClass;
  48. $data->id = $PAGE->cm->id;
  49. $data->pageid = $this->properties->id;
  50. if (isset($USER->modattempts[$this->lesson->id])) {
  51. $data->answer = s($attempt->useranswer);
  52. }
  53. $mform->set_data($data);
  54. return $mform->display();
  55. }
  56. public function check_answer() {
  57. global $CFG;
  58. $result = parent::check_answer();
  59. $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
  60. $data = $mform->get_data();
  61. require_sesskey();
  62. // set defaults
  63. $result->response = '';
  64. $result->newpageid = 0;
  65. if (isset($data->answer)) {
  66. // just doing default PARAM_RAW, not doing PARAM_INT because it could be a float
  67. $result->useranswer = (float)$data->answer;
  68. } else {
  69. $result->noanswer = true;
  70. return $result;
  71. }
  72. $result->studentanswer = $result->userresponse = $result->useranswer;
  73. $answers = $this->get_answers();
  74. foreach ($answers as $answer) {
  75. if (strpos($answer->answer, ':')) {
  76. // there's a pairs of values
  77. list($min, $max) = explode(':', $answer->answer);
  78. $minimum = (float) $min;
  79. $maximum = (float) $max;
  80. } else {
  81. // there's only one value
  82. $minimum = (float) $answer->answer;
  83. $maximum = $minimum;
  84. }
  85. if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) {
  86. $result->newpageid = $answer->jumpto;
  87. $result->response = trim($answer->response);
  88. if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) {
  89. $result->correctanswer = true;
  90. }
  91. if ($this->lesson->custom) {
  92. if ($answer->score > 0) {
  93. $result->correctanswer = true;
  94. } else {
  95. $result->correctanswer = false;
  96. }
  97. }
  98. $result->answerid = $answer->id;
  99. return $result;
  100. }
  101. }
  102. return $result;
  103. }
  104. public function display_answers(html_table $table) {
  105. $answers = $this->get_answers();
  106. $options = new stdClass;
  107. $options->noclean = true;
  108. $options->para = false;
  109. $i = 1;
  110. foreach ($answers as $answer) {
  111. $cells = array();
  112. if ($this->lesson->custom && $answer->score > 0) {
  113. // if the score is > 0, then it is correct
  114. $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
  115. } else if ($this->lesson->custom) {
  116. $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
  117. } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
  118. // underline correct answers
  119. $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
  120. } else {
  121. $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
  122. }
  123. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  124. $table->data[] = new html_table_row($cells);
  125. $cells = array();
  126. $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
  127. $cells[] = format_text($answer->response, $answer->responseformat, $options);
  128. $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);
  133. $cells = array();
  134. $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
  135. $cells[] = $this->get_jump_name($answer->jumpto);
  136. $table->data[] = new html_table_row($cells);
  137. if ($i === 1){
  138. $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
  139. }
  140. $i++;
  141. }
  142. return $table;
  143. }
  144. public function stats(array &$pagestats, $tries) {
  145. if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
  146. $temp = $tries[$this->lesson->maxattempts - 1];
  147. } else {
  148. // else, user attempted the question less than the max, so grab the last one
  149. $temp = end($tries);
  150. }
  151. if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
  152. $pagestats[$temp->pageid][$temp->useranswer]++;
  153. } else {
  154. $pagestats[$temp->pageid][$temp->useranswer] = 1;
  155. }
  156. if (isset($pagestats[$temp->pageid]["total"])) {
  157. $pagestats[$temp->pageid]["total"]++;
  158. } else {
  159. $pagestats[$temp->pageid]["total"] = 1;
  160. }
  161. return true;
  162. }
  163. public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
  164. $answers = $this->get_answers();
  165. $formattextdefoptions = new stdClass;
  166. $formattextdefoptions->para = false; //I'll use it widely in this page
  167. foreach ($answers as $answer) {
  168. if ($useranswer == null && $i == 0) {
  169. // I have the $i == 0 because it is easier to blast through it all at once.
  170. if (isset($pagestats[$this->properties->id])) {
  171. $stats = $pagestats[$this->properties->id];
  172. $total = $stats["total"];
  173. unset($stats["total"]);
  174. foreach ($stats as $valentered => $ntimes) {
  175. $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($valentered).'" />';
  176. $percent = $ntimes / $total * 100;
  177. $percent = round($percent, 2);
  178. $percent .= "% ".get_string("enteredthis", "lesson");
  179. $answerdata->answers[] = array($data, $percent);
  180. }
  181. } else {
  182. $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
  183. }
  184. $i++;
  185. } else if ($useranswer != null && ($answer->id == $useranswer->answerid || ($answer == end($answers) && empty($answerdata)))) {
  186. // get in here when what the user entered is not one of the answers
  187. $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">';
  188. if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
  189. $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
  190. $percent = round($percent, 2);
  191. $percent .= "% ".get_string("enteredthis", "lesson");
  192. } else {
  193. $percent = get_string("nooneenteredthis", "lesson");
  194. }
  195. $answerdata->answers[] = array($data, $percent);
  196. if ($answer->id == $useranswer->answerid) {
  197. if ($answer->response == null) {
  198. if ($useranswer->correct) {
  199. $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
  200. } else {
  201. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  202. }
  203. } else {
  204. $answerdata->response = $answer->response;
  205. }
  206. if ($this->lesson->custom) {
  207. $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
  208. } elseif ($useranswer->correct) {
  209. $answerdata->score = get_string("receivedcredit", "lesson");
  210. } else {
  211. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  212. }
  213. } else {
  214. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  215. if ($this->lesson->custom) {
  216. $answerdata->score = get_string("pointsearned", "lesson").": 0";
  217. } else {
  218. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  219. }
  220. }
  221. }
  222. $answerpage->answerdata = $answerdata;
  223. }
  224. return $answerpage;
  225. }
  226. }
  227. class lesson_add_page_form_numerical extends lesson_add_page_form_base {
  228. public $qtype = 'numerical';
  229. public $qtypestring = 'numerical';
  230. public function custom_definition() {
  231. for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
  232. $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
  233. $this->add_answer($i, null, ($i < 1));
  234. $this->add_response($i);
  235. $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
  236. $this->add_score($i, null, ($i===0)?1:0);
  237. }
  238. }
  239. }
  240. class lesson_display_answer_form_numerical extends moodleform {
  241. public function definition() {
  242. global $USER, $OUTPUT;
  243. $mform = $this->_form;
  244. $contents = $this->_customdata['contents'];
  245. $mform->addElement('header', 'pageheader');
  246. $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
  247. $hasattempt = false;
  248. $attrs = array('size'=>'50', 'maxlength'=>'200');
  249. if (isset($this->_customdata['lessonid'])) {
  250. $lessonid = $this->_customdata['lessonid'];
  251. if (isset($USER->modattempts[$lessonid]->useranswer)) {
  252. $attrs['readonly'] = 'readonly';
  253. $hasattempt = true;
  254. }
  255. }
  256. $options = new stdClass;
  257. $options->para = false;
  258. $options->noclean = true;
  259. $mform->addElement('hidden', 'id');
  260. $mform->setType('id', PARAM_INT);
  261. $mform->addElement('hidden', 'pageid');
  262. $mform->setType('pageid', PARAM_INT);
  263. $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
  264. $mform->setType('answer', PARAM_FLOAT);
  265. if ($hasattempt) {
  266. $this->add_action_buttons(null, get_string("nextpage", "lesson"));
  267. } else {
  268. $this->add_action_buttons(null, get_string("submit", "lesson"));
  269. }
  270. }
  271. }