Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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   * 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  class lesson_add_page_form_cluster extends lesson_add_page_form_base {
  93  
  94      public $qtype = LESSON_PAGE_CLUSTER;
  95      public $qtypestring = 'cluster';
  96      protected $standard = false;
  97  
  98      public function custom_definition() {
  99          global $PAGE, $CFG;
 100  
 101          $mform = $this->_form;
 102          $lesson = $this->_customdata['lesson'];
 103          $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
 104  
 105          $mform->addElement('hidden', 'firstpage');
 106          $mform->setType('firstpage', PARAM_BOOL);
 107  
 108          $mform->addElement('hidden', 'qtype');
 109          $mform->setType('qtype', PARAM_TEXT);
 110  
 111          $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
 112          if (!empty($CFG->formatstringstriptags)) {
 113              $mform->setType('title', PARAM_TEXT);
 114          } else {
 115              $mform->setType('title', PARAM_CLEANHTML);
 116          }
 117  
 118          $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
 119          $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
 120          $mform->setType('contents_editor', PARAM_RAW);
 121  
 122          $this->add_jumpto(0);
 123      }
 124  
 125  
 126      public function construction_override($pageid, lesson $lesson) {
 127          global $PAGE, $CFG, $DB;
 128          require_sesskey();
 129  
 130          $timenow = time();
 131  
 132          if ($pageid == 0) {
 133              if ($lesson->has_pages()) {
 134                  if (!$page = $DB->get_record("lesson_pages", array("prevpageid" => 0, "lessonid" => $lesson->id))) {
 135                      print_error('cannotfindpagerecord', 'lesson');
 136                  }
 137              } else {
 138                  // This is the ONLY page
 139                  $page = new stdClass;
 140                  $page->id = 0;
 141              }
 142          } else {
 143              if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
 144                  print_error('cannotfindpagerecord', 'lesson');
 145              }
 146          }
 147          $newpage = new stdClass;
 148          $newpage->lessonid = $lesson->id;
 149          $newpage->prevpageid = $pageid;
 150          if ($pageid != 0) {
 151              $newpage->nextpageid = $page->nextpageid;
 152          } else {
 153              $newpage->nextpageid = $page->id;
 154          }
 155          $newpage->qtype = $this->qtype;
 156          $newpage->timecreated = $timenow;
 157          $newpage->title = get_string("clustertitle", "lesson");
 158          $newpage->contents = get_string("clustertitle", "lesson");
 159          $newpageid = $DB->insert_record("lesson_pages", $newpage);
 160          // update the linked list...
 161          if ($pageid != 0) {
 162              $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
 163          }
 164  
 165          if ($pageid == 0) {
 166              $page->nextpageid = $page->id;
 167          }
 168          if ($page->nextpageid) {
 169              // the new page is not the last page
 170              $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
 171          }
 172          // ..and the single "answer"
 173          $newanswer = new stdClass;
 174          $newanswer->lessonid = $lesson->id;
 175          $newanswer->pageid = $newpageid;
 176          $newanswer->timecreated = $timenow;
 177          $newanswer->jumpto = LESSON_CLUSTERJUMP;
 178          $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
 179          $lesson->add_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
 180          redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
 181      }
 182  }