Skip to content

Commit

Permalink
MDL-23532 enrol - abstracted user enrolment action icons to the enrol…
Browse files Browse the repository at this point in the history
…ment plugin class
  • Loading branch information
Sam Hemelryk committed Apr 21, 2011
1 parent 410135a commit 291215f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 28 deletions.
95 changes: 88 additions & 7 deletions enrol/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,8 @@ public function get_other_users_for_display(core_enrol_renderer $renderer, moodl
* @param int $perpage
* @return array
*/
public function get_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage) {
public function get_users_for_display(course_enrolment_manager $manager, $sort, $direction, $page, $perpage) {
$pageurl = $manager->get_moodlepage()->url;
$users = $this->get_users($sort, $direction, $page, $perpage);

$now = time();
Expand All @@ -826,10 +827,6 @@ public function get_users_for_display(core_enrol_renderer $renderer, moodle_url
$strunenrol = get_string('unenrol', 'enrol');
$stredit = get_string('edit');

$iconedit = $renderer->pix_url('t/edit');
$iconenroladd = $renderer->pix_url('t/enroladd');
$iconenrolremove = $renderer->pix_url('t/delete');

$allroles = $this->get_all_roles();
$assignable = $this->get_assignable_roles();
$allgroups = $this->get_all_groups();
Expand Down Expand Up @@ -887,8 +884,7 @@ public function get_users_for_display(core_enrol_renderer $renderer, moodle_url
'text' => $ue->enrolmentinstancename,
'period' => $period,
'dimmed' => ($periodoutside || $ue->status != ENROL_USER_ACTIVE),
'canunenrol' => ($ue->enrolmentplugin->allow_unenrol($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":unenrol", $context)),
'canmanage' => ($ue->enrolmentplugin->allow_manage($ue->enrolmentinstance) && has_capability("enrol/".$ue->enrolmentinstance->enrol.":manage", $context))
'actions' => $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue)
);
}
$userdetails[$user->id] = $details;
Expand Down Expand Up @@ -1032,6 +1028,91 @@ public function initialise_js(moodle_page $page) {
}
}

/**
* User enrolment action
*
* This class is used to manage a renderable ue action such as editing an user enrolment or deleting
* a user enrolment.
*
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_enrolment_action implements renderable {

/**
* The icon to display for the action
* @var pix_icon
*/
protected $icon;

/**
* The title for the action
* @var string
*/
protected $title;

/**
* The URL to the action
* @var moodle_url
*/
protected $url;

/**
* An array of HTML attributes
* @var array
*/
protected $attributes = array();

/**
* Constructor
* @param pix_icon $icon
* @param string $title
* @param moodle_url $url
* @param array $attributes
*/
public function __construct(pix_icon $icon, $title, $url, array $attributes = null) {
$this->icon = $icon;
$this->title = $title;
$this->url = new moodle_url($url);
if (!empty($attributes)) {
$this->attributes = $attributes;
}
$this->attributes['title'] = $title;
}

/**
* Returns the icon for this action
* @return pix_icon
*/
public function get_icon() {
return $this->icon;
}

/**
* Returns the title for this action
* @return string
*/
public function get_title() {
return $this->title;
}

/**
* Returns the URL for this action
* @return moodle_url
*/
public function get_url() {
return $this->url;
}

/**
* Returns the attributes to use for this action
* @return array
*/
public function get_attributes() {
return $this->attributes;
}
}

class enrol_ajax_exception extends moodle_exception {
/**
* Constructor
Expand Down
34 changes: 16 additions & 18 deletions enrol/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,33 +233,31 @@ public function user_groups_and_actions($userid, $groups, $allgroups, $canmanage
* @param moodle_url $pageurl
* @return string
*/
public function user_enrolments_and_actions($userid, $enrolments, $pageurl) {
$iconedit = $this->output->pix_url('t/edit');
$iconenrolremove = $this->output->pix_url('t/delete');
$strunenrol = get_string('unenrol', 'enrol');
$stredit = get_string('edit');

public function user_enrolments_and_actions($enrolments) {
$output = '';
foreach ($enrolments as $ueid=>$enrolment) {
$enrolmentoutput = $enrolment['text'].' '.$enrolment['period'];
if ($enrolment['dimmed']) {
foreach ($enrolments as $ue) {
$enrolmentoutput = $ue['text'].' '.$ue['period'];
if ($ue['dimmed']) {
$enrolmentoutput = html_writer::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
} else {
$enrolmentoutput = html_writer::tag('span', $enrolmentoutput);
}
if ($enrolment['canunenrol']) {
$icon = html_writer::empty_tag('img', array('alt'=>$strunenrol, 'src'=>$iconenrolremove));
$url = new moodle_url($pageurl, array('action'=>'unenrol', 'ue'=>$ueid));
$enrolmentoutput .= html_writer::link($url, $icon, array('class'=>'unenrollink', 'rel'=>$ueid));
}
if ($enrolment['canmanage']) {
$icon = html_writer::empty_tag('img', array('alt'=>$stredit, 'src'=>$iconedit));
$url = new moodle_url($url, array('action'=>'edit', 'ue'=>$ueid));
$enrolmentoutput .= html_writer::link($url, $icon, array('class'=>'editenrollink', 'rel'=>$ueid));
foreach ($ue['actions'] as $action) {
$enrolmentoutput .= $this->render($action);
}
$output .= html_writer::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
}
return $output;
}

/**
* Renders a user enrolment action
* @param user_enrolment_action $icon
* @return string
*/
protected function render_user_enrolment_action(user_enrolment_action $icon) {
return html_writer::link($icon->get_url(), $this->output->render($icon->get_icon()), $icon->get_attributes());
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions enrol/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@
$table->set_fields($fields, $renderer);

$canassign = has_capability('moodle/role:assign', $manager->get_context());
$users = $manager->get_users_for_display($renderer, $PAGE->url, $table->sort, $table->sortdirection, $table->page, $table->perpage);
$users = $manager->get_users_for_display($manager, $table->sort, $table->sortdirection, $table->page, $table->perpage);
foreach ($users as $userid=>&$user) {
$user['picture'] = $OUTPUT->render($user['picture']);
$user['role'] = $renderer->user_roles_and_actions($userid, $user['roles'], $manager->get_assignable_roles(), $canassign, $PAGE->url);
$user['group'] = $renderer->user_groups_and_actions($userid, $user['groups'], $manager->get_all_groups(), has_capability('moodle/course:managegroups', $manager->get_context()), $PAGE->url);
$user['enrol'] = $renderer->user_enrolments_and_actions($userid, $user['enrolments'], $PAGE->url);
$user['enrol'] = $renderer->user_enrolments_and_actions($user['enrolments']);;
}
$table->set_total_users($manager->get_total_users());
$table->set_users($users);
Expand Down
24 changes: 23 additions & 1 deletion lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1531,4 +1531,26 @@ public function user_delete($user) {
public function get_manual_enrol_button(course_enrolment_manager $manager) {
return false;
}
}

/**
* Gets an array of the user enrolment actions
*
* @param course_enrolment_manager $manager
* @param stdClass $ue
* @return array An array of user_enrolment_actions
*/
public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
$actions = array();
$context = $manager->get_context();
$instance = $ue->enrolmentinstance;
if ($this->allow_unenrol($instance) && has_capability("enrol/".$instance->enrol.":unenrol", $context)) {
$url = new moodle_url($manager->get_moodlepage()->url, array('action' => 'unenrol', 'ue' => $ue->id));
$actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
}
if ($this->allow_manage($instance) && has_capability("enrol/".$instance->enrol.":manage", $context)) {
$url = new moodle_url($manager->get_moodlepage()->url, array('action' => 'edit', 'ue' => $ue->id));
$actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id));
}
return $actions;
}
}

0 comments on commit 291215f

Please sign in to comment.