[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 '<AVAILABILITY_CMNAME_' . $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 public function get_user_list_sql($onlyactive = true) { 113 global $CFG, $DB; 114 if (!$CFG->enableavailability) { 115 return array('', array()); 116 } 117 118 // Get query for section (if any) and module. 119 $section = $this->cm->get_modinfo()->get_section_info( 120 $this->cm->sectionnum, MUST_EXIST); 121 $sectioninfo = new info_section($section); 122 $sectionresult = $sectioninfo->get_user_list_sql($onlyactive); 123 $moduleresult = parent::get_user_list_sql($onlyactive); 124 125 if (!$sectionresult[0]) { 126 return $moduleresult; 127 } 128 if (!$moduleresult[0]) { 129 return $sectionresult; 130 } 131 132 return array($DB->sql_intersect(array($sectionresult[0], $moduleresult[0]), 'id'), 133 array_merge($sectionresult[1], $moduleresult[1])); 134 } 135 136 /** 137 * Checks if an activity is visible to the given user. 138 * 139 * Unlike other checks in the availability system, this check includes the 140 * $cm->visible flag. It is equivalent to $cm->uservisible. 141 * 142 * If you have already checked (or do not care whether) the user has access 143 * to the course, you can set $checkcourse to false to save it checking 144 * course access. 145 * 146 * When checking for the current user, you should generally not call 147 * this function. Instead, use get_fast_modinfo to get a cm_info object, 148 * then simply check the $cm->uservisible flag. This function is intended 149 * to obtain that information for a separate course-module object that 150 * wasn't loaded with get_fast_modinfo, or for a different user. 151 * 152 * This function has a performance cost unless the availability system is 153 * disabled, and you supply a $cm object with necessary fields, and you 154 * don't check course access. 155 * 156 * @param int|\stdClass|\cm_info $cmorid Object or id representing activity 157 * @param int $userid User id (0 = current user) 158 * @param bool $checkcourse If true, checks whether the user has course access 159 * @return bool True if the activity is visible to the specified user 160 * @throws \moodle_exception If the cmid doesn't exist 161 */ 162 public static function is_user_visible($cmorid, $userid = 0, $checkcourse = true) { 163 global $USER, $DB, $CFG; 164 165 // Evaluate user id. 166 if (!$userid) { 167 $userid = $USER->id; 168 } 169 170 // If this happens to be already called with a cm_info for the right user 171 // then just return uservisible. 172 if (($cmorid instanceof \cm_info) && $cmorid->get_modinfo()->userid == $userid) { 173 return $cmorid->uservisible; 174 } 175 176 // If the $cmorid isn't an object or doesn't have required fields, load it. 177 if (is_object($cmorid) && isset($cmorid->course) && isset($cmorid->visible)) { 178 $cm = $cmorid; 179 } else { 180 if (is_object($cmorid)) { 181 $cmorid = $cmorid->id; 182 } 183 $cm = $DB->get_record('course_modules', array('id' => $cmorid), '*', MUST_EXIST); 184 } 185 186 // If requested, check user can access the course. 187 if ($checkcourse) { 188 $coursecontext = \context_course::instance($cm->course); 189 if (!is_enrolled($coursecontext, $userid, '', true) && 190 !has_capability('moodle/course:view', $coursecontext, $userid)) { 191 return false; 192 } 193 } 194 195 // If availability is disabled, then all we need to do is check the visible flag. 196 if (!$CFG->enableavailability && $cm->visible) { 197 return true; 198 } 199 200 // When availability is enabled, access can depend on 3 things: 201 // 1. $cm->visible 202 // 2. $cm->availability 203 // 3. $section->availability (for activity section and possibly for 204 // parent sections) 205 // As a result we cannot take short cuts any longer and must get 206 // standard modinfo. 207 $modinfo = get_fast_modinfo($cm->course, $userid); 208 $cms = $modinfo->get_cms(); 209 if (!isset($cms[$cm->id])) { 210 // In some cases this might get called with a cmid that is no longer 211 // available, for example when a module is hidden at system level. 212 debugging('info_module::is_user_visible called with invalid cmid ' . $cm->id, DEBUG_DEVELOPER); 213 return false; 214 } 215 return $cms[$cm->id]->uservisible; 216 } 217 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |