I'm currently a student at a University and I'm using Moodle everyday. I would like to access some information that is available to me (For example, information of the classes I'm taking, Which assignments are due and when , etc)
I did some research regarding Moodle's API but it all seemed geared toward the power user who actually runs Moodle (My University).
Is there an easy way for me as a student to get the information?
My application uses Node.js
If your university has enabled Web Services for the mobile app, you can generate your own API token and call the Web Services used by the mobile app. If the latter are not enabled, you have to get in touch with your administrator to get Web Services access.
Demo using moodle.org
First, let's get an API token (replace $USERNAME
with your username, and $PASSWORD
with your password):
$ curl -d username="$USERNAME" -d password="$PASSWORD" 'https://moodle.org/login/token.php?service=moodle_mobile_app'
{
"token":"SNIPTOKEN",
"privatetoken":"SNIPPRIVATE"
}
Next, we need your userid
, it will be used throughout other web services call. You can obtain your userid
by calling the web service core_webservice_get_site_info
. Make sure to replace $TOKEN
with the token you obtained above.
$ curl -d wstoken="$TOKEN" -d wsfunction=core_webservice_get_site_info 'https://moodle.org/webservice/rest/server.php?moodlewsrestformat=json' | python -m json.tool | grep userid
"userid": 1451616,
Now that you have your userid
, we can request the courses that you are enrolled in.
$ curl -d wstoken="$TOKEN" -d wsfunction=core_enrol_get_users_courses -d userid=1451616 'https://moodle.org/webservice/rest/server.php?moodlewsrestformat=json' | python -m json.tool
[
{
...snip...
"fullname": "Moodle in English",
"id": 5,
...snip...
},
{
...snip...
"fullname": "Moodle en fran\u00e7ais",
"id": 20,
...snip...
},
{
...snip...
"fullname": "Moodle Certification",
"id": 48,
...snip...
}
]
Recap'
Pre-requisites:
Querying:
YOURHOST/webservice/rest/server.php?moodlewsrestformat=json
.POST
requestswstoken
: Your tokenwsfunction
: The function you are callingapplication/x-www-form-urlencoded
More
I've greatly simplified how this works and what alternatives there are, but this should get you started. You will likely be interested in looking at the developer documentation to get more information about the available web services: