forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
703 lines (638 loc) · 24.2 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This is the main renderer for the enrol section.
*
* @package core_enrol
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* This is the core renderer
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_enrol_renderer extends plugin_renderer_base {
/**
* Renderers the enrol_user_button.
*
* @param enrol_user_button $button
* @return string XHTML
*/
protected function render_enrol_user_button(enrol_user_button $button) {
$attributes = array('type' => 'submit',
'value' => $button->label,
'disabled' => $button->disabled ? 'disabled' : null,
'title' => $button->tooltip,
'class' => 'btn ' . ($button->primary ? 'btn-primary' : 'btn-secondary'));
if ($button->actions) {
$id = html_writer::random_id('single_button');
$attributes['id'] = $id;
foreach ($button->actions as $action) {
$this->add_action_handler($action, $id);
}
}
$button->initialise_js($this->page);
// first the input element
$output = html_writer::empty_tag('input', $attributes);
// then hidden fields
$params = $button->url->params();
if ($button->method === 'post') {
$params['sesskey'] = sesskey();
}
foreach ($params as $var => $val) {
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val));
}
// then div wrapper for xhtml strictness
$output = html_writer::tag('div', $output);
// now the form itself around it
if ($button->method === 'get') {
$url = $button->url->out_omit_querystring(true); // url without params, the anchor part allowed
} else {
$url = $button->url->out_omit_querystring(); // url without params, the anchor part not allowed
}
if ($url === '') {
$url = '#'; // there has to be always some action
}
$attributes = array('method' => $button->method,
'action' => $url,
'id' => $button->formid);
$output = html_writer::tag('form', $output, $attributes);
// and finally one more wrapper with class
return html_writer::tag('div', $output, array('class' => $button->class));
}
/**
* Renders a course enrolment table
*
* @param course_enrolment_table $table
* @return string
*/
protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table $table) {
$table->initialise_javascript();
$content = '';
$content .= html_writer::tag('div', get_string('otheruserdesc', 'enrol'), array('class'=>'otherusersdesc'));
$content .= $this->output->render($table->get_paging_bar());
$content .= html_writer::table($table);
$content .= $this->output->render($table->get_paging_bar());
return $content;
}
/**
* Generates HTML to display the users roles and any available actions
*
* @param int $userid
* @param array $roles
* @param array $assignableroles
* @param moodle_url $pageurl
* @return string
*/
public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) {
// Get list of roles.
$rolesoutput = '';
foreach ($roles as $roleid=>$role) {
if ($canassign and (is_siteadmin() or isset($assignableroles[$roleid])) and !$role['unchangeable']) {
$strunassign = get_string('unassignarole', 'role', $role['text']);
$icon = $this->output->pix_icon('t/delete', $strunassign);
$url = new moodle_url($pageurl, array('action'=>'unassign', 'roleid'=>$roleid, 'user'=>$userid));
$rolesoutput .= html_writer::tag('div', $role['text'] . html_writer::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid));
} else {
$rolesoutput .= html_writer::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
}
}
$output = '';
if (!empty($assignableroles) && $canassign) {
$roleids = array_keys($roles);
$hasallroles = true;
foreach (array_keys($assignableroles) as $key) {
if (!in_array($key, $roleids)) {
$hasallroles = false;
break;
}
}
if (!$hasallroles) {
$url = new moodle_url($pageurl, array('action' => 'assign', 'user' => $userid));
$roleicon = $this->output->pix_icon('i/assignroles', get_string('assignroles', 'role'));
$link = html_writer::link($url, $roleicon, array('class' => 'assignrolelink'));
$output = html_writer::tag('div', $link, array('class'=>'addrole'));
}
}
$output .= html_writer::tag('div', $rolesoutput, array('class'=>'roles'));
return $output;
}
/**
* Generates the HTML to view the users groups and available group actions
*
* @param int $userid
* @param array $groups
* @param array $allgroups
* @param bool $canmanagegroups
* @param moodle_url $pageurl
* @return string
*/
public function user_groups_and_actions($userid, $groups, $allgroups, $canmanagegroups, $pageurl) {
$groupicon = $this->output->pix_icon('i/group', get_string('addgroup', 'group'));
$groupoutput = '';
foreach($groups as $groupid=>$name) {
if ($canmanagegroups and groups_remove_member_allowed($groupid, $userid)) {
$icon = $this->output->pix_icon('t/delete', get_string('removefromgroup', 'group', $name));
$url = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid));
$groupoutput .= html_writer::tag('div', $name . html_writer::link($url, $icon), array('class'=>'group', 'rel'=>$groupid));
} else {
$groupoutput .= html_writer::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
}
}
$output = '';
if ($canmanagegroups && (count($groups) < count($allgroups))) {
$url = new moodle_url($pageurl, array('action'=>'addmember', 'user'=>$userid));
$output .= html_writer::tag('div', html_writer::link($url, $groupicon), array('class'=>'addgroup'));
}
$output = $output.html_writer::tag('div', $groupoutput, array('class'=>'groups'));
return $output;
}
/**
* Generates the HTML for the given enrolments + available actions
*
* @param int $userid
* @param array $enrolments
* @param moodle_url $pageurl
* @return string
*/
public function user_enrolments_and_actions($enrolments) {
$output = '';
foreach ($enrolments as $ue) {
$enrolmentoutput = $ue['text'].' '.$ue['period'];
if ($ue['dimmed']) {
$enrolmentoutput = html_writer::tag('span', $enrolmentoutput, array('class'=>'dimmed_text'));
} else {
$enrolmentoutput = html_writer::tag('span', $enrolmentoutput);
}
foreach ($ue['actions'] as $action) {
$enrolmentoutput .= $this->render($action);
}
$output .= html_writer::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
}
return $output;
}
/**
* Renders a user enrolment action
* @param user_enrolment_action $icon
* @return string
*/
protected function render_user_enrolment_action(user_enrolment_action $icon) {
return html_writer::link($icon->get_url(), $this->output->render($icon->get_icon()), $icon->get_attributes());
}
}
/**
* Main course enrolment table
*
* This table is used to display the enrolment information for a course.
* It requires that a course enrolment manager be provided during constuct with
* provides all of the information for the table.
* The control then produces the table, the paging, and the associated JS actions
* for the page.
*
* @package core
* @subpackage enrol
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_enrolment_table extends html_table implements renderable {
/**
* The get/post variable that is used to identify the page.
* Default: page
*/
const PAGEVAR = 'page';
/**
* The get/post variable to is used to identify the number of items to display
* per page.
* Default: perpage
*/
const PERPAGEVAR = 'perpage';
/**
* The get/post variable that is used to identify the sort field for the table.
* Default: sort
*/
const SORTVAR = 'sort';
/**
* The get/post variable that is used to identify the sort direction for the table.
* Default: dir
*/
const SORTDIRECTIONVAR = 'dir';
/**
* The default number of items per page.
* Default: 100
*/
const DEFAULTPERPAGE = 100;
/**
* The default sort, options are course_enrolment_table::$sortablefields
* Default: lastname
*/
const DEFAULTSORT = 'lastname';
/**
* The default direction
* Default: ASC
*/
const DEFAULTSORTDIRECTION = 'ASC';
/**
* The current page, starting from 0
* @var int
*/
public $page = 0;
/**
* The total number of pages
* @var int
*/
public $pages = 0;
/**
* The number of items to display per page
* @var int
*/
public $perpage = 0;
/**
* The sort field for this table, should be one of course_enrolment_table::$sortablefields
* @var string
*/
public $sort;
/**
* The sort direction, either ASC or DESC
* @var string
*/
public $sortdirection;
/**
* The course manager this table is displaying for
* @var course_enrolment_manager
*/
protected $manager;
/**
* The paging bar that controls the paging for this table
* @var paging_bar
*/
protected $pagingbar = null;
/**
* The total number of users enrolled in the course
* @var int
*/
protected $totalusers = null;
/**
* The users enrolled in this course
* @var array
*/
protected $users = null;
/**
* The fields for this table
* @var array
*/
protected $fields = array();
/**
* An array of bulk user enrolment operations
* @var array
*/
protected $bulkoperations = array();
/**
* An array of sortable fields
* @static
* @var array
*/
protected static $sortablefields = array('firstname', 'lastname', 'firstnamephonetic', 'lastnamephonetic', 'middlename',
'alternatename', 'username', 'idnumber', 'email', 'phone1', 'phone2',
'institution', 'department', 'lastaccess', 'lastcourseaccess');
/**
* Constructs the table
*
* @param course_enrolment_manager $manager
*/
public function __construct(course_enrolment_manager $manager) {
$this->manager = $manager;
$this->page = optional_param(self::PAGEVAR, 0, PARAM_INT);
$this->perpage = optional_param(self::PERPAGEVAR, self::DEFAULTPERPAGE, PARAM_INT);
$this->sort = optional_param(self::SORTVAR, self::DEFAULTSORT, PARAM_ALPHANUM);
$this->sortdirection = optional_param(self::SORTDIRECTIONVAR, self::DEFAULTSORTDIRECTION, PARAM_ALPHA);
$this->attributes = array('class' => 'userenrolment table-striped');
if (!in_array($this->sort, self::$sortablefields)) {
$this->sort = self::DEFAULTSORT;
}
if ($this->page < 0) {
$this->page = 0;
}
if ($this->sortdirection !== 'ASC' && $this->sortdirection !== 'DESC') {
$this->sortdirection = self::DEFAULTSORTDIRECTION;
}
$this->id = html_writer::random_id();
// Collect the bulk operations for the currently filtered plugin if there is one.
$plugin = $manager->get_filtered_enrolment_plugin();
if ($plugin and enrol_is_enabled($plugin->get_name())) {
$this->bulkoperations = $plugin->get_bulk_operations($manager);
}
}
/**
* Returns an array of enrol_user_buttons that are created by the different
* enrolment plugins available.
*
* @return array
*/
public function get_manual_enrol_buttons() {
return $this->manager->get_manual_enrol_buttons();
}
/**
* Gets the sort direction for a given field
*
* @param string $field
* @return string ASC or DESC
*/
public function get_field_sort_direction($field) {
if ($field == $this->sort) {
return ($this->sortdirection == 'ASC')?'DESC':'ASC';
}
return self::DEFAULTSORTDIRECTION;
}
/**
* Sets the fields for this table. These get added to the tables head as well.
*
* You can also use a multi dimensional array for this to have multiple fields
* in a single column
*
* @param array $fields An array of fields to set
* @param string $output
*/
public function set_fields($fields, $output) {
$this->fields = $fields;
$this->head = array();
$this->colclasses = array();
$this->align = array();
$url = $this->manager->get_moodlepage()->url;
if (!empty($this->bulkoperations)) {
// If there are bulk operations add a column for checkboxes.
$this->head[] = '';
$this->colclasses[] = 'field col_bulkops';
}
foreach ($fields as $name => $label) {
$newlabel = '';
if (is_array($label)) {
$bits = array();
foreach ($label as $n => $l) {
if ($l === false) {
continue;
}
if (!in_array($n, self::$sortablefields)) {
$bits[] = $l;
} else {
$sorturl = new moodle_url($url, array(self::SORTVAR => $n, self::SORTDIRECTIONVAR => $this->get_field_sort_direction($n)));
$link = html_writer::link($sorturl, $fields[$name][$n]);
if ($this->sort == $n) {
$link .= $this->get_direction_icon($output, $n);
}
$bits[] = html_writer::tag('span', $link, array('class'=>'subheading_'.$n));
}
}
$newlabel = join(' / ', $bits);
} else {
if (!in_array($name, self::$sortablefields)) {
$newlabel = $label;
} else {
$sorturl = new moodle_url($url, array(self::SORTVAR => $name, self::SORTDIRECTIONVAR => $this->get_field_sort_direction($name)));
$newlabel = html_writer::link($sorturl, $fields[$name]);
if ($this->sort == $name) {
$newlabel .= $this->get_direction_icon($output, $name);
}
}
}
$this->head[] = $newlabel;
$this->colclasses[] = 'field col_'.$name;
}
}
/**
* Sets the total number of users
*
* @param int $totalusers
*/
public function set_total_users($totalusers) {
$this->totalusers = $totalusers;
$this->pages = ceil($this->totalusers / $this->perpage);
if ($this->page > $this->pages) {
$this->page = $this->pages;
}
}
/**
* Sets the users for this table
*
* @param array $users
* @return void
*/
public function set_users(array $users) {
$this->users = $users;
$hasbulkops = !empty($this->bulkoperations);
foreach ($users as $userid=>$user) {
$user = (array)$user;
$row = new html_table_row();
$row->attributes = array('class' => 'userinforow');
$row->id = 'user_'.$userid;
$row->cells = array();
if ($hasbulkops) {
// Add a checkbox into the first column.
$input = html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid));
$row->cells[] = new html_table_cell($input);
}
foreach ($this->fields as $field => $label) {
if (is_array($label)) {
$bits = array();
foreach (array_keys($label) as $subfield) {
if (array_key_exists($subfield, $user)) {
$bits[] = html_writer::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield));
}
}
if (empty($bits)) {
$bits[] = ' ';
}
$row->cells[] = new html_table_cell(join(' ', $bits));
} else {
if (!array_key_exists($field, $user)) {
$user[$field] = ' ';
}
$row->cells[] = new html_table_cell($user[$field]);
}
}
$this->data[] = $row;
}
}
public function initialise_javascript() {
if (has_capability('moodle/role:assign', $this->manager->get_context())) {
$this->manager->get_moodlepage()->requires->strings_for_js(array(
'assignroles',
'confirmunassign',
'confirmunassigntitle',
'confirmunassignyes',
'confirmunassignno'
), 'role');
$modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
$function = 'M.enrol.rolemanager.init';
$arguments = array(
'containerId'=>$this->id,
'userIds'=>array_keys($this->users),
'courseId'=>$this->manager->get_course()->id,
'otherusers'=>isset($this->otherusers));
$this->manager->get_moodlepage()->requires->yui_module($modules, $function, array($arguments));
}
}
/**
* Gets the paging bar instance for this table
*
* @return paging_bar
*/
public function get_paging_bar() {
if ($this->pagingbar == null) {
$this->pagingbar = new paging_bar($this->totalusers, $this->page, $this->perpage, $this->manager->get_moodlepage()->url, self::PAGEVAR);
}
return $this->pagingbar;
}
/**
* Gets the direction icon for the sortable field within this table
*
* @param core_renderer $output
* @param string $field
* @return string
*/
protected function get_direction_icon($output, $field) {
$direction = self::DEFAULTSORTDIRECTION;
if ($this->sort == $field) {
$direction = $this->sortdirection;
}
if ($direction === 'ASC') {
return $output->pix_icon('t/sort_asc', get_string('sort'));
} else {
return $output->pix_icon('t/sort_desc', get_string('sort'));
}
}
/**
* Gets the params that will need to be added to the url in order to return to this page.
*
* @return array
*/
public function get_url_params() {
return array(
self::PAGEVAR => $this->page,
self::PERPAGEVAR => $this->perpage,
self::SORTVAR => $this->sort,
self::SORTDIRECTIONVAR => $this->sortdirection
);
}
/**
* Returns an array of URL params for both the table and the manager.
*
* @return array
*/
public function get_combined_url_params() {
return $this->get_url_params() + $this->manager->get_url_params();
}
/**
* Sets the bulk operations for this table.
*
* @param array $bulkoperations
*/
public function set_bulk_user_enrolment_operations(array $bulkoperations) {
$this->bulkoperations = $bulkoperations;
}
/**
* Returns an array of bulk operations.
*
* @return array
*/
public function get_bulk_user_enrolment_operations() {
return $this->bulkoperations;
}
/**
* Returns true fi the table is aware of any bulk operations that can be performed on users
* selected from the currently filtered enrolment plugins.
*
* @return bool
*/
public function has_bulk_user_enrolment_operations() {
return !empty($this->bulkoperations);
}
}
/**
* Table control used for enrolled users
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_enrolment_users_table extends course_enrolment_table {
}
/**
* Table used for other users
*
* Other users are users who have roles but are not enrolled.
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_enrolment_other_users_table extends course_enrolment_table {
public $otherusers = true;
/**
* Constructs the table
*
* @param course_enrolment_manager $manager
*/
public function __construct(course_enrolment_manager $manager) {
parent::__construct($manager);
$this->attributes = array('class'=>'userenrolment otheruserenrolment');
}
/**
* Gets a button to search users and assign them roles in the course.
*
* @staticvar int $count
* @param int $page
* @return single_button
*/
public function get_user_search_button() {
static $count = 0;
if (!has_capability('moodle/role:assign', $this->manager->get_context())) {
return false;
}
$count++;
$url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager->get_context()->id, 'sesskey'=>sesskey()));
$control = new single_button($url, get_string('assignroles', 'role'), 'get');
$control->class = 'singlebutton assignuserrole instance'.$count;
if ($count == 1) {
$this->manager->get_moodlepage()->requires->strings_for_js(array(
'ajaxoneuserfound',
'ajaxxusersfound',
'ajaxxmoreusersfound',
'ajaxnext25',
'enrol',
'enrolmentoptions',
'enrolusers',
'enrolxusers',
'errajaxfailedenrol',
'errajaxsearch',
'foundxcohorts',
'none',
'usersearch',
'unlimitedduration',
'startdatetoday',
'durationdays',
'enrolperiod'), 'enrol');
$this->manager->get_moodlepage()->requires->string_for_js('assignrole', 'role');
$modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
$function = 'M.enrol.otherusersmanager.init';
$arguments = array(
'courseId'=> $this->manager->get_course()->id,
'ajaxUrl' => '/enrol/ajax.php',
'url' => $this->manager->get_moodlepage()->url->out(false));
$this->manager->get_moodlepage()->requires->yui_module($modules, $function, array($arguments));
}
return $control;
}
}