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 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Course format class to allow plugins developed for Moodle 2.3 to work in the new API
  19   *
  20   * @package    core_course
  21   * @copyright  2012 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  /**
  28   * Course format class to allow plugins developed for Moodle 2.3 to work in the new API
  29   *
  30   * @package    core_course
  31   * @copyright  2012 Marina Glancy
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class format_legacy extends format_base {
  35  
  36      /**
  37       * Returns true if this course format uses sections
  38       *
  39       * This function calls function callback_FORMATNAME_uses_sections() if it exists
  40       *
  41       * @return bool
  42       */
  43      public function uses_sections() {
  44          global $CFG;
  45          // Note that lib.php in course format folder is already included by now
  46          $featurefunction = 'callback_'.$this->format.'_uses_sections';
  47          if (function_exists($featurefunction)) {
  48              return $featurefunction();
  49          }
  50          return false;
  51      }
  52  
  53      /**
  54       * Returns the display name of the given section that the course prefers.
  55       *
  56       * This function calls function callback_FORMATNAME_get_section_name() if it exists
  57       *
  58       * @param int|stdClass $section Section object from database or just field section.section
  59       * @return string Display name that the course format prefers, e.g. "Topic 2"
  60       */
  61      public function get_section_name($section) {
  62          // Use course formatter callback if it exists
  63          $namingfunction = 'callback_'.$this->format.'_get_section_name';
  64          if (function_exists($namingfunction) && ($course = $this->get_course())) {
  65              return $namingfunction($course, $this->get_section($section));
  66          }
  67  
  68          // else, default behavior:
  69          return parent::get_section_name($section);
  70      }
  71  
  72      /**
  73       * The URL to use for the specified course (with section)
  74       *
  75       * This function calls function callback_FORMATNAME_get_section_url() if it exists
  76       *
  77       * @param int|stdClass $section Section object from database or just field course_sections.section
  78       *     if omitted the course view page is returned
  79       * @param array $options options for view URL. At the moment core uses:
  80       *     'navigation' (bool) if true and section has no separate page, the function returns null
  81       *     'sr' (int) used by multipage formats to specify to which section to return
  82       * @return null|moodle_url
  83       */
  84      public function get_view_url($section, $options = array()) {
  85          // Use course formatter callback if it exists
  86          $featurefunction = 'callback_'.$this->format.'_get_section_url';
  87          if (function_exists($featurefunction) && ($course = $this->get_course())) {
  88              if (is_object($section)) {
  89                  $sectionnum = $section->section;
  90              } else {
  91                  $sectionnum = $section;
  92              }
  93              if ($sectionnum) {
  94                  $url = $featurefunction($course, $sectionnum);
  95                  if ($url || !empty($options['navigation'])) {
  96                      return $url;
  97                  }
  98              }
  99          }
 100  
 101          // if function is not defined
 102          if (!$this->uses_sections() ||
 103                  !array_key_exists('coursedisplay', $this->course_format_options())) {
 104              // default behaviour
 105              return parent::get_view_url($section, $options);
 106          }
 107  
 108          $course = $this->get_course();
 109          $url = new moodle_url('/course/view.php', array('id' => $course->id));
 110  
 111          $sr = null;
 112          if (array_key_exists('sr', $options)) {
 113              $sr = $options['sr'];
 114          }
 115          if (is_object($section)) {
 116              $sectionno = $section->section;
 117          } else {
 118              $sectionno = $section;
 119          }
 120          if ($sectionno !== null) {
 121              if ($sr !== null) {
 122                  if ($sr) {
 123                      $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
 124                      $sectionno = $sr;
 125                  } else {
 126                      $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
 127                  }
 128              } else {
 129                  $usercoursedisplay = $course->coursedisplay;
 130              }
 131              if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
 132                  $url->param('section', $sectionno);
 133              } else {
 134                  if (!empty($options['navigation'])) {
 135                      return null;
 136                  }
 137                  $url->set_anchor('section-'.$sectionno);
 138              }
 139          }
 140          return $url;
 141      }
 142  
 143      /**
 144       * Returns the information about the ajax support in the given source format
 145       *
 146       * This function calls function callback_FORMATNAME_ajax_support() if it exists
 147       *
 148       * The returned object's property (boolean)capable indicates that
 149       * the course format supports Moodle course ajax features.
 150       *
 151       * @return stdClass
 152       */
 153      public function supports_ajax() {
 154          // set up default values
 155          $ajaxsupport = parent::supports_ajax();
 156  
 157          // get the information from the course format library
 158          $featurefunction = 'callback_'.$this->format.'_ajax_support';
 159          if (function_exists($featurefunction)) {
 160              $formatsupport = $featurefunction();
 161              if (isset($formatsupport->capable)) {
 162                  $ajaxsupport->capable = $formatsupport->capable;
 163              }
 164          }
 165          return $ajaxsupport;
 166      }
 167  
 168      /**
 169       * Loads all of the course sections into the navigation
 170       *
 171       * First this function calls callback_FORMATNAME_display_content() if it exists to check
 172       * if the navigation should be extended at all
 173       *
 174       * Then it calls function callback_FORMATNAME_load_content() if it exists to actually extend
 175       * navigation
 176       *
 177       * By default the parent method is called
 178       *
 179       * @param global_navigation $navigation
 180       * @param navigation_node $node The course node within the navigation
 181       */
 182      public function extend_course_navigation($navigation, navigation_node $node) {
 183          global $PAGE;
 184          // if course format displays section on separate pages and we are on course/view.php page
 185          // and the section parameter is specified, make sure this section is expanded in
 186          // navigation
 187          if ($navigation->includesectionnum === false) {
 188              $selectedsection = optional_param('section', null, PARAM_INT);
 189              if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
 190                      $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
 191                  $navigation->includesectionnum = $selectedsection;
 192              }
 193          }
 194  
 195          // check if there are callbacks to extend course navigation
 196          $displayfunc = 'callback_'.$this->format.'_display_content';
 197          if (function_exists($displayfunc) && !$displayfunc()) {
 198              return;
 199          }
 200          $featurefunction = 'callback_'.$this->format.'_load_content';
 201          if (function_exists($featurefunction) && ($course = $this->get_course())) {
 202              $featurefunction($navigation, $course, $node);
 203          } else {
 204              parent::extend_course_navigation($navigation, $node);
 205          }
 206      }
 207  
 208      /**
 209       * Custom action after section has been moved in AJAX mode
 210       *
 211       * Used in course/rest.php
 212       *
 213       * This function calls function callback_FORMATNAME_ajax_section_move() if it exists
 214       *
 215       * @return array This will be passed in ajax respose
 216       */
 217      function ajax_section_move() {
 218          $featurefunction = 'callback_'.$this->format.'_ajax_section_move';
 219          if (function_exists($featurefunction) && ($course = $this->get_course())) {
 220              return $featurefunction($course);
 221          } else {
 222              return parent::ajax_section_move();
 223          }
 224      }
 225  
 226      /**
 227       * Returns the list of blocks to be automatically added for the newly created course
 228       *
 229       * This function checks the existence of the file config.php in the course format folder.
 230       * If file exists and contains the code
 231       * $format['defaultblocks'] = 'leftblock1,leftblock2:rightblock1,rightblock2';
 232       * these blocks are used, otherwise parent function is called
 233       *
 234       * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
 235       *     each of values is an array of block names (for left and right side columns)
 236       */
 237      public function get_default_blocks() {
 238          global $CFG;
 239          $formatconfig = $CFG->dirroot.'/course/format/'.$this->format.'/config.php';
 240          $format = array(); // initialize array in external file
 241          if (is_readable($formatconfig)) {
 242              include($formatconfig);
 243          }
 244          if (!empty($format['defaultblocks'])) {
 245              return blocks_parse_default_blocks_list($format['defaultblocks']);
 246          }
 247          return parent::get_default_blocks();
 248      }
 249  
 250      /**
 251       * Definitions of the additional options that this course format uses for course
 252       *
 253       * By default course formats have the options that existed in Moodle 2.3:
 254       * - coursedisplay
 255       * - numsections
 256       * - hiddensections
 257       *
 258       * @param bool $foreditform
 259       * @return array of options
 260       */
 261      public function course_format_options($foreditform = false) {
 262          static $courseformatoptions = false;
 263          if ($courseformatoptions === false) {
 264              $courseconfig = get_config('moodlecourse');
 265              $courseformatoptions = array(
 266                  'numsections' => array(
 267                      'default' => $courseconfig->numsections,
 268                      'type' => PARAM_INT,
 269                  ),
 270                  'hiddensections' => array(
 271                      'default' => $courseconfig->hiddensections,
 272                      'type' => PARAM_INT,
 273                  ),
 274                  'coursedisplay' => array(
 275                      'default' => $courseconfig->coursedisplay,
 276                      'type' => PARAM_INT,
 277                  ),
 278              );
 279          }
 280          if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
 281              $courseconfig = get_config('moodlecourse');
 282              $sectionmenu = array();
 283              for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
 284                  $sectionmenu[$i] = "$i";
 285              }
 286              $courseformatoptionsedit = array(
 287                  'numsections' => array(
 288                      'label' => new lang_string('numberweeks'),
 289                      'element_type' => 'select',
 290                      'element_attributes' => array($sectionmenu),
 291                  ),
 292                  'hiddensections' => array(
 293                      'label' => new lang_string('hiddensections'),
 294                      'help' => 'hiddensections',
 295                      'help_component' => 'moodle',
 296                      'element_type' => 'select',
 297                      'element_attributes' => array(
 298                          array(
 299                              0 => new lang_string('hiddensectionscollapsed'),
 300                              1 => new lang_string('hiddensectionsinvisible')
 301                          )
 302                      ),
 303                  ),
 304                  'coursedisplay' => array(
 305                      'label' => new lang_string('coursedisplay'),
 306                      'element_type' => 'select',
 307                      'element_attributes' => array(
 308                          array(
 309                              COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
 310                              COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
 311                          )
 312                      ),
 313                      'help' => 'coursedisplay',
 314                      'help_component' => 'moodle',
 315                  )
 316              );
 317              $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
 318          }
 319          return $courseformatoptions;
 320      }
 321  
 322      /**
 323       * Updates format options for a course
 324       *
 325       * Legacy course formats may assume that course format options
 326       * ('coursedisplay', 'numsections' and 'hiddensections') are shared between formats.
 327       * Therefore we make sure to copy them from the previous format
 328       *
 329       * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
 330       * @param stdClass $oldcourse if this function is called from {@link update_course()}
 331       *     this object contains information about the course before update
 332       * @return bool whether there were any changes to the options values
 333       */
 334      public function update_course_format_options($data, $oldcourse = null) {
 335          global $DB;
 336          if ($oldcourse !== null) {
 337              $data = (array)$data;
 338              $oldcourse = (array)$oldcourse;
 339              $options = $this->course_format_options();
 340              foreach ($options as $key => $unused) {
 341                  if (!array_key_exists($key, $data)) {
 342                      if (array_key_exists($key, $oldcourse)) {
 343                          $data[$key] = $oldcourse[$key];
 344                      } else if ($key === 'numsections') {
 345                          // If previous format does not have the field 'numsections' and this one does,
 346                          // and $data['numsections'] is not set fill it with the maximum section number from the DB
 347                          $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
 348                              WHERE course = ?', array($this->courseid));
 349                          if ($maxsection) {
 350                              // If there are no sections, or just default 0-section, 'numsections' will be set to default
 351                              $data['numsections'] = $maxsection;
 352                          }
 353                      }
 354                  }
 355              }
 356          }
 357          return $this->update_format_options($data);
 358      }
 359  }