PageRenderTime 112ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/branchtable.php

https://bitbucket.org/moodle/moodle
PHP | 370 lines | 283 code | 44 blank | 43 comment | 47 complexity | f284705785734d316b2e60352282ea17 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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. * Branch Table
  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. /** Branch Table page */
  25. define("LESSON_PAGE_BRANCHTABLE", "20");
  26. class lesson_page_type_branchtable extends lesson_page {
  27. protected $type = lesson_page::TYPE_STRUCTURE;
  28. protected $typeid = LESSON_PAGE_BRANCHTABLE;
  29. protected $typeidstring = 'branchtable';
  30. protected $string = null;
  31. protected $jumpto = 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. /**
  42. * Gets an array of the jumps used by the answers of this page
  43. *
  44. * @return array
  45. */
  46. public function get_jumps() {
  47. global $DB;
  48. $jumps = array();
  49. $params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id);
  50. if ($answers = $this->get_answers()) {
  51. foreach ($answers as $answer) {
  52. if ($answer->answer === '') {
  53. // show only jumps for real branches (==have description)
  54. continue;
  55. }
  56. $jumps[] = $this->get_jump_name($answer->jumpto);
  57. }
  58. } else {
  59. // We get here is the lesson was created on a Moodle 1.9 site and
  60. // the lesson contains question pages without any answers.
  61. $jumps[] = $this->get_jump_name($this->properties->nextpageid);
  62. }
  63. return $jumps;
  64. }
  65. public static function get_jumptooptions($firstpage, lesson $lesson) {
  66. global $DB, $PAGE;
  67. $jump = array();
  68. $jump[0] = get_string("thispage", "lesson");
  69. $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson");
  70. $jump[LESSON_PREVIOUSPAGE] = get_string("previouspage", "lesson");
  71. $jump[LESSON_EOL] = get_string("endoflesson", "lesson");
  72. $jump[LESSON_UNSEENBRANCHPAGE] = get_string("unseenpageinbranch", "lesson");
  73. $jump[LESSON_RANDOMPAGE] = get_string("randompageinbranch", "lesson");
  74. $jump[LESSON_RANDOMBRANCH] = get_string("randombranch", "lesson");
  75. if (!$firstpage) {
  76. if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) {
  77. print_error('cannotfindfirstpage', 'lesson');
  78. }
  79. while (true) {
  80. if ($apageid) {
  81. $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid));
  82. $jump[$apageid] = $title;
  83. $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid));
  84. } else {
  85. // last page reached
  86. break;
  87. }
  88. }
  89. }
  90. return $jump;
  91. }
  92. public function get_idstring() {
  93. return $this->typeidstring;
  94. }
  95. public function display($renderer, $attempt) {
  96. global $PAGE, $CFG;
  97. $output = '';
  98. $options = new stdClass;
  99. $options->para = false;
  100. $options->noclean = true;
  101. if ($this->lesson->slideshow) {
  102. $output .= $renderer->slideshow_start($this->lesson);
  103. }
  104. // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
  105. $output .= $renderer->heading(format_string($this->properties->title), 3);
  106. $output .= $renderer->box($this->get_contents(), 'contents');
  107. $buttons = array();
  108. $i = 0;
  109. foreach ($this->get_answers() as $answer) {
  110. if ($answer->answer === '') {
  111. // not a branch!
  112. continue;
  113. }
  114. $params = array();
  115. $params['id'] = $PAGE->cm->id;
  116. $params['pageid'] = $this->properties->id;
  117. $params['sesskey'] = sesskey();
  118. $params['jumpto'] = $answer->jumpto;
  119. $url = new moodle_url('/mod/lesson/continue.php', $params);
  120. $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)));
  121. $i++;
  122. }
  123. // Set the orientation
  124. if ($this->properties->layout) {
  125. $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal');
  126. } else {
  127. $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical');
  128. }
  129. $output .= $buttonshtml;
  130. if ($this->lesson->slideshow) {
  131. $output .= $renderer->slideshow_end();
  132. }
  133. // Trigger an event: content page viewed.
  134. $eventparams = array(
  135. 'context' => context_module::instance($PAGE->cm->id),
  136. 'objectid' => $this->properties->id
  137. );
  138. $event = \mod_lesson\event\content_page_viewed::create($eventparams);
  139. $event->trigger();
  140. return $output;
  141. }
  142. public function check_answer() {
  143. global $USER, $DB, $PAGE, $CFG;
  144. $result = parent::check_answer();
  145. require_sesskey();
  146. $newpageid = optional_param('jumpto', null, PARAM_INT);
  147. // going to insert into lesson_branch
  148. if ($newpageid == LESSON_RANDOMBRANCH) {
  149. $branchflag = 1;
  150. } else {
  151. $branchflag = 0;
  152. }
  153. if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) {
  154. $retries = count($grades);
  155. } else {
  156. $retries = 0;
  157. }
  158. // First record this page in lesson_branch. This record may be needed by lesson_unseen_branch_jump.
  159. $branch = new stdClass;
  160. $branch->lessonid = $this->lesson->id;
  161. $branch->userid = $USER->id;
  162. $branch->pageid = $this->properties->id;
  163. $branch->retry = $retries;
  164. $branch->flag = $branchflag;
  165. $branch->timeseen = time();
  166. $branch->nextpageid = 0; // Next page id will be set later.
  167. $branch->id = $DB->insert_record("lesson_branch", $branch);
  168. // this is called when jumping to random from a branch table
  169. $context = context_module::instance($PAGE->cm->id);
  170. if($newpageid == LESSON_UNSEENBRANCHPAGE) {
  171. if (has_capability('mod/lesson:manage', $context)) {
  172. $newpageid = LESSON_NEXTPAGE;
  173. } else {
  174. $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id); // this may return 0
  175. }
  176. }
  177. // convert jumpto page into a proper page id
  178. if ($newpageid == 0) {
  179. $newpageid = $this->properties->id;
  180. } elseif ($newpageid == LESSON_NEXTPAGE) {
  181. if (!$newpageid = $this->nextpageid) {
  182. // no nextpage go to end of lesson
  183. $newpageid = LESSON_EOL;
  184. }
  185. } elseif ($newpageid == LESSON_PREVIOUSPAGE) {
  186. $newpageid = $this->prevpageid;
  187. } elseif ($newpageid == LESSON_RANDOMPAGE) {
  188. $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id);
  189. } elseif ($newpageid == LESSON_RANDOMBRANCH) {
  190. $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id);
  191. }
  192. // Update record to set nextpageid.
  193. $branch->nextpageid = $newpageid;
  194. $DB->update_record("lesson_branch", $branch);
  195. // This will force to redirect to the newpageid.
  196. $result->inmediatejump = true;
  197. $result->newpageid = $newpageid;
  198. return $result;
  199. }
  200. public function display_answers(html_table $table) {
  201. $answers = $this->get_answers();
  202. $options = new stdClass;
  203. $options->noclean = true;
  204. $options->para = false;
  205. $i = 1;
  206. foreach ($answers as $answer) {
  207. if ($answer->answer === '') {
  208. // not a branch!
  209. continue;
  210. }
  211. $cells = array();
  212. $cells[] = '<label>' . get_string('branch', 'lesson') . ' ' . $i . '</label>: ';
  213. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  214. $table->data[] = new html_table_row($cells);
  215. $cells = array();
  216. $cells[] = '<label>' . get_string('jump', 'lesson') . ' ' . $i . '</label>: ';
  217. $cells[] = $this->get_jump_name($answer->jumpto);
  218. $table->data[] = new html_table_row($cells);
  219. if ($i === 1){
  220. $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
  221. }
  222. $i++;
  223. }
  224. return $table;
  225. }
  226. public function get_grayout() {
  227. return 1;
  228. }
  229. public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
  230. $answers = $this->get_answers();
  231. $formattextdefoptions = new stdClass;
  232. $formattextdefoptions->para = false; //I'll use it widely in this page
  233. $formattextdefoptions->context = $answerpage->context;
  234. foreach ($answers as $answer) {
  235. $data = "<input type=\"button\" class=\"btn btn-secondary\" name=\"$answer->id\" " .
  236. "value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE, $formattextdefoptions)))."\" " .
  237. "disabled=\"disabled\"> ";
  238. $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto));
  239. $answerdata->answers[] = array($data, "");
  240. $answerpage->answerdata = $answerdata;
  241. }
  242. return $answerpage;
  243. }
  244. public function update($properties, $context = null, $maxbytes = null) {
  245. if (empty($properties->display)) {
  246. $properties->display = '0';
  247. }
  248. if (empty($properties->layout)) {
  249. $properties->layout = '0';
  250. }
  251. return parent::update($properties);
  252. }
  253. public function add_page_link($previd) {
  254. global $PAGE, $CFG;
  255. $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE));
  256. return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson'));
  257. }
  258. protected function get_displayinmenublock() {
  259. return true;
  260. }
  261. public function is_unseen($param) {
  262. global $USER, $DB;
  263. if (is_array($param)) {
  264. $seenpages = $param;
  265. $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
  266. foreach ($branchpages as $branchpage) {
  267. if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed
  268. return false;
  269. }
  270. }
  271. return true;
  272. } else {
  273. $nretakes = $param;
  274. if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
  275. return true;
  276. }
  277. return false;
  278. }
  279. }
  280. }
  281. class lesson_add_page_form_branchtable extends lesson_add_page_form_base {
  282. public $qtype = LESSON_PAGE_BRANCHTABLE;
  283. public $qtypestring = 'branchtable';
  284. protected $standard = false;
  285. public function custom_definition() {
  286. global $PAGE, $CFG;
  287. $mform = $this->_form;
  288. $lesson = $this->_customdata['lesson'];
  289. $firstpage = optional_param('firstpage', false, PARAM_BOOL);
  290. $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson);
  291. if ($this->_customdata['edit']) {
  292. $mform->setDefault('qtypeheading', get_string('editbranchtable', 'lesson'));
  293. } else {
  294. $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson'));
  295. }
  296. $mform->addElement('hidden', 'firstpage');
  297. $mform->setType('firstpage', PARAM_BOOL);
  298. $mform->setDefault('firstpage', $firstpage);
  299. $mform->addElement('hidden', 'qtype');
  300. $mform->setType('qtype', PARAM_INT);
  301. $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
  302. $mform->addRule('title', null, 'required', null, 'server');
  303. if (!empty($CFG->formatstringstriptags)) {
  304. $mform->setType('title', PARAM_TEXT);
  305. } else {
  306. $mform->setType('title', PARAM_CLEANHTML);
  307. }
  308. $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
  309. $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
  310. $mform->setType('contents_editor', PARAM_RAW);
  311. $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson"));
  312. $mform->setDefault('layout', true);
  313. $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson"));
  314. $mform->setDefault('display', true);
  315. for ($i = 0; $i < $lesson->maxanswers; $i++) {
  316. $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1));
  317. $this->add_answer($i, get_string("description", "lesson"), $i == 0);
  318. $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions);
  319. if ($i === 0) {
  320. $mform->setDefault('jumpto['.$i.']', 0);
  321. } else {
  322. $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE);
  323. }
  324. }
  325. }
  326. }