Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 return $downloadurl; 87 } 88 89 /** 90 * Return the user mobile app WebService access token. 91 * 92 * @param int $userid the user to return the token from 93 * @return stdClass|false the token or false if the token doesn't exists 94 * @since 3.10 95 */ 96 function tool_mobile_get_token($userid) { 97 global $DB; 98 99 $sql = "SELECT t.* 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 106 return $DB->get_record_sql($sql, [$userid], IGNORE_MULTIPLE); 107 } 108 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 */ 115 function tool_mobile_user_has_token($userid) { 116 117 return !empty(tool_mobile_get_token($userid)); 118 } 119 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 */ 130 function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser) { 131 global $CFG; 132 133 if (empty($CFG->enablemobilewebservice)) { 134 return; 135 } 136 137 $newnodes = []; 138 $mobilesettings = get_config('tool_mobile'); 139 140 // Check if we should display a QR code. 141 if ($iscurrentuser && !empty($mobilesettings->qrcodetype)) { 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 } 159 160 } else if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_URL) { 161 $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings); 162 163 $mobileqr = get_string('qrcodeformobileappurlabout', 'tool_mobile'); 164 $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr)); 165 } 166 167 if ($mobileqr) { 168 $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappqr', $qrcodeforappstr, null, null, $mobileqr); 169 } 170 } 171 172 // Check if the user is using the app, encouraging him to use it otherwise. 173 if ($iscurrentuser || is_siteadmin()) { 174 $usertoken = tool_mobile_get_token($user->id); 175 $mobilestrconnected = null; 176 $mobilelastaccess = null; 177 178 if ($usertoken) { 179 $mobilestrconnected = get_string('lastsiteaccess'); 180 if ($usertoken->lastaccess) { 181 $mobilelastaccess = userdate($usertoken->lastaccess) . " (" . format_time(time() - $usertoken->lastaccess) . ")"; 182 // Logout link. 183 $validtoken = empty($usertoken->validuntil) || time() < $usertoken->validuntil; 184 if ($iscurrentuser && $validtoken) { 185 $url = new moodle_url('/'.$CFG->admin.'/tool/mobile/logout.php', ['sesskey' => sesskey()]); 186 $logoutlink = html_writer::link($url, get_string('logout')); 187 $mobilelastaccess .= " ($logoutlink)"; 188 } 189 } else { 190 // We should not reach this point. 191 $mobilelastaccess = get_string("never"); 192 } 193 } else if ($url = tool_mobile_create_app_download_url()) { 194 $mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out()); 195 } 196 197 if ($mobilestrconnected) { 198 $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null, null, 199 $mobilelastaccess); 200 } 201 } 202 203 // Add nodes, if any. 204 if (!empty($newnodes)) { 205 $mobilecat = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'), 'loginactivity'); 206 $tree->add_category($mobilecat); 207 208 foreach ($newnodes as $node) { 209 $tree->add_node($node); 210 } 211 } 212 } 213 214 /** 215 * Callback to add footer elements. 216 * 217 * @return str valid html footer content 218 * @since Moodle 3.4 219 */ 220 function tool_mobile_standard_footer_html() { 221 global $CFG; 222 $output = ''; 223 if (!empty($CFG->enablemobilewebservice) && $url = tool_mobile_create_app_download_url()) { 224 $output .= html_writer::link($url, get_string('getmoodleonyourmobile', 'tool_mobile')); 225 } 226 return $output; 227 } 228 229 /** 230 * Callback to be able to change a message/notification data per processor. 231 * 232 * @param str $procname processor name 233 * @param stdClass $data message or notification data 234 */ 235 function tool_mobile_pre_processor_message_send($procname, $data) { 236 global $CFG; 237 238 if (empty($CFG->enablemobilewebservice)) { 239 return; 240 } 241 242 if (empty($data->userto)) { 243 return; 244 } 245 246 // Only hack email. 247 if ($procname == 'email') { 248 249 // Send a message only when there is an HTML version of the email, mobile services are enabled, 250 // the user receiving the message has not used the app and there is an app download URL set. 251 if (empty($data->fullmessagehtml)) { 252 return; 253 } 254 255 if (!$url = tool_mobile_create_app_download_url()) { 256 return; 257 } 258 259 $userto = is_object($data->userto) ? $data->userto->id : $data->userto; 260 if (tool_mobile_user_has_token($userto)) { 261 return; 262 } 263 264 $data->fullmessagehtml .= html_writer::tag('p', get_string('readingthisemailgettheapp', 'tool_mobile', $url->out())); 265 } 266 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body