3 // This script regrades all attempts at this quiz
4 require_once($CFG->libdir
.'/tablelib.php');
6 class quiz_report
extends quiz_default_report
{
8 function display($quiz, $cm, $course) {
12 $this->print_header_and_tabs($cm, $course, $quiz, $reportmode="regrade");
15 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
16 if (!has_capability('mod/quiz:grade', $context)) {
17 notify(get_string('regradenotallowed', 'quiz'));
22 if (!$attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = 0")) {
23 print_heading(get_string('noattempts', 'quiz'));
27 // Fetch all questions
28 $sql = "SELECT q.*, i.grade AS maxgrade FROM {$CFG->prefix}question q,
29 {$CFG->prefix}quiz_question_instances i
30 WHERE i.quiz = $quiz->id
31 AND i.question = q.id";
33 if (! $questions = get_records_sql($sql)) {
34 error("Failed to get questions for regrading!");
36 get_question_options($questions);
39 print_heading(get_string('regradingquiz', 'quiz', format_string($quiz->name
)));
40 echo '<div class="boxaligncenter">';
41 print_string('regradedisplayexplanation', 'quiz');
44 // Loop through all questions and all attempts and regrade while printing progress info
45 foreach ($questions as $question) {
46 echo '<strong>'.get_string('regradingquestion', 'quiz', $question->name
).'</strong> '.get_string('attempts', 'quiz').": \n";
47 foreach ($attempts as $attempt) {
49 $changed = regrade_question_in_attempt($question, $attempt, $quiz, true);
51 link_to_popup_window ('/mod/quiz/reviewquestion.php?attempt='.$attempt->id
.'&question='.$question->id
,
52 'reviewquestion', ' #'.$attempt->id
, 450, 550, get_string('reviewresponse', 'quiz'));
54 echo ' #'.$attempt->id
;
58 // the following makes sure that the output is sent immediately.
62 // Loop through all questions and recalculate $attempt->sumgrade
64 foreach ($attempts as $attempt) {
66 $questionids = explode(',', quiz_questions_in_quiz($attempt->layout
));
67 foreach($questionids as $questionid) {
68 $lastgradedid = get_field('question_sessions', 'newgraded', 'attemptid', $attempt->uniqueid
, 'questionid', $questionid);
69 $sumgrades +
= get_field('question_states', 'grade', 'id', $lastgradedid);
71 if ($attempt->sumgrades
!= $sumgrades) {
73 set_field('quiz_attempts', 'sumgrades', $sumgrades, 'id', $attempt->id
);
77 // Update the overall quiz grades
78 if ($grades = get_records('quiz_grades', 'quiz', $quiz->id
)) {
79 foreach($grades as $grade) {
80 quiz_save_best_grade($quiz, $grade->userid
);