mirror of
https://github.com/moodle/moodle.git
synced 2025-03-10 02:40:10 +01:00
162 lines
4.8 KiB
PHP
162 lines
4.8 KiB
PHP
<?PHP // $Id$
|
|
|
|
// This page prints a particular instance of quiz
|
|
|
|
require("../../config.php");
|
|
require("lib.php");
|
|
|
|
optional_variable($id); // Course Module ID, or
|
|
optional_variable($q); // quiz ID
|
|
|
|
if ($id) {
|
|
if (! $cm = get_record("course_modules", "id", $id)) {
|
|
error("Course Module ID was incorrect");
|
|
}
|
|
|
|
if (! $course = get_record("course", "id", $cm->course)) {
|
|
error("Course is misconfigured");
|
|
}
|
|
|
|
if (! $quiz = get_record("quiz", "id", $cm->instance)) {
|
|
error("Course module is incorrect");
|
|
}
|
|
|
|
} else {
|
|
if (! $quiz = get_record("quiz", "id", $q)) {
|
|
error("Course module is incorrect");
|
|
}
|
|
if (! $course = get_record("course", "id", $quiz->course)) {
|
|
error("Course is misconfigured");
|
|
}
|
|
if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
|
|
error("Course Module ID was incorrect");
|
|
}
|
|
}
|
|
|
|
require_login($course->id);
|
|
|
|
|
|
|
|
// Print the page header
|
|
|
|
if ($course->category) {
|
|
$navigation = "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->";
|
|
}
|
|
|
|
$strquizzes = get_string("modulenameplural", "quiz");
|
|
$strquiz = get_string("modulename", "quiz");
|
|
|
|
print_header("$course->shortname: $quiz->name", "$course->fullname",
|
|
"$navigation <A HREF=index.php?id=$course->id>$strquizzes</A> -> $quiz->name",
|
|
"", "", true);
|
|
|
|
/// Check some quiz parameters
|
|
|
|
if ($attempts = quiz_get_user_attempts($quiz->id, $USER->id)) {
|
|
$numattempts = count($attempts) + 1;
|
|
} else {
|
|
$numattempts = 1;
|
|
}
|
|
|
|
if ($quiz->attempts) {
|
|
if ($numattempts > $quiz->attempts) {
|
|
error("Sorry, you've had $quiz->attempts attempts already.", "view.php?id=$cm->id");
|
|
}
|
|
}
|
|
|
|
if ($course->format == "weeks" and $quiz->days) {
|
|
$timenow = time();
|
|
$timestart = $course->startdate + (($cw->section - 1) * 608400);
|
|
$timefinish = $timestart + (3600 * 24 * $quiz->days);
|
|
$available = ($timestart < $timenow and $timenow < $timefinish);
|
|
} else {
|
|
$available = true;
|
|
}
|
|
|
|
/// Check to see if they are submitting answers
|
|
if (match_referer() && isset($HTTP_POST_VARS)) {
|
|
add_to_log($course->id, "quiz", "submit", "attempt.php?id=$cm->id", "$quiz->id");
|
|
|
|
$rawanswers = $HTTP_POST_VARS;
|
|
unset($rawanswers["q"]); // quiz id
|
|
if (! count($rawanswers)) {
|
|
print_heading(get_string("noanswers", "quiz"));
|
|
print_continue("attempt.php?q=$quiz->id");
|
|
exit;
|
|
}
|
|
|
|
if (!$questions = get_records_list("quiz_questions", "id", $quiz->questions)) {
|
|
error("No questions found!");
|
|
}
|
|
|
|
foreach ($rawanswers as $key => $value) { // Parse input for question -> answers
|
|
if (substr($key, 0, 1) == "q") {
|
|
$key = substr($key,1);
|
|
if (!isset($questions[$key])) {
|
|
if (substr_count($key, "a")) { // checkbox style multiple answers
|
|
$check = explode("a", $key);
|
|
$key = $check[0];
|
|
$value = $check[1];
|
|
} else {
|
|
error("Answer received for non-existent question ($key)!");
|
|
}
|
|
}
|
|
$questions[$key]->answer[] = $value; // Store answers in array
|
|
}
|
|
}
|
|
|
|
if (!$result = quiz_grade_attempt_results($quiz, $questions)) {
|
|
error("Could not grade your quiz attempt!");
|
|
}
|
|
|
|
if (! $attempt = quiz_save_attempt($quiz, $questions, $result, $numattempts)) {
|
|
error("Sorry! Could not save your attempt!");
|
|
}
|
|
|
|
if (! quiz_save_best_grade($quiz, $USER)) {
|
|
error("Sorry! Could not calculate your best grade!");
|
|
}
|
|
|
|
print_heading(get_string("grade", "quiz").": $result->grade/$quiz->grade ($result->sumgrades / $quiz->sumgrades = $result->percentage %)");
|
|
|
|
print_continue("view.php?id=$cm->id");
|
|
|
|
if ($quiz->correctanswers) {
|
|
quiz_print_quiz_questions($quiz, $result);
|
|
print_continue("view.php?id=$cm->id");
|
|
}
|
|
|
|
exit;
|
|
}
|
|
|
|
add_to_log($course->id, "quiz", "attempt", "attempt.php?id=$cm->id", "$quiz->id");
|
|
|
|
/// Print the quiz page
|
|
|
|
/// First print the headings and so on
|
|
|
|
print_heading($quiz->name);
|
|
|
|
if (!$available) {
|
|
error("Sorry, this quiz is not available", "view.php?id=$cm->id");
|
|
}
|
|
|
|
print_heading(get_string("attempt", "quiz", $numattempts));
|
|
|
|
print_simple_box($quiz->intro, "CENTER");
|
|
|
|
|
|
/// Print all the questions
|
|
|
|
echo "<BR>";
|
|
|
|
if (! quiz_print_quiz_questions($quiz)) {
|
|
print_continue("view.php?id=$cm->id");
|
|
}
|
|
|
|
|
|
/// Finish the page
|
|
print_footer($course);
|
|
|
|
?>
|