MDL-14589, added filepath
[moodle.git] / enrol / locallib.php
CommitLineData
a70eb30f
SH
1<?php
2
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.
14//
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/>.
17
18/**
19 * This file contains the course_enrolment_manager class which is used to interface
20 * with the functions that exist in enrollib.php in relation to a single course.
21 *
22 * @package moodlecore
23 * @copyright 2010 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27/**
28 * This class provides a targeted tied together means of interfacing the enrolment
29 * tasks together with a course.
30 *
31 * It is provided as a convenience more than anything else.
32 *
33 * @copyright 2010 Sam Hemelryk
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class course_enrolment_manager {
37
38 /**
39 * The course context
40 * @var stdClass
41 */
42 protected $context;
43 /**
44 * The course we are managing enrolments for
45 * @var stdClass
46 */
47 protected $course = null;
48 /**
49 * Limits the focus of the manager to one enrolment plugin instance
50 * @var string
51 */
52 protected $instancefilter = null;
53
54 /**
55 * The total number of users enrolled in the course
56 * Populated by course_enrolment_manager::get_total_users
57 * @var int
58 */
59 protected $totalusers = null;
60 /**
61 * An array of users currently enrolled in the course
62 * Populated by course_enrolment_manager::get_users
63 * @var array
64 */
65 protected $users = array();
66
67 /**#@+
68 * These variables are used to cache the information this class uses
69 * please never use these directly instead use thier get_ counterparts.
70 * @access private
71 * @var array
72 */
73 private $_instancessql = null;
74 private $_instances = null;
75 private $_inames = null;
76 private $_plugins = null;
77 private $_roles = null;
78 private $_assignableroles = null;
79 private $_groups = null;
80 /**#@-*/
81
82 /**
83 * Constructs the course enrolment manager
84 *
85 * @param stdClass $course
86 * @param string $instancefilter
87 */
88 public function __construct($course, $instancefilter = null) {
89 $this->context = get_context_instance(CONTEXT_COURSE, $course->id);
90 $this->course = $course;
91 $this->instancefilter = $instancefilter;
92 }
93
94 /**
95 * Returns the total number of enrolled users in the course.
96 *
97 * If a filter was specificed this will be the total number of users enrolled
98 * in this course by means of that instance.
99 *
100 * @global moodle_database $DB
101 * @return int
102 */
103 public function get_total_users() {
104 global $DB;
105 if ($this->totalusers === null) {
106 list($instancessql, $params, $filter) = $this->get_instance_sql();
107 $sqltotal = "SELECT COUNT(DISTINCT u.id)
108 FROM {user} u
109 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid $instancessql)
110 JOIN {enrol} e ON (e.id = ue.enrolid)";
111 $this->totalusers = (int)$DB->count_records_sql($sqltotal, $params);
112 }
113 return $this->totalusers;
114 }
115
116 /**
117 * Gets all of the users enrolled in this course.
118 *
119 * If a filter was specificed this will be the users who were enrolled
120 * in this course by means of that instance.
121 *
122 * @global moodle_database $DB
123 * @param string $sort
124 * @param string $direction ASC or DESC
125 * @param int $page First page should be 0
126 * @param int $perpage Defaults to 25
127 * @return array
128 */
129 public function get_users($sort, $direction='ASC', $page=0, $perpage=25) {
130 global $DB;
131 if ($direction !== 'ASC') {
132 $direction = 'DESC';
133 }
134 $key = md5("$sort-$direction-$page-$perpage");
135 if (!array_key_exists($key, $this->users)) {
136 list($instancessql, $params, $filter) = $this->get_instance_sql();
137 $sql = "SELECT DISTINCT u.*, ul.timeaccess AS lastseen
138 FROM {user} u
139 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid $instancessql)
140 JOIN {enrol} e ON (e.id = ue.enrolid)
141 LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = u.id)";
142 if ($sort === 'firstname') {
143 $sql .= " ORDER BY u.firstname $direction, u.lastname $direction";
144 } else if ($sort === 'lastname') {
145 $sql .= " ORDER BY u.lastname $direction, u.firstname $direction";
146 } else if ($sort === 'email') {
147 $sql .= " ORDER BY u.email $direction, u.lastname $direction, u.firstname $direction";
148 } else if ($sort === 'lastseen') {
149 $sql .= " ORDER BY ul.timeaccess $direction, u.lastname $direction, u.firstname $direction";
150 }
151 $this->users[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage);
152 }
153 return $this->users[$key];
154 }
155
156 /**
157 * Gets an array of the users that can be enrolled in this course.
158 *
159 * @global moodle_database $DB
160 * @param int $enrolid
161 * @param string $search
162 * @param bool $searchanywhere
163 * @param int $page Defaults to 0
164 * @param int $perpage Defaults to 25
165 * @return array Array(totalusers => int, users => array)
166 */
167 public function get_potential_users($enrolid, $search='', $searchanywhere=false, $page=0, $perpage=25) {
168 global $DB;
169
170 // Add some additional sensible conditions
171 $tests = array("u.username <> 'guest'", 'u.deleted = 0', 'u.confirmed = 1');
172 $params = array();
173 if (!empty($search)) {
174 $conditions = array('u.firstname','u.lastname');
175 $ilike = ' ' . $DB->sql_ilike();
176 if ($searchanywhere) {
177 $searchparam = '%' . $search . '%';
178 } else {
179 $searchparam = $search . '%';
180 }
181 $i = 0;
182 foreach ($conditions as &$condition) {
183 $condition .= "$ilike :con{$i}00";
184 $params["con{$i}00"] = $searchparam;
185 $i++;
186 }
187 $tests[] = '(' . implode(' OR ', $conditions) . ')';
188 }
189 $wherecondition = implode(' AND ', $tests);
190
191 $ufields = user_picture::fields('u');
192
193 $fields = 'SELECT u.id, u.firstname, u.lastname, u.username, u.email, u.lastaccess, u.picture, u.imagealt, '.$ufields;
194 $countfields = 'SELECT COUNT(1)';
195 $sql = " FROM {user} u
196 WHERE $wherecondition
197 AND u.id NOT IN (SELECT ue.userid
198 FROM {user_enrolments} ue
199 JOIN {enrol} e ON (e.id = ue.enrolid AND e.id = :enrolid))";
200 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
201 $params['enrolid'] = $enrolid;
202 $totalusers = $DB->count_records_sql($countfields . $sql, $params);
203 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
204 return array('totalusers'=>$totalusers, 'users'=>$availableusers);
205 }
206
207 /**
208 * Gets an array containing some SQL to user for when selecting, params for
209 * that SQL, and the filter that was used in constructing the sql.
210 *
211 * @global moodle_database $DB
212 * @return string
213 */
214 protected function get_instance_sql() {
215 global $DB;
216 if ($this->_instancessql === null) {
217 $instances = $this->get_enrolment_instances();
218 $filter = $this->get_enrolment_filter();
219 if ($filter && array_key_exists($filter, $instances)) {
220 $sql = " = :ifilter";
221 $params = array('ifilter'=>$filter);
222 } else {
223 $filter = 0;
224 if ($instances) {
225 list($sql, $params) = $DB->get_in_or_equal(array_keys($this->get_enrolment_instances()), SQL_PARAMS_NAMED);
226 } else {
227 // no enabled instances, oops, we should probably say something
228 $sql = "= :never";
229 $params = array('never'=>-1);
230 }
231 }
232 $this->instancefilter = $filter;
233 $this->_instancessql = array($sql, $params, $filter);
234 }
235 return $this->_instancessql;
236 }
237
238 /**
239 * Returns all of the enrolment instances for this course.
240 *
241 * @return array
242 */
243 public function get_enrolment_instances() {
244 if ($this->_instances === null) {
245 $this->_instances = enrol_get_instances($this->course->id, true);
246 }
247 return $this->_instances;
248 }
249
250 /**
251 * Returns the names for all of the enrolment instances for this course.
252 *
253 * @return array
254 */
255 public function get_enrolment_instance_names() {
256 if ($this->_inames === null) {
257 $instances = $this->get_enrolment_instances();
258 $plugins = $this->get_enrolment_plugins();
259 foreach ($instances as $key=>$instance) {
260 if (!isset($plugins[$instance->enrol])) {
261 // weird, some broken stuff in plugin
262 unset($instances[$key]);
263 continue;
264 }
265 $this->_inames[$key] = $plugins[$instance->enrol]->get_instance_name($instance);
266 }
267 }
268 return $this->_inames;
269 }
270
271 /**
272 * Gets all of the enrolment plugins that are active for this course.
273 *
274 * @return array
275 */
276 public function get_enrolment_plugins() {
277 if ($this->_plugins === null) {
278 $this->_plugins = enrol_get_plugins(true);
279 }
280 return $this->_plugins;
281 }
282
283 /**
284 * Gets all of the roles this course can contain.
285 *
286 * @return array
287 */
288 public function get_all_roles() {
289 if ($this->_roles === null) {
290 $this->_roles = role_fix_names(get_all_roles(), $this->context);
291 }
292 return $this->_roles;
293 }
294
295 /**
296 * Gets all of the assignable roles for this course.
297 *
298 * @return array
299 */
300 public function get_assignable_roles() {
301 if ($this->_assignableroles === null) {
302 $this->_assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false); // verifies unassign access control too
303 }
304 return $this->_assignableroles;
305 }
306
307 /**
308 * Gets all of the groups for this course.
309 *
310 * @return array
311 */
312 public function get_all_groups() {
313 if ($this->_groups === null) {
314 $this->_groups = groups_get_all_groups($this->course->id);
315 foreach ($this->_groups as $gid=>$group) {
316 $this->_groups[$gid]->name = format_string($group->name);
317 }
318 }
319 return $this->_groups;
320 }
321
322 /**
323 * Unenroles a user from the course given the users ue entry
324 *
325 * @global moodle_database $DB
326 * @param stdClass $ue
327 * @return bool
328 */
329 public function unenrol_user($ue) {
330 global $DB;
331
332 $instances = $this->get_enrolment_instances();
333 $plugins = $this->get_enrolment_plugins();
334
335 $user = $DB->get_record('user', array('id'=>$ue->userid), '*', MUST_EXIST);
336
337 if (!isset($instances[$ue->enrolid])) {
338 return false;
339 }
340 $instance = $instances[$ue->enrolid];
341 $plugin = $plugins[$instance->enrol];
342 if (!$plugin->allow_unenrol($instance) || !has_capability("enrol/$instance->enrol:unenrol", $this->context)) {
343 return false;
344 }
345 $plugin->unenrol_user($instance, $ue->userid);
346 return true;
347 }
348
349 /**
350 * Removes an assigned role from a user.
351 *
352 * @global moodle_database $DB
353 * @param int $userid
354 * @param int $roleid
355 * @return bool
356 */
357 public function unassign_role_from_user($userid, $roleid) {
358 global $DB;
359 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
360 try {
361 role_unassign($roleid, $user->id, $this->context->id, '', NULL);
362 } catch (Exception $e) {
363 return false;
364 }
365 return true;
366 }
367
368 /**
369 * Assigns a role to a user.
370 *
371 * @param int $roleid
372 * @param int $userid
373 * @return int|false
374 */
375 public function assign_role_to_user($roleid, $userid) {
376 require_capability('moodle/role:assign', $this->context);
377 return role_assign($roleid, $userid, $this->context->id, '', NULL);
378 }
379
380 /**
381 * Adds a user to a group
382 *
383 * @param stdClass $user
384 * @param int $groupid
385 * @return bool
386 */
387 public function add_user_to_group($user, $groupid) {
388 require_capability('moodle/course:managegroups', $this->context);
389 $group = $this->get_group($groupid);
390 if (!$group) {
391 return false;
392 }
393 return groups_add_member($group->id, $user->id);
394 }
395
396 /**
397 * Removes a user from a group
398 *
399 * @global moodle_database $DB
400 * @param StdClass $user
401 * @param int $groupid
402 * @return bool
403 */
404 public function remove_user_from_group($user, $groupid) {
405 global $DB;
406 require_capability('moodle/course:managegroups', $this->context);
407 $group = $this->get_group($groupid);
408 if (!$group) {
409 return false;
410 }
411 return groups_remove_member($group, $user);
412 }
413
414 /**
415 * Gets the requested group
416 *
417 * @param int $groupid
418 * @return stdClass|int
419 */
420 public function get_group($groupid) {
421 $groups = $this->get_all_groups();
422 if (!array_key_exists($groupid, $groups)) {
423 return false;
424 }
425 return $groups[$groupid];
426 }
427
428 /**
429 * Edits an enrolment
430 *
431 * @param stdClass $userenrolment
432 * @param stdClass $data
433 * @return bool
434 */
435 public function edit_enrolment($userenrolment, $data) {
436 $instances = $this->get_enrolment_instances();
437 if (!array_key_exists($userenrolment->enrolid, $instances)) {
438 return false;
439 }
440 $instance = $instances[$userenrolment->enrolid];
441
442 $plugins = $this->get_enrolment_plugins();
443 $plugin = $plugins[$instance->enrol];
444 if (!$plugin->allow_unenrol($instance) || !has_capability("enrol/$instance->enrol:unenrol", $this->context)) {
445 return false;
446 }
447
448 if (!isset($data->status)) {
449 $data->status = $userenrolment->status;
450 }
451
452 $plugin->update_user_enrol($instance, $userenrolment->userid, $data->status, $data->timestart, $data->timeend);
453 return true;
454 }
455
456 /**
457 * Returns the current enrolment filter that is being applied by this class
458 * @return string
459 */
460 public function get_enrolment_filter() {
461 return $this->instancefilter;
462 }
463
464 /**
465 * Gets the roles assigned to this user that are applicable for this course.
466 *
467 * @param int $userid
468 * @return array
469 */
470 public function get_user_roles($userid) {
471 $roles = array();
472 $ras = get_user_roles($this->context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC');
473 foreach ($ras as $ra) {
474 if ($ra->contextid != $this->context->id) {
475 if (!array_key_exists($ra->roleid, $roles)) {
476 $roles[$ra->roleid] = null;
477 }
478 // higher ras, course always takes precedence
479 continue;
480 }
481 if (array_key_exists($ra->roleid, $roles) && $roles[$ra->roleid] === false) {
482 continue;
483 }
484 $roles[$ra->roleid] = ($ra->itemid == 0 and $ra->component === '');
485 }
486 return $roles;
487 }
488
489 /**
490 * Gets the enrolments this user has in the course
491 *
492 * @global moodle_database $DB
493 * @param int $userid
494 * @return array
495 */
496 public function get_user_enrolments($userid) {
497 global $DB;
498 list($instancessql, $params, $filter) = $this->get_instance_sql();
499 $params['userid'] = $userid;
500 $userenrolments = $DB->get_records_select('user_enrolments', "enrolid $instancessql AND userid = :userid", $params);
501 $instances = $this->get_enrolment_instances();
502 $plugins = $this->get_enrolment_plugins();
503 $inames = $this->get_enrolment_instance_names();
504 foreach ($userenrolments as &$ue) {
505 $ue->enrolmentinstance = $instances[$ue->enrolid];
506 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol];
507 $ue->enrolmentinstancename = $inames[$ue->enrolmentinstance->id];
508 }
509 return $userenrolments;
510 }
511
512 /**
513 * Gets the groups this user belongs to
514 *
515 * @param int $userid
516 * @return array
517 */
518 public function get_user_groups($userid) {
519 return groups_get_all_groups($this->course->id, $userid, 0, 'g.id');
520 }
521
522 /**
523 * Retursn an array of params that would go into the URL to return to this
524 * exact page.
525 *
526 * @return array
527 */
528 public function get_url_params() {
529 $args = array(
530 'id' => $this->course->id
531 );
532 if (!empty($this->instancefilter)) {
533 $args['ifilter'] = $this->instancefilter;
534 }
535 return $args;
536 }
537
538 /**
539 * Returns the course this object is managing enrolments for
540 *
541 * @return stdClass
542 */
543 public function get_course() {
544 return $this->course;
545 }
546
547 /**
548 * Returns the course context
549 *
550 * @return stdClass
551 */
552 public function get_context() {
553 return $this->context;
554 }
555
2ec702c9
SH
556 /**
557 * Gets all the cohorts the user is able to view.
558 *
559 * @global moodle_database $DB
560 * @return array
561 */
562 public function get_cohorts() {
563 global $DB;
564 $context = $this->get_context();
565 $cohorts = array();
566 $instances = $this->get_enrolment_instances();
567 $enrolled = array();
568 foreach ($instances as $instance) {
569 if ($instance->enrol == 'cohort') {
570 $enrolled[] = $instance->customint1;
571 }
572 }
573 list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
574 $sql = "SELECT id, name, contextid
575 FROM {cohort}
576 WHERE contextid $sqlparents
577 ORDER BY name ASC";
578 $rs = $DB->get_recordset_sql($sql, $params);
579 foreach ($rs as $c) {
580 $context = get_context_instance_by_id($c->contextid);
581 if (!has_capability('moodle/cohort:view', $context)) {
582 continue;
583 }
584 $cohorts[$c->id] = array(
585 'cohortid'=>$c->id,
586 'name'=>format_string($c->name),
587 'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
588 'enrolled'=>in_array($c->id, $enrolled)
589 );
590 }
591 $rs->close();
592 return $cohorts;
593 }
594
595 /**
596 * Enrols a cohort in a course.
597 *
598 * Essentially this just adds a cohort enrolment plugin instance to the course
599 *
600 * @param int $cohortid
601 * @param int $roleid
602 * @return bool
603 */
604 public function enrol_cohort($cohortid, $roleid) {
605 global $CFG;
606 require_capability('moodle/course:enrolconfig', $this->get_context());
607 require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
608 $roles = $this->get_assignable_roles();
609 $cohorts = $this->get_cohorts();
610 if (!array_key_exists($cohortid, $cohorts) || !array_key_exists($roleid, $roles)) {
611 return false;
612 }
613 $enrol = enrol_get_plugin('cohort');
614 $enrol->add_instance($this->course, array('customint1'=>$cohortid, 'roleid'=>$roleid));
615 enrol_cohort_sync($this->course->id);
616 return true;
617 }
618
619 /**
620 * Enrols all of the users in a cohort within this course.
621 *
622 * Note this is VERY different from creating an enrolment instance for a cohort.
623 *
624 * @global moodle_database $DB
625 * @param int $cohortid
626 * @param int $roleid
627 * @return bool
628 */
629 public function enrol_cohort_users($cohortid, $roleid) {
630 global $DB;
631 require_capability('moodle/course:enrolconfig', $this->get_context());
91b99e80 632 require_capability('enrol/manual:enrol', $this->get_context());
2ec702c9
SH
633 $instances = $this->get_enrolment_instances();
634 $instance = false;
635 foreach ($instances as $i) {
636 if ($i->enrol == 'manual') {
637 $instance = $i;
638 break;
639 }
640 }
641 if (!$instance) {
642 return false;
643 }
644 $plugin = enrol_get_plugin('manual');
645
91b99e80 646 $sql = "SELECT com.userid
2ec702c9
SH
647 FROM {cohort_members} com
648 LEFT JOIN (
649 SELECT *
650 FROM {user_enrolments} ue
651 WHERE ue.enrolid = :enrolid
652 ) ue ON ue.userid=com.userid
653 WHERE com.cohortid = :cohortid AND ue.id IS NULL";
654 $params = array('cohortid'=>$cohortid, 'enrolid'=>$instance->id);
655 $rs = $DB->get_recordset_sql($sql, $params);
656 $count = 0;
657 foreach ($rs as $user) {
658 $count++;
659 $plugin->enrol_user($instance, $user->userid, $roleid);
660 }
661 $rs->close();
662 return $count;
663 }
664
a70eb30f
SH
665 /**
666 * Gets an array of users for display, this includes minimal user information
667 * as well as minimal information on the users roles, groups, and enrolments.
668 *
669 * @param core_enrol_renderer $renderer
670 * @param moodle_url $pageurl
671 * @param int $sort
672 * @param string $direction ASC or DESC
673 * @param int $page
674 * @param int $perpage
675 * @return array
676 */
677 public function get_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage) {
678 $users = $this->get_users($sort, $direction, $page, $perpage);
679
680 $now = time();
681 $strnever = get_string('never');
682 $straddgroup = get_string('addgroup', 'group');
683 $strunenrol = get_string('unenrol', 'enrol');
684 $stredit = get_string('edit');
685
686 $iconedit = $renderer->pix_url('t/edit');
687 $iconenroladd = $renderer->pix_url('t/enroladd');
688 $iconenrolremove = $renderer->pix_url('t/delete');
689
690 $allroles = $this->get_all_roles();
691 $assignable = $this->get_assignable_roles();
692 $allgroups = $this->get_all_groups();
693 $courseid = $this->get_course()->id;
694 $context = $this->get_context();
695 $canmanagegroups = has_capability('moodle/course:managegroups', $context);
696
697 $url = new moodle_url($pageurl, $this->get_url_params());
698
699 $userdetails = array();
700 foreach ($users as $user) {
701 $details = array(
702 'userid' => $user->id,
703 'courseid' => $courseid,
704 'picture' => new user_picture($user),
705 'firstname' => fullname($user, true),
706 'email' => $user->email,
707 'lastseen' => $strnever,
708 'roles' => array(),
709 'groups' => array(),
710 'enrolments' => array()
711 );
712
713 if ($user->lastseen) {
714 $details['lastseen'] = format_time($user->lastaccess);
715 }
716
717 // Roles
718 foreach ($this->get_user_roles($user->id) as $rid=>$rassignable) {
719 $details['roles'][$rid] = array('text'=>$allroles[$rid]->localname, 'unchangeable'=>(!$rassignable || !isset($assignable[$rid])));
720 }
721
722 // Users
723 $usergroups = $this->get_user_groups($user->id);
724 foreach($usergroups as $gid=>$unused) {
725 $details['groups'][$gid] = $allgroups[$gid]->name;
726 }
727
728 // Enrolments
729 foreach ($this->get_user_enrolments($user->id) as $ue) {
730 if ($ue->timestart and $ue->timeend) {
731 $period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend)));
732 $periodoutside = ($ue->timestart && $ue->timeend && $now < $ue->timestart && $now > $ue->timeend);
733 } else if ($ue->timestart) {
734 $period = get_string('periodstart', 'enrol', userdate($ue->timestart));
735 $periodoutside = ($ue->timestart && $now < $ue->timestart);
736 } else if ($ue->timeend) {
737 $period = get_string('periodend', 'enrol', userdate($ue->timeend));
738 $periodoutside = ($ue->timeend && $now > $ue->timeend);
739 } else {
740 $period = '';
741 $periodoutside = false;
742 }
743 $details['enrolments'][$ue->id] = array(
744 'text' => $ue->enrolmentinstancename,
745 'period' => $period,
746 'dimmed' => ($periodoutside || $ue->status != ENROL_USER_ACTIVE),
747 'canunenrol' => ($ue->enrolmentplugin->allow_unenrol($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":unenrol", $context)),
748 'canmanage' => ($ue->enrolmentplugin->allow_manage($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":manage", $context))
749 );
750 }
751 $userdetails[$user->id] = $details;
752 }
753 return $userdetails;
754
755 if (1==2){
756 // get list of roles
757 $roles = $this->get_user_roles($user->id);
758 foreach ($roles as $rid=>$unassignable) {
759 if ($unassignable && isset($assignable[$rid])) {
760 $icon = html_writer::empty_tag('img', array('alt'=>get_string('unassignarole', 'role', $allroles[$rid]->localname), 'src'=>$iconenrolremove));
761 $url = new moodle_url($url, array('action'=>'unassign', 'role'=>$rid, 'user'=>$user->id));
762 $roles[$rid] = html_writer::tag('div', $allroles[$rid]->localname . html_writer::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$rid)), array('class'=>'role role_'.$rid));
763 } else {
764 $roles[$rid] = html_writer::tag('div', $allroles[$rid]->localname, array('class'=>'role unchangeable', 'rel'=>$rid));
765 }
766 }
767 $addrole = '';
768 if ($assignable) {
769 foreach ($assignable as $rid=>$unused) {
770 if (!isset($roles[$rid])) {
771 //candidate for role assignment
772 $url = new moodle_url($url, array('action'=>'assign', 'user'=>$user->id));
773 $icon = html_writer::empty_tag('img', array('alt'=>get_string('assignroles', 'role', $allroles[$rid]->localname), 'src'=>$iconenroladd));
774 $addrole .= html_writer::link($url, $icon, array('class'=>'assignrolelink'));
775 break;
776 }
777 }
778 }
779 $roles = html_writer::tag('div', implode('', $roles), array('class'=>'roles'));
780 if ($addrole) {
781 $roles = html_writer::tag('div', $addrole, array('class'=>'addrole')).$roles;
782 }
783
784 // Get list of groups
785 $usergroups = $this->get_user_groups($user->id);
786 $groups = array();
787 foreach($usergroups as $gid=>$unused) {
788 $group = $allgroups[$gid];
789 if ($canmanagegroups) {
790 $icon = html_writer::empty_tag('img', array('alt'=>get_string('removefromgroup', 'group', $group->name), 'src'=>$iconenrolremove));
791 $url = new moodle_url($url, array('action'=>'removemember', 'group'=>$gid, 'user'=>$user->id));
792 $groups[] = $group->name . html_writer::link($url, $icon);
793 } else {
794 $groups[] = $group->name;
795 }
796 }
797 $groups = implode(', ', $groups);
798 if ($canmanagegroups and (count($usergroups) < count($allgroups))) {
799 $icon = html_writer::empty_tag('img', array('alt'=>$straddgroup, 'src'=>$iconenroladd));
800 $url = new moodle_url($url, array('action'=>'addmember', 'user'=>$user->id));
801 $groups .= '<div>'.html_writer::link($url, $icon).'</div>';
802 }
803
804
805 // get list of enrol instances
806 $ues = $this->get_user_enrolments($user->id);
807 $edits = array();
808 foreach ($ues as $ue) {
809 $edit = $ue->enrolmentinstancename;
810
811 $dimmed = false;
812 if ($ue->timestart and $ue->timeend) {
813 $edit .= '&nbsp;('.get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend))).')';
814 $dimmed = ($now < $ue->timestart and $now > $ue->timeend);
815 } else if ($ue->timestart) {
816 $edit .= '&nbsp;('.get_string('periodstart', 'enrol', userdate($ue->timestart)).')';
817 $dimmed = ($now < $ue->timestart);
818 } else if ($ue->timeend) {
819 $edit .= '&nbsp;('.get_string('periodend', 'enrol', userdate($ue->timeend)).')';
820 $dimmed = ($now > $ue->timeend);
821 }
822
823 if ($dimmed or $ue->status != ENROL_USER_ACTIVE) {
824 $edit = html_writer::tag('span', $edit, array('class'=>'dimmed_text'));
825 }
826
827 if ($ue->enrolmentplugin->allow_unenrol($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":unenrol", $context)) {
828 $icon = html_writer::empty_tag('img', array('alt'=>$strunenrol, 'src'=>$iconenrolremove));
829 $url = new moodle_url($url, array('action'=>'unenrol', 'ue'=>$ue->id));
830 $edit .= html_writer::link($url, $icon, array('class'=>'unenrollink', 'rel'=>$ue->id));
831 }
832
833 if ($ue->enrolmentplugin->allow_manage($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":manage", $context)) {
834 $icon = html_writer::empty_tag('img', array('alt'=>$stredit, 'src'=>$iconedit));
835 $url = new moodle_url($url, array('action'=>'edit', 'ue'=>$ue->id));
836 $edit .= html_writer::link($url, $icon, array('class'=>'editenrollink', 'rel'=>$ue->id));
837 }
838
839 $edits[] = html_writer::tag('div', $edit, array('class'=>'enrolment'));
840 }
841 $edits = implode('', $edits);
842
843
844 $userdetails[$user->id] = array(
845 'picture' => $renderer->user_picture($user, array('courseid'=>$courseid)),
846 'firstname' => html_writer::link(new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$courseid)), fullname($user, true)),
847 'email' => $user->email,
848 'lastseen' => $strlastaccess,
849 'role' => $roles,
850 'group' => $groups,
851 'enrol' => $edits
852 );
853 }
854 return $userdetails;
855 }
856}