/**
  * Deletes an event and if selected an repeated events in the same series
  *
  * This function deletes an event, any associated events if $deleterepeated=true,
  * and cleans up any files associated with the events.
  *
  * @see delete_event()
  *
  * @param bool $deleterepeated
  * @return bool
  */
 public function delete($deleterepeated = false)
 {
     global $DB;
     // If $this->properties->id is not set then something is wrong
     if (empty($this->properties->id)) {
         debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER);
         return false;
     }
     // Delete the event
     $DB->delete_records('event', array('id' => $this->properties->id));
     // If the editor context hasn't already been set then set it now
     if ($this->editorcontext === null) {
         $this->editorcontext = $this->properties->context;
     }
     // If the context has been set delete all associated files
     if ($this->editorcontext !== null) {
         $fs = get_file_storage();
         $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id);
         foreach ($files as $file) {
             $file->delete();
         }
     }
     // Fire the event deleted hook
     self::calendar_event_hook('delete_event', array($this->properties->id, $deleterepeated));
     // If we need to delete repeated events then we will fetch them all and delete one by one
     if ($deleterepeated && !empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
         // Get all records where the repeatid is the same as the event being removed
         $events = $DB->get_records('event', array('repeatid' => $this->properties->repeatid));
         // For each of the returned events populate a calendar_event object and call delete
         // make sure the arg passed is false as we are already deleting all repeats
         foreach ($events as $event) {
             $event = new calendar_event($event);
             $event->delete(false);
         }
     }
     return true;
 }
Esempio n. 2
0
 /**
  * Deletes an event and if selected an repeated events in the same series
  *
  * This function deletes an event, any associated events if $deleterepeated=true,
  * and cleans up any files associated with the events.
  *
  * @see delete_event()
  *
  * @param bool $deleterepeated  delete event repeatedly
  * @return bool succession of deleting event
  */
 public function delete($deleterepeated = false)
 {
     global $DB;
     // If $this->properties->id is not set then something is wrong
     if (empty($this->properties->id)) {
         debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER);
         return false;
     }
     // Delete the event
     $DB->delete_records('event', array('id' => $this->properties->id));
     // 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.
             foreach ($events as $event) {
                 self::calendar_event_hook('update_event', array($event, false));
             }
         }
     }
     // If the editor context hasn't already been set then set it now
     if ($this->editorcontext === null) {
         $this->editorcontext = $this->properties->context;
     }
     // If the context has been set delete all associated files
     if ($this->editorcontext !== null) {
         $fs = get_file_storage();
         $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id);
         foreach ($files as $file) {
             $file->delete();
         }
     }
     // Fire the event deleted hook
     self::calendar_event_hook('delete_event', array($this->properties->id, $deleterepeated));
     // If we need to delete repeated events then we will fetch them all and delete one by one
     if ($deleterepeated && !empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
         // Get all records where the repeatid is the same as the event being removed
         $events = $DB->get_records('event', array('repeatid' => $this->properties->repeatid));
         // For each of the returned events populate a calendar_event object and call delete
         // make sure the arg passed is false as we are already deleting all repeats
         foreach ($events as $event) {
             $event = new calendar_event($event);
             $event->delete(false);
         }
     }
     return true;
 }
Esempio n. 3
0
 /**
  * Deletes an event and if selected an repeated events in the same series
  *
  * This function deletes an event, any associated events if $deleterepeated=true,
  * and cleans up any files associated with the events.
  *
  * @see delete_event()
  *
  * @param bool $deleterepeated
  * @return bool
  */
 public function delete($deleterepeated = false)
 {
     global $DB, $USER, $CFG;
     // If $this->properties->id is not set then something is wrong
     if (empty($this->properties->id)) {
         debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER);
         return false;
     }
     // Delete the event
     $DB->delete_records('event', array('id' => $this->properties->id));
     // If the editor context hasn't already been set then set it now
     if ($this->editorcontext === null) {
         switch ($this->properties->eventtype) {
             case 'course':
             case 'group':
                 $this->editorcontext = get_context_instance(CONTEXT_COURSE, $this->properties->courseid);
                 break;
             case 'user':
                 $this->editorcontext = get_context_instance(CONTEXT_USER, $USER->id);
                 break;
             case 'site':
                 $this->editorcontext = get_context_instance(CONTEXT_SYSTEM);
                 break;
             default:
                 // There is a chance we can get here as some modules use there own
                 // eventtype strings. In this case the event has been added by code
                 // and we don't need to worry about it anyway
                 break;
         }
     }
     // If the context has been set delete all associated files
     if ($this->editorcontext !== null) {
         $fs = get_file_storage();
         $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id);
         foreach ($files as $file) {
             $file->delete();
         }
     }
     // Fire the event deleted hook
     self::calendar_event_hook('delete_event', array($this->properties->id, $deleterepeated));
     // If we need to delete repeated events then we will fetch them all and delete one by one
     if ($deleterepeated && !empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
         // Get all records where the repeatid is the same as the event being removed
         $events = $DB->get_records('event', array('repeatid' => $this->properties->repeatid));
         // For each of the returned events populate a calendar_event object and call delete
         // make sure the arg passed is false as we are already deleting all repeats
         foreach ($events as $event) {
             $event = new calendar_event($event);
             $event->delete(false);
         }
     }
     return true;
 }