function display($quiz, $cm, $course) { global $CFG; // Print header $this->print_header_and_tabs($cm, $course, $quiz, $reportmode = "regrade"); // Check permissions $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (!has_capability('mod/quiz:grade', $context)) { notify(get_string('regradenotallowed', 'quiz')); return true; } // Fetch all attempts if (!($attempts = get_records_select('quiz_attempts', "quiz = '{$quiz->id}' AND preview = 0"))) { print_heading(get_string('noattempts', 'quiz')); return true; } // Fetch all questions $sql = "SELECT q.*, i.grade AS maxgrade FROM {$CFG->prefix}question q,\n {$CFG->prefix}quiz_question_instances i\n WHERE i.quiz = {$quiz->id}\n AND i.question = q.id"; if (!($questions = get_records_sql($sql))) { error("Failed to get questions for regrading!"); } get_question_options($questions); // Print heading print_heading(get_string('regradingquiz', 'quiz', format_string($quiz->name))); echo '<div class="boxaligncenter">'; print_string('regradedisplayexplanation', 'quiz'); echo '</div>'; // Loop through all questions and all attempts and regrade while printing progress info foreach ($questions as $question) { echo '<strong>' . get_string('regradingquestion', 'quiz', $question->name) . '</strong> ' . get_string('attempts', 'quiz') . ": \n"; foreach ($attempts as $attempt) { set_time_limit(30); $changed = regrade_question_in_attempt($question, $attempt, $quiz, true); if ($changed) { link_to_popup_window('/mod/quiz/reviewquestion.php?attempt=' . $attempt->id . '&question=' . $question->id, 'reviewquestion', ' #' . $attempt->id, 450, 550, get_string('reviewresponse', 'quiz')); } else { echo ' #' . $attempt->id; } } echo '<br />'; // the following makes sure that the output is sent immediately. @flush(); @ob_flush(); } // Loop through all questions and recalculate $attempt->sumgrade $attemptschanged = 0; foreach ($attempts as $attempt) { $sumgrades = 0; $questionids = explode(',', quiz_questions_in_quiz($attempt->layout)); foreach ($questionids as $questionid) { $lastgradedid = get_field('question_sessions', 'newgraded', 'attemptid', $attempt->uniqueid, 'questionid', $questionid); $sumgrades += get_field('question_states', 'grade', 'id', $lastgradedid); } if ($attempt->sumgrades != $sumgrades) { $attemptschanged++; set_field('quiz_attempts', 'sumgrades', $sumgrades, 'id', $attempt->id); } } // Update the overall quiz grades if ($grades = get_records('quiz_grades', 'quiz', $quiz->id)) { foreach ($grades as $grade) { quiz_save_best_grade($quiz, $grade->userid); } } return true; }
function regrade_selected_attempts($quiz, $attemptids, $groupstudents) { global $DB; require_capability('mod/quiz:regrade', $this->context); if ($groupstudents) { list($usql, $params) = $DB->get_in_or_equal($groupstudents); $where = "qa.userid $usql AND "; } else { $params = array(); $where = ''; } list($asql, $aparams) = $DB->get_in_or_equal($attemptids); $where = "qa.id $asql AND "; $params = array_merge($params, $aparams); $where .= "qa.quiz = ? AND qa.preview = 0"; $params[] = $quiz->id; if (!$attempts = $DB->get_records_sql('SELECT qa.* FROM {quiz_attempts} qa WHERE '. $where, $params)) { print_error('noattemptstoregrade', 'quiz_overview'); } // Fetch all questions $questions = question_load_questions(explode(',',quiz_questions_in_quiz($quiz->questions)), 'qqi.grade AS maxgrade, qqi.id AS instance', '{quiz_question_instances} qqi ON qqi.quiz = ' . $quiz->id . ' AND q.id = qqi.question'); $updateoverallgrades = array(); foreach($attempts as $attempt) { foreach ($questions as $question) { $changed = regrade_question_in_attempt($question, $attempt, $quiz, true); } $updateoverallgrades[] = $attempt->uniqueid; } $this->check_overall_grades($quiz, array(), $updateoverallgrades); }