Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Calendar API: Difference between revisions

From MoodleDocs
Line 17: Line 17:


===Creating new event===
===Creating new event===
Creating new calendar event to database by defining some properties for the event.
Creating new calendar event to database by defining some properties for the event
<code>
 
If event hook is used, it will also call self::calendar_event_hook() to create event for the hook.
<code php>
calendar_event::create($properties)
calendar_event::create($properties)
</code>
</code>
Line 24: Line 26:
===Updating event===
===Updating event===
Updating an existing event in database by providing at least an event id.  If the event is a repeated events, the rest of series event will also be updated (depending on the properties value of repeateditall).  This function could also be use to insert new event to database If the requested event is not exist in database.  The optional parameter $checkcapability is use to check user's capability to edit/add event.  By default $checkcapability parameter is set to true.   
Updating an existing event in database by providing at least an event id.  If the event is a repeated events, the rest of series event will also be updated (depending on the properties value of repeateditall).  This function could also be use to insert new event to database If the requested event is not exist in database.  The optional parameter $checkcapability is use to check user's capability to edit/add event.  By default $checkcapability parameter is set to true.   
<code>
 
If event hook is used, it will also call self::calendar_event_hook() to update the hook event.
<code php>
calendar_event::update($data, $checkcapability=true)
calendar_event::update($data, $checkcapability=true)
</code>
</code>
Line 30: Line 34:
===Deleting event===
===Deleting event===
Deleting an existing event in database.  The optional parameter $deleterepeated is use as indicator to remove the rest of repeated events.  The default value for $deleterepeated is true. Deleting an event will also deleting all associated files related to the event's editor context.
Deleting an existing event in database.  The optional parameter $deleterepeated is use as indicator to remove the rest of repeated events.  The default value for $deleterepeated is true. Deleting an event will also deleting all associated files related to the event's editor context.
<code>
 
If event hook is used, it will also call self::calendar_event_hook() to delete event for the hook.
<code php>
calendar_event::delete($deleterepeated=false)
calendar_event::delete($deleterepeated=false)
</code>
===Event hook===
The capability to use hook to perform specific action to calendar event.  This requires setting up $CFG->calendar and include external calendar file in $CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php').
<code php>
calendar_event_hook($action, array $args)
</code>
</code>



Revision as of 07:43, 16 January 2012

The Calendar API allows you to add and modify events in the calendar for user, groups, courses, or the whole site.

Overview

The Moodle Calendar collects and displays calendar events from everything users have access to.

If your plugin generates calendar events (such as due dates) then you need to add your events to the calendar.

File locations

All the calendar code is located in /calendar/lib.php. You need to include this file in your script if you intend to use it.


The calendar_event class

In general functionality of calendar_event() class are to create, update and delete events.

Creating new event

Creating new calendar event to database by defining some properties for the event.

If event hook is used, it will also call self::calendar_event_hook() to create event for the hook. calendar_event::create($properties)

Updating event

Updating an existing event in database by providing at least an event id. If the event is a repeated events, the rest of series event will also be updated (depending on the properties value of repeateditall). This function could also be use to insert new event to database If the requested event is not exist in database. The optional parameter $checkcapability is use to check user's capability to edit/add event. By default $checkcapability parameter is set to true.

If event hook is used, it will also call self::calendar_event_hook() to update the hook event. calendar_event::update($data, $checkcapability=true)

Deleting event

Deleting an existing event in database. The optional parameter $deleterepeated is use as indicator to remove the rest of repeated events. The default value for $deleterepeated is true. Deleting an event will also deleting all associated files related to the event's editor context.

If event hook is used, it will also call self::calendar_event_hook() to delete event for the hook. calendar_event::delete($deleterepeated=false)

Event hook

The capability to use hook to perform specific action to calendar event. This requires setting up $CFG->calendar and include external calendar file in $CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'). calendar_event_hook($action, array $args)

Examples

The following are examples of using the basic calendar_event API in Moodle.

Example to create new event

Creating new calendar event for closing feedback date. $event = new stdClass; $event->name = get_string('stop', 'feedback').' '.$feedback->name; $event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule); $event->courseid = $feedback->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'feedback'; $event->instance = $feedback->id; $event->eventtype = 'close'; $event->timestart = $feedback->timeclose; $event->visible = instance_is_visible('feedback', $feedback); $event->timeduration = 0;

calendar_event::create($event);

Updating existing calendar event

Simple example of updating exiting event through moodle form. $eventid = required_param('id', PARAM_INT); $event = calendar_event::load($eventid);

$data = $mform->get_data(); $event->update($data);

Deleting existing calendar event

Simple example of deleting existing event from database. $eventid = required_param('id', PARAM_INT); $event = calendar_event::load($eventid); $event->delete($repeats);

See also