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.
   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   * Contains the default section navigation output class.
  19   *
  20   * @package   core_courseformat
  21   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_courseformat\output\local\content;
  26  
  27  use context_course;
  28  use core\output\named_templatable;
  29  use core_courseformat\base as course_format;
  30  use core_courseformat\output\local\courseformat_named_templatable;
  31  use renderable;
  32  use stdClass;
  33  
  34  /**
  35   * Base class to render a course add section navigation.
  36   *
  37   * @package   core_courseformat
  38   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class sectionnavigation implements named_templatable, renderable {
  42  
  43      use courseformat_named_templatable;
  44  
  45      /** @var course_format the course format class */
  46      protected $format;
  47  
  48      /** @var int the course displayed section */
  49      protected $sectionno;
  50  
  51      /** @var stdClass the calculated data to prevent calculations when rendered several times */
  52      private $data = null;
  53  
  54      /**
  55       * Constructor.
  56       *
  57       * @param course_format $format the course format
  58       * @param int $sectionno the section number
  59       */
  60      public function __construct(course_format $format, int $sectionno) {
  61          $this->format = $format;
  62          $this->sectionno = $sectionno;
  63      }
  64  
  65      /**
  66       * Export this data so it can be used as the context for a mustache template.
  67       *
  68       * @param renderer_base $output typically, the renderer that's calling this function
  69       * @return stdClass data context for a mustache template
  70       */
  71      public function export_for_template(\renderer_base $output): stdClass {
  72          global $USER;
  73  
  74          if ($this->data !== null) {
  75              return $this->data;
  76          }
  77  
  78          $format = $this->format;
  79          $course = $format->get_course();
  80          $context = context_course::instance($course->id);
  81  
  82          $modinfo = $this->format->get_modinfo();
  83          $sections = $modinfo->get_section_info_all();
  84  
  85          // FIXME: This is really evil and should by using the navigation API.
  86          $canviewhidden = has_capability('moodle/course:viewhiddensections', $context, $USER);
  87  
  88          $data = (object)[
  89              'previousurl' => '',
  90              'nexturl' => '',
  91              'larrow' => $output->larrow(),
  92              'rarrow' => $output->rarrow(),
  93              'currentsection' => $this->sectionno,
  94          ];
  95  
  96          $back = $this->sectionno - 1;
  97          while ($back > 0 and empty($data->previousurl)) {
  98              if ($canviewhidden || $sections[$back]->uservisible) {
  99                  if (!$sections[$back]->visible) {
 100                      $data->previoushidden = true;
 101                  }
 102                  $data->previousname = get_section_name($course, $sections[$back]);
 103                  $data->previousurl = course_get_url($course, $back);
 104                  $data->hasprevious = true;
 105              }
 106              $back--;
 107          }
 108  
 109          $forward = $this->sectionno + 1;
 110          $numsections = course_get_format($course)->get_last_section_number();
 111          while ($forward <= $numsections and empty($data->nexturl)) {
 112              if ($canviewhidden || $sections[$forward]->uservisible) {
 113                  if (!$sections[$forward]->visible) {
 114                      $data->nexthidden = true;
 115                  }
 116                  $data->nextname = get_section_name($course, $sections[$forward]);
 117                  $data->nexturl = course_get_url($course, $forward);
 118                  $data->hasnext = true;
 119              }
 120              $forward++;
 121          }
 122  
 123          $this->data = $data;
 124          return $data;
 125      }
 126  }