From 29d5d0b40c31f1b589af9c91598676dae9b6444e Mon Sep 17 00:00:00 2001 From: moodler Date: Thu, 24 Jul 2003 05:18:00 +0000 Subject: [PATCH] New modular plugin structure for quiz reports. The code for reviewing an existing attempt is now separate in review.php and now has a log entry of it's own. The overview and regrade reports are now in separate subdirectories under the "report" directory. Each has a primary "report.php" file which implements the report as a class. These existing reports are very simple, but now more complex ones can easily be written. (I am about to do one). --- lang/en/quiz.php | 3 + mod/quiz/db/mysql.php | 4 + mod/quiz/db/mysql.sql | 1 + mod/quiz/db/postgres7.php | 5 + mod/quiz/db/postgres7.sql | 1 + mod/quiz/lib.php | 19 ++- mod/quiz/report.php | 198 ++++------------------------ mod/quiz/report/default.php | 26 ++++ mod/quiz/report/overview/report.php | 42 ++++++ mod/quiz/report/regrade/report.php | 69 ++++++++++ mod/quiz/review.php | 130 ++++++++++++++++++ mod/quiz/version.php | 2 +- mod/quiz/view.php | 14 +- 13 files changed, 329 insertions(+), 185 deletions(-) create mode 100644 mod/quiz/report/default.php create mode 100644 mod/quiz/report/overview/report.php create mode 100644 mod/quiz/report/regrade/report.php create mode 100644 mod/quiz/review.php diff --git a/lang/en/quiz.php b/lang/en/quiz.php index d51d7f45d1..ac5990fd59 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -121,6 +121,9 @@ $string['regradecomplete'] = "All attempts have been regraded"; $string['regradecount'] = "\$a->changed out of \$a->attempt grades were changed"; $string['rename'] = "Rename"; $string['report'] = "Reports"; +$string['reportoverview'] = "Overview"; +$string['reportregrade'] = "Regrade attempts"; +$string['review'] = "Review"; $string['save'] = "Save"; $string['savegrades'] = "Save grades"; $string['savemyanswers'] = "Save my answers"; diff --git a/mod/quiz/db/mysql.php b/mod/quiz/db/mysql.php index e641ca63a1..f75ca85afe 100644 --- a/mod/quiz/db/mysql.php +++ b/mod/quiz/db/mysql.php @@ -105,6 +105,10 @@ function quiz_upgrade($oldversion) { ) TYPE=MyISAM COMMENT='Options for numerical questions'; "); } + if ($oldversion < 2003072400) { + execute_sql(" INSERT INTO {$CFG->prefix}log_display VALUES ('quiz', 'review', 'quiz', 'name') "); + } + return true; } diff --git a/mod/quiz/db/mysql.sql b/mod/quiz/db/mysql.sql index f20ecd8cdf..c35d63fcfb 100644 --- a/mod/quiz/db/mysql.sql +++ b/mod/quiz/db/mysql.sql @@ -249,4 +249,5 @@ INSERT INTO prefix_log_display VALUES ('quiz', 'view', 'quiz', 'name'); INSERT INTO prefix_log_display VALUES ('quiz', 'report', 'quiz', 'name'); INSERT INTO prefix_log_display VALUES ('quiz', 'attempt', 'quiz', 'name'); INSERT INTO prefix_log_display VALUES ('quiz', 'submit', 'quiz', 'name'); +INSERT INTO prefix_log_display VALUES ('quiz', 'review', 'quiz', 'name'); diff --git a/mod/quiz/db/postgres7.php b/mod/quiz/db/postgres7.php index c222b6bc78..062bb66517 100644 --- a/mod/quiz/db/postgres7.php +++ b/mod/quiz/db/postgres7.php @@ -69,6 +69,11 @@ function quiz_upgrade($oldversion) { ); "); modify_database ("", "CREATE INDEX prefix_quiz_numerical_answer_idx ON prefix_quiz_numerical (answer);"); } + + if ($oldversion < 2003072400) { + execute_sql(" INSERT INTO {$CFG->prefix}log_display VALUES ('quiz', 'review', 'quiz', 'name') "); + } + return true; } diff --git a/mod/quiz/db/postgres7.sql b/mod/quiz/db/postgres7.sql index 003eab5b99..c9e8d8c7d1 100644 --- a/mod/quiz/db/postgres7.sql +++ b/mod/quiz/db/postgres7.sql @@ -223,4 +223,5 @@ INSERT INTO prefix_log_display VALUES ('quiz', 'view', 'quiz', 'name'); INSERT INTO prefix_log_display VALUES ('quiz', 'report', 'quiz', 'name'); INSERT INTO prefix_log_display VALUES ('quiz', 'attempt', 'quiz', 'name'); INSERT INTO prefix_log_display VALUES ('quiz', 'submit', 'quiz', 'name'); +INSERT INTO prefix_log_display VALUES ('quiz', 'review', 'quiz', 'name'); diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 322a21332c..606e566871 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -1234,9 +1234,9 @@ function quiz_get_user_attempts_string($quiz, $attempts, $bestgrade) { foreach ($attempts as $attempt) { $attemptgrade = format_float(($attempt->sumgrades / $quiz->sumgrades) * $quiz->grade); if ($attemptgrade == $bestgrade) { - $userattempts[] = "id&attempt=$attempt->id\">$attemptgrade"; + $userattempts[] = "id&attempt=$attempt->id\">$attemptgrade"; } else { - $userattempts[] = "id&attempt=$attempt->id\">$attemptgrade"; + $userattempts[] = "id&attempt=$attempt->id\">$attemptgrade"; } } return implode(",", $userattempts); @@ -1987,6 +1987,21 @@ function quiz_save_question_options($question) { } +function quiz_remove_unwanted_questions(&$questions, $quiz) { +/// Used by review.php + + $quizquestions = array(); + $quizids = explode(",", $quiz->questions); + foreach ($quizids as $quizid) { + $quizquestions[$quizid] = true; + } + foreach ($questions as $key => $question) { + if (!isset($quizquestions[$question->id])) { + unset($questions[$key]); + } + } +} + ?> diff --git a/mod/quiz/report.php b/mod/quiz/report.php index 4d9d75bbb4..24078a9a07 100644 --- a/mod/quiz/report.php +++ b/mod/quiz/report.php @@ -1,6 +1,6 @@ id); - if (!isteacher($course->id)) { - if (!$quiz->review) { - error(get_string("noreview", "quiz")); - } - if (time() < $quiz->timeclose) { - error(get_string("noreviewuntil", "quiz", userdate($quiz->timeclose))); - } - if (empty($review)) { - error("You are using this script wrongly."); - } - } - - if (!empty($review)) { - $attempt = $review; + error("You are not allowed to use this script"); } add_to_log($course->id, "quiz", "report", "report.php?id=$cm->id", "$quiz->id"); -// Print the page header +/// Print the page header if ($course->category) { - $navigation = "id\">$course->shortname ->"; + $navigation = "id\">$course->shortname ->"; } $strquizzes = get_string("modulenameplural", "quiz"); $strquiz = get_string("modulename", "quiz"); $strreport = get_string("report", "quiz"); - $strname = get_string("name"); - $strattempts = get_string("attempts", "quiz"); - $strscore = get_string("score", "quiz"); - $strgrade = get_string("grade"); - $strbestgrade = get_string("bestgrade", "quiz"); - $strtimetaken = get_string("timetaken", "quiz"); - $strtimecompleted = get_string("timecompleted", "quiz"); print_header("$course->shortname: $quiz->name", "$course->fullname", "$navigation id>$strquizzes - -> id\">$quiz->name -> $strreport", + -> id\">$quiz->name -> $strreport", "", "", true); print_heading($quiz->name); - if (!empty($attempt)) { // Show a particular attempt - - if (! $attempt = get_record("quiz_attempts", "id", $attempt)) { - error("No such attempt ID exists"); - } - - if (!isteacher($course->id)) { - if ($attempt->userid != $USER->id) { - error("This is not your attempt!"); - } - } - - if (! $questions = quiz_get_attempt_responses($attempt, $quiz)) { - error("Could not reconstruct quiz results for attempt $attempt->id!"); - } - - quiz_remove_unwanted_questions($questions, $quiz); - - if (!$result = quiz_grade_attempt_results($quiz, $questions)) { - error("Could not re-grade this quiz attempt!"); - } - - if ($timetaken = ($attempt->timefinish - $attempt->timestart)) { - $timetaken = format_time($timetaken); - } else { - $timetaken = "-"; - } - - $table->align = array("RIGHT", "LEFT"); - $table->data[] = array("$strtimetaken:", $timetaken); - $table->data[] = array("$strtimecompleted:", userdate($attempt->timefinish)); - $table->data[] = array("$strscore:", "$result->sumgrades/$quiz->sumgrades ($result->percentage %)"); - $table->data[] = array("$strgrade:", "$result->grade/$quiz->grade"); - print_table($table); - - if (empty($review)) { - print_continue("report.php?q=$quiz->id"); - } else { - print_continue("view.php?q=$quiz->id"); - } - - $quiz->feedback = true; - $quiz->correctanswers = true; - $quiz->shuffleanswers = false; - $quiz->shufflequestions = false; - quiz_print_quiz_questions($quiz, $result, $questions); +/// Print list of available quiz reports - if (empty($review)) { - print_continue("report.php?q=$quiz->id"); + $reports = get_list_of_plugins("mod/quiz/report"); + echo ""; + foreach ($reports as $report) { + $strreport = get_string("report$report", "quiz"); + if ($report == $mode) { + echo ""; } else { - print_continue("view.php?q=$quiz->id"); + echo ""; } - print_footer($course); - exit; } + echo "
$strreportid&mode=$report\">$strreport


"; - if (!empty($regrade)) { - if (!$attempts = get_records("quiz_attempts", "quiz", $quiz->id)) { - print_header(get_string("noattempts", "quiz")); - print_continue("report.php?id=$cm->id"); - print_footer($course); - exit; - } - - $users = array(); - $count->attempt = 0; - $count->changed = 0; - foreach ($attempts as $attempt) { - - set_time_limit(120); - - if (!$attempt->timefinish) { // Skip incomplete attempts - continue; - } - - $count->attempt++; - - if (! $questions = quiz_get_attempt_responses($attempt, $quiz)) { - error("Could not reconstruct quiz results for attempt $attempt->id!"); - } - quiz_remove_unwanted_questions($questions, $quiz); - - if (!$result = quiz_grade_attempt_results($quiz, $questions)) { - error("Could not re-grade this quiz attempt!"); - } - if ($attempt->sumgrades != $result->sumgrades) { - $attempt->sumgrades = $result->sumgrades; - $count->changed++; +/// Open the selected quiz report and display it - if (! update_record("quiz_attempts", $attempt)) { - notify("Could not regrade attempt $attempt->id"); - } - } - - $users[$attempt->userid] = $attempt->userid; - } - - if ($users) { - foreach ($users as $userid) { - if (! quiz_save_best_grade($quiz, $userid)) { - notify("Could not save best grade for user $userid!"); - } - } - } - print_heading(get_string("regradecomplete", "quiz")); - print_heading(get_string("regradecount", "quiz", $count)); - print_continue("report.php?id=$cm->id"); - print_footer($course); - exit; + if (! is_readable("report/$mode/report.php")) { + error("Report not known ($mode)"); } - if (!$grades = quiz_get_grade_records($quiz)) { - print_footer($course); - exit; - } - - $table->head = array(" ", $strname, $strattempts, "$strbestgrade /$quiz->grade"); - $table->align = array("CENTER", "LEFT", "LEFT", "CENTER"); - $table->width = array(10, "*", "*", 20); - - foreach ($grades as $grade) { - $picture = print_user_picture($grade->userid, $course->id, $grade->picture, false, true); + include("report/default.php"); // Parent class + include("report/$mode/report.php"); - if ($attempts = quiz_get_user_attempts($quiz->id, $grade->userid)) { - $userattempts = quiz_get_user_attempts_string($quiz, $attempts, $grade->grade); - } + $report = new quiz_report(); - $table->data[] = array ($picture, - "wwwroot/user/view.php?id=$grade->userid&course=$course->id\">$grade->firstname $grade->lastname", - "$userattempts", round($grade->grade,0)); + if (! $report->display($quiz, $cm, $course)) { // Do anything before that we need to + error("Error occurred during pre-processing!"); } - print_table($table); - - echo "

"; - $options["regrade"] = "true"; - $options["id"] = $cm->id; - print_single_button("report.php", $options, get_string("regrade", "quiz")); - echo "

"; - -// Finish the page print_footer($course); - -function quiz_remove_unwanted_questions(&$questions, $quiz) { - - $quizquestions = array(); - $quizids = explode(",", $quiz->questions); - foreach ($quizids as $quizid) { - $quizquestions[$quizid] = true; - } - foreach ($questions as $key => $question) { - if (!isset($quizquestions[$question->id])) { - unset($questions[$key]); - } - } -} - - - - ?> diff --git a/mod/quiz/report/default.php b/mod/quiz/report/default.php new file mode 100644 index 0000000000..083ec06a01 --- /dev/null +++ b/mod/quiz/report/default.php @@ -0,0 +1,26 @@ +id" or q=$quiz->id", and "mode=reportname". +//////////////////////////////////////////////////////////////////// + +// Included by ../report.php + +class quiz_default_report { + + function display($cm, $course, $quiz) { /// This function just displays the report + return true; + } + +} + +?> diff --git a/mod/quiz/report/overview/report.php b/mod/quiz/report/overview/report.php new file mode 100644 index 0000000000..fc1e9001d3 --- /dev/null +++ b/mod/quiz/report/overview/report.php @@ -0,0 +1,42 @@ +head = array(" ", $strname, $strattempts, "$strbestgrade /$quiz->grade"); + $table->align = array("center", "left", "left", "center"); + $table->width = array(10, "*", "*", 20); + + foreach ($grades as $grade) { + $picture = print_user_picture($grade->userid, $course->id, $grade->picture, false, true); + + if ($attempts = quiz_get_user_attempts($quiz->id, $grade->userid)) { + $userattempts = quiz_get_user_attempts_string($quiz, $attempts, $grade->grade); + } + + $table->data[] = array ($picture, + "wwwroot/user/view.php?id=$grade->userid&course=$course->id\">". + "$grade->firstname $grade->lastname", + "$userattempts", round($grade->grade,0)); + } + + print_table($table); + + return true; + } +} + +?> diff --git a/mod/quiz/report/regrade/report.php b/mod/quiz/report/regrade/report.php new file mode 100644 index 0000000000..dbbb269025 --- /dev/null +++ b/mod/quiz/report/regrade/report.php @@ -0,0 +1,69 @@ +id)) { + print_header(get_string("noattempts", "quiz")); + print_continue("report.php?id=$cm->id"); + print_footer($course); + exit; + } + + $users = array(); + $count->attempt = 0; + $count->changed = 0; + + foreach ($attempts as $attempt) { + + set_time_limit(120); + + if (!$attempt->timefinish) { // Skip incomplete attempts + continue; + } + + $count->attempt++; + + if (! $questions = quiz_get_attempt_responses($attempt, $quiz)) { + error("Could not reconstruct quiz results for attempt $attempt->id!"); + } + quiz_remove_unwanted_questions($questions, $quiz); + + if (!$result = quiz_grade_attempt_results($quiz, $questions)) { + error("Could not re-grade this quiz attempt!"); + } + + if ($attempt->sumgrades != $result->sumgrades) { + $attempt->sumgrades = $result->sumgrades; + $count->changed++; + + if (! update_record("quiz_attempts", $attempt)) { + notify("Could not regrade attempt $attempt->id"); + } + } + + $users[$attempt->userid] = $attempt->userid; + } + + if ($users) { + foreach ($users as $userid) { + if (! quiz_save_best_grade($quiz, $userid)) { + notify("Could not save best grade for user $userid!"); + } + } + } + + print_heading(get_string("regradecomplete", "quiz")); + print_heading(get_string("regradecount", "quiz", $count)); + + return true; + + } +} + +?> diff --git a/mod/quiz/review.php b/mod/quiz/review.php new file mode 100644 index 0000000000..4f6b478260 --- /dev/null +++ b/mod/quiz/review.php @@ -0,0 +1,130 @@ +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"); + } + } + + if (! $attempt = get_record("quiz_attempts", "id", $attempt)) { + error("No such attempt ID exists"); + } + + + require_login($course->id); + + if (!isteacher($course->id)) { + if (!$quiz->review) { + error(get_string("noreview", "quiz")); + } + if (time() < $quiz->timeclose) { + error(get_string("noreviewuntil", "quiz", userdate($quiz->timeclose))); + } + if ($attempt->userid != $USER->id) { + error("This is not your attempt!"); + } + } + + add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&attempt=$attempt->id", "$quiz->id"); + + +// Print the page header + + if ($course->category) { + $navigation = "id\">$course->shortname ->"; + } + + $strquizzes = get_string("modulenameplural", "quiz"); + $strquiz = get_string("modulename", "quiz"); + $strreport = get_string("report", "quiz"); + $strreview = get_string("review", "quiz"); + $strname = get_string("name"); + $strattempts = get_string("attempts", "quiz"); + $strscore = get_string("score", "quiz"); + $strgrade = get_string("grade"); + $strbestgrade = get_string("bestgrade", "quiz"); + $strtimetaken = get_string("timetaken", "quiz"); + $strtimecompleted = get_string("timecompleted", "quiz"); + + print_header("$course->shortname: $quiz->name", "$course->fullname", + "$navigation id>$strquizzes + -> id\">$quiz->name -> $strreview", + "", "", true); + + print_heading($quiz->name); + + + if (! $questions = quiz_get_attempt_responses($attempt, $quiz)) { + error("Could not reconstruct quiz results for attempt $attempt->id!"); + } + + quiz_remove_unwanted_questions($questions, $quiz); + + if (!$result = quiz_grade_attempt_results($quiz, $questions)) { + error("Could not re-grade this quiz attempt!"); + } + + if ($timetaken = ($attempt->timefinish - $attempt->timestart)) { + $timetaken = format_time($timetaken); + } else { + $timetaken = "-"; + } + + $table->align = array("right", "left"); + $table->data[] = array("$strtimetaken:", $timetaken); + $table->data[] = array("$strtimecompleted:", userdate($attempt->timefinish)); + $table->data[] = array("$strscore:", "$result->sumgrades/$quiz->sumgrades ($result->percentage %)"); + $table->data[] = array("$strgrade:", "$result->grade/$quiz->grade"); + + print_table($table); + + if (isteacher($course->id)) { + print_continue("report.php?q=$quiz->id"); + } else { + print_continue("view.php?q=$quiz->id"); + } + + $quiz->feedback = true; + $quiz->correctanswers = true; + $quiz->shuffleanswers = false; + $quiz->shufflequestions = false; + quiz_print_quiz_questions($quiz, $result, $questions); + + if (isteacher($course->id)) { + print_continue("report.php?q=$quiz->id"); + } else { + print_continue("view.php?q=$quiz->id"); + } + + print_footer($course); + +?> diff --git a/mod/quiz/version.php b/mod/quiz/version.php index fc32d31176..2a26c241c5 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 = 2003071001; // The (date) version of this module +$module->version = 2003072400; // 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 e99cb08a2f..ccacce4b97 100644 --- a/mod/quiz/view.php +++ b/mod/quiz/view.php @@ -59,7 +59,7 @@ } else { $answercount = 0; } - echo "

id\">".get_string("viewallanswers","quiz",$answercount)."

"; + echo "

id\">".get_string("viewallanswers","quiz",$answercount)."

"; } else if (!$cm->visible) { notice(get_string("activityiscurrentlyhidden")); } @@ -102,7 +102,7 @@ if ($numattempts) { $table->head = array($strattempt, $strtimetaken, $strtimecompleted, "$strgrade / $quiz->grade"); - $table->align = array("CENTER", "CENTER", "LEFT", "RIGHT"); + $table->align = array("center", "center", "left", "right"); $table->width = array("", "", "", ""); foreach ($attempts as $attempt) { if ($timetaken = ($attempt->timefinish - $attempt->timestart)) { @@ -112,10 +112,10 @@ } $attemptgrade = format_float(($attempt->sumgrades/$quiz->sumgrades)*$quiz->grade); if ($attemptgrade == $mygrade) { - $attemptgrade = "$attemptgrade"; + $attemptgrade = "$attemptgrade"; } if (!$available and $quiz->review) { - $attemptgrade = "id&review=$attempt->id\">$attemptgrade"; + $attemptgrade = "id&attempt=$attempt->id\">$attemptgrade"; } $table->data[] = array( $attempt->attempt, format_time($attempt->timefinish - $attempt->timestart), @@ -126,11 +126,11 @@ } if ($available) { - echo "

".get_string("quizavailable", "quiz", userdate($quiz->timeclose)); + echo "

".get_string("quizavailable", "quiz", userdate($quiz->timeclose)); } else if ($timenow < $quiz->timeopen) { - echo "

".get_string("quiznotavailable", "quiz", userdate($quiz->timeopen)); + echo "

".get_string("quiznotavailable", "quiz", userdate($quiz->timeopen)); } else { - echo "

".get_string("quizclosed", "quiz", userdate($quiz->timeclose)); + echo "

".get_string("quizclosed", "quiz", userdate($quiz->timeclose)); } -- 2.39.5