MDL-59369 user: Add status column to the participants table
authorJun Pataleta <jun@moodle.com>
Thu, 13 Jul 2017 02:18:15 +0000 (10:18 +0800)
committerJun Pataleta <jun@moodle.com>
Wed, 26 Jul 2017 01:42:38 +0000 (09:42 +0800)
lang/en/enrol.php
user/classes/participants_table.php
user/lib.php

index f9c026d..5f6af83 100644 (file)
@@ -104,6 +104,7 @@ $string['notenrollable'] = 'You can not enrol yourself in this course.';
 $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}';
index 85f937a..0ee1dae 100644 (file)
 
 namespace core_user;
 
+use context;
+use DateTime;
+use html_writer;
+
 defined('MOODLE_INTERNAL') || die;
 
 global $CFG;
@@ -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;
 
@@ -125,6 +129,7 @@ class participants_table extends \table_sql {
         // Get the context.
         $this->course = get_course($courseid);
         $context = \context_course::instance($courseid, MUST_EXIST);
+        $this->context = $context;
 
         // Define the headers and columns.
         $headers = [];
@@ -168,6 +173,13 @@ class participants_table extends \table_sql {
             $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);
 
@@ -290,6 +302,59 @@ class participants_table extends \table_sql {
         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.
      *
index 9231d9c..1188d09 100644 (file)
@@ -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();