3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This is the main renderer for the enrol section.
23 * @copyright 2010 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * This is the core renderer
30 * @copyright 2010 Sam Hemelryk
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class core_enrol_renderer
extends plugin_renderer_base
{
36 * Renders a course enrolment table
38 * @param course_enrolment_table $table
41 protected function render_course_enrolment_users_table(course_enrolment_users_table
$table) {
43 $table->initialise_javascript();
46 $enrolmentselector = $table->get_enrolment_selector();
47 if ($enrolmentselector) {
48 $content .= $this->output
->render($enrolmentselector);
50 $cohortenroller = $table->get_cohort_enrolment_control();
51 if ($cohortenroller) {
52 $content .= $this->output
->render($cohortenroller);
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();
59 if ($enrolmentselector) {
60 $content .= $this->output
->render($enrolmentselector);
62 $cohortenroller = $table->get_cohort_enrolment_control();
63 if ($cohortenroller) {
64 $content .= $this->output
->render($cohortenroller);
70 * Renders a course enrolment table
72 * @param course_enrolment_table $table
75 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table
$table) {
77 $table->initialise_javascript();
80 $searchbutton = $table->get_user_search_button();
82 $content .= $this->output
->render($searchbutton);
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();
90 $content .= $this->output
->render($searchbutton);
96 * Generates HTML to display the users roles and any available actions
100 * @param array $assignableroles
101 * @param moodle_url $pageurl
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');
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));
117 $rolesoutput .= html_writer
::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
121 if (!empty($assignableroles) && $canassign) {
122 $roleids = array_keys($roles);
124 foreach (array_keys($assignableroles) as $key) {
125 if (!in_array($key, $roleids)) {
126 $hasallroles = false;
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'));
136 $output .= html_writer
::tag('div', $rolesoutput, array('class'=>'roles'));
141 * Generates the HTML to view the users groups and available group actions
144 * @param array $groups
145 * @param array $allgroups
146 * @param bool $canmanagegroups
147 * @param moodle_url $pageurl
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');
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));
162 $groupoutput .= html_writer
::tag('div', $name, array('class'=>'group', 'rel'=>$groupid));
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'));
175 * Generates the HTML for the given enrolments + available actions
178 * @param array $enrolments
179 * @param moodle_url $pageurl
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');
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'));
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));
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));
204 $output .= html_writer
::tag('div', $enrolmentoutput, array('class'=>'enrolment'));
212 * Main course enrolment table
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
222 * @copyright 2010 Sam Hemelryk
223 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
225 class course_enrolment_table
extends html_table
implements renderable
{
228 * The get/post variable that is used to identify the page.
231 const PAGEVAR
= 'page';
234 * The get/post variable to is used to identify the number of items to display
238 const PERPAGEVAR
= 'perpage';
241 * The get/post variable that is used to identify the sort field for the table.
244 const SORTVAR
= 'sort';
247 * The get/post variable that is used to identify the sort direction for the table.
250 const SORTDIRECTIONVAR
= 'dir';
253 * The default number of items per page.
256 const DEFAULTPERPAGE
= 100;
259 * The default sort, options are course_enrolment_table::$sortablefields
262 const DEFAULTSORT
= 'lastname';
265 * The default direction
268 const DEFAULTSORTDIRECTION
= 'ASC';
271 * The current page, starting from 0
277 * The total number of pages
283 * The number of items to display per page
289 * The URL of the page for this table
295 * The sort field for this table, should be one of course_enrolment_table::$sortablefields
301 * The sort direction, either ASC or DESC
304 public $sortdirection;
307 * The course manager this table is displaying for
308 * @var course_enrolment_manager
313 * The paging bar that controls the paging for this table
316 protected $pagingbar = null;
319 * The total number of users enrolled in the course
322 protected $totalusers = null;
325 * The users enrolled in this course
328 protected $users = null;
331 * The fields for this table
334 protected $fields = array();
337 * An array of sortable fields
341 protected static $sortablefields = array('firstname', 'lastname', 'email');
344 * Constructs the table
346 * @param course_enrolment_manager $manager
348 public function __construct(course_enrolment_manager
$manager, moodle_page
$moodlepage) {
350 $this->manager
= $manager;
351 $this->moodlepage
= $moodlepage;
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
);
358 $this->attributes
= array('class'=>'userenrolment');
359 if (!in_array($this->sort
, self
::$sortablefields)) {
360 $this->sort
= self
::DEFAULTSORT
;
362 if ($this->page
< 0) {
365 if ($this->sortdirection
!== 'ASC' && $this->sortdirection
!== 'DESC') {
366 $this->sortdirection
= self
::DEFAULTSORTDIRECTION
;
369 $this->id
= html_writer
::random_id();
373 * Gets the sort direction for a given field
375 * @param string $field
376 * @return string ASC or DESC
378 public function get_field_sort_direction($field) {
379 if ($field == $this->sort
) {
380 return ($this->sortdirection
== 'ASC')?
'DESC':'ASC';
382 return self
::DEFAULTSORTDIRECTION
;
386 * Sets the fields for this table. These get added to the tables head as well.
388 * You can also use a multi dimensional array for this to have multiple fields
391 * @param array $fields An array of fields to set
392 * @param string $output
394 public function set_fields($fields, $output) {
395 $this->fields
= $fields;
396 $this->head
= array();
397 $this->colclasses
= array();
398 $this->align
= array();
399 $url = $this->moodlepage
->url
;
400 foreach ($fields as $name => $label) {
402 if (is_array($label)) {
404 foreach ($label as $n => $l) {
408 if (!in_array($n, self
::$sortablefields)) {
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));
415 $bits[] = html_writer
::tag('span', $link, array('class'=>'subheading_'.$n));
419 $newlabel = join(' / ', $bits);
421 if (!in_array($name, self
::$sortablefields)) {
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));
430 $this->head
[] = $newlabel;
431 $this->colclasses
[] = 'field col_'.$name;
435 * Sets the total number of users
437 * @param int $totalusers
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
;
450 * Sets the users for this table
452 * @param array $users
455 public function set_users(array $users) {
456 $this->users
= $users;
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)) {
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));
474 $row->cells
[] = new html_table_cell(join(' ', $bits));
476 if (!array_key_exists($field, $user)) {
477 $user[$field] = ' ';
479 $row->cells
[] = new html_table_cell($user[$field]);
482 $this->data
[] = $row;
486 public function initialise_javascript() {
487 if (has_capability('moodle/role:assign', $this->manager
->get_context())) {
488 $this->moodlepage
->requires
->strings_for_js(array(
491 'confirmunassigntitle',
492 'confirmunassignyes',
495 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin');
496 $function = 'M.enrol.rolemanager.init';
498 'containerId'=>$this->id
,
499 'userIds'=>array_keys($this->users
),
500 'courseId'=>$this->manager
->get_course()->id
,
501 'otherusers'=>isset($this->otherusers
));
502 $this->moodlepage
->requires
->yui_module($modules, $function, array($arguments));
507 * Gets the paging bar instance for this table
511 public function get_paging_bar() {
512 if ($this->pagingbar
== null) {
513 $this->pagingbar
= new paging_bar($this->totalusers
, $this->page
, $this->perpage
, $this->moodlepage
->url
, self
::PAGEVAR
);
515 return $this->pagingbar
;
519 * Gets the direction icon for the sortable field within this table
521 * @param core_renderer $output
522 * @param string $field
525 protected function get_direction_icon($output, $field) {
526 $direction = self
::DEFAULTSORTDIRECTION
;
527 if ($this->sort
== $field) {
528 $direction = $this->sortdirection
;
530 if ($direction === 'ASC') {
531 return html_writer
::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/down')));
533 return html_writer
::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/up')));
538 * Gets the params that will need to be added to the url in order to return to this page.
542 public function get_url_params() {
544 self
::PAGEVAR
=> $this->page
,
545 self
::PERPAGEVAR
=> $this->perpage
,
546 self
::SORTVAR
=> $this->sort
,
547 self
::SORTDIRECTIONVAR
=> $this->sortdirection
553 * Table control used for enrolled users
555 * @copyright 2010 Sam Hemelryk
556 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
558 class course_enrolment_users_table
extends course_enrolment_table
{
561 * An array of sortable fields
565 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
568 * Returns a button to enrol cohorts or thier users
570 * @staticvar int $count
571 * @return single_button|false
573 public function get_cohort_enrolment_control() {
576 // First make sure that cohorts is enabled
577 $plugins = $this->manager
->get_enrolment_plugins();
578 if (!array_key_exists('cohort', $plugins)) {
581 $course = $this->manager
->get_course();
582 if (!$plugins['cohort']->get_newinstance_link($course->id
)) {
583 // user can not see any cohort === can not use this
587 $cohorturl = new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$course->id
));
588 $control = new single_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get');
589 $control->class = 'singlebutton enrolcohortbutton instance'.$count;
590 $control->formid
= 'manuallyenrol_single_'+
$count;
592 $this->moodlepage
->requires
->strings_for_js(array('enrol','synced','enrolcohort','enrolcohortusers'), 'enrol');
593 $this->moodlepage
->requires
->string_for_js('assignroles', 'role');
594 $this->moodlepage
->requires
->string_for_js('cohort', 'cohort');
595 $this->moodlepage
->requires
->string_for_js('users', 'moodle');
597 $hasmanualinstance = false;
598 // No point showing this at all if the user cant manually enrol users
599 if (has_capability('enrol/manual:manage', $this->manager
->get_context())) {
600 // Make sure manual enrolments instance exists
601 $instances = $this->manager
->get_enrolment_instances();
602 foreach ($instances as $instance) {
603 if ($instance->enrol
== 'manual') {
604 $hasmanualinstance = true;
610 $modules = array('moodle-enrol-quickcohortenrolment', 'moodle-enrol-quickcohortenrolment-skin');
611 $function = 'M.enrol.quickcohortenrolment.init';
613 'courseid'=>$course->id
,
614 'ajaxurl'=>'/enrol/ajax.php',
615 'url'=>$this->moodlepage
->url
->out(false),
616 'manualEnrolment'=>$hasmanualinstance);
617 $this->moodlepage
->requires
->yui_module($modules, $function, array($arguments));
623 * Gets the enrolment selector control for this table and initialises its
626 * @return single_button|url_select
628 public function get_enrolment_selector() {
632 $instances = $this->manager
->get_enrolment_instances();
633 $plugins = $this->manager
->get_enrolment_plugins();
635 // print enrol link or selection
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 $manuals[$instance->id
] = $instance;
644 if (!empty($links)) {
645 $arguments = array();
647 if (count($links) == 1) {
648 $control = new single_button(reset($links), get_string('enrolusers', 'enrol_manual'), 'get');
649 $control->class = 'singlebutton enrolusersbutton instance'.$count;
650 $control->formid
= 'manuallyenrol_single_'+
$count;
651 $arguments[] = array('id'=>key($links), 'name'=>$plugins[$instances[key($links)]->enrol
]->get_instance_name($instances[key($links)]));
652 } else if (count($links) > 1) {
653 $inames = $this->manager
->get_enrolment_instance_names();
655 foreach ($links as $i=>$link) {
656 $options[$link->out(false)] = $inames[$i];
657 $arguments[] = array('id'=>$i, 'name'=>$plugins[$instances[$i]->enrol
]->get_instance_name($instances[$i]));
659 $control = new url_select($options, '', array(''=>get_string('enrolusers', 'enrol_manual').'...'));
660 $control->class = 'singlebutton enrolusersbutton instance'.$count;
661 $control->formid
= 'manuallyenrol_select_'+
$count;
664 $course = $this->manager
->get_course();
665 $timeformat = get_string('strftimedatefullshort');
667 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
668 $startdateoptions = array();
669 if ($course->startdate
> 0) {
670 $startdateoptions[2] = get_string('coursestart') . ' (' . userdate($course->startdate
, $timeformat) . ')';
672 $startdateoptions[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
675 $instance = reset($manuals);
676 $this->moodlepage
->requires
->strings_for_js(array(
683 'errajaxfailedenrol',
691 'finishenrollingusers',
692 'recovergrades'), 'enrol');
693 $this->moodlepage
->requires
->string_for_js('assignroles', 'role');
694 $this->moodlepage
->requires
->string_for_js('startingfrom', 'moodle');
696 $modules = array('moodle-enrol-enrolmentmanager', 'moodle-enrol-enrolmentmanager-skin');
697 $function = 'M.enrol.enrolmentmanager.init';
699 'instances'=>$arguments,
700 'courseid'=>$course->id
,
701 'ajaxurl'=>'/enrol/ajax.php',
702 'url'=>$this->moodlepage
->url
->out(false),
703 'optionsStartDate'=>$startdateoptions,
704 'defaultRole'=>$instance->roleid
,
705 'disableGradeHistory'=>$CFG->disablegradehistory
);
706 $this->moodlepage
->requires
->yui_module($modules, $function, array($arguments));
713 * Gets the enrolment type filter control for this table
715 * @return single_select
717 public function get_enrolment_type_filter() {
718 $selector = new single_select($this->moodlepage
->url
, 'ifilter', array(0=>get_string('all')) +
(array)$this->manager
->get_enrolment_instance_names(), $this->manager
->get_enrolment_filter(), array());
719 $selector->set_label( get_string('enrolmentinstances', 'enrol'));
725 * Table used for other users
727 * Other users are users who have roles but are not enrolled.
729 * @copyright 2010 Sam Hemelryk
730 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
732 class course_enrolment_other_users_table
extends course_enrolment_table
{
734 public $otherusers = true;
737 * Constructs the table
739 * @param course_enrolment_manager $manager
740 * @param moodle_page $moodlepage
742 public function __construct(course_enrolment_manager
$manager, moodle_page
$moodlepage) {
743 parent
::__construct($manager, $moodlepage);
744 $this->attributes
= array('class'=>'userenrolment otheruserenrolment');
748 * Gets a button to search users and assign them roles in the course.
750 * @staticvar int $count
752 * @return single_button
754 public function get_user_search_button() {
756 if (!has_capability('moodle/role:assign', $this->manager
->get_context())) {
760 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager
->get_context()->id
, 'sesskey'=>sesskey()));
761 $control = new single_button($url, get_string('assignroles', 'role'), 'get');
762 $control->class = 'singlebutton assignuserrole instance'.$count;
764 $this->moodlepage
->requires
->strings_for_js(array(
771 'errajaxfailedenrol',
778 'enrolperiod'), 'enrol');
779 $this->moodlepage
->requires
->string_for_js('assignrole', 'role');
781 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin');
782 $function = 'M.enrol.otherusersmanager.init';
784 'courseId'=> $this->manager
->get_course()->id
,
785 'ajaxUrl' => '/enrol/ajax.php',
786 'url' => $this->moodlepage
->url
->out(false));
787 $this->moodlepage
->requires
->yui_module($modules, $function, array($arguments));