diff --git a/user/classes/status_field.php b/user/classes/output/status_field.php similarity index 75% rename from user/classes/status_field.php rename to user/classes/output/status_field.php index 38bf432d968f3..18d36ba944f19 100644 --- a/user/classes/status_field.php +++ b/user/classes/output/status_field.php @@ -22,7 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -namespace core_user; +namespace core_user\output; defined('MOODLE_INTERNAL') || die(); @@ -41,6 +41,15 @@ */ class status_field implements renderable, templatable { + /** Active user enrolment status constant. */ + const STATUS_ACTIVE = 0; + + /** Suspended user enrolment status constant. */ + const STATUS_SUSPENDED = 1; + + /** Not current user enrolment status constant. */ + const STATUS_NOT_CURRENT = 2; + /** @var string $enrolinstancename The enrolment instance name. */ protected $enrolinstancename; @@ -53,9 +62,6 @@ class status_field implements renderable, templatable { /** @var string $status The user enrolment status. */ protected $status; - /** @var string $statusclass The CSS class to be used for rendering the status label. */ - protected $statusclass; - /** @var int $timestart The timestamp when the user's enrolment starts. */ protected $timestart; @@ -65,6 +71,15 @@ class status_field implements renderable, templatable { /** @var user_enrolment_action[] $enrolactions Array of enrol action objects for the given enrolment method. */ protected $enrolactions; + /** @var bool $statusactive Indicates whether a user enrolment status should be rendered as active. */ + protected $statusactive = false; + + /** @var bool $statusactive Indicates whether a user enrolment status should be rendered as suspended. */ + protected $statussuspended = false; + + /** @var bool $statusactive Indicates whether a user enrolment status should be rendered as not current. */ + protected $statusnotcurrent = false; + /** * status_field constructor. * @@ -72,18 +87,16 @@ class status_field implements renderable, templatable { * @param string $coursename The course's full name. * @param string $fullname The user's full name. * @param string $status The user enrolment status. - * @param string $statusclass The CSS class to be used for rendering the status label. * @param int|null $timestart The timestamp when the user's enrolment starts. * @param int|null $timeend The timestamp when the user's enrolment ends. * @param user_enrolment_action[] $enrolactions Array of enrol action objects for the given enrolment method. */ - public function __construct($enrolinstancename, $coursename, $fullname, $status, $statusclass = '', - $timestart = null, $timeend = null, $enrolactions = []) { + public function __construct($enrolinstancename, $coursename, $fullname, $status, $timestart = null, $timeend = null, + $enrolactions = []) { $this->enrolinstancename = $enrolinstancename; $this->coursename = $coursename; $this->fullname = $fullname; $this->status = $status; - $this->statusclass = $statusclass; $this->timestart = $timestart; $this->timeend = $timeend; $this->enrolactions = $enrolactions; @@ -104,7 +117,10 @@ public function export_for_template(renderer_base $output) { $data->coursename = $this->coursename; $data->fullname = $this->fullname; $data->status = $this->status; - $data->statusclass = $this->statusclass; + $data->active = $this->statusactive; + $data->suspended = $this->statussuspended; + $data->notcurrent = $this->statusnotcurrent; + if ($this->timestart) { $data->timestart = userdate($this->timestart); } @@ -130,4 +146,18 @@ public function export_for_template(renderer_base $output) { return $data; } + + /** + * Status setter. + * + * @param int $status The user enrolment status representing one of this class' STATUS_* constants. + * @return status_field This class' instance. Useful for chaining. + */ + public function set_status($status = self::STATUS_ACTIVE) { + $this->statusactive = $status == static::STATUS_ACTIVE; + $this->statussuspended = $status == static::STATUS_SUSPENDED; + $this->statusnotcurrent = $status == static::STATUS_NOT_CURRENT; + + return $this; + } } diff --git a/user/classes/participants_table.php b/user/classes/participants_table.php index 94256f76db87b..becb5e45c0ae5 100644 --- a/user/classes/participants_table.php +++ b/user/classes/participants_table.php @@ -25,6 +25,7 @@ namespace core_user; use context; +use core_user\output\status_field; use DateTime; defined('MOODLE_INTERNAL') || die; @@ -339,30 +340,30 @@ public function col_status($data) { foreach ($userenrolments as $ue) { $timestart = $ue->timestart; $timeend = $ue->timeend; - $status = ''; - $statusclass = ''; + $actions = $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue); + $instancename = $ue->enrolmentinstancename; + + // Default status field label and value. + $status = get_string('participationactive', 'enrol'); + $statusval = status_field::STATUS_ACTIVE; 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'); - $statusclass = 'success'; - } else { + // If user enrolment status has not yet started/already ended. + if ($timestart > $now || ($timeend > 0 && $timeend < $now)) { $status = get_string('participationnotcurrent', 'enrol'); - $statusclass = 'default'; + $statusval = status_field::STATUS_NOT_CURRENT; } break; case ENROL_USER_SUSPENDED: $status = get_string('participationsuspended', 'enrol'); - $statusclass = 'warning'; + $statusval = status_field::STATUS_SUSPENDED; break; } - $actions = $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue); - $instancename = $ue->enrolmentinstancename; - $statusfield = new status_field($instancename, $coursename, $fullname, $status, $statusclass, $timestart, $timeend, - $actions); - $statusfielddata = $statusfield->export_for_template($OUTPUT); + + $statusfield = new status_field($instancename, $coursename, $fullname, $status, $timestart, $timeend, $actions); + $statusfielddata = $statusfield->set_status($statusval)->export_for_template($OUTPUT); $enrolstatusoutput .= $OUTPUT->render_from_template('core_user/status_field', $statusfielddata); } } diff --git a/user/templates/status_field.mustache b/user/templates/status_field.mustache index 002b06e1ba8a3..57fcf8c91014d 100644 --- a/user/templates/status_field.mustache +++ b/user/templates/status_field.mustache @@ -24,6 +24,9 @@ * coursename string - The course name. * enrolinstancename string - The enrolment instance name. * status string - The enrolment status. + * active boolean - Flag to indicate whether the user has an active enrolment. + * suspended boolean - Flag to indicate whether the user is suspended. + * notcurrent boolean - Flag to indicate whether the user's enrolment is active, but not current. * timestart string - Optional. The enrolment time start. * timeend string - Optional. The enrolment time end. * enrolactions array - Optional. Array of enrol actions consisting of: @@ -37,6 +40,7 @@ "coursename": "TC 1", "enrolinstancename": "Manual enrolment", "status": "Active", + "active": true, "timestart": "1 January 2017", "timeend": "31 January 2018", "enrolactions": [ @@ -55,7 +59,7 @@ }}
- {{status}} + {{status}} {{#pix}}docs, core, {{#str}}enroldetails, enrol{{/str}}{{/pix}}