How to retrieve the User Enrolment ID

Re: How to retrieve the User Enrolment ID

by Mayank Tyagi -
Number of replies: 12

Hey Carlos,

Yes, you are on the right track. You can easily update the "Enrolment Ends" date field for your user. The webservice function core_enrol_edit_user_enrolment requires three required parameter - courseid, user enrolments id, enrol status and optional parameters like  timestart and timeend (timestamp). You can easily find user enrolment id in Moodle's user_enrolment table. This requires a good knowledge of Mysql queries and Moodle's table. You can apply join of user_enrolments and enrol table. 

After getting all required fields you can call webservice like 

 http://localhost/moodle3.6.1/webservice/rest/server.php?wstoken=fd324bf337764cbe45fbdb7760d09604&wsfunction=core_enrol_edit_user_enrolment&moodlewsrestformat=json&courseid=4&ueid=1&status=0&timestart=1552410399&timeend=1555089582

If you want I will share the required query with you with the help of which you can get user enrolment id. 

If you don't want to do this with webservice API function, you can also use moodle's core function update_user_enrol. 

Regards,

Mayank Tyagi

In reply to Mayank Tyagi

Re: How to retrieve the User Enrolment ID

by Carlos Chiarella -

Thanks Mayank for the information. Let me explain what we want to achieve and please tell me if you have better ideas.

All of our enrolments are handle by Drupal. So Drupal will follow these steps:

1. call for a specific course the following web service method: core_enrol_get_enrooled_users

This method should return the user enrolment id. With this field, I think we will have enough information to call

2. core_enrol_edit_user_enrolment


Does that make sense? Thanks for your ideas.


Carlos

In reply to Carlos Chiarella

Re: How to retrieve the User Enrolment ID

by Mayank Tyagi -

Hey Carlos,

I don't think so this method will return enrolment id for the user. It will return enrolled users details like firstname, lastname, profile description and many other details related to users. The details return by this method are  

[{"id":3,"username":"sooraj","firstname":"Sooraj","lastname":"Singh","fullname":"Sooraj Singh","email":"singhsooraj30zz@gmail.com","department":"","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"country":"IN","profileimageurlsmall":"http:\/\/localhost\/moodle3.6.1\/theme\/image.php\/academic\/core\/1551116448\/u\/f2","profileimageurl":"http:\/\/localhost\/moodle3.6.1\/theme\/image.php\/academic\/core\/1551116448\/u\/f1","groups":[],"roles":[{"roleid":5,"name":"","shortname":"student","sortorder":0}],"enrolledcourses":[{"id":4,"fullname":"dd","shortname":"45"}]}]

All these details are not required for calling core_enrol_edit_user_enrolment.


Thanks and Regards,

Mayank 

In reply to Mayank Tyagi

Re: How to retrieve the User Enrolment ID

by Jo Ha -

Hello,

I'm in a similiar situation as carlos - I want to change a user enrollment (role in course) via webservice.

Happily I found the core_enrol_edit_user_enrolment webservice function which allows that (well, at least i thought that from its name). But apparently that webservice requires some parameters, which you are not able to get via webservice... (But wouldn't that, if true, make the core_enrol_edit_user_enrolment webservice completly superfluous?)
And secondly, the core_enrol_edit_user_enrolment  only allows changing enrolment status and time, correct?!

This leads me to following questions:

  1. Is it possible to use the core_enrol_edit_user_enrolment webservice for changing the user enrollment in a course, or to be more precise, the role(s) a user is enrolled in a course.
  2. (If 1) What webservice can be used to retrieve the information needed as parameters for core_enrol_edit_user_enrolment ?


Maybe I'm on the wrong track with the above, so my general question is:

What webservice to use for changeing a users role(s) in a course. I know courseid, userid and new roleid.


Thanks in advance.

In reply to Jo Ha

Re: How to retrieve the User Enrolment ID

by Mayank Tyagi -
Hello,

No, the core_enrol_edit_user_enrolment is not for change user role in the enrolled course.
For assigning the role to a user you can use core_role_assign_roles.
What do you mean by changing the user role? Is it mean to unassign the current role of the user in that course and assign the new role?
Well as far in my knowledge there is no direct web service for this(unassign present role and assign new). You can do it in two steps:-
1) use core_role_unassign_roles to unassign current role.
2) use core_role_assign_roles to assign new role.
If you don't want to use these web service you can make your own web service for this functionality.

Regards
Mayank
In reply to Mayank Tyagi

Re: How to retrieve the User Enrolment ID

by Aaron Alo -

Hi Mayank,

Core_enrol_get_enrooled_users

This method DOES NOT return the user enrolment id. 

I use core_enrol_get_users_courses to get a list of courses user A is in. Then I display all course on the view.

* Course A   Update
* Course B   Update
* Course C   Update

I click the "Update" button next to course A.  How can I update my enrolment if I don't have the enrolment ID? 

Kind regards,

Aaron

In reply to Aaron Alo

Re: How to retrieve the User Enrolment ID

by Anthony Erickson -
Responding as I similarly want to change the enrolment status for a user on a course but core_enrol_edit_user_enrolment() requires the ueid variable. I can't find another function that returns that variable? How do I source it, if I know the Course ID and User ID?
In reply to Aaron Alo

Re: How to retrieve the User Enrolment ID

by Benjamin Hays -
I'm running into this problem as well. Using webservices I can see enrollments in a course using core_enrol_get_enrolled_users but I can't edit them without finding a way to get the UEID. Can I get the enrollment ID of an enrollment in a course using webservices? Any help would be awesome. Thanks!
In reply to Benjamin Hays

Re: How to retrieve the User Enrolment ID

by Anthony Erickson -

Hi Ben,

In consultation with our Moodle partner provider, we were getting some custom APIs created so included this as a requirement as the only way to solve it. Pretty annoying - would be a good inclusion on the enhancement/development web site as I think the effort required to add that field in to the web service(s) is pretty straightforward.

Ant


In reply to Anthony Erickson

Re: How to retrieve the User Enrolment ID

by Benjamin Hays -
Ant,
Thank you for your reply! That is disappointing but I really appreciate the information.
In reply to Benjamin Hays

Re: How to retrieve the User Enrolment ID

by Anthony Erickson -
Hi Ben, I'm moving out of the LMS space in the coming week but I went ahead and raised an improvement request (we're using 3.5 currently) so it can at least be considered to be included in to the core APIs. A mandatory parameter that can't be obtained is a bit annoying so would be great housekeeping for it to be sorted.

Create an account and vote/watch here: https://tracker.moodle.org/browse/MDL-70582