3965bf72cb00e9ecd0573fe8a3f49b1e3769359f
[moodle.git] / mod / lesson / pagetypes / branchtable.php
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.
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/>.
18 /**
19  * Branch Table
20  *
21  * @package    mod
22  * @subpackage lesson
23  * @copyright  2009 Sam Hemelryk
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  **/
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() {
41         return $this->typeid;
42     }
43     public function get_typestring() {
44         if ($this->string===null) {
45             $this->string = get_string($this->typeidstring, 'lesson');
46         }
47         return $this->string;
48     }
50     /**
51      * Gets an array of the jumps used by the answers of this page
52      *
53      * @return array
54      */
55     public function get_jumps() {
56         global $DB;
57         $jumps = array();
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)
63                     continue;
64                 }
65                 $jumps[] = $this->get_jump_name($answer->jumpto);
66             }
67         } else {
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);
71         }
72         return $jumps;
73     }
75     public static function get_jumptooptions($firstpage, lesson $lesson) {
76         global $DB, $PAGE;
77         $jump = array();
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         if (!$firstpage) {
83             if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) {
84                 print_error('cannotfindfirstpage', 'lesson');
85             }
86             while (true) {
87                 if ($apageid) {
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));
91                 } else {
92                     // last page reached
93                     break;
94                 }
95             }
96          }
97         return $jump;
98     }
99     public function get_idstring() {
100         return $this->typeidstring;
101     }
102     public function display($renderer, $attempt) {
103         global $PAGE, $CFG;
105         $output = '';
106         $options = new stdClass;
107         $options->para = false;
108         $options->noclean = true;
110         if ($this->lesson->slideshow) {
111             $output .= $renderer->slideshow_start($this->lesson);
112         }
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');
117         $buttons = array();
118         $i = 0;
119         foreach ($this->get_answers() as $answer) {
120             if ($answer->answer === '') {
121                 // not a branch!
122                 continue;
123             }
124             $params = array();
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)));
131             $i++;
132         }
133         // Set the orientation
134         if ($this->properties->layout) {
135             $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal');
136         } else {
137             $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical');
138         }
139         $output .= $buttonshtml;
141         if ($this->lesson->slideshow) {
142             $output .= $renderer->slideshow_end();
143         }
145         return $output;
146     }
148     public function check_answer() {
149         global $USER, $DB, $PAGE, $CFG;
151         require_sesskey();
152         $newpageid = optional_param('jumpto', NULL, PARAM_INT);
153         // going to insert into lesson_branch
154         if ($newpageid == LESSON_RANDOMBRANCH) {
155             $branchflag = 1;
156         } else {
157             $branchflag = 0;
158         }
159         if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) {
160             $retries = count($grades);
161         } else {
162             $retries = 0;
163         }
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;
179             } else {
180                  $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id);  // this may return 0
181             }
182         }
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;
190             }
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);
197         }
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)));
200     }
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;
207         $i = 1;
208         foreach ($answers as $answer) {
209             if ($answer->answer === '') {
210                 // not a branch!
211                 continue;
212             }
213             $cells = array();
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);
218             $cells = array();
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);
223             if ($i === 1){
224                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
225             }
226             $i++;
227         }
228         return $table;
229     }
230     public function get_grayout() {
231         return 1;
232     }
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;
242         }
243         return $answerpage;
244     }
246     public function update($properties, $context = null, $maxbytes = null) {
247         if (empty($properties->display)) {
248             $properties->display = '0';
249         }
250         if (empty($properties->layout)) {
251             $properties->layout = '0';
252         }
253         return parent::update($properties);
254     }
255     public function add_page_link($previd) {
256         global $PAGE, $CFG;
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'));
259     }
260     protected function get_displayinmenublock() {
261         return true;
262     }
263     public function is_unseen($param) {
264         global $USER, $DB;
265         if (is_array($param)) {
266             $seenpages = $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
270                     return false;
271                 }
272             }
273             return true;
274         } else {
275             $nretakes = $param;
276             if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
277                 return true;
278             }
279             return false;
280         }
281     }
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() {
291         global $PAGE;
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);
328             if ($i === 0) {
329                 $mform->setDefault('jumpto['.$i.']', 0);
330             } else {
331                 $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE);
332             }
333         }
334     }