Totara Mobile plugin
This plugin is provided to support the standard Totara Mobile app.
It is disabled by default. To enable and configure it, go to Plugins > Mobile > Mobile settings.
Testing or troubleshooting using the Mobile Device Emulator
An HTML + JavaScript endpoint is included for developers that emulates key behaviour of the Totara Mobile app. It is disabled by default.
- Add
$CFG->mobile_device_emulator = true;
to your config.php - Enable the mobile plugin.
- Point your desktop web browser at /totara/mobile/device_emulator.php
- Log in using any valid username and password.
- You can submit GraphQL persisted queries using the GraphQL browser.
- Any pluginfile.php links are handled automatically; click to emulate downloading the file.
- Use a
totara_mobile_create_webview
query (see sample below) to open a URL in the WebView window.
If you see a 'Mobile access error' click the Log out link at the bottom of the page, and resubmit the totara_mobile_create_webview
query.
Some sample queries
Basic information about the current user:
{
"operationName" : "totara_mobile_me",
"variables" : {}
}
List of current learning items:
{
"operationName" : "totara_mobile_current_learning",
"variables" : {}
}
Detailed information about a course:
{
"operationName" : "totara_mobile_course",
"variables" : {"courseid": 2}
}
Create a WebView of a URL (opens in emulated WebView browser):
{
"operationName" : "totara_mobile_create_webview",
"variables" : {"url" : "/course/view.php?id=2"}
}
Remove the current device registration
{
"operationName" : "totara_mobile_delete_device",
"variables" : {}
}
Behind the scenes
If you are unable to use the device emulator or want to submit queries and work with endpoints directly, see the sections below.
Registering a new user device from the app
- Use a regular browser to log in as a Site Administrator and enable the mobile plugin.
- Then the Totara Mobile app should open the site login page in new WebView with TOTARA_MOBILE_DEVICE_REGISTRATION HTTP request header that contains the app name and version.
- After a successful log in the user is taken to the /totara/mobile/device_request.php page which contains a secret setup code (in data attribute of the success message).
- Ideally the app should then reload the page in WebView to log the user out automatically.
- Then the app should make a simple Curl POST request to the /totara/mobile/device_register.php script with json encoded object in request body, for example {"setupsecret" : "1DFPnot6fVV99dvfhjrOf2JDtiM6Om"}.
- The returned data structure contains API key and GraphQL endpoint URL. The app is expected to store the API key in secure storage.
If you do not have a mobile app, you can emulate WebView by adding the following temporarily to your config.php
$_SERVER['HTTP_X_TOTARA_MOBILE_DEVICE_REGISTRATION'] = 'Regular browser hack v1';
Manual execution of persisted GraphQL queries
Create PhpStorm HTTP scratch with following text, change POST URL and API-KEY header value to match your site and device registration:
POST http://localhost:8080/totara/mobile/api.php
Accept: application/json
X-API-KEY: vzW4My45Z41i8KKXGy53RxCYZz5LFHkbk8BT1Jbrk56qrSx9BE
{
"operationName" : "totara_mobile_me",
"variables" : {}
}
Manually initiate WebView with user session for embedding in mobile app
- Execute totara_mobile_create_webview persisted GraphQL mutation to obtain a secret header token, you need to specify requested URL as parameter.
- Create a new WebView with /totara/mobile/device_webview.php URL in mobile app with HTTP request header TOTARA_MOBILE_WEBVIEW_SECRET containing previously obtained secret.
- New user session is automatically created without visiting login page and WebView is redirected to previously specified URL.
- When WebView is not necessary any more mobile app should execute totara_mobile_delete_webview persisted GraphQL mutation.
You can fake WebView in regular browser by adding following to your config.php
$_SERVER['HTTP_X_TOTARA_MOBILE_WEBVIEW_SECRET'] = '8yWXYXrCgG9QGCZkz0YyBxhxFXajXN';
Examples
POST http://localhost:8080/totara/mobile/api.php
Accept: application/json
X-API-KEY: vzW4My45Z41i8KKXGy53RxCYZz5LFHkbk8BT1Jbrk56qrSx9BE
{
"operationName" : "totara_mobile_create_webview",
"variables" : {"url" : "/course/view.php?id=5"}
}
POST http://localhost:8080/totara/mobile/api.php
Accept: application/json
X-API-KEY: vzW4My45Z41i8KKXGy53RxCYZz5LFHkbk8BT1Jbrk56qrSx9BE
{
"operationName" : "totara_mobile_delete_webview",
"variables" : {"secret" : "8yWXYXrCgG9QGCZkz0YyBxhxFXajXN"}
}
Manually unregistering device
- Execute totara_mobile_delete_device persisted GraphQL mutation.
POST http://localhost:8080/totara/mobile/api.php
Accept: application/json
X-API-KEY: vzW4My45Z41i8KKXGy53RxCYZz5LFHkbk8BT1Jbrk56qrSx9BE
{
"operationName" : "totara_mobile_delete_device",
"variables" : {}
}