From: moodler Date: Fri, 18 Oct 2002 16:05:21 +0000 (+0000) Subject: Quiz attempts are now timed. This is done by starting the quiz_attempts X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=958aafe2b419978eb03d4ea622c93224d28602c7;p=moodle.git Quiz attempts are now timed. This is done by starting the quiz_attempts 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. --- diff --git a/lang/en/quiz.php b/lang/en/quiz.php index b37adfe990..d0186a8ca8 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -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"; diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index e4cfd4e8b5..9b6c2a5d4f 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -144,6 +144,14 @@ /// 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); diff --git a/mod/quiz/db/mysql.php b/mod/quiz/db/mysql.php index 18a5176af3..ad60e02b60 100644 --- a/mod/quiz/db/mysql.php +++ b/mod/quiz/db/mysql.php @@ -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; diff --git a/mod/quiz/db/mysql.sql b/mod/quiz/db/mysql.sql index c5f46617ab..6ffefd5264 100644 --- a/mod/quiz/db/mysql.sql +++ b/mod/quiz/db/mysql.sql @@ -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'; diff --git a/mod/quiz/index.php b/mod/quiz/index.php index febce54f59..1c9dc705f1 100644 --- a/mod/quiz/index.php +++ b/mod/quiz/index.php @@ -40,29 +40,31 @@ $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 = "coursemodule\">$quiz->name"; + $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"); } } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index a6e25e8cff..b6f65b058f 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -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; } diff --git a/mod/quiz/question.php b/mod/quiz/question.php index 79f7c05c95..c1e855fc91 100644 --- a/mod/quiz/question.php +++ b/mod/quiz/question.php @@ -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!"); } diff --git a/mod/quiz/version.php b/mod/quiz/version.php index 50c7c0af48..3f0e8cf731 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 = 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)? ?> diff --git a/mod/quiz/view.php b/mod/quiz/view.php index eca66b1692..6dc2a9428d 100644 --- a/mod/quiz/view.php +++ b/mod/quiz/view.php @@ -89,16 +89,20 @@ echo "
"; } - $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); @@ -113,7 +117,7 @@ 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 "
"; echo "
";