]> git.mjollnir.org Git - moodle.git/commitdiff
Quizzes with long periods (longer than 5 days) are now handled
authormoodler <moodler>
Mon, 17 May 2004 17:04:01 +0000 (17:04 +0000)
committermoodler <moodler>
Mon, 17 May 2004 17:04:01 +0000 (17:04 +0000)
in the calendar as two separate events ... one for open and one for close

bug 1402

mod/quiz/db/mysql.php
mod/quiz/db/postgres7.php
mod/quiz/lib.php
mod/quiz/version.php

index 0d5ae9bc204bdd8ddaed89799a492dfe9cc8fb4f..41003a0b32ba5df64b71872eb8d82ea17ff520c5 100644 (file)
@@ -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();
     }
index 8cc3e248a43396f3351f2acd537ea24994b1812a..fae9afd505a26a3f8c20d0354249d18d3a6b829b 100644 (file)
@@ -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();
     }
index a4376aa767ab4e68f69b87bb00c015d82efc2e26..d68144a349db41f16197a53513f4c3c4f46ed753 100644 (file)
@@ -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;
 }
index c206d5d7d247a934f7a768f5a6bfa392c007a404..85a13d3074b1fb334715278f5297211bc72b4aa3 100644 (file)
@@ -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)?