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  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              'bulkeditable' => $this->is_bulk_editable(),
  92          ];
  93  
  94          if (empty($modinfo->sections[$section->section])) {
  95              return $data;
  96          }
  97  
  98          foreach ($modinfo->sections[$section->section] as $modnumber) {
  99              $mod = $modinfo->cms[$modnumber];
 100              if ($section->uservisible && $mod->is_visible_on_course_page()) {
 101                  $data->cmlist[] = $mod->id;
 102              }
 103          }
 104  
 105          return $data;
 106      }
 107  
 108      /**
 109       * Return if the section can be selected for bulk editing.
 110       * @return bool if the section can be edited in bulk
 111       */
 112      protected function is_bulk_editable(): bool {
 113          $section = $this->section;
 114          return ($section->section != 0);
 115      }
 116  
 117      /**
 118       * Return if the section has a restrictions icon displayed or not.
 119       *
 120       * @return bool if the section has visible restrictions for the user.
 121       */
 122      protected function get_has_restrictions(): bool {
 123          global $CFG;
 124  
 125          $section = $this->section;
 126          $course = $this->format->get_course();
 127          $context = context_course::instance($course->id);
 128  
 129          // Hidden sections have no restriction indicator displayed.
 130          if (empty($section->visible) || empty($CFG->enableavailability)) {
 131              return false;
 132          }
 133          // The activity is not visible to the user but it may have some availability information.
 134          if (!$section->uservisible) {
 135              return !empty($section->availableinfo);
 136          }
 137          // Course editors can see all restrictions if the section is visible.
 138          if (has_capability('moodle/course:viewhiddensections', $context)) {
 139              $ci = new info_section($section);
 140              return !empty($ci->get_full_information());
 141          }
 142          // Regular users can only see restrictions if apply to them.
 143          return false;
 144      }
 145  }