3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This is the main renderer for the enrol section.
23 * @copyright 2010 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * This is the core renderer
30 * @copyright 2010 Sam Hemelryk
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class core_enrol_renderer
extends plugin_renderer_base
{
36 * Renders a course enrolment table
38 * @param course_enrolment_table $table
41 protected function render_course_enrolment_users_table(course_enrolment_users_table
$table) {
43 $table->initialise_javascript();
45 $buttons = $table->get_manual_enrol_buttons();
47 if (count($buttons) > 0) {
48 $buttonhtml .= html_writer
::start_tag('div', array('class' => 'enrol_user_buttons'));
49 foreach ($buttons as $button) {
50 $buttonhtml .= $this->render($button);
52 $buttonhtml .= html_writer
::end_tag('div');
56 if (!empty($buttonhtml)) {
57 $content .= $buttonhtml;
59 $content .= $this->output
->render($table->get_enrolment_type_filter());
60 $content .= $this->output
->render($table->get_paging_bar());
62 // Check if the table has any bulk operations. If it does we want to wrap the table in a
63 // form so that we can capture and perform any required bulk operations.
64 if ($table->has_bulk_user_enrolment_operations()) {
65 $content .= html_writer
::start_tag('form', array('action' => new moodle_url('/enrol/bulkchange.php'), 'method' => 'post'));
66 foreach ($table->get_combined_url_params() as $key => $value) {
67 if ($key == 'action') {
70 $content .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
72 $content .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulkchange'));
73 $content .= html_writer
::table($table);
74 $content .= html_writer
::start_tag('div', array('class' => 'singleselect bulkuserop'));
75 $content .= html_writer
::start_tag('select', array('name' => 'bulkuserop'));
76 $content .= html_writer
::tag('option', get_string('withselectedusers', 'enrol'), array('value' => ''));
77 $options = array('' => get_string('withselectedusers', 'enrol'));
78 foreach ($table->get_bulk_user_enrolment_operations() as $operation) {
79 $content .= html_writer
::tag('option', $operation->get_title(), array('value' => $operation->get_identifier()));
81 $content .= html_writer
::end_tag('select');
82 $content .= html_writer
::empty_tag('input', array('type' => 'submit', 'value' => get_string('go')));
83 $content .= html_writer
::end_tag('div');
85 $content .= html_writer
::end_tag('form');
87 $content .= html_writer
::table($table);
89 $content .= $this->output
->render($table->get_paging_bar());
90 if (!empty($buttonhtml)) {
91 $content .= $buttonhtml;
97 * Renderers the enrol_user_button.
99 * @param enrol_user_button $button
100 * @return string XHTML
102 protected function render_enrol_user_button(enrol_user_button
$button) {
103 $attributes = array('type' => 'submit',
104 'value' => $button->label
,
105 'disabled' => $button->disabled ?
'disabled' : null,
106 'title' => $button->tooltip
);
108 if ($button->actions
) {
109 $id = html_writer
::random_id('single_button');
110 $attributes['id'] = $id;
111 foreach ($button->actions
as $action) {
112 $this->add_action_handler($action, $id);
115 $button->initialise_js($this->page
);
117 // first the input element
118 $output = html_writer
::empty_tag('input', $attributes);
120 // then hidden fields
121 $params = $button->url
->params();
122 if ($button->method
=== 'post') {
123 $params['sesskey'] = sesskey();
125 foreach ($params as $var => $val) {
126 $output .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
129 // then div wrapper for xhtml strictness
130 $output = html_writer
::tag('div', $output);
132 // now the form itself around it
133 if ($button->method
=== 'get') {
134 $url = $button->url
->out_omit_querystring(true); // url without params, the anchor part allowed
136 $url = $button->url
->out_omit_querystring(); // url without params, the anchor part not allowed
139 $url = '#'; // there has to be always some action
141 $attributes = array('method' => $button->method
,
143 'id' => $button->formid
);
144 $output = html_writer
::tag('form', $output, $attributes);
146 // and finally one more wrapper with class
147 return html_writer
::tag('div', $output, array('class' => $button->class));
151 * Renders a course enrolment table
153 * @param course_enrolment_table $table
156 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table
$table) {
158 $table->initialise_javascript();
161 $searchbutton = $table->get_user_search_button();
163 $content .= $this->output
->render($searchbutton);
165 $content .= html_writer
::tag('div', get_string('otheruserdesc', 'enrol'), array('class'=>'otherusersdesc'));
166 $content .= $this->output
->render($table->get_paging_bar());
167 $content .= html_writer
::table($table);
168 $content .= $this->output
->render($table->get_paging_bar());
169 $searchbutton = $table->get_user_search_button();
171 $content .= $this->output
->render($searchbutton);
177 * Generates HTML to display the users roles and any available actions
180 * @param array $roles
181 * @param array $assignableroles
182 * @param moodle_url $pageurl
185 public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) {
186 $iconenroladd = $this->output
->pix_url('t/enroladd');
187 $iconenrolremove = $this->output
->pix_url('t/delete');
191 foreach ($roles as $roleid=>$role) {
192 if ($canassign && !$role['unchangeable']) {
193 $strunassign = get_string('unassignarole', 'role', $role['text']);
194 $icon = html_writer
::empty_tag('img', array('alt'=>$strunassign, 'src'=>$iconenrolremove));
195 $url = new moodle_url($pageurl, array('action'=>'unassign', 'role'=>$roleid, 'user'=>$userid));
196 $rolesoutput .= html_writer
::tag('div', $role['text'] . html_writer
::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid));
198 $rolesoutput .= html_writer
::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
202 if (!empty($assignableroles) && $canassign) {
203 $roleids = array_keys($roles);
205 foreach (array_keys($assignableroles) as $key) {
206 if (!in_array($key, $roleids)) {
207 $hasallroles = false;
212 $url = new moodle_url($pageurl, array('action'=>'assign', 'user'=>$userid));
213 $icon = html_writer
::empty_tag('img', array('alt'=>get_string('assignroles', 'role'), 'src'=>$iconenroladd));
214 $output = html_writer
::tag('div', html_writer
::link($url, $icon, array('class'=>'assignrolelink', 'title'=>get_string('assignroles', 'role'))), array('class'=>'addrole'));
217 $output .= html_writer
::tag('div', $rolesoutput, array('class'=>'roles'));
222 * Generates the HTML to view the users groups and available group actions
225 * @param array $groups
226 * @param array $allgroups
227 * @param bool $canmanagegroups
228 * @param moodle_url $pageurl
231 public function user_groups_and_actions($userid, $groups, $allgroups, $canmanagegroups, $pageurl) {
232 $iconenroladd = $this->output
->pix_url('t/enroladd');
233 $iconenrolremove = $this->output
->pix_url('t/delete');
234 $straddgroup = get_string('addgroup', 'group');
237 foreach($groups as $groupid=>$name) {
238 if ($canmanagegroups) {
239 $icon = html_writer
::empty_tag('img', array('alt'=>get_string('removefromgroup', 'group', $name), 'src'=>$iconenrolremove));
240 $url = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid));
241 $groupoutput .= html_writer
::tag('div', $name . html_writer
::link($url, $icon), array('class'=>'group', 'rel'=>$groupid));
243 $groupoutput .= html_writer
::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
246 $groupoutput = html_writer
::tag('div', $groupoutput, array('class'=>'groups'));
247 if ($canmanagegroups && (count($groups) < count($allgroups))) {
248 $icon = html_writer
::empty_tag('img', array('alt'=>$straddgroup, 'src'=>$iconenroladd));
249 $url = new moodle_url($pageurl, array('action'=>'addmember', 'user'=>$userid));
250 $groupoutput .= html_writer
::tag('div', html_writer
::link($url, $icon), array('class'=>'addgroup'));
256 * Generates the HTML for the given enrolments + available actions
259 * @param array $enrolments
260 * @param moodle_url $pageurl
263 public function user_enrolments_and_actions($enrolments) {
265 foreach ($enrolments as $ue) {
266 $enrolmentoutput = $ue['text'].' '.$ue['period'];
268 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
270 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput);
272 foreach ($ue['actions'] as $action) {
273 $enrolmentoutput .= $this->render($action);
275 $output .= html_writer
::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
281 * Renders a user enrolment action
282 * @param user_enrolment_action $icon
285 protected function render_user_enrolment_action(user_enrolment_action
$icon) {
286 return html_writer
::link($icon->get_url(), $this->output
->render($icon->get_icon()), $icon->get_attributes());
291 * Main course enrolment table
293 * This table is used to display the enrolment information for a course.
294 * It requires that a course enrolment manager be provided during constuct with
295 * provides all of the information for the table.
296 * The control then produces the table, the paging, and the associated JS actions
301 * @copyright 2010 Sam Hemelryk
302 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
304 class course_enrolment_table
extends html_table
implements renderable
{
307 * The get/post variable that is used to identify the page.
310 const PAGEVAR
= 'page';
313 * The get/post variable to is used to identify the number of items to display
317 const PERPAGEVAR
= 'perpage';
320 * The get/post variable that is used to identify the sort field for the table.
323 const SORTVAR
= 'sort';
326 * The get/post variable that is used to identify the sort direction for the table.
329 const SORTDIRECTIONVAR
= 'dir';
332 * The default number of items per page.
335 const DEFAULTPERPAGE
= 100;
338 * The default sort, options are course_enrolment_table::$sortablefields
341 const DEFAULTSORT
= 'lastname';
344 * The default direction
347 const DEFAULTSORTDIRECTION
= 'ASC';
350 * The current page, starting from 0
356 * The total number of pages
362 * The number of items to display per page
368 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
374 * The sort direction, either ASC or DESC
377 public $sortdirection;
380 * The course manager this table is displaying for
381 * @var course_enrolment_manager
386 * The paging bar that controls the paging for this table
389 protected $pagingbar = null;
392 * The total number of users enrolled in the course
395 protected $totalusers = null;
398 * The users enrolled in this course
401 protected $users = null;
404 * The fields for this table
407 protected $fields = array();
410 * An array of bulk user enrolment operations
413 protected $bulkoperations = array();
416 * An array of sortable fields
420 protected static $sortablefields = array('firstname', 'lastname', 'idnumber', 'email',
421 'phone1', 'phone2', 'institution', 'department' );
424 * Constructs the table
426 * @param course_enrolment_manager $manager
428 public function __construct(course_enrolment_manager
$manager) {
430 $this->manager
= $manager;
432 $this->page
= optional_param(self
::PAGEVAR
, 0, PARAM_INT
);
433 $this->perpage
= optional_param(self
::PERPAGEVAR
, self
::DEFAULTPERPAGE
, PARAM_INT
);
434 $this->sort
= optional_param(self
::SORTVAR
, self
::DEFAULTSORT
, PARAM_ALPHA
);
435 $this->sortdirection
= optional_param(self
::SORTDIRECTIONVAR
, self
::DEFAULTSORTDIRECTION
, PARAM_ALPHA
);
437 $this->attributes
= array('class'=>'userenrolment');
438 if (!in_array($this->sort
, self
::$sortablefields)) {
439 $this->sort
= self
::DEFAULTSORT
;
441 if ($this->page
< 0) {
444 if ($this->sortdirection
!== 'ASC' && $this->sortdirection
!== 'DESC') {
445 $this->sortdirection
= self
::DEFAULTSORTDIRECTION
;
448 $this->id
= html_writer
::random_id();
450 // Collect the bulk operations for the currently filtered plugin if there is one.
451 $plugin = $manager->get_filtered_enrolment_plugin();
452 if ($plugin and enrol_is_enabled($plugin->get_name())) {
453 $this->bulkoperations
= $plugin->get_bulk_operations($manager);
458 * Returns an array of enrol_user_buttons that are created by the different
459 * enrolment plugins available.
463 public function get_manual_enrol_buttons() {
464 return $this->manager
->get_manual_enrol_buttons();
468 * Gets the sort direction for a given field
470 * @param string $field
471 * @return string ASC or DESC
473 public function get_field_sort_direction($field) {
474 if ($field == $this->sort
) {
475 return ($this->sortdirection
== 'ASC')?
'DESC':'ASC';
477 return self
::DEFAULTSORTDIRECTION
;
481 * Sets the fields for this table. These get added to the tables head as well.
483 * You can also use a multi dimensional array for this to have multiple fields
486 * @param array $fields An array of fields to set
487 * @param string $output
489 public function set_fields($fields, $output) {
490 $this->fields
= $fields;
491 $this->head
= array();
492 $this->colclasses
= array();
493 $this->align
= array();
494 $url = $this->manager
->get_moodlepage()->url
;
496 if (!empty($this->bulkoperations
)) {
497 // If there are bulk operations add a column for checkboxes.
499 $this->colclasses
[] = 'field col_bulkops';
502 foreach ($fields as $name => $label) {
504 if (is_array($label)) {
506 foreach ($label as $n => $l) {
510 if (!in_array($n, self
::$sortablefields)) {
513 $link = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$n)), $fields[$name][$n]);
514 if ($this->sort
== $n) {
515 $link .= ' '.html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$n, self
::SORTDIRECTIONVAR
=>$this->get_field_sort_direction($n))), $this->get_direction_icon($output, $n));
517 $bits[] = html_writer
::tag('span', $link, array('class'=>'subheading_'.$n));
521 $newlabel = join(' / ', $bits);
523 if (!in_array($name, self
::$sortablefields)) {
526 $newlabel = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$name)), $fields[$name]);
527 if ($this->sort
== $name) {
528 $newlabel .= ' '.html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$name, self
::SORTDIRECTIONVAR
=>$this->get_field_sort_direction($name))), $this->get_direction_icon($output, $name));
532 $this->head
[] = $newlabel;
533 $this->colclasses
[] = 'field col_'.$name;
537 * Sets the total number of users
539 * @param int $totalusers
541 public function set_total_users($totalusers) {
542 $this->totalusers
= $totalusers;
543 $this->pages
= ceil($this->totalusers
/ $this->perpage
);
544 if ($this->page
> $this->pages
) {
545 $this->page
= $this->pages
;
549 * Sets the users for this table
551 * @param array $users
554 public function set_users(array $users) {
555 $this->users
= $users;
556 $hasbulkops = !empty($this->bulkoperations
);
557 foreach ($users as $userid=>$user) {
558 $user = (array)$user;
559 $row = new html_table_row();
560 $row->attributes
= array('class' => 'userinforow');
561 $row->id
= 'user_'.$userid;
562 $row->cells
= array();
564 // Add a checkbox into the first column.
565 $input = html_writer
::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid));
566 $row->cells
[] = new html_table_cell($input);
568 foreach ($this->fields
as $field => $label) {
569 if (is_array($label)) {
571 foreach (array_keys($label) as $subfield) {
572 if (array_key_exists($subfield, $user)) {
573 $bits[] = html_writer
::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
579 $row->cells
[] = new html_table_cell(join(' ', $bits));
581 if (!array_key_exists($field, $user)) {
582 $user[$field] = ' ';
584 $row->cells
[] = new html_table_cell($user[$field]);
587 $this->data
[] = $row;
591 public function initialise_javascript() {
592 if (has_capability('moodle/role:assign', $this->manager
->get_context())) {
593 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
596 'confirmunassigntitle',
597 'confirmunassignyes',
600 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
601 $function = 'M.enrol.rolemanager.init';
603 'containerId'=>$this->id
,
604 'userIds'=>array_keys($this->users
),
605 'courseId'=>$this->manager
->get_course()->id
,
606 'otherusers'=>isset($this->otherusers
));
607 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));
612 * Gets the paging bar instance for this table
616 public function get_paging_bar() {
617 if ($this->pagingbar
== null) {
618 $this->pagingbar
= new paging_bar($this->totalusers
, $this->page
, $this->perpage
, $this->manager
->get_moodlepage()->url
, self
::PAGEVAR
);
620 return $this->pagingbar
;
624 * Gets the direction icon for the sortable field within this table
626 * @param core_renderer $output
627 * @param string $field
630 protected function get_direction_icon($output, $field) {
631 $direction = self
::DEFAULTSORTDIRECTION
;
632 if ($this->sort
== $field) {
633 $direction = $this->sortdirection
;
635 if ($direction === 'ASC') {
636 return html_writer
::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/down')));
638 return html_writer
::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/up')));
643 * Gets the params that will need to be added to the url in order to return to this page.
647 public function get_url_params() {
649 self
::PAGEVAR
=> $this->page
,
650 self
::PERPAGEVAR
=> $this->perpage
,
651 self
::SORTVAR
=> $this->sort
,
652 self
::SORTDIRECTIONVAR
=> $this->sortdirection
657 * Returns an array of URL params for both the table and the manager.
661 public function get_combined_url_params() {
662 return $this->get_url_params() +
$this->manager
->get_url_params();
666 * Sets the bulk operations for this table.
668 * @param array $bulkoperations
670 public function set_bulk_user_enrolment_operations(array $bulkoperations) {
671 $this->bulkoperations
= $bulkoperations;
675 * Returns an array of bulk operations.
679 public function get_bulk_user_enrolment_operations() {
680 return $this->bulkoperations
;
684 * Returns true fi the table is aware of any bulk operations that can be performed on users
685 * selected from the currently filtered enrolment plugins.
689 public function has_bulk_user_enrolment_operations() {
690 return !empty($this->bulkoperations
);
695 * Table control used for enrolled users
697 * @copyright 2010 Sam Hemelryk
698 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
700 class course_enrolment_users_table
extends course_enrolment_table
{
703 * An array of sortable fields
707 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
710 * Gets the enrolment type filter control for this table
712 * @return single_select
714 public function get_enrolment_type_filter() {
715 $selector = new single_select($this->manager
->get_moodlepage()->url
, 'ifilter', array(0=>get_string('all')) +
(array)$this->manager
->get_enrolment_instance_names(), $this->manager
->get_enrolment_filter(), array());
716 $selector->set_label( get_string('enrolmentinstances', 'enrol'));
722 * Table used for other users
724 * Other users are users who have roles but are not enrolled.
726 * @copyright 2010 Sam Hemelryk
727 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
729 class course_enrolment_other_users_table
extends course_enrolment_table
{
731 public $otherusers = true;
734 * Constructs the table
736 * @param course_enrolment_manager $manager
738 public function __construct(course_enrolment_manager
$manager) {
739 parent
::__construct($manager);
740 $this->attributes
= array('class'=>'userenrolment otheruserenrolment');
744 * Gets a button to search users and assign them roles in the course.
746 * @staticvar int $count
748 * @return single_button
750 public function get_user_search_button() {
752 if (!has_capability('moodle/role:assign', $this->manager
->get_context())) {
756 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager
->get_context()->id
, 'sesskey'=>sesskey()));
757 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
758 $control->class = 'singlebutton assignuserrole instance'.$count;
760 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
767 'errajaxfailedenrol',
774 'enrolperiod'), 'enrol');
775 $this->manager
->get_moodlepage()->requires
->string_for_js('assignrole', 'role');
777 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
778 $function = 'M.enrol.otherusersmanager.init';
780 'courseId'=> $this->manager
->get_course()->id
,
781 'ajaxUrl' => '/enrol/ajax.php',
782 'url' => $this->manager
->get_moodlepage()->url
->out(false));
783 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));