From b2a3cd2d248da7fa173d4ebcb01f518b65396fcc Mon Sep 17 00:00:00 2001 From: moodler Date: Mon, 17 May 2004 17:04:01 +0000 Subject: [PATCH] Quizzes with long periods (longer than 5 days) are now handled in the calendar as two separate events ... one for open and one for close bug 1402 --- mod/quiz/db/mysql.php | 2 +- mod/quiz/db/postgres7.php | 2 +- mod/quiz/lib.php | 133 +++++++++++++++++++++++++++++++------- mod/quiz/version.php | 2 +- 4 files changed, 113 insertions(+), 26 deletions(-) diff --git a/mod/quiz/db/mysql.php b/mod/quiz/db/mysql.php index 0d5ae9bc20..41003a0b32 100644 --- a/mod/quiz/db/mysql.php +++ b/mod/quiz/db/mysql.php @@ -190,7 +190,7 @@ function quiz_upgrade($oldversion) { modify_database("","INSERT INTO prefix_log_display VALUES ('quiz', 'update', 'quiz', 'name');"); } - if ($oldversion < 2004042501) { + if ($oldversion < 2004051700) { include_once("$CFG->dirroot/mod/quiz/lib.php"); quiz_refresh_events(); } diff --git a/mod/quiz/db/postgres7.php b/mod/quiz/db/postgres7.php index 8cc3e248a4..fae9afd505 100644 --- a/mod/quiz/db/postgres7.php +++ b/mod/quiz/db/postgres7.php @@ -145,7 +145,7 @@ function quiz_upgrade($oldversion) { modify_database("","INSERT INTO prefix_log_display VALUES ('quiz', 'update', 'quiz', 'name');"); } - if ($oldversion < 2004042501) { + if ($oldversion < 2004051700) { include_once("$CFG->dirroot/mod/quiz/lib.php"); quiz_refresh_events(); } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index a4376aa767..d68144a349 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -40,7 +40,7 @@ define("QUIZ_PICTURE_MAX_WIDTH", "600"); // Not currently implemented define("QUIZ_MAX_NUMBER_ANSWERS", "10"); -define("QUIZ_MAX_EVENT_LENGTH", "43200"); // 5 days maximum +define("QUIZ_MAX_EVENT_LENGTH", "432000"); // 5 days maximum /// FUNCTIONS /////////////////////////////////////////////////////////////////// @@ -88,12 +88,24 @@ function quiz_add_instance($quiz) { $event->userid = 0; $event->modulename = 'quiz'; $event->instance = $quiz->id; - $event->eventtype = 'start'; + $event->eventtype = 'open'; $event->timestart = $quiz->timeopen; $event->timeduration = ($quiz->timeclose - $quiz->timeopen); - if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Ignore long durations - $event->timeduration = 1; + + if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Long durations create two events + $event2 = $event; + + $event->name .= ' ('.get_string('quizopens', 'quiz').')'; + $event->timeduration = 0; + + $event2->timestart = $quiz->timeclose; + $event2->eventtype = 'close'; + $event2->timeduration = 0; + $event2->name .= ' ('.get_string('quizcloses', 'quiz').')'; + + add_event($event2); } + add_event($event); return $quiz->id; @@ -149,16 +161,53 @@ function quiz_update_instance($quiz) { } } - $event = NULL; + if ($events = get_records_select('event', "modulename = 'quiz' AND instance = '$quiz->id' ORDER BY timestart")) { - if ($event->id = get_field('event', 'id', 'modulename', 'quiz', 'instance', $quiz->id)) { + $event = array_shift($events); + if (!empty($events)) { + $event2old = array_shift($events); + if (!empty($events)) { + foreach ($events as $badevent) { + delete_records('event', 'id', $badevent->id); + } + } + } else { + $event2old = NULL; + } $event->name = $quiz->name; $event->description = $quiz->intro; + $event->courseid = $quiz->course; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = 'quiz'; + $event->instance = $quiz->id; + $event->visible = instance_is_visible('quiz', $quiz->id); $event->timestart = $quiz->timeopen; + $event->eventtype = 'open'; $event->timeduration = ($quiz->timeclose - $quiz->timeopen); - if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Ignore long durations - $event->timeduration = 1; + + if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events + + $event2 = $event; + + $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')'; + $event->timeduration = 0; + + $event2->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')'; + $event2->timestart = $quiz->timeclose; + $event2->eventtype = 'close'; + $event2->timeduration = 0; + + if (empty($event2old->id)) { + unset($event2->id); + add_event($event2); + } else { + $event2->id = $event2old->id; + update_event($event2); + } + } else if (!empty($event2->id)) { + delete_event($event2->id); } update_event($event); @@ -203,6 +252,10 @@ function quiz_delete_instance($id) { $result = false; } + if (! delete_records('event', 'modulename', 'quiz', 'instance', $quiz->id)) { + $result = false; + } + return $result; } @@ -300,8 +353,8 @@ function quiz_get_participants($quizid) { function quiz_refresh_events($courseid = 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 assignment event in the site is checked, else -// only assignment events belonging to the course specified are checked. +// If courseid = 0, then every quiz event in the site is checked, else +// only quiz events belonging to the course specified are checked. // This function is used, in its new format, by restore_refresh_events() if ($courseid == 0) { @@ -317,28 +370,62 @@ function quiz_refresh_events($courseid = 0) { foreach ($quizzes as $quiz) { $event = NULL; + $event2 = NULL; + $event2old = NULL; + + if ($events = get_records_select('event', "modulename = 'quiz' AND instance = '$quiz->id' ORDER BY timestart")) { + $event = array_shift($events); + if (!empty($events)) { + $event2old = array_shift($events); + if (!empty($events)) { + foreach ($events as $badevent) { + delete_records('event', 'id', $badevent->id); + } + } + } + } + $event->name = addslashes($quiz->name); $event->description = addslashes($quiz->intro); + $event->courseid = $quiz->course; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = 'quiz'; + $event->instance = $quiz->id; + $event->visible = instance_is_visible('quiz', $quiz->id); $event->timestart = $quiz->timeopen; + $event->eventtype = 'open'; $event->timeduration = ($quiz->timeclose - $quiz->timeopen); - if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Ignore long durations - $event->timeduration = 1; - } - if ($event->id = get_field('event', 'id', 'modulename', 'quiz', 'instance', $quiz->id)) { - update_event($event); + if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events - } else { - $event->courseid = $quiz->course; - $event->groupid = 0; - $event->userid = 0; - $event->modulename = 'quiz'; - $event->instance = $quiz->id; - $event->eventtype = 'start'; - $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $quiz->id); + $event2 = $event; + $event->name = addslashes($quiz->name).' ('.get_string('quizopens', 'quiz').')'; + $event->timeduration = 0; + + $event2->name = addslashes($quiz->name).' ('.get_string('quizcloses', 'quiz').')'; + $event2->timestart = $quiz->timeclose; + $event2->eventtype = 'close'; + $event2->timeduration = 0; + + if (empty($event2old->id)) { + unset($event2->id); + add_event($event2); + } else { + $event2->id = $event2old->id; + update_event($event2); + } + } else if (!empty($event2->id)) { + delete_event($event2->id); + } + + if (empty($event->id)) { add_event($event); + } else { + update_event($event); } + } return true; } diff --git a/mod/quiz/version.php b/mod/quiz/version.php index c206d5d7d2..85a13d3074 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -5,7 +5,7 @@ // This fragment is called by moodle_needs_upgrading() and /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2004042501; // The (date) version of this module +$module->version = 2004051700; // The (date) version of this module $module->requires = 2004013101; // Requires this Moodle version $module->cron = 0; // How often should cron check this module (seconds)? -- 2.39.5