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

/mod/lesson/pagetypes/truefalse.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 408 lines | 326 code | 42 blank | 40 comment | 89 complexity | 795a8d1326fd0d5b02c1319430d9ebc1 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. * True/false
  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. /** True/False question type */
  26. define("LESSON_PAGE_TRUEFALSE", "2");
  27. class lesson_page_type_truefalse extends lesson_page {
  28. protected $type = lesson_page::TYPE_QUESTION;
  29. protected $typeidstring = 'truefalse';
  30. protected $typeid = LESSON_PAGE_TRUEFALSE;
  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. $answers = $this->get_answers();
  47. shuffle($answers);
  48. $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt);
  49. $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params);
  50. $data = new stdClass;
  51. $data->id = $PAGE->cm->id;
  52. $data->pageid = $this->properties->id;
  53. $mform->set_data($data);
  54. return $mform->display();
  55. }
  56. public function check_answer() {
  57. global $DB, $CFG;
  58. $formattextdefoptions = new stdClass();
  59. $formattextdefoptions->noclean = true;
  60. $formattextdefoptions->para = false;
  61. $answers = $this->get_answers();
  62. shuffle($answers);
  63. $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
  64. $mform = new lesson_display_answer_form_truefalse($CFG->wwwroot.'/mod/lesson/continue.php', $params);
  65. $data = $mform->get_data();
  66. require_sesskey();
  67. $result = parent::check_answer();
  68. if (empty($data->answerid)) {
  69. $result->noanswer = true;
  70. return $result;
  71. }
  72. $result->answerid = $data->answerid;
  73. $answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST);
  74. if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
  75. $result->correctanswer = true;
  76. }
  77. if ($this->lesson->custom) {
  78. if ($answer->score > 0) {
  79. $result->correctanswer = true;
  80. } else {
  81. $result->correctanswer = false;
  82. }
  83. }
  84. $result->newpageid = $answer->jumpto;
  85. $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
  86. $result->studentanswer = $result->userresponse = $answer->answer;
  87. return $result;
  88. }
  89. public function display_answers(html_table $table) {
  90. $answers = $this->get_answers();
  91. $options = new stdClass();
  92. $options->noclean = true;
  93. $options->para = false;
  94. $i = 1;
  95. foreach ($answers as $answer) {
  96. $cells = array();
  97. if ($this->lesson->custom && $answer->score > 0) {
  98. // if the score is > 0, then it is correct
  99. $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
  100. } else if ($this->lesson->custom) {
  101. $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
  102. } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
  103. // underline correct answers
  104. $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
  105. } else {
  106. $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
  107. }
  108. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  109. $table->data[] = new html_table_row($cells);
  110. $cells = array();
  111. $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
  112. $cells[] = format_text($answer->response, $answer->responseformat, $options);
  113. $table->data[] = new html_table_row($cells);
  114. $cells = array();
  115. $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
  116. $cells[] = $answer->score;
  117. $table->data[] = new html_table_row($cells);
  118. $cells = array();
  119. $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
  120. $cells[] = $this->get_jump_name($answer->jumpto);
  121. $table->data[] = new html_table_row($cells);
  122. if ($i === 1){
  123. $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
  124. }
  125. $i++;
  126. }
  127. return $table;
  128. }
  129. /**
  130. * Updates the page and its answers
  131. *
  132. * @global moodle_database $DB
  133. * @global moodle_page $PAGE
  134. * @param stdClass $properties
  135. * @return bool
  136. */
  137. public function update($properties, $context = null, $maxbytes = null) {
  138. global $DB, $PAGE;
  139. $answers = $this->get_answers();
  140. $properties->id = $this->properties->id;
  141. $properties->lessonid = $this->lesson->id;
  142. $properties->timemodified = time();
  143. $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
  144. $DB->update_record("lesson_pages", $properties);
  145. // need to reset offset for correct and wrong responses
  146. $this->lesson->maxanswers = 2;
  147. for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
  148. if (!array_key_exists($i, $this->answers)) {
  149. $this->answers[$i] = new stdClass;
  150. $this->answers[$i]->lessonid = $this->lesson->id;
  151. $this->answers[$i]->pageid = $this->id;
  152. $this->answers[$i]->timecreated = $this->timecreated;
  153. }
  154. if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
  155. $this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
  156. $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
  157. }
  158. if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
  159. $this->answers[$i]->response = $properties->response_editor[$i]['text'];
  160. $this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
  161. }
  162. // we don't need to check for isset here because properties called it's own isset method.
  163. if ($this->answers[$i]->answer != '') {
  164. if (isset($properties->jumpto[$i])) {
  165. $this->answers[$i]->jumpto = $properties->jumpto[$i];
  166. }
  167. if ($this->lesson->custom && isset($properties->score[$i])) {
  168. $this->answers[$i]->score = $properties->score[$i];
  169. }
  170. if (!isset($this->answers[$i]->id)) {
  171. $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
  172. } else {
  173. $DB->update_record("lesson_answers", $this->answers[$i]->properties());
  174. }
  175. } else if (isset($this->answers[$i]->id)) {
  176. $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
  177. unset($this->answers[$i]);
  178. }
  179. }
  180. return true;
  181. }
  182. public function stats(array &$pagestats, $tries) {
  183. if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
  184. $temp = $tries[$this->lesson->maxattempts - 1];
  185. } else {
  186. // else, user attempted the question less than the max, so grab the last one
  187. $temp = end($tries);
  188. }
  189. if ($this->properties->qoption) {
  190. $userresponse = explode(",", $temp->useranswer);
  191. foreach ($userresponse as $response) {
  192. if (isset($pagestats[$temp->pageid][$response])) {
  193. $pagestats[$temp->pageid][$response]++;
  194. } else {
  195. $pagestats[$temp->pageid][$response] = 1;
  196. }
  197. }
  198. } else {
  199. if (isset($pagestats[$temp->pageid][$temp->answerid])) {
  200. $pagestats[$temp->pageid][$temp->answerid]++;
  201. } else {
  202. $pagestats[$temp->pageid][$temp->answerid] = 1;
  203. }
  204. }
  205. if (isset($pagestats[$temp->pageid]["total"])) {
  206. $pagestats[$temp->pageid]["total"]++;
  207. } else {
  208. $pagestats[$temp->pageid]["total"] = 1;
  209. }
  210. return true;
  211. }
  212. public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
  213. $answers = $this->get_answers();
  214. $formattextdefoptions = new stdClass(); //I'll use it widely in this page
  215. $formattextdefoptions->para = false;
  216. $formattextdefoptions->noclean = true;
  217. foreach ($answers as $answer) {
  218. if ($this->properties->qoption) {
  219. if ($useranswer == null) {
  220. $userresponse = array();
  221. } else {
  222. $userresponse = explode(",", $useranswer->useranswer);
  223. }
  224. if (in_array($answer->id, $userresponse)) {
  225. // make checked
  226. $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />";
  227. if (!isset($answerdata->response)) {
  228. if ($answer->response == null) {
  229. if ($useranswer->correct) {
  230. $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
  231. } else {
  232. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  233. }
  234. } else {
  235. $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
  236. }
  237. }
  238. if (!isset($answerdata->score)) {
  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. }
  247. } else {
  248. // unchecked
  249. $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />";
  250. }
  251. if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) {
  252. $data .= "<div class=highlight>".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>";
  253. } else {
  254. $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
  255. }
  256. } else {
  257. if ($useranswer != null and $answer->id == $useranswer->answerid) {
  258. // make checked
  259. $data = "<input readonly=\"readonly\" disabled=\"disabled\" name=\"answer[$i]\" checked=\"checked\" type=\"checkbox\" value=\"1\" />";
  260. if ($answer->response == null) {
  261. if ($useranswer->correct) {
  262. $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
  263. } else {
  264. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  265. }
  266. } else {
  267. $answerdata->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
  268. }
  269. if ($this->lesson->custom) {
  270. $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
  271. } elseif ($useranswer->correct) {
  272. $answerdata->score = get_string("receivedcredit", "lesson");
  273. } else {
  274. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  275. }
  276. } else {
  277. // unchecked
  278. $data = "<input type=\"checkbox\" readonly=\"readonly\" name=\"answer[$i]\" value=\"0\" disabled=\"disabled\" />";
  279. }
  280. if (($answer->score > 0 && $this->lesson->custom) || ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto) && !$this->lesson->custom)) {
  281. $data .= "<div class=\"highlight\">".format_text($answer->answer, $answer->answerformat, $formattextdefoptions)."</div>";
  282. } else {
  283. $data .= format_text($answer->answer, $answer->answerformat, $formattextdefoptions);
  284. }
  285. }
  286. if (isset($pagestats[$this->properties->id][$answer->id])) {
  287. $percent = $pagestats[$this->properties->id][$answer->id] / $pagestats[$this->properties->id]["total"] * 100;
  288. $percent = round($percent, 2);
  289. $percent .= "% ".get_string("checkedthisone", "lesson");
  290. } else {
  291. $percent = get_string("noonecheckedthis", "lesson");
  292. }
  293. $answerdata->answers[] = array($data, $percent);
  294. $answerpage->answerdata = $answerdata;
  295. }
  296. return $answerpage;
  297. }
  298. }
  299. class lesson_add_page_form_truefalse extends lesson_add_page_form_base {
  300. public $qtype = 'truefalse';
  301. public $qtypestring = 'truefalse';
  302. public function custom_definition() {
  303. $this->_form->addElement('header', 'answertitle0', get_string('correctresponse', 'lesson'));
  304. $this->add_answer(0, null, true);
  305. $this->add_response(0);
  306. $this->add_jumpto(0, get_string('correctanswerjump', 'lesson'), LESSON_NEXTPAGE);
  307. $this->add_score(0, get_string('correctanswerscore', 'lesson'), 1);
  308. $this->_form->addElement('header', 'answertitle1', get_string('wrongresponse', 'lesson'));
  309. $this->add_answer(1, null, true);
  310. $this->add_response(1);
  311. $this->add_jumpto(1, get_string('wronganswerjump', 'lesson'), LESSON_THISPAGE);
  312. $this->add_score(1, get_string('wronganswerscore', 'lesson'), 0);
  313. }
  314. }
  315. class lesson_display_answer_form_truefalse extends moodleform {
  316. public function definition() {
  317. global $USER, $OUTPUT;
  318. $mform = $this->_form;
  319. $answers = $this->_customdata['answers'];
  320. $lessonid = $this->_customdata['lessonid'];
  321. $contents = $this->_customdata['contents'];
  322. if (array_key_exists('attempt', $this->_customdata)) {
  323. $attempt = $this->_customdata['attempt'];
  324. } else {
  325. $attempt = new stdClass();
  326. $attempt->answerid = null;
  327. }
  328. $mform->addElement('header', 'pageheader');
  329. $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
  330. $hasattempt = false;
  331. $disabled = '';
  332. if (isset($USER->modattempts[$lessonid]) && !empty($USER->modattempts[$lessonid])) {
  333. $hasattempt = true;
  334. $disabled = array('disabled' => 'disabled');
  335. }
  336. $options = new stdClass();
  337. $options->para = false;
  338. $options->noclean = true;
  339. $mform->addElement('hidden', 'id');
  340. $mform->setType('id', PARAM_INT);
  341. $mform->addElement('hidden', 'pageid');
  342. $mform->setType('pageid', PARAM_INT);
  343. $i = 0;
  344. foreach ($answers as $answer) {
  345. $mform->addElement('html', '<div class="answeroption">');
  346. $ansid = 'answerid';
  347. if ($hasattempt) {
  348. $ansid = 'answer_id';
  349. }
  350. $mform->addElement('radio', $ansid, null, format_text($answer->answer, $answer->answerformat, $options), $answer->id, $disabled);
  351. $mform->setType($ansid, PARAM_INT);
  352. if ($hasattempt && $answer->id == $USER->modattempts[$lessonid]->answerid) {
  353. $mform->setDefault($ansid, $attempt->answerid);
  354. $mform->addElement('hidden', 'answerid', $answer->id);
  355. $mform->setType('answerid', PARAM_INT);
  356. }
  357. $mform->addElement('html', '</div>');
  358. $i++;
  359. }
  360. if ($hasattempt) {
  361. $this->add_action_buttons(null, get_string("nextpage", "lesson"));
  362. } else {
  363. $this->add_action_buttons(null, get_string("submit", "lesson"));
  364. }
  365. }
  366. }