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.

Differences Between: [Versions 401 and 402] [Versions 401 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  namespace core_courseformat\output\local\state;
  18  
  19  use core_availability\info_section;
  20  use core_courseformat\base as course_format;
  21  use section_info;
  22  use renderable;
  23  use stdClass;
  24  use context_course;
  25  
  26  /**
  27   * Contains the ajax update section structure.
  28   *
  29   * @package   core_course
  30   * @copyright 2021 Ferran Recio <ferran@moodle.com>
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class section implements renderable {
  34  
  35      /** @var course_format the course format class */
  36      protected $format;
  37  
  38      /** @var section_info the course section class */
  39      protected $section;
  40  
  41      /**
  42       * Constructor.
  43       *
  44       * @param course_format $format the course format
  45       * @param section_info $section the section info
  46       */
  47      public function __construct(course_format $format, section_info $section) {
  48          $this->format = $format;
  49          $this->section = $section;
  50      }
  51  
  52      /**
  53       * Export this data so it can be used as state object in the course editor.
  54       *
  55       * @param renderer_base $output typically, the renderer that's calling this function
  56       * @return array data context for a mustache template
  57       */
  58      public function export_for_template(\renderer_base $output): stdClass {
  59          $format = $this->format;
  60          $course = $format->get_course();
  61          $section = $this->section;
  62          $modinfo = $format->get_modinfo();
  63  
  64          $indexcollapsed = false;
  65          $contentcollapsed = false;
  66          $preferences = $format->get_sections_preferences();
  67          if (isset($preferences[$section->id])) {
  68              $sectionpreferences = $preferences[$section->id];
  69              if (!empty($sectionpreferences->contentcollapsed)) {
  70                  $contentcollapsed = true;
  71              }
  72              if (!empty($sectionpreferences->indexcollapsed)) {
  73                  $indexcollapsed = true;
  74              }
  75          }
  76  
  77          $data = (object)[
  78              'id' => $section->id,
  79              'section' => $section->section,
  80              'number' => $section->section,
  81              'title' => $format->get_section_name($section),
  82              'hassummary' => !empty($section->summary),
  83              'rawtitle' => $section->name,
  84              'cmlist' => [],
  85              'visible' => !empty($section->visible),
  86              'sectionurl' => course_get_url($course, $section->section)->out(),
  87              'current' => $format->is_section_current($section),
  88              'indexcollapsed' => $indexcollapsed,
  89              'contentcollapsed' => $contentcollapsed,
  90              'hasrestrictions' => $this->get_has_restrictions(),
  91          ];
  92  
  93          if (empty($modinfo->sections[$section->section])) {
  94              return $data;
  95          }
  96  
  97          foreach ($modinfo->sections[$section->section] as $modnumber) {
  98              $mod = $modinfo->cms[$modnumber];
  99              if ($section->uservisible && $mod->is_visible_on_course_page()) {
 100                  $data->cmlist[] = $mod->id;
 101              }
 102          }
 103  
 104          return $data;
 105      }
 106  
 107      /**
 108       * Return if the section has a restrictions icon displayed or not.
 109       *
 110       * @return bool if the section has visible restrictions for the user.
 111       */
 112      protected function get_has_restrictions(): bool {
 113          global $CFG;
 114  
 115          $section = $this->section;
 116          $course = $this->format->get_course();
 117          $context = context_course::instance($course->id);
 118  
 119          // Hidden sections have no restriction indicator displayed.
 120          if (empty($section->visible) || empty($CFG->enableavailability)) {
 121              return false;
 122          }
 123          // The activity is not visible to the user but it may have some availability information.
 124          if (!$section->uservisible) {
 125              return !empty($section->availableinfo);
 126          }
 127          // Course editors can see all restrictions if the section is visible.
 128          if (has_capability('moodle/course:viewhiddensections', $context)) {
 129              $ci = new info_section($section);
 130              return !empty($ci->get_full_information());
 131          }
 132          // Regular users can only see restrictions if apply to them.
 133          return false;
 134      }
 135  }