PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/cluster.php

https://bitbucket.org/moodle/moodle
PHP | 182 lines | 131 code | 22 blank | 29 comment | 23 complexity | d755feb9942d96c97418369a2a8e12bd 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. * 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. /** Start of Cluster page */
  25. define("LESSON_PAGE_CLUSTER", "30");
  26. class lesson_page_type_cluster extends lesson_page {
  27. protected $type = lesson_page::TYPE_STRUCTURE;
  28. protected $typeidstring = 'cluster';
  29. protected $typeid = LESSON_PAGE_CLUSTER;
  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 get_grayout() {
  48. return 1;
  49. }
  50. public function callback_on_view($canmanage, $redirect = true) {
  51. global $USER;
  52. if (!$canmanage) {
  53. // Get the next page in the lesson cluster jump
  54. return (int) $this->lesson->cluster_jump($this->properties->id);
  55. } else {
  56. // get the next page
  57. return (int) $this->properties->nextpageid;
  58. }
  59. }
  60. public function override_next_page() {
  61. global $USER;
  62. return $this->lesson->cluster_jump($this->properties->id);
  63. }
  64. public function add_page_link($previd) {
  65. global $PAGE, $CFG;
  66. $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_CLUSTER));
  67. return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_CLUSTER, 'name'=>get_string('addcluster', 'lesson'));
  68. }
  69. public function valid_page_and_view(&$validpages, &$pageviews) {
  70. $validpages[$this->properties->id] = 1; // add the cluster page as a valid page
  71. foreach ($this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_ENDOFCLUSTER)) as $subpage) {
  72. if (in_array($subpage->id, $pageviews)) {
  73. unset($pageviews[array_search($subpage->id, $pageviews)]); // remove it
  74. // since the user did see one page in the cluster, add the cluster pageid to the viewedpageids
  75. if (!in_array($this->properties->id, $pageviews)) {
  76. $pageviews[] = $this->properties->id;
  77. }
  78. }
  79. }
  80. return $this->properties->nextpageid;
  81. }
  82. }
  83. class lesson_add_page_form_cluster extends lesson_add_page_form_base {
  84. public $qtype = LESSON_PAGE_CLUSTER;
  85. public $qtypestring = 'cluster';
  86. protected $standard = false;
  87. public function custom_definition() {
  88. global $PAGE, $CFG;
  89. $mform = $this->_form;
  90. $lesson = $this->_customdata['lesson'];
  91. $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
  92. $mform->addElement('hidden', 'firstpage');
  93. $mform->setType('firstpage', PARAM_BOOL);
  94. $mform->addElement('hidden', 'qtype');
  95. $mform->setType('qtype', PARAM_TEXT);
  96. $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
  97. if (!empty($CFG->formatstringstriptags)) {
  98. $mform->setType('title', PARAM_TEXT);
  99. } else {
  100. $mform->setType('title', PARAM_CLEANHTML);
  101. }
  102. $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
  103. $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
  104. $mform->setType('contents_editor', PARAM_RAW);
  105. $this->add_jumpto(0);
  106. }
  107. public function construction_override($pageid, lesson $lesson) {
  108. global $PAGE, $CFG, $DB;
  109. require_sesskey();
  110. $timenow = time();
  111. if ($pageid == 0) {
  112. if ($lesson->has_pages()) {
  113. if (!$page = $DB->get_record("lesson_pages", array("prevpageid" => 0, "lessonid" => $lesson->id))) {
  114. print_error('cannotfindpagerecord', 'lesson');
  115. }
  116. } else {
  117. // This is the ONLY page
  118. $page = new stdClass;
  119. $page->id = 0;
  120. }
  121. } else {
  122. if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
  123. print_error('cannotfindpagerecord', 'lesson');
  124. }
  125. }
  126. $newpage = new stdClass;
  127. $newpage->lessonid = $lesson->id;
  128. $newpage->prevpageid = $pageid;
  129. if ($pageid != 0) {
  130. $newpage->nextpageid = $page->nextpageid;
  131. } else {
  132. $newpage->nextpageid = $page->id;
  133. }
  134. $newpage->qtype = $this->qtype;
  135. $newpage->timecreated = $timenow;
  136. $newpage->title = get_string("clustertitle", "lesson");
  137. $newpage->contents = get_string("clustertitle", "lesson");
  138. $newpageid = $DB->insert_record("lesson_pages", $newpage);
  139. // update the linked list...
  140. if ($pageid != 0) {
  141. $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
  142. }
  143. if ($pageid == 0) {
  144. $page->nextpageid = $page->id;
  145. }
  146. if ($page->nextpageid) {
  147. // the new page is not the last page
  148. $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
  149. }
  150. // ..and the single "answer"
  151. $newanswer = new stdClass;
  152. $newanswer->lessonid = $lesson->id;
  153. $newanswer->pageid = $newpageid;
  154. $newanswer->timecreated = $timenow;
  155. $newanswer->jumpto = LESSON_CLUSTERJUMP;
  156. $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
  157. $lesson->add_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
  158. redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
  159. }
  160. }