PageRenderTime 116ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/endofbranch.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 201 lines | 138 code | 35 blank | 28 comment | 34 complexity | db166a5566ebf08a23830465eaa87edc 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. * 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;
  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. $mform->setType('title', PARAM_TEXT);
  114. $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
  115. $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
  116. $mform->setType('contents_editor', PARAM_RAW);
  117. $this->add_jumpto(0);
  118. }
  119. public function construction_override($pageid, lesson $lesson) {
  120. global $DB, $CFG, $PAGE;
  121. require_sesskey();
  122. // first get the preceeding page
  123. $timenow = time();
  124. // the new page is not the first page (end of branch always comes after an existing page)
  125. if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
  126. print_error('cannotfindpagerecord', 'lesson');
  127. }
  128. // chain back up to find the (nearest branch table)
  129. $btpage = clone($page);
  130. $btpageid = $btpage->id;
  131. while (($btpage->qtype != LESSON_PAGE_BRANCHTABLE) && ($btpage->prevpageid > 0)) {
  132. $btpageid = $btpage->prevpageid;
  133. if (!$btpage = $DB->get_record("lesson_pages", array("id" => $btpageid))) {
  134. print_error('cannotfindpagerecord', 'lesson');
  135. }
  136. }
  137. if ($btpage->qtype == LESSON_PAGE_BRANCHTABLE) {
  138. $newpage = new stdClass;
  139. $newpage->lessonid = $lesson->id;
  140. $newpage->prevpageid = $pageid;
  141. $newpage->nextpageid = $page->nextpageid;
  142. $newpage->qtype = $this->qtype;
  143. $newpage->timecreated = $timenow;
  144. $newpage->title = get_string("endofbranch", "lesson");
  145. $newpage->contents = get_string("endofbranch", "lesson");
  146. $newpageid = $DB->insert_record("lesson_pages", $newpage);
  147. // update the linked list...
  148. $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
  149. if ($page->nextpageid) {
  150. // the new page is not the last page
  151. $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
  152. }
  153. // ..and the single "answer"
  154. $newanswer = new stdClass;
  155. $newanswer->lessonid = $lesson->id;
  156. $newanswer->pageid = $newpageid;
  157. $newanswer->timecreated = $timenow;
  158. $newanswer->jumpto = $btpageid;
  159. $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
  160. $lesson->add_message(get_string('addedanendofbranch', 'lesson'), 'notifysuccess');
  161. } else {
  162. $lesson->add_message(get_string('nobranchtablefound', 'lesson'));
  163. }
  164. redirect($CFG->wwwroot."/mod/lesson/edit.php?id=".$PAGE->cm->id);
  165. }
  166. }