Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]
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 print_error('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 // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911). 117 $output .= $renderer->heading(format_string($this->properties->title), 3); 118 $output .= $renderer->box($this->get_contents(), 'contents'); 119 120 $buttons = array(); 121 $i = 0; 122 foreach ($this->get_answers() as $answer) { 123 if ($answer->answer === '') { 124 // not a branch! 125 continue; 126 } 127 $params = array(); 128 $params['id'] = $PAGE->cm->id; 129 $params['pageid'] = $this->properties->id; 130 $params['sesskey'] = sesskey(); 131 $params['jumpto'] = $answer->jumpto; 132 $url = new moodle_url('/mod/lesson/continue.php', $params); 133 $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options))); 134 $i++; 135 } 136 // Set the orientation 137 if ($this->properties->layout) { 138 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal'); 139 } else { 140 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical'); 141 } 142 $output .= $buttonshtml; 143 144 if ($this->lesson->slideshow) { 145 $output .= $renderer->slideshow_end(); 146 } 147 148 // Trigger an event: content page viewed. 149 $eventparams = array( 150 'context' => context_module::instance($PAGE->cm->id), 151 'objectid' => $this->properties->id 152 ); 153 154 $event = \mod_lesson\event\content_page_viewed::create($eventparams); 155 $event->trigger(); 156 157 return $output; 158 } 159 160 public function check_answer() { 161 global $USER, $DB, $PAGE, $CFG; 162 163 $result = parent::check_answer(); 164 165 require_sesskey(); 166 $newpageid = optional_param('jumpto', null, PARAM_INT); 167 // going to insert into lesson_branch 168 if ($newpageid == LESSON_RANDOMBRANCH) { 169 $branchflag = 1; 170 } else { 171 $branchflag = 0; 172 } 173 if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) { 174 $retries = count($grades); 175 } else { 176 $retries = 0; 177 } 178 179 // First record this page in lesson_branch. This record may be needed by lesson_unseen_branch_jump. 180 $branch = new stdClass; 181 $branch->lessonid = $this->lesson->id; 182 $branch->userid = $USER->id; 183 $branch->pageid = $this->properties->id; 184 $branch->retry = $retries; 185 $branch->flag = $branchflag; 186 $branch->timeseen = time(); 187 $branch->nextpageid = 0; // Next page id will be set later. 188 $branch->id = $DB->insert_record("lesson_branch", $branch); 189 190 // this is called when jumping to random from a branch table 191 $context = context_module::instance($PAGE->cm->id); 192 if($newpageid == LESSON_UNSEENBRANCHPAGE) { 193 if (has_capability('mod/lesson:manage', $context)) { 194 $newpageid = LESSON_NEXTPAGE; 195 } else { 196 $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id); // this may return 0 197 } 198 } 199 // convert jumpto page into a proper page id 200 if ($newpageid == 0) { 201 $newpageid = $this->properties->id; 202 } elseif ($newpageid == LESSON_NEXTPAGE) { 203 if (!$newpageid = $this->nextpageid) { 204 // no nextpage go to end of lesson 205 $newpageid = LESSON_EOL; 206 } 207 } elseif ($newpageid == LESSON_PREVIOUSPAGE) { 208 $newpageid = $this->prevpageid; 209 } elseif ($newpageid == LESSON_RANDOMPAGE) { 210 $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id); 211 } elseif ($newpageid == LESSON_RANDOMBRANCH) { 212 $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id); 213 } 214 215 // Update record to set nextpageid. 216 $branch->nextpageid = $newpageid; 217 $DB->update_record("lesson_branch", $branch); 218 219 // This will force to redirect to the newpageid. 220 $result->inmediatejump = true; 221 $result->newpageid = $newpageid; 222 return $result; 223 } 224 225 public function display_answers(html_table $table) { 226 $answers = $this->get_answers(); 227 $options = new stdClass; 228 $options->noclean = true; 229 $options->para = false; 230 $i = 1; 231 foreach ($answers as $answer) { 232 if ($answer->answer === '') { 233 // not a branch! 234 continue; 235 } 236 $cells = array(); 237 $cells[] = '<label>' . get_string('branch', 'lesson') . ' ' . $i . '</label>: '; 238 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 239 $table->data[] = new html_table_row($cells); 240 241 $cells = array(); 242 $cells[] = '<label>' . get_string('jump', 'lesson') . ' ' . $i . '</label>: '; 243 $cells[] = $this->get_jump_name($answer->jumpto); 244 $table->data[] = new html_table_row($cells); 245 246 if ($i === 1){ 247 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 248 } 249 $i++; 250 } 251 return $table; 252 } 253 public function get_grayout() { 254 return 1; 255 } 256 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 257 $answers = $this->get_answers(); 258 $formattextdefoptions = new stdClass; 259 $formattextdefoptions->para = false; //I'll use it widely in this page 260 $formattextdefoptions->context = $answerpage->context; 261 262 foreach ($answers as $answer) { 263 $data = "<input type=\"button\" class=\"btn btn-secondary\" name=\"$answer->id\" " . 264 "value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE, $formattextdefoptions)))."\" " . 265 "disabled=\"disabled\"> "; 266 $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto)); 267 $answerdata->answers[] = array($data, ""); 268 $answerpage->answerdata = $answerdata; 269 } 270 return $answerpage; 271 } 272 273 public function update($properties, $context = null, $maxbytes = null) { 274 if (empty($properties->display)) { 275 $properties->display = '0'; 276 } 277 if (empty($properties->layout)) { 278 $properties->layout = '0'; 279 } 280 return parent::update($properties); 281 } 282 public function add_page_link($previd) { 283 global $PAGE, $CFG; 284 $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE)); 285 return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson')); 286 } 287 protected function get_displayinmenublock() { 288 return true; 289 } 290 public function is_unseen($param) { 291 global $USER, $DB; 292 if (is_array($param)) { 293 $seenpages = $param; 294 $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH)); 295 foreach ($branchpages as $branchpage) { 296 if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed 297 return false; 298 } 299 } 300 return true; 301 } else { 302 $nretakes = $param; 303 if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) { 304 return true; 305 } 306 return false; 307 } 308 } 309 } 310 311 class lesson_add_page_form_branchtable extends lesson_add_page_form_base { 312 313 public $qtype = LESSON_PAGE_BRANCHTABLE; 314 public $qtypestring = 'branchtable'; 315 protected $standard = false; 316 317 public function custom_definition() { 318 global $PAGE, $CFG; 319 320 $mform = $this->_form; 321 $lesson = $this->_customdata['lesson']; 322 323 $firstpage = optional_param('firstpage', false, PARAM_BOOL); 324 325 $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson); 326 327 if ($this->_customdata['edit']) { 328 $mform->setDefault('qtypeheading', get_string('editbranchtable', 'lesson')); 329 } else { 330 $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson')); 331 } 332 333 $mform->addElement('hidden', 'firstpage'); 334 $mform->setType('firstpage', PARAM_BOOL); 335 $mform->setDefault('firstpage', $firstpage); 336 337 $mform->addElement('hidden', 'qtype'); 338 $mform->setType('qtype', PARAM_INT); 339 340 $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70)); 341 $mform->addRule('title', null, 'required', null, 'server'); 342 if (!empty($CFG->formatstringstriptags)) { 343 $mform->setType('title', PARAM_TEXT); 344 } else { 345 $mform->setType('title', PARAM_CLEANHTML); 346 } 347 348 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes); 349 $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions); 350 $mform->setType('contents_editor', PARAM_RAW); 351 352 $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson")); 353 $mform->setDefault('layout', true); 354 355 $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson")); 356 $mform->setDefault('display', true); 357 358 for ($i = 0; $i < $lesson->maxanswers; $i++) { 359 $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1)); 360 $this->add_answer($i, get_string("description", "lesson"), $i == 0); 361 362 $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions); 363 if ($i === 0) { 364 $mform->setDefault('jumpto['.$i.']', 0); 365 } else { 366 $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE); 367 } 368 } 369 } 370 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body