Identify the current user into local plugin through the moodle mobile app

Identify the current user into local plugin through the moodle mobile app

by Iglika Raykova -
Number of replies: 8

Dear colleagues, I have the following problem that I can't resolve: I've made a local plugin. Into index.php page I need to detect the $USER->id property. When I'm running my plugin's page from the PC browser - everything is OK - the global object $USER is available, but my big problem is when I run the page into the mobile app - there the object $USER is empty.

Does anybody has an idea what approach to use to identify the current user from the mobile app? 

Thanks in advance!

Average of ratings: -
In reply to Iglika Raykova

Re: Identify the current user into local plugin through the moodle mobile app

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm not sure what you mean by "run my page in the mobile app". The mobile app shouldn't be loading a page directly, it should be requesting data and templates through web services, see the Mobile support for plugins documentation.

Inside a method in the classes/output/mobile.php file for your plugin, you can do global $USER; to access the user making the request from the app. They will already have been authenticated using the web service token before your method is run.

Average of ratings: Useful (1)
In reply to Mark Johnson

Re: Identify the current user into local plugin through the moodle mobile app

by Iglika Raykova -

Thank you for your quick response Mark. Under "run my page in the mobile app" I mean that I'm invoking the page directly from the app. I've put link to the page in the menu items using Site Administration->Mobile Features.

Anyway, I also tried to follow the Mobile support for plugins documentation, but when I upload all files described into the documentation the system gives me an error. Do you know any workable example for mobile support of local plugins that I can use in my case? I need to put the link to the page in the main menu of the mobile app and then to show my index.php page. In fact can I use global $USER; from the classes/output/mobile.php to set in the $_SESSION and not to render anything?

In reply to Iglika Raykova

Re: Identify the current user into local plugin through the moodle mobile app

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Let's back up a minute. What does the page you're linking to do, and what are you hoping will happen when the user clicks the link in the app's main menu?

In reply to Mark Johnson

Re: Identify the current user into local plugin through the moodle mobile app

by Iglika Raykova -

I've put the link to my local plugin index.php page into the main menu of the mobile app. The link just open the index.php page. I need then to use the $USER->id in that index.php page, because there is a functionality that displays different forms, related to the user.

In reply to Iglika Raykova

Re: Identify the current user into local plugin through the moodle mobile app

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ok, so you are hoping that clicking the link in the app will log them in to your moodle site, and they'll be viewing your page with an active user session?

Doing this using custom menu items in your Moodle site settings isn't going to work, since the link wont log them in to your moodle site. We have a plugin that does something similar to display a user feedback form. Unfortunately it's not public so I can't point you to the code, but it uses a CourseMainMenuDelegate to display the menu item, then displays the form in the app. The db/mobile.php looks a bit like this:

    $addons = [
        'local_yourplugin' => [
            'handlers' => [
                'oufeedback' => [
                    'displaydata' => [
                        'title' => 'titlestring',
                        'icon' => 'text',
                        'class' => ''
                    ],
                   'delegate' => 'CoreMainMenuDelegate',
                   'method' => 'mobile_yourplugin_view',
                   'offlinefunctions' => []
                  ]
            ]
    ];

The mobile_yourplugin_view template function in classes/output/mobile.php then returns the template for the form, which is displayed in the app.

Another approach, if you're using basic Moodle authentication (not SSO) and you actually want to send the user to your moodle site rather than using the app, would be to create an autologin link in the app. This uses their web service token from the app to create a Moodle session for them so they will be logged in. Have a look at the documentation for the core-link directive.

If you are trying something like this and getting errors, please let us know what the errors say and when they appear.

In reply to Mark Johnson

Re: Identify the current user into local plugin through the moodle mobile app

by Iglika Raykova -

Dear Mark,

thank you for your detailed answer.

I've created db/mobile.php with the code you suggested. Also implement classes/output/mobile.php file with the function pointed into $addons array. Then create mobile_view_page.mustache file just with the following code (just to test):

{{=<% %>=}}

<h2>Hello wolrd!</h2>


I've purge the cache as it is written in the documentation, but there is no link named
'titlestring'
into my app.
Where I'm wrong? Do I need something extra to implement?

About the index.php page of my plugin - it detects on which step user was in his/her last visit and get directly into it. So after I succeeded with the test example that I'm trying to run, I will think about a work-around that will fit my logic.
In reply to Iglika Raykova

Re: Identify the current user into local plugin through the moodle mobile app

by Iglika Raykova -

Dear Mark,

I've set the Debug messages option to be on DEVELOPER mode. After that when I'm purging the cache the system shows me: Error output, so disabling automatic redirect.


May be this is the reason why nothing is shown in my mobile app. But my code is very poor of functionality, it is very strange.

The following is my code of classes/output/mobile.php

namespace local_myplugin\output;
defined('MOODLE_INTERNAL') || die();
use context_module;

class mobile {
    public static function mobile_myplugin_view($args) {
        global $OUTPUT, $USER, $DB;
        $args = (object) $args;

        // Capabilities check.
        require_login();

        if ($args->userid != $USER->id) {
            require_capability('local/myplugin:view', context_system::instance());
        }

        $data = array();

        return [
            'templates' => [
                [
                    'id' => 'main',
                    'html' => $OUTPUT->render_from_template('local_myplugin/mobile_view_page', $data),
                ],
            ],
            'javascript' => '',
            'otherdata' => '',
            'files' => [],
        ];
    }
}

In reply to Iglika Raykova

Re: Identify the current user into local plugin through the moodle mobile app

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you see the message "Error output, so disabling automatic redirect.", then there should also be some sort of error, probably a PHP Warning or similar, near the top of the page. What does that error say?

Your code looks broadly correct to me, so this error is proabably a clue to why it isn't working.