moodle/moodle · error · moodle_exception

Could not find readonly pages for grade

Error message

Could not find readonly pages for grade 

What it means

This error is thrown by Moodle's editpdf feedback document service when generating a readonly version of an annotated PDF but no page records exist for the grade. The library treats the absence of page image records for a readonly request as an internal invariant violation — the comment in source says 'there should be a version of the pages available whenever we are requesting the readonly version'. It protects downstream code from operating on an empty page set.

Solutions

  1. Re-trigger generation: open the assignment grading interface for the affected submission so the PDF is regenerated from the combined document.
  2. Check the adhoc task queue (tool_task / adhoc_tasks table) for stuck or failed assignfeedback_editpdf tasks and rerun cron: php admin/cli/cron.php.
  3. Verify the combined PDF file exists in the assignfeedback_editpdf file area for the grade's context (check files table by contextid/component/filearea).
  4. Confirm the moodledata filedir is intact and permissions allow the web user to read stored page images.
  5. If page records are corrupt, delete the grade's editpdf pages/annotations so the next regeneration starts cleanly, then regenerate.

Example fix

// before: blindly requesting readonly pages
$pages = document_services::get_page_images_for_attempt($assignment, $userid, $attemptnumber);
// after: ensure the combined pdf is ready first
$document = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
if ($document->get_status() !== \assignfeedback_editpdf\document::STATUS_COMPLETE) {
    return false; // conversion still pending or failed; retry later
}
Defensive patterns

Strategy: validation

Validate before calling

$document = \assignfeedback_editpdf\document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
if ($document->get_status() !== \assignfeedback_editpdf\combined_document::STATUS_COMPLETE) {
    return false; // page images cannot exist yet; avoid the readonly-pages invariant error
}

Try / catch

try {
    $pages = \assignfeedback_editpdf\document_services::get_page_images_for_attempt($assignment, $userid, $attemptnumber);
} catch (\moodle_exception $e) {
    debugging($e->getMessage(), DEBUG_DEVELOPER);
    return false; // fall back to regeneration path
}

Prevention

When it happens

Trigger: Calling get_combined_pdf_for_attempt / generate_feedback_document with a readonly request (e.g. viewing feedback via send_pdf_file or get_corrected_pdf) when the assignfeedback_editpdf page records for the grade have not been generated yet or were lost.

Common situations: 1) The document conversion adhoc task died (node failure, cron restart) after the combined PDF existed but before page images were stored. 2) DB cleanup/pruning removed files in the editpdf file area while the grade remains. 3) Manually copied site where the filedir contents were not migrated. 4) Requesting readonly pages for a grade whose annotation generation never completed.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/e4fce6193324c600. Report an issue: GitHub.

Appendix: source

Thrown at public/mod/assign/feedback/editpdf/classes/document_services.php:628

                    // Extract the page number from the file name image_pageXXXX.png.
                    preg_match('/page([\d]+)\./', $file->get_filename(), $matches);
                    if (empty($matches) or !is_numeric($matches[1])) {
                        throw new \coding_exception("'" . $file->get_filename()
                            . "' file hasn't the expected format filename: image_pageXXXX.png.");
                    }
                    $pagenumber = (int)$matches[1];

                    // Save the page in the ordered array.
                    $pages[$pagenumber] = $file;
                }
                ksort($pages);
            }
        }

        // This should never happen, there should be a version of the pages available
        // whenever we are requesting the readonly version.
        if (empty($pages) && $readonly) {
            throw new \moodle_exception('Could not find readonly pages for grade ' . $grade->id);
        }

        // There are two situations where the number of page images generated does not
        // match the number of pages in the PDF:
        //
        // 1. The document conversion adhoc task was interrupted somehow (node died, solar flare, etc)
        // 2. The submission has been updated by the student
        //
        // In the case of 1. we need to regenerate the pages, see MDL-66626.
        // In the case of 2. we should do nothing, see MDL-45580.
        //
        // To differentiate between 1. and 2. we can check if the submission has been modified since the
        // pages were generated. If it has, then we're in situation 2.
        $totalpagesforattempt = self::page_number_for_attempt($assignment, $userid, $attemptnumber, false);
        $submissionmodified = isset($pagemodified) && $submission->timemodified > $pagemodified;
        if (empty($pages) || (count($pages) != $totalpagesforattempt && !$submissionmodified)) {
            $pages = self::generate_page_images_for_attempt($assignment, $userid, $attemptnumber, $resetrotation);
        }

View on GitHub (pinned to 48ea8c3322)