2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Class handles conditional availability information for an activity.
20 * @package core_availability
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_availability
;
27 defined('MOODLE_INTERNAL') ||
die();
30 * Class handles conditional availability information for an activity.
32 * @package core_availability
33 * @copyright 2014 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class info_module
extends info
{
37 /** @var \cm_info Activity. */
41 * Constructs with item details.
43 * @param \cm_info $cm Course-module object
45 public function __construct(\cm_info
$cm) {
46 parent
::__construct($cm->get_course(), $cm->visible
, $cm->availability
);
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 '<AVAILABILITY_CMNAME_' . $this->cm
->id
. '/>';
57 protected function set_in_database($availability) {
59 $DB->set_field('course_modules', 'availability', $availability,
60 array('id' => $this->cm
->id
));
64 * Gets the course-module object. Intended for use by conditions.
66 * @return \cm_info Course module
68 public function get_course_module() {
72 public function get_context() {
73 return \context_module
::instance($this->cm
->id
);
77 * Tests against a user list. Users who cannot access the activity due to
78 * availability restrictions will be removed from the list.
80 * Note this only includes availability restrictions (those handled within
81 * this API) and not other ways of restricting access.
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.
87 * When called on a module, this test DOES also include restrictions on the
90 * The function operates reasonably efficiently i.e. should not do per-user
91 * database queries. It is however likely to be fairly slow.
93 * @param array $users Array of userid => object
94 * @return array Filtered version of input array
96 public function filter_user_list(array $users) {
98 if (!$CFG->enableavailability
) {
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);
108 // Now do base class (module) filtering on top.
109 return parent
::filter_user_list($filtered);
112 protected function get_view_hidden_capability() {
113 return 'moodle/course:ignoreavailabilityrestrictions';
116 public function get_user_list_sql($onlyactive = true) {
118 if (!$CFG->enableavailability
) {
119 return array('', array());
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);
129 if (!$sectionresult[0]) {
130 return $moduleresult;
132 if (!$moduleresult[0]) {
133 return $sectionresult;
136 return array($DB->sql_intersect(array($sectionresult[0], $moduleresult[0]), 'id'),
137 array_merge($sectionresult[1], $moduleresult[1]));
141 * Checks if an activity is visible to the given user.
143 * Unlike other checks in the availability system, this check includes the
144 * $cm->visible flag. It is equivalent to $cm->uservisible.
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
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.
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.
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
166 public static function is_user_visible($cmorid, $userid = 0, $checkcourse = true) {
167 global $USER, $DB, $CFG;
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
;
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
)) {
184 if (is_object($cmorid)) {
185 $cmorid = $cmorid->id
;
187 $cm = $DB->get_record('course_modules', array('id' => $cmorid));
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
);
195 // If requested, check user can access the course.
197 $coursecontext = \context_course
::instance($cm->course
);
198 if (!is_enrolled($coursecontext, $userid, '', true) &&
199 !has_capability('moodle/course:view', $coursecontext, $userid)) {
204 // If availability is disabled, then all we need to do is check the visible flag.
205 if (!$CFG->enableavailability
&& $cm->visible
) {
209 // When availability is enabled, access can depend on 3 things:
211 // 2. $cm->availability
212 // 3. $section->availability (for activity section and possibly for
214 // As a result we cannot take short cuts any longer and must get
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
);
224 return $cms[$cm->id
]->uservisible
;