2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Cohort role assignments table
20 * @package tool_cohortroles
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace tool_cohortroles\output;
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/tablelib.php');
37 * Cohort role assignments table.
39 * @package tool_cohortroles
40 * @copyright 2015 Damyon Wiese
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class cohort_role_assignments_table extends table_sql {
48 * @param string $uniqueid Unique id of table.
49 * @param moodle_url $url The base URL.
51 public function __construct($uniqueid, $url) {
53 parent::__construct($uniqueid);
54 $context = context_system::instance();
56 $this->context = $context;
58 $this->rolenames = role_get_names();
60 // This object should not be used without the right permissions.
61 require_capability('moodle/role:manage', $context);
63 $this->useridfield = 'userid';
65 // Define columns in the table.
66 $this->define_table_columns();
68 $this->define_baseurl($url);
70 $this->define_table_configs();
76 * @param array $data Row data.
79 protected function col_rolename($data) {
80 return $this->rolenames[$data->roleid]->localname;
86 * @param array $data Row data.
89 protected function col_cohortname($data) {
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
100 $context = context_helper::instance_by_id($data->cohortcontextid);
102 $exporter = new \core_cohort\external\cohort_summary_exporter($record, array('context' => $context));
103 $cohort = $exporter->export($OUTPUT);
105 $html = $OUTPUT->render_from_template('tool_cohortroles/cohort-in-list', $cohort);
112 * @param array $data Row data.
115 protected function col_actions($data) {
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);
126 * Setup the headers for the table.
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);
132 // Define headers and columns.
134 'cohortname' => get_string('cohort', 'cohort'),
135 'rolename' => get_string('role'),
136 'fullname' => get_string('name'),
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);
144 $cols[$field] = $field;
148 // Add remaining headers.
149 $cols = array_merge($cols, array('actions' => get_string('actions')));
151 $this->define_columns(array_keys($cols));
152 $this->define_headers(array_values($cols));
156 * Define table configs.
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');
166 * Builds the SQL query.
168 * @param bool $count When true, return the count SQL.
169 * @return array containing sql to use and an array of params.
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';
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;
182 $select = "COUNT(1)";
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";
192 // Check if any additional filtering is required.
193 [$sqlwhere, $params] = $this->get_sql_where();
195 $sql .= " WHERE {$sqlwhere}";
198 // Add order by if needed.
199 if (!$count && $sqlsort = $this->get_sql_sort()) {
200 $sql .= " ORDER BY " . $sqlsort;
203 return array($sql, $params);
207 * Override the default implementation to set a decent heading level.
209 public function print_nothing_to_display() {
211 echo $this->render_reset_button();
212 $this->print_initials_bar();
213 echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
219 * @param int $pagesize size of page for paginated displayed table.
220 * @param bool $useinitialsbar do you want to use the initials bar.
222 public function query_db($pagesize, $useinitialsbar = true) {
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());
232 if ($useinitialsbar) {
233 $this->initialbars($total > $pagesize);