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");
83 if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) {
84 print_error('cannotfindfirstpage', 'lesson');
88 $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid));
89 $jump[$apageid] = $title;
90 $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid));
99 public function get_idstring() {
100 return $this->typeidstring;
102 public function display($renderer, $attempt) {
106 $options = new stdClass;
107 $options->para = false;
108 $options->noclean = true;
110 if ($this->lesson->slideshow) {
111 $output .= $renderer->slideshow_start($this->lesson);
113 // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
114 $output .= $renderer->heading(format_string($this->properties->title), 3);
115 $output .= $renderer->box($this->get_contents(), 'contents');
119 foreach ($this->get_answers() as $answer) {
120 if ($answer->answer === '') {
125 $params['id'] = $PAGE->cm->id;
126 $params['pageid'] = $this->properties->id;
127 $params['sesskey'] = sesskey();
128 $params['jumpto'] = $answer->jumpto;
129 $url = new moodle_url('/mod/lesson/continue.php', $params);
130 $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)));
133 // Set the orientation
134 if ($this->properties->layout) {
135 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal');
137 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical');
139 $output .= $buttonshtml;
141 if ($this->lesson->slideshow) {
142 $output .= $renderer->slideshow_end();
148 public function check_answer() {
149 global $USER, $DB, $PAGE, $CFG;
152 $newpageid = optional_param('jumpto', NULL, PARAM_INT);
153 // going to insert into lesson_branch
154 if ($newpageid == LESSON_RANDOMBRANCH) {
159 if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) {
160 $retries = count($grades);
164 $branch = new stdClass;
165 $branch->lessonid = $this->lesson->id;
166 $branch->userid = $USER->id;
167 $branch->pageid = $this->properties->id;
168 $branch->retry = $retries;
169 $branch->flag = $branchflag;
170 $branch->timeseen = time();
172 $DB->insert_record("lesson_branch", $branch);
174 // this is called when jumping to random from a branch table
175 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
176 if($newpageid == LESSON_UNSEENBRANCHPAGE) {
177 if (has_capability('mod/lesson:manage', $context)) {
178 $newpageid = LESSON_NEXTPAGE;
180 $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id); // this may return 0
183 // convert jumpto page into a proper page id
184 if ($newpageid == 0) {
185 $newpageid = $this->properties->id;
186 } elseif ($newpageid == LESSON_NEXTPAGE) {
187 if (!$newpageid = $this->nextpageid) {
188 // no nextpage go to end of lesson
189 $newpageid = LESSON_EOL;
191 } elseif ($newpageid == LESSON_PREVIOUSPAGE) {
192 $newpageid = $this->prevpageid;
193 } elseif ($newpageid == LESSON_RANDOMPAGE) {
194 $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id);
195 } elseif ($newpageid == LESSON_RANDOMBRANCH) {
196 $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id);
198 // no need to record anything in lesson_attempts
199 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id,'pageid'=>$newpageid)));
202 public function display_answers(html_table $table) {
203 $answers = $this->get_answers();
204 $options = new stdClass;
205 $options->noclean = true;
206 $options->para = false;
208 foreach ($answers as $answer) {
209 if ($answer->answer === '') {
214 $cells[] = "<span class=\"label\">".get_string("branch", "lesson")." $i<span>: ";
215 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
216 $table->data[] = new html_table_row($cells);
219 $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
220 $cells[] = $this->get_jump_name($answer->jumpto);
221 $table->data[] = new html_table_row($cells);
224 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
230 public function get_grayout() {
233 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
234 $answers = $this->get_answers();
235 $formattextdefoptions = new stdClass;
236 $formattextdefoptions->para = false; //I'll use it widely in this page
237 foreach ($answers as $answer) {
238 $data = "<input type=\"button\" name=\"$answer->id\" value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE,$formattextdefoptions)))."\" disabled=\"disabled\"> ";
239 $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto));
240 $answerdata->answers[] = array($data, "");
241 $answerpage->answerdata = $answerdata;
246 public function update($properties, $context = null, $maxbytes = null) {
247 if (empty($properties->display)) {
248 $properties->display = '0';
250 if (empty($properties->layout)) {
251 $properties->layout = '0';
253 return parent::update($properties);
255 public function add_page_link($previd) {
257 $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE));
258 return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson'));
260 protected function get_displayinmenublock() {
263 public function is_unseen($param) {
265 if (is_array($param)) {
267 $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
268 foreach ($branchpages as $branchpage) {
269 if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed
276 if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
284 class lesson_add_page_form_branchtable extends lesson_add_page_form_base {
286 public $qtype = LESSON_PAGE_BRANCHTABLE;
287 public $qtypestring = 'branchtable';
288 protected $standard = false;
290 public function custom_definition() {
293 $mform = $this->_form;
294 $lesson = $this->_customdata['lesson'];
296 $firstpage = optional_param('firstpage', false, PARAM_BOOL);
298 $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson);
300 $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson'));
302 $mform->addElement('hidden', 'firstpage');
303 $mform->setType('firstpage', PARAM_BOOL);
304 $mform->setDefault('firstpage', $firstpage);
306 $mform->addElement('hidden', 'qtype');
307 $mform->setType('qtype', PARAM_INT);
309 $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
310 $mform->setType('title', PARAM_TEXT);
311 $mform->addRule('title', null, 'required', null, 'server');
313 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
314 $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
315 $mform->setType('contents_editor', PARAM_RAW);
317 $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson"));
318 $mform->setDefault('layout', true);
320 $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson"));
321 $mform->setDefault('display', true);
323 for ($i = 0; $i < $lesson->maxanswers; $i++) {
324 $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1));
325 $this->add_answer($i, get_string("description", "lesson"), $i == 0);
327 $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions);
329 $mform->setDefault('jumpto['.$i.']', 0);
331 $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE);