Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Cohort role assignments table 19 * 20 * @package tool_cohortroles 21 * @copyright 2015 Damyon Wiese 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace tool_cohortroles\output; 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->libdir . '/tablelib.php'); 29 30 use context_helper; 31 use context_system; 32 use html_writer; 33 use moodle_url; 34 use table_sql; 35 36 /** 37 * Cohort role assignments table. 38 * 39 * @package tool_cohortroles 40 * @copyright 2015 Damyon Wiese 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 42 */ 43 class cohort_role_assignments_table extends table_sql { 44 45 /** 46 * Sets up the table. 47 * 48 * @param string $uniqueid Unique id of table. 49 * @param moodle_url $url The base URL. 50 */ 51 public function __construct($uniqueid, $url) { 52 global $CFG; 53 parent::__construct($uniqueid); 54 $context = context_system::instance(); 55 56 $this->context = $context; 57 58 $this->rolenames = role_get_names(); 59 60 // This object should not be used without the right permissions. 61 require_capability('moodle/role:manage', $context); 62 63 $this->useridfield = 'userid'; 64 65 // Define columns in the table. 66 $this->define_table_columns(); 67 68 $this->define_baseurl($url); 69 // Define configs. 70 $this->define_table_configs(); 71 } 72 73 /** 74 * Role name column. 75 * 76 * @param array $data Row data. 77 * @return string 78 */ 79 protected function col_rolename($data) { 80 return $this->rolenames[$data->roleid]->localname; 81 } 82 83 /** 84 * Cohort name column. 85 * 86 * @param array $data Row data. 87 * @return string 88 */ 89 protected function col_cohortname($data) { 90 global $OUTPUT; 91 92 $record = (object) array( 93 'id' => $data->cohortid, 94 'idnumber' => $data->cohortidnumber, 95 'description' => $data->cohortdescription, 96 'visible' => $data->cohortvisible, 97 'name' => $data->cohortname, 98 'theme' => $data->cohorttheme 99 ); 100 $context = context_helper::instance_by_id($data->cohortcontextid); 101 102 $exporter = new \core_cohort\external\cohort_summary_exporter($record, array('context' => $context)); 103 $cohort = $exporter->export($OUTPUT); 104 105 $html = $OUTPUT->render_from_template('tool_cohortroles/cohort-in-list', $cohort); 106 return $html; 107 } 108 109 /** 110 * Actions column. 111 * 112 * @param array $data Row data. 113 * @return string 114 */ 115 protected function col_actions($data) { 116 global $OUTPUT; 117 118 $action = new \confirm_action(get_string('removecohortroleassignmentconfirm', 'tool_cohortroles')); 119 $url = new moodle_url($this->baseurl); 120 $url->params(array('removecohortroleassignment' => $data->id, 'sesskey' => sesskey())); 121 $pix = new \pix_icon('t/delete', get_string('removecohortroleassignment', 'tool_cohortroles')); 122 return $OUTPUT->action_link($url, '', $action, null, $pix); 123 } 124 125 /** 126 * Setup the headers for the table. 127 */ 128 protected function define_table_columns() { 129 // TODO Does not support custom user profile fields (MDL-70456). 130 $extrafields = \core_user\fields::get_identity_fields($this->context, false); 131 132 // Define headers and columns. 133 $cols = array( 134 'cohortname' => get_string('cohort', 'cohort'), 135 'rolename' => get_string('role'), 136 'fullname' => get_string('name'), 137 ); 138 139 // Add headers for extra user fields. 140 foreach ($extrafields as $field) { 141 if (get_string_manager()->string_exists($field, 'moodle')) { 142 $cols[$field] = get_string($field); 143 } else { 144 $cols[$field] = $field; 145 } 146 } 147 148 // Add remaining headers. 149 $cols = array_merge($cols, array('actions' => get_string('actions'))); 150 151 $this->define_columns(array_keys($cols)); 152 $this->define_headers(array_values($cols)); 153 } 154 155 /** 156 * Define table configs. 157 */ 158 protected function define_table_configs() { 159 $this->collapsible(false); 160 $this->sortable(true, 'lastname', SORT_ASC); 161 $this->pageable(true); 162 $this->no_sorting('actions'); 163 } 164 165 /** 166 * Builds the SQL query. 167 * 168 * @param bool $count When true, return the count SQL. 169 * @return array containing sql to use and an array of params. 170 */ 171 protected function get_sql_and_params($count = false) { 172 $fields = 'uca.id, uca.cohortid, uca.userid, uca.roleid, '; 173 $fields .= 'c.name as cohortname, c.idnumber as cohortidnumber, c.contextid as cohortcontextid, '; 174 $fields .= 'c.visible as cohortvisible, c.description as cohortdescription, c.theme as cohorttheme'; 175 176 // Add extra user fields that we need for the graded user. 177 // TODO Does not support custom user profile fields (MDL-70456). 178 $userfieldsapi = \core_user\fields::for_identity($this->context, false)->with_name(); 179 $fields .= $userfieldsapi->get_sql('u')->selects; 180 181 if ($count) { 182 $select = "COUNT(1)"; 183 } else { 184 $select = "$fields"; 185 } 186 187 $sql = "SELECT $select 188 FROM {tool_cohortroles} uca 189 JOIN {user} u ON u.id = uca.userid 190 JOIN {cohort} c ON c.id = uca.cohortid"; 191 192 // Check if any additional filtering is required. 193 [$sqlwhere, $params] = $this->get_sql_where(); 194 if ($sqlwhere) { 195 $sql .= " WHERE {$sqlwhere}"; 196 } 197 198 // Add order by if needed. 199 if (!$count && $sqlsort = $this->get_sql_sort()) { 200 $sql .= " ORDER BY " . $sqlsort; 201 } 202 203 return array($sql, $params); 204 } 205 206 /** 207 * Override the default implementation to set a decent heading level. 208 */ 209 public function print_nothing_to_display() { 210 global $OUTPUT; 211 echo $this->render_reset_button(); 212 $this->print_initials_bar(); 213 echo $OUTPUT->heading(get_string('nothingtodisplay'), 4); 214 } 215 216 /** 217 * Query the DB. 218 * 219 * @param int $pagesize size of page for paginated displayed table. 220 * @param bool $useinitialsbar do you want to use the initials bar. 221 */ 222 public function query_db($pagesize, $useinitialsbar = true) { 223 global $DB; 224 225 list($countsql, $countparams) = $this->get_sql_and_params(true); 226 list($sql, $params) = $this->get_sql_and_params(); 227 $total = $DB->count_records_sql($countsql, $countparams); 228 $this->pagesize($pagesize, $total); 229 $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size()); 230 231 // Set initial bars. 232 if ($useinitialsbar) { 233 $this->initialbars($total > $pagesize); 234 } 235 } 236 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body