3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /** Branch Table page */
30 define("LESSON_PAGE_BRANCHTABLE", "20");
32 class lesson_page_type_branchtable extends lesson_page {
34 protected $type = lesson_page::TYPE_STRUCTURE;
35 protected $typeid = LESSON_PAGE_BRANCHTABLE;
36 protected $typeidstring = 'branchtable';
37 protected $string = null;
38 protected $jumpto = null;
40 public function get_typeid() {
43 public function get_typestring() {
44 if ($this->string===null) {
45 $this->string = get_string($this->typeidstring, 'lesson');
51 * Gets an array of the jumps used by the answers of this page
55 public function get_jumps() {
58 $params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id);
59 if ($answers = $this->get_answers()) {
60 foreach ($answers as $answer) {
61 if ($answer->answer === '') {
62 // show only jumps for real branches (==have description)
65 $jumps[] = $this->get_jump_name($answer->jumpto);
68 // We get here is the lesson was created on a Moodle 1.9 site and
69 // the lesson contains question pages without any answers.
70 $jumps[] = $this->get_jump_name($this->properties->nextpageid);
75 public static function get_jumptooptions($firstpage, lesson $lesson) {
78 $jump[0] = get_string("thispage", "lesson");
79 $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson");
80 $jump[LESSON_PREVIOUSPAGE] = get_string("previouspage", "lesson");
81 $jump[LESSON_EOL] = get_string("endoflesson", "lesson");
82 $jump[LESSON_UNSEENBRANCHPAGE] = get_string("unseenpageinbranch", "lesson");
83 $jump[LESSON_RANDOMPAGE] = get_string("randompageinbranch", "lesson");
84 $jump[LESSON_RANDOMBRANCH] = get_string("randombranch", "lesson");
87 if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) {
88 print_error('cannotfindfirstpage', 'lesson');
92 $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid));
93 $jump[$apageid] = $title;
94 $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid));
103 public function get_idstring() {
104 return $this->typeidstring;
106 public function display($renderer, $attempt) {
110 $options = new stdClass;
111 $options->para = false;
112 $options->noclean = true;
114 if ($this->lesson->slideshow) {
115 $output .= $renderer->slideshow_start($this->lesson);
117 // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
118 $output .= $renderer->heading(format_string($this->properties->title), 3);
119 $output .= $renderer->box($this->get_contents(), 'contents');
123 foreach ($this->get_answers() as $answer) {
124 if ($answer->answer === '') {
129 $params['id'] = $PAGE->cm->id;
130 $params['pageid'] = $this->properties->id;
131 $params['sesskey'] = sesskey();
132 $params['jumpto'] = $answer->jumpto;
133 $url = new moodle_url('/mod/lesson/continue.php', $params);
134 $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)));
137 // Set the orientation
138 if ($this->properties->layout) {
139 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal');
141 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical');
143 $output .= $buttonshtml;
145 if ($this->lesson->slideshow) {
146 $output .= $renderer->slideshow_end();
152 public function check_answer() {
153 global $USER, $DB, $PAGE, $CFG;
156 $newpageid = optional_param('jumpto', NULL, PARAM_INT);
157 // going to insert into lesson_branch
158 if ($newpageid == LESSON_RANDOMBRANCH) {
163 if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) {
164 $retries = count($grades);
168 $branch = new stdClass;
169 $branch->lessonid = $this->lesson->id;
170 $branch->userid = $USER->id;
171 $branch->pageid = $this->properties->id;
172 $branch->retry = $retries;
173 $branch->flag = $branchflag;
174 $branch->timeseen = time();
176 $DB->insert_record("lesson_branch", $branch);
178 // this is called when jumping to random from a branch table
179 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
180 if($newpageid == LESSON_UNSEENBRANCHPAGE) {
181 if (has_capability('mod/lesson:manage', $context)) {
182 $newpageid = LESSON_NEXTPAGE;
184 $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id); // this may return 0
187 // convert jumpto page into a proper page id
188 if ($newpageid == 0) {
189 $newpageid = $this->properties->id;
190 } elseif ($newpageid == LESSON_NEXTPAGE) {
191 if (!$newpageid = $this->nextpageid) {
192 // no nextpage go to end of lesson
193 $newpageid = LESSON_EOL;
195 } elseif ($newpageid == LESSON_PREVIOUSPAGE) {
196 $newpageid = $this->prevpageid;
197 } elseif ($newpageid == LESSON_RANDOMPAGE) {
198 $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id);
199 } elseif ($newpageid == LESSON_RANDOMBRANCH) {
200 $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id);
202 // no need to record anything in lesson_attempts
203 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id,'pageid'=>$newpageid)));
206 public function display_answers(html_table $table) {
207 $answers = $this->get_answers();
208 $options = new stdClass;
209 $options->noclean = true;
210 $options->para = false;
212 foreach ($answers as $answer) {
213 if ($answer->answer === '') {
218 $cells[] = "<span class=\"label\">".get_string("branch", "lesson")." $i<span>: ";
219 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
220 $table->data[] = new html_table_row($cells);
223 $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
224 $cells[] = $this->get_jump_name($answer->jumpto);
225 $table->data[] = new html_table_row($cells);
228 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
234 public function get_grayout() {
237 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
238 $answers = $this->get_answers();
239 $formattextdefoptions = new stdClass;
240 $formattextdefoptions->para = false; //I'll use it widely in this page
241 foreach ($answers as $answer) {
242 $data = "<input type=\"button\" name=\"$answer->id\" value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE,$formattextdefoptions)))."\" disabled=\"disabled\"> ";
243 $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto));
244 $answerdata->answers[] = array($data, "");
245 $answerpage->answerdata = $answerdata;
250 public function update($properties, $context = null, $maxbytes = null) {
251 if (empty($properties->display)) {
252 $properties->display = '0';
254 if (empty($properties->layout)) {
255 $properties->layout = '0';
257 return parent::update($properties);
259 public function add_page_link($previd) {
261 $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE));
262 return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson'));
264 protected function get_displayinmenublock() {
267 public function is_unseen($param) {
269 if (is_array($param)) {
271 $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
272 foreach ($branchpages as $branchpage) {
273 if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed
280 if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
288 class lesson_add_page_form_branchtable extends lesson_add_page_form_base {
290 public $qtype = LESSON_PAGE_BRANCHTABLE;
291 public $qtypestring = 'branchtable';
292 protected $standard = false;
294 public function custom_definition() {
297 $mform = $this->_form;
298 $lesson = $this->_customdata['lesson'];
300 $firstpage = optional_param('firstpage', false, PARAM_BOOL);
302 $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson);
304 $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson'));
306 $mform->addElement('hidden', 'firstpage');
307 $mform->setType('firstpage', PARAM_BOOL);
308 $mform->setDefault('firstpage', $firstpage);
310 $mform->addElement('hidden', 'qtype');
311 $mform->setType('qtype', PARAM_INT);
313 $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
314 $mform->setType('title', PARAM_TEXT);
315 $mform->addRule('title', null, 'required', null, 'server');
317 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
318 $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
319 $mform->setType('contents_editor', PARAM_RAW);
321 $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson"));
322 $mform->setDefault('layout', true);
324 $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson"));
325 $mform->setDefault('display', true);
327 for ($i = 0; $i < $lesson->maxanswers; $i++) {
328 $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1));
329 $this->add_answer($i, get_string("description", "lesson"), $i == 0);
331 $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions);
333 $mform->setDefault('jumpto['.$i.']', 0);
335 $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE);