enrol_manual_enrol_users without enrolling the web service user

enrol_manual_enrol_users without enrolling the web service user

by Max Longus -
Number of replies: 6

Hello,

Is it possible to enrol users in a course by REST web service without previously manually enrolling the web service user in that course ?
I have to create a lot of courses in Moodle and i must enrol teachers and students in these courses.
I would like to do this by the web service API.
I can easily create courses with REST.
But i cannot enrol teachers in that course with enrol_manual_enrol_users, i have an exception :
<EXCEPTION class="require_login_exception">
<ERRORCODE>requireloginerror</ERRORCODE>
<MESSAGE>Cours ou activité pas accessible.</MESSAGE>           "Course or activity not accessible"
</EXCEPTION>

 The only way i found to enrol a teacher with enrol_manual_enrol_users is to manually enrol the web service user in the course. Is there a solution to get around that problem  in order to have a complete automatic process ?

Thanks.

Average of ratings: -
In reply to Max Longus

Re: enrol_manual_enrol_users without enrolling the web service user

by Elias Cisneros -
I have same situation, I need enrol a user without previously  enrolling the web service user in the course .


Some idea?


Thanks

In reply to Max Longus

Re: enrol_manual_enrol_users without enrolling the web service user

by Mayank Tyagi -

Hey Max,

I think this web service is restricted to logged in users. If you want you can create your own webservice function. You can easily enrol users in to course by creating your own webservice function. I have done this many times.

Attachment login.PNG
In reply to Mayank Tyagi

Re: enrol_manual_enrol_users without enrolling the web service user

by Massimo Scali -

In order to enrol a student or a teacher in a course you must authorise the web service client user to assign those roles and this is managed in a reserved tab in the "Role management" page.

Massimo

Average of ratings: Useful (1)
In reply to Mayank Tyagi

Re: enrol_manual_enrol_users without enrolling the web service user

by Mily Rose -
In this contemporary world, we all use more than one web service in our day to day life. A web service is any piece of software that makes itself available over the internet. It also can be the communication between electronic devices. click over here
In reply to Max Longus

Ri: enrol_manual_enrol_users without enrolling the web service user

by Brunetto Spinelli -
Searched in google for this without solution, so I did it by myself.

Short answer:
the user you use for your webservices need to allow the following capability:
moodle/course:view
(you need to allow moodle/role:assign too)

Long answer:
Probably you created a new role for handle webservices, based on user archetipe. You added all the needed capabilities (ie: enrol/manual:enrol to enrol users), but you get the error code: requireloginerror.
This happens because the function enrol_users (enrol\manual\externallib.php) calls the function require_login (lib\moodlelib.php). In require login, variable $access must be true or the require_login_exception will be trown.
A way require_login set to true $access is with function is_viewing (lib\accesslib.php):

if (is_viewing($coursecontext, $USER)) {
// Ok, no need to mess with enrol.
$access = true;
}

Now, if you check function is_viewing, there is:
if (!has_capability('moodle/course:view', $coursecontext, $user)) {
// admins are allowed to inspect courses
return false;
}

So adding capability moodle/course:view to your webservice user should achieve the task.

Hope it helps.
Average of ratings: Useful (3)
In reply to Max Longus

Re: enrol_manual_enrol_users without enrolling the web service user

by Ben Kahn -
I would recommend looking at Massimo's suggestion - you can enable roles to assign other roles rather than creating a new webservice like seen here for the Web Service User role.