Skip to content

Commit

Permalink
MDL-40915 events: Replace add_to_log with events in calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitagarwal committed Mar 21, 2014
1 parent 451c0e9 commit 9cb5d05
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ public function count_repeats() {
* @return bool event updated
*/
public function update($data, $checkcapability=true) {
global $CFG, $DB, $USER;
global $DB, $USER;

foreach ($data as $key=>$value) {
$this->properties->$key = $value;
Expand All @@ -2174,6 +2174,17 @@ public function update($data, $checkcapability=true) {
$this->properties->timemodified = time();
$usingeditor = (!empty($this->properties->description) && is_array($this->properties->description));

// Prepare event data.
$eventargs = array(
'context' => $this->properties->context,
'objectid' => $this->properties->id,
'other' => array(
'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
'timestart' => $this->properties->timestart,
'name' => $this->properties->name
)
);

if (empty($this->properties->id) || $this->properties->id < 1) {

if ($checkcapability) {
Expand Down Expand Up @@ -2239,7 +2250,10 @@ public function update($data, $checkcapability=true) {
}

// Log the event entry.
add_to_log($this->properties->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$this->properties->id, $this->properties->name);
$eventargs['objectid'] = $this->properties->id;
$eventargs['context'] = $this->properties->context;
$event = \core\event\calendar_event_created::create($eventargs);
$event->trigger();

$repeatedids = array();

Expand Down Expand Up @@ -2267,8 +2281,12 @@ public function update($data, $checkcapability=true) {
}

$repeatedids[] = $eventcopyid;
// Log the event entry.
add_to_log($eventcopy->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventcopyid, $eventcopy->name);

// Trigger an event.
$eventargs['objectid'] = $eventcopyid;
$eventargs['other']['timestart'] = $eventcopy->timestart;
$event = \core\event\calendar_event_created::create($eventargs);
$event->trigger();
}
}

Expand Down Expand Up @@ -2322,13 +2340,22 @@ public function update($data, $checkcapability=true) {
}
$DB->execute($sql, $params);

// Log the event update.
add_to_log($this->properties->courseid, 'calendar', 'edit all', 'event.php?action=edit&amp;id='.$this->properties->id, $this->properties->name);
// Trigger an update event for each of the calendar event.
$events = $DB->get_records('event', array('repeatid' => $event->repeatid), '', 'id,timestart');
foreach ($events as $event) {
$eventargs['objectid'] = $event->id;
$eventargs['other']['timestart'] = $event->timestart;
$event = \core\event\calendar_event_updated::create($eventargs);
$event->trigger();
}
} else {
$DB->update_record('event', $this->properties);
$event = calendar_event::load($this->properties->id);
$this->properties = $event->properties();
add_to_log($this->properties->courseid, 'calendar', 'edit', 'event.php?action=edit&amp;id='.$this->properties->id, $this->properties->name);

// Trigger an update event.
$event = \core\event\calendar_event_updated::create($eventargs);
$event->trigger();
}

// Hook for tracking event updates
Expand Down Expand Up @@ -2356,19 +2383,38 @@ public function delete($deleterepeated=false) {
debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER);
return false;
}

$calevent = $DB->get_record('event', array('id' => $this->properties->id), '*', MUST_EXIST);
// Delete the event
$DB->delete_records('event', array('id'=>$this->properties->id));

// Trigger an event for the delete action.
$eventargs = array(
'context' => $this->properties->context,
'objectid' => $this->properties->id,
'other' => array(
'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
'timestart' => $this->properties->timestart,
'name' => $this->properties->name
));
$event = \core\event\calendar_event_deleted::create($eventargs);
$event->add_record_snapshot('event', $calevent);
$event->trigger();

// If we are deleting parent of a repeated event series, promote the next event in the series as parent
if (($this->properties->id == $this->properties->repeatid) && !$deleterepeated) {
$newparent = $DB->get_field_sql("SELECT id from {event} where repeatid = ? order by id ASC", array($this->properties->id), IGNORE_MULTIPLE);
if (!empty($newparent)) {
$DB->execute("UPDATE {event} SET repeatid = ? WHERE repeatid = ?", array($newparent, $this->properties->id));
// Get all records where the repeatid is the same as the event being removed
$events = $DB->get_records('event', array('repeatid' => $newparent));
// For each of the returned events trigger the event_update hook.
// For each of the returned events trigger the event_update hook and an update event.
foreach ($events as $event) {
// Trigger an event for the update.
$eventargs['objectid'] = $event->id;
$eventargs['other']['timestart'] = $event->timestart;
$event = \core\event\calendar_event_updated::create($eventargs);
$event->trigger();

self::calendar_event_hook('update_event', array($event, false));
}
}
Expand Down

0 comments on commit 9cb5d05

Please sign in to comment.