General developer forum

Detect if an event is triggered by a webservice

 
Picture of Julien Thomas
Detect if an event is triggered by a webservice
 

Hello awesome community,

I am developping a custom local plugin which listen to the \core\event\user_enrolment_created event.

I need to find a solution to detect if an enrolment is created by a webservice or manually. 

The user are enrolled by a Wordpress plugin (EdwiserBridge) into Moodle courses.

Do you have any ideads ?


My only other option is to detect if a user is enrolled in a particular course.
But this course need to be hidden.


Thank you.


Julien


 
Average of ratings: -
Renaat
Re: Detect if an event is triggered by a webservice
Core developersParticularly helpful MoodlersPlugin developers

Perhaps this can help:  The plugin availability_mobile contains a function is_ws_access

   /**
     * Detects if the user is accesing Moodle via Web Services.
     * @return boolean True if the user is accesing via WS
     */
    protected function is_ws_access() {
        global $ME;

        // First check this global const.
        if (WS_SERVER) {
            return true;
        }

        // Check rare cases, like webservice/pluginfile.php.
        if (strpos($ME, "webservice/") !== false) {
            $token = optional_param('token', '', PARAM_ALPHANUM);
            if ($token) {
                return true;
            }
        }

        return false;
    }



 
Average of ratings: Useful (1)
Picture of Julien Thomas
Re: Detect if an event is triggered by a webservice
 

Wow it was so simple !

It seems to work, thank you Renaat.

 
Average of ratings: -