From a68be3c7fa533a6bf0f86928574c692217c694da Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Tue, 23 Aug 2022 10:57:49 +0800 Subject: [PATCH] MDL-68943 assignfeedback_editpdf: Upgrade step for stale conversions --- .../bump_submission_for_stale_conversions.php | 103 ++++++++++++++++++ mod/assign/feedback/editpdf/db/upgrade.php | 11 ++ mod/assign/feedback/editpdf/version.php | 2 +- 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 mod/assign/feedback/editpdf/classes/task/bump_submission_for_stale_conversions.php diff --git a/mod/assign/feedback/editpdf/classes/task/bump_submission_for_stale_conversions.php b/mod/assign/feedback/editpdf/classes/task/bump_submission_for_stale_conversions.php new file mode 100644 index 0000000000000..baa53f2e8a410 --- /dev/null +++ b/mod/assign/feedback/editpdf/classes/task/bump_submission_for_stale_conversions.php @@ -0,0 +1,103 @@ +. + +/** + * Bump submission timemodified for conversions that are stale. + * + * @package assignfeedback_editpdf + * @copyright 2022 Catalyst IT Australia Pty Ltd + * @author Cameron Ball + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace assignfeedback_editpdf\task; + +use core\task\adhoc_task; + +/** + * Adhoc task to bump the submission timemodified associated with a stale conversion. + * + * @package assignfeedback_editpdf + * @copyright 2022 Catalyst IT Australia Pty Ltd + * @author Cameron Ball + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class bump_submission_for_stale_conversions extends adhoc_task { + + /** + * Run the task. + */ + public function execute() { + global $DB; + + // Used to only get records after whenever document conversion was enabled for this site. + $earliestconversion = $DB->get_record_sql("SELECT MIN(timecreated) AS min + FROM {files} + WHERE filearea = 'documentconversion'"); + + if ($earliestconversion) { + ['sql' => $extensionsql, 'params' => $extensionparams] = array_reduce( + ['doc', 'docx', 'rtf', 'xls', 'xlsx', 'ppt', 'pptx', 'html', 'odt', 'ods', 'png', 'jpg', 'txt', 'gif'], + function(array $c, string $ext) use ($DB): array { + return [ + 'sql' => $c['sql'] . ($c['sql'] ? ' OR ' : '') . $DB->sql_like('f1.filename', ':' . $ext), + 'params' => $c['params'] + [$ext => '%.' . $ext] + ]; + }, + ['sql' => '', 'params' => []] + ); + + // A converted file has its filename set to the contenthash of the file it converted. + // Find all files in the relevant file areas for which there is no corresponding + // file with the contenthash as the file name. + // + // Also check if the file has a greater modified time than the submission, if it does + // that means it is both stale (as per the above) and will never be reconverted. + $sql = "SELECT f3.id, f3.timemodified as fmodified, asu.id as submissionid + FROM {files} f1 + LEFT JOIN {files} f2 ON f1.contenthash = f2.filename + AND f2.component = 'core' AND f2.filearea = 'documentconversion' + JOIN {assign_submission} asu ON asu.id = f1.itemid + JOIN {assign_grades} asg ON asg.userid = asu.userid AND asg.assignment = asu.assignment + JOIN {files} f3 ON f3.itemid = asg.id + WHERE f1.filearea = 'submission_files' + AND f3.timecreated >= :earliest + AND ($extensionsql) + AND f2.filename IS NULL + AND f3.component = 'assignfeedback_editpdf' + AND f3.filearea = 'combined' + AND f3.filename = 'combined.pdf' + AND f3.timemodified >= asu.timemodified"; + + $submissionstobump = $DB->get_records_sql($sql, ['earliest' => $earliestconversion->min] + $extensionparams); + foreach ($submissionstobump as $submission) { + + // Set the submission modified time to one second later than the + // converted files modified time, this will cause assign to reconvert + // everything and delete the old files when the assignment grader is + // viewed. See get_page_images_for_attempt in document_services.php. + $newmodified = $submission->fmodified + 1; + $record = (object)[ + 'id' => $submission->submissionid, + 'timemodified' => $newmodified + ]; + + mtrace('Set submission ' . $submission->submissionid . ' timemodified to ' . $newmodified); + $DB->update_record('assign_submission', $record); + } + } + } +} diff --git a/mod/assign/feedback/editpdf/db/upgrade.php b/mod/assign/feedback/editpdf/db/upgrade.php index 387e1abbee63e..863cfe51a0ba0 100644 --- a/mod/assign/feedback/editpdf/db/upgrade.php +++ b/mod/assign/feedback/editpdf/db/upgrade.php @@ -98,5 +98,16 @@ function xmldb_assignfeedback_editpdf_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2021051701, 'assignfeedback', 'editpdf'); } + if ($oldversion < 2022082200) { + // Conversion records need to be removed in order for conversions to restart. + $DB->delete_records('file_conversion'); + + // Schedule an adhoc task to fix existing stale conversions. + $task = new \assignfeedback_editpdf\task\bump_submission_for_stale_conversions(); + \core\task\manager::queue_adhoc_task($task); + + upgrade_plugin_savepoint(true, 2022082200, 'assignfeedback', 'editpdf'); + } + return true; } diff --git a/mod/assign/feedback/editpdf/version.php b/mod/assign/feedback/editpdf/version.php index cf861d9e893ae..023c1fa3343c6 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 = 2021051701; +$plugin->version = 2022082200; $plugin->requires = 2021051100; $plugin->component = 'assignfeedback_editpdf';