2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Auxiliary manual user enrolment lib, the main purpose is to lower memory requirements...
20 * @package enrol_manual
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once($CFG->dirroot
. '/user/selector/lib.php');
28 require_once($CFG->dirroot
. '/enrol/locallib.php');
34 class enrol_manual_potential_participant
extends user_selector_base
{
37 public function __construct($name, $options) {
38 $this->enrolid
= $options['enrolid'];
39 parent
::__construct($name, $options);
44 * @param string $search
47 public function find_users($search) {
49 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
50 list($wherecondition, $params) = $this->search_sql($search, 'u');
51 $params['enrolid'] = $this->enrolid
;
53 $fields = 'SELECT ' . $this->required_fields_sql('u');
54 $countfields = 'SELECT COUNT(1)';
56 $sql = " FROM {user} u
57 LEFT JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
61 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext
);
62 $order = ' ORDER BY ' . $sort;
64 if (!$this->is_validating()) {
65 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
66 if ($potentialmemberscount > 100) {
67 return $this->too_many_results($search, $potentialmemberscount);
71 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
73 if (empty($availableusers)) {
79 $groupname = get_string('enrolcandidatesmatching', 'enrol', $search);
81 $groupname = get_string('enrolcandidates', 'enrol');
84 return array($groupname => $availableusers);
87 protected function get_options() {
88 $options = parent
::get_options();
89 $options['enrolid'] = $this->enrolid
;
90 $options['file'] = 'enrol/manual/locallib.php';
98 class enrol_manual_current_participant
extends user_selector_base
{
102 public function __construct($name, $options) {
103 $this->enrolid
= $options['enrolid'];
104 parent
::__construct($name, $options);
109 * @param string $search
112 public function find_users($search) {
114 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
115 list($wherecondition, $params) = $this->search_sql($search, 'u');
116 $params['enrolid'] = $this->enrolid
;
118 $fields = 'SELECT ' . $this->required_fields_sql('u');
119 $countfields = 'SELECT COUNT(1)';
121 $sql = " FROM {user} u
122 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
123 WHERE $wherecondition";
125 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext
);
126 $order = ' ORDER BY ' . $sort;
128 if (!$this->is_validating()) {
129 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
130 if ($potentialmemberscount > 100) {
131 return $this->too_many_results($search, $potentialmemberscount);
135 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
137 if (empty($availableusers)) {
143 $groupname = get_string('enrolledusersmatching', 'enrol', $search);
145 $groupname = get_string('enrolledusers', 'enrol');
148 return array($groupname => $availableusers);
151 protected function get_options() {
152 $options = parent
::get_options();
153 $options['enrolid'] = $this->enrolid
;
154 $options['file'] = 'enrol/manual/locallib.php';
160 * A bulk operation for the manual enrolment plugin to edit selected users.
162 * @copyright 2011 Sam Hemelryk
163 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
165 class enrol_manual_editselectedusers_operation
extends enrol_bulk_enrolment_operation
{
168 * Returns the title to display for this bulk operation.
172 public function get_title() {
173 return get_string('editselectedusers', 'enrol_manual');
177 * Returns the identifier for this bulk operation. This is the key used when the plugin
178 * returns an array containing all of the bulk operations it supports.
180 public function get_identifier() {
181 return 'editselectedusers';
185 * Processes the bulk operation request for the given userids with the provided properties.
187 * @param course_enrolment_manager $manager
188 * @param array $userids
189 * @param stdClass $properties The data returned by the form.
191 public function process(course_enrolment_manager
$manager, array $users, stdClass
$properties) {
194 if (!has_capability("enrol/manual:manage", $manager->get_context())) {
198 // Get all of the user enrolment id's.
200 $instances = array();
201 foreach ($users as $user) {
202 foreach ($user->enrolments
as $enrolment) {
203 $ueids[] = $enrolment->id
;
204 if (!array_key_exists($enrolment->id
, $instances)) {
205 $instances[$enrolment->id
] = $enrolment;
210 // Check that each instance is manageable by the current user.
211 foreach ($instances as $instance) {
212 if (!$this->plugin
->allow_manage($instance)) {
217 // Collect the known properties.
218 $status = $properties->status
;
219 $timestart = $properties->timestart
;
220 $timeend = $properties->timeend
;
222 list($ueidsql, $params) = $DB->get_in_or_equal($ueids, SQL_PARAMS_NAMED
);
224 $updatesql = array();
225 if ($status == ENROL_USER_ACTIVE ||
$status == ENROL_USER_SUSPENDED
) {
226 $updatesql[] = 'status = :status';
227 $params['status'] = (int)$status;
229 if (!empty($timestart)) {
230 $updatesql[] = 'timestart = :timestart';
231 $params['timestart'] = (int)$timestart;
233 if (!empty($timeend)) {
234 $updatesql[] = 'timeend = :timeend';
235 $params['timeend'] = (int)$timeend;
237 if (empty($updatesql)) {
241 // Update the modifierid.
242 $updatesql[] = 'modifierid = :modifierid';
243 $params['modifierid'] = (int)$USER->id
;
245 // Update the time modified.
246 $updatesql[] = 'timemodified = :timemodified';
247 $params['timemodified'] = time();
249 // Build the SQL statement.
250 $updatesql = join(', ', $updatesql);
251 $sql = "UPDATE {user_enrolments}
255 if ($DB->execute($sql, $params)) {
256 foreach ($users as $user) {
257 foreach ($user->enrolments
as $enrolment) {
258 $enrolment->courseid
= $enrolment->enrolmentinstance
->courseid
;
259 $enrolment->enrol
= 'manual';
260 events_trigger('user_enrol_modified', $enrolment);
270 * Returns a enrol_bulk_enrolment_operation extension form to be used
271 * in collecting required information for this operation to be processed.
273 * @param string|moodle_url|null $defaultaction
274 * @param mixed $defaultcustomdata
275 * @return enrol_manual_editselectedusers_form
277 public function get_form($defaultaction = null, $defaultcustomdata = null) {
279 require_once($CFG->dirroot
.'/enrol/manual/bulkchangeforms.php');
280 return new enrol_manual_editselectedusers_form($defaultaction, $defaultcustomdata);
286 * A bulk operation for the manual enrolment plugin to delete selected users enrolments.
288 * @copyright 2011 Sam Hemelryk
289 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
291 class enrol_manual_deleteselectedusers_operation
extends enrol_bulk_enrolment_operation
{
294 * Returns the title to display for this bulk operation.
298 public function get_identifier() {
299 return 'deleteselectedusers';
303 * Returns the identifier for this bulk operation. This is the key used when the plugin
304 * returns an array containing all of the bulk operations it supports.
308 public function get_title() {
309 return get_string('deleteselectedusers', 'enrol_manual');
313 * Returns a enrol_bulk_enrolment_operation extension form to be used
314 * in collecting required information for this operation to be processed.
316 * @param string|moodle_url|null $defaultaction
317 * @param mixed $defaultcustomdata
318 * @return enrol_manual_editselectedusers_form
320 public function get_form($defaultaction = null, $defaultcustomdata = null) {
322 require_once($CFG->dirroot
.'/enrol/manual/bulkchangeforms.php');
323 if (!array($defaultcustomdata)) {
324 $defaultcustomdata = array();
326 $defaultcustomdata['title'] = $this->get_title();
327 $defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_manual');
328 $defaultcustomdata['button'] = get_string('unenrolusers', 'enrol_manual');
329 return new enrol_manual_deleteselectedusers_form($defaultaction, $defaultcustomdata);
333 * Processes the bulk operation request for the given userids with the provided properties.
335 * @global moodle_database $DB
336 * @param course_enrolment_manager $manager
337 * @param array $userids
338 * @param stdClass $properties The data returned by the form.
340 public function process(course_enrolment_manager
$manager, array $users, stdClass
$properties) {
343 if (!has_capability("enrol/manual:unenrol", $manager->get_context())) {
346 foreach ($users as $user) {
347 foreach ($user->enrolments
as $enrolment) {
348 $plugin = $enrolment->enrolmentplugin
;
349 $instance = $enrolment->enrolmentinstance
;
350 if ($plugin->allow_unenrol_user($instance, $enrolment)) {
351 $plugin->unenrol_user($instance, $user->id
);
360 * Migrates all enrolments of the given plugin to enrol_manual plugin,
361 * this is used for example during plugin uninstallation.
363 * NOTE: this function does not trigger role and enrolment related events.
365 * @param string $enrol The enrolment method.
367 function enrol_manual_migrate_plugin_enrolments($enrol) {
370 if ($enrol === 'manual') {
371 // We can not migrate to self.
375 $manualplugin = enrol_get_plugin('manual');
377 $params = array('enrol'=>$enrol);
378 $sql = "SELECT e.id, e.courseid, e.status, MIN(me.id) AS mid, COUNT(ue.id) AS cu
380 JOIN {user_enrolments} ue ON (ue.enrolid = e.id)
381 JOIN {course} c ON (c.id = e.courseid)
382 LEFT JOIN {enrol} me ON (me.courseid = e.courseid AND me.enrol='manual')
383 WHERE e.enrol = :enrol
384 GROUP BY e.id, e.courseid, e.status
386 $rs = $DB->get_recordset_sql($sql, $params);
391 // Manual instance does not exist yet, add a new one.
392 $course = $DB->get_record('course', array('id'=>$e->courseid
), '*', MUST_EXIST
);
393 if ($minstance = $DB->get_record('enrol', array('courseid'=>$course->id
, 'enrol'=>'manual'))) {
394 // Already created by previous iteration.
395 $e->mid
= $minstance->id
;
396 } else if ($e->mid
= $manualplugin->add_default_instance($course)) {
397 $minstance = $DB->get_record('enrol', array('id'=>$e->mid
));
398 if ($e->status
!= ENROL_INSTANCE_ENABLED
) {
399 $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED
, array('id'=>$e->mid
));
400 $minstance->status
= ENROL_INSTANCE_DISABLED
;
404 $minstance = $DB->get_record('enrol', array('id'=>$e->mid
));
408 // This should never happen unless adding of default instance fails unexpectedly.
412 // First delete potential role duplicates.
413 $params = array('id'=>$e->id
, 'component'=>'enrol_'.$enrol, 'empty'=>$DB->sql_empty());
415 FROM {role_assignments} ra
416 JOIN {role_assignments} mra ON (mra.contextid = ra.contextid AND mra.userid = ra.userid AND mra.roleid = ra.roleid AND mra.component = :empty AND mra.itemid = 0)
417 WHERE ra.component = :component AND ra.itemid = :id";
418 $ras = $DB->get_records_sql($sql, $params);
419 $ras = array_keys($ras);
420 $DB->delete_records_list('role_assignments', 'id', $ras);
424 $sql = "UPDATE {role_assignments}
425 SET itemid = 0, component = :empty
426 WHERE itemid = :id AND component = :component";
427 $params = array('empty'=>$DB->sql_empty(), 'id'=>$e->id
, 'component'=>'enrol_'.$enrol);
428 $DB->execute($sql, $params);
430 // Delete potential enrol duplicates.
431 $params = array('id'=>$e->id
, 'mid'=>$e->mid
);
433 FROM {user_enrolments} ue
434 JOIN {user_enrolments} mue ON (mue.userid = ue.userid AND mue.enrolid = :mid)
435 WHERE ue.enrolid = :id";
436 $ues = $DB->get_records_sql($sql, $params);
437 $ues = array_keys($ues);
438 $DB->delete_records_list('user_enrolments', 'id', $ues);
441 // Migrate to manual enrol instance.
442 $params = array('id'=>$e->id
, 'mid'=>$e->mid
);
443 if ($e->status
!= ENROL_INSTANCE_ENABLED
and $minstance->status
== ENROL_INSTANCE_ENABLED
) {
444 $status = ", status = :disabled";
445 $params['disabled'] = ENROL_USER_SUSPENDED
;
449 $sql = "UPDATE {user_enrolments}
450 SET enrolid = :mid $status
451 WHERE enrolid = :id";
452 $DB->execute($sql, $params);