From 164c865fc7f4fcd1f5d79f9dfb55829a66bb224c Mon Sep 17 00:00:00 2001 From: gbateson Date: Sun, 1 Oct 2006 06:22:12 +0000 Subject: [PATCH] calendar events are now added/deleted when hotpots are added/updated/deleted --- mod/hotpot/lib.php | 82 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/mod/hotpot/lib.php b/mod/hotpot/lib.php index 1b34df810c1..51e4d9248e4 100644 --- a/mod/hotpot/lib.php +++ b/mod/hotpot/lib.php @@ -161,6 +161,12 @@ define("HOTPOT_DISPLAYNEXT_QUIZ", "0"); define("HOTPOT_DISPLAYNEXT_COURSE", "1"); define("HOTPOT_DISPLAYNEXT_INDEX", "2"); +/** + * If start and end date for the quiz are more than this many seconds apart + * they will be represented by two separate events in the calendar + */ +define("HOTPOT_MAX_EVENT_LENGTH", "432000"); // 5 days maximum + ////////////////////////////////// /// CORE FUNCTIONS @@ -186,7 +192,9 @@ define("HOTPOT_DISPLAYNEXT_INDEX", "2"); function hotpot_add_instance(&$hp) { if (hotpot_set_form_values($hp)) { - $result = insert_record("hotpot", $hp); + if ($result = insert_record('hotpot', $hp)) { + hotpot_update_events($hp); + } } else { $result= false; } @@ -196,12 +204,66 @@ function hotpot_add_instance(&$hp) { function hotpot_update_instance(&$hp) { if (hotpot_set_form_values($hp)) { $hp->id = $hp->instance; - $result = update_record("hotpot", $hp); + if ($result = update_record('hotpot', $hp)) { + hotpot_update_events($hp); + } } else { $result= false; } return $result; } +function hotpot_update_events($hotpot) { + + // remove any previous calendar events for this hotpot + delete_records('event', 'modulename', 'hotpot', 'instance', $hotpot->id); + + $event = new stdClass(); + $event->description = $hotpot->summary; + $event->courseid = $hotpot->course; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = 'hotpot'; + $event->instance = $hotpot->id; + $event->timestart = $hotpot->timeopen; + if ($cm = get_coursemodule_from_id('hotpot', $hotpot->id)) { + $event->visible = hotpot_is_visible($cm); + } else { + $event->visible = 1; + } + + if ($hotpot->timeclose && $hotpot->timeopen) { + // we have both a start and an end date + $event->eventtype = 'open'; + $event->timeduration = ($hotpot->timeclose - $hotpot->timeopen); + + if ($event->timeduration > HOTPOT_MAX_EVENT_LENGTH) { /// Long durations create two events + + $event->name = $hotpot->name.' ('.get_string('hotpotopens', 'hotpot').')'; + $event->timeduration = 0; + add_event($event); + + $event->timestart = $hotpot->timeclose; + $event->eventtype = 'close'; + $event->name = $hotpot->name.' ('.get_string('hotpotcloses', 'hotpot').')'; + unset($event->id); + add_event($event); + } else { // single event with duration + $event->name = $hotpot->name; + add_event($event); + } + } elseif ($hotpot->timeopen) { // only an open date + $event->name = $hotpot->name.' ('.get_string('hotpotopens', 'hotpot').')'; + $event->eventtype = 'open'; + $event->timeduration = 0; + add_event($event); + } elseif ($hotpot->timeclose) { // only a closing date + $event->name = $hotpot->name.' ('.get_string('hotpotcloses', 'hotpot').')'; + $event->timestart = $hotpot->timeclose; + $event->eventtype = 'close'; + $event->timeduration = 0; + add_event($event); + } +} function hotpot_set_form_values(&$hp) { $ok = true; @@ -796,16 +858,18 @@ function hotpot_delete_instance($id) { /// and any data that depends on it. $result = false; - if (delete_records("hotpot", "id", "$id")) { + if (delete_records('hotpot', 'id', "$id")) { $result = true; - delete_records("hotpot_questions", "hotpot", "$id"); - if ($attempts = get_records_select("hotpot_attempts", "hotpot='$id'")) { + delete_records('hotpot_questions', 'hotpot', "$id"); + if ($attempts = get_records_select('hotpot_attempts', "hotpot='$id'")) { $ids = implode(',', array_keys($attempts)); - delete_records_select("hotpot_attempts", "id IN ($ids)"); - delete_records_select("hotpot_details", "attempt IN ($ids)"); - delete_records_select("hotpot_responses", "attempt IN ($ids)"); + delete_records_select('hotpot_attempts', "id IN ($ids)"); + delete_records_select('hotpot_details', "attempt IN ($ids)"); + delete_records_select('hotpot_responses', "attempt IN ($ids)"); } - } + // remove calendar events for this hotpot + delete_records('event', 'modulename', 'hotpot', 'instance', $id); + } return $result; } function hotpot_delete_and_notify($table, $select, $strtable) {