Example #1
0
    /**
     * @param bool changedb whether to change contents of state and grades
     * tables.
     */
    function regrade_all($dry, $quiz, $groupstudents) {
        global $DB, $OUTPUT;
        if (!has_capability('mod/quiz:regrade', $this->context)) {
            echo $OUTPUT->notification(get_string('regradenotallowed', 'quiz'));
            return true;
        }
        // Fetch all attempts
        if ($groupstudents) {
            list($usql, $params) = $DB->get_in_or_equal($groupstudents);
            $select = "userid $usql AND ";
        } else {
            $select = '';
            $params = array();
        }
        $select .= "quiz = ? AND preview = 0";
        $params[] = $quiz->id;
        if (!$attempts = $DB->get_records_select('quiz_attempts', $select, $params)) {
            echo $OUTPUT->heading(get_string('noattempts', 'quiz'));
            return true;
        }

        $this->clear_regrade_table($quiz, $groupstudents);

        // 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');

        // Print heading
        echo $OUTPUT->heading(get_string('regradingquiz', 'quiz', format_string($quiz->name)));
        $qstodo = count($questions);
        $qsdone = 0;
        if ($qstodo > 1) {
            $qpb = new progress_bar('qregradingbar', 500, true);
            $qpb->update($qsdone, $qstodo, "Question $qsdone of $qstodo");
        }
        $apb = new progress_bar('aregradingbar', 500, true);

        // Loop through all questions and all attempts and regrade while printing progress info
        $attemptstodo = count($attempts);
        foreach ($questions as $question) {
            $attemptsdone = 0;
            $apb->restart();
            echo '<p class="mdl-align"><strong>'.get_string('regradingquestion', 'quiz', $question->name).'</strong></p>';
            @flush();@ob_flush();
            foreach ($attempts as $attempt) {
                set_time_limit(30);
                $changed = regrade_question_in_attempt($question, $attempt, $quiz, true, $dry);

                $attemptsdone++;
                $a = new stdClass();
                $a->done = $attemptsdone;
                $a->todo = $attemptstodo;
                $apb->update($attemptsdone, $attemptstodo, get_string('attemptprogress', 'quiz_overview', $a));
            }
            $qsdone++;
            if (isset($qpb)) {
                $a = new stdClass();
                $a->done = $qsdone;
                $a->todo = $qstodo;
                $qpb->update($qsdone, $qstodo, get_string('qprogress', 'quiz_overview', $a));
            }
            // the following makes sure that the output is sent immediately.
            @flush();@ob_flush();
        }

        if (!$dry) {
            $this->check_overall_grades($quiz, $groupstudents);
        }
    }