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 * Renderers the enrol_user_button.
36 * @param enrol_user_button $button
37 * @return string XHTML
39 protected function render_enrol_user_button(enrol_user_button
$button) {
40 $buttoninfo = $button->export_for_template($this->output
);
44 'value' => $buttoninfo->label
,
45 'disabled' => $buttoninfo->disabled ?
'disabled' : null,
46 'title' => $buttoninfo->tooltip
,
47 'class' => 'btn ' . "btn-{$buttoninfo->type}",
49 if ($button->actions
) {
50 $id = html_writer
::random_id('single_button');
51 $attributes['id'] = $id;
52 foreach ($button->actions
as $action) {
53 $this->add_action_handler($action, $id);
56 $button->initialise_js($this->page
);
58 // first the input element
59 $output = html_writer
::empty_tag('input', $attributes);
62 $params = $button->url
->params();
63 if ($button->method
=== 'post') {
64 $params['sesskey'] = sesskey();
66 foreach ($params as $var => $val) {
67 $output .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
70 // then div wrapper for xhtml strictness
71 $output = html_writer
::tag('div', $output);
73 // now the form itself around it
74 if ($button->method
=== 'get') {
75 $url = $button->url
->out_omit_querystring(true); // url without params, the anchor part allowed
77 $url = $button->url
->out_omit_querystring(); // url without params, the anchor part not allowed
80 $url = '#'; // there has to be always some action
82 $attributes = array('method' => $button->method
,
84 'id' => $button->formid
);
85 $output = html_writer
::tag('form', $output, $attributes);
87 // and finally one more wrapper with class
88 return html_writer
::tag('div', $output, array('class' => $button->class));
92 * Renders a course enrolment table
94 * @param course_enrolment_table $table
97 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table
$table) {
99 $table->initialise_javascript();
102 $content .= html_writer
::tag('div', get_string('otheruserdesc', 'enrol'), array('class'=>'otherusersdesc'));
103 $content .= $this->output
->render($table->get_paging_bar());
104 $content .= html_writer
::table($table);
105 $content .= $this->output
->render($table->get_paging_bar());
110 * Generates HTML to display the users roles and any available actions
113 * @param array $roles
114 * @param array $assignableroles
115 * @param moodle_url $pageurl
118 public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) {
120 // Get list of roles.
122 foreach ($roles as $roleid=>$role) {
123 if ($canassign and (is_siteadmin() or isset($assignableroles[$roleid])) and !$role['unchangeable']) {
124 $strunassign = get_string('unassignarole', 'role', $role['text']);
125 $icon = $this->output
->pix_icon('t/delete', $strunassign);
126 $url = new moodle_url($pageurl, array('action'=>'unassign', 'roleid'=>$roleid, 'user'=>$userid));
127 $rolesoutput .= html_writer
::tag('div', $role['text'] . html_writer
::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid));
129 $rolesoutput .= html_writer
::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
133 if (!empty($assignableroles) && $canassign) {
134 $roleids = array_keys($roles);
136 foreach (array_keys($assignableroles) as $key) {
137 if (!in_array($key, $roleids)) {
138 $hasallroles = false;
143 $url = new moodle_url($pageurl, array('action' => 'assign', 'user' => $userid));
144 $roleicon = $this->output
->pix_icon('i/assignroles', get_string('assignroles', 'role'));
145 $link = html_writer
::link($url, $roleicon, array('class' => 'assignrolelink'));
146 $output = html_writer
::tag('div', $link, array('class'=>'addrole'));
149 $output .= html_writer
::tag('div', $rolesoutput, array('class'=>'roles'));
154 * Generates the HTML to view the users groups and available group actions
157 * @param array $groups
158 * @param array $allgroups
159 * @param bool $canmanagegroups
160 * @param moodle_url $pageurl
163 public function user_groups_and_actions($userid, $groups, $allgroups, $canmanagegroups, $pageurl) {
164 $groupicon = $this->output
->pix_icon('i/group', get_string('addgroup', 'group'));
167 foreach($groups as $groupid=>$name) {
168 if ($canmanagegroups and groups_remove_member_allowed($groupid, $userid)) {
169 $icon = $this->output
->pix_icon('t/delete', get_string('removefromgroup', 'group', $name));
170 $url = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid));
171 $groupoutput .= html_writer
::tag('div', $name . html_writer
::link($url, $icon), array('class'=>'group', 'rel'=>$groupid));
173 $groupoutput .= html_writer
::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
177 if ($canmanagegroups && (count($groups) < count($allgroups))) {
178 $url = new moodle_url($pageurl, array('action'=>'addmember', 'user'=>$userid));
179 $output .= html_writer
::tag('div', html_writer
::link($url, $groupicon), array('class'=>'addgroup'));
181 $output = $output.html_writer
::tag('div', $groupoutput, array('class'=>'groups'));
186 * Generates the HTML for the given enrolments + available actions
189 * @param array $enrolments
190 * @param moodle_url $pageurl
193 public function user_enrolments_and_actions($enrolments) {
195 foreach ($enrolments as $ue) {
196 $enrolmentoutput = $ue['text'].' '.$ue['period'];
198 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
200 $enrolmentoutput = html_writer
::tag('span', $enrolmentoutput);
202 foreach ($ue['actions'] as $action) {
203 $enrolmentoutput .= $this->render($action);
205 $output .= html_writer
::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
211 * Renders a user enrolment action
212 * @param user_enrolment_action $icon
215 protected function render_user_enrolment_action(user_enrolment_action
$icon) {
216 return html_writer
::link($icon->get_url(), $this->output
->render($icon->get_icon()), $icon->get_attributes());
221 * Main course enrolment table
223 * This table is used to display the enrolment information for a course.
224 * It requires that a course enrolment manager be provided during constuct with
225 * provides all of the information for the table.
226 * The control then produces the table, the paging, and the associated JS actions
231 * @copyright 2010 Sam Hemelryk
232 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
234 class course_enrolment_table
extends html_table
implements renderable
{
237 * The get/post variable that is used to identify the page.
240 const PAGEVAR
= 'page';
243 * The get/post variable to is used to identify the number of items to display
247 const PERPAGEVAR
= 'perpage';
250 * The get/post variable that is used to identify the sort field for the table.
253 const SORTVAR
= 'sort';
256 * The get/post variable that is used to identify the sort direction for the table.
259 const SORTDIRECTIONVAR
= 'dir';
262 * The default number of items per page.
265 const DEFAULTPERPAGE
= 100;
268 * The default sort, options are course_enrolment_table::$sortablefields
271 const DEFAULTSORT
= 'lastname';
274 * The default direction
277 const DEFAULTSORTDIRECTION
= 'ASC';
280 * The current page, starting from 0
286 * The total number of pages
292 * The number of items to display per page
298 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
304 * The sort direction, either ASC or DESC
307 public $sortdirection;
310 * The course manager this table is displaying for
311 * @var course_enrolment_manager
316 * The paging bar that controls the paging for this table
319 protected $pagingbar = null;
322 * The total number of users enrolled in the course
325 protected $totalusers = null;
328 * The users enrolled in this course
331 protected $users = null;
334 * The fields for this table
337 protected $fields = array();
340 * An array of bulk user enrolment operations
343 protected $bulkoperations = array();
346 * An array of sortable fields
350 protected static $sortablefields = array('firstname', 'lastname', 'firstnamephonetic', 'lastnamephonetic', 'middlename',
351 'alternatename', 'username', 'idnumber', 'email', 'phone1', 'phone2',
352 'institution', 'department', 'lastaccess', 'lastcourseaccess');
355 * Constructs the table
357 * @param course_enrolment_manager $manager
359 public function __construct(course_enrolment_manager
$manager) {
361 $this->manager
= $manager;
363 $this->page
= optional_param(self
::PAGEVAR
, 0, PARAM_INT
);
364 $this->perpage
= optional_param(self
::PERPAGEVAR
, self
::DEFAULTPERPAGE
, PARAM_INT
);
365 $this->sort
= optional_param(self
::SORTVAR
, self
::DEFAULTSORT
, PARAM_ALPHANUM
);
366 $this->sortdirection
= optional_param(self
::SORTDIRECTIONVAR
, self
::DEFAULTSORTDIRECTION
, PARAM_ALPHA
);
368 $this->attributes
= array('class' => 'userenrolment table-striped');
369 if (!in_array($this->sort
, self
::$sortablefields)) {
370 $this->sort
= self
::DEFAULTSORT
;
372 if ($this->page
< 0) {
375 if ($this->sortdirection
!== 'ASC' && $this->sortdirection
!== 'DESC') {
376 $this->sortdirection
= self
::DEFAULTSORTDIRECTION
;
379 $this->id
= html_writer
::random_id();
381 // Collect the bulk operations for the currently filtered plugin if there is one.
382 $plugin = $manager->get_filtered_enrolment_plugin();
383 if ($plugin and enrol_is_enabled($plugin->get_name())) {
384 $this->bulkoperations
= $plugin->get_bulk_operations($manager);
389 * Returns an array of enrol_user_buttons that are created by the different
390 * enrolment plugins available.
394 public function get_manual_enrol_buttons() {
395 return $this->manager
->get_manual_enrol_buttons();
399 * Gets the sort direction for a given field
401 * @param string $field
402 * @return string ASC or DESC
404 public function get_field_sort_direction($field) {
405 if ($field == $this->sort
) {
406 return ($this->sortdirection
== 'ASC')?
'DESC':'ASC';
408 return self
::DEFAULTSORTDIRECTION
;
412 * Sets the fields for this table. These get added to the tables head as well.
414 * You can also use a multi dimensional array for this to have multiple fields
417 * @param array $fields An array of fields to set
418 * @param string $output
420 public function set_fields($fields, $output) {
421 $this->fields
= $fields;
422 $this->head
= array();
423 $this->colclasses
= array();
424 $this->align
= array();
425 $url = $this->manager
->get_moodlepage()->url
;
427 if (!empty($this->bulkoperations
)) {
428 // If there are bulk operations add a column for checkboxes.
430 $this->colclasses
[] = 'field col_bulkops';
433 foreach ($fields as $name => $label) {
435 if (is_array($label)) {
437 foreach ($label as $n => $l) {
441 if (!in_array($n, self
::$sortablefields)) {
444 $sorturl = new moodle_url($url, array(self
::SORTVAR
=> $n, self
::SORTDIRECTIONVAR
=> $this->get_field_sort_direction($n)));
445 $link = html_writer
::link($sorturl, $fields[$name][$n]);
446 if ($this->sort
== $n) {
447 $link .= $this->get_direction_icon($output, $n);
449 $bits[] = html_writer
::tag('span', $link, array('class'=>'subheading_'.$n));
453 $newlabel = join(' / ', $bits);
455 if (!in_array($name, self
::$sortablefields)) {
458 $sorturl = new moodle_url($url, array(self
::SORTVAR
=> $name, self
::SORTDIRECTIONVAR
=> $this->get_field_sort_direction($name)));
459 $newlabel = html_writer
::link($sorturl, $fields[$name]);
460 if ($this->sort
== $name) {
461 $newlabel .= $this->get_direction_icon($output, $name);
465 $this->head
[] = $newlabel;
466 $this->colclasses
[] = 'field col_'.$name;
470 * Sets the total number of users
472 * @param int $totalusers
474 public function set_total_users($totalusers) {
475 $this->totalusers
= $totalusers;
476 $this->pages
= ceil($this->totalusers
/ $this->perpage
);
477 if ($this->page
> $this->pages
) {
478 $this->page
= $this->pages
;
482 * Sets the users for this table
484 * @param array $users
487 public function set_users(array $users) {
488 $this->users
= $users;
489 $hasbulkops = !empty($this->bulkoperations
);
490 foreach ($users as $userid=>$user) {
491 $user = (array)$user;
492 $row = new html_table_row();
493 $row->attributes
= array('class' => 'userinforow');
494 $row->id
= 'user_'.$userid;
495 $row->cells
= array();
497 // Add a checkbox into the first column.
498 $input = html_writer
::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid));
499 $row->cells
[] = new html_table_cell($input);
501 foreach ($this->fields
as $field => $label) {
502 if (is_array($label)) {
504 foreach (array_keys($label) as $subfield) {
505 if (array_key_exists($subfield, $user)) {
506 $bits[] = html_writer
::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
512 $row->cells
[] = new html_table_cell(join(' ', $bits));
514 if (!array_key_exists($field, $user)) {
515 $user[$field] = ' ';
517 $row->cells
[] = new html_table_cell($user[$field]);
520 $this->data
[] = $row;
524 public function initialise_javascript() {
525 if (has_capability('moodle/role:assign', $this->manager
->get_context())) {
526 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
529 'confirmunassigntitle',
530 'confirmunassignyes',
533 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
534 $function = 'M.enrol.rolemanager.init';
536 'containerId'=>$this->id
,
537 'userIds'=>array_keys($this->users
),
538 'courseId'=>$this->manager
->get_course()->id
,
539 'otherusers'=>isset($this->otherusers
));
540 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));
545 * Gets the paging bar instance for this table
549 public function get_paging_bar() {
550 if ($this->pagingbar
== null) {
551 $this->pagingbar
= new paging_bar($this->totalusers
, $this->page
, $this->perpage
, $this->manager
->get_moodlepage()->url
, self
::PAGEVAR
);
553 return $this->pagingbar
;
557 * Gets the direction icon for the sortable field within this table
559 * @param core_renderer $output
560 * @param string $field
563 protected function get_direction_icon($output, $field) {
564 $direction = self
::DEFAULTSORTDIRECTION
;
565 if ($this->sort
== $field) {
566 $direction = $this->sortdirection
;
568 if ($direction === 'ASC') {
569 return $output->pix_icon('t/sort_asc', get_string('sort'));
571 return $output->pix_icon('t/sort_desc', get_string('sort'));
576 * Gets the params that will need to be added to the url in order to return to this page.
580 public function get_url_params() {
582 self
::PAGEVAR
=> $this->page
,
583 self
::PERPAGEVAR
=> $this->perpage
,
584 self
::SORTVAR
=> $this->sort
,
585 self
::SORTDIRECTIONVAR
=> $this->sortdirection
590 * Returns an array of URL params for both the table and the manager.
594 public function get_combined_url_params() {
595 return $this->get_url_params() +
$this->manager
->get_url_params();
599 * Sets the bulk operations for this table.
601 * @param array $bulkoperations
603 public function set_bulk_user_enrolment_operations(array $bulkoperations) {
604 $this->bulkoperations
= $bulkoperations;
608 * Returns an array of bulk operations.
612 public function get_bulk_user_enrolment_operations() {
613 return $this->bulkoperations
;
617 * Returns true fi the table is aware of any bulk operations that can be performed on users
618 * selected from the currently filtered enrolment plugins.
622 public function has_bulk_user_enrolment_operations() {
623 return !empty($this->bulkoperations
);
628 * Table control used for enrolled users
630 * @copyright 2010 Sam Hemelryk
631 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
633 class course_enrolment_users_table
extends course_enrolment_table
{
638 * Table used for other users
640 * Other users are users who have roles but are not enrolled.
642 * @copyright 2010 Sam Hemelryk
643 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
645 class course_enrolment_other_users_table
extends course_enrolment_table
{
647 public $otherusers = true;
650 * Constructs the table
652 * @param course_enrolment_manager $manager
654 public function __construct(course_enrolment_manager
$manager) {
655 parent
::__construct($manager);
656 $this->attributes
= array('class'=>'userenrolment otheruserenrolment');
660 * Gets a button to search users and assign them roles in the course.
662 * @staticvar int $count
664 * @return single_button
666 public function get_user_search_button() {
668 if (!has_capability('moodle/role:assign', $this->manager
->get_context())) {
672 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager
->get_context()->id
, 'sesskey'=>sesskey()));
673 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
674 $control->class = 'singlebutton assignuserrole instance'.$count;
676 $this->manager
->get_moodlepage()->requires
->strings_for_js(array(
679 'ajaxxmoreusersfound',
685 'errajaxfailedenrol',
693 'enrolperiod'), 'enrol');
694 $this->manager
->get_moodlepage()->requires
->string_for_js('assignrole', 'role');
696 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
697 $function = 'M.enrol.otherusersmanager.init';
699 'courseId'=> $this->manager
->get_course()->id
,
700 'ajaxUrl' => '/enrol/ajax.php',
701 'url' => $this->manager
->get_moodlepage()->url
->out(false));
702 $this->manager
->get_moodlepage()->requires
->yui_module($modules, $function, array($arguments));