enrol MDL-22854 New ajaxified enrolment interface
[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
556 /**
557 * Gets an array of users for display, this includes minimal user information
558 * as well as minimal information on the users roles, groups, and enrolments.
559 *
560 * @param core_enrol_renderer $renderer
561 * @param moodle_url $pageurl
562 * @param int $sort
563 * @param string $direction ASC or DESC
564 * @param int $page
565 * @param int $perpage
566 * @return array
567 */
568 public function get_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage) {
569 $users = $this->get_users($sort, $direction, $page, $perpage);
570
571 $now = time();
572 $strnever = get_string('never');
573 $straddgroup = get_string('addgroup', 'group');
574 $strunenrol = get_string('unenrol', 'enrol');
575 $stredit = get_string('edit');
576
577 $iconedit = $renderer->pix_url('t/edit');
578 $iconenroladd = $renderer->pix_url('t/enroladd');
579 $iconenrolremove = $renderer->pix_url('t/delete');
580
581 $allroles = $this->get_all_roles();
582 $assignable = $this->get_assignable_roles();
583 $allgroups = $this->get_all_groups();
584 $courseid = $this->get_course()->id;
585 $context = $this->get_context();
586 $canmanagegroups = has_capability('moodle/course:managegroups', $context);
587
588 $url = new moodle_url($pageurl, $this->get_url_params());
589
590 $userdetails = array();
591 foreach ($users as $user) {
592 $details = array(
593 'userid' => $user->id,
594 'courseid' => $courseid,
595 'picture' => new user_picture($user),
596 'firstname' => fullname($user, true),
597 'email' => $user->email,
598 'lastseen' => $strnever,
599 'roles' => array(),
600 'groups' => array(),
601 'enrolments' => array()
602 );
603
604 if ($user->lastseen) {
605 $details['lastseen'] = format_time($user->lastaccess);
606 }
607
608 // Roles
609 foreach ($this->get_user_roles($user->id) as $rid=>$rassignable) {
610 $details['roles'][$rid] = array('text'=>$allroles[$rid]->localname, 'unchangeable'=>(!$rassignable || !isset($assignable[$rid])));
611 }
612
613 // Users
614 $usergroups = $this->get_user_groups($user->id);
615 foreach($usergroups as $gid=>$unused) {
616 $details['groups'][$gid] = $allgroups[$gid]->name;
617 }
618
619 // Enrolments
620 foreach ($this->get_user_enrolments($user->id) as $ue) {
621 if ($ue->timestart and $ue->timeend) {
622 $period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend)));
623 $periodoutside = ($ue->timestart && $ue->timeend && $now < $ue->timestart && $now > $ue->timeend);
624 } else if ($ue->timestart) {
625 $period = get_string('periodstart', 'enrol', userdate($ue->timestart));
626 $periodoutside = ($ue->timestart && $now < $ue->timestart);
627 } else if ($ue->timeend) {
628 $period = get_string('periodend', 'enrol', userdate($ue->timeend));
629 $periodoutside = ($ue->timeend && $now > $ue->timeend);
630 } else {
631 $period = '';
632 $periodoutside = false;
633 }
634 $details['enrolments'][$ue->id] = array(
635 'text' => $ue->enrolmentinstancename,
636 'period' => $period,
637 'dimmed' => ($periodoutside || $ue->status != ENROL_USER_ACTIVE),
638 'canunenrol' => ($ue->enrolmentplugin->allow_unenrol($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":unenrol", $context)),
639 'canmanage' => ($ue->enrolmentplugin->allow_manage($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":manage", $context))
640 );
641 }
642 $userdetails[$user->id] = $details;
643 }
644 return $userdetails;
645
646 if (1==2){
647 // get list of roles
648 $roles = $this->get_user_roles($user->id);
649 foreach ($roles as $rid=>$unassignable) {
650 if ($unassignable && isset($assignable[$rid])) {
651 $icon = html_writer::empty_tag('img', array('alt'=>get_string('unassignarole', 'role', $allroles[$rid]->localname), 'src'=>$iconenrolremove));
652 $url = new moodle_url($url, array('action'=>'unassign', 'role'=>$rid, 'user'=>$user->id));
653 $roles[$rid] = html_writer::tag('div', $allroles[$rid]->localname . html_writer::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$rid)), array('class'=>'role role_'.$rid));
654 } else {
655 $roles[$rid] = html_writer::tag('div', $allroles[$rid]->localname, array('class'=>'role unchangeable', 'rel'=>$rid));
656 }
657 }
658 $addrole = '';
659 if ($assignable) {
660 foreach ($assignable as $rid=>$unused) {
661 if (!isset($roles[$rid])) {
662 //candidate for role assignment
663 $url = new moodle_url($url, array('action'=>'assign', 'user'=>$user->id));
664 $icon = html_writer::empty_tag('img', array('alt'=>get_string('assignroles', 'role', $allroles[$rid]->localname), 'src'=>$iconenroladd));
665 $addrole .= html_writer::link($url, $icon, array('class'=>'assignrolelink'));
666 break;
667 }
668 }
669 }
670 $roles = html_writer::tag('div', implode('', $roles), array('class'=>'roles'));
671 if ($addrole) {
672 $roles = html_writer::tag('div', $addrole, array('class'=>'addrole')).$roles;
673 }
674
675 // Get list of groups
676 $usergroups = $this->get_user_groups($user->id);
677 $groups = array();
678 foreach($usergroups as $gid=>$unused) {
679 $group = $allgroups[$gid];
680 if ($canmanagegroups) {
681 $icon = html_writer::empty_tag('img', array('alt'=>get_string('removefromgroup', 'group', $group->name), 'src'=>$iconenrolremove));
682 $url = new moodle_url($url, array('action'=>'removemember', 'group'=>$gid, 'user'=>$user->id));
683 $groups[] = $group->name . html_writer::link($url, $icon);
684 } else {
685 $groups[] = $group->name;
686 }
687 }
688 $groups = implode(', ', $groups);
689 if ($canmanagegroups and (count($usergroups) < count($allgroups))) {
690 $icon = html_writer::empty_tag('img', array('alt'=>$straddgroup, 'src'=>$iconenroladd));
691 $url = new moodle_url($url, array('action'=>'addmember', 'user'=>$user->id));
692 $groups .= '<div>'.html_writer::link($url, $icon).'</div>';
693 }
694
695
696 // get list of enrol instances
697 $ues = $this->get_user_enrolments($user->id);
698 $edits = array();
699 foreach ($ues as $ue) {
700 $edit = $ue->enrolmentinstancename;
701
702 $dimmed = false;
703 if ($ue->timestart and $ue->timeend) {
704 $edit .= '&nbsp;('.get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend))).')';
705 $dimmed = ($now < $ue->timestart and $now > $ue->timeend);
706 } else if ($ue->timestart) {
707 $edit .= '&nbsp;('.get_string('periodstart', 'enrol', userdate($ue->timestart)).')';
708 $dimmed = ($now < $ue->timestart);
709 } else if ($ue->timeend) {
710 $edit .= '&nbsp;('.get_string('periodend', 'enrol', userdate($ue->timeend)).')';
711 $dimmed = ($now > $ue->timeend);
712 }
713
714 if ($dimmed or $ue->status != ENROL_USER_ACTIVE) {
715 $edit = html_writer::tag('span', $edit, array('class'=>'dimmed_text'));
716 }
717
718 if ($ue->enrolmentplugin->allow_unenrol($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":unenrol", $context)) {
719 $icon = html_writer::empty_tag('img', array('alt'=>$strunenrol, 'src'=>$iconenrolremove));
720 $url = new moodle_url($url, array('action'=>'unenrol', 'ue'=>$ue->id));
721 $edit .= html_writer::link($url, $icon, array('class'=>'unenrollink', 'rel'=>$ue->id));
722 }
723
724 if ($ue->enrolmentplugin->allow_manage($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":manage", $context)) {
725 $icon = html_writer::empty_tag('img', array('alt'=>$stredit, 'src'=>$iconedit));
726 $url = new moodle_url($url, array('action'=>'edit', 'ue'=>$ue->id));
727 $edit .= html_writer::link($url, $icon, array('class'=>'editenrollink', 'rel'=>$ue->id));
728 }
729
730 $edits[] = html_writer::tag('div', $edit, array('class'=>'enrolment'));
731 }
732 $edits = implode('', $edits);
733
734
735 $userdetails[$user->id] = array(
736 'picture' => $renderer->user_picture($user, array('courseid'=>$courseid)),
737 'firstname' => html_writer::link(new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$courseid)), fullname($user, true)),
738 'email' => $user->email,
739 'lastseen' => $strlastaccess,
740 'role' => $roles,
741 'group' => $groups,
742 'enrol' => $edits
743 );
744 }
745 return $userdetails;
746 }
747}