PageRenderTime 30ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/endofcluster.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 202 lines | 150 code | 24 blank | 28 comment | 24 complexity | 05515369fc14e3611f1138f4ed1bc54f 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. * End of cluster
  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 Cluster page */
  26. define("LESSON_PAGE_ENDOFCLUSTER", "31");
  27. class lesson_page_type_endofcluster extends lesson_page {
  28. protected $type = lesson_page::TYPE_STRUCTURE;
  29. protected $typeidstring = 'endofcluster';
  30. protected $typeid = LESSON_PAGE_ENDOFCLUSTER;
  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_next_page($canmanage);
  50. exit;
  51. }
  52. public function redirect_to_next_page() {
  53. global $PAGE;
  54. if ($this->properties->nextpageid == 0) {
  55. $nextpageid = LESSON_EOL;
  56. } else {
  57. $nextpageid = $this->properties->nextpageid;
  58. }
  59. redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id,'pageid'=>$nextpageid)));
  60. }
  61. public function get_grayout() {
  62. return 1;
  63. }
  64. public function update($properties, $context = null, $maxbytes = null) {
  65. global $DB, $PAGE;
  66. $properties->id = $this->properties->id;
  67. $properties->lessonid = $this->lesson->id;
  68. if (empty($properties->qoption)) {
  69. $properties->qoption = '0';
  70. }
  71. $properties->timemodified = time();
  72. $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);
  73. $DB->update_record("lesson_pages", $properties);
  74. $answers = $this->get_answers();
  75. if (count($answers)>1) {
  76. $answer = array_shift($answers);
  77. foreach ($answers as $a) {
  78. $DB->delete_record('lesson_answers', array('id'=>$a->id));
  79. }
  80. } else if (count($answers)==1) {
  81. $answer = array_shift($answers);
  82. } else {
  83. $answer = new stdClass;
  84. }
  85. $answer->timemodified = time();
  86. if (isset($properties->jumpto[0])) {
  87. $answer->jumpto = $properties->jumpto[0];
  88. }
  89. if (isset($properties->score[0])) {
  90. $answer->score = $properties->score[0];
  91. }
  92. if (!empty($answer->id)) {
  93. $DB->update_record("lesson_answers", $answer->properties());
  94. } else {
  95. $DB->insert_record("lesson_answers", $answer);
  96. }
  97. return true;
  98. }
  99. public function override_next_page() {
  100. global $DB;
  101. $jump = $DB->get_field("lesson_answers", "jumpto", array("pageid" => $this->properties->id, "lessonid" => $this->lesson->id));
  102. if ($jump == LESSON_NEXTPAGE) {
  103. if ($this->properties->nextpageid == 0) {
  104. return LESSON_EOL;
  105. } else {
  106. return $this->properties->nextpageid;
  107. }
  108. } else {
  109. return $jump;
  110. }
  111. }
  112. public function add_page_link($previd) {
  113. global $PAGE, $CFG;
  114. if ($previd != 0) {
  115. $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_ENDOFCLUSTER));
  116. return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_ENDOFCLUSTER, 'name'=>get_string('addendofcluster', 'lesson'));
  117. }
  118. return false;
  119. }
  120. public function valid_page_and_view(&$validpages, &$pageviews) {
  121. return $this->properties->nextpageid;
  122. }
  123. }
  124. class lesson_add_page_form_endofcluster extends lesson_add_page_form_base {
  125. public $qtype = LESSON_PAGE_ENDOFCLUSTER;
  126. public $qtypestring = 'endofcluster';
  127. protected $standard = false;
  128. public function custom_definition() {
  129. global $PAGE;
  130. $mform = $this->_form;
  131. $lesson = $this->_customdata['lesson'];
  132. $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
  133. $mform->addElement('hidden', 'firstpage');
  134. $mform->setType('firstpage', PARAM_BOOL);
  135. $mform->addElement('hidden', 'qtype');
  136. $mform->setType('qtype', PARAM_TEXT);
  137. $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
  138. $mform->setType('title', PARAM_TEXT);
  139. $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
  140. $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
  141. $mform->setType('contents_editor', PARAM_RAW);
  142. $this->add_jumpto(0);
  143. }
  144. public function construction_override($pageid, lesson $lesson) {
  145. global $CFG, $PAGE, $DB;
  146. require_sesskey();
  147. $timenow = time();
  148. // the new page is not the first page (end of cluster always comes after an existing page)
  149. if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
  150. print_error('cannotfindpages', 'lesson');
  151. }
  152. // could put code in here to check if the user really can insert an end of cluster
  153. $newpage = new stdClass;
  154. $newpage->lessonid = $lesson->id;
  155. $newpage->prevpageid = $pageid;
  156. $newpage->nextpageid = $page->nextpageid;
  157. $newpage->qtype = $this->qtype;
  158. $newpage->timecreated = $timenow;
  159. $newpage->title = get_string("endofclustertitle", "lesson");
  160. $newpage->contents = get_string("endofclustertitle", "lesson");
  161. $newpageid = $DB->insert_record("lesson_pages", $newpage);
  162. // update the linked list...
  163. $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
  164. if ($page->nextpageid) {
  165. // the new page is not the last page
  166. $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
  167. }
  168. // ..and the single "answer"
  169. $newanswer = new stdClass;
  170. $newanswer->lessonid = $lesson->id;
  171. $newanswer->pageid = $newpageid;
  172. $newanswer->timecreated = $timenow;
  173. $newanswer->jumpto = LESSON_NEXTPAGE;
  174. $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
  175. $lesson->add_message(get_string('addedendofcluster', 'lesson'), 'notifysuccess');
  176. redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
  177. }
  178. }