[ 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 * Condition main class. 19 * 20 * @package availability_group 21 * @copyright 2014 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace availability_group; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Condition main class. 31 * 32 * @package availability_group 33 * @copyright 2014 The Open University 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class condition extends \core_availability\condition { 37 /** @var array Array from group id => name */ 38 protected static $groupnames = array(); 39 40 /** @var int ID of group that this condition requires, or 0 = any group */ 41 protected $groupid; 42 43 /** 44 * Constructor. 45 * 46 * @param \stdClass $structure Data structure from JSON decode 47 * @throws \coding_exception If invalid data structure. 48 */ 49 public function __construct($structure) { 50 // Get group id. 51 if (!property_exists($structure, 'id')) { 52 $this->groupid = 0; 53 } else if (is_int($structure->id)) { 54 $this->groupid = $structure->id; 55 } else { 56 throw new \coding_exception('Invalid ->id for group condition'); 57 } 58 } 59 60 public function save() { 61 $result = (object)array('type' => 'group'); 62 if ($this->groupid) { 63 $result->id = $this->groupid; 64 } 65 return $result; 66 } 67 68 public function is_available($not, \core_availability\info $info, $grabthelot, $userid) { 69 $course = $info->get_course(); 70 $context = \context_course::instance($course->id); 71 $allow = true; 72 if (!has_capability('moodle/site:accessallgroups', $context, $userid)) { 73 // Get all groups the user belongs to. 74 $groups = $info->get_modinfo()->get_groups(); 75 if ($this->groupid) { 76 $allow = in_array($this->groupid, $groups); 77 } else { 78 // No specific group. Allow if they belong to any group at all. 79 $allow = $groups ? true : false; 80 } 81 82 // The NOT condition applies before accessallgroups (i.e. if you 83 // set something to be available to those NOT in group X, 84 // people with accessallgroups can still access it even if 85 // they are in group X). 86 if ($not) { 87 $allow = !$allow; 88 } 89 } 90 return $allow; 91 } 92 93 public function get_description($full, $not, \core_availability\info $info) { 94 global $DB; 95 96 if ($this->groupid) { 97 // Need to get the name for the group. Unfortunately this requires 98 // a database query. To save queries, get all groups for course at 99 // once in a static cache. 100 $course = $info->get_course(); 101 if (!array_key_exists($this->groupid, self::$groupnames)) { 102 $coursegroups = $DB->get_records( 103 'groups', array('courseid' => $course->id), '', 'id, name'); 104 foreach ($coursegroups as $rec) { 105 self::$groupnames[$rec->id] = $rec->name; 106 } 107 } 108 109 // If it still doesn't exist, it must have been misplaced. 110 if (!array_key_exists($this->groupid, self::$groupnames)) { 111 $name = get_string('missing', 'availability_group'); 112 } else { 113 $context = \context_course::instance($course->id); 114 $name = format_string(self::$groupnames[$this->groupid], true, 115 array('context' => $context)); 116 } 117 } else { 118 return get_string($not ? 'requires_notanygroup' : 'requires_anygroup', 119 'availability_group'); 120 } 121 122 return get_string($not ? 'requires_notgroup' : 'requires_group', 123 'availability_group', $name); 124 } 125 126 protected function get_debug_string() { 127 return $this->groupid ? '#' . $this->groupid : 'any'; 128 } 129 130 public function update_after_restore($restoreid, $courseid, \base_logger $logger, $name) { 131 global $DB; 132 if (!$this->groupid) { 133 return false; 134 } 135 $rec = \restore_dbops::get_backup_ids_record($restoreid, 'group', $this->groupid); 136 if (!$rec || !$rec->newitemid) { 137 // If we are on the same course (e.g. duplicate) then we can just 138 // use the existing one. 139 if ($DB->record_exists('groups', 140 array('id' => $this->groupid, 'courseid' => $courseid))) { 141 return false; 142 } 143 // Otherwise it's a warning. 144 $this->groupid = -1; 145 $logger->process('Restored item (' . $name . 146 ') has availability condition on group that was not restored', 147 \backup::LOG_WARNING); 148 } else { 149 $this->groupid = (int)$rec->newitemid; 150 } 151 return true; 152 } 153 154 public function update_dependency_id($table, $oldid, $newid) { 155 if ($table === 'groups' && (int)$this->groupid === (int)$oldid) { 156 $this->groupid = $newid; 157 return true; 158 } else { 159 return false; 160 } 161 } 162 163 /** 164 * Wipes the static cache used to store grouping names. 165 */ 166 public static function wipe_static_cache() { 167 self::$groupnames = array(); 168 } 169 170 public function is_applied_to_user_lists() { 171 // Group conditions are assumed to be 'permanent', so they affect the 172 // display of user lists for activities. 173 return true; 174 } 175 176 public function filter_user_list(array $users, $not, \core_availability\info $info, 177 \core_availability\capability_checker $checker) { 178 global $CFG, $DB; 179 180 // If the array is empty already, just return it. 181 if (!$users) { 182 return $users; 183 } 184 185 require_once($CFG->libdir . '/grouplib.php'); 186 $course = $info->get_course(); 187 188 // List users for this course who match the condition. 189 if ($this->groupid) { 190 $groupusers = groups_get_members($this->groupid, 'u.id', 'u.id ASC'); 191 } else { 192 $groupusers = $DB->get_records_sql(" 193 SELECT DISTINCT gm.userid 194 FROM {groups} g 195 JOIN {groups_members} gm ON gm.groupid = g.id 196 WHERE g.courseid = ?", array($course->id)); 197 } 198 199 // List users who have access all groups. 200 $aagusers = $checker->get_users_by_capability('moodle/site:accessallgroups'); 201 202 // Filter the user list. 203 $result = array(); 204 foreach ($users as $id => $user) { 205 // Always include users with access all groups. 206 if (array_key_exists($id, $aagusers)) { 207 $result[$id] = $user; 208 continue; 209 } 210 // Other users are included or not based on group membership. 211 $allow = array_key_exists($id, $groupusers); 212 if ($not) { 213 $allow = !$allow; 214 } 215 if ($allow) { 216 $result[$id] = $user; 217 } 218 } 219 return $result; 220 } 221 222 /** 223 * Returns a JSON object which corresponds to a condition of this type. 224 * 225 * Intended for unit testing, as normally the JSON values are constructed 226 * by JavaScript code. 227 * 228 * @param int $groupid Required group id (0 = any group) 229 * @return stdClass Object representing condition 230 */ 231 public static function get_json($groupid = 0) { 232 return (object)array('type' => 'group', 'id' => (int)$groupid); 233 } 234 235 public function get_user_list_sql($not, \core_availability\info $info, $onlyactive) { 236 global $DB; 237 238 // Get enrolled users with access all groups. These always are allowed. 239 list($aagsql, $aagparams) = get_enrolled_sql( 240 $info->get_context(), 'moodle/site:accessallgroups', 0, $onlyactive); 241 242 // Get all enrolled users. 243 list ($enrolsql, $enrolparams) = 244 get_enrolled_sql($info->get_context(), '', 0, $onlyactive); 245 246 // Condition for specified or any group. 247 $matchparams = array(); 248 if ($this->groupid) { 249 $matchsql = "SELECT 1 250 FROM {groups_members} gm 251 WHERE gm.userid = userids.id 252 AND gm.groupid = " . 253 self::unique_sql_parameter($matchparams, $this->groupid); 254 } else { 255 $matchsql = "SELECT 1 256 FROM {groups_members} gm 257 JOIN {groups} g ON g.id = gm.groupid 258 WHERE gm.userid = userids.id 259 AND g.courseid = " . 260 self::unique_sql_parameter($matchparams, $info->get_course()->id); 261 } 262 263 // Overall query combines all this. 264 $condition = $not ? 'NOT' : ''; 265 $sql = "SELECT userids.id 266 FROM ($enrolsql) userids 267 WHERE (userids.id IN ($aagsql)) OR $condition EXISTS ($matchsql)"; 268 return array($sql, array_merge($enrolparams, $aagparams, $matchparams)); 269 } 270 }
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 |