1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
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.
|
|
|
14 |
//
|
|
|
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/>.
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* End of cluster
|
|
|
20 |
*
|
|
|
21 |
* @package mod_lesson
|
|
|
22 |
* @copyright 2009 Sam Hemelryk
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
**/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
/** End of Cluster page */
|
|
|
29 |
define("LESSON_PAGE_ENDOFCLUSTER", "31");
|
|
|
30 |
|
|
|
31 |
class lesson_page_type_endofcluster extends lesson_page {
|
|
|
32 |
|
|
|
33 |
protected $type = lesson_page::TYPE_STRUCTURE;
|
|
|
34 |
protected $typeidstring = 'endofcluster';
|
|
|
35 |
protected $typeid = LESSON_PAGE_ENDOFCLUSTER;
|
|
|
36 |
protected $string = null;
|
|
|
37 |
protected $jumpto = null;
|
|
|
38 |
|
|
|
39 |
public function display($renderer, $attempt) {
|
|
|
40 |
return '';
|
|
|
41 |
}
|
|
|
42 |
public function get_typeid() {
|
|
|
43 |
return $this->typeid;
|
|
|
44 |
}
|
|
|
45 |
public function get_typestring() {
|
|
|
46 |
if ($this->string===null) {
|
|
|
47 |
$this->string = get_string($this->typeidstring, 'lesson');
|
|
|
48 |
}
|
|
|
49 |
return $this->string;
|
|
|
50 |
}
|
|
|
51 |
public function get_idstring() {
|
|
|
52 |
return $this->typeidstring;
|
|
|
53 |
}
|
|
|
54 |
public function callback_on_view($canmanage, $redirect = true) {
|
|
|
55 |
return (int) $this->redirect_to_next_page($canmanage, $redirect);
|
|
|
56 |
}
|
|
|
57 |
public function redirect_to_next_page($canmanage, $redirect) {
|
|
|
58 |
global $PAGE;
|
|
|
59 |
if ($this->properties->nextpageid == 0) {
|
|
|
60 |
$nextpageid = LESSON_EOL;
|
|
|
61 |
} else {
|
|
|
62 |
$nextpageid = $this->properties->nextpageid;
|
|
|
63 |
}
|
|
|
64 |
if ($redirect) {
|
|
|
65 |
redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $nextpageid)));
|
|
|
66 |
die;
|
|
|
67 |
}
|
|
|
68 |
return $nextpageid;
|
|
|
69 |
}
|
|
|
70 |
public function get_grayout() {
|
|
|
71 |
return 1;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function override_next_page() {
|
|
|
75 |
global $DB;
|
|
|
76 |
$jump = $DB->get_field("lesson_answers", "jumpto", array("pageid" => $this->properties->id, "lessonid" => $this->lesson->id));
|
|
|
77 |
if ($jump == LESSON_NEXTPAGE) {
|
|
|
78 |
if ($this->properties->nextpageid == 0) {
|
|
|
79 |
return LESSON_EOL;
|
|
|
80 |
} else {
|
|
|
81 |
return $this->properties->nextpageid;
|
|
|
82 |
}
|
|
|
83 |
} else {
|
|
|
84 |
return $jump;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
public function add_page_link($previd) {
|
|
|
88 |
global $PAGE, $CFG;
|
|
|
89 |
if ($previd != 0) {
|
|
|
90 |
$addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_ENDOFCLUSTER));
|
|
|
91 |
return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_ENDOFCLUSTER, 'name'=>get_string('addendofcluster', 'lesson'));
|
|
|
92 |
}
|
|
|
93 |
return false;
|
|
|
94 |
}
|
|
|
95 |
public function valid_page_and_view(&$validpages, &$pageviews) {
|
|
|
96 |
return $this->properties->nextpageid;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Creates answers within the database for this end of cluster page. Usually only ever
|
|
|
101 |
* called when creating a new page instance.
|
|
|
102 |
* @param object $properties
|
|
|
103 |
* @return array
|
|
|
104 |
*/
|
|
|
105 |
public function create_answers($properties) {
|
|
|
106 |
global $DB;
|
|
|
107 |
|
|
|
108 |
$newanswer = new stdClass;
|
|
|
109 |
$newanswer->lessonid = $this->lesson->id;
|
|
|
110 |
$newanswer->pageid = $this->properties->id;
|
|
|
111 |
$newanswer->timecreated = $this->properties->timecreated;
|
|
|
112 |
|
|
|
113 |
if (isset($properties->jumpto[0])) {
|
|
|
114 |
$newanswer->jumpto = $properties->jumpto[0];
|
|
|
115 |
}
|
|
|
116 |
$newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
|
|
|
117 |
$answers = [$newanswer->id => new lesson_page_answer($newanswer)];
|
|
|
118 |
$this->answers = $answers;
|
|
|
119 |
return $answers;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
class lesson_add_page_form_endofcluster extends lesson_add_page_form_base {
|
|
|
124 |
|
|
|
125 |
public $qtype = LESSON_PAGE_ENDOFCLUSTER;
|
|
|
126 |
public $qtypestring = 'endofcluster';
|
|
|
127 |
protected $standard = false;
|
|
|
128 |
|
|
|
129 |
public function custom_definition() {
|
|
|
130 |
global $PAGE, $CFG;
|
|
|
131 |
|
|
|
132 |
$mform = $this->_form;
|
|
|
133 |
$lesson = $this->_customdata['lesson'];
|
|
|
134 |
$jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
|
|
|
135 |
|
|
|
136 |
$mform->addElement('hidden', 'firstpage');
|
|
|
137 |
$mform->setType('firstpage', PARAM_BOOL);
|
|
|
138 |
|
|
|
139 |
$mform->addElement('hidden', 'qtype');
|
|
|
140 |
$mform->setType('qtype', PARAM_TEXT);
|
|
|
141 |
|
|
|
142 |
$mform->addElement('text', 'title', get_string("pagetitle", "lesson"), ['size' => 70, 'maxlength' => 255]);
|
|
|
143 |
$mform->addRule('title', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
|
|
|
144 |
if (!empty($CFG->formatstringstriptags)) {
|
|
|
145 |
$mform->setType('title', PARAM_TEXT);
|
|
|
146 |
} else {
|
|
|
147 |
$mform->setType('title', PARAM_CLEANHTML);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
$this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
|
|
|
151 |
$mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
|
|
|
152 |
$mform->setType('contents_editor', PARAM_RAW);
|
|
|
153 |
|
|
|
154 |
$this->add_jumpto(0);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
public function construction_override($pageid, lesson $lesson) {
|
|
|
158 |
global $CFG, $PAGE, $DB;
|
|
|
159 |
require_sesskey();
|
|
|
160 |
|
|
|
161 |
$timenow = time();
|
|
|
162 |
|
|
|
163 |
// the new page is not the first page (end of cluster always comes after an existing page)
|
|
|
164 |
if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
|
|
|
165 |
throw new \moodle_exception('cannotfindpages', 'lesson');
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
// could put code in here to check if the user really can insert an end of cluster
|
|
|
169 |
|
|
|
170 |
$newpage = new stdClass;
|
|
|
171 |
$newpage->lessonid = $lesson->id;
|
|
|
172 |
$newpage->prevpageid = $pageid;
|
|
|
173 |
$newpage->nextpageid = $page->nextpageid;
|
|
|
174 |
$newpage->qtype = $this->qtype;
|
|
|
175 |
$newpage->timecreated = $timenow;
|
|
|
176 |
$newpage->title = get_string("endofclustertitle", "lesson");
|
|
|
177 |
$newpage->contents = get_string("endofclustertitle", "lesson");
|
|
|
178 |
$newpageid = $DB->insert_record("lesson_pages", $newpage);
|
|
|
179 |
// update the linked list...
|
|
|
180 |
$DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
|
|
|
181 |
if ($page->nextpageid) {
|
|
|
182 |
// the new page is not the last page
|
|
|
183 |
$DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
|
|
|
184 |
}
|
|
|
185 |
// ..and the single "answer"
|
|
|
186 |
$newanswer = new stdClass;
|
|
|
187 |
$newanswer->lessonid = $lesson->id;
|
|
|
188 |
$newanswer->pageid = $newpageid;
|
|
|
189 |
$newanswer->timecreated = $timenow;
|
|
|
190 |
$newanswer->jumpto = LESSON_NEXTPAGE;
|
|
|
191 |
$newanswerid = $DB->insert_record("lesson_answers", $newanswer);
|
|
|
192 |
$lesson->add_message(get_string('addedendofcluster', 'lesson'), 'notifysuccess');
|
|
|
193 |
redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
|
|
|
194 |
}
|
|
|
195 |
}
|