Merge branch 'MOODLE_21_STABLE' into install_21_STABLE
[moodle.git] / enrol / cohort / locallib.php
blob30835fd8fddd00cff62faf697c88ba4d3aa12105
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Local stuff for cohort enrolment plugin.
21 * @package enrol
22 * @subpackage cohort
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');
32 /**
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) {
40 global $DB;
42 if (!enrol_is_enabled('cohort')) {
43 return true;
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')) {
49 return true;
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);
58 return true;
61 public function member_removed($ca) {
62 global $DB;
64 // does anything want to sync with this parent?
65 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ca->cohortid, 'enrol'=>'cohort'), 'id ASC')) {
66 return true;
69 $plugin = enrol_get_plugin('cohort');
70 foreach ($enrols as $enrol) {
71 // no problem if already enrolled
72 $plugin->unenrol_user($enrol, $ca->userid);
75 return true;
78 public function deleted($cohort) {
79 global $DB;
81 // does anything want to sync with this parent?
82 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$cohort->id, 'enrol'=>'cohort'), 'id ASC')) {
83 return true;
86 $plugin = enrol_get_plugin('cohort');
87 foreach ($enrols as $enrol) {
88 $plugin->delete_instance($enrol);
91 return true;
95 /**
96 * Sync all cohort course links.
97 * @param int $courseid one course, empty mean all
98 * @return void
100 function enrol_cohort_sync($courseid = NULL) {
101 global $CFG, $DB;
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')) {
112 $params = array();
113 $onecourse = "";
114 if ($courseid) {
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);
133 $rs->close();
134 unset($instances);
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);
152 $rs->close();
153 unset($instances);
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";
163 $params = array();
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);
172 $rs->close();
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);
189 $rs->close();
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
202 * @param int $roleid
203 * @return int
205 function enrol_cohort_enrol_all_users(course_enrolment_manager $manager, $cohortid, $roleid) {
206 global $DB;
207 $context = $manager->get_context();
208 require_capability('moodle/course:enrolconfig', $context);
210 $instance = false;
211 $instances = $manager->get_enrolment_instances();
212 foreach ($instances as $i) {
213 if ($i->enrol == 'manual') {
214 $instance = $i;
215 break;
218 $plugin = enrol_get_plugin('manual');
219 if (!$instance || !$plugin || !$plugin->allow_enrol($instance) || !has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
220 return false;
222 $sql = "SELECT com.userid
223 FROM {cohort_members} com
224 LEFT JOIN (
225 SELECT *
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);
232 $count = 0;
233 foreach ($rs as $user) {
234 $count++;
235 $plugin->enrol_user($instance, $user->userid, $roleid);
237 $rs->close();
238 return $count;
242 * Gets all the cohorts the user is able to view.
244 * @global moodle_database $DB
245 * @return array
247 function enrol_cohort_get_cohorts(course_enrolment_manager $manager) {
248 global $DB;
249 $context = $manager->get_context();
250 $cohorts = array();
251 $instances = $manager->get_enrolment_instances();
252 $enrolled = array();
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
260 FROM {cohort}
261 WHERE contextid $sqlparents
262 ORDER BY name ASC";
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)) {
267 continue;
269 $cohorts[$c->id] = array(
270 'cohortid'=>$c->id,
271 'name'=>format_string($c->name),
272 'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
273 'enrolled'=>in_array($c->id, $enrolled)
276 $rs->close();
277 return $cohorts;