Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 39 and 400]

   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   * Class handles conditional availability information for an activity.
  19   *
  20   * @package core_availability
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_availability;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class handles conditional availability information for an activity.
  31   *
  32   * @package core_availability
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class info_module extends info {
  37      /** @var \cm_info Activity. */
  38      protected $cm;
  39  
  40      /**
  41       * Constructs with item details.
  42       *
  43       * @param \cm_info $cm Course-module object
  44       */
  45      public function __construct(\cm_info $cm) {
  46          parent::__construct($cm->get_course(), $cm->visible, $cm->availability);
  47          $this->cm = $cm;
  48      }
  49  
  50      protected function get_thing_name() {
  51          // We cannot access $cm->name as a property at this point, because this
  52          // code may itself run in response to the $cm->name property access, and
  53          // PHP magic function properties do not allow recursion (because PHP).
  54          return condition::description_cm_name($this->cm->id);
  55      }
  56  
  57      protected function set_in_database($availability) {
  58          global $DB;
  59          $DB->set_field('course_modules', 'availability', $availability,
  60                  array('id' => $this->cm->id));
  61      }
  62  
  63      /**
  64       * Gets the course-module object. Intended for use by conditions.
  65       *
  66       * @return \cm_info Course module
  67       */
  68      public function get_course_module() {
  69          return $this->cm;
  70      }
  71  
  72      public function get_context() {
  73          return \context_module::instance($this->cm->id);
  74      }
  75  
  76      /**
  77       * Tests against a user list. Users who cannot access the activity due to
  78       * availability restrictions will be removed from the list.
  79       *
  80       * Note this only includes availability restrictions (those handled within
  81       * this API) and not other ways of restricting access.
  82       *
  83       * This test ONLY includes conditions which are marked as being applied to
  84       * user lists. For example, group conditions are included but date
  85       * conditions are not included.
  86       *
  87       * When called on a module, this test DOES also include restrictions on the
  88       * section (if any).
  89       *
  90       * The function operates reasonably efficiently i.e. should not do per-user
  91       * database queries. It is however likely to be fairly slow.
  92       *
  93       * @param array $users Array of userid => object
  94       * @return array Filtered version of input array
  95       */
  96      public function filter_user_list(array $users) {
  97          global $CFG;
  98          if (!$CFG->enableavailability) {
  99              return $users;
 100          }
 101  
 102          // Apply section filtering first.
 103          $section = $this->cm->get_modinfo()->get_section_info(
 104                  $this->cm->sectionnum, MUST_EXIST);
 105          $sectioninfo = new info_section($section);
 106          $filtered = $sectioninfo->filter_user_list($users);
 107  
 108          // Now do base class (module) filtering on top.
 109          return parent::filter_user_list($filtered);
 110      }
 111  
 112      protected function get_view_hidden_capability() {
 113          return 'moodle/course:ignoreavailabilityrestrictions';
 114      }
 115  
 116      public function get_user_list_sql($onlyactive = true) {
 117          global $CFG, $DB;
 118          if (!$CFG->enableavailability) {
 119              return array('', array());
 120          }
 121  
 122          // Get query for section (if any) and module.
 123          $section = $this->cm->get_modinfo()->get_section_info(
 124                  $this->cm->sectionnum, MUST_EXIST);
 125          $sectioninfo = new info_section($section);
 126          $sectionresult = $sectioninfo->get_user_list_sql($onlyactive);
 127          $moduleresult = parent::get_user_list_sql($onlyactive);
 128  
 129          if (!$sectionresult[0]) {
 130              return $moduleresult;
 131          }
 132          if (!$moduleresult[0]) {
 133              return $sectionresult;
 134          }
 135  
 136          return array($DB->sql_intersect(array($sectionresult[0], $moduleresult[0]), 'id'),
 137                  array_merge($sectionresult[1], $moduleresult[1]));
 138      }
 139  
 140      /**
 141       * Checks if an activity is visible to the given user.
 142       *
 143       * Unlike other checks in the availability system, this check includes the
 144       * $cm->visible flag. It is equivalent to $cm->uservisible.
 145       *
 146       * If you have already checked (or do not care whether) the user has access
 147       * to the course, you can set $checkcourse to false to save it checking
 148       * course access.
 149       *
 150       * When checking for the current user, you should generally not call
 151       * this function. Instead, use get_fast_modinfo to get a cm_info object,
 152       * then simply check the $cm->uservisible flag. This function is intended
 153       * to obtain that information for a separate course-module object that
 154       * wasn't loaded with get_fast_modinfo, or for a different user.
 155       *
 156       * This function has a performance cost unless the availability system is
 157       * disabled, and you supply a $cm object with necessary fields, and you
 158       * don't check course access.
 159       *
 160       * @param int|\stdClass|\cm_info $cmorid Object or id representing activity
 161       * @param int $userid User id (0 = current user)
 162       * @param bool $checkcourse If true, checks whether the user has course access
 163       * @return bool True if the activity is visible to the specified user
 164       * @throws \moodle_exception If the cmid doesn't exist
 165       */
 166      public static function is_user_visible($cmorid, $userid = 0, $checkcourse = true) {
 167          global $USER, $DB, $CFG;
 168  
 169          // Evaluate user id.
 170          if (!$userid) {
 171              $userid = $USER->id;
 172          }
 173  
 174          // If this happens to be already called with a cm_info for the right user
 175          // then just return uservisible.
 176          if (($cmorid instanceof \cm_info) && $cmorid->get_modinfo()->userid == $userid) {
 177              return $cmorid->uservisible;
 178          }
 179  
 180          // If the $cmorid isn't an object or doesn't have required fields, load it.
 181          if (is_object($cmorid) && isset($cmorid->course) && isset($cmorid->visible)) {
 182              $cm = $cmorid;
 183          } else {
 184              if (is_object($cmorid)) {
 185                  $cmorid = $cmorid->id;
 186              }
 187              $cm = $DB->get_record('course_modules', array('id' => $cmorid));
 188              if (!$cm) {
 189                  // In some error cases, the course module may not exist.
 190                  debugging('info_module::is_user_visible called with invalid cmid ' . $cmorid, DEBUG_DEVELOPER);
 191                  return false;
 192              }
 193          }
 194  
 195          // If requested, check user can access the course.
 196          if ($checkcourse) {
 197              $coursecontext = \context_course::instance($cm->course);
 198              if (!is_enrolled($coursecontext, $userid, '', true) &&
 199                      !has_capability('moodle/course:view', $coursecontext, $userid)) {
 200                  return false;
 201              }
 202          }
 203  
 204          // If availability is disabled, then all we need to do is check the visible flag.
 205          if (!$CFG->enableavailability && $cm->visible) {
 206              return true;
 207          }
 208  
 209          // When availability is enabled, access can depend on 3 things:
 210          // 1. $cm->visible
 211          // 2. $cm->availability
 212          // 3. $section->availability (for activity section and possibly for
 213          //    parent sections)
 214          // As a result we cannot take short cuts any longer and must get
 215          // standard modinfo.
 216          $modinfo = get_fast_modinfo($cm->course, $userid);
 217          $cms = $modinfo->get_cms();
 218          if (!isset($cms[$cm->id])) {
 219              // In some cases this might get called with a cmid that is no longer
 220              // available, for example when a module is hidden at system level.
 221              debugging('info_module::is_user_visible called with invalid cmid ' . $cm->id, DEBUG_DEVELOPER);
 222              return false;
 223          }
 224          return $cms[$cm->id]->uservisible;
 225      }
 226  }