Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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   * End of 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   /** End of Branch page */
  29  define("LESSON_PAGE_ENDOFBRANCH",   "21");
  30  
  31  class lesson_page_type_endofbranch extends lesson_page {
  32  
  33      protected $type = lesson_page::TYPE_STRUCTURE;
  34      protected $typeidstring = 'endofbranch';
  35      protected $typeid = LESSON_PAGE_ENDOFBRANCH;
  36      protected $string = null;
  37      protected $jumpto = null;
  38  
  39      public function display($renderer, $attempt) {
  40          return '';
  41      }
  42      public function get_typeid() {
  43          return $this->typeid;
  44      }
  45      public function get_typestring() {
  46          if ($this->string===null) {
  47              $this->string = get_string($this->typeidstring, 'lesson');
  48          }
  49          return $this->string;
  50      }
  51      public function get_idstring() {
  52          return $this->typeidstring;
  53      }
  54      public function callback_on_view($canmanage, $redirect = true) {
  55          return (int) $this->redirect_to_first_answer($canmanage, $redirect);
  56      }
  57  
  58      public function redirect_to_first_answer($canmanage, $redirect) {
  59          global $USER, $PAGE;
  60          $answers = $this->get_answers();
  61          $answer = array_shift($answers);
  62          $jumpto = $answer->jumpto;
  63          if ($jumpto == LESSON_RANDOMBRANCH) {
  64  
  65              $jumpto = lesson_unseen_branch_jump($this->lesson, $USER->id);
  66  
  67          } elseif ($jumpto == LESSON_CLUSTERJUMP) {
  68  
  69              if (!$canmanage) {
  70                  $jumpto = $this->lesson->cluster_jump($this->properties->id);
  71              } else {
  72                  if ($this->properties->nextpageid == 0) {
  73                      $jumpto = LESSON_EOL;
  74                  } else {
  75                      $jumpto = $this->properties->nextpageid;
  76                  }
  77              }
  78  
  79          } else if ($answer->jumpto == LESSON_NEXTPAGE) {
  80  
  81              if ($this->properties->nextpageid == 0) {
  82                  $jumpto = LESSON_EOL;
  83              } else {
  84                  $jumpto = $this->properties->nextpageid;
  85              }
  86  
  87          } else if ($jumpto == 0) {
  88  
  89              $jumpto = $this->properties->id;
  90  
  91          } else if ($jumpto == LESSON_PREVIOUSPAGE) {
  92  
  93              $jumpto = $this->properties->prevpageid;
  94  
  95          }
  96  
  97          if ($redirect) {
  98              redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $jumpto)));
  99              die;
 100          }
 101          return $jumpto;
 102      }
 103      public function get_grayout() {
 104          return 1;
 105      }
 106  
 107      public function add_page_link($previd) {
 108          global $PAGE, $CFG;
 109          if ($previd != 0) {
 110              $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_ENDOFBRANCH));
 111              return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_ENDOFBRANCH, 'name'=>get_string('addanendofbranch', 'lesson'));
 112          }
 113          return false;
 114      }
 115      public function valid_page_and_view(&$validpages, &$pageviews) {
 116          return $this->properties->nextpageid;
 117      }
 118  }
 119  
 120  class lesson_add_page_form_endofbranch extends lesson_add_page_form_base {
 121  
 122      public $qtype = LESSON_PAGE_ENDOFBRANCH;
 123      public $qtypestring = 'endofbranch';
 124      protected $standard = false;
 125  
 126      public function custom_definition() {
 127          global $PAGE, $CFG;
 128  
 129          $mform = $this->_form;
 130          $lesson = $this->_customdata['lesson'];
 131          $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
 132  
 133          $mform->addElement('hidden', 'firstpage');
 134          $mform->setType('firstpage', PARAM_BOOL);
 135  
 136          $mform->addElement('hidden', 'qtype');
 137          $mform->setType('qtype', PARAM_TEXT);
 138  
 139          $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
 140          if (!empty($CFG->formatstringstriptags)) {
 141              $mform->setType('title', PARAM_TEXT);
 142          } else {
 143              $mform->setType('title', PARAM_CLEANHTML);
 144          }
 145  
 146          $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
 147          $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
 148          $mform->setType('contents_editor', PARAM_RAW);
 149  
 150          $this->add_jumpto(0);
 151      }
 152  
 153      public function construction_override($pageid, lesson $lesson) {
 154          global $DB, $CFG, $PAGE;
 155          require_sesskey();
 156  
 157          // first get the preceeding page
 158  
 159          $timenow = time();
 160  
 161          // the new page is not the first page (end of branch always comes after an existing page)
 162          if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
 163              print_error('cannotfindpagerecord', 'lesson');
 164          }
 165          // chain back up to find the (nearest branch table)
 166          $btpage = clone($page);
 167          $btpageid = $btpage->id;
 168          while (($btpage->qtype != LESSON_PAGE_BRANCHTABLE) && ($btpage->prevpageid > 0)) {
 169              $btpageid = $btpage->prevpageid;
 170              if (!$btpage = $DB->get_record("lesson_pages", array("id" => $btpageid))) {
 171                  print_error('cannotfindpagerecord', 'lesson');
 172              }
 173          }
 174  
 175          if ($btpage->qtype == LESSON_PAGE_BRANCHTABLE) {
 176              $newpage = new stdClass;
 177              $newpage->lessonid = $lesson->id;
 178              $newpage->prevpageid = $pageid;
 179              $newpage->nextpageid = $page->nextpageid;
 180              $newpage->qtype = $this->qtype;
 181              $newpage->timecreated = $timenow;
 182              $newpage->title = get_string("endofbranch", "lesson");
 183              $newpage->contents = get_string("endofbranch", "lesson");
 184              $newpageid = $DB->insert_record("lesson_pages", $newpage);
 185              // update the linked list...
 186              $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
 187              if ($page->nextpageid) {
 188                  // the new page is not the last page
 189                  $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
 190              }
 191              // ..and the single "answer"
 192              $newanswer = new stdClass;
 193              $newanswer->lessonid = $lesson->id;
 194              $newanswer->pageid = $newpageid;
 195              $newanswer->timecreated = $timenow;
 196              $newanswer->jumpto = $btpageid;
 197              $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
 198              $lesson->add_message(get_string('addedanendofbranch', 'lesson'), 'notifysuccess');
 199          } else {
 200              $lesson->add_message(get_string('nobranchtablefound', 'lesson'));
 201          }
 202  
 203          redirect($CFG->wwwroot."/mod/lesson/edit.php?id=".$PAGE->cm->id);
 204      }
 205  }