Skip to content

Commit

Permalink
MDL-59369 user: Add status column to the participants table
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Jul 26, 2017
1 parent afa752e commit fb79296
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions lang/en/enrol.php
Expand Up @@ -104,6 +104,7 @@
$string['notenrolledusers'] = 'Other users';
$string['otheruserdesc'] = 'The following users are not enrolled in this course but do have roles, inherited or assigned within it.';
$string['participationactive'] = 'Active';
$string['participationnotcurrent'] = 'Not current';
$string['participationstatus'] = 'Status';
$string['participationsuspended'] = 'Suspended';
$string['periodend'] = 'until {$a}';
Expand Down
69 changes: 67 additions & 2 deletions user/classes/participants_table.php
Expand Up @@ -24,6 +24,10 @@

namespace core_user;

use context;
use DateTime;
use html_writer;

defined('MOODLE_INTERNAL') || die;

global $CFG;
Expand Down Expand Up @@ -86,12 +90,12 @@ class participants_table extends \table_sql {
protected $extrafields;

/**
* @var \stdClass The course details.
* @var \stdClass $course The course details.
*/
protected $course;

/**
* @var \context The course context.
* @var context $context The course context.
*/
protected $context;

Expand Down Expand Up @@ -125,6 +129,7 @@ public function __construct($courseid, $currentgroup, $accesssince, $roleid, $se
// Get the context.
$this->course = get_course($courseid);
$context = \context_course::instance($courseid, MUST_EXIST);
$this->context = $context;

// Define the headers and columns.
$headers = [];
Expand Down Expand Up @@ -168,6 +173,13 @@ public function __construct($courseid, $currentgroup, $accesssince, $roleid, $se
$columns[] = 'lastaccess';
}

$canreviewenrol = has_capability('moodle/course:enrolreview', $context);
if ($canreviewenrol) {
$columns[] = 'status';
$headers[] = get_string('participationstatus', 'enrol');
$this->no_sorting('status');
};

$this->define_columns($columns);
$this->define_headers($headers);

Expand Down Expand Up @@ -290,6 +302,59 @@ public function col_lastaccess($data) {
return get_string('never');
}

/**
* Generate the status column.
*
* @param stdClass $data The data object.
* @return string
*/
public function col_status($data) {
global $CFG, $OUTPUT, $PAGE;

$enrolstatusoutput = '';
$canreviewenrol = has_capability('moodle/course:enrolreview', $this->context);
if ($canreviewenrol) {
$fullname = fullname($data);
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new \course_enrolment_manager($PAGE, $this->course);
$userenrolments = $manager->get_user_enrolments($data->id);
foreach ($userenrolments as $ue) {
$enrolactions = $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue);
$timestart = $ue->timestart;
$timeend = $ue->timeend;
$status = '';
$dimmed = '';
switch ($ue->status) {
case ENROL_USER_ACTIVE:
$currentdate = new DateTime();
$now = $currentdate->getTimestamp();
if ($timestart <= $now && ($timeend == 0 || $timeend >= $now)) {
$status = get_string('participationactive', 'enrol');
} else {
$status = get_string('participationnotcurrent', 'enrol');
$dimmed = 'dimmed_text';
}
break;
case ENROL_USER_SUSPENDED:
$status = get_string('participationsuspended', 'enrol');
$dimmed = 'dimmed_text';
break;
}
$statusout = html_writer::span($status, $dimmed, ['title' => "{$ue->enrolmentinstancename}: {$status}"]);
$enrolactionsout = '';
foreach ($enrolactions as $action) {
$icon = $OUTPUT->render($action->get_icon());
$attributes = $action->get_attributes();
$attributes['data-fullname'] = $fullname;
$attributes['data-coursename'] = $this->course->fullname;
$enrolactionsout .= html_writer::link($action->get_url(), $icon, $attributes);
}
$enrolstatusoutput .= html_writer::div($statusout . $enrolactionsout);
}
}
return $enrolstatusoutput;
}

/**
* This function is used for the extra user fields.
*
Expand Down
4 changes: 3 additions & 1 deletion user/lib.php
Expand Up @@ -1234,7 +1234,9 @@ function user_get_participants_sql($courseid, $groupid = 0, $accesssince = 0, $r

$isfrontpage = ($courseid == SITEID);

list($esql, $params) = get_enrolled_sql($context, null, $groupid, true);
// Show active users only if the user doesn't have the 'moodle/course:enrolreview' capability.
$onlyactive = !has_capability('moodle/course:enrolreview', $context);
list($esql, $params) = get_enrolled_sql($context, null, $groupid, $onlyactive);

$joins = array('FROM {user} u');
$wheres = array();
Expand Down

0 comments on commit fb79296

Please sign in to comment.