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 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 activity availability information.
  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\cm;
  26  
  27  use core_courseformat\output\local\content\section\availability as section_avalability;
  28  use cm_info;
  29  use core_courseformat\base as course_format;
  30  use section_info;
  31  use stdClass;
  32  use core_availability\info_module;
  33  use core_availability\info;
  34  
  35  /**
  36   * Base class to render a course module availability inside a course format.
  37   *
  38   * @package   core_courseformat
  39   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class availability extends section_avalability {
  43  
  44      /** @var course_format the course format */
  45      protected $format;
  46  
  47      /** @var section_info the section object */
  48      protected $section;
  49  
  50      /** @var cm_info the course module instance */
  51      protected $mod;
  52  
  53      /** @var array optional display options */
  54      protected $displayoptions;
  55  
  56      /** @var bool the has availability attribute name */
  57      protected $hasavailabilityname;
  58  
  59      /** @var stdClass|null the instance export data */
  60      protected $data = null;
  61  
  62      /**
  63       * Constructor.
  64       *
  65       * @param course_format $format the course format
  66       * @param section_info $section the section info
  67       * @param cm_info $mod the course module ionfo
  68       * @param array $displayoptions optional extra display options
  69       */
  70      public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
  71          $this->format = $format;
  72          $this->section = $section;
  73          $this->mod = $mod;
  74          $this->displayoptions = $displayoptions;
  75          $this->hasavailabilityname = 'hasmodavailability';
  76      }
  77  
  78      /**
  79       * Get the availability data to be used as the context for a mustache template.
  80       *
  81       * @param \renderer_base $output typically, the renderer that's calling this function
  82       * @return array the availability data.
  83       */
  84      protected function get_info(\renderer_base $output): array {
  85          if (!$this->mod->is_visible_on_course_page()) {
  86              // Nothing to be displayed to the user.
  87              return [];
  88          }
  89          if (!$this->mod->uservisible) {
  90              return $this->user_availability_info($output);
  91          }
  92  
  93          return $this->conditional_availability_info($output);
  94      }
  95  
  96      /**
  97       * Get the current user availability data.
  98       *
  99       * This is a student who is not allowed to see the module but might be allowed
 100       * to see availability info (i.e. "Available from ...").
 101       *
 102       * @param \renderer_base $output typically, the renderer that's calling this function
 103       * @return array the availability data.
 104       */
 105      protected function user_availability_info(\renderer_base $output): array {
 106          if (empty($this->mod->availableinfo)) {
 107              return [];
 108          }
 109  
 110          $info = [];
 111          $formattedinfo = \core_availability\info::format_info(
 112              $this->mod->availableinfo,
 113              $this->mod->get_course()
 114          );
 115          $info[] = $this->availability_info($formattedinfo, 'isrestricted');
 116          return $info;
 117      }
 118  
 119      /**
 120       * Get the activity availability data to display.
 121       *
 122       * @param \renderer_base $output typically, the renderer that's calling this function
 123       * @return array the availability data.
 124       */
 125      protected function conditional_availability_info(\renderer_base $output): array {
 126          global $CFG;
 127  
 128          // This is a teacher who is allowed to see module but still should see the
 129          // information that module is not available to all/some students.
 130          $mod  = $this->mod;
 131          $modcontext = $mod->context;
 132          $canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext);
 133          if (!$canviewhidden || empty($CFG->enableavailability)) {
 134              return [];
 135          }
 136  
 137          // Display information about conditional availability.
 138          // Don't add availability information if user is not editing and activity is hidden.
 139          if (!$mod->visible && !$this->format->show_editor()) {
 140              return [];
 141          }
 142  
 143          $ci = new info_module($mod);
 144          $fullinfo = $ci->get_full_information();
 145          if (!$fullinfo) {
 146              return [];
 147          }
 148  
 149          $info = [];
 150          $hidinfoclass = 'isrestricted isfullinfo';
 151          if (!$mod->visible) {
 152              $hidinfoclass .= ' hide';
 153          }
 154          $formattedinfo = info::format_info(
 155              $fullinfo,
 156              $mod->get_course()
 157          );
 158          $info[] = $this->availability_info($formattedinfo, $hidinfoclass);
 159  
 160          return $info;
 161      }
 162  }