PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/endofcluster.php

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