core_enrol_get_users_courses returning empty array

core_enrol_get_users_courses returning empty array

by Carles Franquesa -
Number of replies: 8


Hi,

As it can be seen in the picture, there is a course called "001 Curs numero 1" in my moodle installation. 


Once a token has been created and the API functions needed also added in the external service I have been doing all the things I have needed from my web app. 

I have tried, for instance, core_user_get_users_by_field, for this student:


And certainly, it worked. For that, I have had to add the API function core_user_get_users_by_field to the external service that I called aodb, just any several others, as you can see in the next image.



My problem is that when I ask for the courses where Bart Simpson is enrolled in, I get no courses.

To do that, I use the curl:

https://mydomain.org/webservice/rest/server.php?wstoken=xxxxxxxxxxxxxxxxxxxxxxxx&moodlewsrestformat=json&wsfunction=core_enrol_get_users_courses&userid=72

But just get 

[]

Can someone explain to me what I am doing wrong?

Any help would be infinitely appreciated!


Carles







Average of ratings: -
In reply to Carles Franquesa

Re: core_enrol_get_users_courses returning empty array

by Geoffrey Van Wyk -
Picture of Plugin developers
Only courses in which the user's enrolment is active will be returned. As you can see in your screenshot, Bart Simpson's enrolment is "Not current".

See these lines of code:
* https://github.com/moodle/moodle/blob/MOODLE_311_STABLE/enrol/externallib.php#L314
* https://github.com/moodle/moodle/blob/MOODLE_311_STABLE/lib/enrollib.php#L917
* https://github.com/moodle/moodle/blob/MOODLE_311_STABLE/lib/enrollib.php#L1081
Average of ratings: Useful (3)
In reply to Geoffrey Van Wyk

Re: core_enrol_get_users_courses returning empty array

by Carles Franquesa -
Thanks Geoffrey,

I know that it's been long ago. By then, I found a way that didn't need to get all courses for a given user. But now, I'm back with the same problem again. I've been looking at the links you sent me, but I am not using moodle API from php. So, they are not much useful for me. And I thank you for them.

Let me tell you that with respect to the terminology, it seems that there's a confusion in moodle's vocabulary: The value "active" for an enrollment belongs to the status \in {!suspended","active"}. But on the other hand, when an enrollment is for the future, moodle says "not current" in the enrollment status. That's confusing me.

Anyway, by now, I need a way to get all courses for a given user via web services. Is there any parameter that I might add to de url when asking for a user courses theat give me back all of them, including those of the status "not current".

I would appreciate much your answer, and in any case, thanks for your time.

Carles










In reply to Carles Franquesa

Re: core_enrol_get_users_courses returning empty array

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi,

Maybe that comment will help you better than my last one : )

https://localhost/webservice/rest/server.php?moodlewsrestformat=json&&wstoken=a92z1affa123a45zzz678af901ada2de8f&wsfunction=core_enrol_get_users_courses&userid=2700

Have a nice day.

Dominique
In reply to Dominique Palumbo

Re: core_enrol_get_users_courses returning empty array

by Carles Franquesa -
Hi Dominique,

I do not understand you.

You show me a link to your localhost? 

The link itself is in the format that I am currently using to get info from moodle. Of course, with another host and token. But, why are you sending me this link? 

Please, explain me some! And, the most important, if you know the way to get all courses, including not current, to which a student is enrolled in with moodle API.

Thanks again!

Carles



In reply to Carles Franquesa

Re: core_enrol_get_users_courses returning empty array

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi,

I didn't see that you've already the correct call to core_enrol_get_users_courses. Sorry. I read all of this too quickly.

In fact, in that case you probably need to write your own web service.
https://moodledev.io/docs/apis/subsystems/external/writing-a-service

Dominique.
In reply to Dominique Palumbo

Re: core_enrol_get_users_courses returning empty array

by Carles Franquesa -
Thank you very much, Dominique.

I will try it as soon as I can, since as usually, I am already in other tasks.

Anyway, thanks again. Probably will be asking in short when trying to implement my own external service (that sounds great!)

Carles