MDL-18162 mod_forum: Correct @param type in phpdoc to use stdClass
[moodle.git] / enrol / renderer.php
blob2a2ee1f8caa1baf468bb8009e2f27f1bed9f073d
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * This is the main renderer for the enrol section.
20 * @package core_enrol
21 * @copyright 2010 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 /**
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 {
33 /**
34 * Renders a course enrolment table
36 * @param course_enrolment_table $table
37 * @param moodleform $mform Form that contains filter controls
38 * @return string
40 public function render_course_enrolment_users_table(course_enrolment_users_table $table,
41 moodleform $mform) {
43 $table->initialise_javascript();
45 $buttons = $table->get_manual_enrol_buttons();
46 $buttonhtml = '';
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');
55 $content = '';
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') {
69 continue;
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');
87 } else {
88 $content .= html_writer::table($table);
90 $content .= $this->output->render($table->get_paging_bar());
91 if (!empty($buttonhtml)) {
92 $content .= $buttonhtml;
94 return $content;
97 /**
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
136 } else {
137 $url = $button->url->out_omit_querystring(); // url without params, the anchor part not allowed
139 if ($url === '') {
140 $url = '#'; // there has to be always some action
142 $attributes = array('method' => $button->method,
143 'action' => $url,
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
155 * @return string
157 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table $table) {
159 $table->initialise_javascript();
161 $content = '';
162 $searchbutton = $table->get_user_search_button();
163 if ($searchbutton) {
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();
171 if ($searchbutton) {
172 $content .= $this->output->render($searchbutton);
174 return $content;
178 * Generates HTML to display the users roles and any available actions
180 * @param int $userid
181 * @param array $roles
182 * @param array $assignableroles
183 * @param moodle_url $pageurl
184 * @return string
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');
190 // get list of roles
191 $rolesoutput = '';
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', 'roleid'=>$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));
198 } else {
199 $rolesoutput .= html_writer::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
202 $output = '';
203 if (!empty($assignableroles) && $canassign) {
204 $roleids = array_keys($roles);
205 $hasallroles = true;
206 foreach (array_keys($assignableroles) as $key) {
207 if (!in_array($key, $roleids)) {
208 $hasallroles = false;
209 break;
212 if (!$hasallroles) {
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'));
219 return $output;
223 * Generates the HTML to view the users groups and available group actions
225 * @param int $userid
226 * @param array $groups
227 * @param array $allgroups
228 * @param bool $canmanagegroups
229 * @param moodle_url $pageurl
230 * @return string
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');
237 $groupoutput = '';
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));
243 } else {
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'));
253 return $groupoutput;
257 * Generates the HTML for the given enrolments + available actions
259 * @param int $userid
260 * @param array $enrolments
261 * @param moodle_url $pageurl
262 * @return string
264 public function user_enrolments_and_actions($enrolments) {
265 $output = '';
266 foreach ($enrolments as $ue) {
267 $enrolmentoutput = $ue['text'].' '.$ue['period'];
268 if ($ue['dimmed']) {
269 $enrolmentoutput = html_writer::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
270 } else {
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'));
278 return $output;
282 * Renders a user enrolment action
283 * @param user_enrolment_action $icon
284 * @return string
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
298 * for the page.
300 * @package core
301 * @subpackage enrol
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.
309 * Default: page
311 const PAGEVAR = 'page';
314 * The get/post variable to is used to identify the number of items to display
315 * per page.
316 * Default: perpage
318 const PERPAGEVAR = 'perpage';
321 * The get/post variable that is used to identify the sort field for the table.
322 * Default: sort
324 const SORTVAR = 'sort';
327 * The get/post variable that is used to identify the sort direction for the table.
328 * Default: dir
330 const SORTDIRECTIONVAR = 'dir';
333 * The default number of items per page.
334 * Default: 100
336 const DEFAULTPERPAGE = 100;
339 * The default sort, options are course_enrolment_table::$sortablefields
340 * Default: lastname
342 const DEFAULTSORT = 'lastname';
345 * The default direction
346 * Default: ASC
348 const DEFAULTSORTDIRECTION = 'ASC';
351 * The current page, starting from 0
352 * @var int
354 public $page = 0;
357 * The total number of pages
358 * @var int
360 public $pages = 0;
363 * The number of items to display per page
364 * @var int
366 public $perpage = 0;
369 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
370 * @var string
372 public $sort;
375 * The sort direction, either ASC or DESC
376 * @var string
378 public $sortdirection;
381 * The course manager this table is displaying for
382 * @var course_enrolment_manager
384 protected $manager;
387 * The paging bar that controls the paging for this table
388 * @var paging_bar
390 protected $pagingbar = null;
393 * The total number of users enrolled in the course
394 * @var int
396 protected $totalusers = null;
399 * The users enrolled in this course
400 * @var array
402 protected $users = null;
405 * The fields for this table
406 * @var array
408 protected $fields = array();
411 * An array of bulk user enrolment operations
412 * @var array
414 protected $bulkoperations = array();
417 * An array of sortable fields
418 * @static
419 * @var array
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) {
443 $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.
462 * @return array
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
485 * in a single column
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.
499 $this->head[] = '';
500 $this->colclasses[] = 'field col_bulkops';
503 foreach ($fields as $name => $label) {
504 $newlabel = '';
505 if (is_array($label)) {
506 $bits = array();
507 foreach ($label as $n => $l) {
508 if ($l === false) {
509 continue;
511 if (!in_array($n, self::$sortablefields)) {
512 $bits[] = $l;
513 } else {
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);
523 } else {
524 if (!in_array($name, self::$sortablefields)) {
525 $newlabel = $label;
526 } else {
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
553 * @return void
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();
564 if ($hasbulkops) {
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)) {
571 $bits = array();
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));
577 if (empty($bits)) {
578 $bits[] = '&nbsp;';
580 $row->cells[] = new html_table_cell(join(' ', $bits));
581 } else {
582 if (!array_key_exists($field, $user)) {
583 $user[$field] = '&nbsp;';
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(
595 'assignroles',
596 'confirmunassign',
597 'confirmunassigntitle',
598 'confirmunassignyes',
599 'confirmunassignno'
600 ), 'role');
601 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
602 $function = 'M.enrol.rolemanager.init';
603 $arguments = array(
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
615 * @return paging_bar
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
629 * @return string
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')));
639 } else {
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.
648 * @return array
650 public function get_url_params() {
651 return array(
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.
662 * @return array
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.
680 * @return array
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.
690 * @return bool
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
707 * @static
708 * @var array
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
739 * @param int $page
740 * @return single_button
742 public function get_user_search_button() {
743 static $count = 0;
744 if (!has_capability('moodle/role:assign', $this->manager->get_context())) {
745 return false;
747 $count++;
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;
751 if ($count == 1) {
752 $this->manager->get_moodlepage()->requires->strings_for_js(array(
753 'ajaxoneuserfound',
754 'ajaxxusersfound',
755 'ajaxnext25',
756 'enrol',
757 'enrolmentoptions',
758 'enrolusers',
759 'errajaxfailedenrol',
760 'errajaxsearch',
761 'none',
762 'usersearch',
763 'unlimitedduration',
764 'startdatetoday',
765 'durationdays',
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';
771 $arguments = array(
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));
777 return $control;