MDL-59369 user: IR fixes

* 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.
This commit is contained in:
Jun Pataleta 2017-07-25 14:20:39 +08:00
parent 4544451aff
commit 90ffcfa83a
3 changed files with 58 additions and 23 deletions

View File

@ -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 @@ use user_enrolment_action;
*/
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 @@ class status_field implements renderable, templatable {
$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 @@ class status_field implements renderable, templatable {
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;
}
}

View File

@ -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 @@ class participants_table extends \table_sql {
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);
}
}

View File

@ -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 @@
}}
<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>