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 > $this->maxusersperpage
) {
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 > $this->maxusersperpage
) {
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';
261 $event = \core\event\user_enrolment_updated
::create(
263 'objectid' => $enrolment->id
,
264 'courseid' => $enrolment->courseid
,
265 'context' => context_course
::instance($enrolment->courseid
),
266 'relateduserid' => $user->id
,
267 'other' => array('enrol' => 'manual')
273 // Delete cached course contacts for this course because they may be affected.
274 cache
::make('core', 'coursecontacts')->delete($manager->get_context()->instanceid
);
282 * Returns a enrol_bulk_enrolment_operation extension form to be used
283 * in collecting required information for this operation to be processed.
285 * @param string|moodle_url|null $defaultaction
286 * @param mixed $defaultcustomdata
287 * @return enrol_manual_editselectedusers_form
289 public function get_form($defaultaction = null, $defaultcustomdata = null) {
291 require_once($CFG->dirroot
.'/enrol/manual/bulkchangeforms.php');
292 return new enrol_manual_editselectedusers_form($defaultaction, $defaultcustomdata);
298 * A bulk operation for the manual enrolment plugin to delete selected users enrolments.
300 * @copyright 2011 Sam Hemelryk
301 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
303 class enrol_manual_deleteselectedusers_operation
extends enrol_bulk_enrolment_operation
{
306 * Returns the title to display for this bulk operation.
310 public function get_identifier() {
311 return 'deleteselectedusers';
315 * Returns the identifier for this bulk operation. This is the key used when the plugin
316 * returns an array containing all of the bulk operations it supports.
320 public function get_title() {
321 return get_string('deleteselectedusers', 'enrol_manual');
325 * Returns a enrol_bulk_enrolment_operation extension form to be used
326 * in collecting required information for this operation to be processed.
328 * @param string|moodle_url|null $defaultaction
329 * @param mixed $defaultcustomdata
330 * @return enrol_manual_editselectedusers_form
332 public function get_form($defaultaction = null, $defaultcustomdata = null) {
334 require_once($CFG->dirroot
.'/enrol/manual/bulkchangeforms.php');
335 if (!array($defaultcustomdata)) {
336 $defaultcustomdata = array();
338 $defaultcustomdata['title'] = $this->get_title();
339 $defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_manual');
340 $defaultcustomdata['button'] = get_string('unenrolusers', 'enrol_manual');
341 return new enrol_manual_deleteselectedusers_form($defaultaction, $defaultcustomdata);
345 * Processes the bulk operation request for the given userids with the provided properties.
347 * @global moodle_database $DB
348 * @param course_enrolment_manager $manager
349 * @param array $userids
350 * @param stdClass $properties The data returned by the form.
352 public function process(course_enrolment_manager
$manager, array $users, stdClass
$properties) {
355 if (!has_capability("enrol/manual:unenrol", $manager->get_context())) {
358 foreach ($users as $user) {
359 foreach ($user->enrolments
as $enrolment) {
360 $plugin = $enrolment->enrolmentplugin
;
361 $instance = $enrolment->enrolmentinstance
;
362 if ($plugin->allow_unenrol_user($instance, $enrolment)) {
363 $plugin->unenrol_user($instance, $user->id
);
372 * Migrates all enrolments of the given plugin to enrol_manual plugin,
373 * this is used for example during plugin uninstallation.
375 * NOTE: this function does not trigger role and enrolment related events.
377 * @param string $enrol The enrolment method.
379 function enrol_manual_migrate_plugin_enrolments($enrol) {
382 if ($enrol === 'manual') {
383 // We can not migrate to self.
387 $manualplugin = enrol_get_plugin('manual');
389 $params = array('enrol'=>$enrol);
390 $sql = "SELECT e.id, e.courseid, e.status, MIN(me.id) AS mid, COUNT(ue.id) AS cu
392 JOIN {user_enrolments} ue ON (ue.enrolid = e.id)
393 JOIN {course} c ON (c.id = e.courseid)
394 LEFT JOIN {enrol} me ON (me.courseid = e.courseid AND me.enrol='manual')
395 WHERE e.enrol = :enrol
396 GROUP BY e.id, e.courseid, e.status
398 $rs = $DB->get_recordset_sql($sql, $params);
403 // Manual instance does not exist yet, add a new one.
404 $course = $DB->get_record('course', array('id'=>$e->courseid
), '*', MUST_EXIST
);
405 if ($minstance = $DB->get_record('enrol', array('courseid'=>$course->id
, 'enrol'=>'manual'))) {
406 // Already created by previous iteration.
407 $e->mid
= $minstance->id
;
408 } else if ($e->mid
= $manualplugin->add_default_instance($course)) {
409 $minstance = $DB->get_record('enrol', array('id'=>$e->mid
));
410 if ($e->status
!= ENROL_INSTANCE_ENABLED
) {
411 $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED
, array('id'=>$e->mid
));
412 $minstance->status
= ENROL_INSTANCE_DISABLED
;
416 $minstance = $DB->get_record('enrol', array('id'=>$e->mid
));
420 // This should never happen unless adding of default instance fails unexpectedly.
421 debugging('Failed to find manual enrolment instance', DEBUG_DEVELOPER
);
425 // First delete potential role duplicates.
426 $params = array('id'=>$e->id
, 'component'=>'enrol_'.$enrol, 'empty'=>'');
428 FROM {role_assignments} ra
429 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)
430 WHERE ra.component = :component AND ra.itemid = :id";
431 $ras = $DB->get_records_sql($sql, $params);
432 $ras = array_keys($ras);
433 $DB->delete_records_list('role_assignments', 'id', $ras);
437 $sql = "UPDATE {role_assignments}
438 SET itemid = 0, component = :empty
439 WHERE itemid = :id AND component = :component";
440 $params = array('empty'=>'', 'id'=>$e->id
, 'component'=>'enrol_'.$enrol);
441 $DB->execute($sql, $params);
443 // Delete potential enrol duplicates.
444 $params = array('id'=>$e->id
, 'mid'=>$e->mid
);
446 FROM {user_enrolments} ue
447 JOIN {user_enrolments} mue ON (mue.userid = ue.userid AND mue.enrolid = :mid)
448 WHERE ue.enrolid = :id";
449 $ues = $DB->get_records_sql($sql, $params);
450 $ues = array_keys($ues);
451 $DB->delete_records_list('user_enrolments', 'id', $ues);
454 // Migrate to manual enrol instance.
455 $params = array('id'=>$e->id
, 'mid'=>$e->mid
);
456 if ($e->status
!= ENROL_INSTANCE_ENABLED
and $minstance->status
== ENROL_INSTANCE_ENABLED
) {
457 $status = ", status = :disabled";
458 $params['disabled'] = ENROL_USER_SUSPENDED
;
462 $sql = "UPDATE {user_enrolments}
463 SET enrolid = :mid $status
464 WHERE enrolid = :id";
465 $DB->execute($sql, $params);
471 * Gets an array of the cohorts that can be enrolled in this course.
473 * @param int $enrolid
474 * @param string $search
475 * @param int $page Defaults to 0
476 * @param int $perpage Defaults to 25
477 * @param int $addedenrollment
478 * @return array Array(totalcohorts => int, cohorts => array)
480 function enrol_manual_get_potential_cohorts($context, $enrolid, $search = '', $page = 0, $perpage = 25, $addedenrollment = 0) {
482 require_once($CFG->dirroot
. '/cohort/lib.php');
484 $allcohorts = cohort_get_available_cohorts($context, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY
, 0, 0, $search);
485 $totalcohorts = count($allcohorts);
488 foreach ($allcohorts as $c) {
489 if ($cnt >= $page * $perpage && (!$perpage ||
$cnt < ($page+
1)*$perpage)) {
490 $cohorts[] = (object)array(
492 'name' => format_string($c->name
, true, array('context' => $c->contextid
)),
493 'cnt' => $c->memberscnt
- $c->enrolledcnt
498 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts);