From fada51aa32f4ec3004a72f978d62c07f5ff2f824 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 15 Sep 2020 18:45:49 +0200 Subject: [PATCH] MDL-67841 tool_mobile: Inform about last access with the app --- admin/tool/mobile/lang/en/deprecated.txt | 1 + admin/tool/mobile/lang/en/tool_mobile.php | 4 ++- admin/tool/mobile/lib.php | 41 +++++++++++++++++------ 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 admin/tool/mobile/lang/en/deprecated.txt diff --git a/admin/tool/mobile/lang/en/deprecated.txt b/admin/tool/mobile/lang/en/deprecated.txt new file mode 100644 index 00000000000..0edc1360939 --- /dev/null +++ b/admin/tool/mobile/lang/en/deprecated.txt @@ -0,0 +1 @@ +mobileappconnected,tool_mobile \ No newline at end of file diff --git a/admin/tool/mobile/lang/en/tool_mobile.php b/admin/tool/mobile/lang/en/tool_mobile.php index 678a1b56ec1..6fafc5fbd54 100644 --- a/admin/tool/mobile/lang/en/tool_mobile.php +++ b/admin/tool/mobile/lang/en/tool_mobile.php @@ -88,7 +88,6 @@ $string['managefiletypes'] = 'Manage file types'; $string['minimumversion'] = 'If an app version is specified (3.8.0 or higher), any users using an older app version will be prompted to upgrade their app before being allowed access to the site.'; $string['minimumversion_key'] = 'Minimum app version required'; $string['mobileapp'] = 'Mobile app'; -$string['mobileappconnected'] = 'Mobile app connected'; $string['mobileappenabled'] = 'This site has mobile app access enabled.
Download the mobile app.'; $string['mobileappearance'] = 'Mobile appearance'; $string['mobileappsubscription'] = 'Moodle app subscription'; @@ -144,3 +143,6 @@ $string['privacy:metadata:preference:tool_mobile_autologin_request_last'] = 'The $string['privacy:metadata:core_userkey'] = 'User\'s keys used to create auto-login key for the current user.'; $string['responsivemainmenuitems'] = 'Responsive menu items'; $string['viewqrcode'] = 'View QR code'; + +// Deprecated since Moodle 3.10. +$string['mobileappconnected'] = 'Mobile app connected'; diff --git a/admin/tool/mobile/lib.php b/admin/tool/mobile/lib.php index 43d6cc23e92..fa3273d96cd 100644 --- a/admin/tool/mobile/lib.php +++ b/admin/tool/mobile/lib.php @@ -87,22 +87,35 @@ function tool_mobile_create_app_download_url() { } /** - * Checks if the given user has a mobile token (has used recently the app). + * Return the user mobile app WebService access token. * - * @param int $userid the user to check - * @return bool true if the user has a token, false otherwise. + * @param int $userid the user to return the token from + * @return stdClass the token + * @since 3.10 */ -function tool_mobile_user_has_token($userid) { +function tool_mobile_get_token($userid) { global $DB; - $sql = "SELECT 1 + $sql = "SELECT t.* FROM {external_tokens} t, {external_services} s WHERE t.externalserviceid = s.id AND s.enabled = 1 AND s.shortname IN ('moodle_mobile_app', 'local_mobile') AND t.userid = ?"; - return $DB->record_exists_sql($sql, [$userid]); + return $DB->get_record_sql($sql, [$userid], IGNORE_MULTIPLE); +} + + +/** + * Checks if the given user has a mobile token (has used recently the app). + * + * @param int $userid the user to check + * @return bool true if the user has a token, false otherwise. + */ +function tool_mobile_user_has_token($userid) { + + return !empty(tool_mobile_get_token($userid)); } /** @@ -162,17 +175,25 @@ function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree } // Check if the user is using the app, encouraging him to use it otherwise. - $userhastoken = tool_mobile_user_has_token($user->id); + $usertoken = tool_mobile_get_token($user->id); $mobilestrconnected = null; + $mobilelastaccess = null; - if ($userhastoken) { - $mobilestrconnected = get_string('mobileappconnected', 'tool_mobile'); + if ($usertoken) { + $mobilestrconnected = get_string('lastsiteaccess'); + if ($usertoken->lastaccess) { + $mobilelastaccess = userdate($usertoken->lastaccess) . "  (" . format_time(time() - $usertoken->lastaccess) . ")"; + } else { + // We should not reach this point. + $mobilelastaccess = get_string("never"); + } } else if ($url = tool_mobile_create_app_download_url()) { $mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out()); } if ($mobilestrconnected) { - $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null); + $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null, null, + $mobilelastaccess); } // Add nodes, if any.