diff --git a/mod/assign/feedback/editpdf/classes/privacy/provider.php b/mod/assign/feedback/editpdf/classes/privacy/provider.php index c089b40dc5e..ebad1e7bcbc 100644 --- a/mod/assign/feedback/editpdf/classes/privacy/provider.php +++ b/mod/assign/feedback/editpdf/classes/privacy/provider.php @@ -106,7 +106,7 @@ class provider implements $currentpath[] = get_string('privacy:path', 'assignfeedback_editpdf'); $assign = $exportdata->get_assign(); $plugin = $assign->get_plugin_by_type('assignfeedback', 'editpdf'); - $fileareas = $plugin->get_file_areas(); + $fileareas = $plugin->get_user_data_file_areas(); $grade = $exportdata->get_pluginobject(); foreach ($fileareas as $filearea => $notused) { writer::with_context($exportdata->get_context()) diff --git a/mod/assign/feedback/editpdf/classes/task/remove_orphaned_editpdf_files.php b/mod/assign/feedback/editpdf/classes/task/remove_orphaned_editpdf_files.php new file mode 100644 index 00000000000..ff2a7b70d45 --- /dev/null +++ b/mod/assign/feedback/editpdf/classes/task/remove_orphaned_editpdf_files.php @@ -0,0 +1,80 @@ +. + +/** + * Cleans up orphaned feedback pdf files and table entries. + * + * @package assignfeedback_editpdf + * @copyright 2022 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace assignfeedback_editpdf\task; + +use core\task\adhoc_task; + +/** + * Cleans up orphaned feedback pdf files and table entries. + * + * @package assignfeedback_editpdf + * @copyright 2022 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class remove_orphaned_editpdf_files extends adhoc_task { + + /** + * Run the task. + */ + public function execute() { + $this->remove_files_and_entries(); + $this->remove_rotated_table_entries(); + } + + /** + * Removes edit pdf feedback files and table entries that have been orphaned. + */ + private function remove_files_and_entries(): void { + global $DB; + + // Patiently remove all orphaned temporary pdf files. + $sql = "SELECT DISTINCT f.contextid, f.component, f.filearea, f.itemid + FROM {files} f + LEFT JOIN {assign_grades} g ON g.id = f.itemid + WHERE f.component = :assigneditpdf + AND NOT (filearea = :stamps AND f.itemid = 0) + AND g.id IS NULL"; + $params = ['assigneditpdf' => 'assignfeedback_editpdf', 'stamps' => 'stamps']; + + $results = $DB->get_recordset_sql($sql, $params); + foreach ($results as $record) { + $fs = get_file_storage(); + $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid); + } + $results->close(); + } + + /** + * Removes orphaned entries in the feedback edit pdf rotation table. + */ + private function remove_rotated_table_entries(): void { + global $DB; + $rotatesql = "SELECT er.id AS erid + FROM {assignfeedback_editpdf_rot} er + LEFT JOIN {assign_grades} g ON g.id = er.gradeid + WHERE g.id IS NULL"; + $DB->delete_records_subquery('assignfeedback_editpdf_rot', 'id', 'erid' ,$rotatesql); + } +} diff --git a/mod/assign/feedback/editpdf/db/upgrade.php b/mod/assign/feedback/editpdf/db/upgrade.php index c439bb98ccb..69bc1aab78f 100644 --- a/mod/assign/feedback/editpdf/db/upgrade.php +++ b/mod/assign/feedback/editpdf/db/upgrade.php @@ -93,5 +93,11 @@ function xmldb_assignfeedback_editpdf_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2022082200, 'assignfeedback', 'editpdf'); } + if ($oldversion < 2022112801) { + $task = new \assignfeedback_editpdf\task\remove_orphaned_editpdf_files(); + \core\task\manager::queue_adhoc_task($task); + + upgrade_plugin_savepoint(true, 2022112801, 'assignfeedback', 'editpdf'); + } return true; } diff --git a/mod/assign/feedback/editpdf/locallib.php b/mod/assign/feedback/editpdf/locallib.php index 9babc9e71e2..c7ffc1acf1e 100644 --- a/mod/assign/feedback/editpdf/locallib.php +++ b/mod/assign/feedback/editpdf/locallib.php @@ -405,6 +405,7 @@ class assign_feedback_editpdf extends assign_feedback_plugin { list($gradeids, $params) = $DB->get_in_or_equal(array_keys($grades), SQL_PARAMS_NAMED); $DB->delete_records_select('assignfeedback_editpdf_annot', 'gradeid ' . $gradeids, $params); $DB->delete_records_select('assignfeedback_editpdf_cmnt', 'gradeid ' . $gradeids, $params); + $DB->delete_records_select('assignfeedback_editpdf_rot', 'gradeid ' . $gradeids, $params); } return true; } @@ -436,7 +437,28 @@ class assign_feedback_editpdf extends assign_feedback_plugin { * @return array - An array of fileareas (keys) and descriptions (values) */ public function get_file_areas() { - return array(document_services::FINAL_PDF_FILEAREA => $this->get_name()); + return [ + document_services::FINAL_PDF_FILEAREA => $this->get_name(), + document_services::COMBINED_PDF_FILEAREA => $this->get_name(), + document_services::PARTIAL_PDF_FILEAREA => $this->get_name(), + document_services::IMPORT_HTML_FILEAREA => $this->get_name(), + document_services::PAGE_IMAGE_FILEAREA => $this->get_name(), + document_services::PAGE_IMAGE_READONLY_FILEAREA => $this->get_name(), + document_services::STAMPS_FILEAREA => $this->get_name(), + document_services::TMP_JPG_TO_PDF_FILEAREA => $this->get_name(), + document_services::TMP_ROTATED_JPG_FILEAREA => $this->get_name() + ]; + } + + /** + * Get all file areas for user data related to this plugin. + * + * @return array - An array of user data fileareas (keys) and descriptions (values) + */ + public function get_user_data_file_areas(): array { + return [ + document_services::FINAL_PDF_FILEAREA => $this->get_name(), + ]; } /** diff --git a/mod/assign/feedback/editpdf/version.php b/mod/assign/feedback/editpdf/version.php index 7ee4a23a73d..6fc788556f7 100644 --- a/mod/assign/feedback/editpdf/version.php +++ b/mod/assign/feedback/editpdf/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022112800; +$plugin->version = 2022112801; $plugin->requires = 2022111800; $plugin->component = 'assignfeedback_editpdf'; diff --git a/mod/assign/tests/locallib_test.php b/mod/assign/tests/locallib_test.php index e91b5291cc5..72c1c1a9b7f 100644 --- a/mod/assign/tests/locallib_test.php +++ b/mod/assign/tests/locallib_test.php @@ -3601,7 +3601,18 @@ Anchor link 2:Link text $fileareas = $plugin->get_file_areas(); if ($type == 'editpdf') { - $this->assertEquals(array('download' => 'Annotate PDF'), $fileareas); + $checkareas = [ + 'download' => 'Annotate PDF', + 'combined' => 'Annotate PDF', + 'partial' => 'Annotate PDF', + 'importhtml' => 'Annotate PDF', + 'pages' => 'Annotate PDF', + 'readonlypages' => 'Annotate PDF', + 'stamps' => 'Annotate PDF', + 'tmp_jpg_to_pdf' => 'Annotate PDF', + 'tmp_rotated_jpg' => 'Annotate PDF' + ]; + $this->assertEquals($checkareas, $fileareas); $usingfilearea++; } else if ($type == 'file') { $this->assertEquals(array('feedback_files' => 'Feedback files'), $fileareas); diff --git a/mod/assign/upgrade.txt b/mod/assign/upgrade.txt index 94d0a1aa77e..c26ebdd7bb2 100644 --- a/mod/assign/upgrade.txt +++ b/mod/assign/upgrade.txt @@ -2,6 +2,8 @@ This files describes API changes in the assign code. === 4.1 === * New method \assign::is_userid_filtered() has been implemented. It returns false if user id is filtered out by either user preferences for grading table or submission status filter. Otherwise, returns true. +* A new method was added to feedback/editpdf/locallib.php - get_user_data_file_areas() This just returns the necessary file areas + for retrieving a complete feedback pdf. === 4.0 === * The method \assign::grading_disabled() now has optional $gradinginfo parameter to improve performance