Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
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 /** @var context_system */ 46 protected ?context_system $context = null; 47 48 /** @var array */ 49 protected array $rolenames = []; 50 51 /** 52 * Sets up the table. 53 * 54 * @param string $uniqueid Unique id of table. 55 * @param moodle_url $url The base URL. 56 */ 57 public function __construct($uniqueid, $url) { 58 global $CFG; 59 parent::__construct($uniqueid); 60 $context = context_system::instance(); 61 62 $this->context = $context; 63 64 $this->rolenames = role_get_names(); 65 66 // This object should not be used without the right permissions. 67 require_capability('moodle/role:manage', $context); 68 69 $this->useridfield = 'userid'; 70 71 // Define columns in the table. 72 $this->define_table_columns(); 73 74 $this->define_baseurl($url); 75 // Define configs. 76 $this->define_table_configs(); 77 } 78 79 /** 80 * Role name column. 81 * 82 * @param array $data Row data. 83 * @return string 84 */ 85 protected function col_rolename($data) { 86 return $this->rolenames[$data->roleid]->localname; 87 } 88 89 /** 90 * Cohort name column. 91 * 92 * @param array $data Row data. 93 * @return string 94 */ 95 protected function col_cohortname($data) { 96 global $OUTPUT; 97 98 $record = (object) array( 99 'id' => $data->cohortid, 100 'idnumber' => $data->cohortidnumber, 101 'description' => $data->cohortdescription, 102 'visible' => $data->cohortvisible, 103 'name' => $data->cohortname, 104 'theme' => $data->cohorttheme 105 ); 106 $context = context_helper::instance_by_id($data->cohortcontextid); 107 108 $exporter = new \core_cohort\external\cohort_summary_exporter($record, array('context' => $context)); 109 $cohort = $exporter->export($OUTPUT); 110 111 $html = $OUTPUT->render_from_template('tool_cohortroles/cohort-in-list', $cohort); 112 return $html; 113 } 114 115 /** 116 * Actions column. 117 * 118 * @param array $data Row data. 119 * @return string 120 */ 121 protected function col_actions($data) { 122 global $OUTPUT; 123 124 $action = new \confirm_action(get_string('removecohortroleassignmentconfirm', 'tool_cohortroles')); 125 $url = new moodle_url($this->baseurl); 126 $url->params(array('removecohortroleassignment' => $data->id, 'sesskey' => sesskey())); 127 $pix = new \pix_icon('t/delete', get_string('removecohortroleassignment', 'tool_cohortroles')); 128 return $OUTPUT->action_link($url, '', $action, null, $pix); 129 } 130 131 /** 132 * Setup the headers for the table. 133 */ 134 protected function define_table_columns() { 135 // TODO Does not support custom user profile fields (MDL-70456). 136 $extrafields = \core_user\fields::get_identity_fields($this->context, false); 137 138 // Define headers and columns. 139 $cols = array( 140 'cohortname' => get_string('cohort', 'cohort'), 141 'rolename' => get_string('role'), 142 'fullname' => get_string('name'), 143 ); 144 145 // Add headers for extra user fields. 146 foreach ($extrafields as $field) { 147 if (get_string_manager()->string_exists($field, 'moodle')) { 148 $cols[$field] = get_string($field); 149 } else { 150 $cols[$field] = $field; 151 } 152 } 153 154 // Add remaining headers. 155 $cols = array_merge($cols, array('actions' => get_string('actions'))); 156 157 $this->define_columns(array_keys($cols)); 158 $this->define_headers(array_values($cols)); 159 } 160 161 /** 162 * Define table configs. 163 */ 164 protected function define_table_configs() { 165 $this->collapsible(false); 166 $this->sortable(true, 'lastname', SORT_ASC); 167 $this->pageable(true); 168 $this->no_sorting('actions'); 169 } 170 171 /** 172 * Builds the SQL query. 173 * 174 * @param bool $count When true, return the count SQL. 175 * @return array containing sql to use and an array of params. 176 */ 177 protected function get_sql_and_params($count = false) { 178 $fields = 'uca.id, uca.cohortid, uca.userid, uca.roleid, '; 179 $fields .= 'c.name as cohortname, c.idnumber as cohortidnumber, c.contextid as cohortcontextid, '; 180 $fields .= 'c.visible as cohortvisible, c.description as cohortdescription, c.theme as cohorttheme'; 181 182 // Add extra user fields that we need for the graded user. 183 // TODO Does not support custom user profile fields (MDL-70456). 184 $userfieldsapi = \core_user\fields::for_identity($this->context, false)->with_name(); 185 $fields .= $userfieldsapi->get_sql('u')->selects; 186 187 if ($count) { 188 $select = "COUNT(1)"; 189 } else { 190 $select = "$fields"; 191 } 192 193 $sql = "SELECT $select 194 FROM {tool_cohortroles} uca 195 JOIN {user} u ON u.id = uca.userid 196 JOIN {cohort} c ON c.id = uca.cohortid"; 197 198 // Check if any additional filtering is required. 199 [$sqlwhere, $params] = $this->get_sql_where(); 200 if ($sqlwhere) { 201 $sql .= " WHERE {$sqlwhere}"; 202 } 203 204 // Add order by if needed. 205 if (!$count && $sqlsort = $this->get_sql_sort()) { 206 $sql .= " ORDER BY " . $sqlsort; 207 } 208 209 return array($sql, $params); 210 } 211 212 /** 213 * Override the default implementation to set a decent heading level. 214 */ 215 public function print_nothing_to_display() { 216 global $OUTPUT; 217 echo $this->render_reset_button(); 218 $this->print_initials_bar(); 219 echo $OUTPUT->heading(get_string('nothingtodisplay'), 4); 220 } 221 222 /** 223 * Query the DB. 224 * 225 * @param int $pagesize size of page for paginated displayed table. 226 * @param bool $useinitialsbar do you want to use the initials bar. 227 */ 228 public function query_db($pagesize, $useinitialsbar = true) { 229 global $DB; 230 231 list($countsql, $countparams) = $this->get_sql_and_params(true); 232 list($sql, $params) = $this->get_sql_and_params(); 233 $total = $DB->count_records_sql($countsql, $countparams); 234 $this->pagesize($pagesize, $total); 235 $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size()); 236 237 // Set initial bars. 238 if ($useinitialsbar) { 239 $this->initialbars($total > $pagesize); 240 } 241 } 242 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body