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
39 * @param moodleform $mform Form that contains filter controls
42 public function render_course_enrolment_users_table(course_enrolment_users_table
$table,
45 $table->initialise_javascript();
47 $buttons = $table->get_manual_enrol_buttons();
49 if (count($buttons) > 0) {
50 $buttonhtml .= html_writer
::start_tag('div', array('class' => 'enrol_user_buttons'));
51 foreach ($buttons as $button) {
52 $buttonhtml .= $this->render($button);
54 $buttonhtml .= html_writer
::end_tag('div');
58 if (!empty($buttonhtml)) {
59 $content .= $buttonhtml;
61 $content .= $mform->render();
63 $content .= $this->output
->render($table->get_paging_bar());
65 // Check if the table has any bulk operations. If it does we want to wrap the table in a
66 // form so that we can capture and perform any required bulk operations.
67 if ($table->has_bulk_user_enrolment_operations()) {
68 $content .= html_writer
::start_tag('form', array('action' => new moodle_url('/enrol/bulkchange.php'), 'method' => 'post'));
69 foreach ($table->get_combined_url_params() as $key => $value) {
70 if ($key == 'action') {
73 $content .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
75 $content .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulkchange'));
76 $content .= html_writer
::table($table);
77 $content .= html_writer
::start_tag('div', array('class' => 'singleselect bulkuserop'));
78 $content .= html_writer
::start_tag('select', array('name' => 'bulkuserop'));
79 $content .= html_writer
::tag('option', get_string('withselectedusers', 'enrol'), array('value' => ''));
80 $options = array('' => get_string('withselectedusers', 'enrol'));
81 foreach ($table->get_bulk_user_enrolment_operations() as $operation) {
82 $content .= html_writer
::tag('option', $operation->get_title(), array('value' => $operation->get_identifier()));
84 $content .= html_writer
::end_tag('select');
85 $content .= html_writer
::empty_tag('input', array('type' => 'submit', 'value' => get_string('go')));
86 $content .= html_writer
::end_tag('div');
88 $content .= html_writer
::end_tag('form');
90 $content .= html_writer
::table($table);
92 $content .= $this->output
->render($table->get_paging_bar());
93 if (!empty($buttonhtml)) {
94 $content .= $buttonhtml;
100 * Renderers the enrol_user_button.
102 * @param enrol_user_button $button
103 * @return string XHTML
105 protected function render_enrol_user_button(enrol_user_button
$button) {
106 $attributes = array('type' => 'submit',
107 'value' => $button->label
,
108 'disabled' => $button->disabled ?
'disabled' : null,
109 'title' => $button->tooltip
);
111 if ($button->actions
) {
112 $id = html_writer
::random_id('single_button');
113 $attributes['id'] = $id;
114 foreach ($button->actions
as $action) {
115 $this->add_action_handler($action, $id);
118 $button->initialise_js($this->page
);
120 // first the input element
121 $output = html_writer
::empty_tag('input', $attributes);
123 // then hidden fields
124 $params = $button->url
->params();
125 if ($button->method
=== 'post') {
126 $params['sesskey'] = sesskey();
128 foreach ($params as $var => $val) {
129 $output .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
132 // then div wrapper for xhtml strictness
133 $output = html_writer
::tag('div', $output);
135 // now the form itself around it
136 if ($button->method
=== 'get') {
137 $url = $button->url
->out_omit_querystring(true); // url without params, the anchor part allowed
139 $url = $button->url
->out_omit_querystring(); // url without params, the anchor part not allowed
142 $url = '#'; // there has to be always some action
144 $attributes = array('method' => $button->method
,
146 'id' => $button->formid
);
147 $output = html_writer
::tag('form', $output, $attributes);
149 // and finally one more wrapper with class
150 return html_writer
::tag('div', $output, array('class' => $button->class));
154 * Renders a course enrolment table
156 * @param course_enrolment_table $table
159 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table
$table) {
161 $table->initialise_javascript();
164 $searchbutton = $table->get_user_search_button();
166 $content .= $this->output
->render($searchbutton);
168 $content .= html_writer
::tag('div', get_string('otheruserdesc', 'enrol'), array('class'=>'otherusersdesc'));
169 $content .= $this->output
->render($table->get_paging_bar());
170 $content .= html_writer
::table($table);
171 $content .= $this->output
->render($table->get_paging_bar());
172 $searchbutton = $table->get_user_search_button();
174 $content .= $this->output
->render($searchbutton);
180 * Generates HTML to display the users roles and any available actions
183 * @param array $roles
184 * @param array $assignableroles
185 * @param moodle_url $pageurl
188 public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) {
189 $iconenroladd = $this->output
->pix_url('t/enroladd');
190 $iconenrolremove = $this->output
->pix_url('t/delete');
194 foreach ($roles as $roleid=>$role) {
195 if ($canassign and (is_siteadmin() or isset($assignableroles[$roleid])) and !$role['unchangeable']) {
196 $strunassign = get_string('unassignarole', 'role', $role['text']);
197 $icon = html_writer
::empty_tag('img', array('alt'=>$strunassign, 'src'=>$iconenrolremove));
198 $url = new moodle_url($pageurl, array('action'=>'unassign', 'role'=>$roleid, 'user'=>$userid));
199 $rolesoutput .= html_writer
::tag('div', $role['text'] . html_writer
::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid));
201 $rolesoutput .= html_writer
::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
205 if (!empty($assignableroles) && $canassign) {
206 $roleids = array_keys($roles);
208 foreach (array_keys($assignableroles) as $key) {
209 if (!in_array($key, $roleids)) {
210 $hasallroles = false;
215 $url = new moodle_url($pageurl, array('action'=>'assign', 'user'=>$userid));
216 $icon = html_writer
::empty_tag('img', array('alt'=>get_string('assignroles', 'role'), 'src'=>$iconenroladd));
217 $output = html_writer
::tag('div', html_writer
::link($url, $icon, array('class'=>'assignrolelink', 'title'=>get_string('assignroles', 'role'))), array('class'=>'addrole'));
220 $output .= html_writer
::tag('div', $rolesoutput, array('class'=>'roles'));
225 * Generates the HTML to view the users groups and available group actions
228 * @param array $groups
229 * @param array $allgroups
230 * @param bool $canmanagegroups
231 * @param moodle_url $pageurl
234 public function user_groups_and_actions($userid, $groups, $allgroups, $canmanagegroups, $pageurl) {
235 $iconenroladd = $this->output
->pix_url('t/enroladd');
236 $iconenrolremove = $this->output
->pix_url('t/delete');
237 $straddgroup = get_string('addgroup', 'group');
240 foreach($groups as $groupid=>$name) {
241 if ($canmanagegroups and groups_remove_member_allowed($groupid, $userid)) {
242 $icon = html_writer
::empty_tag('img', array('alt'=>get_string('removefromgroup', 'group', $name), 'src'=>$iconenrolremove));
243 $url = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid));
244 $groupoutput .= html_writer
::tag('div', $name . html_writer
::link($url, $icon), array('class'=>'group', 'rel'=>$groupid));
246 $groupoutput .= html_writer
::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
249 $groupoutput = html_writer
::tag('div', $groupoutput, array('class'=>'groups'));
250 if ($canmanagegroups && (count($groups) < count($allgroups))) {
251 $icon = html_writer
::empty_tag('img', array('alt'=>$straddgroup, 'src'=>$iconenroladd));
252 $url = new moodle_url($pageurl, array('action'=>'addmember', 'user'=>$userid));
253 $groupoutput .= html_writer
::tag('div', html_writer
::link($url, $icon), array('class'=>'addgroup'));
259 * Generates the HTML for the given enrolments + available actions
262 * @param array $enrolments
263 * @param moodle_url $pageurl
266 public function user_enrolments_and_actions($enrolments) {
268 foreach ($enrolments as $ue) {
269 $enrolmentoutput = $ue['text'].' '.$ue['period'];
271 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
273 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput);
275 foreach ($ue['actions'] as $action) {
276 $enrolmentoutput .= $this->render($action);
278 $output .= html_writer
::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
284 * Renders a user enrolment action
285 * @param user_enrolment_action $icon
288 protected function render_user_enrolment_action(user_enrolment_action
$icon) {
289 return html_writer
::link($icon->get_url(), $this->output
->render($icon->get_icon()), $icon->get_attributes());
294 * Main course enrolment table
296 * This table is used to display the enrolment information for a course.
297 * It requires that a course enrolment manager be provided during constuct with
298 * provides all of the information for the table.
299 * The control then produces the table, the paging, and the associated JS actions
304 * @copyright 2010 Sam Hemelryk
305 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
307 class course_enrolment_table
extends html_table
implements renderable
{
310 * The get/post variable that is used to identify the page.
313 const PAGEVAR
= 'page';
316 * The get/post variable to is used to identify the number of items to display
320 const PERPAGEVAR
= 'perpage';
323 * The get/post variable that is used to identify the sort field for the table.
326 const SORTVAR
= 'sort';
329 * The get/post variable that is used to identify the sort direction for the table.
332 const SORTDIRECTIONVAR
= 'dir';
335 * The default number of items per page.
338 const DEFAULTPERPAGE
= 100;
341 * The default sort, options are course_enrolment_table::$sortablefields
344 const DEFAULTSORT
= 'lastname';
347 * The default direction
350 const DEFAULTSORTDIRECTION
= 'ASC';
353 * The current page, starting from 0
359 * The total number of pages
365 * The number of items to display per page
371 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
377 * The sort direction, either ASC or DESC
380 public $sortdirection;
383 * The course manager this table is displaying for
384 * @var course_enrolment_manager
389 * The paging bar that controls the paging for this table
392 protected $pagingbar = null;
395 * The total number of users enrolled in the course
398 protected $totalusers = null;
401 * The users enrolled in this course
404 protected $users = null;
407 * The fields for this table
410 protected $fields = array();
413 * An array of bulk user enrolment operations
416 protected $bulkoperations = array();
419 * An array of sortable fields
423 protected static $sortablefields = array('firstname', 'lastname', 'idnumber', 'email',
424 'phone1', 'phone2', 'institution', 'department' );
427 * Constructs the table
429 * @param course_enrolment_manager $manager
431 public function __construct(course_enrolment_manager
$manager) {
433 $this->manager
= $manager;
435 $this->page
= optional_param(self
::PAGEVAR
, 0, PARAM_INT
);
436 $this->perpage
= optional_param(self
::PERPAGEVAR
, self
::DEFAULTPERPAGE
, PARAM_INT
);
437 $this->sort
= optional_param(self
::SORTVAR
, self
::DEFAULTSORT
, PARAM_ALPHANUM
);
438 $this->sortdirection
= optional_param(self
::SORTDIRECTIONVAR
, self
::DEFAULTSORTDIRECTION
, PARAM_ALPHA
);
440 $this->attributes
= array('class'=>'userenrolment');
441 if (!in_array($this->sort
, self
::$sortablefields)) {
442 $this->sort
= self
::DEFAULTSORT
;
444 if ($this->page
< 0) {
447 if ($this->sortdirection
!== 'ASC' && $this->sortdirection
!== 'DESC') {
448 $this->sortdirection
= self
::DEFAULTSORTDIRECTION
;
451 $this->id
= html_writer
::random_id();
453 // Collect the bulk operations for the currently filtered plugin if there is one.
454 $plugin = $manager->get_filtered_enrolment_plugin();
455 if ($plugin and enrol_is_enabled($plugin->get_name())) {
456 $this->bulkoperations
= $plugin->get_bulk_operations($manager);
461 * Returns an array of enrol_user_buttons that are created by the different
462 * enrolment plugins available.
466 public function get_manual_enrol_buttons() {
467 return $this->manager
->get_manual_enrol_buttons();
471 * Gets the sort direction for a given field
473 * @param string $field
474 * @return string ASC or DESC
476 public function get_field_sort_direction($field) {
477 if ($field == $this->sort
) {
478 return ($this->sortdirection
== 'ASC')?
'DESC':'ASC';
480 return self
::DEFAULTSORTDIRECTION
;
484 * Sets the fields for this table. These get added to the tables head as well.
486 * You can also use a multi dimensional array for this to have multiple fields
489 * @param array $fields An array of fields to set
490 * @param string $output
492 public function set_fields($fields, $output) {
493 $this->fields
= $fields;
494 $this->head
= array();
495 $this->colclasses
= array();
496 $this->align
= array();
497 $url = $this->manager
->get_moodlepage()->url
;
499 if (!empty($this->bulkoperations
)) {
500 // If there are bulk operations add a column for checkboxes.
502 $this->colclasses
[] = 'field col_bulkops';
505 foreach ($fields as $name => $label) {
507 if (is_array($label)) {
509 foreach ($label as $n => $l) {
513 if (!in_array($n, self
::$sortablefields)) {
516 $link = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$n)), $fields[$name][$n]);
517 if ($this->sort
== $n) {
518 $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));
520 $bits[] = html_writer
::tag('span', $link, array('class'=>'subheading_'.$n));
524 $newlabel = join(' / ', $bits);
526 if (!in_array($name, self
::$sortablefields)) {
529 $newlabel = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$name)), $fields[$name]);
530 if ($this->sort
== $name) {
531 $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));
535 $this->head
[] = $newlabel;
536 $this->colclasses
[] = 'field col_'.$name;
540 * Sets the total number of users
542 * @param int $totalusers
544 public function set_total_users($totalusers) {
545 $this->totalusers
= $totalusers;
546 $this->pages
= ceil($this->totalusers
/ $this->perpage
);
547 if ($this->page
> $this->pages
) {
548 $this->page
= $this->pages
;
552 * Sets the users for this table
554 * @param array $users
557 public function set_users(array $users) {
558 $this->users
= $users;
559 $hasbulkops = !empty($this->bulkoperations
);
560 foreach ($users as $userid=>$user) {
561 $user = (array)$user;
562 $row = new html_table_row();
563 $row->attributes
= array('class' => 'userinforow');
564 $row->id
= 'user_'.$userid;
565 $row->cells
= array();
567 // Add a checkbox into the first column.
568 $input = html_writer
::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid));
569 $row->cells
[] = new html_table_cell($input);
571 foreach ($this->fields
as $field => $label) {
572 if (is_array($label)) {
574 foreach (array_keys($label) as $subfield) {
575 if (array_key_exists($subfield, $user)) {
576 $bits[] = html_writer
::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
582 $row->cells
[] = new html_table_cell(join(' ', $bits));
584 if (!array_key_exists($field, $user)) {
585 $user[$field] = ' ';
587 $row->cells
[] = new html_table_cell($user[$field]);
590 $this->data
[] = $row;
594 public function initialise_javascript() {
595 if (has_capability('moodle/role:assign', $this->manager
->get_context())) {
596 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
599 'confirmunassigntitle',
600 'confirmunassignyes',
603 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
604 $function = 'M.enrol.rolemanager.init';
606 'containerId'=>$this->id
,
607 'userIds'=>array_keys($this->users
),
608 'courseId'=>$this->manager
->get_course()->id
,
609 'otherusers'=>isset($this->otherusers
));
610 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));
615 * Gets the paging bar instance for this table
619 public function get_paging_bar() {
620 if ($this->pagingbar
== null) {
621 $this->pagingbar
= new paging_bar($this->totalusers
, $this->page
, $this->perpage
, $this->manager
->get_moodlepage()->url
, self
::PAGEVAR
);
623 return $this->pagingbar
;
627 * Gets the direction icon for the sortable field within this table
629 * @param core_renderer $output
630 * @param string $field
633 protected function get_direction_icon($output, $field) {
634 $direction = self
::DEFAULTSORTDIRECTION
;
635 if ($this->sort
== $field) {
636 $direction = $this->sortdirection
;
638 if ($direction === 'ASC') {
639 return html_writer
::empty_tag('img', array('alt' => '', 'class' => 'iconsort',
640 'src' => $output->pix_url('t/sort_asc')));
642 return html_writer
::empty_tag('img', array('alt' => '', 'class' => 'iconsort',
643 'src' => $output->pix_url('t/sort_desc')));
648 * Gets the params that will need to be added to the url in order to return to this page.
652 public function get_url_params() {
654 self
::PAGEVAR
=> $this->page
,
655 self
::PERPAGEVAR
=> $this->perpage
,
656 self
::SORTVAR
=> $this->sort
,
657 self
::SORTDIRECTIONVAR
=> $this->sortdirection
662 * Returns an array of URL params for both the table and the manager.
666 public function get_combined_url_params() {
667 return $this->get_url_params() +
$this->manager
->get_url_params();
671 * Sets the bulk operations for this table.
673 * @param array $bulkoperations
675 public function set_bulk_user_enrolment_operations(array $bulkoperations) {
676 $this->bulkoperations
= $bulkoperations;
680 * Returns an array of bulk operations.
684 public function get_bulk_user_enrolment_operations() {
685 return $this->bulkoperations
;
689 * Returns true fi the table is aware of any bulk operations that can be performed on users
690 * selected from the currently filtered enrolment plugins.
694 public function has_bulk_user_enrolment_operations() {
695 return !empty($this->bulkoperations
);
700 * Table control used for enrolled users
702 * @copyright 2010 Sam Hemelryk
703 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
705 class course_enrolment_users_table
extends course_enrolment_table
{
708 * An array of sortable fields
712 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
716 * Table used for other users
718 * Other users are users who have roles but are not enrolled.
720 * @copyright 2010 Sam Hemelryk
721 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
723 class course_enrolment_other_users_table
extends course_enrolment_table
{
725 public $otherusers = true;
728 * Constructs the table
730 * @param course_enrolment_manager $manager
732 public function __construct(course_enrolment_manager
$manager) {
733 parent
::__construct($manager);
734 $this->attributes
= array('class'=>'userenrolment otheruserenrolment');
738 * Gets a button to search users and assign them roles in the course.
740 * @staticvar int $count
742 * @return single_button
744 public function get_user_search_button() {
746 if (!has_capability('moodle/role:assign', $this->manager
->get_context())) {
750 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager
->get_context()->id
, 'sesskey'=>sesskey()));
751 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
752 $control->class = 'singlebutton assignuserrole instance'.$count;
754 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
761 'errajaxfailedenrol',
768 'enrolperiod'), 'enrol');
769 $this->manager
->get_moodlepage()->requires
->string_for_js('assignrole', 'role');
771 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
772 $function = 'M.enrol.otherusersmanager.init';
774 'courseId'=> $this->manager
->get_course()->id
,
775 'ajaxUrl' => '/enrol/ajax.php',
776 'url' => $this->manager
->get_moodlepage()->url
->out(false));
777 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));