-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlocallib.php
114 lines (99 loc) · 3.53 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Plugin local library methods
*
* @package local_cohortrole
* @copyright 2013 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('LOCAL_COHORTROLE_ROLE_COMPONENT', 'local_cohortrole');
/**
* Assign users to a role; using local role component
*
* @param integer $cohortid the id of a cohort
* @param integer $roleid the id of a role
* @param array $userids an array of user ids to assign
* @return void
*/
function local_cohortrole_role_assign($cohortid, $roleid, array $userids) {
$context = context_system::instance();
foreach ($userids as $userid) {
try {
$user = core_user::get_user($userid, '*', MUST_EXIST);
core_user::require_active_user($user);
role_assign($roleid, $user->id, $context->id, LOCAL_COHORTROLE_ROLE_COMPONENT, $cohortid);
} catch (Exception $e) { // phpcs:ignore
// Exception is caught. Do nothing.
}
}
}
/**
* Unassign users from a role; using local role component
*
* @param integer $cohortid the id of a cohort
* @param integer $roleid the id of a role
* @param array $userids an array of user ids to unassign
* @return void
*/
function local_cohortrole_role_unassign($cohortid, $roleid, array $userids) {
$context = context_system::instance();
foreach ($userids as $userid) {
role_unassign($roleid, $userid, $context->id, LOCAL_COHORTROLE_ROLE_COMPONENT, $cohortid);
}
}
/**
* Add users to a role that synchronizes from a cohort
*
* @param integer $cohortid the id of a cohort
* @param integer $roleid the id of a role
* @return void
*/
function local_cohortrole_synchronize($cohortid, $roleid) {
global $DB;
$userids = $DB->get_records_menu('cohort_members', ['cohortid' => $cohortid], null, 'id, userid');
local_cohortrole_role_assign($cohortid, $roleid, $userids);
}
/**
* Remove users from a role that was synchronized from a cohort
*
* @param integer $cohortid the id of a cohort
* @param integer|null $roleid the id of a role, all roles if null
* @return void
*/
function local_cohortrole_unsynchronize($cohortid, $roleid = null) {
$params = [
'contextid' => context_system::instance()->id, 'component' => LOCAL_COHORTROLE_ROLE_COMPONENT, 'itemid' => $cohortid, ];
if ($roleid === null) {
$roleids = local_cohortrole_get_cohort_roles($cohortid);
} else {
$roleids = [$roleid];
}
foreach ($roleids as $roleid) {
$params['roleid'] = $roleid;
role_unassign_all($params, false, false);
}
}
/**
* Get roles defined as being populated by a cohort
*
* @param integer $cohortid the id of a cohort
* @return array role ids
*/
function local_cohortrole_get_cohort_roles($cohortid) {
global $DB;
return $DB->get_records_menu('local_cohortrole', ['cohortid' => $cohortid], null, 'id, roleid');
}