Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / quiz / report / regrade / report.php
blob2c0ea4d2b00d3bbe7d6f6699f5f12e974ba2ca37
1 <?php // $Id$
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) {
9 global $CFG;
11 // Print header
12 $this->print_header_and_tabs($cm, $course, $quiz, $reportmode="regrade");
14 // Check permissions
15 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
16 if (!has_capability('mod/quiz:grade', $context)) {
17 notify(get_string('regradenotallowed', 'quiz'));
18 return true;
21 // Fetch all attempts
22 if (!$attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = 0")) {
23 print_heading(get_string('noattempts', 'quiz'));
24 return true;
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);
38 // Print heading
39 print_heading(get_string('regradingquiz', 'quiz', format_string($quiz->name)));
40 echo '<div class="boxaligncenter">';
41 print_string('regradedisplayexplanation', 'quiz');
42 echo '</div>';
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) {
48 set_time_limit(30);
49 $changed = regrade_question_in_attempt($question, $attempt, $quiz, true);
50 if ($changed) {
51 link_to_popup_window ('/mod/quiz/reviewquestion.php?attempt='.$attempt->id.'&amp;question='.$question->id,
52 'reviewquestion', ' #'.$attempt->id, 450, 550, get_string('reviewresponse', 'quiz'));
53 } else {
54 echo ' #'.$attempt->id;
57 echo '<br />';
58 // the following makes sure that the output is sent immediately.
59 @flush();@ob_flush();
62 // Loop through all questions and recalculate $attempt->sumgrade
63 $attemptschanged = 0;
64 foreach ($attempts as $attempt) {
65 $sumgrades = 0;
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) {
72 $attemptschanged++;
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);
84 return true;