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 310 and 311] [Versions 39 and 311]

   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   * This file contains the main class for the section links block.
  19   *
  20   * @package    block_section_links
  21   * @copyright  Jason Hardin
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Section links block class.
  27   *
  28   * @package    block_section_links
  29   * @copyright  Jason Hardin
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class block_section_links extends block_base {
  33  
  34      /**
  35       * Initialises the block instance.
  36       */
  37      public function init() {
  38          $this->title = get_string('pluginname', 'block_section_links');
  39      }
  40  
  41      /**
  42       * Returns an array of formats for which this block can be used.
  43       *
  44       * @return array
  45       */
  46      public function applicable_formats() {
  47          return array(
  48              'course-view-weeks' => true,
  49              'course-view-topics' => true
  50          );
  51      }
  52  
  53      /**
  54       * Generates the content of the block and returns it.
  55       *
  56       * If the content has already been generated then the previously generated content is returned.
  57       *
  58       * @return stdClass
  59       */
  60      public function get_content() {
  61  
  62          // The config should be loaded by now.
  63          // If its empty then we will use the global config for the section links block.
  64          if (isset($this->config)){
  65              $config = $this->config;
  66          } else{
  67              $config = get_config('block_section_links');
  68          }
  69  
  70          if ($this->content !== null) {
  71              return $this->content;
  72          }
  73  
  74          $this->content = new stdClass;
  75          $this->content->footer = '';
  76          $this->content->text   = '';
  77  
  78          if (empty($this->instance)) {
  79              return $this->content;
  80          }
  81  
  82          $course = $this->page->course;
  83          $courseformat = course_get_format($course);
  84          $numsections = $courseformat->get_last_section_number();
  85          $context = context_course::instance($course->id);
  86  
  87          // Course format options 'numsections' is required to display the block.
  88          if (empty($numsections)) {
  89              return $this->content;
  90          }
  91  
  92          // Prepare the increment value.
  93          if (!empty($config->numsections1) and ($numsections > $config->numsections1)) {
  94              $inc = $config->incby1;
  95          } else if ($numsections > 22) {
  96              $inc = 2;
  97          } else {
  98              $inc = 1;
  99          }
 100          if (!empty($config->numsections2) and ($numsections > $config->numsections2)) {
 101              $inc = $config->incby2;
 102          } else {
 103              if ($numsections > 40) {
 104                  $inc = 5;
 105              }
 106          }
 107  
 108          // Whether or not section name should be displayed.
 109          $showsectionname = !empty($config->showsectionname) ? true : false;
 110  
 111          // Prepare an array of sections to create links for.
 112          $sections = array();
 113          $canviewhidden = has_capability('moodle/course:update', $context);
 114          $coursesections = $courseformat->get_sections();
 115          $coursesectionscount = count($coursesections);
 116          $sectiontojumpto = false;
 117          for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
 118              if ($i > $numsections || !isset($coursesections[$i])) {
 119                  continue;
 120              }
 121              $section = $coursesections[$i];
 122              if ($section->section && ($section->visible || $canviewhidden)) {
 123                  $sections[$i] = (object)array(
 124                      'section' => $section->section,
 125                      'visible' => $section->visible,
 126                      'highlight' => false
 127                  );
 128                  if ($courseformat->is_section_current($section)) {
 129                      $sections[$i]->highlight = true;
 130                      $sectiontojumpto = $section->section;
 131                  }
 132                  if ($showsectionname) {
 133                      $sections[$i]->name = $courseformat->get_section_name($i);
 134                  }
 135              }
 136          }
 137  
 138          if (!empty($sections)) {
 139              // Render the sections.
 140              $renderer = $this->page->get_renderer('block_section_links');
 141              $this->content->text = $renderer->render_section_links($this->page->course, $sections,
 142                  $sectiontojumpto, $showsectionname);
 143          }
 144  
 145          return $this->content;
 146      }
 147      /**
 148       * Returns true if this block has instance config.
 149       *
 150       * @return bool
 151       **/
 152      public function instance_allow_config() {
 153          return true;
 154      }
 155  
 156      /**
 157       * Returns true if this block has global config.
 158       *
 159       * @return bool
 160       */
 161      public function has_config() {
 162          return true;
 163      }
 164  
 165      /**
 166       * Return the plugin config settings for external functions.
 167       *
 168       * @return stdClass the configs for both the block instance and plugin
 169       * @since Moodle 3.8
 170       */
 171      public function get_config_for_external() {
 172          // Return all settings for all users since it is safe (no private keys, etc..).
 173          $instanceconfigs = !empty($this->config) ? $this->config : new stdClass();
 174          $pluginconfigs = get_config('block_section_links');
 175  
 176          return (object) [
 177              'instance' => $instanceconfigs,
 178              'plugin' => $pluginconfigs,
 179          ];
 180      }
 181  }
 182  
 183