MDL-68406 tool_mobile: Allow users to log-out from the app
[moodle.git] / admin / tool / mobile / lib.php
CommitLineData
18821583
JL
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 * Lib functions, mostly callbacks.
19 *
20 * @package tool_mobile
21 * @copyright 2017 Juan Leyva
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Callback to add head elements.
29 *
30 * @return str valid html head content
31 * @since Moodle 3.3
32 */
33function tool_mobile_before_standard_html_head() {
34 global $CFG, $PAGE;
35 $output = '';
36 // Smart App Banners meta tag is only displayed if mobile services are enabled and configured.
37 if (!empty($CFG->enablemobilewebservice)) {
38 $mobilesettings = get_config('tool_mobile');
e8eb8893
JL
39 if (!empty($mobilesettings->enablesmartappbanners)) {
40 if (!empty($mobilesettings->iosappid)) {
41 $output .= '<meta name="apple-itunes-app" content="app-id=' . s($mobilesettings->iosappid) . ', ';
42 $output .= 'app-argument=' . $PAGE->url->out() . '"/>';
43 }
44
45 if (!empty($mobilesettings->androidappid)) {
46 $mobilemanifesturl = "$CFG->wwwroot/$CFG->admin/tool/mobile/mobile.webmanifest.php";
47 $output .= '<link rel="manifest" href="'.$mobilemanifesturl.'" />';
48 }
18821583
JL
49 }
50 }
51 return $output;
52}
83f50acd
SL
53
54/**
55 * Generate the app download url to promote moodle mobile.
56 *
57 * @return moodle_url|void App download moodle_url object or return if setuplink is not set.
58 */
59function tool_mobile_create_app_download_url() {
60 global $CFG;
61
62 $mobilesettings = get_config('tool_mobile');
63
64 if (empty($mobilesettings->setuplink)) {
65 return;
66 }
67
68 $downloadurl = new moodle_url($mobilesettings->setuplink);
c9a95277
JL
69
70 // Do not update the URL if it is a custom one (we may break it completely).
71 if ($mobilesettings->setuplink != 'https://download.moodle.org/mobile') {
72 return $downloadurl;
73 }
74
83f50acd
SL
75 $downloadurl->param('version', $CFG->version);
76 $downloadurl->param('lang', current_language());
77
78 if (!empty($mobilesettings->iosappid)) {
79 $downloadurl->param('iosappid', $mobilesettings->iosappid);
80 }
81
82 if (!empty($mobilesettings->androidappid)) {
83 $downloadurl->param('androidappid', $mobilesettings->androidappid);
84 }
85
86 return $downloadurl;
87}
88
c6ebb024 89/**
0d1187b0 90 * Return the user mobile app WebService access token.
c6ebb024 91 *
0d1187b0 92 * @param int $userid the user to return the token from
9c28e07d 93 * @return stdClass|false the token or false if the token doesn't exists
0d1187b0 94 * @since 3.10
c6ebb024 95 */
0d1187b0 96function tool_mobile_get_token($userid) {
c6ebb024
JL
97 global $DB;
98
0d1187b0 99 $sql = "SELECT t.*
c6ebb024
JL
100 FROM {external_tokens} t, {external_services} s
101 WHERE t.externalserviceid = s.id
102 AND s.enabled = 1
103 AND s.shortname IN ('moodle_mobile_app', 'local_mobile')
104 AND t.userid = ?";
105
0d1187b0
JL
106 return $DB->get_record_sql($sql, [$userid], IGNORE_MULTIPLE);
107}
108
0d1187b0
JL
109/**
110 * Checks if the given user has a mobile token (has used recently the app).
111 *
112 * @param int $userid the user to check
113 * @return bool true if the user has a token, false otherwise.
114 */
115function tool_mobile_user_has_token($userid) {
116
117 return !empty(tool_mobile_get_token($userid));
c6ebb024
JL
118}
119
83f50acd
SL
120/**
121 * User profile page callback.
122 *
123 * Used add a section about the moodle mobile app.
124 *
125 * @param \core_user\output\myprofile\tree $tree My profile tree where the setting will be added.
126 * @param stdClass $user The user object.
127 * @param bool $iscurrentuser Is this the current user viewing
128 * @return void Return if the mobile web services setting is disabled or if not the current user.
129 */
130function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser) {
c6ebb024 131 global $CFG;
83f50acd
SL
132
133 if (empty($CFG->enablemobilewebservice)) {
134 return;
135 }
136
9df51510
JL
137 $newnodes = [];
138 $mobilesettings = get_config('tool_mobile');
139
af59fe58 140 // Check if we should display a QR code.
3dc820a0 141 if ($iscurrentuser && !empty($mobilesettings->qrcodetype)) {
af59fe58
JL
142 $mobileqr = null;
143 $qrcodeforappstr = get_string('qrcodeformobileappaccess', 'tool_mobile');
144
145 if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_LOGIN && is_https()) {
146
147 if (is_siteadmin() || \core\session\manager::is_loggedinas()) {
148 $mobileqr = get_string('qrsiteadminsnotallowed', 'tool_mobile');
149 } else {
150 $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
151
152 $minutes = tool_mobile\api::LOGIN_QR_KEY_TTL / MINSECS;
153 $mobileqr = html_writer::tag('p', get_string('qrcodeformobileapploginabout', 'tool_mobile', $minutes));
154 $mobileqr .= html_writer::link('#qrcode', get_string('viewqrcode', 'tool_mobile'),
155 ['class' => 'btn btn-primary mt-2', 'data-toggle' => 'collapse',
156 'role' => 'button', 'aria-expanded' => 'false']);
157 $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr), 'collapse mt-4', ['id' => 'qrcode']);
158 }
9df51510 159
af59fe58 160 } else if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_URL) {
9df51510
JL
161 $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
162
af59fe58
JL
163 $mobileqr = get_string('qrcodeformobileappurlabout', 'tool_mobile');
164 $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr));
9df51510
JL
165 }
166
af59fe58
JL
167 if ($mobileqr) {
168 $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappqr', $qrcodeforappstr, null, null, $mobileqr);
169 }
83f50acd
SL
170 }
171
9df51510 172 // Check if the user is using the app, encouraging him to use it otherwise.
0d1187b0 173 $usertoken = tool_mobile_get_token($user->id);
9df51510 174 $mobilestrconnected = null;
0d1187b0
JL
175 $mobilelastaccess = null;
176
177 if ($usertoken) {
178 $mobilestrconnected = get_string('lastsiteaccess');
179 if ($usertoken->lastaccess) {
180 $mobilelastaccess = userdate($usertoken->lastaccess) . "&nbsp; (" . format_time(time() - $usertoken->lastaccess) . ")";
3dc820a0
JL
181 // Logout link.
182 $validtoken = empty($usertoken->validuntil) || time() < $usertoken->validuntil;
183 if ($iscurrentuser && $validtoken) {
184 $url = new moodle_url('/'.$CFG->admin.'/tool/mobile/logout.php', ['sesskey' => sesskey()]);
185 $logoutlink = html_writer::link($url, get_string('logout'));
186 $mobilelastaccess .= "&nbsp; ($logoutlink)";
187 }
0d1187b0
JL
188 } else {
189 // We should not reach this point.
190 $mobilelastaccess = get_string("never");
191 }
9df51510
JL
192 } else if ($url = tool_mobile_create_app_download_url()) {
193 $mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out());
194 }
195
196 if ($mobilestrconnected) {
0d1187b0
JL
197 $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null, null,
198 $mobilelastaccess);
83f50acd
SL
199 }
200
9df51510
JL
201 // Add nodes, if any.
202 if (!empty($newnodes)) {
203 $mobilecat = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'), 'loginactivity');
204 $tree->add_category($mobilecat);
205
206 foreach ($newnodes as $node) {
207 $tree->add_node($node);
208 }
209 }
83f50acd 210}
1e68076e
AN
211
212/**
213 * Callback to add footer elements.
214 *
215 * @return str valid html footer content
216 * @since Moodle 3.4
217 */
218function tool_mobile_standard_footer_html() {
219 global $CFG;
220 $output = '';
221 if (!empty($CFG->enablemobilewebservice) && $url = tool_mobile_create_app_download_url()) {
222 $output .= html_writer::link($url, get_string('getmoodleonyourmobile', 'tool_mobile'));
223 }
224 return $output;
225}
c6ebb024
JL
226
227/**
228 * Callback to be able to change a message/notification data per processor.
229 *
230 * @param str $procname processor name
231 * @param stdClass $data message or notification data
232 */
233function tool_mobile_pre_processor_message_send($procname, $data) {
234 global $CFG;
235
236 if (empty($CFG->enablemobilewebservice)) {
237 return;
238 }
239
240 if (empty($data->userto)) {
241 return;
242 }
243
244 // Only hack email.
245 if ($procname == 'email') {
246
247 // Send a message only when there is an HTML version of the email, mobile services are enabled,
248 // the user receiving the message has not used the app and there is an app download URL set.
249 if (empty($data->fullmessagehtml)) {
250 return;
251 }
252
253 if (!$url = tool_mobile_create_app_download_url()) {
254 return;
255 }
256
257 $userto = is_object($data->userto) ? $data->userto->id : $data->userto;
258 if (tool_mobile_user_has_token($userto)) {
259 return;
260 }
261
262 $data->fullmessagehtml .= html_writer::tag('p', get_string('readingthisemailgettheapp', 'tool_mobile', $url->out()));
263 }
264}