Best practice forum (Archived)

core_calendar_get_calendar_events API Error - wrong data

 
Dan Bonetto
core_calendar_get_calendar_events API Error - wrong data
by Dan Bonetto - Wednesday, 21 May 2014, 11:10 PM
 

The API documentation for the 'core_calendar_get_calendar_events' web service suggests that the following information is displayed/returned:

General structure
object {
events list of (
//event
object {
id int //event id
name string //event name
description string Optional //Description
format int //description format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN)
courseid int //course id
groupid int //group id
userid int //user id
repeatid int //repeat id
modulename string Optional //module name
instance int //instance id
eventtype string //Event type
timestart int //timestart
timeduration int //time duration
visible int //visible
uuid string Optional //unique id of ical events
sequence int //sequence
timemodified int //time modified
subscriptionid int Optional //Subscription id
}
)warnings Optional //list of warnings
list of (
//warning
object {
item string Optional //item
itemid int Optional //item id
warningcode string //the warning code can be used by the client app to implement specific behaviour
message string //untranslated english message to explain the warning
}
)}

When we look at the returned data, the courseid is only ever 0 or 1 and is not representative of our actual data.

Has anyone successfully tested the actual data that is returned?

Thanks
Daniel
? ?
Re: core_calendar_get_calendar_events API Error - wrong data
by ? ? - Monday, 26 May 2014, 2:39 PM
 

I've not used webservices much, but from looking at the code, the webservice call runs function get_calendar_events in calendar/externallib.php, which ultimately runs function calendar_get_events in calendar/lib.php

It seems that it is constructed to only return sitewide-events (those in mdl_event with a courseid of 0 or 1) by default. If you want to additionally get user & sitewide events in particular courses you also have to send a populated events[courseids] array in the webservice call so for a basic call:

webservice/rest/server.php?wstoken=<usertokenhere>&wsfunction=core_calendar_get_calendar_events

then for each course you also want to obtain events for you need to add additional params as follows:

&events[courseids][0]=x where x is the course id.

To query multiple courses, build up the array:

&events[courseids][0]=x&events[courseids][1]=y&events[courseids][2]=z

You will see in get_calendar_events in calendar/externallib.php in the block starting around line 175 that there are checks so that courses passed in this manner are only queried for events if the user is actually enrolled on those courses.

It does seem a bit strange that all a users events in all their courses are not added by default but the behaviour you are seeing is by design by Moodle. It would be pretty easy to modify the code after line 176 to automatically add all a users enrolled courses to the output if that was your desired behaviour, you could replace that section with something like

            $courses = enrol_get_my_courses();
            $courses = array_keys($courses);
            foreach ($courses as $id) {
                $funcparam['courses'][] = $id;
            }
Dan Bonetto
Re: core_calendar_get_calendar_events API Error - wrong data
by Dan Bonetto - Thursday, 29 May 2014, 10:19 PM
 

Hi Ciaran,

No matter what I try, adding the &events[courseids][0]=x with x being the number always returns the same data.

Any other ideas?

 

? ?
Re: core_calendar_get_calendar_events API Error - wrong data
by ? ? - Thursday, 29 May 2014, 11:05 PM
 

The user sending the query has to actually be enrolled in the course to get any data - check the user token you are sending. Also there would actually have to be events for that course in mdl_event?