weekly release 3.2.1+
[moodle.git] / mod / lesson / pagetypes / cluster.php
blobc179913fd559e1e665118ad74a555df71262b37f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Cluster
21 * @package mod_lesson
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
26 defined('MOODLE_INTERNAL') || die();
28 /** Start of Cluster page */
29 define("LESSON_PAGE_CLUSTER", "30");
31 class lesson_page_type_cluster extends lesson_page {
33 protected $type = lesson_page::TYPE_STRUCTURE;
34 protected $typeidstring = 'cluster';
35 protected $typeid = LESSON_PAGE_CLUSTER;
36 protected $string = null;
37 protected $jumpto = null;
39 public function display($renderer, $attempt) {
40 return '';
43 public function get_typeid() {
44 return $this->typeid;
46 public function get_typestring() {
47 if ($this->string===null) {
48 $this->string = get_string($this->typeidstring, 'lesson');
50 return $this->string;
52 public function get_idstring() {
53 return $this->typeidstring;
55 public function get_grayout() {
56 return 1;
58 public function callback_on_view($canmanage) {
59 global $USER;
60 if (!$canmanage) {
61 // Get the next page in the lesson cluster jump
62 return $this->lesson->cluster_jump($this->properties->id);
63 } else {
64 // get the next page
65 return $this->properties->nextpageid;
68 public function override_next_page() {
69 global $USER;
70 return $this->lesson->cluster_jump($this->properties->id);
72 public function add_page_link($previd) {
73 global $PAGE, $CFG;
74 $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_CLUSTER));
75 return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_CLUSTER, 'name'=>get_string('addcluster', 'lesson'));
77 public function valid_page_and_view(&$validpages, &$pageviews) {
78 $validpages[$this->properties->id] = 1; // add the cluster page as a valid page
79 foreach ($this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_ENDOFCLUSTER)) as $subpage) {
80 if (in_array($subpage->id, $pageviews)) {
81 unset($pageviews[array_search($subpage->id, $pageviews)]); // remove it
82 // since the user did see one page in the cluster, add the cluster pageid to the viewedpageids
83 if (!in_array($this->properties->id, $pageviews)) {
84 $pageviews[] = $this->properties->id;
88 return $this->properties->nextpageid;
92 class lesson_add_page_form_cluster extends lesson_add_page_form_base {
94 public $qtype = LESSON_PAGE_CLUSTER;
95 public $qtypestring = 'cluster';
96 protected $standard = false;
98 public function custom_definition() {
99 global $PAGE;
101 $mform = $this->_form;
102 $lesson = $this->_customdata['lesson'];
103 $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
105 $mform->addElement('hidden', 'firstpage');
106 $mform->setType('firstpage', PARAM_BOOL);
108 $mform->addElement('hidden', 'qtype');
109 $mform->setType('qtype', PARAM_TEXT);
111 $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
112 $mform->setType('title', PARAM_TEXT);
114 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
115 $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
116 $mform->setType('contents_editor', PARAM_RAW);
118 $this->add_jumpto(0);
122 public function construction_override($pageid, lesson $lesson) {
123 global $PAGE, $CFG, $DB;
124 require_sesskey();
126 $timenow = time();
128 if ($pageid == 0) {
129 if ($lesson->has_pages()) {
130 if (!$page = $DB->get_record("lesson_pages", array("prevpageid" => 0, "lessonid" => $lesson->id))) {
131 print_error('cannotfindpagerecord', 'lesson');
133 } else {
134 // This is the ONLY page
135 $page = new stdClass;
136 $page->id = 0;
138 } else {
139 if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
140 print_error('cannotfindpagerecord', 'lesson');
143 $newpage = new stdClass;
144 $newpage->lessonid = $lesson->id;
145 $newpage->prevpageid = $pageid;
146 if ($pageid != 0) {
147 $newpage->nextpageid = $page->nextpageid;
148 } else {
149 $newpage->nextpageid = $page->id;
151 $newpage->qtype = $this->qtype;
152 $newpage->timecreated = $timenow;
153 $newpage->title = get_string("clustertitle", "lesson");
154 $newpage->contents = get_string("clustertitle", "lesson");
155 $newpageid = $DB->insert_record("lesson_pages", $newpage);
156 // update the linked list...
157 if ($pageid != 0) {
158 $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
161 if ($pageid == 0) {
162 $page->nextpageid = $page->id;
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));
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_CLUSTERJUMP;
174 $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
175 $lesson->add_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
176 redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);