MDL-36169 fix enrolled users sorting by alphanumerical column
[moodle.git] / enrol / renderer.php
CommitLineData
a70eb30f
SH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * This is the main renderer for the enrol section.
20 *
5379726a
PS
21 * @package core
22 * @subpackage enrol
23 * @copyright 2010 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a70eb30f
SH
25 */
26
27/**
28 * This is the core renderer
29 *
30 * @copyright 2010 Sam Hemelryk
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33class core_enrol_renderer extends plugin_renderer_base {
34
35 /**
36 * Renders a course enrolment table
37 *
38 * @param course_enrolment_table $table
39 * @return string
40 */
6db3eee0 41 protected function render_course_enrolment_users_table(course_enrolment_users_table $table) {
9fe92ae8 42
61c32b7b 43 $table->initialise_javascript();
9fe92ae8 44
b69ca6be
SH
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);
51 }
52 $buttonhtml .= html_writer::end_tag('div');
a70eb30f 53 }
b69ca6be
SH
54
55 $content = '';
56 if (!empty($buttonhtml)) {
57 $content .= $buttonhtml;
2ec702c9 58 }
b69ca6be 59 $content .= $this->output->render($table->get_enrolment_type_filter());
a70eb30f 60 $content .= $this->output->render($table->get_paging_bar());
75ee207b
SH
61
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') {
68 continue;
69 }
70 $content .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value));
71 }
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()));
80 }
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');
84
85 $content .= html_writer::end_tag('form');
86 } else {
87 $content .= html_writer::table($table);
88 }
a70eb30f 89 $content .= $this->output->render($table->get_paging_bar());
b69ca6be
SH
90 if (!empty($buttonhtml)) {
91 $content .= $buttonhtml;
2ec702c9 92 }
a70eb30f
SH
93 return $content;
94 }
95
b69ca6be
SH
96 /**
97 * Renderers the enrol_user_button.
98 *
99 * @param enrol_user_button $button
100 * @return string XHTML
101 */
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);
107
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);
113 }
114 }
115 $button->initialise_js($this->page);
116
117 // first the input element
118 $output = html_writer::empty_tag('input', $attributes);
119
120 // then hidden fields
121 $params = $button->url->params();
122 if ($button->method === 'post') {
123 $params['sesskey'] = sesskey();
124 }
125 foreach ($params as $var => $val) {
126 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
127 }
128
129 // then div wrapper for xhtml strictness
130 $output = html_writer::tag('div', $output);
131
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
135 } else {
136 $url = $button->url->out_omit_querystring(); // url without params, the anchor part not allowed
137 }
138 if ($url === '') {
139 $url = '#'; // there has to be always some action
140 }
141 $attributes = array('method' => $button->method,
142 'action' => $url,
143 'id' => $button->formid);
144 $output = html_writer::tag('form', $output, $attributes);
145
146 // and finally one more wrapper with class
147 return html_writer::tag('div', $output, array('class' => $button->class));
148 }
149
6db3eee0
SH
150 /**
151 * Renders a course enrolment table
152 *
153 * @param course_enrolment_table $table
154 * @return string
155 */
156 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table $table) {
9fe92ae8 157
61c32b7b 158 $table->initialise_javascript();
9fe92ae8 159
6db3eee0 160 $content = '';
61c32b7b 161 $searchbutton = $table->get_user_search_button();
6db3eee0
SH
162 if ($searchbutton) {
163 $content .= $this->output->render($searchbutton);
164 }
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());
61c32b7b 169 $searchbutton = $table->get_user_search_button();
6db3eee0
SH
170 if ($searchbutton) {
171 $content .= $this->output->render($searchbutton);
172 }
173 return $content;
174 }
175
a70eb30f
SH
176 /**
177 * Generates HTML to display the users roles and any available actions
178 *
179 * @param int $userid
180 * @param array $roles
181 * @param array $assignableroles
182 * @param moodle_url $pageurl
183 * @return string
184 */
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');
188
189 // get list of roles
190 $rolesoutput = '';
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));
197 } else {
198 $rolesoutput .= html_writer::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
199 }
200 }
201 $output = '';
202 if (!empty($assignableroles) && $canassign) {
203 $roleids = array_keys($roles);
204 $hasallroles = true;
205 foreach (array_keys($assignableroles) as $key) {
206 if (!in_array($key, $roleids)) {
207 $hasallroles = false;
208 break;
209 }
210 }
211 if (!$hasallroles) {
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'));
215 }
216 }
217 $output .= html_writer::tag('div', $rolesoutput, array('class'=>'roles'));
218 return $output;
219 }
220
221 /**
222 * Generates the HTML to view the users groups and available group actions
223 *
224 * @param int $userid
225 * @param array $groups
226 * @param array $allgroups
227 * @param bool $canmanagegroups
228 * @param moodle_url $pageurl
229 * @return string
230 */
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');
235
236 $groupoutput = '';
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));
242 } else {
243 $groupoutput .= html_writer::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
244 }
245 }
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'));
251 }
252 return $groupoutput;
253 }
254
255 /**
256 * Generates the HTML for the given enrolments + available actions
257 *
258 * @param int $userid
259 * @param array $enrolments
260 * @param moodle_url $pageurl
261 * @return string
262 */
291215f4 263 public function user_enrolments_and_actions($enrolments) {
a70eb30f 264 $output = '';
291215f4
SH
265 foreach ($enrolments as $ue) {
266 $enrolmentoutput = $ue['text'].' '.$ue['period'];
267 if ($ue['dimmed']) {
a70eb30f 268 $enrolmentoutput = html_writer::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
291215f4
SH
269 } else {
270 $enrolmentoutput = html_writer::tag('span', $enrolmentoutput);
a70eb30f 271 }
291215f4
SH
272 foreach ($ue['actions'] as $action) {
273 $enrolmentoutput .= $this->render($action);
a70eb30f
SH
274 }
275 $output .= html_writer::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
276 }
277 return $output;
278 }
279
291215f4
SH
280 /**
281 * Renders a user enrolment action
282 * @param user_enrolment_action $icon
283 * @return string
284 */
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());
287 }
a70eb30f
SH
288}
289
290/**
291 * Main course enrolment table
292 *
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
297 * for the page.
298 *
299 * @package core
300 * @subpackage enrol
301 * @copyright 2010 Sam Hemelryk
302 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
303 */
304class course_enrolment_table extends html_table implements renderable {
305
306 /**
307 * The get/post variable that is used to identify the page.
308 * Default: page
309 */
310 const PAGEVAR = 'page';
311
312 /**
313 * The get/post variable to is used to identify the number of items to display
314 * per page.
315 * Default: perpage
316 */
317 const PERPAGEVAR = 'perpage';
318
319 /**
320 * The get/post variable that is used to identify the sort field for the table.
321 * Default: sort
322 */
323 const SORTVAR = 'sort';
324
325 /**
326 * The get/post variable that is used to identify the sort direction for the table.
327 * Default: dir
328 */
329 const SORTDIRECTIONVAR = 'dir';
330
331 /**
332 * The default number of items per page.
23cee7a4 333 * Default: 100
a70eb30f 334 */
23cee7a4 335 const DEFAULTPERPAGE = 100;
a70eb30f
SH
336
337 /**
338 * The default sort, options are course_enrolment_table::$sortablefields
339 * Default: lastname
340 */
341 const DEFAULTSORT = 'lastname';
342
343 /**
344 * The default direction
345 * Default: ASC
346 */
347 const DEFAULTSORTDIRECTION = 'ASC';
348
349 /**
350 * The current page, starting from 0
351 * @var int
352 */
353 public $page = 0;
354
355 /**
356 * The total number of pages
357 * @var int
358 */
359 public $pages = 0;
360
361 /**
362 * The number of items to display per page
363 * @var int
364 */
365 public $perpage = 0;
366
a70eb30f
SH
367 /**
368 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
369 * @var string
370 */
371 public $sort;
372
373 /**
374 * The sort direction, either ASC or DESC
375 * @var string
376 */
377 public $sortdirection;
378
379 /**
380 * The course manager this table is displaying for
381 * @var course_enrolment_manager
382 */
383 protected $manager;
384
385 /**
386 * The paging bar that controls the paging for this table
387 * @var paging_bar
388 */
389 protected $pagingbar = null;
390
391 /**
392 * The total number of users enrolled in the course
393 * @var int
394 */
395 protected $totalusers = null;
396
397 /**
398 * The users enrolled in this course
399 * @var array
400 */
401 protected $users = null;
402
403 /**
404 * The fields for this table
405 * @var array
406 */
407 protected $fields = array();
408
75ee207b
SH
409 /**
410 * An array of bulk user enrolment operations
411 * @var array
412 */
413 protected $bulkoperations = array();
414
6db3eee0
SH
415 /**
416 * An array of sortable fields
417 * @static
418 * @var array
419 */
b849c212 420 protected static $sortablefields = array('firstname', 'lastname', 'idnumber', 'email',
421 'phone1', 'phone2', 'institution', 'department' );
a70eb30f
SH
422
423 /**
424 * Constructs the table
425 *
426 * @param course_enrolment_manager $manager
427 */
076995bf 428 public function __construct(course_enrolment_manager $manager) {
a70eb30f 429
61c32b7b 430 $this->manager = $manager;
5379726a 431
61c32b7b
PS
432 $this->page = optional_param(self::PAGEVAR, 0, PARAM_INT);
433 $this->perpage = optional_param(self::PERPAGEVAR, self::DEFAULTPERPAGE, PARAM_INT);
3952908d 434 $this->sort = optional_param(self::SORTVAR, self::DEFAULTSORT, PARAM_ALPHANUM);
a70eb30f
SH
435 $this->sortdirection = optional_param(self::SORTDIRECTIONVAR, self::DEFAULTSORTDIRECTION, PARAM_ALPHA);
436
437 $this->attributes = array('class'=>'userenrolment');
438 if (!in_array($this->sort, self::$sortablefields)) {
439 $this->sort = self::DEFAULTSORT;
440 }
441 if ($this->page < 0) {
442 $this->page = 0;
443 }
444 if ($this->sortdirection !== 'ASC' && $this->sortdirection !== 'DESC') {
445 $this->sortdirection = self::DEFAULTSORTDIRECTION;
446 }
447
448 $this->id = html_writer::random_id();
75ee207b
SH
449
450 // Collect the bulk operations for the currently filtered plugin if there is one.
451 $plugin = $manager->get_filtered_enrolment_plugin();
452 if ($plugin) {
453 $this->bulkoperations = $plugin->get_bulk_operations($manager);
454 }
a70eb30f
SH
455 }
456
b69ca6be
SH
457 /**
458 * Returns an array of enrol_user_buttons that are created by the different
459 * enrolment plugins available.
460 *
461 * @return array
462 */
463 public function get_manual_enrol_buttons() {
464 return $this->manager->get_manual_enrol_buttons();
465 }
466
a70eb30f
SH
467 /**
468 * Gets the sort direction for a given field
469 *
470 * @param string $field
471 * @return string ASC or DESC
472 */
473 public function get_field_sort_direction($field) {
474 if ($field == $this->sort) {
475 return ($this->sortdirection == 'ASC')?'DESC':'ASC';
476 }
477 return self::DEFAULTSORTDIRECTION;
478 }
479
480 /**
481 * Sets the fields for this table. These get added to the tables head as well.
482 *
483 * You can also use a multi dimensional array for this to have multiple fields
484 * in a single column
485 *
486 * @param array $fields An array of fields to set
487 * @param string $output
488 */
9fe92ae8 489 public function set_fields($fields, $output) {
a70eb30f
SH
490 $this->fields = $fields;
491 $this->head = array();
492 $this->colclasses = array();
493 $this->align = array();
076995bf 494 $url = $this->manager->get_moodlepage()->url;
75ee207b
SH
495
496 if (!empty($this->bulkoperations)) {
497 // If there are bulk operations add a column for checkboxes.
498 $this->head[] = '';
499 $this->colclasses[] = 'field col_bulkops';
500 }
501
a70eb30f
SH
502 foreach ($fields as $name => $label) {
503 $newlabel = '';
504 if (is_array($label)) {
505 $bits = array();
506 foreach ($label as $n => $l) {
507 if ($l === false) {
508 continue;
509 }
510 if (!in_array($n, self::$sortablefields)) {
511 $bits[] = $l;
512 } else {
513 $link = html_writer::link(new moodle_url($url, array(self::SORTVAR=>$n)), $fields[$name][$n]);
514 if ($this->sort == $n) {
515 $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 }
517 $bits[] = html_writer::tag('span', $link, array('class'=>'subheading_'.$n));
518
519 }
520 }
521 $newlabel = join(' / ', $bits);
522 } else {
523 if (!in_array($name, self::$sortablefields)) {
524 $newlabel = $label;
525 } else {
526 $newlabel = html_writer::link(new moodle_url($url, array(self::SORTVAR=>$name)), $fields[$name]);
527 if ($this->sort == $name) {
528 $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));
529 }
530 }
531 }
532 $this->head[] = $newlabel;
533 $this->colclasses[] = 'field col_'.$name;
534 }
535 }
536 /**
537 * Sets the total number of users
538 *
539 * @param int $totalusers
540 */
541 public function set_total_users($totalusers) {
542 $this->totalusers = $totalusers;
543 $this->pages = ceil($this->totalusers / $this->perpage);
544 if ($this->page > $this->pages) {
545 $this->page = $this->pages;
546 }
547 }
a70eb30f
SH
548 /**
549 * Sets the users for this table
550 *
551 * @param array $users
61c32b7b 552 * @return void
a70eb30f 553 */
9fe92ae8 554 public function set_users(array $users) {
555 $this->users = $users;
75ee207b 556 $hasbulkops = !empty($this->bulkoperations);
a70eb30f
SH
557 foreach ($users as $userid=>$user) {
558 $user = (array)$user;
559 $row = new html_table_row();
560 $row->attributes = array('class' => 'userinforow');
561 $row->id = 'user_'.$userid;
562 $row->cells = array();
75ee207b
SH
563 if ($hasbulkops) {
564 // Add a checkbox into the first column.
565 $input = html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid));
566 $row->cells[] = new html_table_cell($input);
567 }
a70eb30f
SH
568 foreach ($this->fields as $field => $label) {
569 if (is_array($label)) {
570 $bits = array();
571 foreach (array_keys($label) as $subfield) {
572 if (array_key_exists($subfield, $user)) {
573 $bits[] = html_writer::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
574 }
575 }
576 if (empty($bits)) {
577 $bits[] = '&nbsp;';
578 }
579 $row->cells[] = new html_table_cell(join(' ', $bits));
580 } else {
581 if (!array_key_exists($field, $user)) {
582 $user[$field] = '&nbsp;';
583 }
584 $row->cells[] = new html_table_cell($user[$field]);
585 }
586 }
587 $this->data[] = $row;
588 }
9fe92ae8 589 }
590
61c32b7b 591 public function initialise_javascript() {
a70eb30f 592 if (has_capability('moodle/role:assign', $this->manager->get_context())) {
076995bf 593 $this->manager->get_moodlepage()->requires->strings_for_js(array(
6db3eee0
SH
594 'assignroles',
595 'confirmunassign',
596 'confirmunassigntitle',
597 'confirmunassignyes',
598 'confirmunassignno'
599 ), 'role');
600 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
601 $function = 'M.enrol.rolemanager.init';
602 $arguments = array(
603 'containerId'=>$this->id,
9fe92ae8 604 'userIds'=>array_keys($this->users),
9f15347e
SH
605 'courseId'=>$this->manager->get_course()->id,
606 'otherusers'=>isset($this->otherusers));
076995bf 607 $this->manager->get_moodlepage()->requires->yui_module($modules, $function, array($arguments));
a70eb30f
SH
608 }
609 }
5379726a 610
a70eb30f
SH
611 /**
612 * Gets the paging bar instance for this table
613 *
614 * @return paging_bar
615 */
616 public function get_paging_bar() {
617 if ($this->pagingbar == null) {
076995bf 618 $this->pagingbar = new paging_bar($this->totalusers, $this->page, $this->perpage, $this->manager->get_moodlepage()->url, self::PAGEVAR);
a70eb30f
SH
619 }
620 return $this->pagingbar;
621 }
622
623 /**
624 * Gets the direction icon for the sortable field within this table
625 *
626 * @param core_renderer $output
627 * @param string $field
628 * @return string
629 */
630 protected function get_direction_icon($output, $field) {
631 $direction = self::DEFAULTSORTDIRECTION;
632 if ($this->sort == $field) {
633 $direction = $this->sortdirection;
634 }
635 if ($direction === 'ASC') {
636 return html_writer::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/down')));
637 } else {
638 return html_writer::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/up')));
639 }
640 }
641
642 /**
643 * Gets the params that will need to be added to the url in order to return to this page.
644 *
645 * @return array
646 */
647 public function get_url_params() {
648 return array(
649 self::PAGEVAR => $this->page,
650 self::PERPAGEVAR => $this->perpage,
651 self::SORTVAR => $this->sort,
652 self::SORTDIRECTIONVAR => $this->sortdirection
653 );
654 }
75ee207b
SH
655
656 /**
657 * Returns an array of URL params for both the table and the manager.
658 *
659 * @return array
660 */
661 public function get_combined_url_params() {
662 return $this->get_url_params() + $this->manager->get_url_params();
663 }
664
665 /**
666 * Sets the bulk operations for this table.
667 *
668 * @param array $bulkoperations
669 */
670 public function set_bulk_user_enrolment_operations(array $bulkoperations) {
671 $this->bulkoperations = $bulkoperations;
672 }
673
674 /**
675 * Returns an array of bulk operations.
676 *
677 * @return array
678 */
679 public function get_bulk_user_enrolment_operations() {
680 return $this->bulkoperations;
681 }
682
683 /**
684 * Returns true fi the table is aware of any bulk operations that can be performed on users
685 * selected from the currently filtered enrolment plugins.
686 *
687 * @return bool
688 */
689 public function has_bulk_user_enrolment_operations() {
690 return !empty($this->bulkoperations);
691 }
6db3eee0
SH
692}
693
694/**
695 * Table control used for enrolled users
696 *
697 * @copyright 2010 Sam Hemelryk
698 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
699 */
700class course_enrolment_users_table extends course_enrolment_table {
a70eb30f
SH
701
702 /**
6db3eee0
SH
703 * An array of sortable fields
704 * @static
705 * @var array
a70eb30f 706 */
6db3eee0 707 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
a70eb30f 708
6db3eee0
SH
709 /**
710 * Gets the enrolment type filter control for this table
711 *
712 * @return single_select
713 */
714 public function get_enrolment_type_filter() {
076995bf 715 $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());
6db3eee0
SH
716 $selector->set_label( get_string('enrolmentinstances', 'enrol'));
717 return $selector;
718 }
719}
720
721/**
722 * Table used for other users
723 *
724 * Other users are users who have roles but are not enrolled.
725 *
726 * @copyright 2010 Sam Hemelryk
727 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
728 */
729class course_enrolment_other_users_table extends course_enrolment_table {
730
9f15347e
SH
731 public $otherusers = true;
732
6db3eee0
SH
733 /**
734 * Constructs the table
735 *
736 * @param course_enrolment_manager $manager
6db3eee0 737 */
076995bf
SH
738 public function __construct(course_enrolment_manager $manager) {
739 parent::__construct($manager);
6db3eee0
SH
740 $this->attributes = array('class'=>'userenrolment otheruserenrolment');
741 }
742
743 /**
744 * Gets a button to search users and assign them roles in the course.
745 *
746 * @staticvar int $count
747 * @param int $page
748 * @return single_button
749 */
61c32b7b 750 public function get_user_search_button() {
6db3eee0
SH
751 static $count = 0;
752 if (!has_capability('moodle/role:assign', $this->manager->get_context())) {
753 return false;
754 }
755 $count++;
61c32b7b 756 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager->get_context()->id, 'sesskey'=>sesskey()));
6db3eee0
SH
757 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
758 $control->class = 'singlebutton assignuserrole instance'.$count;
759 if ($count == 1) {
076995bf 760 $this->manager->get_moodlepage()->requires->strings_for_js(array(
6db3eee0
SH
761 'ajaxoneuserfound',
762 'ajaxxusersfound',
763 'ajaxnext25',
764 'enrol',
765 'enrolmentoptions',
766 'enrolusers',
767 'errajaxfailedenrol',
768 'errajaxsearch',
769 'none',
770 'usersearch',
771 'unlimitedduration',
772 'startdatetoday',
773 'durationdays',
774 'enrolperiod'), 'enrol');
076995bf 775 $this->manager->get_moodlepage()->requires->string_for_js('assignrole', 'role');
6db3eee0
SH
776
777 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
778 $function = 'M.enrol.otherusersmanager.init';
6db3eee0
SH
779 $arguments = array(
780 'courseId'=> $this->manager->get_course()->id,
781 'ajaxUrl' => '/enrol/ajax.php',
076995bf
SH
782 'url' => $this->manager->get_moodlepage()->url->out(false));
783 $this->manager->get_moodlepage()->requires->yui_module($modules, $function, array($arguments));
6db3eee0
SH
784 }
785 return $control;
786 }
2ec702c9 787}