Automatic installer lang files (20101122)
[moodle.git] / enrol / cohort / locallib.php
blobff0b10774e5856d4c1d5ec8f27f58839235c3887
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 /**
30 * Event handler for cohort enrolment plugin.
32 * We try to keep everything in sync via listening to events,
33 * it may fail sometimes, so we always do a full sync in cron too.
35 class enrol_cohort_handler {
36 public function member_added($ca) {
37 global $DB;
39 if (!enrol_is_enabled('cohort')) {
40 return true;
43 // does anything want to sync with this parent?
44 //TODO: add join to role table to make sure that roleid actually exists
45 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ca->cohortid, 'enrol'=>'cohort'), 'id ASC')) {
46 return true;
49 $plugin = enrol_get_plugin('cohort');
50 foreach ($enrols as $enrol) {
51 // no problem if already enrolled
52 $plugin->enrol_user($enrol, $ca->userid, $enrol->roleid);
55 return true;
58 public function member_removed($ca) {
59 global $DB;
61 // does anything want to sync with this parent?
62 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ca->cohortid, 'enrol'=>'cohort'), 'id ASC')) {
63 return true;
66 $plugin = enrol_get_plugin('cohort');
67 foreach ($enrols as $enrol) {
68 // no problem if already enrolled
69 $plugin->unenrol_user($enrol, $ca->userid);
72 return true;
75 public function deleted($cohort) {
76 global $DB;
78 // does anything want to sync with this parent?
79 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$cohort->id, 'enrol'=>'cohort'), 'id ASC')) {
80 return true;
83 $plugin = enrol_get_plugin('cohort');
84 foreach ($enrols as $enrol) {
85 $plugin->delete_instance($enrol);
88 return true;
92 /**
93 * Sync all cohort course links.
94 * @param int $courseid one course, empty mean all
95 * @return void
97 function enrol_cohort_sync($courseid = NULL) {
98 global $CFG, $DB;
100 // unfortunately this may take a long time
101 @set_time_limit(0); //if this fails during upgrade we can continue from cron, no big deal
103 $cohort = enrol_get_plugin('cohort');
105 $onecourse = $courseid ? "AND e.courseid = :courseid" : "";
107 // iterate through all not enrolled yet users
108 if (enrol_is_enabled('cohort')) {
109 $params = array();
110 $onecourse = "";
111 if ($courseid) {
112 $params['courseid'] = $courseid;
113 $onecourse = "AND e.courseid = :courseid";
115 $sql = "SELECT cm.userid, e.id AS enrolid
116 FROM {cohort_members} cm
117 JOIN {enrol} e ON (e.customint1 = cm.cohortid AND e.status = :statusenabled AND e.enrol = 'cohort' $onecourse)
118 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = cm.userid)
119 WHERE ue.id IS NULL";
120 $params['statusenabled'] = ENROL_INSTANCE_ENABLED;
121 $params['courseid'] = $courseid;
122 $rs = $DB->get_recordset_sql($sql, $params);
123 $instances = array(); //cache
124 foreach($rs as $ue) {
125 if (!isset($instances[$ue->enrolid])) {
126 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
128 $cohort->enrol_user($instances[$ue->enrolid], $ue->userid);
130 $rs->close();
131 unset($instances);
134 // unenrol as necessary - ignore enabled flag, we want to get rid of all
135 $sql = "SELECT ue.userid, e.id AS enrolid
136 FROM {user_enrolments} ue
137 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' $onecourse)
138 LEFT JOIN {cohort_members} cm ON (cm.cohortid = e.customint1 AND cm.userid = ue.userid)
139 WHERE cm.id IS NULL";
140 //TODO: this may use a bit of SQL optimisation
141 $rs = $DB->get_recordset_sql($sql, array('courseid'=>$courseid));
142 $instances = array(); //cache
143 foreach($rs as $ue) {
144 if (!isset($instances[$ue->enrolid])) {
145 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
147 $cohort->unenrol_user($instances[$ue->enrolid], $ue->userid);
149 $rs->close();
150 unset($instances);
152 // now assign all necessary roles
153 if (enrol_is_enabled('cohort')) {
154 $sql = "SELECT e.roleid, ue.userid, c.id AS contextid, e.id AS itemid
155 FROM {user_enrolments} ue
156 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' AND e.status = :statusenabled $onecourse)
157 JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :coursecontext)
158 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)
159 WHERE ra.id IS NULL";
160 $params = array();
161 $params['statusenabled'] = ENROL_INSTANCE_ENABLED;
162 $params['coursecontext'] = CONTEXT_COURSE;
163 $params['courseid'] = $courseid;
165 $rs = $DB->get_recordset_sql($sql, $params);
166 foreach($rs as $ra) {
167 role_assign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_cohort', $ra->itemid);
169 $rs->close();
172 // remove unwanted roles - include ignored roles and disabled plugins too
173 $onecourse = $courseid ? "AND c.instanceid = :courseid" : "";
174 $sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid
175 FROM {role_assignments} ra
176 JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :coursecontext $onecourse)
177 LEFT JOIN (SELECT e.id AS enrolid, e.roleid, ue.userid
178 FROM {user_enrolments} ue
179 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort')
180 ) x ON (x.enrolid = ra.itemid AND ra.component = 'enrol_cohort' AND x.roleid = ra.roleid AND x.userid = ra.userid)
181 WHERE x.userid IS NULL AND ra.component = 'enrol_cohort'";
182 $params = array('coursecontext' => CONTEXT_COURSE, 'courseid' => $courseid);
184 $rs = $DB->get_recordset_sql($sql, $params);
185 foreach($rs as $ra) {
186 role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_cohort', $ra->itemid);
188 $rs->close();