From 3a506ca2999ea0d4c49dfbc8ec0634e62181bff3 Mon Sep 17 00:00:00 2001 From: moodler Date: Sun, 6 Oct 2002 03:23:16 +0000 Subject: [PATCH] Added some more functions --- mod/quiz/lib.php | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 79d5a28b08..94d0f5aefd 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -100,5 +100,62 @@ function quiz_cron () { // Any other quiz functions go here. Each of them must have a name that // starts with quiz_ +function quiz_print_question($number, $questionid) { + echo "

$number

"; + echo ""; + echo "
"; +} + + +function quiz_get_user_attempts($quizid, $userid) { + return get_records_sql("SELECT * FROM quiz_attempts WHERE quiz = '$quizid' and user = '$userid' ORDER by attempt ASC"); +} + +function quiz_get_grade($quizid, $userid) { + if (!$grade = get_record_sql("SELECT * FROM quiz_grades WHERE quiz = '$quizid' and user = '$userid'")) { + return 0; + } + + return $grade->grade; +} + +function quiz_calculate_best_grade($quiz, $attempts) { +// Calculate the best grade for a quiz given a number of attempts by a particular user. + + switch ($quiz->grademethod) { + case "1": // Use highest score + $max = 0; + foreach ($attempts as $attempt) { + if ($attempt->grade > $max) { + $max = $attempt->grade; + } + } + return $max; + + case "2": // Use average score + $sum = 0; + $count = 0; + foreach ($attempts as $attempt) { + $sum += $attempt->grade; + $count++; + } + return (float)$sum/$count; + + case "3": // Use first attempt + foreach ($attempts as $attempt) { + return $attempt->attempt; + } + break; + + default: + case "4": // Use last attempt + foreach ($attempts as $attempt) { + $final = $attempt->attempt; + } + return $final; + } +} ?> -- 2.39.5