MDL-23187 Generic ratings and comments permissions now appear on course pages, AND...
[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) {
a942f757 330 global $DB;
a3c71984
SH
331 list ($instance, $plugin) = $this->get_user_enrolment_components($ue);
332 if ($instance && $plugin && $plugin->allow_unenrol($instance) && has_capability("enrol/$instance->enrol:unenrol", $this->context)) {
333 $plugin->unenrol_user($instance, $ue->userid);
334 return true;
335 }
336 return false;
337 }
a70eb30f 338
a3c71984
SH
339 /**
340 * Given a user enrolment record this method returns the plugin and enrolment
341 * instance that relate to it.
342 *
343 * @param stdClass|int $userenrolment
344 * @return array array($instance, $plugin)
345 */
346 public function get_user_enrolment_components($userenrolment) {
347 global $DB;
a942f757 348 if (is_numeric($userenrolment)) {
a3c71984
SH
349 $userenrolment = $DB->get_record('user_enrolments', array('id'=>(int)$userenrolment));
350 }
a70eb30f
SH
351 $instances = $this->get_enrolment_instances();
352 $plugins = $this->get_enrolment_plugins();
a3c71984
SH
353 if (!$userenrolment || !isset($instances[$userenrolment->enrolid])) {
354 return array(false, false);
a70eb30f 355 }
a3c71984 356 $instance = $instances[$userenrolment->enrolid];
a70eb30f 357 $plugin = $plugins[$instance->enrol];
a3c71984 358 return array($instance, $plugin);
a70eb30f
SH
359 }
360
361 /**
362 * Removes an assigned role from a user.
363 *
364 * @global moodle_database $DB
365 * @param int $userid
366 * @param int $roleid
367 * @return bool
368 */
369 public function unassign_role_from_user($userid, $roleid) {
370 global $DB;
a3c71984 371 require_capability('moodle/role:assign', $this->context);
a70eb30f
SH
372 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
373 try {
374 role_unassign($roleid, $user->id, $this->context->id, '', NULL);
375 } catch (Exception $e) {
376 return false;
377 }
378 return true;
379 }
380
381 /**
382 * Assigns a role to a user.
383 *
384 * @param int $roleid
385 * @param int $userid
386 * @return int|false
387 */
388 public function assign_role_to_user($roleid, $userid) {
389 require_capability('moodle/role:assign', $this->context);
823a807e
SH
390 if (!array_key_exists($roleid, $this->get_assignable_roles())) {
391 return false;
392 }
a70eb30f
SH
393 return role_assign($roleid, $userid, $this->context->id, '', NULL);
394 }
395
396 /**
397 * Adds a user to a group
398 *
399 * @param stdClass $user
400 * @param int $groupid
401 * @return bool
402 */
403 public function add_user_to_group($user, $groupid) {
404 require_capability('moodle/course:managegroups', $this->context);
405 $group = $this->get_group($groupid);
406 if (!$group) {
407 return false;
408 }
409 return groups_add_member($group->id, $user->id);
410 }
411
412 /**
413 * Removes a user from a group
414 *
415 * @global moodle_database $DB
416 * @param StdClass $user
417 * @param int $groupid
418 * @return bool
419 */
420 public function remove_user_from_group($user, $groupid) {
421 global $DB;
422 require_capability('moodle/course:managegroups', $this->context);
423 $group = $this->get_group($groupid);
424 if (!$group) {
425 return false;
426 }
427 return groups_remove_member($group, $user);
428 }
429
430 /**
431 * Gets the requested group
432 *
433 * @param int $groupid
434 * @return stdClass|int
435 */
436 public function get_group($groupid) {
437 $groups = $this->get_all_groups();
438 if (!array_key_exists($groupid, $groups)) {
439 return false;
440 }
441 return $groups[$groupid];
442 }
443
444 /**
445 * Edits an enrolment
446 *
447 * @param stdClass $userenrolment
448 * @param stdClass $data
449 * @return bool
450 */
451 public function edit_enrolment($userenrolment, $data) {
a3c71984
SH
452 list($instance, $plugin) = $this->get_user_enrolment_components($ue);
453 if ($instance && $plugin && $plugin->allow_manage($instance) && has_capability("enrol/$instance->enrol:manage", $this->context)) {
454 if (!isset($data->status)) {
455 $data->status = $userenrolment->status;
456 }
457 $plugin->update_user_enrol($instance, $userenrolment->userid, $data->status, $data->timestart, $data->timeend);
458 return true;
a70eb30f 459 }
a3c71984 460 return false;
a70eb30f
SH
461 }
462
463 /**
464 * Returns the current enrolment filter that is being applied by this class
465 * @return string
466 */
467 public function get_enrolment_filter() {
468 return $this->instancefilter;
469 }
470
471 /**
472 * Gets the roles assigned to this user that are applicable for this course.
473 *
474 * @param int $userid
475 * @return array
476 */
477 public function get_user_roles($userid) {
478 $roles = array();
479 $ras = get_user_roles($this->context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC');
480 foreach ($ras as $ra) {
481 if ($ra->contextid != $this->context->id) {
482 if (!array_key_exists($ra->roleid, $roles)) {
483 $roles[$ra->roleid] = null;
484 }
485 // higher ras, course always takes precedence
486 continue;
487 }
488 if (array_key_exists($ra->roleid, $roles) && $roles[$ra->roleid] === false) {
489 continue;
490 }
491 $roles[$ra->roleid] = ($ra->itemid == 0 and $ra->component === '');
492 }
493 return $roles;
494 }
495
496 /**
497 * Gets the enrolments this user has in the course
498 *
499 * @global moodle_database $DB
500 * @param int $userid
501 * @return array
502 */
503 public function get_user_enrolments($userid) {
504 global $DB;
505 list($instancessql, $params, $filter) = $this->get_instance_sql();
506 $params['userid'] = $userid;
507 $userenrolments = $DB->get_records_select('user_enrolments', "enrolid $instancessql AND userid = :userid", $params);
508 $instances = $this->get_enrolment_instances();
509 $plugins = $this->get_enrolment_plugins();
510 $inames = $this->get_enrolment_instance_names();
511 foreach ($userenrolments as &$ue) {
512 $ue->enrolmentinstance = $instances[$ue->enrolid];
513 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol];
514 $ue->enrolmentinstancename = $inames[$ue->enrolmentinstance->id];
515 }
516 return $userenrolments;
517 }
518
519 /**
520 * Gets the groups this user belongs to
521 *
522 * @param int $userid
523 * @return array
524 */
525 public function get_user_groups($userid) {
526 return groups_get_all_groups($this->course->id, $userid, 0, 'g.id');
527 }
528
529 /**
530 * Retursn an array of params that would go into the URL to return to this
531 * exact page.
532 *
533 * @return array
534 */
535 public function get_url_params() {
536 $args = array(
537 'id' => $this->course->id
538 );
539 if (!empty($this->instancefilter)) {
540 $args['ifilter'] = $this->instancefilter;
541 }
542 return $args;
543 }
544
545 /**
546 * Returns the course this object is managing enrolments for
547 *
548 * @return stdClass
549 */
550 public function get_course() {
551 return $this->course;
552 }
553
554 /**
555 * Returns the course context
556 *
557 * @return stdClass
558 */
559 public function get_context() {
560 return $this->context;
561 }
562
2ec702c9
SH
563 /**
564 * Gets all the cohorts the user is able to view.
565 *
566 * @global moodle_database $DB
567 * @return array
568 */
569 public function get_cohorts() {
570 global $DB;
571 $context = $this->get_context();
572 $cohorts = array();
573 $instances = $this->get_enrolment_instances();
574 $enrolled = array();
575 foreach ($instances as $instance) {
576 if ($instance->enrol == 'cohort') {
577 $enrolled[] = $instance->customint1;
578 }
579 }
580 list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
581 $sql = "SELECT id, name, contextid
582 FROM {cohort}
583 WHERE contextid $sqlparents
584 ORDER BY name ASC";
585 $rs = $DB->get_recordset_sql($sql, $params);
586 foreach ($rs as $c) {
587 $context = get_context_instance_by_id($c->contextid);
588 if (!has_capability('moodle/cohort:view', $context)) {
589 continue;
590 }
591 $cohorts[$c->id] = array(
592 'cohortid'=>$c->id,
593 'name'=>format_string($c->name),
594 'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
595 'enrolled'=>in_array($c->id, $enrolled)
596 );
597 }
598 $rs->close();
599 return $cohorts;
600 }
601
602 /**
603 * Enrols a cohort in a course.
604 *
605 * Essentially this just adds a cohort enrolment plugin instance to the course
606 *
607 * @param int $cohortid
608 * @param int $roleid
609 * @return bool
610 */
611 public function enrol_cohort($cohortid, $roleid) {
612 global $CFG;
613 require_capability('moodle/course:enrolconfig', $this->get_context());
614 require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
615 $roles = $this->get_assignable_roles();
616 $cohorts = $this->get_cohorts();
617 if (!array_key_exists($cohortid, $cohorts) || !array_key_exists($roleid, $roles)) {
618 return false;
619 }
620 $enrol = enrol_get_plugin('cohort');
621 $enrol->add_instance($this->course, array('customint1'=>$cohortid, 'roleid'=>$roleid));
622 enrol_cohort_sync($this->course->id);
623 return true;
624 }
625
626 /**
627 * Enrols all of the users in a cohort within this course.
628 *
629 * Note this is VERY different from creating an enrolment instance for a cohort.
630 *
631 * @global moodle_database $DB
632 * @param int $cohortid
633 * @param int $roleid
634 * @return bool
635 */
636 public function enrol_cohort_users($cohortid, $roleid) {
637 global $DB;
638 require_capability('moodle/course:enrolconfig', $this->get_context());
2ec702c9 639 $instance = false;
a3c71984 640 $instances = $this->get_enrolment_instances();
2ec702c9
SH
641 foreach ($instances as $i) {
642 if ($i->enrol == 'manual') {
643 $instance = $i;
644 break;
645 }
646 }
a3c71984
SH
647 $plugin = enrol_get_plugin('manual');
648 if (!$instance || !$plugin || !$plugin->allow_enrol($instance) || !has_capability('enrol/'.$plugin->get_name().':enrol', $this->get_context())) {
2ec702c9
SH
649 return false;
650 }
91b99e80 651 $sql = "SELECT com.userid
2ec702c9
SH
652 FROM {cohort_members} com
653 LEFT JOIN (
654 SELECT *
655 FROM {user_enrolments} ue
656 WHERE ue.enrolid = :enrolid
657 ) ue ON ue.userid=com.userid
658 WHERE com.cohortid = :cohortid AND ue.id IS NULL";
659 $params = array('cohortid'=>$cohortid, 'enrolid'=>$instance->id);
660 $rs = $DB->get_recordset_sql($sql, $params);
661 $count = 0;
662 foreach ($rs as $user) {
663 $count++;
664 $plugin->enrol_user($instance, $user->userid, $roleid);
665 }
666 $rs->close();
667 return $count;
668 }
669
a70eb30f
SH
670 /**
671 * Gets an array of users for display, this includes minimal user information
672 * as well as minimal information on the users roles, groups, and enrolments.
673 *
674 * @param core_enrol_renderer $renderer
675 * @param moodle_url $pageurl
676 * @param int $sort
677 * @param string $direction ASC or DESC
678 * @param int $page
679 * @param int $perpage
680 * @return array
681 */
682 public function get_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage) {
683 $users = $this->get_users($sort, $direction, $page, $perpage);
684
685 $now = time();
686 $strnever = get_string('never');
687 $straddgroup = get_string('addgroup', 'group');
688 $strunenrol = get_string('unenrol', 'enrol');
689 $stredit = get_string('edit');
690
691 $iconedit = $renderer->pix_url('t/edit');
692 $iconenroladd = $renderer->pix_url('t/enroladd');
693 $iconenrolremove = $renderer->pix_url('t/delete');
694
695 $allroles = $this->get_all_roles();
696 $assignable = $this->get_assignable_roles();
697 $allgroups = $this->get_all_groups();
698 $courseid = $this->get_course()->id;
699 $context = $this->get_context();
700 $canmanagegroups = has_capability('moodle/course:managegroups', $context);
701
702 $url = new moodle_url($pageurl, $this->get_url_params());
703
704 $userdetails = array();
705 foreach ($users as $user) {
706 $details = array(
707 'userid' => $user->id,
708 'courseid' => $courseid,
709 'picture' => new user_picture($user),
710 'firstname' => fullname($user, true),
711 'email' => $user->email,
712 'lastseen' => $strnever,
713 'roles' => array(),
714 'groups' => array(),
715 'enrolments' => array()
716 );
717
718 if ($user->lastseen) {
719 $details['lastseen'] = format_time($user->lastaccess);
720 }
721
722 // Roles
723 foreach ($this->get_user_roles($user->id) as $rid=>$rassignable) {
724 $details['roles'][$rid] = array('text'=>$allroles[$rid]->localname, 'unchangeable'=>(!$rassignable || !isset($assignable[$rid])));
725 }
726
727 // Users
728 $usergroups = $this->get_user_groups($user->id);
729 foreach($usergroups as $gid=>$unused) {
730 $details['groups'][$gid] = $allgroups[$gid]->name;
731 }
732
733 // Enrolments
734 foreach ($this->get_user_enrolments($user->id) as $ue) {
735 if ($ue->timestart and $ue->timeend) {
736 $period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend)));
737 $periodoutside = ($ue->timestart && $ue->timeend && $now < $ue->timestart && $now > $ue->timeend);
738 } else if ($ue->timestart) {
739 $period = get_string('periodstart', 'enrol', userdate($ue->timestart));
740 $periodoutside = ($ue->timestart && $now < $ue->timestart);
741 } else if ($ue->timeend) {
742 $period = get_string('periodend', 'enrol', userdate($ue->timeend));
743 $periodoutside = ($ue->timeend && $now > $ue->timeend);
744 } else {
745 $period = '';
746 $periodoutside = false;
747 }
748 $details['enrolments'][$ue->id] = array(
749 'text' => $ue->enrolmentinstancename,
750 'period' => $period,
751 'dimmed' => ($periodoutside || $ue->status != ENROL_USER_ACTIVE),
752 'canunenrol' => ($ue->enrolmentplugin->allow_unenrol($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":unenrol", $context)),
753 'canmanage' => ($ue->enrolmentplugin->allow_manage($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":manage", $context))
754 );
755 }
756 $userdetails[$user->id] = $details;
757 }
758 return $userdetails;
a70eb30f
SH
759 }
760}