See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]
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 * Branch Table 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 /** Branch Table page */ 29 define("LESSON_PAGE_BRANCHTABLE", "20"); 30 31 class lesson_page_type_branchtable extends lesson_page { 32 33 protected $type = lesson_page::TYPE_STRUCTURE; 34 protected $typeid = LESSON_PAGE_BRANCHTABLE; 35 protected $typeidstring = 'branchtable'; 36 protected $string = null; 37 protected $jumpto = null; 38 39 public function get_typeid() { 40 return $this->typeid; 41 } 42 public function get_typestring() { 43 if ($this->string===null) { 44 $this->string = get_string($this->typeidstring, 'lesson'); 45 } 46 return $this->string; 47 } 48 49 /** 50 * Gets an array of the jumps used by the answers of this page 51 * 52 * @return array 53 */ 54 public function get_jumps() { 55 global $DB; 56 $jumps = array(); 57 $params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id); 58 if ($answers = $this->get_answers()) { 59 foreach ($answers as $answer) { 60 if ($answer->answer === '') { 61 // show only jumps for real branches (==have description) 62 continue; 63 } 64 $jumps[] = $this->get_jump_name($answer->jumpto); 65 } 66 } else { 67 // We get here is the lesson was created on a Moodle 1.9 site and 68 // the lesson contains question pages without any answers. 69 $jumps[] = $this->get_jump_name($this->properties->nextpageid); 70 } 71 return $jumps; 72 } 73 74 public static function get_jumptooptions($firstpage, lesson $lesson) { 75 global $DB, $PAGE; 76 $jump = array(); 77 $jump[0] = get_string("thispage", "lesson"); 78 $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson"); 79 $jump[LESSON_PREVIOUSPAGE] = get_string("previouspage", "lesson"); 80 $jump[LESSON_EOL] = get_string("endoflesson", "lesson"); 81 $jump[LESSON_UNSEENBRANCHPAGE] = get_string("unseenpageinbranch", "lesson"); 82 $jump[LESSON_RANDOMPAGE] = get_string("randompageinbranch", "lesson"); 83 $jump[LESSON_RANDOMBRANCH] = get_string("randombranch", "lesson"); 84 85 if (!$firstpage) { 86 if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) { 87 throw new \moodle_exception('cannotfindfirstpage', 'lesson'); 88 } 89 while (true) { 90 if ($apageid) { 91 $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid)); 92 $jump[$apageid] = $title; 93 $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid)); 94 } else { 95 // last page reached 96 break; 97 } 98 } 99 } 100 return $jump; 101 } 102 public function get_idstring() { 103 return $this->typeidstring; 104 } 105 public function display($renderer, $attempt) { 106 global $PAGE, $CFG; 107 108 $output = ''; 109 $options = new stdClass; 110 $options->para = false; 111 $options->noclean = true; 112 113 if ($this->lesson->slideshow) { 114 $output .= $renderer->slideshow_start($this->lesson); 115 } 116 117 // The heading level depends on whether the theme's activity header displays a heading (usually the activity name). 118 $headinglevel = $PAGE->activityheader->get_heading_level(); 119 $output .= $renderer->heading(format_string($this->properties->title), $headinglevel); 120 $output .= $renderer->box($this->get_contents(), 'contents'); 121 122 $buttons = array(); 123 $i = 0; 124 foreach ($this->get_answers() as $answer) { 125 if ($answer->answer === '') { 126 // not a branch! 127 continue; 128 } 129 $params = array(); 130 $params['id'] = $PAGE->cm->id; 131 $params['pageid'] = $this->properties->id; 132 $params['sesskey'] = sesskey(); 133 $params['jumpto'] = $answer->jumpto; 134 $url = new moodle_url('/mod/lesson/continue.php', $params); 135 $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options))); 136 $i++; 137 } 138 // Set the orientation 139 if ($this->properties->layout) { 140 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal'); 141 } else { 142 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical'); 143 } 144 $output .= $buttonshtml; 145 146 if ($this->lesson->slideshow) { 147 $output .= $renderer->slideshow_end(); 148 } 149 150 // Trigger an event: content page viewed. 151 $eventparams = array( 152 'context' => context_module::instance($PAGE->cm->id), 153 'objectid' => $this->properties->id 154 ); 155 156 $event = \mod_lesson\event\content_page_viewed::create($eventparams); 157 $event->trigger(); 158 159 return $output; 160 } 161 162 public function check_answer() { 163 global $USER, $DB, $PAGE, $CFG; 164 165 $result = parent::check_answer(); 166 167 require_sesskey(); 168 $newpageid = optional_param('jumpto', null, PARAM_INT); 169 // going to insert into lesson_branch 170 if ($newpageid == LESSON_RANDOMBRANCH) { 171 $branchflag = 1; 172 } else { 173 $branchflag = 0; 174 } 175 if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) { 176 $retries = count($grades); 177 } else { 178 $retries = 0; 179 } 180 181 // First record this page in lesson_branch. This record may be needed by lesson_unseen_branch_jump. 182 $branch = new stdClass; 183 $branch->lessonid = $this->lesson->id; 184 $branch->userid = $USER->id; 185 $branch->pageid = $this->properties->id; 186 $branch->retry = $retries; 187 $branch->flag = $branchflag; 188 $branch->timeseen = time(); 189 $branch->nextpageid = 0; // Next page id will be set later. 190 $branch->id = $DB->insert_record("lesson_branch", $branch); 191 192 // this is called when jumping to random from a branch table 193 $context = context_module::instance($PAGE->cm->id); 194 if($newpageid == LESSON_UNSEENBRANCHPAGE) { 195 if (has_capability('mod/lesson:manage', $context)) { 196 $newpageid = LESSON_NEXTPAGE; 197 } else { 198 $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id); // this may return 0 199 } 200 } 201 // convert jumpto page into a proper page id 202 if ($newpageid == 0) { 203 $newpageid = $this->properties->id; 204 } elseif ($newpageid == LESSON_NEXTPAGE) { 205 if (!$newpageid = $this->nextpageid) { 206 // no nextpage go to end of lesson 207 $newpageid = LESSON_EOL; 208 } 209 } elseif ($newpageid == LESSON_PREVIOUSPAGE) { 210 $newpageid = $this->prevpageid; 211 } elseif ($newpageid == LESSON_RANDOMPAGE) { 212 $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id); 213 } elseif ($newpageid == LESSON_RANDOMBRANCH) { 214 $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id); 215 } 216 217 // Update record to set nextpageid. 218 $branch->nextpageid = $newpageid; 219 $DB->update_record("lesson_branch", $branch); 220 221 // This will force to redirect to the newpageid. 222 $result->inmediatejump = true; 223 $result->newpageid = $newpageid; 224 return $result; 225 } 226 227 public function display_answers(html_table $table) { 228 $answers = $this->get_answers(); 229 $options = new stdClass; 230 $options->noclean = true; 231 $options->para = false; 232 $i = 1; 233 foreach ($answers as $answer) { 234 if ($answer->answer === '') { 235 // not a branch! 236 continue; 237 } 238 $cells = array(); 239 $cells[] = '<label>' . get_string('branch', 'lesson') . ' ' . $i . '</label>: '; 240 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 241 $table->data[] = new html_table_row($cells); 242 243 $cells = array(); 244 $cells[] = '<label>' . get_string('jump', 'lesson') . ' ' . $i . '</label>: '; 245 $cells[] = $this->get_jump_name($answer->jumpto); 246 $table->data[] = new html_table_row($cells); 247 248 if ($i === 1){ 249 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 250 } 251 $i++; 252 } 253 return $table; 254 } 255 public function get_grayout() { 256 return 1; 257 } 258 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 259 $answers = $this->get_answers(); 260 $formattextdefoptions = new stdClass; 261 $formattextdefoptions->para = false; //I'll use it widely in this page 262 $formattextdefoptions->context = $answerpage->context; 263 264 foreach ($answers as $answer) { 265 $data = "<input type=\"button\" class=\"btn btn-secondary\" name=\"$answer->id\" " . 266 "value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE, $formattextdefoptions)))."\" " . 267 "disabled=\"disabled\"> "; 268 $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto)); 269 $answerdata->answers[] = array($data, ""); 270 $answerpage->answerdata = $answerdata; 271 } 272 return $answerpage; 273 } 274 275 public function update($properties, $context = null, $maxbytes = null) { 276 if (empty($properties->display)) { 277 $properties->display = '0'; 278 } 279 if (empty($properties->layout)) { 280 $properties->layout = '0'; 281 } 282 return parent::update($properties); 283 } 284 public function add_page_link($previd) { 285 global $PAGE, $CFG; 286 $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE)); 287 return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson')); 288 } 289 protected function get_displayinmenublock() { 290 return true; 291 } 292 public function is_unseen($param) { 293 global $USER, $DB; 294 if (is_array($param)) { 295 $seenpages = $param; 296 $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH)); 297 foreach ($branchpages as $branchpage) { 298 if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed 299 return false; 300 } 301 } 302 return true; 303 } else { 304 $nretakes = $param; 305 if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) { 306 return true; 307 } 308 return false; 309 } 310 } 311 } 312 313 class lesson_add_page_form_branchtable extends lesson_add_page_form_base { 314 315 public $qtype = LESSON_PAGE_BRANCHTABLE; 316 public $qtypestring = 'branchtable'; 317 protected $standard = false; 318 319 public function custom_definition() { 320 global $PAGE, $CFG; 321 322 $mform = $this->_form; 323 $lesson = $this->_customdata['lesson']; 324 325 $firstpage = optional_param('firstpage', false, PARAM_BOOL); 326 327 $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson); 328 329 if ($this->_customdata['edit']) { 330 $mform->setDefault('qtypeheading', get_string('editbranchtable', 'lesson')); 331 } else { 332 $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson')); 333 } 334 335 $mform->addElement('hidden', 'firstpage'); 336 $mform->setType('firstpage', PARAM_BOOL); 337 $mform->setDefault('firstpage', $firstpage); 338 339 $mform->addElement('hidden', 'qtype'); 340 $mform->setType('qtype', PARAM_INT); 341 342 $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70)); 343 $mform->addRule('title', null, 'required', null, 'server'); 344 if (!empty($CFG->formatstringstriptags)) { 345 $mform->setType('title', PARAM_TEXT); 346 } else { 347 $mform->setType('title', PARAM_CLEANHTML); 348 } 349 350 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes); 351 $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions); 352 $mform->setType('contents_editor', PARAM_RAW); 353 354 $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson")); 355 $mform->setDefault('layout', true); 356 357 $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson")); 358 $mform->setDefault('display', true); 359 360 for ($i = 0; $i < $lesson->maxanswers; $i++) { 361 $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1)); 362 $this->add_answer($i, get_string("description", "lesson"), $i == 0); 363 364 $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions); 365 if ($i === 0) { 366 $mform->setDefault('jumpto['.$i.']', 0); 367 } else { 368 $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE); 369 } 370 } 371 } 372 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body