/** * Delete Calendar events. * * @param array $events A list of events to create. * @return array array of events created. * @since Moodle 2.5 * @throws moodle_exception if user doesnt have the permission to create events. */ public static function create_calendar_events($events) { global $CFG, $DB, $USER; require_once $CFG->dirroot . "/calendar/lib.php"; // Parameter validation. $params = self::validate_parameters(self::create_calendar_events_parameters(), array('events' => $events)); $transaction = $DB->start_delegated_transaction(); $return = array(); $warnings = array(); foreach ($params['events'] as $event) { // Let us set some defaults. $event['userid'] = $USER->id; $event['modulename'] = ''; $event['instance'] = 0; $event['subscriptionid'] = null; $event['uuid'] = ''; $event['format'] = external_validate_format($event['format']); if ($event['repeats'] > 0) { $event['repeat'] = 1; } else { $event['repeat'] = 0; } $eventobj = new calendar_event($event); // Let's check if the user is allowed to delete an event. if (!calendar_add_event_allowed($eventobj)) { $warnings[] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event'); continue; } // Let's create the event. $var = $eventobj->create($event); $var = (array) $var->properties(); if ($event['repeat']) { $children = $DB->get_records('event', array('repeatid' => $var['id'])); foreach ($children as $child) { $return[] = (array) $child; } } else { $return[] = $var; } } // Everything done smoothly, let's commit. $transaction->allow_commit(); return array('events' => $return, 'warnings' => $warnings); }
if (!empty($form) && !empty($form->name) && confirm_sesskey()) { $form->name = clean_text(strip_tags($form->name, '<lang><span>')); $form->timestart = make_timestamp($form->startyr, $form->startmon, $form->startday, $form->starthr, $form->startmin); if ($form->duration == 1) { $form->timeduration = make_timestamp($form->endyr, $form->endmon, $form->endday, $form->endhr, $form->endmin) - $form->timestart; if ($form->timeduration < 0) { $form->timeduration = 0; } } else { if ($form->duration == 2) { $form->timeduration = $form->minutes * MINSECS; } else { $form->timeduration = 0; } } if (!calendar_add_event_allowed($form)) { error('You are not authorized to do this'); } validate_form($form, $err); if (count($err) == 0) { $form->timemodified = time(); /// Get the event id for the log record. $eventid = insert_record('event', $form, true); /// Use the event id as the repeatid to link repeat entries together if ($form->repeat) { $form->repeatid = $form->id = $eventid; update_record('event', $form); // update the row, to set its repeatid } /// Log the event entry. add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id=' . $eventid, stripslashes($form->name));
/** * Update or create an event within the database * * Pass in a object containing the event properties and this function will * insert it into the database and deal with any associated files * * @see add_event() * @see update_event() * * @param stdClass $data object of event * @param bool $checkcapability if moodle should check calendar managing capability or not * @return bool event updated */ public function update($data, $checkcapability = true) { global $CFG, $DB, $USER; foreach ($data as $key => $value) { $this->properties->{$key} = $value; } $this->properties->timemodified = time(); $usingeditor = !empty($this->properties->description) && is_array($this->properties->description); if (empty($this->properties->id) || $this->properties->id < 1) { if ($checkcapability) { if (!calendar_add_event_allowed($this->properties)) { print_error('nopermissiontoupdatecalendar'); } } if ($usingeditor) { switch ($this->properties->eventtype) { case 'user': $this->properties->courseid = 0; $this->properties->course = 0; $this->properties->groupid = 0; $this->properties->userid = $USER->id; break; case 'site': $this->properties->courseid = SITEID; $this->properties->course = SITEID; $this->properties->groupid = 0; $this->properties->userid = $USER->id; break; case 'course': $this->properties->groupid = 0; $this->properties->userid = $USER->id; break; case 'group': $this->properties->userid = $USER->id; break; default: // Ewww we should NEVER get here, but just incase we do lets // fail gracefully $usingeditor = false; break; } // If we are actually using the editor, we recalculate the context because some default values // were set when calculate_context() was called from the constructor. if ($usingeditor) { $this->properties->context = $this->calculate_context($this->properties); $this->editorcontext = $this->properties->context; } $editor = $this->properties->description; $this->properties->format = $this->properties->description['format']; $this->properties->description = $this->properties->description['text']; } // Insert the event into the database $this->properties->id = $DB->insert_record('event', $this->properties); if ($usingeditor) { $this->properties->description = file_save_draft_area_files($editor['itemid'], $this->editorcontext->id, 'calendar', 'event_description', $this->properties->id, $this->editoroptions, $editor['text'], $this->editoroptions['forcehttps']); $DB->set_field('event', 'description', $this->properties->description, array('id' => $this->properties->id)); } // Log the event entry. add_to_log($this->properties->courseid, 'calendar', 'add', 'event.php?action=edit&id=' . $this->properties->id, $this->properties->name); $repeatedids = array(); if (!empty($this->properties->repeat)) { $this->properties->repeatid = $this->properties->id; $DB->set_field('event', 'repeatid', $this->properties->repeatid, array('id' => $this->properties->id)); $eventcopy = clone $this->properties; unset($eventcopy->id); for ($i = 1; $i < $eventcopy->repeats; $i++) { $eventcopy->timestart = $eventcopy->timestart + WEEKSECS + dst_offset_on($eventcopy->timestart) - dst_offset_on($eventcopy->timestart + WEEKSECS); // Get the event id for the log record. $eventcopyid = $DB->insert_record('event', $eventcopy); // If the context has been set delete all associated files if ($usingeditor) { $fs = get_file_storage(); $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id); foreach ($files as $file) { $fs->create_file_from_storedfile(array('itemid' => $eventcopyid), $file); } } $repeatedids[] = $eventcopyid; // Log the event entry. add_to_log($eventcopy->courseid, 'calendar', 'add', 'event.php?action=edit&id=' . $eventcopyid, $eventcopy->name); } } // Hook for tracking added events self::calendar_event_hook('add_event', array($this->properties, $repeatedids)); return true; } else { if ($checkcapability) { if (!calendar_edit_event_allowed($this->properties)) { print_error('nopermissiontoupdatecalendar'); } } if ($usingeditor) { if ($this->editorcontext !== null) { $this->properties->description = file_save_draft_area_files($this->properties->description['itemid'], $this->editorcontext->id, 'calendar', 'event_description', $this->properties->id, $this->editoroptions, $this->properties->description['text'], $this->editoroptions['forcehttps']); } else { $this->properties->format = $this->properties->description['format']; $this->properties->description = $this->properties->description['text']; } } $event = $DB->get_record('event', array('id' => $this->properties->id)); $updaterepeated = !empty($this->properties->repeatid) && !empty($this->properties->repeateditall); if ($updaterepeated) { // Update all if ($this->properties->timestart != $event->timestart) { $timestartoffset = $this->properties->timestart - $event->timestart; $sql = "UPDATE {event}\n SET name = ?,\n description = ?,\n timestart = timestart + ?,\n timeduration = ?,\n timemodified = ?\n WHERE repeatid = ?"; $params = array($this->properties->name, $this->properties->description, $timestartoffset, $this->properties->timeduration, time(), $event->repeatid); } else { $sql = "UPDATE {event} SET name = ?, description = ?, timeduration = ?, timemodified = ? WHERE repeatid = ?"; $params = array($this->properties->name, $this->properties->description, $this->properties->timeduration, time(), $event->repeatid); } $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); } 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); } // Hook for tracking event updates self::calendar_event_hook('update_event', array($this->properties, $updaterepeated)); return true; } }
unset($formoptions->eventtypes->groups); } } if ($cal_y && $cal_m && $cal_d && checkdate($cal_m, $cal_d, $cal_y)) { $event->timestart = make_timestamp($cal_y, $cal_m, $cal_d, 0, 0, 0); } else { if ($cal_y && $cal_m && checkdate($cal_m, 1, $cal_y)) { if ($cal_y == $now['year'] && $cal_m == $now['mon']) { $event->timestart = make_timestamp($cal_y, $cal_m, $now['mday'], 0, 0, 0); } else { $event->timestart = make_timestamp($cal_y, $cal_m, 1, 0, 0, 0); } } } $event = new calendar_event($event); if (!calendar_add_event_allowed($event)) { print_error('nopermissions'); } } $properties = $event->properties(true); $formoptions->event = $event; $formoptions->hasduration = $event->timeduration > 0; $mform = new event_form(null, $formoptions); $mform->set_data($properties); $data = $mform->get_data(); if ($data) { if ($data->duration == 1) { $data->timeduration = $data->timedurationuntil - $data->timestart; } else { if ($data->duration == 2) { $data->timeduration = $data->timedurationminutes * MINSECS;