{"record":{"id":"e4fce6193324c600","repo":"moodle/moodle","slug":"could-not-find-readonly-pages-for-grade","errorCode":null,"errorMessage":"Could not find readonly pages for grade ","messagePattern":"Could not find readonly pages for grade ","errorType":"exception","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/mod/assign/feedback/editpdf/classes/document_services.php","lineNumber":628,"sourceCode":"                    // Extract the page number from the file name image_pageXXXX.png.\n                    preg_match('/page([\\d]+)\\./', $file->get_filename(), $matches);\n                    if (empty($matches) or !is_numeric($matches[1])) {\n                        throw new \\coding_exception(\"'\" . $file->get_filename()\n                            . \"' file hasn't the expected format filename: image_pageXXXX.png.\");\n                    }\n                    $pagenumber = (int)$matches[1];\n\n                    // Save the page in the ordered array.\n                    $pages[$pagenumber] = $file;\n                }\n                ksort($pages);\n            }\n        }\n\n        // This should never happen, there should be a version of the pages available\n        // whenever we are requesting the readonly version.\n        if (empty($pages) && $readonly) {\n            throw new \\moodle_exception('Could not find readonly pages for grade ' . $grade->id);\n        }\n\n        // There are two situations where the number of page images generated does not\n        // match the number of pages in the PDF:\n        //\n        // 1. The document conversion adhoc task was interrupted somehow (node died, solar flare, etc)\n        // 2. The submission has been updated by the student\n        //\n        // In the case of 1. we need to regenerate the pages, see MDL-66626.\n        // In the case of 2. we should do nothing, see MDL-45580.\n        //\n        // To differentiate between 1. and 2. we can check if the submission has been modified since the\n        // pages were generated. If it has, then we're in situation 2.\n        $totalpagesforattempt = self::page_number_for_attempt($assignment, $userid, $attemptnumber, false);\n        $submissionmodified = isset($pagemodified) && $submission->timemodified > $pagemodified;\n        if (empty($pages) || (count($pages) != $totalpagesforattempt && !$submissionmodified)) {\n            $pages = self::generate_page_images_for_attempt($assignment, $userid, $attemptnumber, $resetrotation);\n        }","sourceCodeStart":610,"sourceCodeEnd":646,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/mod/assign/feedback/editpdf/classes/document_services.php#L610-L646","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-trigger generation: open the assignment grading interface for the affected submission so the PDF is regenerated from the combined document.","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.","Verify the combined PDF file exists in the assignfeedback_editpdf file area for the grade's context (check files table by contextid/component/filearea).","Confirm the moodledata filedir is intact and permissions allow the web user to read stored page images.","If page records are corrupt, delete the grade's editpdf pages/annotations so the next regeneration starts cleanly, then regenerate."],"exampleFix":"// before: blindly requesting readonly pages\n$pages = document_services::get_page_images_for_attempt($assignment, $userid, $attemptnumber);\n// after: ensure the combined pdf is ready first\n$document = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);\nif ($document->get_status() !== \\assignfeedback_editpdf\\document::STATUS_COMPLETE) {\n    return false; // conversion still pending or failed; retry later\n}","handlingStrategy":"validation","validationCode":"$document = \\assignfeedback_editpdf\\document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);\nif ($document->get_status() !== \\assignfeedback_editpdf\\combined_document::STATUS_COMPLETE) {\n    return false; // page images cannot exist yet; avoid the readonly-pages invariant error\n}","typeGuard":null,"tryCatchPattern":"try {\n    $pages = \\assignfeedback_editpdf\\document_services::get_page_images_for_attempt($assignment, $userid, $attemptnumber);\n} catch (\\moodle_exception $e) {\n    debugging($e->getMessage(), DEBUG_DEVELOPER);\n    return false; // fall back to regeneration path\n}","preventionTips":["Ensure cron runs reliably so the page-image adhoc tasks complete.","Before requesting readonly pages, check the combined document status is STATUS_COMPLETE.","Monitor the files table for assignfeedback_editpdf page records; alert when grades have a combined PDF but no pages.","Keep moodledata filedir intact during migrations and verify permissions after upgrades."],"tags":["moodle","editpdf","pdf-conversion","missing-pages","adhoc-task"],"backgroundTag":"internal-invariant-violation","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}