|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00033 class core_enrol_renderer extends plugin_renderer_base { 00034 00041 protected function render_course_enrolment_users_table(course_enrolment_users_table $table) { 00042 00043 $table->initialise_javascript(); 00044 00045 $buttons = $table->get_manual_enrol_buttons(); 00046 $buttonhtml = ''; 00047 if (count($buttons) > 0) { 00048 $buttonhtml .= html_writer::start_tag('div', array('class' => 'enrol_user_buttons')); 00049 foreach ($buttons as $button) { 00050 $buttonhtml .= $this->render($button); 00051 } 00052 $buttonhtml .= html_writer::end_tag('div'); 00053 } 00054 00055 $content = ''; 00056 if (!empty($buttonhtml)) { 00057 $content .= $buttonhtml; 00058 } 00059 $content .= $this->output->render($table->get_enrolment_type_filter()); 00060 $content .= $this->output->render($table->get_paging_bar()); 00061 00062 // Check if the table has any bulk operations. If it does we want to wrap the table in a 00063 // form so that we can capture and perform any required bulk operations. 00064 if ($table->has_bulk_user_enrolment_operations()) { 00065 $content .= html_writer::start_tag('form', array('action' => new moodle_url('/enrol/bulkchange.php'), 'method' => 'post')); 00066 foreach ($table->get_combined_url_params() as $key => $value) { 00067 if ($key == 'action') { 00068 continue; 00069 } 00070 $content .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $key, 'value' => $value)); 00071 } 00072 $content .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulkchange')); 00073 $content .= html_writer::table($table); 00074 $content .= html_writer::start_tag('div', array('class' => 'singleselect bulkuserop')); 00075 $content .= html_writer::start_tag('select', array('name' => 'bulkuserop')); 00076 $content .= html_writer::tag('option', get_string('withselectedusers', 'enrol'), array('value' => '')); 00077 $options = array('' => get_string('withselectedusers', 'enrol')); 00078 foreach ($table->get_bulk_user_enrolment_operations() as $operation) { 00079 $content .= html_writer::tag('option', $operation->get_title(), array('value' => $operation->get_identifier())); 00080 } 00081 $content .= html_writer::end_tag('select'); 00082 $content .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('go'))); 00083 $content .= html_writer::end_tag('div'); 00084 00085 $content .= html_writer::end_tag('form'); 00086 } else { 00087 $content .= html_writer::table($table); 00088 } 00089 $content .= $this->output->render($table->get_paging_bar()); 00090 if (!empty($buttonhtml)) { 00091 $content .= $buttonhtml; 00092 } 00093 return $content; 00094 } 00095 00102 protected function render_enrol_user_button(enrol_user_button $button) { 00103 $attributes = array('type' => 'submit', 00104 'value' => $button->label, 00105 'disabled' => $button->disabled ? 'disabled' : null, 00106 'title' => $button->tooltip); 00107 00108 if ($button->actions) { 00109 $id = html_writer::random_id('single_button'); 00110 $attributes['id'] = $id; 00111 foreach ($button->actions as $action) { 00112 $this->add_action_handler($action, $id); 00113 } 00114 } 00115 $button->initialise_js($this->page); 00116 00117 // first the input element 00118 $output = html_writer::empty_tag('input', $attributes); 00119 00120 // then hidden fields 00121 $params = $button->url->params(); 00122 if ($button->method === 'post') { 00123 $params['sesskey'] = sesskey(); 00124 } 00125 foreach ($params as $var => $val) { 00126 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val)); 00127 } 00128 00129 // then div wrapper for xhtml strictness 00130 $output = html_writer::tag('div', $output); 00131 00132 // now the form itself around it 00133 if ($button->method === 'get') { 00134 $url = $button->url->out_omit_querystring(true); // url without params, the anchor part allowed 00135 } else { 00136 $url = $button->url->out_omit_querystring(); // url without params, the anchor part not allowed 00137 } 00138 if ($url === '') { 00139 $url = '#'; // there has to be always some action 00140 } 00141 $attributes = array('method' => $button->method, 00142 'action' => $url, 00143 'id' => $button->formid); 00144 $output = html_writer::tag('form', $output, $attributes); 00145 00146 // and finally one more wrapper with class 00147 return html_writer::tag('div', $output, array('class' => $button->class)); 00148 } 00149 00156 protected function render_course_enrolment_other_users_table(course_enrolment_other_users_table $table) { 00157 00158 $table->initialise_javascript(); 00159 00160 $content = ''; 00161 $searchbutton = $table->get_user_search_button(); 00162 if ($searchbutton) { 00163 $content .= $this->output->render($searchbutton); 00164 } 00165 $content .= html_writer::tag('div', get_string('otheruserdesc', 'enrol'), array('class'=>'otherusersdesc')); 00166 $content .= $this->output->render($table->get_paging_bar()); 00167 $content .= html_writer::table($table); 00168 $content .= $this->output->render($table->get_paging_bar()); 00169 $searchbutton = $table->get_user_search_button(); 00170 if ($searchbutton) { 00171 $content .= $this->output->render($searchbutton); 00172 } 00173 return $content; 00174 } 00175 00185 public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) { 00186 $iconenroladd = $this->output->pix_url('t/enroladd'); 00187 $iconenrolremove = $this->output->pix_url('t/delete'); 00188 00189 // get list of roles 00190 $rolesoutput = ''; 00191 foreach ($roles as $roleid=>$role) { 00192 if ($canassign && !$role['unchangeable']) { 00193 $strunassign = get_string('unassignarole', 'role', $role['text']); 00194 $icon = html_writer::empty_tag('img', array('alt'=>$strunassign, 'src'=>$iconenrolremove)); 00195 $url = new moodle_url($pageurl, array('action'=>'unassign', 'role'=>$roleid, 'user'=>$userid)); 00196 $rolesoutput .= html_writer::tag('div', $role['text'] . html_writer::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid)); 00197 } else { 00198 $rolesoutput .= html_writer::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid)); 00199 } 00200 } 00201 $output = ''; 00202 if (!empty($assignableroles) && $canassign) { 00203 $roleids = array_keys($roles); 00204 $hasallroles = true; 00205 foreach (array_keys($assignableroles) as $key) { 00206 if (!in_array($key, $roleids)) { 00207 $hasallroles = false; 00208 break; 00209 } 00210 } 00211 if (!$hasallroles) { 00212 $url = new moodle_url($pageurl, array('action'=>'assign', 'user'=>$userid)); 00213 $icon = html_writer::empty_tag('img', array('alt'=>get_string('assignroles', 'role'), 'src'=>$iconenroladd)); 00214 $output = html_writer::tag('div', html_writer::link($url, $icon, array('class'=>'assignrolelink', 'title'=>get_string('assignroles', 'role'))), array('class'=>'addrole')); 00215 } 00216 } 00217 $output .= html_writer::tag('div', $rolesoutput, array('class'=>'roles')); 00218 return $output; 00219 } 00220 00231 public function user_groups_and_actions($userid, $groups, $allgroups, $canmanagegroups, $pageurl) { 00232 $iconenroladd = $this->output->pix_url('t/enroladd'); 00233 $iconenrolremove = $this->output->pix_url('t/delete'); 00234 $straddgroup = get_string('addgroup', 'group'); 00235 00236 $groupoutput = ''; 00237 foreach($groups as $groupid=>$name) { 00238 if ($canmanagegroups) { 00239 $icon = html_writer::empty_tag('img', array('alt'=>get_string('removefromgroup', 'group', $name), 'src'=>$iconenrolremove)); 00240 $url = new moodle_url($pageurl, array('action'=>'removemember', 'group'=>$groupid, 'user'=>$userid)); 00241 $groupoutput .= html_writer::tag('div', $name . html_writer::link($url, $icon), array('class'=>'group', 'rel'=>$groupid)); 00242 } else { 00243 $groupoutput .= html_writer::tag('div', $name, array('class'=>'group', 'rel'=>$groupid)); 00244 } 00245 } 00246 $groupoutput = html_writer::tag('div', $groupoutput, array('class'=>'groups')); 00247 if ($canmanagegroups && (count($groups) < count($allgroups))) { 00248 $icon = html_writer::empty_tag('img', array('alt'=>$straddgroup, 'src'=>$iconenroladd)); 00249 $url = new moodle_url($pageurl, array('action'=>'addmember', 'user'=>$userid)); 00250 $groupoutput .= html_writer::tag('div', html_writer::link($url, $icon), array('class'=>'addgroup')); 00251 } 00252 return $groupoutput; 00253 } 00254 00263 public function user_enrolments_and_actions($enrolments) { 00264 $output = ''; 00265 foreach ($enrolments as $ue) { 00266 $enrolmentoutput = $ue['text'].' '.$ue['period']; 00267 if ($ue['dimmed']) { 00268 $enrolmentoutput = html_writer::tag('span', $enrolmentoutput, array('class'=>'dimmed_text')); 00269 } else { 00270 $enrolmentoutput = html_writer::tag('span', $enrolmentoutput); 00271 } 00272 foreach ($ue['actions'] as $action) { 00273 $enrolmentoutput .= $this->render($action); 00274 } 00275 $output .= html_writer::tag('div', $enrolmentoutput, array('class'=>'enrolment')); 00276 } 00277 return $output; 00278 } 00279 00285 protected function render_user_enrolment_action(user_enrolment_action $icon) { 00286 return html_writer::link($icon->get_url(), $this->output->render($icon->get_icon()), $icon->get_attributes()); 00287 } 00288 } 00289 00304 class course_enrolment_table extends html_table implements renderable { 00305 00310 const PAGEVAR = 'page'; 00311 00317 const PERPAGEVAR = 'perpage'; 00318 00323 const SORTVAR = 'sort'; 00324 00329 const SORTDIRECTIONVAR = 'dir'; 00330 00335 const DEFAULTPERPAGE = 100; 00336 00341 const DEFAULTSORT = 'lastname'; 00342 00347 const DEFAULTSORTDIRECTION = 'ASC'; 00348 00353 public $page = 0; 00354 00359 public $pages = 0; 00360 00365 public $perpage = 0; 00366 00371 public $sort; 00372 00377 public $sortdirection; 00378 00383 protected $manager; 00384 00389 protected $pagingbar = null; 00390 00395 protected $totalusers = null; 00396 00401 protected $users = null; 00402 00407 protected $fields = array(); 00408 00413 protected $bulkoperations = array(); 00414 00420 protected static $sortablefields = array('firstname', 'lastname', 'idnumber', 'email', 00421 'phone1', 'phone2', 'institution', 'department' ); 00422 00428 public function __construct(course_enrolment_manager $manager) { 00429 00430 $this->manager = $manager; 00431 00432 $this->page = optional_param(self::PAGEVAR, 0, PARAM_INT); 00433 $this->perpage = optional_param(self::PERPAGEVAR, self::DEFAULTPERPAGE, PARAM_INT); 00434 $this->sort = optional_param(self::SORTVAR, self::DEFAULTSORT, PARAM_ALPHA); 00435 $this->sortdirection = optional_param(self::SORTDIRECTIONVAR, self::DEFAULTSORTDIRECTION, PARAM_ALPHA); 00436 00437 $this->attributes = array('class'=>'userenrolment'); 00438 if (!in_array($this->sort, self::$sortablefields)) { 00439 $this->sort = self::DEFAULTSORT; 00440 } 00441 if ($this->page < 0) { 00442 $this->page = 0; 00443 } 00444 if ($this->sortdirection !== 'ASC' && $this->sortdirection !== 'DESC') { 00445 $this->sortdirection = self::DEFAULTSORTDIRECTION; 00446 } 00447 00448 $this->id = html_writer::random_id(); 00449 00450 // Collect the bulk operations for the currently filtered plugin if there is one. 00451 $plugin = $manager->get_filtered_enrolment_plugin(); 00452 if ($plugin) { 00453 $this->bulkoperations = $plugin->get_bulk_operations($manager); 00454 } 00455 } 00456 00463 public function get_manual_enrol_buttons() { 00464 return $this->manager->get_manual_enrol_buttons(); 00465 } 00466 00473 public function get_field_sort_direction($field) { 00474 if ($field == $this->sort) { 00475 return ($this->sortdirection == 'ASC')?'DESC':'ASC'; 00476 } 00477 return self::DEFAULTSORTDIRECTION; 00478 } 00479 00489 public function set_fields($fields, $output) { 00490 $this->fields = $fields; 00491 $this->head = array(); 00492 $this->colclasses = array(); 00493 $this->align = array(); 00494 $url = $this->manager->get_moodlepage()->url; 00495 00496 if (!empty($this->bulkoperations)) { 00497 // If there are bulk operations add a column for checkboxes. 00498 $this->head[] = ''; 00499 $this->colclasses[] = 'field col_bulkops'; 00500 } 00501 00502 foreach ($fields as $name => $label) { 00503 $newlabel = ''; 00504 if (is_array($label)) { 00505 $bits = array(); 00506 foreach ($label as $n => $l) { 00507 if ($l === false) { 00508 continue; 00509 } 00510 if (!in_array($n, self::$sortablefields)) { 00511 $bits[] = $l; 00512 } else { 00513 $link = html_writer::link(new moodle_url($url, array(self::SORTVAR=>$n)), $fields[$name][$n]); 00514 if ($this->sort == $n) { 00515 $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)); 00516 } 00517 $bits[] = html_writer::tag('span', $link, array('class'=>'subheading_'.$n)); 00518 00519 } 00520 } 00521 $newlabel = join(' / ', $bits); 00522 } else { 00523 if (!in_array($name, self::$sortablefields)) { 00524 $newlabel = $label; 00525 } else { 00526 $newlabel = html_writer::link(new moodle_url($url, array(self::SORTVAR=>$name)), $fields[$name]); 00527 if ($this->sort == $name) { 00528 $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)); 00529 } 00530 } 00531 } 00532 $this->head[] = $newlabel; 00533 $this->colclasses[] = 'field col_'.$name; 00534 } 00535 } 00541 public function set_total_users($totalusers) { 00542 $this->totalusers = $totalusers; 00543 $this->pages = ceil($this->totalusers / $this->perpage); 00544 if ($this->page > $this->pages) { 00545 $this->page = $this->pages; 00546 } 00547 } 00554 public function set_users(array $users) { 00555 $this->users = $users; 00556 $hasbulkops = !empty($this->bulkoperations); 00557 foreach ($users as $userid=>$user) { 00558 $user = (array)$user; 00559 $row = new html_table_row(); 00560 $row->attributes = array('class' => 'userinforow'); 00561 $row->id = 'user_'.$userid; 00562 $row->cells = array(); 00563 if ($hasbulkops) { 00564 // Add a checkbox into the first column. 00565 $input = html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'bulkuser[]', 'value' => $userid)); 00566 $row->cells[] = new html_table_cell($input); 00567 } 00568 foreach ($this->fields as $field => $label) { 00569 if (is_array($label)) { 00570 $bits = array(); 00571 foreach (array_keys($label) as $subfield) { 00572 if (array_key_exists($subfield, $user)) { 00573 $bits[] = html_writer::tag('div', $user[$subfield], array('class'=>'subfield subfield_'.$subfield)); 00574 } 00575 } 00576 if (empty($bits)) { 00577 $bits[] = ' '; 00578 } 00579 $row->cells[] = new html_table_cell(join(' ', $bits)); 00580 } else { 00581 if (!array_key_exists($field, $user)) { 00582 $user[$field] = ' '; 00583 } 00584 $row->cells[] = new html_table_cell($user[$field]); 00585 } 00586 } 00587 $this->data[] = $row; 00588 } 00589 } 00590 00591 public function initialise_javascript() { 00592 if (has_capability('moodle/role:assign', $this->manager->get_context())) { 00593 $this->manager->get_moodlepage()->requires->strings_for_js(array( 00594 'assignroles', 00595 'confirmunassign', 00596 'confirmunassigntitle', 00597 'confirmunassignyes', 00598 'confirmunassignno' 00599 ), 'role'); 00600 $modules = array('moodle-enrol-rolemanager', 'moodle-enrol-rolemanager-skin'); 00601 $function = 'M.enrol.rolemanager.init'; 00602 $arguments = array( 00603 'containerId'=>$this->id, 00604 'userIds'=>array_keys($this->users), 00605 'courseId'=>$this->manager->get_course()->id, 00606 'otherusers'=>isset($this->otherusers)); 00607 $this->manager->get_moodlepage()->requires->yui_module($modules, $function, array($arguments)); 00608 } 00609 } 00610 00616 public function get_paging_bar() { 00617 if ($this->pagingbar == null) { 00618 $this->pagingbar = new paging_bar($this->totalusers, $this->page, $this->perpage, $this->manager->get_moodlepage()->url, self::PAGEVAR); 00619 } 00620 return $this->pagingbar; 00621 } 00622 00630 protected function get_direction_icon($output, $field) { 00631 $direction = self::DEFAULTSORTDIRECTION; 00632 if ($this->sort == $field) { 00633 $direction = $this->sortdirection; 00634 } 00635 if ($direction === 'ASC') { 00636 return html_writer::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/down'))); 00637 } else { 00638 return html_writer::empty_tag('img', array('alt'=>'', 'src'=>$output->pix_url('t/up'))); 00639 } 00640 } 00641 00647 public function get_url_params() { 00648 return array( 00649 self::PAGEVAR => $this->page, 00650 self::PERPAGEVAR => $this->perpage, 00651 self::SORTVAR => $this->sort, 00652 self::SORTDIRECTIONVAR => $this->sortdirection 00653 ); 00654 } 00655 00661 public function get_combined_url_params() { 00662 return $this->get_url_params() + $this->manager->get_url_params(); 00663 } 00664 00670 public function set_bulk_user_enrolment_operations(array $bulkoperations) { 00671 $this->bulkoperations = $bulkoperations; 00672 } 00673 00679 public function get_bulk_user_enrolment_operations() { 00680 return $this->bulkoperations; 00681 } 00682 00689 public function has_bulk_user_enrolment_operations() { 00690 return !empty($this->bulkoperations); 00691 } 00692 } 00693 00700 class course_enrolment_users_table extends course_enrolment_table { 00701 00707 protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess'); 00708 00714 public function get_enrolment_type_filter() { 00715 $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()); 00716 $selector->set_label( get_string('enrolmentinstances', 'enrol')); 00717 return $selector; 00718 } 00719 } 00720 00729 class course_enrolment_other_users_table extends course_enrolment_table { 00730 00731 public $otherusers = true; 00732 00738 public function __construct(course_enrolment_manager $manager) { 00739 parent::__construct($manager); 00740 $this->attributes = array('class'=>'userenrolment otheruserenrolment'); 00741 } 00742 00750 public function get_user_search_button() { 00751 static $count = 0; 00752 if (!has_capability('moodle/role:assign', $this->manager->get_context())) { 00753 return false; 00754 } 00755 $count++; 00756 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$this->manager->get_context()->id, 'sesskey'=>sesskey())); 00757 $control = new single_button($url, get_string('assignroles', 'role'), 'get'); 00758 $control->class = 'singlebutton assignuserrole instance'.$count; 00759 if ($count == 1) { 00760 $this->manager->get_moodlepage()->requires->strings_for_js(array( 00761 'ajaxoneuserfound', 00762 'ajaxxusersfound', 00763 'ajaxnext25', 00764 'enrol', 00765 'enrolmentoptions', 00766 'enrolusers', 00767 'errajaxfailedenrol', 00768 'errajaxsearch', 00769 'none', 00770 'usersearch', 00771 'unlimitedduration', 00772 'startdatetoday', 00773 'durationdays', 00774 'enrolperiod'), 'enrol'); 00775 $this->manager->get_moodlepage()->requires->string_for_js('assignrole', 'role'); 00776 00777 $modules = array('moodle-enrol-otherusersmanager', 'moodle-enrol-otherusersmanager-skin'); 00778 $function = 'M.enrol.otherusersmanager.init'; 00779 $arguments = array( 00780 'courseId'=> $this->manager->get_course()->id, 00781 'ajaxUrl' => '/enrol/ajax.php', 00782 'url' => $this->manager->get_moodlepage()->url->out(false)); 00783 $this->manager->get_moodlepage()->requires->yui_module($modules, $function, array($arguments)); 00784 } 00785 return $control; 00786 } 00787 }