Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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   * 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       * Creates answers within the database for this end of cluster page. Usually only ever
 121       * called when creating a new page instance.
 122       * @param object $properties
 123       * @return array
 124       */
 125      public function create_answers($properties) {
 126          global $DB;
 127  
 128          $newanswer = new stdClass;
 129          $newanswer->lessonid = $this->lesson->id;
 130          $newanswer->pageid = $this->properties->id;
 131          $newanswer->timecreated = $this->properties->timecreated;
 132  
 133          if (isset($properties->jumpto[0])) {
 134              $newanswer->jumpto = $properties->jumpto[0];
 135          }
 136          $newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
 137          $answers = [$newanswer->id => new lesson_page_answer($newanswer)];
 138          $this->answers = $answers;
 139          return $answers;
 140      }
 141  }
 142  
 143  class lesson_add_page_form_endofbranch extends lesson_add_page_form_base {
 144  
 145      public $qtype = LESSON_PAGE_ENDOFBRANCH;
 146      public $qtypestring = 'endofbranch';
 147      protected $standard = false;
 148  
 149      public function custom_definition() {
 150          global $PAGE, $CFG;
 151  
 152          $mform = $this->_form;
 153          $lesson = $this->_customdata['lesson'];
 154          $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
 155  
 156          $mform->addElement('hidden', 'firstpage');
 157          $mform->setType('firstpage', PARAM_BOOL);
 158  
 159          $mform->addElement('hidden', 'qtype');
 160          $mform->setType('qtype', PARAM_TEXT);
 161  
 162          $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
 163          if (!empty($CFG->formatstringstriptags)) {
 164              $mform->setType('title', PARAM_TEXT);
 165          } else {
 166              $mform->setType('title', PARAM_CLEANHTML);
 167          }
 168  
 169          $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
 170          $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
 171          $mform->setType('contents_editor', PARAM_RAW);
 172  
 173          $this->add_jumpto(0);
 174      }
 175  
 176      public function construction_override($pageid, lesson $lesson) {
 177          global $DB, $CFG, $PAGE;
 178          require_sesskey();
 179  
 180          // first get the preceeding page
 181  
 182          $timenow = time();
 183  
 184          // the new page is not the first page (end of branch always comes after an existing page)
 185          if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
 186              throw new \moodle_exception('cannotfindpagerecord', 'lesson');
 187          }
 188          // chain back up to find the (nearest branch table)
 189          $btpage = clone($page);
 190          $btpageid = $btpage->id;
 191          while (($btpage->qtype != LESSON_PAGE_BRANCHTABLE) && ($btpage->prevpageid > 0)) {
 192              $btpageid = $btpage->prevpageid;
 193              if (!$btpage = $DB->get_record("lesson_pages", array("id" => $btpageid))) {
 194                  throw new \moodle_exception('cannotfindpagerecord', 'lesson');
 195              }
 196          }
 197  
 198          if ($btpage->qtype == LESSON_PAGE_BRANCHTABLE) {
 199              $newpage = new stdClass;
 200              $newpage->lessonid = $lesson->id;
 201              $newpage->prevpageid = $pageid;
 202              $newpage->nextpageid = $page->nextpageid;
 203              $newpage->qtype = $this->qtype;
 204              $newpage->timecreated = $timenow;
 205              $newpage->title = get_string("endofbranch", "lesson");
 206              $newpage->contents = get_string("endofbranch", "lesson");
 207              $newpageid = $DB->insert_record("lesson_pages", $newpage);
 208              // update the linked list...
 209              $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
 210              if ($page->nextpageid) {
 211                  // the new page is not the last page
 212                  $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
 213              }
 214              // ..and the single "answer"
 215              $newanswer = new stdClass;
 216              $newanswer->lessonid = $lesson->id;
 217              $newanswer->pageid = $newpageid;
 218              $newanswer->timecreated = $timenow;
 219              $newanswer->jumpto = $btpageid;
 220              $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
 221              $lesson->add_message(get_string('addedanendofbranch', 'lesson'), 'notifysuccess');
 222          } else {
 223              $lesson->add_message(get_string('nobranchtablefound', 'lesson'));
 224          }
 225  
 226          redirect($CFG->wwwroot."/mod/lesson/edit.php?id=".$PAGE->cm->id);
 227      }
 228  }