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   * Cluster
  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   /** Start of Cluster page */
  29  define("LESSON_PAGE_CLUSTER",   "30");
  30  
  31  class lesson_page_type_cluster extends lesson_page {
  32  
  33      protected $type = lesson_page::TYPE_STRUCTURE;
  34      protected $typeidstring = 'cluster';
  35      protected $typeid = LESSON_PAGE_CLUSTER;
  36      protected $string = null;
  37      protected $jumpto = null;
  38  
  39      public function display($renderer, $attempt) {
  40          return '';
  41      }
  42  
  43      public function get_typeid() {
  44          return $this->typeid;
  45      }
  46      public function get_typestring() {
  47          if ($this->string===null) {
  48              $this->string = get_string($this->typeidstring, 'lesson');
  49          }
  50          return $this->string;
  51      }
  52      public function get_idstring() {
  53          return $this->typeidstring;
  54      }
  55      public function get_grayout() {
  56          return 1;
  57      }
  58      public function callback_on_view($canmanage, $redirect = true) {
  59          global $USER;
  60          if (!$canmanage) {
  61              // Get the next page in the lesson cluster jump
  62              return (int) $this->lesson->cluster_jump($this->properties->id);
  63          } else {
  64              // get the next page
  65              return (int) $this->properties->nextpageid;
  66          }
  67      }
  68      public function override_next_page() {
  69          global $USER;
  70          return $this->lesson->cluster_jump($this->properties->id);
  71      }
  72      public function add_page_link($previd) {
  73          global $PAGE, $CFG;
  74          $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_CLUSTER));
  75          return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_CLUSTER, 'name'=>get_string('addcluster', 'lesson'));
  76      }
  77      public function valid_page_and_view(&$validpages, &$pageviews) {
  78          $validpages[$this->properties->id] = 1;  // add the cluster page as a valid page
  79          foreach ($this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_ENDOFCLUSTER)) as $subpage) {
  80              if (in_array($subpage->id, $pageviews)) {
  81                  unset($pageviews[array_search($subpage->id, $pageviews)]);  // remove it
  82                  // since the user did see one page in the cluster, add the cluster pageid to the viewedpageids
  83                  if (!in_array($this->properties->id, $pageviews)) {
  84                      $pageviews[] = $this->properties->id;
  85                  }
  86              }
  87          }
  88          return $this->properties->nextpageid;
  89      }
  90  
  91      /**
  92       * Creates answers within the database for this cluster page. Usually only ever
  93       * called when creating a new page instance.
  94       * @param object $properties
  95       * @return array
  96       */
  97      public function create_answers($properties) {
  98          global $DB;
  99  
 100          $newanswer = new stdClass;
 101          $newanswer->lessonid = $this->lesson->id;
 102          $newanswer->pageid = $this->properties->id;
 103          $newanswer->timecreated = $this->properties->timecreated;
 104  
 105          if (isset($properties->jumpto[0])) {
 106              $newanswer->jumpto = $properties->jumpto[0];
 107          }
 108          $newanswer->id = $DB->insert_record('lesson_answers', $newanswer);
 109          $answers = [$newanswer->id => new lesson_page_answer($newanswer)];
 110          $this->answers = $answers;
 111          return $answers;
 112      }
 113  }
 114  
 115  class lesson_add_page_form_cluster extends lesson_add_page_form_base {
 116  
 117      public $qtype = LESSON_PAGE_CLUSTER;
 118      public $qtypestring = 'cluster';
 119      protected $standard = false;
 120  
 121      public function custom_definition() {
 122          global $PAGE, $CFG;
 123  
 124          $mform = $this->_form;
 125          $lesson = $this->_customdata['lesson'];
 126          $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
 127  
 128          $mform->addElement('hidden', 'firstpage');
 129          $mform->setType('firstpage', PARAM_BOOL);
 130  
 131          $mform->addElement('hidden', 'qtype');
 132          $mform->setType('qtype', PARAM_TEXT);
 133  
 134          $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
 135          if (!empty($CFG->formatstringstriptags)) {
 136              $mform->setType('title', PARAM_TEXT);
 137          } else {
 138              $mform->setType('title', PARAM_CLEANHTML);
 139          }
 140  
 141          $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
 142          $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
 143          $mform->setType('contents_editor', PARAM_RAW);
 144  
 145          $this->add_jumpto(0);
 146      }
 147  
 148  
 149      public function construction_override($pageid, lesson $lesson) {
 150          global $PAGE, $CFG, $DB;
 151          require_sesskey();
 152  
 153          $timenow = time();
 154  
 155          if ($pageid == 0) {
 156              if ($lesson->has_pages()) {
 157                  if (!$page = $DB->get_record("lesson_pages", array("prevpageid" => 0, "lessonid" => $lesson->id))) {
 158                      throw new \moodle_exception('cannotfindpagerecord', 'lesson');
 159                  }
 160              } else {
 161                  // This is the ONLY page
 162                  $page = new stdClass;
 163                  $page->id = 0;
 164              }
 165          } else {
 166              if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
 167                  throw new \moodle_exception('cannotfindpagerecord', 'lesson');
 168              }
 169          }
 170          $newpage = new stdClass;
 171          $newpage->lessonid = $lesson->id;
 172          $newpage->prevpageid = $pageid;
 173          if ($pageid != 0) {
 174              $newpage->nextpageid = $page->nextpageid;
 175          } else {
 176              $newpage->nextpageid = $page->id;
 177          }
 178          $newpage->qtype = $this->qtype;
 179          $newpage->timecreated = $timenow;
 180          $newpage->title = get_string("clustertitle", "lesson");
 181          $newpage->contents = get_string("clustertitle", "lesson");
 182          $newpageid = $DB->insert_record("lesson_pages", $newpage);
 183          // update the linked list...
 184          if ($pageid != 0) {
 185              $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
 186          }
 187  
 188          if ($pageid == 0) {
 189              $page->nextpageid = $page->id;
 190          }
 191          if ($page->nextpageid) {
 192              // the new page is not the last page
 193              $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
 194          }
 195          // ..and the single "answer"
 196          $newanswer = new stdClass;
 197          $newanswer->lessonid = $lesson->id;
 198          $newanswer->pageid = $newpageid;
 199          $newanswer->timecreated = $timenow;
 200          $newanswer->jumpto = LESSON_CLUSTERJUMP;
 201          $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
 202          $lesson->add_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
 203          redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
 204      }
 205  }