Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402]

   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 main course format out 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;
  26  
  27  use core\output\named_templatable;
  28  use core_courseformat\base as course_format;
  29  use course_modinfo;
  30  use renderable;
  31  
  32  /**
  33   * Base class to render a course format.
  34   *
  35   * @package   core_courseformat
  36   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class content implements named_templatable, renderable {
  40      use courseformat_named_templatable;
  41  
  42      /** @var core_courseformat\base the course format class */
  43      protected $format;
  44  
  45      /** @var string the section format class */
  46      protected $sectionclass;
  47  
  48      /** @var string the add section output class name */
  49      protected $addsectionclass;
  50  
  51      /** @var string section navigation class name */
  52      protected $sectionnavigationclass;
  53  
  54      /** @var string section selector class name */
  55      protected $sectionselectorclass;
  56  
  57      /** @var string bulk editor bar toolbox */
  58      protected $bulkedittoolsclass;
  59  
  60      /** @var bool if uses add section */
  61      protected $hasaddsection = true;
  62  
  63      /**
  64       * Constructor.
  65       *
  66       * @param course_format $format the coruse format
  67       */
  68      public function __construct(course_format $format) {
  69          $this->format = $format;
  70  
  71          // Load output classes names from format.
  72          $this->sectionclass = $format->get_output_classname('content\\section');
  73          $this->addsectionclass = $format->get_output_classname('content\\addsection');
  74          $this->sectionnavigationclass = $format->get_output_classname('content\\sectionnavigation');
  75          $this->sectionselectorclass = $format->get_output_classname('content\\sectionselector');
  76          $this->bulkedittoolsclass = $format->get_output_classname('content\\bulkedittools');
  77      }
  78  
  79      /**
  80       * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
  81       *
  82       * @param renderer_base $output typically, the renderer that's calling this function
  83       * @return stdClass data context for a mustache template
  84       */
  85      public function export_for_template(\renderer_base $output) {
  86          global $PAGE;
  87          $format = $this->format;
  88  
  89          // Most formats uses section 0 as a separate section so we remove from the list.
  90          $sections = $this->export_sections($output);
  91          $initialsection = '';
  92          if (!empty($sections)) {
  93              $initialsection = array_shift($sections);
  94          }
  95  
  96          $data = (object)[
  97              'title' => $format->page_title(), // This method should be in the course_format class.
  98              'initialsection' => $initialsection,
  99              'sections' => $sections,
 100              'format' => $format->get_format(),
 101              'sectionreturn' => 0,
 102          ];
 103  
 104          // The single section format has extra navigation.
 105          $singlesection = $this->format->get_section_number();
 106          if ($singlesection) {
 107              if (!$PAGE->theme->usescourseindex) {
 108                  $sectionnavigation = new $this->sectionnavigationclass($format, $singlesection);
 109                  $data->sectionnavigation = $sectionnavigation->export_for_template($output);
 110  
 111                  $sectionselector = new $this->sectionselectorclass($format, $sectionnavigation);
 112                  $data->sectionselector = $sectionselector->export_for_template($output);
 113              }
 114              $data->hasnavigation = true;
 115              $data->singlesection = array_shift($data->sections);
 116              $data->sectionreturn = $singlesection;
 117          }
 118  
 119          if ($this->hasaddsection) {
 120              $addsection = new $this->addsectionclass($format);
 121              $data->numsections = $addsection->export_for_template($output);
 122          }
 123  
 124          if ($format->show_editor()) {
 125              $bulkedittools = new $this->bulkedittoolsclass($format);
 126              $data->bulkedittools = $bulkedittools->export_for_template($output);
 127          }
 128  
 129          return $data;
 130      }
 131  
 132      /**
 133       * Export sections array data.
 134       *
 135       * @param renderer_base $output typically, the renderer that's calling this function
 136       * @return array data context for a mustache template
 137       */
 138      protected function export_sections(\renderer_base $output): array {
 139  
 140          $format = $this->format;
 141          $course = $format->get_course();
 142          $modinfo = $this->format->get_modinfo();
 143  
 144          // Generate section list.
 145          $sections = [];
 146          $stealthsections = [];
 147          $numsections = $format->get_last_section_number();
 148          foreach ($this->get_sections_to_display($modinfo) as $sectionnum => $thissection) {
 149              // The course/view.php check the section existence but the output can be called
 150              // from other parts so we need to check it.
 151              if (!$thissection) {
 152                  throw new \moodle_exception('unknowncoursesection', 'error', course_get_url($course),
 153                      format_string($course->fullname));
 154              }
 155  
 156              $section = new $this->sectionclass($format, $thissection);
 157  
 158              if ($sectionnum > $numsections) {
 159                  // Activities inside this section are 'orphaned', this section will be printed as 'stealth' below.
 160                  if (!empty($modinfo->sections[$sectionnum])) {
 161                      $stealthsections[] = $section->export_for_template($output);
 162                  }
 163                  continue;
 164              }
 165  
 166              if (!$format->is_section_visible($thissection)) {
 167                  continue;
 168              }
 169  
 170              $sections[] = $section->export_for_template($output);
 171          }
 172          if (!empty($stealthsections)) {
 173              $sections = array_merge($sections, $stealthsections);
 174          }
 175          return $sections;
 176      }
 177  
 178      /**
 179       * Return an array of sections to display.
 180       *
 181       * This method is used to differentiate between display a specific section
 182       * or a list of them.
 183       *
 184       * @param course_modinfo $modinfo the current course modinfo object
 185       * @return section_info[] an array of section_info to display
 186       */
 187      private function get_sections_to_display(course_modinfo $modinfo): array {
 188          $singlesection = $this->format->get_section_number();
 189          if ($singlesection) {
 190              return [
 191                  $modinfo->get_section_info(0),
 192                  $modinfo->get_section_info($singlesection),
 193              ];
 194          }
 195  
 196          return $modinfo->get_section_info_all();
 197      }
 198  }