PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/lesson/pagetypes/endofbranch.php

https://bitbucket.org/moodle/moodle
PHP | 205 lines | 142 code | 35 blank | 28 comment | 36 complexity | 576afe01d5bf6fbe30b57967cfbd67de 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. * End of 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. /** End of Branch page */
  25. define("LESSON_PAGE_ENDOFBRANCH", "21");
  26. class lesson_page_type_endofbranch extends lesson_page {
  27. protected $type = lesson_page::TYPE_STRUCTURE;
  28. protected $typeidstring = 'endofbranch';
  29. protected $typeid = LESSON_PAGE_ENDOFBRANCH;
  30. protected $string = null;
  31. protected $jumpto = null;
  32. public function display($renderer, $attempt) {
  33. return '';
  34. }
  35. public function get_typeid() {
  36. return $this->typeid;
  37. }
  38. public function get_typestring() {
  39. if ($this->string===null) {
  40. $this->string = get_string($this->typeidstring, 'lesson');
  41. }
  42. return $this->string;
  43. }
  44. public function get_idstring() {
  45. return $this->typeidstring;
  46. }
  47. public function callback_on_view($canmanage, $redirect = true) {
  48. return (int) $this->redirect_to_first_answer($canmanage, $redirect);
  49. }
  50. public function redirect_to_first_answer($canmanage, $redirect) {
  51. global $USER, $PAGE;
  52. $answers = $this->get_answers();
  53. $answer = array_shift($answers);
  54. $jumpto = $answer->jumpto;
  55. if ($jumpto == LESSON_RANDOMBRANCH) {
  56. $jumpto = lesson_unseen_branch_jump($this->lesson, $USER->id);
  57. } elseif ($jumpto == LESSON_CLUSTERJUMP) {
  58. if (!$canmanage) {
  59. $jumpto = $this->lesson->cluster_jump($this->properties->id);
  60. } else {
  61. if ($this->properties->nextpageid == 0) {
  62. $jumpto = LESSON_EOL;
  63. } else {
  64. $jumpto = $this->properties->nextpageid;
  65. }
  66. }
  67. } else if ($answer->jumpto == LESSON_NEXTPAGE) {
  68. if ($this->properties->nextpageid == 0) {
  69. $jumpto = LESSON_EOL;
  70. } else {
  71. $jumpto = $this->properties->nextpageid;
  72. }
  73. } else if ($jumpto == 0) {
  74. $jumpto = $this->properties->id;
  75. } else if ($jumpto == LESSON_PREVIOUSPAGE) {
  76. $jumpto = $this->properties->prevpageid;
  77. }
  78. if ($redirect) {
  79. redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $jumpto)));
  80. die;
  81. }
  82. return $jumpto;
  83. }
  84. public function get_grayout() {
  85. return 1;
  86. }
  87. public function add_page_link($previd) {
  88. global $PAGE, $CFG;
  89. if ($previd != 0) {
  90. $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_ENDOFBRANCH));
  91. return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_ENDOFBRANCH, 'name'=>get_string('addanendofbranch', 'lesson'));
  92. }
  93. return false;
  94. }
  95. public function valid_page_and_view(&$validpages, &$pageviews) {
  96. return $this->properties->nextpageid;
  97. }
  98. }
  99. class lesson_add_page_form_endofbranch extends lesson_add_page_form_base {
  100. public $qtype = LESSON_PAGE_ENDOFBRANCH;
  101. public $qtypestring = 'endofbranch';
  102. protected $standard = false;
  103. public function custom_definition() {
  104. global $PAGE, $CFG;
  105. $mform = $this->_form;
  106. $lesson = $this->_customdata['lesson'];
  107. $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
  108. $mform->addElement('hidden', 'firstpage');
  109. $mform->setType('firstpage', PARAM_BOOL);
  110. $mform->addElement('hidden', 'qtype');
  111. $mform->setType('qtype', PARAM_TEXT);
  112. $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
  113. if (!empty($CFG->formatstringstriptags)) {
  114. $mform->setType('title', PARAM_TEXT);
  115. } else {
  116. $mform->setType('title', PARAM_CLEANHTML);
  117. }
  118. $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
  119. $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
  120. $mform->setType('contents_editor', PARAM_RAW);
  121. $this->add_jumpto(0);
  122. }
  123. public function construction_override($pageid, lesson $lesson) {
  124. global $DB, $CFG, $PAGE;
  125. require_sesskey();
  126. // first get the preceeding page
  127. $timenow = time();
  128. // the new page is not the first page (end of branch always comes after an existing page)
  129. if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
  130. print_error('cannotfindpagerecord', 'lesson');
  131. }
  132. // chain back up to find the (nearest branch table)
  133. $btpage = clone($page);
  134. $btpageid = $btpage->id;
  135. while (($btpage->qtype != LESSON_PAGE_BRANCHTABLE) && ($btpage->prevpageid > 0)) {
  136. $btpageid = $btpage->prevpageid;
  137. if (!$btpage = $DB->get_record("lesson_pages", array("id" => $btpageid))) {
  138. print_error('cannotfindpagerecord', 'lesson');
  139. }
  140. }
  141. if ($btpage->qtype == LESSON_PAGE_BRANCHTABLE) {
  142. $newpage = new stdClass;
  143. $newpage->lessonid = $lesson->id;
  144. $newpage->prevpageid = $pageid;
  145. $newpage->nextpageid = $page->nextpageid;
  146. $newpage->qtype = $this->qtype;
  147. $newpage->timecreated = $timenow;
  148. $newpage->title = get_string("endofbranch", "lesson");
  149. $newpage->contents = get_string("endofbranch", "lesson");
  150. $newpageid = $DB->insert_record("lesson_pages", $newpage);
  151. // update the linked list...
  152. $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
  153. if ($page->nextpageid) {
  154. // the new page is not the last page
  155. $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
  156. }
  157. // ..and the single "answer"
  158. $newanswer = new stdClass;
  159. $newanswer->lessonid = $lesson->id;
  160. $newanswer->pageid = $newpageid;
  161. $newanswer->timecreated = $timenow;
  162. $newanswer->jumpto = $btpageid;
  163. $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
  164. $lesson->add_message(get_string('addedanendofbranch', 'lesson'), 'notifysuccess');
  165. } else {
  166. $lesson->add_message(get_string('nobranchtablefound', 'lesson'));
  167. }
  168. redirect($CFG->wwwroot."/mod/lesson/edit.php?id=".$PAGE->cm->id);
  169. }
  170. }