Skip to content

Commit

Permalink
MDL-59369 user: IR fixes
Browse files Browse the repository at this point in the history
* Moved status_field to core_user\output namespace.
* Assigned status flags for the template context in order to determine
  which label class to use for the given status.

Part of MDL-59290.
  • Loading branch information
junpataleta committed Jul 26, 2017
1 parent 4544451 commit 90ffcfa
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
Expand Up @@ -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();

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -65,25 +71,32 @@ 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.
*
* @param string $enrolinstancename The enrolment instance name.
* @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;
Expand All @@ -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);
}
Expand All @@ -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;
}
}
27 changes: 14 additions & 13 deletions user/classes/participants_table.php
Expand Up @@ -25,6 +25,7 @@
namespace core_user;

use context;
use core_user\output\status_field;
use DateTime;

defined('MOODLE_INTERNAL') || die;
Expand Down Expand Up @@ -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);
}
}
Expand Down
6 changes: 5 additions & 1 deletion user/templates/status_field.mustache
Expand Up @@ -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:
Expand All @@ -37,6 +40,7 @@
"coursename": "TC 1",
"enrolinstancename": "Manual enrolment",
"status": "Active",
"active": true,
"timestart": "1 January 2017",
"timeend": "31 January 2018",
"enrolactions": [
Expand All @@ -55,7 +59,7 @@
}}
<div data-fullname="{{fullname}}" data-coursename="{{coursename}}" data-enrolinstancename="{{enrolinstancename}}"
data-status="{{status}}" data-timestart="{{timestart}}" data-timeend="{{timeend}}">
<span class="label {{#statusclass}}label-{{statusclass}}{{/statusclass}}">{{status}}</span>
<span class="label {{#active}}label-success{{/active}}{{#suspended}}label-warning{{/suspended}}{{#notcurrent}}label-default{{/notcurrent}}">{{status}}</span>
<a data-action="showdetails" role="button" tabindex="0">
{{#pix}}docs, core, {{#str}}enroldetails, enrol{{/str}}{{/pix}}
</a>
Expand Down

0 comments on commit 90ffcfa

Please sign in to comment.