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.
$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";
$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";
/// 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);
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;
`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';
$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");
}
}
}
+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");
}
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;
}
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!");
}
// 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)?
?>
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>";