MDL-59936 calendar: use icons in event summary modal
[moodle.git] / webservice / classes / token_table.php
blob059fc049d8fcbc027de9aba7c56e0d84c8c272da
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.
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/>.
17 /**
18 * Contains the class used for the displaying the tokens table.
20 * @package core_webservice
21 * @copyright 2017 John Okely <john@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace webservice;
27 defined('MOODLE_INTERNAL') || die;
29 require_once($CFG->libdir . '/tablelib.php');
30 require_once($CFG->dirroot . '/webservice/lib.php');
31 require_once($CFG->dirroot . '/user/lib.php');
33 /**
34 * Class for the displaying the participants table.
36 * @package core_webservice
37 * @copyright 2017 John Okely <john@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class token_table extends \table_sql {
42 /**
43 * @var bool $showalltokens Whether or not the user is able to see all tokens.
45 protected $showalltokens;
47 /**
48 * Sets up the table.
49 * @param int $id The id of the table
51 public function __construct($id) {
52 parent::__construct($id);
54 // Get the context.
55 $context = \context_system::instance();
57 // Can we see tokens created by all users?
58 $this->showalltokens = has_capability('moodle/webservice:managealltokens', $context);
60 // Define the headers and columns.
61 $headers = [];
62 $columns = [];
64 $headers[] = get_string('token', 'webservice');
65 $columns[] = 'token';
66 $headers[] = get_string('user');
67 $columns[] = 'fullname';
68 $headers[] = get_string('service', 'webservice');
69 $columns[] = 'name';
70 $headers[] = get_string('iprestriction', 'webservice');
71 $columns[] = 'iprestriction';
72 $headers[] = get_string('validuntil', 'webservice');
73 $columns[] = 'validuntil';
74 if ($this->showalltokens) {
75 // Only need to show creator if you can see tokens created by other people.
76 $headers[] = get_string('tokencreator', 'webservice');
77 $columns[] = 'creatorlastname'; // So we can have semi-useful sorting. Table SQL doesn't two fullname collumns.
79 $headers[] = get_string('operation', 'webservice');
80 $columns[] = 'operation';
82 $this->define_columns($columns);
83 $this->define_headers($headers);
85 $this->no_sorting('operation');
86 $this->no_sorting('token');
87 $this->no_sorting('iprestriction');
89 $this->set_attribute('id', $id);
92 /**
93 * Generate the operation column.
95 * @param \stdClass $data Data for the current row
96 * @return string Content for the column
98 public function col_operation($data) {
99 $tokenpageurl = new \moodle_url(
100 "/admin/webservice/tokens.php",
102 "sesskey" => sesskey(),
103 "action" => "delete",
104 "tokenid" => $data->id
107 return \html_writer::link($tokenpageurl, get_string("delete"));
111 * Generate the validuntil column.
113 * @param \stdClass $data Data for the current row
114 * @return string Content for the column
116 public function col_validuntil($data) {
117 if (empty($data->validuntil)) {
118 return '';
119 } else {
120 return userdate($data->validuntil, get_string('strftimedatetime', 'langconfig'));
125 * Generate the fullname column. Also includes capabilities the user is missing for the webservice (if any)
127 * @param \stdClass $data Data for the current row
128 * @return string Content for the column
130 public function col_fullname($data) {
131 global $OUTPUT;
133 $userprofilurl = new \moodle_url('/user/profile.php', ['id' => $data->userid]);
134 $content = \html_writer::link($userprofilurl, fullname($data));
136 // Make up list of capabilities that the user is missing for the given webservice.
137 $webservicemanager = new \webservice();
138 $usermissingcaps = $webservicemanager->get_missing_capabilities_by_users([['id' => $data->userid]], $data->serviceid);
140 if (!is_siteadmin($data->userid) && array_key_exists($data->userid, $usermissingcaps)) {
141 $missingcapabilities = implode(', ', $usermissingcaps[$data->userid]);
142 if (!empty($missingcapabilities)) {
143 $capabilitiesstring = get_string('usermissingcaps', 'webservice', $missingcapabilities) . '&nbsp;' .
144 $OUTPUT->help_icon('missingcaps', 'webservice');
145 $content .= \html_writer::div($capabilitiesstring, 'missingcaps');
149 return $content;
153 * Generate the token column.
155 * @param \stdClass $data Data for the current row
156 * @return string Content for the column
158 public function col_token($data) {
159 global $USER;
160 // Hide the token if it wasn't created by the current user.
161 if ($data->creatorid != $USER->id) {
162 return '-';
165 return $data->token;
169 * Generate the creator column.
171 * @param \stdClass $data
172 * @return string
174 public function col_creatorlastname($data) {
175 // We have loaded all the name fields for the creator, with the 'creator' prefix.
176 // So just remove the prefix and make up a user object.
177 $user = [];
178 foreach ($data as $key => $value) {
179 if (strpos($key, 'creator') !== false) {
180 $newkey = str_replace('creator', '', $key);
181 $user[$newkey] = $value;
185 $creatorprofileurl = new \moodle_url('/user/profile.php', ['id' => $data->creatorid]);
186 return \html_writer::link($creatorprofileurl, fullname((object)$user));
190 * This function is used for the extra user fields.
192 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as
193 * the list has the potential to increase in the future and we don't want to have to remember to add
194 * a new method to this class. We also don't want to pollute this class with unnecessary methods.
196 * @param string $colname The column name
197 * @param \stdClass $data
198 * @return string
200 public function other_cols($colname, $data) {
201 return s($data->{$colname});
205 * Query the database for results to display in the table.
207 * Note: Initial bars are not implemented for this table because it includes user details twice and the initial bars do not work
208 * when the user table is included more than once.
210 * @param int $pagesize size of page for paginated displayed table.
211 * @param bool $useinitialsbar Not implemented. Please pass false.
213 public function query_db($pagesize, $useinitialsbar = false) {
214 global $DB, $USER;
216 if ($useinitialsbar) {
217 debugging('Initial bar not implemented yet. Call out($pagesize, false)');
220 $usernamefields = get_all_user_name_fields(true, 'u');
221 $creatorfields = get_all_user_name_fields(true, 'c', null, 'creator');
223 $params = ["tokenmode" => EXTERNAL_TOKEN_PERMANENT];
225 // TODO: in order to let the administrator delete obsolete token, split the request in multiple request or use LEFT JOIN.
227 if ($this->showalltokens) {
228 // Show all tokens.
229 $sql = "SELECT t.id, t.token, u.id AS userid, $usernamefields, s.name, t.iprestriction, t.validuntil, s.id AS serviceid,
230 t.creatorid, $creatorfields
231 FROM {external_tokens} t, {user} u, {external_services} s, {user} c
232 WHERE t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND c.id = t.creatorid";
233 $countsql = "SELECT COUNT(t.id)
234 FROM {external_tokens} t, {user} u, {external_services} s, {user} c
235 WHERE t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND c.id = t.creatorid";
236 } else {
237 // Only show tokens created by the current user.
238 $sql = "SELECT t.id, t.token, u.id AS userid, $usernamefields, s.name, t.iprestriction, t.validuntil, s.id AS serviceid,
239 t.creatorid, $creatorfields
240 FROM {external_tokens} t, {user} u, {external_services} s, {user} c
241 WHERE t.creatorid=:userid AND t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND
242 c.id = t.creatorid";
243 $countsql = "SELECT COUNT(t.id)
244 FROM {external_tokens} t, {user} u, {external_services} s, {user} c
245 WHERE t.creatorid=:userid AND t.tokentype = :tokenmode AND s.id = t.externalserviceid AND
246 t.userid = u.id AND c.id = t.creatorid";
247 $params["userid"] = $USER->id;
250 $sort = $this->get_sql_sort();
251 if ($sort) {
252 $sql = $sql . ' ORDER BY ' . $sort;
255 $total = $DB->count_records_sql($countsql, $params);
256 $this->pagesize($pagesize, $total);
258 $this->rawdata = $DB->get_recordset_sql($sql, $params, $this->get_page_start(), $this->get_page_size());