MDL-27710 lesson module: add the following jump options: unseen branch page, random...
[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         $jump[LESSON_UNSEENBRANCHPAGE] = get_string("unseenpageinbranch", "lesson");
83         $jump[LESSON_RANDOMPAGE] = get_string("randompageinbranch", "lesson");
84         $jump[LESSON_RANDOMBRANCH] = get_string("randombranch", "lesson");
86         if (!$firstpage) {
87             if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) {
88                 print_error('cannotfindfirstpage', 'lesson');
89             }
90             while (true) {
91                 if ($apageid) {
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));
95                 } else {
96                     // last page reached
97                     break;
98                 }
99             }
100          }
101         return $jump;
102     }
103     public function get_idstring() {
104         return $this->typeidstring;
105     }
106     public function display($renderer, $attempt) {
107         global $PAGE, $CFG;
109         $output = '';
110         $options = new stdClass;
111         $options->para = false;
112         $options->noclean = true;
114         if ($this->lesson->slideshow) {
115             $output .= $renderer->slideshow_start($this->lesson);
116         }
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');
121         $buttons = array();
122         $i = 0;
123         foreach ($this->get_answers() as $answer) {
124             if ($answer->answer === '') {
125                 // not a branch!
126                 continue;
127             }
128             $params = array();
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)));
135             $i++;
136         }
137         // Set the orientation
138         if ($this->properties->layout) {
139             $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal');
140         } else {
141             $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical');
142         }
143         $output .= $buttonshtml;
145         if ($this->lesson->slideshow) {
146             $output .= $renderer->slideshow_end();
147         }
149         return $output;
150     }
152     public function check_answer() {
153         global $USER, $DB, $PAGE, $CFG;
155         require_sesskey();
156         $newpageid = optional_param('jumpto', NULL, PARAM_INT);
157         // going to insert into lesson_branch
158         if ($newpageid == LESSON_RANDOMBRANCH) {
159             $branchflag = 1;
160         } else {
161             $branchflag = 0;
162         }
163         if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) {
164             $retries = count($grades);
165         } else {
166             $retries = 0;
167         }
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;
183             } else {
184                  $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id);  // this may return 0
185             }
186         }
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;
194             }
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);
201         }
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)));
204     }
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;
211         $i = 1;
212         foreach ($answers as $answer) {
213             if ($answer->answer === '') {
214                 // not a branch!
215                 continue;
216             }
217             $cells = array();
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);
222             $cells = array();
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);
227             if ($i === 1){
228                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
229             }
230             $i++;
231         }
232         return $table;
233     }
234     public function get_grayout() {
235         return 1;
236     }
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;
246         }
247         return $answerpage;
248     }
250     public function update($properties, $context = null, $maxbytes = null) {
251         if (empty($properties->display)) {
252             $properties->display = '0';
253         }
254         if (empty($properties->layout)) {
255             $properties->layout = '0';
256         }
257         return parent::update($properties);
258     }
259     public function add_page_link($previd) {
260         global $PAGE, $CFG;
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'));
263     }
264     protected function get_displayinmenublock() {
265         return true;
266     }
267     public function is_unseen($param) {
268         global $USER, $DB;
269         if (is_array($param)) {
270             $seenpages = $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
274                     return false;
275                 }
276             }
277             return true;
278         } else {
279             $nretakes = $param;
280             if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
281                 return true;
282             }
283             return false;
284         }
285     }
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() {
295         global $PAGE;
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);
332             if ($i === 0) {
333                 $mform->setDefault('jumpto['.$i.']', 0);
334             } else {
335                 $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE);
336             }
337         }
338     }