/mod/lesson/pagetypes/endofbranch.php

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