2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This is the main renderer for the enrol section.
21 * @copyright 2010 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * This is the core renderer
28 * @copyright 2010 Sam Hemelryk
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class core_enrol_renderer
extends plugin_renderer_base
{
34 * Renders a course enrolment table
36 * @param course_enrolment_table $table
37 * @param moodleform $mform Form that contains filter controls
40 public 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 .= $mform->render();
61 $content .= $this->output
->render($table->get_paging_bar());
63 // Check if the table has any bulk operations. If it does we want to wrap the table in a
64 // form so that we can capture and perform any required bulk operations.
65 if ($table->has_bulk_user_enrolment_operations()) {
66 $content .= html_writer
::start_tag('form', array('action' => new moodle_url('/enrol/bulkchange.php'), 'method' => 'post'));
67 foreach ($table->get_combined_url_params() as $key => $value) {
68 if ($key == 'action') {
71 $content .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
73 $content .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulkchange'));
74 $content .= html_writer
::table($table);
75 $content .= html_writer
::start_tag('div', array('class' => 'singleselect bulkuserop'));
76 $content .= html_writer
::start_tag('select', array('name' => 'bulkuserop'));
77 $content .= html_writer
::tag('option', get_string('withselectedusers', 'enrol'), array('value' => ''));
78 $options = array('' => get_string('withselectedusers', 'enrol'));
79 foreach ($table->get_bulk_user_enrolment_operations() as $operation) {
80 $content .= html_writer
::tag('option', $operation->get_title(), array('value' => $operation->get_identifier()));
82 $content .= html_writer
::end_tag('select');
83 $content .= html_writer
::empty_tag('input', array('type' => 'submit', 'value' => get_string('go')));
84 $content .= html_writer
::end_tag('div');
86 $content .= html_writer
::end_tag('form');
88 $content .= html_writer
::table($table);
90 $content .= $this->output
->render($table->get_paging_bar());
91 if (!empty($buttonhtml)) {
92 $content .= $buttonhtml;
98 * Renderers the enrol_user_button.
100 * @param enrol_user_button $button
101 * @return string XHTML
103 protected function render_enrol_user_button(enrol_user_button
$button) {
104 $attributes = array('type' => 'submit',
105 'value' => $button->label
,
106 'disabled' => $button->disabled ?
'disabled' : null,
107 'title' => $button->tooltip
);
109 if ($button->actions
) {
110 $id = html_writer
::random_id('single_button');
111 $attributes['id'] = $id;
112 foreach ($button->actions
as $action) {
113 $this->add_action_handler($action, $id);
116 $button->initialise_js($this->page
);
118 // first the input element
119 $output = html_writer
::empty_tag('input', $attributes);
121 // then hidden fields
122 $params = $button->url
->params();
123 if ($button->method
=== 'post') {
124 $params['sesskey'] = sesskey();
126 foreach ($params as $var => $val) {
127 $output .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
130 // then div wrapper for xhtml strictness
131 $output = html_writer
::tag('div', $output);
133 // now the form itself around it
134 if ($button->method
=== 'get') {
135 $url = $button->url
->out_omit_querystring(true); // url without params, the anchor part allowed
137 $url = $button->url
->out_omit_querystring(); // url without params, the anchor part not allowed
140 $url = '#'; // there has to be always some action
142 $attributes = array('method' => $button->method
,
144 'id' => $button->formid
);
145 $output = html_writer
::tag('form', $output, $attributes);
147 // and finally one more wrapper with class
148 return html_writer
::tag('div', $output, array('class' => $button->class));
152 * Renders a course enrolment table
154 * @param course_enrolment_table $table
157 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table
$table) {
159 $table->initialise_javascript();
162 $searchbutton = $table->get_user_search_button();
164 $content .= $this->output
->render($searchbutton);
166 $content .= html_writer
::tag('div', get_string('otheruserdesc', 'enrol'), array('class'=>'otherusersdesc'));
167 $content .= $this->output
->render($table->get_paging_bar());
168 $content .= html_writer
::table($table);
169 $content .= $this->output
->render($table->get_paging_bar());
170 $searchbutton = $table->get_user_search_button();
172 $content .= $this->output
->render($searchbutton);
178 * Generates HTML to display the users roles and any available actions
181 * @param array $roles
182 * @param array $assignableroles
183 * @param moodle_url $pageurl
186 public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) {
187 $iconenroladd = $this->output
->pix_url('t/enroladd');
188 $iconenrolremove = $this->output
->pix_url('t/delete');
192 foreach ($roles as $roleid=>$role) {
193 if ($canassign and (is_siteadmin() or isset($assignableroles[$roleid])) and !$role['unchangeable']) {
194 $strunassign = get_string('unassignarole', 'role', $role['text']);
195 $icon = html_writer
::empty_tag('img', array('alt'=>$strunassign, 'src'=>$iconenrolremove));
196 $url = new moodle_url($pageurl, array('action'=>'unassign', 'role'=>$roleid, 'user'=>$userid));
197 $rolesoutput .= html_writer
::tag('div', $role['text'] . html_writer
::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid));
199 $rolesoutput .= html_writer
::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
203 if (!empty($assignableroles) && $canassign) {
204 $roleids = array_keys($roles);
206 foreach (array_keys($assignableroles) as $key) {
207 if (!in_array($key, $roleids)) {
208 $hasallroles = false;
213 $url = new moodle_url($pageurl, array('action'=>'assign', 'user'=>$userid));
214 $icon = html_writer
::empty_tag('img', array('alt'=>get_string('assignroles', 'role'), 'src'=>$iconenroladd));
215 $output = html_writer
::tag('div', html_writer
::link($url, $icon, array('class'=>'assignrolelink', 'title'=>get_string('assignroles', 'role'))), array('class'=>'addrole'));
218 $output .= html_writer
::tag('div', $rolesoutput, array('class'=>'roles'));
223 * Generates the HTML to view the users groups and available group actions
226 * @param array $groups
227 * @param array $allgroups
228 * @param bool $canmanagegroups
229 * @param moodle_url $pageurl
232 public function user_groups_and_actions($userid, $groups, $allgroups, $canmanagegroups, $pageurl) {
233 $iconenroladd = $this->output
->pix_url('t/enroladd');
234 $iconenrolremove = $this->output
->pix_url('t/delete');
235 $straddgroup = get_string('addgroup', 'group');
238 foreach($groups as $groupid=>$name) {
239 if ($canmanagegroups and groups_remove_member_allowed($groupid, $userid)) {
240 $icon = html_writer
::empty_tag('img', array('alt'=>get_string('removefromgroup', 'group', $name), 'src'=>$iconenrolremove));
241 $url = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid));
242 $groupoutput .= html_writer
::tag('div', $name . html_writer
::link($url, $icon), array('class'=>'group', 'rel'=>$groupid));
244 $groupoutput .= html_writer
::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
247 $groupoutput = html_writer
::tag('div', $groupoutput, array('class'=>'groups'));
248 if ($canmanagegroups && (count($groups) < count($allgroups))) {
249 $icon = html_writer
::empty_tag('img', array('alt'=>$straddgroup, 'src'=>$iconenroladd));
250 $url = new moodle_url($pageurl, array('action'=>'addmember', 'user'=>$userid));
251 $groupoutput .= html_writer
::tag('div', html_writer
::link($url, $icon), array('class'=>'addgroup'));
257 * Generates the HTML for the given enrolments + available actions
260 * @param array $enrolments
261 * @param moodle_url $pageurl
264 public function user_enrolments_and_actions($enrolments) {
266 foreach ($enrolments as $ue) {
267 $enrolmentoutput = $ue['text'].' '.$ue['period'];
269 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
271 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput);
273 foreach ($ue['actions'] as $action) {
274 $enrolmentoutput .= $this->render($action);
276 $output .= html_writer
::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
282 * Renders a user enrolment action
283 * @param user_enrolment_action $icon
286 protected function render_user_enrolment_action(user_enrolment_action
$icon) {
287 return html_writer
::link($icon->get_url(), $this->output
->render($icon->get_icon()), $icon->get_attributes());
292 * Main course enrolment table
294 * This table is used to display the enrolment information for a course.
295 * It requires that a course enrolment manager be provided during constuct with
296 * provides all of the information for the table.
297 * The control then produces the table, the paging, and the associated JS actions
302 * @copyright 2010 Sam Hemelryk
303 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
305 class course_enrolment_table
extends html_table
implements renderable
{
308 * The get/post variable that is used to identify the page.
311 const PAGEVAR
= 'page';
314 * The get/post variable to is used to identify the number of items to display
318 const PERPAGEVAR
= 'perpage';
321 * The get/post variable that is used to identify the sort field for the table.
324 const SORTVAR
= 'sort';
327 * The get/post variable that is used to identify the sort direction for the table.
330 const SORTDIRECTIONVAR
= 'dir';
333 * The default number of items per page.
336 const DEFAULTPERPAGE
= 100;
339 * The default sort, options are course_enrolment_table::$sortablefields
342 const DEFAULTSORT
= 'lastname';
345 * The default direction
348 const DEFAULTSORTDIRECTION
= 'ASC';
351 * The current page, starting from 0
357 * The total number of pages
363 * The number of items to display per page
369 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
375 * The sort direction, either ASC or DESC
378 public $sortdirection;
381 * The course manager this table is displaying for
382 * @var course_enrolment_manager
387 * The paging bar that controls the paging for this table
390 protected $pagingbar = null;
393 * The total number of users enrolled in the course
396 protected $totalusers = null;
399 * The users enrolled in this course
402 protected $users = null;
405 * The fields for this table
408 protected $fields = array();
411 * An array of bulk user enrolment operations
414 protected $bulkoperations = array();
417 * An array of sortable fields
421 protected static $sortablefields = array('firstname', 'lastname', 'idnumber', 'email',
422 'phone1', 'phone2', 'institution', 'department' );
425 * Constructs the table
427 * @param course_enrolment_manager $manager
429 public function __construct(course_enrolment_manager
$manager) {
431 $this->manager
= $manager;
433 $this->page
= optional_param(self
::PAGEVAR
, 0, PARAM_INT
);
434 $this->perpage
= optional_param(self
::PERPAGEVAR
, self
::DEFAULTPERPAGE
, PARAM_INT
);
435 $this->sort
= optional_param(self
::SORTVAR
, self
::DEFAULTSORT
, PARAM_ALPHANUM
);
436 $this->sortdirection
= optional_param(self
::SORTDIRECTIONVAR
, self
::DEFAULTSORTDIRECTION
, PARAM_ALPHA
);
438 $this->attributes
= array('class'=>'userenrolment');
439 if (!in_array($this->sort
, self
::$sortablefields)) {
440 $this->sort
= self
::DEFAULTSORT
;
442 if ($this->page
< 0) {
445 if ($this->sortdirection
!== 'ASC' && $this->sortdirection
!== 'DESC') {
446 $this->sortdirection
= self
::DEFAULTSORTDIRECTION
;
449 $this->id
= html_writer
::random_id();
451 // Collect the bulk operations for the currently filtered plugin if there is one.
452 $plugin = $manager->get_filtered_enrolment_plugin();
453 if ($plugin and enrol_is_enabled($plugin->get_name())) {
454 $this->bulkoperations
= $plugin->get_bulk_operations($manager);
459 * Returns an array of enrol_user_buttons that are created by the different
460 * enrolment plugins available.
464 public function get_manual_enrol_buttons() {
465 return $this->manager
->get_manual_enrol_buttons();
469 * Gets the sort direction for a given field
471 * @param string $field
472 * @return string ASC or DESC
474 public function get_field_sort_direction($field) {
475 if ($field == $this->sort
) {
476 return ($this->sortdirection
== 'ASC')?
'DESC':'ASC';
478 return self
::DEFAULTSORTDIRECTION
;
482 * Sets the fields for this table. These get added to the tables head as well.
484 * You can also use a multi dimensional array for this to have multiple fields
487 * @param array $fields An array of fields to set
488 * @param string $output
490 public function set_fields($fields, $output) {
491 $this->fields
= $fields;
492 $this->head
= array();
493 $this->colclasses
= array();
494 $this->align
= array();
495 $url = $this->manager
->get_moodlepage()->url
;
497 if (!empty($this->bulkoperations
)) {
498 // If there are bulk operations add a column for checkboxes.
500 $this->colclasses
[] = 'field col_bulkops';
503 foreach ($fields as $name => $label) {
505 if (is_array($label)) {
507 foreach ($label as $n => $l) {
511 if (!in_array($n, self
::$sortablefields)) {
514 $link = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$n)), $fields[$name][$n]);
515 if ($this->sort
== $n) {
516 $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));
518 $bits[] = html_writer
::tag('span', $link, array('class'=>'subheading_'.$n));
522 $newlabel = join(' / ', $bits);
524 if (!in_array($name, self
::$sortablefields)) {
527 $newlabel = html_writer
::link(new moodle_url($url, array(self
::SORTVAR
=>$name)), $fields[$name]);
528 if ($this->sort
== $name) {
529 $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));
533 $this->head
[] = $newlabel;
534 $this->colclasses
[] = 'field col_'.$name;
538 * Sets the total number of users
540 * @param int $totalusers
542 public function set_total_users($totalusers) {
543 $this->totalusers
= $totalusers;
544 $this->pages
= ceil($this->totalusers
/ $this->perpage
);
545 if ($this->page
> $this->pages
) {
546 $this->page
= $this->pages
;
550 * Sets the users for this table
552 * @param array $users
555 public function set_users(array $users) {
556 $this->users
= $users;
557 $hasbulkops = !empty($this->bulkoperations
);
558 foreach ($users as $userid=>$user) {
559 $user = (array)$user;
560 $row = new html_table_row();
561 $row->attributes
= array('class' => 'userinforow');
562 $row->id
= 'user_'.$userid;
563 $row->cells
= array();
565 // Add a checkbox into the first column.
566 $input = html_writer
::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid));
567 $row->cells
[] = new html_table_cell($input);
569 foreach ($this->fields
as $field => $label) {
570 if (is_array($label)) {
572 foreach (array_keys($label) as $subfield) {
573 if (array_key_exists($subfield, $user)) {
574 $bits[] = html_writer
::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
580 $row->cells
[] = new html_table_cell(join(' ', $bits));
582 if (!array_key_exists($field, $user)) {
583 $user[$field] = ' ';
585 $row->cells
[] = new html_table_cell($user[$field]);
588 $this->data
[] = $row;
592 public function initialise_javascript() {
593 if (has_capability('moodle/role:assign', $this->manager
->get_context())) {
594 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
597 'confirmunassigntitle',
598 'confirmunassignyes',
601 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
602 $function = 'M.enrol.rolemanager.init';
604 'containerId'=>$this->id
,
605 'userIds'=>array_keys($this->users
),
606 'courseId'=>$this->manager
->get_course()->id
,
607 'otherusers'=>isset($this->otherusers
));
608 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));
613 * Gets the paging bar instance for this table
617 public function get_paging_bar() {
618 if ($this->pagingbar
== null) {
619 $this->pagingbar
= new paging_bar($this->totalusers
, $this->page
, $this->perpage
, $this->manager
->get_moodlepage()->url
, self
::PAGEVAR
);
621 return $this->pagingbar
;
625 * Gets the direction icon for the sortable field within this table
627 * @param core_renderer $output
628 * @param string $field
631 protected function get_direction_icon($output, $field) {
632 $direction = self
::DEFAULTSORTDIRECTION
;
633 if ($this->sort
== $field) {
634 $direction = $this->sortdirection
;
636 if ($direction === 'ASC') {
637 return html_writer
::empty_tag('img', array('alt' => '', 'class' => 'iconsort',
638 'src' => $output->pix_url('t/sort_asc')));
640 return html_writer
::empty_tag('img', array('alt' => '', 'class' => 'iconsort',
641 'src' => $output->pix_url('t/sort_desc')));
646 * Gets the params that will need to be added to the url in order to return to this page.
650 public function get_url_params() {
652 self
::PAGEVAR
=> $this->page
,
653 self
::PERPAGEVAR
=> $this->perpage
,
654 self
::SORTVAR
=> $this->sort
,
655 self
::SORTDIRECTIONVAR
=> $this->sortdirection
660 * Returns an array of URL params for both the table and the manager.
664 public function get_combined_url_params() {
665 return $this->get_url_params() +
$this->manager
->get_url_params();
669 * Sets the bulk operations for this table.
671 * @param array $bulkoperations
673 public function set_bulk_user_enrolment_operations(array $bulkoperations) {
674 $this->bulkoperations
= $bulkoperations;
678 * Returns an array of bulk operations.
682 public function get_bulk_user_enrolment_operations() {
683 return $this->bulkoperations
;
687 * Returns true fi the table is aware of any bulk operations that can be performed on users
688 * selected from the currently filtered enrolment plugins.
692 public function has_bulk_user_enrolment_operations() {
693 return !empty($this->bulkoperations
);
698 * Table control used for enrolled users
700 * @copyright 2010 Sam Hemelryk
701 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
703 class course_enrolment_users_table
extends course_enrolment_table
{
706 * An array of sortable fields
710 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
714 * Table used for other users
716 * Other users are users who have roles but are not enrolled.
718 * @copyright 2010 Sam Hemelryk
719 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
721 class course_enrolment_other_users_table
extends course_enrolment_table
{
723 public $otherusers = true;
726 * Constructs the table
728 * @param course_enrolment_manager $manager
730 public function __construct(course_enrolment_manager
$manager) {
731 parent
::__construct($manager);
732 $this->attributes
= array('class'=>'userenrolment otheruserenrolment');
736 * Gets a button to search users and assign them roles in the course.
738 * @staticvar int $count
740 * @return single_button
742 public function get_user_search_button() {
744 if (!has_capability('moodle/role:assign', $this->manager
->get_context())) {
748 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager
->get_context()->id
, 'sesskey'=>sesskey()));
749 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
750 $control->class = 'singlebutton assignuserrole instance'.$count;
752 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
759 'errajaxfailedenrol',
766 'enrolperiod'), 'enrol');
767 $this->manager
->get_moodlepage()->requires
->string_for_js('assignrole', 'role');
769 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
770 $function = 'M.enrol.otherusersmanager.init';
772 'courseId'=> $this->manager
->get_course()->id
,
773 'ajaxUrl' => '/enrol/ajax.php',
774 'url' => $this->manager
->get_moodlepage()->url
->out(false));
775 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));