Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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 25 defined('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 */ 33 function 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'); 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 } 49 } 50 } 51 return $output; 52 } 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 */ 59 function 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); 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 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 // For privacy reasons, add siteurl param only if the site is registered. 87 // This is to implement Google Play Referrer (so the site url is automatically populated in the app after installation). 88 if (\core\hub\registration::is_registered()) { 89 $downloadurl->param('siteurl', $CFG->wwwroot); 90 } 91 92 return $downloadurl; 93 } 94 95 /** 96 * Return the user mobile app WebService access token. 97 * 98 * @param int $userid the user to return the token from 99 * @return stdClass|false the token or false if the token doesn't exists 100 * @since 3.10 101 */ 102 function tool_mobile_get_token($userid) { 103 global $DB; 104 105 $sql = "SELECT t.* 106 FROM {external_tokens} t, {external_services} s 107 WHERE t.externalserviceid = s.id 108 AND s.enabled = 1 109 AND s.shortname IN ('moodle_mobile_app', 'local_mobile') 110 AND t.userid = ?"; 111 112 return $DB->get_record_sql($sql, [$userid], IGNORE_MULTIPLE); 113 } 114 115 /** 116 * Checks if the given user has a mobile token (has used recently the app). 117 * 118 * @param int $userid the user to check 119 * @return bool true if the user has a token, false otherwise. 120 */ 121 function tool_mobile_user_has_token($userid) { 122 123 return !empty(tool_mobile_get_token($userid)); 124 } 125 126 /** 127 * User profile page callback. 128 * 129 * Used add a section about the moodle mobile app. 130 * 131 * @param \core_user\output\myprofile\tree $tree My profile tree where the setting will be added. 132 * @param stdClass $user The user object. 133 * @param bool $iscurrentuser Is this the current user viewing 134 * @return void Return if the mobile web services setting is disabled or if not the current user. 135 */ 136 function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser) { 137 global $CFG; 138 139 if (empty($CFG->enablemobilewebservice)) { 140 return; 141 } 142 143 $newnodes = []; 144 $mobilesettings = get_config('tool_mobile'); 145 146 // Check if we should display a QR code. 147 if ($iscurrentuser && !empty($mobilesettings->qrcodetype)) { 148 $mobileqr = null; 149 $qrcodeforappstr = get_string('qrcodeformobileappaccess', 'tool_mobile'); 150 151 if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_LOGIN && is_https()) { 152 153 if (is_siteadmin() || \core\session\manager::is_loggedinas()) { 154 $mobileqr = get_string('qrsiteadminsnotallowed', 'tool_mobile'); 155 } else { 156 $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings); 157 158 $qrkeyttl = !empty($mobilesettings->qrkeyttl) ? $mobilesettings->qrkeyttl : tool_mobile\api::LOGIN_QR_KEY_TTL; 159 $mobileqr = html_writer::tag('p', get_string('qrcodeformobileapploginabout', 'tool_mobile', 160 format_time($qrkeyttl))); 161 $mobileqr .= html_writer::link('#qrcode', get_string('viewqrcode', 'tool_mobile'), 162 ['class' => 'btn btn-primary mt-2', 'data-toggle' => 'collapse', 163 'role' => 'button', 'aria-expanded' => 'false']); 164 $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr), 'collapse mt-4', ['id' => 'qrcode']); 165 } 166 167 } else if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_URL) { 168 $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings); 169 170 $mobileqr = get_string('qrcodeformobileappurlabout', 'tool_mobile'); 171 $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr)); 172 } 173 174 if ($mobileqr) { 175 $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappqr', $qrcodeforappstr, null, null, $mobileqr); 176 } 177 } 178 179 // Check if the user is using the app, encouraging him to use it otherwise. 180 if ($iscurrentuser || is_siteadmin()) { 181 $usertoken = tool_mobile_get_token($user->id); 182 $mobilestrconnected = null; 183 $mobilelastaccess = null; 184 185 if ($usertoken) { 186 $mobilestrconnected = get_string('lastsiteaccess'); 187 if ($usertoken->lastaccess) { 188 $mobilelastaccess = userdate($usertoken->lastaccess) . " (" . format_time(time() - $usertoken->lastaccess) . ")"; 189 // Logout link. 190 $validtoken = empty($usertoken->validuntil) || time() < $usertoken->validuntil; 191 if ($iscurrentuser && $validtoken) { 192 $url = new moodle_url('/'.$CFG->admin.'/tool/mobile/logout.php', ['sesskey' => sesskey()]); 193 $logoutlink = html_writer::link($url, get_string('logout')); 194 $mobilelastaccess .= " ($logoutlink)"; 195 } 196 } else { 197 // We should not reach this point. 198 $mobilelastaccess = get_string("never"); 199 } 200 } else if ($url = tool_mobile_create_app_download_url()) { 201 $mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out()); 202 } 203 204 if ($mobilestrconnected) { 205 $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null, null, 206 $mobilelastaccess); 207 } 208 } 209 210 // Add nodes, if any. 211 if (!empty($newnodes)) { 212 $mobilecat = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'), 'loginactivity'); 213 $tree->add_category($mobilecat); 214 215 foreach ($newnodes as $node) { 216 $tree->add_node($node); 217 } 218 } 219 } 220 221 /** 222 * Callback to add footer elements. 223 * 224 * @return str valid html footer content 225 * @since Moodle 3.4 226 */ 227 function tool_mobile_standard_footer_html() { 228 global $CFG; 229 $output = ''; 230 if (!empty($CFG->enablemobilewebservice) && $url = tool_mobile_create_app_download_url()) { 231 $output .= html_writer::link($url, get_string('getmoodleonyourmobile', 'tool_mobile'), ['class' => 'mobilelink']); 232 } 233 return $output; 234 } 235 236 /** 237 * Callback to be able to change a message/notification data per processor. 238 * 239 * @param str $procname processor name 240 * @param stdClass $data message or notification data 241 */ 242 function tool_mobile_pre_processor_message_send($procname, $data) { 243 global $CFG; 244 245 if (empty($CFG->enablemobilewebservice)) { 246 return; 247 } 248 249 if (empty($data->userto)) { 250 return; 251 } 252 253 // Only hack email. 254 if ($procname == 'email') { 255 256 // Send a message only when there is an HTML version of the email, mobile services are enabled, 257 // the user receiving the message has not used the app and there is an app download URL set. 258 if (empty($data->fullmessagehtml)) { 259 return; 260 } 261 262 if (!$url = tool_mobile_create_app_download_url()) { 263 return; 264 } 265 266 $userto = is_object($data->userto) ? $data->userto->id : $data->userto; 267 if (tool_mobile_user_has_token($userto)) { 268 return; 269 } 270 271 $data->fullmessagehtml .= html_writer::tag('p', get_string('readingthisemailgettheapp', 'tool_mobile', $url->out())); 272 } 273 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body