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', 'email');
423 * Constructs the table
425 * @param course_enrolment_manager $manager
427 public function __construct(course_enrolment_manager
$manager) {
429 $this->manager
= $manager;
431 $this->page
= optional_param(self
::PAGEVAR
, 0, PARAM_INT
);
432 $this->perpage
= optional_param(self
::PERPAGEVAR
, self
::DEFAULTPERPAGE
, PARAM_INT
);
433 $this->sort
= optional_param(self
::SORTVAR
, self
::DEFAULTSORT
, PARAM_ALPHA
);
434 $this->sortdirection
= optional_param(self
::SORTDIRECTIONVAR
, self
::DEFAULTSORTDIRECTION
, PARAM_ALPHA
);
436 $this->attributes
= array('class'=>'userenrolment');
437 if (!in_array($this->sort
, self
::$sortablefields)) {
438 $this->sort
= self
::DEFAULTSORT
;
440 if ($this->page
< 0) {
443 if ($this->sortdirection
!== 'ASC' && $this->sortdirection
!== 'DESC') {
444 $this->sortdirection
= self
::DEFAULTSORTDIRECTION
;
447 $this->id
= html_writer
::random_id();
449 // Collect the bulk operations for the currently filtered plugin if there is one.
450 $plugin = $manager->get_filtered_enrolment_plugin();
452 $this->bulkoperations
= $plugin->get_bulk_operations($manager);
457 * Returns an array of enrol_user_buttons that are created by the different
458 * enrolment plugins available.
462 public function get_manual_enrol_buttons() {
463 return $this->manager
->get_manual_enrol_buttons();
467 * Gets the sort direction for a given field
469 * @param string $field
470 * @return string ASC or DESC
472 public function get_field_sort_direction($field) {
473 if ($field == $this->sort
) {
474 return ($this->sortdirection
== 'ASC')?
'DESC':'ASC';
476 return self
::DEFAULTSORTDIRECTION
;
480 * Sets the fields for this table. These get added to the tables head as well.
482 * You can also use a multi dimensional array for this to have multiple fields
485 * @param array $fields An array of fields to set
486 * @param string $output
488 public function set_fields($fields, $output) {
489 $this->fields
= $fields;
490 $this->head
= array();
491 $this->colclasses
= array();
492 $this->align
= array();
493 $url = $this->manager
->get_moodlepage()->url
;
495 if (!empty($this->bulkoperations
)) {
496 // If there are bulk operations add a column for checkboxes.
498 $this->colclasses
[] = 'field col_bulkops';
501 foreach ($fields as $name => $label) {
503 if (is_array($label)) {
505 foreach ($label as $n => $l) {
509 if (!in_array($n, self
::$sortablefields)) {
512 $link = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$n)), $fields[$name][$n]);
513 if ($this->sort
== $n) {
514 $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));
516 $bits[] = html_writer
::tag('span', $link, array('class'=>'subheading_'.$n));
520 $newlabel = join(' / ', $bits);
522 if (!in_array($name, self
::$sortablefields)) {
525 $newlabel = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$name)), $fields[$name]);
526 if ($this->sort
== $name) {
527 $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));
531 $this->head
[] = $newlabel;
532 $this->colclasses
[] = 'field col_'.$name;
536 * Sets the total number of users
538 * @param int $totalusers
540 public function set_total_users($totalusers) {
541 $this->totalusers
= $totalusers;
542 $this->pages
= ceil($this->totalusers
/ $this->perpage
);
543 if ($this->page
> $this->pages
) {
544 $this->page
= $this->pages
;
548 * Sets the users for this table
550 * @param array $users
553 public function set_users(array $users) {
554 $this->users
= $users;
555 $hasbulkops = !empty($this->bulkoperations
);
556 foreach ($users as $userid=>$user) {
557 $user = (array)$user;
558 $row = new html_table_row();
559 $row->attributes
= array('class' => 'userinforow');
560 $row->id
= 'user_'.$userid;
561 $row->cells
= array();
563 // Add a checkbox into the first column.
564 $input = html_writer
::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid));
565 $row->cells
[] = new html_table_cell($input);
567 foreach ($this->fields
as $field => $label) {
568 if (is_array($label)) {
570 foreach (array_keys($label) as $subfield) {
571 if (array_key_exists($subfield, $user)) {
572 $bits[] = html_writer
::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
578 $row->cells
[] = new html_table_cell(join(' ', $bits));
580 if (!array_key_exists($field, $user)) {
581 $user[$field] = ' ';
583 $row->cells
[] = new html_table_cell($user[$field]);
586 $this->data
[] = $row;
590 public function initialise_javascript() {
591 if (has_capability('moodle/role:assign', $this->manager
->get_context())) {
592 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
595 'confirmunassigntitle',
596 'confirmunassignyes',
599 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
600 $function = 'M.enrol.rolemanager.init';
602 'containerId'=>$this->id
,
603 'userIds'=>array_keys($this->users
),
604 'courseId'=>$this->manager
->get_course()->id
,
605 'otherusers'=>isset($this->otherusers
));
606 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));
611 * Gets the paging bar instance for this table
615 public function get_paging_bar() {
616 if ($this->pagingbar
== null) {
617 $this->pagingbar
= new paging_bar($this->totalusers
, $this->page
, $this->perpage
, $this->manager
->get_moodlepage()->url
, self
::PAGEVAR
);
619 return $this->pagingbar
;
623 * Gets the direction icon for the sortable field within this table
625 * @param core_renderer $output
626 * @param string $field
629 protected function get_direction_icon($output, $field) {
630 $direction = self
::DEFAULTSORTDIRECTION
;
631 if ($this->sort
== $field) {
632 $direction = $this->sortdirection
;
634 if ($direction === 'ASC') {
635 return html_writer
::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/down')));
637 return html_writer
::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/up')));
642 * Gets the params that will need to be added to the url in order to return to this page.
646 public function get_url_params() {
648 self
::PAGEVAR
=> $this->page
,
649 self
::PERPAGEVAR
=> $this->perpage
,
650 self
::SORTVAR
=> $this->sort
,
651 self
::SORTDIRECTIONVAR
=> $this->sortdirection
656 * Returns an array of URL params for both the table and the manager.
660 public function get_combined_url_params() {
661 return $this->get_url_params() +
$this->manager
->get_url_params();
665 * Sets the bulk operations for this table.
667 * @param array $bulkoperations
669 public function set_bulk_user_enrolment_operations(array $bulkoperations) {
670 $this->bulkoperations
= $bulkoperations;
674 * Returns an array of bulk operations.
678 public function get_bulk_user_enrolment_operations() {
679 return $this->bulkoperations
;
683 * Returns true fi the table is aware of any bulk operations that can be performed on users
684 * selected from the currently filtered enrolment plugins.
688 public function has_bulk_user_enrolment_operations() {
689 return !empty($this->bulkoperations
);
694 * Table control used for enrolled users
696 * @copyright 2010 Sam Hemelryk
697 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
699 class course_enrolment_users_table
extends course_enrolment_table
{
702 * An array of sortable fields
706 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
709 * Gets the enrolment type filter control for this table
711 * @return single_select
713 public function get_enrolment_type_filter() {
714 $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());
715 $selector->set_label( get_string('enrolmentinstances', 'enrol'));
721 * Table used for other users
723 * Other users are users who have roles but are not enrolled.
725 * @copyright 2010 Sam Hemelryk
726 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
728 class course_enrolment_other_users_table
extends course_enrolment_table
{
730 public $otherusers = true;
733 * Constructs the table
735 * @param course_enrolment_manager $manager
737 public function __construct(course_enrolment_manager
$manager) {
738 parent
::__construct($manager);
739 $this->attributes
= array('class'=>'userenrolment otheruserenrolment');
743 * Gets a button to search users and assign them roles in the course.
745 * @staticvar int $count
747 * @return single_button
749 public function get_user_search_button() {
751 if (!has_capability('moodle/role:assign', $this->manager
->get_context())) {
755 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager
->get_context()->id
, 'sesskey'=>sesskey()));
756 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
757 $control->class = 'singlebutton assignuserrole instance'.$count;
759 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
766 'errajaxfailedenrol',
773 'enrolperiod'), 'enrol');
774 $this->manager
->get_moodlepage()->requires
->string_for_js('assignrole', 'role');
776 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
777 $function = 'M.enrol.otherusersmanager.init';
779 'courseId'=> $this->manager
->get_course()->id
,
780 'ajaxUrl' => '/enrol/ajax.php',
781 'url' => $this->manager
->get_moodlepage()->url
->out(false));
782 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));