MDL-21437 data - checkbox and multimenu fields search working + require all too
[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
43 $table->initialise_javascript($this->page);
44
a70eb30f
SH
45 $content = '';
46 $enrolmentselector = $table->get_enrolment_selector($this->page);
47 if ($enrolmentselector) {
48 $content .= $this->output->render($enrolmentselector);
49 }
2ec702c9
SH
50 $cohortenroller = $table->get_cohort_enrolment_control($this->page);
51 if ($cohortenroller) {
52 $content .= $this->output->render($cohortenroller);
53 }
a70eb30f
SH
54 $content .= $this->output->render($table->get_enrolment_type_filter());
55 $content .= $this->output->render($table->get_paging_bar());
56 $content .= html_writer::table($table);
57 $content .= $this->output->render($table->get_paging_bar());
58 $enrolmentselector = $table->get_enrolment_selector($this->page);
59 if ($enrolmentselector) {
60 $content .= $this->output->render($enrolmentselector);
61 }
2ec702c9
SH
62 $cohortenroller = $table->get_cohort_enrolment_control($this->page);
63 if ($cohortenroller) {
64 $content .= $this->output->render($cohortenroller);
65 }
a70eb30f
SH
66 return $content;
67 }
68
6db3eee0
SH
69 /**
70 * Renders a course enrolment table
71 *
72 * @param course_enrolment_table $table
73 * @return string
74 */
75 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table $table) {
9fe92ae8 76
77 $table->initialise_javascript($this->page);
78
6db3eee0
SH
79 $content = '';
80 $searchbutton = $table->get_user_search_button($this->page);
81 if ($searchbutton) {
82 $content .= $this->output->render($searchbutton);
83 }
84 $content .= html_writer::tag('div', get_string('otheruserdesc', 'enrol'), array('class'=>'otherusersdesc'));
85 $content .= $this->output->render($table->get_paging_bar());
86 $content .= html_writer::table($table);
87 $content .= $this->output->render($table->get_paging_bar());
88 $searchbutton = $table->get_user_search_button($this->page);
89 if ($searchbutton) {
90 $content .= $this->output->render($searchbutton);
91 }
92 return $content;
93 }
94
a70eb30f
SH
95 /**
96 * Generates HTML to display the users roles and any available actions
97 *
98 * @param int $userid
99 * @param array $roles
100 * @param array $assignableroles
101 * @param moodle_url $pageurl
102 * @return string
103 */
104 public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) {
105 $iconenroladd = $this->output->pix_url('t/enroladd');
106 $iconenrolremove = $this->output->pix_url('t/delete');
107
108 // get list of roles
109 $rolesoutput = '';
110 foreach ($roles as $roleid=>$role) {
111 if ($canassign && !$role['unchangeable']) {
112 $strunassign = get_string('unassignarole', 'role', $role['text']);
113 $icon = html_writer::empty_tag('img', array('alt'=>$strunassign, 'src'=>$iconenrolremove));
114 $url = new moodle_url($pageurl, array('action'=>'unassign', 'role'=>$roleid, 'user'=>$userid));
115 $rolesoutput .= html_writer::tag('div', $role['text'] . html_writer::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid));
116 } else {
117 $rolesoutput .= html_writer::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
118 }
119 }
120 $output = '';
121 if (!empty($assignableroles) && $canassign) {
122 $roleids = array_keys($roles);
123 $hasallroles = true;
124 foreach (array_keys($assignableroles) as $key) {
125 if (!in_array($key, $roleids)) {
126 $hasallroles = false;
127 break;
128 }
129 }
130 if (!$hasallroles) {
131 $url = new moodle_url($pageurl, array('action'=>'assign', 'user'=>$userid));
132 $icon = html_writer::empty_tag('img', array('alt'=>get_string('assignroles', 'role'), 'src'=>$iconenroladd));
133 $output = html_writer::tag('div', html_writer::link($url, $icon, array('class'=>'assignrolelink', 'title'=>get_string('assignroles', 'role'))), array('class'=>'addrole'));
134 }
135 }
136 $output .= html_writer::tag('div', $rolesoutput, array('class'=>'roles'));
137 return $output;
138 }
139
140 /**
141 * Generates the HTML to view the users groups and available group actions
142 *
143 * @param int $userid
144 * @param array $groups
145 * @param array $allgroups
146 * @param bool $canmanagegroups
147 * @param moodle_url $pageurl
148 * @return string
149 */
150 public function user_groups_and_actions($userid, $groups, $allgroups, $canmanagegroups, $pageurl) {
151 $iconenroladd = $this->output->pix_url('t/enroladd');
152 $iconenrolremove = $this->output->pix_url('t/delete');
153 $straddgroup = get_string('addgroup', 'group');
154
155 $groupoutput = '';
156 foreach($groups as $groupid=>$name) {
157 if ($canmanagegroups) {
158 $icon = html_writer::empty_tag('img', array('alt'=>get_string('removefromgroup', 'group', $name), 'src'=>$iconenrolremove));
159 $url = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid));
160 $groupoutput .= html_writer::tag('div', $name . html_writer::link($url, $icon), array('class'=>'group', 'rel'=>$groupid));
161 } else {
162 $groupoutput .= html_writer::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
163 }
164 }
165 $groupoutput = html_writer::tag('div', $groupoutput, array('class'=>'groups'));
166 if ($canmanagegroups && (count($groups) < count($allgroups))) {
167 $icon = html_writer::empty_tag('img', array('alt'=>$straddgroup, 'src'=>$iconenroladd));
168 $url = new moodle_url($pageurl, array('action'=>'addmember', 'user'=>$userid));
169 $groupoutput .= html_writer::tag('div', html_writer::link($url, $icon), array('class'=>'addgroup'));
170 }
171 return $groupoutput;
172 }
173
174 /**
175 * Generates the HTML for the given enrolments + available actions
176 *
177 * @param int $userid
178 * @param array $enrolments
179 * @param moodle_url $pageurl
180 * @return string
181 */
182 public function user_enrolments_and_actions($userid, $enrolments, $pageurl) {
183 $iconedit = $this->output->pix_url('t/edit');
184 $iconenrolremove = $this->output->pix_url('t/delete');
185 $strunenrol = get_string('unenrol', 'enrol');
186 $stredit = get_string('edit');
187
188 $output = '';
189 foreach ($enrolments as $ueid=>$enrolment) {
190 $enrolmentoutput = $enrolment['text'].' '.$enrolment['period'];
191 if ($enrolment['dimmed']) {
192 $enrolmentoutput = html_writer::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
193 }
194 if ($enrolment['canunenrol']) {
195 $icon = html_writer::empty_tag('img', array('alt'=>$strunenrol, 'src'=>$iconenrolremove));
196 $url = new moodle_url($pageurl, array('action'=>'unenrol', 'ue'=>$ueid));
197 $enrolmentoutput .= html_writer::link($url, $icon, array('class'=>'unenrollink', 'rel'=>$ueid));
198 }
199 if ($enrolment['canmanage']) {
200 $icon = html_writer::empty_tag('img', array('alt'=>$stredit, 'src'=>$iconedit));
201 $url = new moodle_url($url, array('action'=>'edit', 'ue'=>$ueid));
202 $enrolmentoutput .= html_writer::link($url, $icon, array('class'=>'editenrollink', 'rel'=>$ueid));
203 }
204 $output .= html_writer::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
205 }
206 return $output;
207 }
208
209}
210
211/**
212 * Main course enrolment table
213 *
214 * This table is used to display the enrolment information for a course.
215 * It requires that a course enrolment manager be provided during constuct with
216 * provides all of the information for the table.
217 * The control then produces the table, the paging, and the associated JS actions
218 * for the page.
219 *
220 * @package core
221 * @subpackage enrol
222 * @copyright 2010 Sam Hemelryk
223 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
224 */
225class course_enrolment_table extends html_table implements renderable {
226
227 /**
228 * The get/post variable that is used to identify the page.
229 * Default: page
230 */
231 const PAGEVAR = 'page';
232
233 /**
234 * The get/post variable to is used to identify the number of items to display
235 * per page.
236 * Default: perpage
237 */
238 const PERPAGEVAR = 'perpage';
239
240 /**
241 * The get/post variable that is used to identify the sort field for the table.
242 * Default: sort
243 */
244 const SORTVAR = 'sort';
245
246 /**
247 * The get/post variable that is used to identify the sort direction for the table.
248 * Default: dir
249 */
250 const SORTDIRECTIONVAR = 'dir';
251
252 /**
253 * The default number of items per page.
23cee7a4 254 * Default: 100
a70eb30f 255 */
23cee7a4 256 const DEFAULTPERPAGE = 100;
a70eb30f
SH
257
258 /**
259 * The default sort, options are course_enrolment_table::$sortablefields
260 * Default: lastname
261 */
262 const DEFAULTSORT = 'lastname';
263
264 /**
265 * The default direction
266 * Default: ASC
267 */
268 const DEFAULTSORTDIRECTION = 'ASC';
269
270 /**
271 * The current page, starting from 0
272 * @var int
273 */
274 public $page = 0;
275
276 /**
277 * The total number of pages
278 * @var int
279 */
280 public $pages = 0;
281
282 /**
283 * The number of items to display per page
284 * @var int
285 */
286 public $perpage = 0;
287
288 /**
289 * The URL of the page for this table
290 * @var moodle_url
291 */
292 public $pageurl;
293
294 /**
295 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
296 * @var string
297 */
298 public $sort;
299
300 /**
301 * The sort direction, either ASC or DESC
302 * @var string
303 */
304 public $sortdirection;
305
306 /**
307 * The course manager this table is displaying for
308 * @var course_enrolment_manager
309 */
310 protected $manager;
311
312 /**
313 * The paging bar that controls the paging for this table
314 * @var paging_bar
315 */
316 protected $pagingbar = null;
317
318 /**
319 * The total number of users enrolled in the course
320 * @var int
321 */
322 protected $totalusers = null;
323
324 /**
325 * The users enrolled in this course
326 * @var array
327 */
328 protected $users = null;
329
330 /**
331 * The fields for this table
332 * @var array
333 */
334 protected $fields = array();
335
6db3eee0
SH
336 /**
337 * An array of sortable fields
338 * @static
339 * @var array
340 */
341 protected static $sortablefields = array('firstname', 'lastname', 'email');
a70eb30f
SH
342
343 /**
344 * Constructs the table
345 *
346 * @param course_enrolment_manager $manager
347 */
348 public function __construct(course_enrolment_manager $manager, moodle_url $pageurl) {
349
350 $this->manager = $manager;
351 $this->pageurl = $pageurl;
5379726a 352
a70eb30f
SH
353 $this->page = optional_param(self::PAGEVAR, 0, PARAM_INT);
354 $this->perpage = optional_param(self::PERPAGEVAR, self::DEFAULTPERPAGE, PARAM_INT);
355 $this->sort = optional_param(self::SORTVAR, self::DEFAULTSORT, PARAM_ALPHA);
356 $this->sortdirection = optional_param(self::SORTDIRECTIONVAR, self::DEFAULTSORTDIRECTION, PARAM_ALPHA);
357
358 $this->attributes = array('class'=>'userenrolment');
359 if (!in_array($this->sort, self::$sortablefields)) {
360 $this->sort = self::DEFAULTSORT;
361 }
362 if ($this->page < 0) {
363 $this->page = 0;
364 }
365 if ($this->sortdirection !== 'ASC' && $this->sortdirection !== 'DESC') {
366 $this->sortdirection = self::DEFAULTSORTDIRECTION;
367 }
368
369 $this->id = html_writer::random_id();
a70eb30f
SH
370 }
371
372 /**
373 * Gets the sort direction for a given field
374 *
375 * @param string $field
376 * @return string ASC or DESC
377 */
378 public function get_field_sort_direction($field) {
379 if ($field == $this->sort) {
380 return ($this->sortdirection == 'ASC')?'DESC':'ASC';
381 }
382 return self::DEFAULTSORTDIRECTION;
383 }
384
385 /**
386 * Sets the fields for this table. These get added to the tables head as well.
387 *
388 * You can also use a multi dimensional array for this to have multiple fields
389 * in a single column
390 *
391 * @param array $fields An array of fields to set
392 * @param string $output
393 */
9fe92ae8 394 public function set_fields($fields, $output) {
a70eb30f
SH
395 $this->fields = $fields;
396 $this->head = array();
397 $this->colclasses = array();
398 $this->align = array();
399 $url = new moodle_url($this->pageurl, $this->get_url_params()+$this->manager->get_url_params());
400 foreach ($fields as $name => $label) {
401 $newlabel = '';
402 if (is_array($label)) {
403 $bits = array();
404 foreach ($label as $n => $l) {
405 if ($l === false) {
406 continue;
407 }
408 if (!in_array($n, self::$sortablefields)) {
409 $bits[] = $l;
410 } else {
411 $link = html_writer::link(new moodle_url($url, array(self::SORTVAR=>$n)), $fields[$name][$n]);
412 if ($this->sort == $n) {
413 $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));
414 }
415 $bits[] = html_writer::tag('span', $link, array('class'=>'subheading_'.$n));
416
417 }
418 }
419 $newlabel = join(' / ', $bits);
420 } else {
421 if (!in_array($name, self::$sortablefields)) {
422 $newlabel = $label;
423 } else {
424 $newlabel = html_writer::link(new moodle_url($url, array(self::SORTVAR=>$name)), $fields[$name]);
425 if ($this->sort == $name) {
426 $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));
427 }
428 }
429 }
430 $this->head[] = $newlabel;
431 $this->colclasses[] = 'field col_'.$name;
432 }
433 }
434 /**
435 * Sets the total number of users
436 *
437 * @param int $totalusers
438 */
439 public function set_total_users($totalusers) {
440 $this->totalusers = $totalusers;
441 $this->pages = ceil($this->totalusers / $this->perpage);
442 if ($this->page > $this->pages) {
443 $this->page = $this->pages;
444 }
445 }
446 /**
5379726a 447
a70eb30f
SH
448 */
449 /**
450 * Sets the users for this table
451 *
452 * @param array $users
453 * @param moodle_page $page
454 */
9fe92ae8 455 public function set_users(array $users) {
456 $this->users = $users;
a70eb30f
SH
457 foreach ($users as $userid=>$user) {
458 $user = (array)$user;
459 $row = new html_table_row();
460 $row->attributes = array('class' => 'userinforow');
461 $row->id = 'user_'.$userid;
462 $row->cells = array();
463 foreach ($this->fields as $field => $label) {
464 if (is_array($label)) {
465 $bits = array();
466 foreach (array_keys($label) as $subfield) {
467 if (array_key_exists($subfield, $user)) {
468 $bits[] = html_writer::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
469 }
470 }
471 if (empty($bits)) {
472 $bits[] = '&nbsp;';
473 }
474 $row->cells[] = new html_table_cell(join(' ', $bits));
475 } else {
476 if (!array_key_exists($field, $user)) {
477 $user[$field] = '&nbsp;';
478 }
479 $row->cells[] = new html_table_cell($user[$field]);
480 }
481 }
482 $this->data[] = $row;
483 }
9fe92ae8 484 }
485
486 public function initialise_javascript(moodle_page $page) {
a70eb30f 487 if (has_capability('moodle/role:assign', $this->manager->get_context())) {
6db3eee0
SH
488 $page->requires->strings_for_js(array(
489 'assignroles',
490 'confirmunassign',
491 'confirmunassigntitle',
492 'confirmunassignyes',
493 'confirmunassignno'
494 ), 'role');
495 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
496 $function = 'M.enrol.rolemanager.init';
497 $arguments = array(
498 'containerId'=>$this->id,
9fe92ae8 499 'userIds'=>array_keys($this->users),
9f15347e
SH
500 'courseId'=>$this->manager->get_course()->id,
501 'otherusers'=>isset($this->otherusers));
6db3eee0 502 $page->requires->yui_module($modules, $function, array($arguments));
a70eb30f
SH
503 }
504 }
5379726a 505
a70eb30f
SH
506 /**
507 * Gets the paging bar instance for this table
508 *
509 * @return paging_bar
510 */
511 public function get_paging_bar() {
512 if ($this->pagingbar == null) {
513 $this->pagingbar = new paging_bar($this->totalusers, $this->page, $this->perpage, $this->pageurl, self::PAGEVAR);
514 }
515 return $this->pagingbar;
516 }
517
518 /**
519 * Gets the direction icon for the sortable field within this table
520 *
521 * @param core_renderer $output
522 * @param string $field
523 * @return string
524 */
525 protected function get_direction_icon($output, $field) {
526 $direction = self::DEFAULTSORTDIRECTION;
527 if ($this->sort == $field) {
528 $direction = $this->sortdirection;
529 }
530 if ($direction === 'ASC') {
531 return html_writer::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/down')));
532 } else {
533 return html_writer::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/up')));
534 }
535 }
536
537 /**
538 * Gets the params that will need to be added to the url in order to return to this page.
539 *
540 * @return array
541 */
542 public function get_url_params() {
543 return array(
544 self::PAGEVAR => $this->page,
545 self::PERPAGEVAR => $this->perpage,
546 self::SORTVAR => $this->sort,
547 self::SORTDIRECTIONVAR => $this->sortdirection
548 );
549 }
6db3eee0
SH
550}
551
552/**
553 * Table control used for enrolled users
554 *
555 * @copyright 2010 Sam Hemelryk
556 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
557 */
558class course_enrolment_users_table extends course_enrolment_table {
a70eb30f
SH
559
560 /**
6db3eee0
SH
561 * An array of sortable fields
562 * @static
563 * @var array
a70eb30f 564 */
6db3eee0 565 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
a70eb30f 566
2ec702c9
SH
567 /**
568 * Returns a button to enrol cohorts or thier users
569 *
570 * @staticvar int $count
571 * @param moodle_page $page
572 * @return single_button|false
573 */
574 public function get_cohort_enrolment_control(moodle_page $page) {
575 static $count = 0;
576
577 // First make sure that cohorts is enabled
578 $plugins = $this->manager->get_enrolment_plugins();
579 if (!array_key_exists('cohort', $plugins)) {
580 return false;
581 }
2ec702c9 582 $course = $this->manager->get_course();
6aae3712
PS
583 if (!$plugins['cohort']->get_newinstance_link($course->id)) {
584 // user can not see any cohort === can not use this
585 return false;
586 }
587 $count ++;
2ec702c9
SH
588 $cohorturl = new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$course->id));
589 $control = new single_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
590 $control->class = 'singlebutton enrolcohortbutton instance'.$count;
591 $control->formid = 'manuallyenrol_single_'+$count;
592 if ($count == 1) {
593 $page->requires->strings_for_js(array('enrol','synced','enrolcohort','enrolcohortusers'), 'enrol');
594 $page->requires->string_for_js('assignroles', 'role');
595 $page->requires->string_for_js('cohort', 'cohort');
596 $page->requires->string_for_js('users', 'moodle');
597 $url = new moodle_url($this->pageurl, $this->manager->get_url_params()+$this->get_url_params());
598
599 $hasmanualinstance = false;
600 // No point showing this at all if the user cant manually enrol users
601 if (has_capability('enrol/manual:manage', $this->manager->get_context())) {
602 // Make sure manual enrolments instance exists
603 $instances = $this->manager->get_enrolment_instances();
604 foreach ($instances as $instance) {
605 if ($instance->enrol == 'manual') {
606 $hasmanualinstance = true;
607 break;
608 }
609 }
610 }
6db3eee0
SH
611
612 $modules = array('moodle-enrol-quickcohortenrolment', 'moodle-enrol-quickcohortenrolment-skin');
613 $function = 'M.enrol.quickcohortenrolment.init';
614 $arguments = array(
2ec702c9
SH
615 'courseid'=>$course->id,
616 'ajaxurl'=>'/enrol/ajax.php',
617 'url'=>$url->out(false),
6db3eee0
SH
618 'manualEnrolment'=>$hasmanualinstance);
619 $page->requires->yui_module($modules, $function, array($arguments));
2ec702c9
SH
620 }
621 return $control;
622 }
623
a70eb30f
SH
624 /**
625 * Gets the enrolment selector control for this table and initialises its
626 * JavaScript
627 *
628 * @return single_button|url_select
629 */
630 public function get_enrolment_selector(moodle_page $page) {
631 static $count = 0;
632
633 $instances = $this->manager->get_enrolment_instances();
634 $plugins = $this->manager->get_enrolment_plugins();
635 // print enrol link or selection
636 $links = array();
637 foreach($instances as $instance) {
638 $plugin = $plugins[$instance->enrol];
639 if ($link = $plugin->get_manual_enrol_link($instance)) {
640 $links[$instance->id] = $link;
641 }
642 }
643 if (!empty($links)) {
644 $arguments = array();
645 $count ++;
646 if (count($links) == 1) {
647 $control = new single_button(reset($links), get_string('enrolusers', 'enrol_manual'), 'get');
648 $control->class = 'singlebutton enrolusersbutton instance'.$count;
649 $control->formid = 'manuallyenrol_single_'+$count;
650 $arguments[] = array('id'=>key($links), 'name'=>$plugins[$instances[key($links)]->enrol]->get_instance_name($instances[key($links)]));
651 } else if (count($links) > 1) {
652 $inames = $this->manager->get_enrolment_instance_names();
653 $options = array();
654 foreach ($links as $i=>$link) {
655 $options[$link->out(false)] = $inames[$i];
656 $arguments[] = array('id'=>$i, 'name'=>$plugins[$instances[$i]->enrol]->get_instance_name($instances[$i]));
657 }
658 $control = new url_select($options, '', array(''=>get_string('enrolusers', 'enrol_manual').'...'));
659 $control->class = 'singlebutton enrolusersbutton instance'.$count;
660 $control->formid = 'manuallyenrol_select_'+$count;
661 }
662 $course = $this->manager->get_course();
663 $url = new moodle_url($this->pageurl, $this->manager->get_url_params()+$this->get_url_params());
664 $timeformat = get_string('strftimedatefullshort');
665 $today = time();
666 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
667 $startdateoptions = array();
668 if ($course->startdate > 0) {
669 $startdateoptions[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $timeformat) . ')';
670 }
671 $startdateoptions[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
672
673 if ($count == 1) {
674 $page->requires->strings_for_js(array(
675 'ajaxoneuserfound',
676 'ajaxxusersfound',
677 'ajaxnext25',
678 'enrol',
679 'enrolmentoptions',
680 'enrolusers',
681 'errajaxfailedenrol',
682 'errajaxsearch',
683 'none',
684 'usersearch',
685 'unlimitedduration',
686 'startdatetoday',
687 'durationdays',
688 'enrolperiod'), 'enrol');
689 $page->requires->string_for_js('assignroles', 'role');
690 $page->requires->string_for_js('startingfrom', 'moodle');
691
6db3eee0
SH
692 $modules = array('moodle-enrol-enrolmentmanager', 'moodle-enrol-enrolmentmanager-skin');
693 $function = 'M.enrol.enrolmentmanager.init';
694 $arguments = array(
a70eb30f
SH
695 'instances'=>$arguments,
696 'courseid'=>$course->id,
697 'ajaxurl'=>'/enrol/ajax.php',
698 'url'=>$url->out(false),
23cee7a4 699 'optionsStartDate'=>$startdateoptions,
6db3eee0
SH
700 'defaultRole'=>get_config('enrol_manual', 'roleid'));
701 $page->requires->yui_module($modules, $function, array($arguments));
a70eb30f
SH
702 }
703 return $control;
704 }
705 return null;
706 }
6db3eee0
SH
707 /**
708 * Gets the enrolment type filter control for this table
709 *
710 * @return single_select
711 */
712 public function get_enrolment_type_filter() {
713 $url = new moodle_url($this->pageurl, $this->manager->get_url_params()+$this->get_url_params());
714 $selector = new single_select($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'));
716 return $selector;
717 }
718}
719
720/**
721 * Table used for other users
722 *
723 * Other users are users who have roles but are not enrolled.
724 *
725 * @copyright 2010 Sam Hemelryk
726 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
727 */
728class course_enrolment_other_users_table extends course_enrolment_table {
729
9f15347e
SH
730 public $otherusers = true;
731
6db3eee0
SH
732 /**
733 * Constructs the table
734 *
735 * @param course_enrolment_manager $manager
736 * @param moodle_url $pageurl
737 */
738 public function __construct(course_enrolment_manager $manager, moodle_url $pageurl) {
739 parent::__construct($manager, $pageurl);
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 */
750 public function get_user_search_button($page) {
751 global $CFG;
752 static $count = 0;
753 if (!has_capability('moodle/role:assign', $this->manager->get_context())) {
754 return false;
755 }
756 $count++;
757 $url = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid'=>$this->manager->get_context()->id, 'sesskey'=>sesskey()));
758 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
759 $control->class = 'singlebutton assignuserrole instance'.$count;
760 if ($count == 1) {
761 $page->requires->strings_for_js(array(
762 'ajaxoneuserfound',
763 'ajaxxusersfound',
764 'ajaxnext25',
765 'enrol',
766 'enrolmentoptions',
767 'enrolusers',
768 'errajaxfailedenrol',
769 'errajaxsearch',
770 'none',
771 'usersearch',
772 'unlimitedduration',
773 'startdatetoday',
774 'durationdays',
775 'enrolperiod'), 'enrol');
776 $page->requires->string_for_js('assignrole', 'role');
777
778 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
779 $function = 'M.enrol.otherusersmanager.init';
780 $url = new moodle_url($this->pageurl, $this->manager->get_url_params()+$this->get_url_params());
781 $arguments = array(
782 'courseId'=> $this->manager->get_course()->id,
783 'ajaxUrl' => '/enrol/ajax.php',
784 'url' => $url->out(false));
785 $page->requires->yui_module($modules, $function, array($arguments));
786 }
787 return $control;
788 }
2ec702c9 789}