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 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   * Contains the default section availability 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\section;
  26  
  27  use context_course;
  28  use core\output\named_templatable;
  29  use core_availability\info;
  30  use core_availability\info_section;
  31  use core_courseformat\base as course_format;
  32  use core_courseformat\output\local\courseformat_named_templatable;
  33  use renderable;
  34  use section_info;
  35  use stdClass;
  36  
  37  /**
  38   * Base class to render section availability.
  39   *
  40   * @package   core_courseformat
  41   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class availability implements named_templatable, renderable {
  45  
  46      use courseformat_named_templatable;
  47  
  48      /** @var course_format the course format class */
  49      protected $format;
  50  
  51      /** @var section_info the section object */
  52      protected $section;
  53  
  54      /** @var string the has availability attribute name */
  55      protected $hasavailabilityname;
  56  
  57      /** @var stdClass|null the instance export data */
  58      protected $data = null;
  59  
  60      /**
  61       * Constructor.
  62       *
  63       * @param course_format $format the course format
  64       * @param section_info $section the section info
  65       */
  66      public function __construct(course_format $format, section_info $section) {
  67          $this->format = $format;
  68          $this->section = $section;
  69          $this->hasavailabilityname = 'hasavailability';
  70      }
  71  
  72      /**
  73       * Export this data so it can be used as the context for a mustache template.
  74       *
  75       * @param \renderer_base $output typically, the renderer that's calling this function
  76       * @return stdClass data context for a mustache template
  77       */
  78      public function export_for_template(\renderer_base $output): stdClass {
  79          $this->build_export_data($output);
  80          return $this->data;
  81      }
  82  
  83      /**
  84       * Returns if the output has availability info to display.
  85       *
  86       * @param \renderer_base $output typically, the renderer that's calling this function
  87       * @return bool if the element has availability data to display
  88       */
  89      public function has_availability(\renderer_base $output): bool {
  90          $this->build_export_data($output);
  91          $attributename = $this->hasavailabilityname;
  92          return $this->data->$attributename;
  93      }
  94  
  95      /**
  96       * Protected method to build the export data.
  97       *
  98       * @param \renderer_base $output typically, the renderer that's calling this function
  99       */
 100      protected function build_export_data(\renderer_base $output) {
 101          if (!empty($this->data)) {
 102              return;
 103          }
 104  
 105          $data = (object)[
 106              'info' => $this->get_info($output),
 107          ];
 108  
 109          $attributename = $this->hasavailabilityname;
 110          $data->$attributename = !empty($data->info);
 111  
 112          $this->data = $data;
 113      }
 114  
 115      /**
 116       * Export this data so it can be used as the context for a mustache template.
 117       *
 118       * If section is not visible, display the message about that ('Not available
 119       * until...', that sort of thing). Otherwise, returns blank.
 120       *
 121       * For users with the ability to view hidden sections, it shows the
 122       * information even though you can view the section and also may include
 123       * slightly fuller information (so that teachers can tell when sections
 124       * are going to be unavailable etc). This logic is the same as for
 125       * activities.
 126       *
 127       * @param renderer_base $output typically, the renderer that's calling this function
 128       * @return stdclass data context for a mustache template
 129       */
 130      protected function get_info(\renderer_base $output): array {
 131          global $CFG, $USER;
 132  
 133          $section = $this->section;
 134          $context = context_course::instance($section->course);
 135  
 136          $canviewhidden = has_capability('moodle/course:viewhiddensections', $context, $USER);
 137  
 138          $info = [];
 139          if (!$section->visible) {
 140              $info = [];
 141          } else if (!$section->uservisible) {
 142              if ($section->availableinfo) {
 143                  // Note: We only get to this function if availableinfo is non-empty,
 144                  // so there is definitely something to print.
 145                  $formattedinfo = info::format_info($section->availableinfo, $section->course);
 146                  $info[] = $this->availability_info($formattedinfo, 'isrestricted');
 147              }
 148          } else if ($canviewhidden && !empty($CFG->enableavailability)) {
 149              // Check if there is an availability restriction.
 150              $ci = new info_section($section);
 151              $fullinfo = $ci->get_full_information();
 152              if ($fullinfo) {
 153                  $formattedinfo = info::format_info($fullinfo, $section->course);
 154                  $info[] = $this->availability_info($formattedinfo, 'isrestricted isfullinfo');
 155              }
 156          }
 157  
 158          return $info;
 159      }
 160  
 161      /**
 162       * Generate the basic availability information data.
 163       *
 164       * @param string $text the formatted avalability text
 165       * @param string $additionalclasses additional css classes
 166       * @return stdClass the availability information data
 167       */
 168      protected function availability_info($text, $additionalclasses = ''): stdClass {
 169  
 170          $data = (object)[
 171              'text' => $text,
 172              'classes' => $additionalclasses
 173          ];
 174          $additionalclasses = array_filter(explode(' ', $additionalclasses));
 175  
 176          if (in_array('ishidden', $additionalclasses)) {
 177              $data->ishidden = 1;
 178          } else if (in_array('isstealth', $additionalclasses)) {
 179              $data->isstealth = 1;
 180          } else if (in_array('isrestricted', $additionalclasses)) {
 181              $data->isrestricted = 1;
 182  
 183              if (in_array('isfullinfo', $additionalclasses)) {
 184                  $data->isfullinfo = 1;
 185              }
 186          }
 187  
 188          return $data;
 189      }
 190  }