/** * Set up method. */ protected function setUp() { global $DB, $CFG; $this->resetAfterTest(); $this->setTimezone('Australia/Perth'); $user = $this->getDataGenerator()->create_user(); $sub = new stdClass(); $sub->url = ''; $sub->courseid = 0; $sub->groupid = 0; $sub->userid = $user->id; $sub->pollinterval = 0; $subid = $DB->insert_record('event_subscriptions', $sub, true); $event = new stdClass(); $event->name = 'Event name'; $event->description = ''; $event->timestart = 1385913700; // A 2013-12-2 Monday event. $event->timeduration = 3600; $event->uuid = 'uuid'; $event->subscriptionid = $subid; $event->userid = $user->id; $event->groupid = 0; $event->courseid = 0; $event->eventtype = 'user'; $eventobj = calendar_event::create($event, false); $DB->set_field('event', 'repeatid', $eventobj->id, array('id' => $eventobj->id)); $eventobj->repeatid = $eventobj->id; $this->event = $eventobj; }
/** * */ public static function update_event_timedue($data) { global $DB; if (!empty($data->timedue)) { $event = new \stdClass(); $event->name = $data->name; $event->description = format_module_intro('dataform', $data, $data->coursemodule); $event->timestart = $data->timedue; if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'dataform', 'instance' => $data->id))) { $calendarevent = \calendar_event::load($event->id); $calendarevent->update($event); } else { $event->courseid = $data->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'dataform'; $event->instance = $data->id; $event->eventtype = 'due'; $event->timeduration = 0; $event->visible = $DB->get_field('course_modules', 'visible', array('module' => $data->module, 'instance' => $data->id)); \calendar_event::create($event); } } else { $DB->delete_records('event', array('modulename' => 'dataform', 'instance' => $data->id, 'eventtype' => 'due')); } }
/** * Do the job. */ public function execute() { global $DB, $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; // Get calendars set to sync in. $starttime = time(); \local_o365\feature\calsync\observers::set_event_import(true); // Using a direct query here so we don't run into static cache issues. $laststarttime = $DB->get_record('config_plugins', ['plugin' => 'local_o365', 'name' => 'calsyncinlastrun']); $laststarttime = !empty($laststarttime) && !empty($laststarttime->value) ? $laststarttime->value : 0; $httpclient = new \local_o365\httpclient(); $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc(); $calsubs = $DB->get_recordset_select('local_o365_calsub', 'syncbehav = ? OR syncbehav = ?', ['in', 'both']); $calsync = new \local_o365\feature\calsync\main($clientdata, $httpclient); foreach ($calsubs as $i => $calsub) { try { $events = $calsync->get_events($calsub->user_id, $calsub->o365calid, $laststarttime); if (!empty($events) && is_array($events) && isset($events['value']) && is_array($events['value'])) { if (!empty($events['value'])) { foreach ($events['value'] as $i => $event) { if (!isset($event['Id'])) { mtrace('Skipped an event because of malformed data.'); continue; } $idmapexists = $DB->record_exists('local_o365_calidmap', ['outlookeventid' => $event['Id']]); if ($idmapexists === false) { // Create Moodle event. $eventparams = ['name' => $event['Subject'], 'description' => $event['Body']['Content'], 'eventtype' => $calsub->caltype, 'repeatid' => 0, 'modulename' => 0, 'instance' => 0, 'timestart' => strtotime($event['Start']), 'visible' => 1, 'uuid' => '', 'sequence' => 1]; $end = strtotime($event['End']); $eventparams['timeduration'] = $end - $eventparams['timestart']; if ($calsub->caltype === 'user') { $eventparams['userid'] = $calsub->caltypeid; } if ($calsub->caltype === 'course') { $eventparams['courseid'] = $calsub->caltypeid; } $moodleevent = \calendar_event::create($eventparams, false); if (!empty($moodleevent) && !empty($moodleevent->id)) { $idmaprec = ['eventid' => $moodleevent->id, 'outlookeventid' => $event['Id'], 'origin' => 'o365', 'userid' => $calsub->user_id]; $DB->insert_record('local_o365_calidmap', (object) $idmaprec); mtrace('Successfully imported event #' . $moodleevent->id); } } } } else { mtrace('No new events to sync in.'); } } else { mtrace('Bad response received when fetching events.'); } } catch (\Exception $e) { mtrace('Error: ' . $e->getMessage()); } } $calsubs->close(); \local_o365\feature\calsync\observers::set_event_import(false); set_config('calsyncinlastrun', $starttime, 'local_o365'); return true; }
function appear_add_instance($data) { global $DB, $CFG, $USER; $appear = new stdClass(); $appear->course = $data->course; $appear->name = $data->name; $appear->intro = $data->intro; $appear->revision = $data->revision; $appear->timemodified = time(); $appear->timecreated = time(); $appear->timestart = $data->starttime; $appear->id = $DB->insert_record('appear', $appear); //creating group $group = new stdClass(); $group->name = $data->name . '_group_' . $appear->id; $group->courseid = $data->course; $group->hidepicture = 0; $group->id = groups_create_group($group); // creating event subscription $sub = new stdClass(); $sub->url = $CFG->wwwroot . '/mod/appear/view.php?id=' . $data->coursemodule; $sub->courseid = $data->course; $sub->groupid = $group->id; $sub->userid = $USER->id; $sub->pollinterval = 0; $subid = $DB->insert_record('event_subscriptions', $sub, true); // creating calender event $event = new stdClass(); $event->name = $data->name; $event->description = ''; $event->timestart = $data->starttime; $event->modulename = 'appear'; $event->timeduration = 3600; $event->uuid = 'uuid'; $event->subscriptionid = $subid; $event->userid = $USER->id; $event->groupid = $group->id; $event->courseid = $data->course; $event->eventtype = 'feedbackcloses'; $eventobj = calendar_event::create($event, false); return $appear->id; }
function geogebra_after_add_or_update($geogebra, $mform) { global $DB; if ($mform->get_data()->filetype === GEOGEBRA_FILE_TYPE_LOCAL) { $filename = geogebra_set_mainfile($geogebra); $geogebra->url = $filename; $result = $DB->update_record('geogebra', $geogebra); } if ($geogebra->timedue) { $event = new stdClass(); if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'geogebra', 'instance' => $geogebra->id))) { $event->name = $geogebra->name; $event->description = format_module_intro('geogebra', $geogebra, $geogebra->coursemodule); $event->timestart = $geogebra->timedue; $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { $event = new stdClass(); $event->name = $geogebra->name; $event->description = format_module_intro('geogebra', $geogebra, $geogebra->coursemodule); $event->courseid = $geogebra->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'geogebra'; $event->instance = $geogebra->id; $event->eventtype = 'due'; $event->timestart = $geogebra->timedue; $event->timeduration = 0; calendar_event::create($event); } } else { $DB->delete_records('event', array('modulename' => 'geogebra', 'instance' => $geogebra->id)); } // get existing grade item geogebra_grade_item_update($geogebra); return true; }
/** Create calendar events or update them * Set $prop->id, if you want to do an update instead of creating an new event * * @param string $name Event title * @param int $userid User id * @param string $type Event type * @param int $repeats Number of repeated events to create * @param int $timestart Time stamp of the event start * @param mixed $prop List of event properties as array or object * @return mixed Event object or false; * @since Moodle 2.5 */ public static function create_calendar_event($name, $userid = 0, $type = 'user', $repeats = 0, $timestart = null, $prop = null) { global $CFG, $DB, $USER, $SITE; require_once "{$CFG->dirroot}/calendar/lib.php"; if (!empty($prop)) { if (is_array($prop)) { $prop = (object) $prop; } } else { $prop = new stdClass(); } $prop->name = $name; if (empty($prop->eventtype)) { $prop->eventtype = $type; } if (empty($prop->repeats)) { $prop->repeats = $repeats; } if (empty($prop->timestart)) { $prop->timestart = time(); } if (empty($prop->timeduration)) { $prop->timeduration = 0; } if (empty($prop->repeats)) { $prop->repeat = 0; } else { $prop->repeat = 1; } if (empty($prop->userid)) { if (!empty($userid)) { $prop->userid = $userid; } else { return false; } } if (!isset($prop->courseid)) { $prop->courseid = $SITE->id; } $event = new calendar_event($prop); return $event->create($prop); }
/** * This function updates the events associated to the quiz. * If $override is non-zero, then it updates only the events * associated with the specified override. * * @uses QUIZ_MAX_EVENT_LENGTH * @param object $quiz the quiz object. * @param object optional $override limit to a specific override */ function quiz_update_events($quiz, $override = null) { global $DB; // Load the old events relating to this quiz. $conds = array('modulename'=>'quiz', 'instance'=>$quiz->id); if (!empty($override)) { // Only load events for this override. $conds['groupid'] = isset($override->groupid)? $override->groupid : 0; $conds['userid'] = isset($override->userid)? $override->userid : 0; } $oldevents = $DB->get_records('event', $conds); // Now make a todo list of all that needs to be updated. if (empty($override)) { // We are updating the primary settings for the quiz, so we // need to add all the overrides. $overrides = $DB->get_records('quiz_overrides', array('quiz' => $quiz->id)); // As well as the original quiz (empty override). $overrides[] = new stdClass(); } else { // Just do the one override. $overrides = array($override); } foreach ($overrides as $current) { $groupid = isset($current->groupid)? $current->groupid : 0; $userid = isset($current->userid)? $current->userid : 0; $timeopen = isset($current->timeopen)? $current->timeopen : $quiz->timeopen; $timeclose = isset($current->timeclose)? $current->timeclose : $quiz->timeclose; // Only add open/close events for an override if they differ from the quiz default. $addopen = empty($current->id) || !empty($current->timeopen); $addclose = empty($current->id) || !empty($current->timeclose); if (!empty($quiz->coursemodule)) { $cmid = $quiz->coursemodule; } else { $cmid = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course)->id; } $event = new stdClass(); $event->description = format_module_intro('quiz', $quiz, $cmid); // Events module won't show user events when the courseid is nonzero. $event->courseid = ($userid) ? 0 : $quiz->course; $event->groupid = $groupid; $event->userid = $userid; $event->modulename = 'quiz'; $event->instance = $quiz->id; $event->timestart = $timeopen; $event->timeduration = max($timeclose - $timeopen, 0); $event->visible = instance_is_visible('quiz', $quiz); $event->eventtype = 'open'; // Determine the event name. if ($groupid) { $params = new stdClass(); $params->quiz = $quiz->name; $params->group = groups_get_group_name($groupid); if ($params->group === false) { // Group doesn't exist, just skip it. continue; } $eventname = get_string('overridegroupeventname', 'quiz', $params); } else if ($userid) { $params = new stdClass(); $params->quiz = $quiz->name; $eventname = get_string('overrideusereventname', 'quiz', $params); } else { $eventname = $quiz->name; } if ($addopen or $addclose) { if ($timeclose and $timeopen and $event->timeduration <= QUIZ_MAX_EVENT_LENGTH) { // Single event for the whole quiz. if ($oldevent = array_shift($oldevents)) { $event->id = $oldevent->id; } else { unset($event->id); } $event->name = $eventname; // The method calendar_event::create will reuse a db record if the id field is set. calendar_event::create($event); } else { // Separate start and end events. $event->timeduration = 0; if ($timeopen && $addopen) { if ($oldevent = array_shift($oldevents)) { $event->id = $oldevent->id; } else { unset($event->id); } $event->name = $eventname.' ('.get_string('quizopens', 'quiz').')'; // The method calendar_event::create will reuse a db record if the id field is set. calendar_event::create($event); } if ($timeclose && $addclose) { if ($oldevent = array_shift($oldevents)) { $event->id = $oldevent->id; } else { unset($event->id); } $event->name = $eventname.' ('.get_string('quizcloses', 'quiz').')'; $event->timestart = $timeclose; $event->eventtype = 'close'; calendar_event::create($event); } } } } // Delete any leftover events. foreach ($oldevents as $badevent) { $badevent = calendar_event::load($badevent); $badevent->delete(); } }
/** * Updates an instance of the grouptool in the database * * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update an existing instance with new data. * * @param stdClass $grouptool An object from the form in mod_form.php * @param mod_grouptool_mod_form $mform * @return boolean Success/Fail */ function grouptool_update_instance(stdClass $grouptool, mod_grouptool_mod_form $mform = null) { global $DB, $CFG; $grouptool->timemodified = time(); $grouptool->id = $grouptool->instance; $cmid = $grouptool->coursemodule; if (!isset($grouptool->use_size)) { $grouptool->use_size = 0; } if (!isset($grouptool->use_individual)) { $grouptool->use_individual = 0; } if (!isset($grouptool->use_queue)) { $queues = $DB->count_records_sql("SELECT COUNT(DISTINCT queues.id)\n FROM {grouptool_agrps} agrps\n LEFT JOIN {grouptool_queued} queues ON queues.agrpid = agrps.id\n WHERE agrps.grouptoolid = ?", array($grouptool->instance)); if (!empty($queues)) { $grouptool->use_queue = 1; } else { $grouptool->use_queue = 0; } } if (!isset($grouptool->allow_multiple)) { $grouptool->allow_multiple = 0; } $grouptool->grpsize = clean_param($grouptool->grpsize, PARAM_INT); $grouptool->choose_min = clean_param($grouptool->choose_min, PARAM_INT); $grouptool->choose_max = clean_param($grouptool->choose_max, PARAM_INT); // Register students if immediate registration has been turned on! if ($grouptool->immediate_reg) { require_once $CFG->dirroot . '/mod/grouptool/locallib.php'; $instance = new mod_grouptool($grouptool->coursemodule, $grouptool); $instance->push_registrations(); } require_once $CFG->dirroot . '/calendar/lib.php'; $event = new stdClass(); if ($grouptool->allow_reg) { $event->name = get_string('registration_period_start', 'grouptool') . ' ' . $grouptool->name; } else { $event->name = $grouptool->name . ' ' . get_string('availabledate', 'grouptool'); } $event->description = format_module_intro('grouptool', $grouptool, $grouptool->coursemodule); if (!empty($grouptool->timeavailable)) { $event->timestart = $grouptool->timeavailable; } else { $grouptool->timecreated = $DB->get_field('grouptool', 'timecreated', array('id' => $grouptool->id)); $event->timestart = $grouptool->timecreated; } $event->visible = instance_is_visible('grouptool', $grouptool); $event->timeduration = 0; if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'grouptool', 'instance' => $grouptool->id, 'eventtype' => 'availablefrom'))) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event, false); } else { $event->courseid = $grouptool->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'grouptool'; $event->instance = $grouptool->id; /* * For activity module's events, this can be used to set the alternative text of the * event icon. Set it to 'pluginname' unless you have a better string. */ $event->eventtype = 'availablefrom'; calendar_event::create($event); } if ($grouptool->timedue != 0) { unset($event->id); unset($calendarevent); if ($grouptool->allow_reg) { $event->name = get_string('registration_period_end', 'grouptool') . ' ' . $grouptool->name; } else { $event->name = $grouptool->name . ' ' . get_string('duedate', 'grouptool'); } $event->timestart = $grouptool->timedue; $event->eventtype = 'deadline'; /* * For activity module's events, this can be used to set the alternative text of the * event icon. Set it to 'pluginname' unless you have a better string. */ if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'grouptool', 'instance' => $grouptool->id, 'eventtype' => 'due'))) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event, false); } else { unset($event->id); $event->courseid = $grouptool->course; // We've got some permission issues with calendar_event::create() so we work around that! $calev = new calendar_event($event); $calev->update($event, false); } } else { if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'grouptool', 'instance' => $grouptool->id, 'eventtype' => 'due'))) { $calendarevent = calendar_event::load($event->id); $calendarevent->delete(true); } } $coursegroups = $DB->get_fieldset_select('groups', 'id', 'courseid = ?', array($grouptool->course)); foreach ($coursegroups as $groupid) { if (!$DB->record_exists('grouptool_agrps', array('grouptoolid' => $grouptool->instance, 'groupid' => $groupid))) { $record = new stdClass(); $record->grouptoolid = $grouptool->instance; $record->groupid = $groupid; $record->sort_order = 9999999; $record->grpsize = $grouptool->grpsize; $record->active = 0; $DB->insert_record('grouptool_agrps', $record); } } // We have to override the functions fetching of data, because it's not updated yet! grouptool_update_queues($grouptool); return $DB->update_record('grouptool', $grouptool); }
/** * Update a vpl instance * * @param object from the form in mod.html * @return boolean OK * **/ function vpl_update_instance($instance) { global $CFG, $DB; require_once $CFG->dirroot . '/calendar/lib.php'; vpl_truncate_VPL($instance); $instance->id = $instance->instance; //Update event $event = vpl_create_event($instance, $instance->id); if ($eventid = $DB->get_field('event', 'id', array('modulename' => VPL, 'instance' => $instance->id))) { $event->id = $eventid; $calendarevent = calendar_event::load($eventid); if ($instance->duedate) { $calendarevent->update($event); } else { $calendarevent->delete(); } } else { if ($instance->duedate) { calendar_event::create($event); } } $cm = get_coursemodule_from_instance(VPL, $instance->id, $instance->course); vpl_update_grade_item($instance, $cm); return $DB->update_record(VPL, $instance); }
/** * Runs any processes that must be run * after a bigbluebuttonbn insert/update * * @global object * @param object $bigbluebuttonbn BigBlueButtonBN form data * @return void **/ function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) { global $DB, $CFG, $USER; // Now that an id was assigned, generate and set the meetingid property based on // [Moodle Instance + Activity ID + BBB Secret] (but only for new activities) if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) { $bigbluebuttonbn_meetingid = sha1($CFG->wwwroot . $bigbluebuttonbn->id . bigbluebuttonbn_get_cfg_shared_secret()); $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn_meetingid, array('id' => $bigbluebuttonbn->id)); $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn'); } else { $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn'); } // Add evento to the calendar when if openingtime is set if (isset($bigbluebuttonbn->openingtime) && $bigbluebuttonbn->openingtime) { $event = new stdClass(); $event->name = $bigbluebuttonbn->name; $event->courseid = $bigbluebuttonbn->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'bigbluebuttonbn'; $event->instance = $bigbluebuttonbn->id; $event->timestart = $bigbluebuttonbn->openingtime; if ($bigbluebuttonbn->closingtime) { $event->durationtime = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime; } else { $event->durationtime = 0; } if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { calendar_event::create($event); } } else { $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id)); } if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) { // Prepare message $msg = new stdClass(); /// Build the message_body $msg->action = $action; $msg->activity_type = ""; $msg->activity_title = $bigbluebuttonbn->name; /// Add the meeting details to the message_body $msg->action = ucfirst($action); $msg->activity_description = ""; if (!empty($bigbluebuttonbn->intro)) { $msg->activity_description = trim($bigbluebuttonbn->intro); } $msg->activity_openingtime = ""; if ($bigbluebuttonbn->openingtime) { $date = new stdClass(); $date->day = calendar_day_representation($bigbluebuttonbn->openingtime); $date->time = calendar_time_representation($bigbluebuttonbn->openingtime); $msg->activity_openingtime = get_string('email_date', 'bigbluebuttonbn', $date); } $msg->activity_closingtime = ""; if ($bigbluebuttonbn->closingtime) { $date = new stdClass(); $date->day = calendar_day_representation($bigbluebuttonbn->closingtime); $date->time = calendar_time_representation($bigbluebuttonbn->closingtime); $msg->activity_closingtime = get_string('email_date', 'bigbluebuttonbn', $date); } $msg->activity_owner = fullname($USER); $message_text = get_string('email_body_notification', 'bigbluebuttonbn', $msg); // Send notification to all users enrolled bigbluebuttonbn_send_notification($USER, $bigbluebuttonbn, $message_text); } }
/** * Update the calendar entries for this assignment. * * @param int $coursemoduleid - Required to pass this in because it might * not exist in the database yet. * @return bool */ public function update_calendar($coursemoduleid) { global $DB, $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; // Special case for add_instance as the coursemodule has not been set yet. $instance = $this->get_instance(); if ($instance->duedate) { $event = new stdClass(); $params = array('modulename' => 'assign', 'instance' => $instance->id); $event->id = $DB->get_field('event', 'id', $params); if ($event->id) { $event->name = $instance->name; $event->description = format_module_intro('assign', $instance, $coursemoduleid); $event->timestart = $instance->duedate; $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { $event = new stdClass(); $event->name = $instance->name; $event->description = format_module_intro('assign', $instance, $coursemoduleid); $event->courseid = $instance->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'assign'; $event->instance = $instance->id; $event->eventtype = 'due'; $event->timestart = $instance->duedate; $event->timeduration = 0; calendar_event::create($event); } } else { $DB->delete_records('event', array('modulename' => 'assign', 'instance' => $instance->id)); } }
/** * Call this function to add an event to the calendar table and to call any calendar plugins * * @param object $event An object representing an event from the calendar table. * The event will be identified by the id field. The object event should include the following: * <ul> * <li><b>$event->name</b> - Name for the event * <li><b>$event->description</b> - Description of the event (defaults to '') * <li><b>$event->format</b> - Format for the description (using formatting types defined at the top of weblib.php) * <li><b>$event->courseid</b> - The id of the course this event belongs to (0 = all courses) * <li><b>$event->groupid</b> - The id of the group this event belongs to (0 = no group) * <li><b>$event->userid</b> - The id of the user this event belongs to (0 = no user) * <li><b>$event->modulename</b> - Name of the module that creates this event * <li><b>$event->instance</b> - Instance of the module that owns this event * <li><b>$event->eventtype</b> - The type info together with the module info could * be used by calendar plugins to decide how to display event * <li><b>$event->timestart</b>- Timestamp for start of event * <li><b>$event->timeduration</b> - Duration (defaults to zero) * <li><b>$event->visible</b> - 0 if the event should be hidden (e.g. because the activity that created it is hidden) * </ul> * @return int|false The id number of the resulting record or false if failed * @deprecated please use calendar_event::create() instead. * @todo final deprecation of this function in MDL-40607 */ function add_event($event) { global $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; debugging('add_event() is deprecated, please use calendar_event::create() instead.', DEBUG_DEVELOPER); $event = calendar_event::create($event); if ($event !== false) { return $event->id; } return false; }
/** * Call this function to add an event to the calendar table and to call any calendar plugins * * @param object $event An object representing an event from the calendar table. * The event will be identified by the id field. The object event should include the following: * <ul> * <li><b>$event->name</b> - Name for the event * <li><b>$event->description</b> - Description of the event (defaults to '') * <li><b>$event->format</b> - Format for the description (using formatting types defined at the top of weblib.php) * <li><b>$event->courseid</b> - The id of the course this event belongs to (0 = all courses) * <li><b>$event->groupid</b> - The id of the group this event belongs to (0 = no group) * <li><b>$event->userid</b> - The id of the user this event belongs to (0 = no user) * <li><b>$event->modulename</b> - Name of the module that creates this event * <li><b>$event->instance</b> - Instance of the module that owns this event * <li><b>$event->eventtype</b> - The type info together with the module info could * be used by calendar plugins to decide how to display event * <li><b>$event->timestart</b>- Timestamp for start of event * <li><b>$event->timeduration</b> - Duration (defaults to zero) * <li><b>$event->visible</b> - 0 if the event should be hidden (e.g. because the activity that created it is hidden) * </ul> * @return int|false The id number of the resulting record or false if failed */ function add_event($event) { global $CFG; require_once($CFG->dirroot.'/calendar/lib.php'); $event = calendar_event::create($event); if ($event !== false) { return $event->id; } return false; }
/** * Update the event if it exists, else create */ function simplecertificate_send_event($certificate) { global $DB, $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; if ($event = $DB->get_record('event', array('modulename' => 'simplecertificate', 'instance' => $certificate->id))) { $calendarevent = calendar_event::load($event->id); $calendarevent->name = $certificate->name; $calendarevent->update($calendarevent); } else { $event = new stdClass(); $event->name = $certificate->name; $event->description = ''; $event->courseid = $certificate->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'simplecertificate'; $event->instance = $certificate->id; calendar_event::create($event); } }
function elluminate_update_events($elluminate) { global $DB; if ($elluminate->sessiontype == 0 || $elluminate->sessiontype == 1) { $oldevents = $DB->get_records('event', array('modulename' => 'elluminate', 'instance' => $elluminate->id)); } else { if ($elluminate->sessiontype == 2) { $oldevents = $DB->get_records('event', array('modulename' => 'elluminate', 'instance' => $elluminate->groupparentid, 'groupid' => $elluminate->groupid)); } else { if ($elluminate->sessiontype == 3) { if ($elluminate->groupmode != 0) { $oldevents = $DB->get_records('event', array('modulename' => 'elluminate', 'instance' => $elluminate->groupparentid, 'groupid' => $elluminate->groupid)); } else { $oldevents = $DB->get_records('event', array('modulename' => 'elluminate', 'instance' => $elluminate->id)); } } } } $event = new stdClass(); foreach ($oldevents as $oldevent) { $event = $oldevent; } $event->description = $elluminate->description; $event->courseid = $elluminate->course; // Events module won't show user events when the courseid is nonzero if ($elluminate->sessiontype == 0 || $elluminate->sessiontype == 1) { //course private $event->groupid = 0; $event->eventtype = 'open'; $event->name = $elluminate->name; $event->instance = $elluminate->id; } else { if ($elluminate->sessiontype == 3) { //grouping if ($elluminate->groupmode != 0) { //Seperate or Visible groups $event->groupid = $elluminate->groupid; $event->eventtype = 'group'; $event->name = $elluminate->sessionname; $event->instance = $elluminate->groupparentid; } else { //No groups $event->groupid = 0; $event->eventtype = 'open'; $event->name = $elluminate->sessionname; $event->instance = $elluminate->id; } } else { //group $event->groupid = $elluminate->groupid; $event->eventtype = 'group'; $event->name = $elluminate->sessionname; $event->instance = $elluminate->groupparentid; } } $event->modulename = 'elluminate'; $event->timestart = $elluminate->timestart; $event->timeduration = max($elluminate->timeend - $elluminate->timestart, 0); $event->visible = instance_is_visible('elluminate', $elluminate); calendar_event::create($event); }
function questionnaire_set_events($questionnaire) { // Adding the questionnaire to the eventtable. global $DB; if ($events = $DB->get_records('event', array('modulename' => 'questionnaire', 'instance' => $questionnaire->id))) { foreach ($events as $event) { $event = calendar_event::load($event); $event->delete(); } } // The open-event. $event = new stdClass(); $event->description = $questionnaire->name; $event->courseid = $questionnaire->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'questionnaire'; $event->instance = $questionnaire->id; $event->eventtype = 'open'; $event->timestart = $questionnaire->opendate; $event->visible = instance_is_visible('questionnaire', $questionnaire); $event->timeduration = $questionnaire->closedate - $questionnaire->opendate; if ($questionnaire->closedate && $questionnaire->opendate && $event->timeduration <= QUESTIONNAIRE_MAX_EVENT_LENGTH) { // Single event for the whole questionnaire. $event->name = $questionnaire->name; calendar_event::create($event); } else { // Separate start and end events. $event->timeduration = 0; if ($questionnaire->opendate) { $event->name = $questionnaire->name . ' (' . get_string('questionnaireopens', 'questionnaire') . ')'; calendar_event::create($event); unset($event->id); // So we can use the same object for the close event. } if ($questionnaire->closedate) { $event->name = $questionnaire->name . ' (' . get_string('questionnairecloses', 'questionnaire') . ')'; $event->timestart = $questionnaire->closedate; $event->eventtype = 'close'; calendar_event::create($event); } } }
/** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update an existing instance with new data. * * @param object $bigbluebuttonbn An object from the form in mod_form.php * @return boolean Success/Fail */ function bigbluebuttonbn_update_instance($bigbluebuttonbn) { global $DB; $bigbluebuttonbn->timemodified = time(); $bigbluebuttonbn->id = $bigbluebuttonbn->instance; if (!isset($bigbluebuttonbn->newwindow)) { $bigbluebuttonbn->newwindow = 0; } if (!isset($bigbluebuttonbn->wait)) { $bigbluebuttonbn->wait = 0; } if (!isset($bigbluebuttonbn->record)) { $bigbluebuttonbn->record = 0; } if (!isset($bigbluebuttonbn->allmoderators)) { $bigbluebuttonbn->allmoderators = 0; } $returnid = $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn); if (isset($bigbluebuttonbn->timeavailable) && $bigbluebuttonbn->timeavailable) { $event = new stdClass(); $event->name = $bigbluebuttonbn->name; $event->courseid = $bigbluebuttonbn->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'bigbluebuttonbn'; $event->instance = $bigbluebuttonbn->id; $event->timestart = $bigbluebuttonbn->timeavailable; if ($bigbluebuttonbn->timedue) { $event->timeduration = $bigbluebuttonbn->timedue - $bigbluebuttonbn->timeavailable; } else { $event->timeduration = 0; } if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { calendar_event::create($event); } } else { $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id)); } return $returnid; }
/** * This function updates the events associated to the lesson. * If $override is non-zero, then it updates only the events * associated with the specified override. * * @uses LESSON_MAX_EVENT_LENGTH * @param object $lesson the lesson object. * @param object $override (optional) limit to a specific override */ function lesson_update_events($lesson, $override = null) { global $CFG, $DB; require_once $CFG->dirroot . '/calendar/lib.php'; // Load the old events relating to this lesson. $conds = array('modulename' => 'lesson', 'instance' => $lesson->id); if (!empty($override)) { // Only load events for this override. if (isset($override->userid)) { $conds['userid'] = $override->userid; } else { $conds['groupid'] = $override->groupid; } } $oldevents = $DB->get_records('event', $conds); // Now make a todo list of all that needs to be updated. if (empty($override)) { // We are updating the primary settings for the lesson, so we // need to add all the overrides. $overrides = $DB->get_records('lesson_overrides', array('lessonid' => $lesson->id)); // As well as the original lesson (empty override). $overrides[] = new stdClass(); } else { // Just do the one override. $overrides = array($override); } foreach ($overrides as $current) { $groupid = isset($current->groupid) ? $current->groupid : 0; $userid = isset($current->userid) ? $current->userid : 0; $available = isset($current->available) ? $current->available : $lesson->available; $deadline = isset($current->deadline) ? $current->deadline : $lesson->deadline; // Only add open/close events for an override if they differ from the lesson default. $addopen = empty($current->id) || !empty($current->available); $addclose = empty($current->id) || !empty($current->deadline); if (!empty($lesson->coursemodule)) { $cmid = $lesson->coursemodule; } else { $cmid = get_coursemodule_from_instance('lesson', $lesson->id, $lesson->course)->id; } $event = new stdClass(); $event->description = format_module_intro('lesson', $lesson, $cmid); // Events module won't show user events when the courseid is nonzero. $event->courseid = $userid ? 0 : $lesson->course; $event->groupid = $groupid; $event->userid = $userid; $event->modulename = 'lesson'; $event->instance = $lesson->id; $event->timestart = $available; $event->timeduration = max($deadline - $available, 0); $event->visible = instance_is_visible('lesson', $lesson); $event->eventtype = 'open'; // Determine the event name. if ($groupid) { $params = new stdClass(); $params->lesson = $lesson->name; $params->group = groups_get_group_name($groupid); if ($params->group === false) { // Group doesn't exist, just skip it. continue; } $eventname = get_string('overridegroupeventname', 'lesson', $params); } else { if ($userid) { $params = new stdClass(); $params->lesson = $lesson->name; $eventname = get_string('overrideusereventname', 'lesson', $params); } else { $eventname = $lesson->name; } } if ($addopen or $addclose) { if ($deadline and $available and $event->timeduration <= LESSON_MAX_EVENT_LENGTH) { // Single event for the whole lesson. if ($oldevent = array_shift($oldevents)) { $event->id = $oldevent->id; } else { unset($event->id); } $event->name = $eventname; // The method calendar_event::create will reuse a db record if the id field is set. calendar_event::create($event); } else { // Separate start and end events. $event->timeduration = 0; if ($available && $addopen) { if ($oldevent = array_shift($oldevents)) { $event->id = $oldevent->id; } else { unset($event->id); } $event->name = $eventname . ' (' . get_string('lessonopens', 'lesson') . ')'; // The method calendar_event::create will reuse a db record if the id field is set. calendar_event::create($event); } if ($deadline && $addclose) { if ($oldevent = array_shift($oldevents)) { $event->id = $oldevent->id; } else { unset($event->id); } $event->name = $eventname . ' (' . get_string('lessoncloses', 'lesson') . ')'; $event->timestart = $deadline; $event->eventtype = 'close'; calendar_event::create($event); } } } } // Delete any leftover events. foreach ($oldevents as $badevent) { $badevent = calendar_event::load($badevent); $badevent->delete(); } }
/** * Add an iCalendar event to the Moodle calendar. * * @param stdClass $event The RFC-2445 iCalendar event * @param int $courseid The course ID * @param int $subscriptionid The iCalendar subscription ID * @param string $timezone The X-WR-TIMEZONE iCalendar property if provided * @throws dml_exception A DML specific exception is thrown for invalid subscriptionids. * @return int Code: CALENDAR_IMPORT_EVENT_UPDATED = updated, CALENDAR_IMPORT_EVENT_INSERTED = inserted, 0 = error */ function calendar_add_icalendar_event($event, $courseid, $subscriptionid, $timezone = 'UTC') { global $DB; // Probably an unsupported X-MICROSOFT-CDO-BUSYSTATUS event. if (empty($event->properties['SUMMARY'])) { return 0; } $name = $event->properties['SUMMARY'][0]->value; $name = str_replace('\\n', '<br />', $name); $name = str_replace('\\', '', $name); $name = preg_replace('/\\s+/u', ' ', $name); $eventrecord = new stdClass(); $eventrecord->name = clean_param($name, PARAM_NOTAGS); if (empty($event->properties['DESCRIPTION'][0]->value)) { $description = ''; } else { $description = $event->properties['DESCRIPTION'][0]->value; $description = clean_param($description, PARAM_NOTAGS); $description = str_replace('\\n', '<br />', $description); $description = str_replace('\\', '', $description); $description = preg_replace('/\\s+/u', ' ', $description); } $eventrecord->description = $description; // Probably a repeating event with RRULE etc. TODO: skip for now. if (empty($event->properties['DTSTART'][0]->value)) { return 0; } $tz = isset($event->properties['DTSTART'][0]->parameters['TZID']) ? $event->properties['DTSTART'][0]->parameters['TZID'] : $timezone; $tz = core_date::normalise_timezone($tz); $eventrecord->timestart = strtotime($event->properties['DTSTART'][0]->value . ' ' . $tz); if (empty($event->properties['DTEND'])) { $eventrecord->timeduration = 0; // no duration if no end time specified } else { $endtz = isset($event->properties['DTEND'][0]->parameters['TZID']) ? $event->properties['DTEND'][0]->parameters['TZID'] : $timezone; $endtz = core_date::normalise_timezone($endtz); $eventrecord->timeduration = strtotime($event->properties['DTEND'][0]->value . ' ' . $endtz) - $eventrecord->timestart; } // Check to see if it should be treated as an all day event. if ($eventrecord->timeduration == DAYSECS) { // Check to see if the event started at Midnight on the imported calendar. date_default_timezone_set($timezone); if (date('H:i:s', $eventrecord->timestart) === "00:00:00") { // This event should be an all day event. $eventrecord->timeduration = 0; } core_date::set_default_server_timezone(); } $eventrecord->uuid = $event->properties['UID'][0]->value; $eventrecord->timemodified = time(); // Add the iCal subscription details if required. // We should never do anything with an event without a subscription reference. $sub = calendar_get_subscription($subscriptionid); $eventrecord->subscriptionid = $subscriptionid; $eventrecord->userid = $sub->userid; $eventrecord->groupid = $sub->groupid; $eventrecord->courseid = $sub->courseid; $eventrecord->eventtype = $sub->eventtype; if ($updaterecord = $DB->get_record('event', array('uuid' => $eventrecord->uuid))) { $eventrecord->id = $updaterecord->id; $return = CALENDAR_IMPORT_EVENT_UPDATED; // Update. } else { $return = CALENDAR_IMPORT_EVENT_INSERTED; // Insert. } if ($createdevent = calendar_event::create($eventrecord, false)) { if (!empty($event->properties['RRULE'])) { // Repeating events. date_default_timezone_set($tz); // Change time zone to parse all events. $rrule = new \core_calendar\rrule_manager($event->properties['RRULE'][0]->value); $rrule->parse_rrule(); $rrule->create_events($createdevent); core_date::set_default_server_timezone(); // Change time zone back to what it was. } return $return; } else { return 0; } }
/** * This standard function will check all instances of this module * and make sure there are up-to-date events created for each of them. * If courseid = 0, then every chat event in the site is checked, else * only chat events belonging to the course specified are checked. * This function is used, in its new format, by restore_refresh_events() * * @global object * @param int $courseid * @return bool */ function chat_refresh_events($courseid = 0) { global $DB; if ($courseid) { if (!($chats = $DB->get_records("chat", array("course" => $courseid)))) { return true; } } else { if (!($chats = $DB->get_records("chat"))) { return true; } } $moduleid = $DB->get_field('modules', 'id', array('name' => 'chat')); foreach ($chats as $chat) { $cm = get_coursemodule_from_id('chat', $chat->id); $event = new stdClass(); $event->name = $chat->name; $event->description = format_module_intro('chat', $chat, $cm->id); $event->timestart = $chat->chattime; if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'chat', 'instance' => $chat->id))) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { $event->courseid = $chat->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'chat'; $event->instance = $chat->id; $event->eventtype = 'chattime'; $event->timeduration = 0; $event->visible = $DB->get_field('course_modules', 'visible', array('module' => $moduleid, 'instance' => $chat->id)); calendar_event::create($event); } } return true; }
/** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will create a new instance and return the id number * of the new instance. * * @param object $adobeconnect An object from the form in mod_form.php * @return int The id of the newly inserted adobeconnect record */ function adobeconnect_add_instance($adobeconnect) { global $COURSE, $USER, $DB; $adobeconnect->timecreated = time(); $adobeconnect->meeturl = adobeconnect_clean_meet_url($adobeconnect->meeturl); $return = false; $meeting = new stdClass(); // Assign the current user with the Adobe Presenter role $context = get_context_instance(CONTEXT_COURSE, $adobeconnect->course); if (!has_capability('mod/adobeconnect:meetinghost', $context, $USER->id, false)) { $param = array('shortname' => 'adobeconnecthost'); $roleid = $DB->get_field('role', 'id', $param); if (role_assign($roleid, $USER->id, $context->id, 'mod_adobeconnect')) { //DEBUG } else { echo 'role assignment failed'; die; } } $recid = $DB->insert_record('adobeconnect', $adobeconnect); if (empty($recid)) { return false; } $aconnect = aconnect_login(); $meetfldscoid = aconnect_get_folder($aconnect, 'my-meetings'); $meeting = clone $adobeconnect; if (0 != $adobeconnect->groupmode) { // Allow for multiple groups // get all groups for the course $crsgroups = groups_get_all_groups($COURSE->id); if (empty($crsgroups)) { return 0; } require_once dirname(dirname(dirname(__FILE__))) . '/group/lib.php'; // Create the meeting for each group foreach ($crsgroups as $crsgroup) { // The teacher role if they don't already have one and // Assign them to each group if (!groups_is_member($crsgroup->id, $USER->id)) { $param = array('shortname' => 'editingteacher'); $roleid = $DB->get_field('role', 'id', $param); if (!user_has_role_assignment($USER->id, $roleid, $context->id)) { role_assign($roleid, $USER->id, $context->id, 'mod_adobeconnect'); } groups_add_member($crsgroup->id, $USER->id); } $meeting->name = $adobeconnect->name . '_' . $crsgroup->name; if (!empty($adobeconnect->meeturl)) { $meeting->meeturl = adobeconnect_clean_meet_url($adobeconnect->meeturl . '_' . $crsgroup->name); } if (!($meetingscoid = aconnect_create_meeting($aconnect, $meeting, $meetfldscoid))) { debugging('error creating meeting', DEBUG_DEVELOPER); } // Update permissions for meeting if (empty($adobeconnect->meetingpublic)) { aconnect_update_meeting_perm($aconnect, $meetingscoid, ADOBE_MEETPERM_PRIVATE); } else { aconnect_update_meeting_perm($aconnect, $meetingscoid, ADOBE_MEETPERM_PUBLIC); } // Insert record to activity instance in meeting_groups table $record = new stdClass(); $record->instanceid = $recid; $record->meetingscoid = $meetingscoid; $record->groupid = $crsgroup->id; $record->id = $DB->insert_record('adobeconnect_meeting_groups', $record); // Add event to calendar $event = new stdClass(); $event->name = $meeting->name; $event->description = format_module_intro('adobeconnect', $adobeconnect, $adobeconnect->coursemodule); $event->courseid = $adobeconnect->course; $event->groupid = $crsgroup->id; $event->userid = 0; $event->instance = $recid; $event->eventtype = 'group'; $event->timestart = $adobeconnect->starttime; $event->timeduration = $adobeconnect->endtime - $adobeconnect->starttime; $event->visible = 1; $event->modulename = 'adobeconnect'; calendar_event::create($event); } } else { // no groups support $meetingscoid = aconnect_create_meeting($aconnect, $meeting, $meetfldscoid); // Update permissions for meeting if (empty($adobeconnect->meetingpublic)) { aconnect_update_meeting_perm($aconnect, $meetingscoid, ADOBE_MEETPERM_PRIVATE); } else { aconnect_update_meeting_perm($aconnect, $meetingscoid, ADOBE_MEETPERM_PUBLIC); } // Insert record to activity instance in meeting_groups table $record = new stdClass(); $record->instanceid = $recid; $record->meetingscoid = $meetingscoid; $record->groupid = 0; $record->id = $DB->insert_record('adobeconnect_meeting_groups', $record); // Add event to calendar $event = new stdClass(); $event->name = $meeting->name; $event->description = format_module_intro('adobeconnect', $adobeconnect, $adobeconnect->coursemodule); $event->courseid = $adobeconnect->course; $event->groupid = 0; $event->userid = 0; $event->instance = $recid; $event->eventtype = 'course'; $event->timestart = $adobeconnect->starttime; $event->timeduration = $adobeconnect->endtime - $adobeconnect->starttime; $event->visible = 1; $event->modulename = 'adobeconnect'; calendar_event::create($event); } // If no meeting URL was submitted, // update meeting URL for activity with server assigned URL if (empty($adobeconnect->meeturl) and 0 == $adobeconnect->groupmode) { $filter = array('filter-sco-id' => $meetingscoid); $meeting = aconnect_meeting_exists($aconnect, $meetfldscoid, $filter); if (!empty($meeting)) { $meeting = current($meeting); $record = new stdClass(); $record->id = $recid; $record->meeturl = trim($meeting->url, '/'); $DB->update_record('adobeconnect', $record); } } aconnect_logout($aconnect); return $recid; }
private function update_calendar_events($eventtype, array $userids, $eventdata) { global $CFG, $DB; require_once $CFG->dirroot . '/calendar/lib.php'; $eventdata->eventtype = $eventtype; $existingevents = $DB->get_records('event', array('modulename' => 'scheduler', 'eventtype' => $eventtype)); $handledevents = array(); $handledusers = array(); // Update existing calendar events. foreach ($existingevents as $eventid => $existingdata) { if (in_array($existingdata->userid, $userids)) { $eventdata->userid = $existingdata->userid; $calendarevent = calendar_event::load($existingdata); $calendarevent->update($eventdata, false); $handledevents[] = $eventid; $handledusers[] = $existingdata->userid; } } // Add new calendar events. foreach ($userids as $userid) { if (!in_array($userid, $handledusers)) { $thisevent = clone $eventdata; $thisevent->userid = $userid; calendar_event::create($thisevent, false); } } // Remove old, obsolete calendar events. foreach ($existingevents as $eventid => $existingdata) { if (!in_array($eventid, $handledevents)) { $calendarevent = calendar_event::load($existingdata); $calendarevent->delete(); } } }
/** * This is a standard Moodle module that checks to make sure there are events for each activity * * @param var $courseid The ID of the course this activity belongs to (default 0 for all courses) * @return bool success */ function turnitintool_refresh_events($courseid = 0) { if ($courseid == 0) { if (!($turnitintools = turnitintool_get_records("turnitintool"))) { $result = true; } } else { if (!($turnitintools = turnitintool_get_records("turnitintool", "course", $courseid))) { $result = true; } } $module = turnitintool_get_records_select('modules', "name='turnitintool'", NULL, 'id'); $moduleid = current(array_keys($module)); foreach ($turnitintools as $turnitintool) { $event = new stdClass(); $event->description = $turnitintool->intro; if (!($parts = turnitintool_get_records("turnitintool_parts", "turnitintoolid", $turnitintool->id))) { $result = false; } foreach ($parts as $part) { $event->timestart = $part->dtdue; if ($events = turnitintool_get_record_select('event', "modulename='turnitintool' AND instance=" . $turnitintool->id . " AND name='" . $turnitintool->name . " - " . $part->partname . "'")) { $event->id = $events->id; if (method_exists('calendar_event', 'update')) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { update_event($event); } } else { $event->courseid = $turnitintool->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'turnitintool'; $event->instance = $turnitintool->id; $event->eventtype = 'due'; $event->timeduration = 0; $event->name = $turnitintool->name . ' - ' . $part->partname; $coursemodule = turnitintool_get_record('course_modules', 'module', $moduleid, 'instance', $turnitintool->id); $event->visible = $coursemodule->visible; if (method_exists('calendar_event', 'create')) { calendar_event::create($event); } else { add_event($event); } } } $result = true; } return $result; }
/** * This function updates the events associated to the offlinequiz. * If $override is non-zero, then it updates only the events * associated with the specified override. * * @uses OFFLINEQUIZ_MAX_EVENT_LENGTH * @param object $offlinequiz the offlinequiz object. * @param object optional $override limit to a specific override */ function offlinequiz_update_events($offlinequiz) { global $DB, $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; // Load the old events relating to this offlinequiz. $conds = array('modulename' => 'offlinequiz', 'instance' => $offlinequiz->id); if (!empty($override)) { // Only load events for this override. $conds['groupid'] = isset($override->groupid) ? $override->groupid : 0; $conds['userid'] = isset($override->userid) ? $override->userid : 0; } $oldevents = $DB->get_records('event', $conds); $groupid = 0; $userid = 0; $timeopen = $offlinequiz->timeopen; $timeclose = $offlinequiz->timeclose; if ($offlinequiz->time) { $timeopen = $offlinequiz->time; } // Only add open/close events if they differ from the offlinequiz default. if (!empty($offlinequiz->coursemodule)) { $cmid = $offlinequiz->coursemodule; } else { $cmid = get_coursemodule_from_instance('offlinequiz', $offlinequiz->id, $offlinequiz->course)->id; } if (!empty($timeopen)) { $event = new stdClass(); $event->name = $offlinequiz->name; $event->description = format_module_intro('offlinequiz', $offlinequiz, $cmid); // Events module won't show user events when the courseid is nonzero. $event->courseid = $userid ? 0 : $offlinequiz->course; $event->groupid = $groupid; $event->userid = $userid; $event->modulename = 'offlinequiz'; $event->instance = $offlinequiz->id; $event->timestart = $timeopen; $event->timeduration = max($timeclose - $timeopen, 0); $event->visible = instance_is_visible('offlinequiz', $offlinequiz); if ($timeopen == $offlinequiz->time) { $event->name = $offlinequiz->name; } if ($timeopen == $offlinequiz->timeopen) { $event->name = $offlinequiz->name . ' (' . get_string('reportstarts', 'offlinequiz') . ')'; } calendar_event::create($event); } // Delete any leftover events. foreach ($oldevents as $badevent) { $badevent = calendar_event::load($badevent); $badevent->delete(); } }
/** * Update the calendar entries for this assignment. * * @param int $coursemoduleid - Required to pass this in because it might * not exist in the database yet. * @return bool */ public function update_calendar($coursemoduleid) { global $DB, $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; // Special case for add_instance as the coursemodule has not been set yet. $instance = $this->get_instance(); if ($instance->duedate) { $event = new stdClass(); $params = array('modulename' => 'assign', 'instance' => $instance->id); $event->id = $DB->get_field('event', 'id', $params); $event->name = $instance->name; $event->timestart = $instance->duedate; // Convert the links to pluginfile. It is a bit hacky but at this stage the files // might not have been saved in the module area yet. $intro = $instance->intro; if ($draftid = file_get_submitted_draft_itemid('introeditor')) { $intro = file_rewrite_urls_to_pluginfile($intro, $draftid); } // We need to remove the links to files as the calendar is not ready // to support module events with file areas. $intro = strip_pluginfile_content($intro); if ($this->show_intro()) { $event->description = array('text' => $intro, 'format' => $instance->introformat); } else { $event->description = array('text' => '', 'format' => $instance->introformat); } if ($event->id) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { unset($event->id); $event->courseid = $instance->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'assign'; $event->instance = $instance->id; $event->eventtype = 'due'; $event->timeduration = 0; calendar_event::create($event); } } else { $DB->delete_records('event', array('modulename' => 'assign', 'instance' => $instance->id)); } }
/** * Runs any processes that must be run * after a lesson insert/update * * @global object * @param object $lesson Lesson form data * @return void **/ function lesson_process_post_save(&$lesson) { global $DB, $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; require_once $CFG->dirroot . '/mod/lesson/locallib.php'; if ($events = $DB->get_records('event', array('modulename' => 'lesson', 'instance' => $lesson->id))) { foreach ($events as $event) { $event = calendar_event::load($event->id); $event->delete(); } } $event = new stdClass(); $event->description = $lesson->name; $event->courseid = $lesson->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'lesson'; $event->instance = $lesson->id; $event->eventtype = 'open'; $event->timestart = $lesson->available; $event->visible = instance_is_visible('lesson', $lesson); $event->timeduration = $lesson->deadline - $lesson->available; if ($lesson->deadline and $lesson->available and $event->timeduration <= LESSON_MAX_EVENT_LENGTH) { // Single event for the whole lesson. $event->name = $lesson->name; calendar_event::create(clone $event); } else { // Separate start and end events. $event->timeduration = 0; if ($lesson->available) { $event->name = $lesson->name . ' (' . get_string('lessonopens', 'lesson') . ')'; calendar_event::create(clone $event); } if ($lesson->deadline) { $event->name = $lesson->name . ' (' . get_string('lessoncloses', 'lesson') . ')'; $event->timestart = $lesson->deadline; $event->eventtype = 'close'; calendar_event::create(clone $event); } } }
/** * Updates a new assignment activity * * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update the assignment instance and return the id number * The due date is updated in the calendar * This is common to all assignment types. * * @global object * @global object * @param object $assignment The data from the form on mod_form.php * @return bool success */ function update_instance($assignment) { global $COURSE, $DB; $assignment->timemodified = time(); $assignment->id = $assignment->instance; $assignment->courseid = $assignment->course; $DB->update_record('assignment', $assignment); if ($assignment->timedue) { $event = new stdClass(); if ($event->id = $DB->get_field('event', 'id', array('modulename'=>'assignment', 'instance'=>$assignment->id))) { $event->name = $assignment->name; $event->description = format_module_intro('assignment', $assignment, $assignment->coursemodule); $event->timestart = $assignment->timedue; $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); } else { $event = new stdClass(); $event->name = $assignment->name; $event->description = format_module_intro('assignment', $assignment, $assignment->coursemodule); $event->courseid = $assignment->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'assignment'; $event->instance = $assignment->id; $event->eventtype = 'due'; $event->timestart = $assignment->timedue; $event->timeduration = 0; calendar_event::create($event); } } else { $DB->delete_records('event', array('modulename'=>'assignment', 'instance'=>$assignment->id)); } // get existing grade item assignment_grade_item_update($assignment); return true; }
function create_events($events) { global $CFG, $DB, $USER; foreach ($events as $event) { $user = get_complete_user_data('username', $event['username']); $event['userid'] = $user->id; // Let us set some defaults. $event['modulename'] = ''; $event['instance'] = 0; $event['subscriptionid'] = null; $event['uuid'] = ''; $event['format'] = 1; $event['repeat'] = 0; $event['timeduration'] = $event['timeend'] - $event['timestart']; $eventobj = new calendar_event($event); // Let's create the event. $var = $eventobj->create($event); } return count($events); }
/** * This creates new events given as timeopen and closeopen by $feedback. * * @global object * @param object $feedback * @return void */ function feedback_set_events($feedback) { global $DB; // adding the feedback to the eventtable (I have seen this at quiz-module) $DB->delete_records('event', array('modulename'=>'feedback', 'instance'=>$feedback->id)); if (!isset($feedback->coursemodule)) { $cm = get_coursemodule_from_id('feedback', $feedback->id); $feedback->coursemodule = $cm->id; } // the open-event if ($feedback->timeopen > 0) { $event = new stdClass(); $event->name = get_string('start', '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 = 'open'; $event->timestart = $feedback->timeopen; $event->visible = instance_is_visible('feedback', $feedback); if ($feedback->timeclose > 0) { $event->timeduration = ($feedback->timeclose - $feedback->timeopen); } else { $event->timeduration = 0; } calendar_event::create($event); } // the close-event if ($feedback->timeclose > 0) { $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); } }
function booking_update_options($optionvalues) { global $DB, $CFG; require_once "{$CFG->dirroot}/mod/booking/locallib.php"; $bokingUtils = new booking_utils(); $booking = $DB->get_record('booking', array('id' => $optionvalues->bookingid)); $option = new stdClass(); $option->bookingid = $optionvalues->bookingid; $option->text = trim($optionvalues->text); $option->conectedoption = $optionvalues->conectedoption; $option->howmanyusers = $optionvalues->howmanyusers; $option->removeafterminutes = $optionvalues->removeafterminutes; $option->notificationtext = $optionvalues->notificationtext; $option->disablebookingusers = $optionvalues->disablebookingusers; $option->sent = 0; $option->location = trim($optionvalues->location); $option->institution = trim($optionvalues->institution); $option->address = trim($optionvalues->address); $option->daystonotify = $optionvalues->daystonotify; $option->pollurl = $optionvalues->pollurl; $option->pollurlteachers = $optionvalues->pollurlteachers; if ($optionvalues->limitanswers == 0) { $option->limitanswers = 0; $option->maxanswers = 0; $option->maxoverbooking = 0; } else { $option->maxanswers = $optionvalues->maxanswers; $option->maxoverbooking = $optionvalues->maxoverbooking; $option->limitanswers = 1; } if (isset($optionvalues->restrictanswerperiod)) { $option->bookingclosingtime = $optionvalues->bookingclosingtime; } else { $option->bookingclosingtime = 0; } $option->courseid = $optionvalues->courseid; if (isset($optionvalues->startendtimeknown)) { $option->coursestarttime = $optionvalues->coursestarttime; $option->courseendtime = $optionvalues->courseendtime; } else { $option->coursestarttime = 0; $option->courseendtime = 0; } $option->description = $optionvalues->description; $option->limitanswers = $optionvalues->limitanswers; $option->timemodified = time(); if (isset($optionvalues->optionid) && !empty($optionvalues->optionid) && $optionvalues->id != "add") { //existing booking record $option->id = $optionvalues->optionid; if (isset($optionvalues->text) && $optionvalues->text != '') { $event = new stdClass(); $event->id = $DB->get_field('booking_options', 'calendarid', array('id' => $option->id)); $groupid = $DB->get_field('booking_options', 'groupid', array('id' => $option->id)); $coursestarttime = $DB->get_field('booking_options', 'coursestarttime', array('id' => $option->id)); if ($coursestarttime != $optionvalues->coursestarttime) { $option->sent = 0; } else { $option->sent = $DB->get_field('booking_options', 'sent', array('id' => $option->id)); } $option->groupid = $bokingUtils->group($booking, $option); $whereis = ''; if (strlen($option->location) > 0) { $whereis = '<p>' . get_string('location', 'booking') . ': ' . $option->location . '</p>'; } if ($event->id > 0) { // event exist if (isset($optionvalues->addtocalendar)) { $event->name = $option->text; $event->description = $option->description . $whereis; $event->courseid = $option->courseid; if ($option->courseid == 0) { $event->courseid = $booking->course; } $event->groupid = 0; $event->userid = 0; $event->modulename = 'booking'; $event->instance = $option->bookingid; $event->eventtype = 'booking'; $event->timestart = $option->coursestarttime; $event->visible = instance_is_visible('booking', $booking); $event->timeduration = $option->courseendtime - $option->coursestarttime; if ($DB->record_exists("event", array('id' => $event->id))) { $calendarevent = calendar_event::load($event->id); $calendarevent->update($event); $option->calendarid = $event->id; $option->addtocalendar = $optionvalues->addtocalendar; } else { unset($event->id); $tmpEvent = calendar_event::create($event); $option->calendarid = $tmpEvent->id; } } else { // Delete event if exist $event = calendar_event::load($event->id); $event->delete(true); $option->addtocalendar = 0; $option->calendarid = 0; } } else { $option->addtocalendar = 0; $option->calendarid = 0; // Insert into calendar if (isset($optionvalues->addtocalendar)) { $event = new stdClass(); $event->name = $option->text; $event->description = $option->description . $whereis; $event->courseid = $option->courseid; if ($option->courseid == 0) { $event->courseid = $booking->course; } $event->groupid = 0; $event->userid = 0; $event->modulename = 'booking'; $event->instance = $option->bookingid; $event->eventtype = 'booking'; $event->timestart = $option->coursestarttime; $event->visible = instance_is_visible('booking', $booking); $event->timeduration = $option->courseendtime - $option->coursestarttime; $tmpEvent = calendar_event::create($event); $option->calendarid = $tmpEvent->id; $option->addtocalendar = $optionvalues->addtocalendar; } } $DB->update_record("booking_options", $option); return $option->id; } } elseif (isset($optionvalues->text) && $optionvalues->text != '') { $option->addtocalendar = 0; $option->calendarid = 0; // Insert into calendar // We add a new booking_options? $whereis = ''; if (strlen($option->location) > 0) { $whereis = '<p>' . get_string('location', 'booking') . ': ' . $option->location . '</p>'; } if (isset($optionvalues->addtocalendar)) { $event = new stdClass(); $event->name = $option->text; $event->description = $option->description . $whereis; $event->courseid = $option->courseid; if ($option->courseid == 0) { $event->courseid = $booking->course; } $event->groupid = 0; $event->userid = 0; $event->modulename = 'booking'; $event->instance = $option->bookingid; $event->eventtype = 'booking'; $event->timestart = $option->coursestarttime; $event->visible = instance_is_visible('booking', $booking); $event->timeduration = $option->courseendtime; $tmpEvent = calendar_event::create($event); $option->calendarid = $tmpEvent->id; $option->addtocalendar = $optionvalues->addtocalendar; } $option->groupid = $bokingUtils->group($booking, $option); return $DB->insert_record("booking_options", $option); } }