Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * 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          // Prepare an array of sections to create links for.
 109          $sections = array();
 110          $canviewhidden = has_capability('moodle/course:update', $context);
 111          $coursesections = $courseformat->get_sections();
 112          $coursesectionscount = count($coursesections);
 113          $sectiontojumpto = false;
 114          for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
 115              if ($i > $numsections || !isset($coursesections[$i])) {
 116                  continue;
 117              }
 118              $section = $coursesections[$i];
 119              if ($section->section && ($section->visible || $canviewhidden)) {
 120                  $sections[$i] = (object)array(
 121                      'section' => $section->section,
 122                      'visible' => $section->visible,
 123                      'highlight' => false
 124                  );
 125                  if ($courseformat->is_section_current($section)) {
 126                      $sections[$i]->highlight = true;
 127                      $sectiontojumpto = $section->section;
 128                  }
 129              }
 130          }
 131  
 132          if (!empty($sections)) {
 133              // Render the sections.
 134              $renderer = $this->page->get_renderer('block_section_links');
 135              $this->content->text = $renderer->render_section_links($this->page->course, $sections, $sectiontojumpto);
 136          }
 137  
 138          return $this->content;
 139      }
 140      /**
 141       * Returns true if this block has instance config.
 142       *
 143       * @return bool
 144       **/
 145      public function instance_allow_config() {
 146          return true;
 147      }
 148  
 149      /**
 150       * Returns true if this block has global config.
 151       *
 152       * @return bool
 153       */
 154      public function has_config() {
 155          return true;
 156      }
 157  
 158      /**
 159       * Return the plugin config settings for external functions.
 160       *
 161       * @return stdClass the configs for both the block instance and plugin
 162       * @since Moodle 3.8
 163       */
 164      public function get_config_for_external() {
 165          // Return all settings for all users since it is safe (no private keys, etc..).
 166          $instanceconfigs = !empty($this->config) ? $this->config : new stdClass();
 167          $pluginconfigs = get_config('block_section_links');
 168  
 169          return (object) [
 170              'instance' => $instanceconfigs,
 171              'plugin' => $pluginconfigs,
 172          ];
 173      }
 174  }
 175  
 176