]> git.mjollnir.org Git - moodle.git/commitdiff
Quiz attempts are now timed. This is done by starting the quiz_attempts
authormoodler <moodler>
Fri, 18 Oct 2002 16:05:21 +0000 (16:05 +0000)
committermoodler <moodler>
Fri, 18 Oct 2002 16:05:21 +0000 (16:05 +0000)
record upon showing the quiz, and completing it upon submit.

Two new fields in quiz_attempts... should be upgrade friendly.
(Old attempts will show up as being of duration zero)

Also some small fixes here and there.

lang/en/quiz.php
mod/quiz/attempt.php
mod/quiz/db/mysql.php
mod/quiz/db/mysql.sql
mod/quiz/index.php
mod/quiz/lib.php
mod/quiz/question.php
mod/quiz/version.php
mod/quiz/view.php

index b37adfe990eb8449dca6105aa70c1593ce66c0ec..d0186a8ca8da761c88fa87b0ee47f6e80e0c821c 100644 (file)
@@ -17,7 +17,7 @@ $string['attemptlast'] = "Last attempt";
 $string['attempts'] = "Attempts";
 $string['attemptsallowed'] = "Attempts allowed";
 $string['attemptsunlimited'] = "Unlimited attempts";
-$string['attemptsunlimited'] = "Unlimited attempts";
+$string['bestgrade'] = "Best grade";
 $string['categories'] = "Categories";
 $string['category'] = "Category";
 $string['casesensitive'] = "Case sensitivity";
@@ -72,6 +72,8 @@ $string['show'] = "Show";
 $string['showfeedback'] = "After answering, show feedback?";
 $string['showcorrectanswer'] = "After answering, show correct answers?";
 $string['time'] = "Time";
+$string['timetaken'] = "Time taken";
+$string['timecompleted'] = "Completed";
 $string['true'] = "True";
 $string['truefalse'] = "True/False";
 $string['viewallanswers'] = "View \$a completed quizzes";
index e4cfd4e8b55474113f8892f1bcc65e782adf393d..9b6c2a5d4f420e65b2d0613e5ff3030ea496971b 100644 (file)
 
 /// Print the quiz page
 
+/// Actually seeing the questions marks the start of an attempt
+    if (!$unfinished = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
+        if (! quiz_start_attempt($quiz->id, $USER->id, $numattempts)) {
+            error("Sorry! Could not start the quiz (could not save starting time)");
+        }
+    }
+
 /// First print the headings and so on
 
     print_heading($quiz->name);
index 18a5176af35f98ceb0521e1705fcf43a9d30643e..ad60e02b601f2d808e9343444a656bb19eaadd50 100644 (file)
@@ -6,10 +6,12 @@ function quiz_upgrade($oldversion) {
 
     global $CFG;
 
-    if ($oldversion < 2002110100) {
-
-        echo "The quiz module is under heavy development ... at this stage you should delete all existing quiz tables, as well as the quiz entry in the 'modules' table, then come back here to rebuild them.";
-
+    if ($oldversion < 2002101800) {
+        execute_sql(" ALTER TABLE `quiz_attempts` ".
+                    " ADD `timestart` INT(10) UNSIGNED DEFAULT '0' NOT NULL AFTER `sumgrades` , ".
+                    " ADD `timefinish` INT(10) UNSIGNED DEFAULT '0' NOT NULL AFTER `timestart` ");
+        execute_sql(" UPDATE `quiz_attempts` SET timestart = timemodified ");
+        execute_sql(" UPDATE `quiz_attempts` SET timefinish = timemodified ");
     }
 
     return true;
index c5f46617ab23946279fabf2e5d0f036461118cc8..6ffefd52647f63371ef3016f0ed19b5cf0337aed 100644 (file)
@@ -58,6 +58,8 @@ CREATE TABLE `quiz_attempts` (
   `user` int(10) unsigned NOT NULL default '0',
   `attempt` smallint(6) NOT NULL default '0',
   `sumgrades` varchar(10) NOT NULL default '0.0',
+  `timestart` int(10) unsigned NOT NULL default '0',
+  `timefinish` int(10) unsigned NOT NULL default '0',
   `timemodified` int(10) unsigned NOT NULL default '0',
   PRIMARY KEY  (`id`)
 ) TYPE=MyISAM COMMENT='Stores various attempts on a quiz';
index febce54f59e3ac64537358caa49e7148e9555a13..1c9dc705f109689ea6bd40f37b89353fc2390c5f 100644 (file)
     $strname  = get_string("name");
     $strweek  = get_string("week");
     $strtopic  = get_string("topic");
-    $strgrades  = get_string("grades");
+    $strbestgrade  = get_string("bestgrade", "quiz");
 
     if ($course->format == "weeks") {
-        $table->head  = array ($strweek, $strname, $strgrades);
-        $table->align = array ("CENTER", "LEFT");
+        $table->head  = array ($strweek, $strname, $strbestgrade);
+        $table->align = array ("CENTER", "LEFT", "RIGHT");
         $table->width = array (10, "*", 10);
     } else if ($course->format == "topics") {
-        $table->head  = array ($strtopic, $strname, $strgrades);
-        $table->align = array ("CENTER", "LEFT", "LEFT", "LEFT");
+        $table->head  = array ($strtopic, $strname, $strbestgrade);
+        $table->align = array ("CENTER", "LEFT", "RIGHT");
         $table->width = array (10, "*", 10);
     } else {
-        $table->head  = array ($strname, $strgrades);
-        $table->align = array ("LEFT", "LEFT");
+        $table->head  = array ($strname, $strbestgrade);
+        $table->align = array ("LEFT", "RIGHT");
         $table->width = array ("*", 10);
     }
 
     foreach ($quizzes as $quiz) {
         $link = "<A HREF=\"view.php?id=$quiz->coursemodule\">$quiz->name</A>";
 
+        $bestgrade = quiz_get_best_grade($quiz->id, $USER->id);
+
         if ($course->format == "weeks" or $course->format == "topics") {
-            $table->data[] = array ($quiz->section, $link);
+            $table->data[] = array ($quiz->section, $link, "$bestgrade / $quiz->grade");
         } else {
-            $table->data[] = array ($link);
+            $table->data[] = array ($link, "$bestgrade / $quiz->grade");
         }
     }
 
index a6e25e8cff8a8f6cad672eff8f7e4bf931899a9c..b6f65b058ff6e805fdfd470cbdaf65819648e3e7 100644 (file)
@@ -622,9 +622,28 @@ function quiz_print_cat_question_list($categoryid) {
 }
 
 
+function quiz_start_attempt($quizid, $userid, $numattempt) {
+    $attempt->quiz = $quizid;
+    $attempt->user = $userid;
+    $attempt->attempt = $numattempt;
+    $attempt->timestart = time();
+    $attempt->timefinish = 0; 
+    $attempt->timemodified = time();
+
+    return insert_record("quiz_attempts", $attempt);
+}
+
+function quiz_get_user_attempt_unfinished($quizid, $userid) {
+// Returns an object containing an unfinished attempt (if there is one)
+    return get_record_sql("SELECT * FROM quiz_attempts 
+                           WHERE quiz = '$quizid' and user = '$userid' AND timefinish = 0");
+}
+
 function quiz_get_user_attempts($quizid, $userid) {
 // Returns a list of all attempts by a user
-    return get_records_sql("SELECT * FROM quiz_attempts WHERE quiz = '$quizid' and user = '$userid' ORDER by attempt ASC");
+    return get_records_sql("SELECT * FROM quiz_attempts 
+                            WHERE quiz = '$quizid' and user = '$userid' AND timefinish > 0 
+                            ORDER by attempt ASC");
 }
 
 
@@ -769,15 +788,25 @@ function quiz_save_attempt($quiz, $questions, $result, $attemptnum) {
 
     global $USER;
 
-    // First let's save the attempt record itself
+    // First find the attempt in the database (start of attempt)
+
+    if (!$attempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
+        notify("Trying to save an attempt that was not started!");
+        return false;
+    }
+
+    if ($attempt->attempt != $attemptnum) {  // Double check.
+        notify("Number of this attempt is different to the unfinished one!");
+        return false;
+    }
+
+    // Now let's complete this record and save it
 
-    $attempt->quiz = $quiz->id;
-    $attempt->user = $USER->id;
-    $attempt->attempt = $attemptnum;
     $attempt->sumgrades = $result->sumgrades;
+    $attempt->timefinish = time();
     $attempt->timemodified = time();
 
-    if (!$attempt->id = insert_record("quiz_attempts", $attempt)) {
+    if (! update_record("quiz_attempts", $attempt)) {
         notify("Error while saving attempt");
         return false;
     }
index 79f7c05c95869a880099de4e62f2027bd33b53b6..c1e855fc91a3776e8bc50f8251a7d316e19cc0e3 100644 (file)
@@ -67,7 +67,6 @@
                 error("Could not update question!");
             }
         } else {         // Question is a new one
-            $db->debug =true;
             if (!$question->id = insert_record("quiz_questions", $question)) {
                 error("Could not insert new question!");
             }
index 50c7c0af4804a537a55d605c6152d2a24dda0c45..3f0e8cf7311b95c3d578cd5657cb50e1c16bd5b8 100644 (file)
@@ -5,7 +5,7 @@
 //  This fragment is called by moodle_needs_upgrading() and /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2002101300;   // The (date) version of this module
+$module->version  = 2002101800;   // The (date) version of this module
 $module->cron     = 0;            // How often should cron check this module (seconds)?
 
 ?>
index eca66b1692aa556e46f9070834f57782eb2b0840..6dc2a9428ddc91aeb3a1b99a685f63b29c4b2649 100644 (file)
         echo "<BR>";
     }
 
-    $strattempt = get_string("attempt", "quiz");
-    $strtime = get_string("time", "quiz");
-    $strgrade = get_string("grade");
+    $strattempt       = get_string("attempt", "quiz");
+    $strtimetaken     = get_string("timetaken", "quiz");
+    $strtimecompleted = get_string("timecompleted", "quiz");
+    $strgrade         = get_string("grade");
+    $strbestgrade     = get_string("bestgrade", "quiz");
 
     if ($numattempts) { 
-        $table->head = array($strattempt, $strtime, "$strgrade / $quiz->grade");
-        $table->align = array("CENTER", "LEFT", "RIGHT");
+        $table->head = array($strattempt, $strtimetaken, $strtimecompleted, "$strgrade / $quiz->grade");
+        $table->align = array("CENTER", "CENTER", "LEFT", "RIGHT");
+        $table->width = array("", "", "", "");
         foreach ($attempts as $attempt) {
             $table->data[] = array( $attempt->attempt, 
-                                    userdate($attempt->timemodified), 
+                                    format_time($attempt->timefinish - $attempt->timestart),
+                                    userdate($attempt->timefinish), 
                                     format_float(($attempt->sumgrades/$quiz->sumgrades)*$quiz->grade) );
         }
         print_table($table);
             if ($available) {
                 $options["id"] = $cm->id;
                 if ($numattempts) {
-                    print_heading("Your best grade so far is $mygrade / $quiz->grade.");
+                    print_heading("$strbestgrade: $mygrade / $quiz->grade.");
                 }
                 echo "<BR>";
                 echo "<DIV align=CENTER>";