diff --git a/calendar/lib.php b/calendar/lib.php index bcd4faca7a914..3105f65035cf1 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -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; @@ -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) { @@ -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&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(); @@ -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&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(); } } @@ -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&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&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 @@ -2356,10 +2383,23 @@ 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); @@ -2367,8 +2407,14 @@ public function delete($deleterepeated=false) { $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)); } }