PageRenderTime 46ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/lesson/pagetypes/cluster.php

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