3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Local stuff for cohort enrolment plugin.
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
. '/enrol/locallib.php');
33 * Event handler for cohort enrolment plugin.
35 * We try to keep everything in sync via listening to events,
36 * it may fail sometimes, so we always do a full sync in cron too.
38 class enrol_cohort_handler
{
39 public function member_added($ca) {
42 if (!enrol_is_enabled('cohort')) {
46 // does anything want to sync with this parent?
47 //TODO: add join to role table to make sure that roleid actually exists
48 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ca->cohortid
, 'enrol'=>'cohort'), 'id ASC')) {
52 $plugin = enrol_get_plugin('cohort');
53 foreach ($enrols as $enrol) {
54 // no problem if already enrolled
55 $plugin->enrol_user($enrol, $ca->userid
, $enrol->roleid
);
61 public function member_removed($ca) {
64 // does anything want to sync with this parent?
65 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ca->cohortid
, 'enrol'=>'cohort'), 'id ASC')) {
69 $plugin = enrol_get_plugin('cohort');
70 foreach ($enrols as $enrol) {
71 // no problem if already enrolled
72 $plugin->unenrol_user($enrol, $ca->userid
);
78 public function deleted($cohort) {
81 // does anything want to sync with this parent?
82 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$cohort->id
, 'enrol'=>'cohort'), 'id ASC')) {
86 $plugin = enrol_get_plugin('cohort');
87 foreach ($enrols as $enrol) {
88 $plugin->delete_instance($enrol);
96 * Sync all cohort course links.
97 * @param int $courseid one course, empty mean all
100 function enrol_cohort_sync($courseid = NULL) {
103 // unfortunately this may take a long time
104 @set_time_limit
(0); //if this fails during upgrade we can continue from cron, no big deal
106 $cohort = enrol_get_plugin('cohort');
108 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
110 // iterate through all not enrolled yet users
111 if (enrol_is_enabled('cohort')) {
115 $params['courseid'] = $courseid;
116 $onecourse = "AND e.courseid = :courseid";
118 $sql = "SELECT cm.userid, e.id AS enrolid
119 FROM {cohort_members} cm
120 JOIN {enrol} e ON (e.customint1 = cm.cohortid AND e.status = :statusenabled AND e.enrol = 'cohort' $onecourse)
121 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = cm.userid)
122 WHERE ue.id IS NULL";
123 $params['statusenabled'] = ENROL_INSTANCE_ENABLED
;
124 $params['courseid'] = $courseid;
125 $rs = $DB->get_recordset_sql($sql, $params);
126 $instances = array(); //cache
127 foreach($rs as $ue) {
128 if (!isset($instances[$ue->enrolid
])) {
129 $instances[$ue->enrolid
] = $DB->get_record('enrol', array('id'=>$ue->enrolid
));
131 $cohort->enrol_user($instances[$ue->enrolid
], $ue->userid
);
137 // unenrol as necessary - ignore enabled flag, we want to get rid of all
138 $sql = "SELECT ue.userid, e.id AS enrolid
139 FROM {user_enrolments} ue
140 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' $onecourse)
141 LEFT JOIN {cohort_members} cm ON (cm.cohortid = e.customint1 AND cm.userid = ue.userid)
142 WHERE cm.id IS NULL";
143 //TODO: this may use a bit of SQL optimisation
144 $rs = $DB->get_recordset_sql($sql, array('courseid'=>$courseid));
145 $instances = array(); //cache
146 foreach($rs as $ue) {
147 if (!isset($instances[$ue->enrolid
])) {
148 $instances[$ue->enrolid
] = $DB->get_record('enrol', array('id'=>$ue->enrolid
));
150 $cohort->unenrol_user($instances[$ue->enrolid
], $ue->userid
);
155 // now assign all necessary roles
156 if (enrol_is_enabled('cohort')) {
157 $sql = "SELECT e.roleid, ue.userid, c.id AS contextid, e.id AS itemid
158 FROM {user_enrolments} ue
159 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' AND e.status = :statusenabled $onecourse)
160 JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :coursecontext)
161 LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.userid = ue.userid AND ra.itemid = e.id AND ra.component = 'enrol_cohort' AND e.roleid = ra.roleid)
162 WHERE ra.id IS NULL";
164 $params['statusenabled'] = ENROL_INSTANCE_ENABLED
;
165 $params['coursecontext'] = CONTEXT_COURSE
;
166 $params['courseid'] = $courseid;
168 $rs = $DB->get_recordset_sql($sql, $params);
169 foreach($rs as $ra) {
170 role_assign($ra->roleid
, $ra->userid
, $ra->contextid
, 'enrol_cohort', $ra->itemid
);
175 // remove unwanted roles - include ignored roles and disabled plugins too
176 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
177 $sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid
178 FROM {role_assignments} ra
179 JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :coursecontext)
180 JOIN {enrol} e ON (e.id = ra.itemid AND e.enrol = 'cohort' $onecourse)
181 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
182 WHERE ra.component = 'enrol_cohort' AND ue.id IS NULL";
183 $params = array('coursecontext' => CONTEXT_COURSE
, 'courseid' => $courseid);
185 $rs = $DB->get_recordset_sql($sql, $params);
186 foreach($rs as $ra) {
187 role_unassign($ra->roleid
, $ra->userid
, $ra->contextid
, 'enrol_cohort', $ra->itemid
);
194 * Enrols all of the users in a cohort through a manual plugin instance.
196 * In order for this to succeed the course must contain a valid manual
197 * enrolment plugin instance that the user has permission to enrol users through.
199 * @global moodle_database $DB
200 * @param course_enrolment_manager $manager
201 * @param int $cohortid
205 function enrol_cohort_enrol_all_users(course_enrolment_manager
$manager, $cohortid, $roleid) {
207 $context = $manager->get_context();
208 require_capability('moodle/course:enrolconfig', $context);
211 $instances = $manager->get_enrolment_instances();
212 foreach ($instances as $i) {
213 if ($i->enrol
== 'manual') {
218 $plugin = enrol_get_plugin('manual');
219 if (!$instance ||
!$plugin ||
!$plugin->allow_enrol($instance) ||
!has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
222 $sql = "SELECT com.userid
223 FROM {cohort_members} com
226 FROM {user_enrolments} ue
227 WHERE ue.enrolid = :enrolid
228 ) ue ON ue.userid=com.userid
229 WHERE com.cohortid = :cohortid AND ue.id IS NULL";
230 $params = array('cohortid' => $cohortid, 'enrolid' => $instance->id
);
231 $rs = $DB->get_recordset_sql($sql, $params);
233 foreach ($rs as $user) {
235 $plugin->enrol_user($instance, $user->userid
, $roleid);
242 * Gets all the cohorts the user is able to view.
244 * @global moodle_database $DB
247 function enrol_cohort_get_cohorts(course_enrolment_manager
$manager) {
249 $context = $manager->get_context();
251 $instances = $manager->get_enrolment_instances();
253 foreach ($instances as $instance) {
254 if ($instance->enrol
== 'cohort') {
255 $enrolled[] = $instance->customint1
;
258 list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
259 $sql = "SELECT id, name, contextid
261 WHERE contextid $sqlparents
263 $rs = $DB->get_recordset_sql($sql, $params);
264 foreach ($rs as $c) {
265 $context = get_context_instance_by_id($c->contextid
);
266 if (!has_capability('moodle/cohort:view', $context)) {
269 $cohorts[$c->id
] = array(
271 'name'=>format_string($c->name
),
272 'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id
)),
273 'enrolled'=>in_array($c->id
, $enrolled)