Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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_availability_multiple_messages;
  29  use core\output\named_templatable;
  30  use core_availability\info;
  31  use core_availability\info_section;
  32  use core_courseformat\base as course_format;
  33  use core_courseformat\output\local\courseformat_named_templatable;
  34  use renderable;
  35  use section_info;
  36  use stdClass;
  37  
  38  /**
  39   * Base class to render section availability.
  40   *
  41   * @package   core_courseformat
  42   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  43   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class availability implements named_templatable, renderable {
  46  
  47      use courseformat_named_templatable;
  48  
  49      /** @var course_format the course format class */
  50      protected $format;
  51  
  52      /** @var section_info the section object */
  53      protected $section;
  54  
  55      /** @var string the has availability attribute name */
  56      protected $hasavailabilityname;
  57  
  58      /** @var stdClass|null the instance export data */
  59      protected $data = null;
  60  
  61      /** @var int Availability excerpt text max size treshold */
  62      protected const AVAILABILITY_EXCERPT_MAXSIZE = 100;
  63  
  64      /**
  65       * Constructor.
  66       *
  67       * @param course_format $format the course format
  68       * @param section_info $section the section info
  69       */
  70      public function __construct(course_format $format, section_info $section) {
  71          $this->format = $format;
  72          $this->section = $section;
  73          $this->hasavailabilityname = 'hasavailability';
  74      }
  75  
  76      /**
  77       * Export this data so it can be used as the context for a mustache template.
  78       *
  79       * @param \renderer_base $output typically, the renderer that's calling this function
  80       * @return stdClass data context for a mustache template
  81       */
  82      public function export_for_template(\renderer_base $output): stdClass {
  83          $this->build_export_data($output);
  84          return $this->data;
  85      }
  86  
  87      /**
  88       * Returns if the output has availability info to display.
  89       *
  90       * @param \renderer_base $output typically, the renderer that's calling this function
  91       * @return bool if the element has availability data to display
  92       */
  93      public function has_availability(\renderer_base $output): bool {
  94          $this->build_export_data($output);
  95          $attributename = $this->hasavailabilityname;
  96          return $this->data->$attributename;
  97      }
  98  
  99      /**
 100       * Protected method to build the export data.
 101       *
 102       * @param \renderer_base $output typically, the renderer that's calling this function
 103       */
 104      protected function build_export_data(\renderer_base $output) {
 105          if (!empty($this->data)) {
 106              return;
 107          }
 108  
 109          $data = (object) $this->get_info($output);
 110  
 111          $attributename = $this->hasavailabilityname;
 112          $data->$attributename = !empty($data->info);
 113  
 114          $this->data = $data;
 115      }
 116  
 117      /**
 118       * Export this data so it can be used as the context for a mustache template.
 119       *
 120       * If section is not visible, display the message about that ('Not available
 121       * until...', that sort of thing). Otherwise, returns blank.
 122       *
 123       * For users with the ability to view hidden sections, it shows the
 124       * information even though you can view the section and also may include
 125       * slightly fuller information (so that teachers can tell when sections
 126       * are going to be unavailable etc). This logic is the same as for
 127       * activities.
 128       *
 129       * @param \renderer_base $output typically, the renderer that's calling this function
 130       * @return stdclass data context for a mustache template
 131       */
 132      protected function get_info(\renderer_base $output): array {
 133          global $CFG, $USER;
 134  
 135          $section = $this->section;
 136          $context = context_course::instance($section->course);
 137  
 138          $canviewhidden = has_capability('moodle/course:viewhiddensections', $context, $USER);
 139  
 140          $editurl = new \moodle_url(
 141              '/course/editsection.php',
 142              ['id' => $this->section->id, 'showonly' => 'availabilityconditions']
 143          );
 144          $info = ['editurl' => $editurl->out(false)];
 145          if (!$section->visible) {
 146              return [];
 147          } else if (!$section->uservisible) {
 148              if ($section->availableinfo) {
 149                  // Note: We only get to this function if availableinfo is non-empty,
 150                  // so there is definitely something to print.
 151                  $info['info'] = $this->get_availability_data($output, $section->availableinfo, 'isrestricted');
 152              }
 153          } else if ($canviewhidden && !empty($CFG->enableavailability)) {
 154              // Check if there is an availability restriction.
 155              $ci = new info_section($section);
 156              $fullinfo = $ci->get_full_information();
 157              if ($fullinfo) {
 158                  $info['info'] = $this->get_availability_data($output, $fullinfo, 'isrestricted isfullinfo');
 159              }
 160          }
 161  
 162          return $info;
 163      }
 164  
 165      /**
 166       * Get the basic availability information data.
 167       *
 168       * @param \renderer_base $output typically, the renderer that's calling this function
 169       * @param string|core_availability_multiple_messages $availabilityinfo the avalability info
 170       * @param string $additionalclasses additional css classes
 171       * @return stdClass the availability information data
 172       */
 173      protected function get_availability_data($output, $availabilityinfo, $additionalclasses = ''): stdClass {
 174          // At this point, availabilityinfo is either a string or a renderable. We need to handle both cases in a different way.
 175          if (is_string($availabilityinfo)) {
 176              $data = $this->availability_info_from_string($output, $availabilityinfo);
 177          } else {
 178              $data = $this->availability_info_from_output($output, $availabilityinfo);
 179          }
 180  
 181          $data->classes = $additionalclasses;
 182  
 183          $additionalclasses = array_filter(explode(' ', $additionalclasses));
 184          if (in_array('ishidden', $additionalclasses)) {
 185              $data->ishidden = 1;
 186          } else if (in_array('isstealth', $additionalclasses)) {
 187              $data->isstealth = 1;
 188          } else if (in_array('isrestricted', $additionalclasses)) {
 189              $data->isrestricted = 1;
 190              if (in_array('isfullinfo', $additionalclasses)) {
 191                  $data->isfullinfo = 1;
 192              }
 193          }
 194  
 195          return $data;
 196      }
 197  
 198      /**
 199       * Generate the basic availability information data from a string.
 200       * Just shorten availability text to generate the excerpt text.
 201       *
 202       * @param \renderer_base $output typically, the renderer that's calling this function
 203       * @param string $availabilityinfo the avalability info
 204       * @return stdClass the availability information data
 205       */
 206      protected function availability_info_from_string(\renderer_base $output, string $availabilityinfo): stdClass {
 207          $course = $this->format->get_course();
 208  
 209          $text = info::format_info($availabilityinfo, $course);
 210          $data = ['text' => $text];
 211  
 212          if (strlen(html_to_text($text)) > self::AVAILABILITY_EXCERPT_MAXSIZE) {
 213              $data['excerpt'] = shorten_text($text, self::AVAILABILITY_EXCERPT_MAXSIZE);
 214          }
 215  
 216          return (object) $data;
 217      }
 218  
 219      /**
 220       * Generate the basic availability information data from a renderable.
 221       * Use the header and the first item to generate the excerpt text.
 222       *
 223       * @param \renderer_base $output typically, the renderer that's calling this function
 224       * @param core_availability_multiple_messages $availabilityinfo the avalability info
 225       * @return stdClass the availability information data
 226       */
 227      protected function availability_info_from_output(
 228          \renderer_base $output,
 229          core_availability_multiple_messages $availabilityinfo
 230      ): stdClass {
 231          $course = $this->format->get_course();
 232  
 233          $renderable = new \core_availability\output\availability_info($availabilityinfo);
 234          // We need to export_for_template() instead of directly render, to reuse the info for both 'text' and 'excerpt'.
 235          $info = $renderable->export_for_template($output);
 236  
 237          $text = $output->render_from_template('core_availability/availability_info', $info);
 238          $data = ['text' => info::format_info($text, $course)];
 239  
 240          if (!empty($info->items)) {
 241              $excerpttext = $info->header . ' ' . $info->items[0]->header;
 242              $data['excerpt'] = info::format_info($excerpttext, $course);
 243          }
 244  
 245          return (object) $data;
 246      }
 247  
 248      /**
 249       * Generate the basic availability information data.
 250       *
 251       * @deprecated since Moodle 4.3 MDL-78204. Please use {@see self::get_availability_data} instead.
 252       * @todo MDL-78489 This will be deleted in Moodle 4.7.
 253       * @param string $text the formatted avalability text
 254       * @param string $additionalclasses additional css classes
 255       * @return stdClass the availability information data
 256       */
 257      protected function availability_info($text, $additionalclasses = ''): stdClass {
 258          debugging('Use of ' . __FUNCTION__ . '() have been deprecated, ' .
 259          'please use core_courseformat\output\local\content\section\availability::get_availability_data()', DEBUG_DEVELOPER);
 260  
 261          $data = (object)[
 262              'text' => $text,
 263              'classes' => $additionalclasses
 264          ];
 265          $additionalclasses = array_filter(explode(' ', $additionalclasses));
 266  
 267          if (in_array('ishidden', $additionalclasses)) {
 268              $data->ishidden = 1;
 269          } else if (in_array('isstealth', $additionalclasses)) {
 270              $data->isstealth = 1;
 271          } else if (in_array('isrestricted', $additionalclasses)) {
 272              $data->isrestricted = 1;
 273  
 274              if (in_array('isfullinfo', $additionalclasses)) {
 275                  $data->isfullinfo = 1;
 276              }
 277          }
 278  
 279          return $data;
 280      }
 281  }