From 5da180d6fe79309babc47f207bd7ed8870a2c6ec Mon Sep 17 00:00:00 2001 From: Mathew May Date: Tue, 8 Aug 2023 16:30:33 +0800 Subject: [PATCH] MDL-71909 feedback_editpdf: CLI script to remove spurious conversions --- .../cli/delete_disabled_conversions.php | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 mod/assign/feedback/editpdf/cli/delete_disabled_conversions.php diff --git a/mod/assign/feedback/editpdf/cli/delete_disabled_conversions.php b/mod/assign/feedback/editpdf/cli/delete_disabled_conversions.php new file mode 100644 index 0000000000000..b2ec162d1b298 --- /dev/null +++ b/mod/assign/feedback/editpdf/cli/delete_disabled_conversions.php @@ -0,0 +1,106 @@ +. + +/** + * Allow admins to purge 'orphaned' conversions that were made when the feedback type was not enabled for the assignment. + * + * Inspired by the example CLI script provided on MDL-71909 written by Leon Stringer. + * + * @package assignfeedback_editpdf + * @copyright 2023 Mathew May + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define('CLI_SCRIPT', true); + +use \assignfeedback_editpdf\document_services; + +global $CFG, $DB; + +require(__DIR__ . '/../../../../../config.php'); +require_once("{$CFG->libdir}/clilib.php"); +require_once("../../../locallib.php"); + +// Now get cli options. +list($options, $unrecognized) = cli_get_params( + [ + 'help' => false, + 'courseid' => 0, + ], + [ + 'h' => 'help', + 'c' => 'courseid', + ] +); + +if ($unrecognized) { + $unrecognized = implode("\n ", $unrecognized); + cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); +} + +if ($options['help']) { + $help = + "Delete converted submissions that were made when the plugin was disabled. + +Options: +-h, --help Print out this help +-c, --courseid Course identifier (id) in which we look for assign activities and editpdf conversions. If not specified + we check every single assign activity. +Example: +\$ sudo -u www-data /usr/bin/php mod/feedback/editpdf/cli/delete_disabled_conversions.php -c=123 +"; + + echo $help; + die; +} + +$fs = get_file_storage(); +$assignset = []; + +// Get all assign activities in the course. +if (!empty($options['courseid'])) { + $courseid = $options['courseid']; + $assigncms = get_fast_modinfo($courseid)->get_instances_of('assign'); + $assignset = array_values($assigncms); +} else { + // Get all courses with assign activities. + $courses = core_course_category::search_courses(['modulelist' => 'assign']); + foreach ($courses as $course) { + // Get all assign activities in the course. + $assigncms = get_fast_modinfo($course->id)->get_instances_of('assign'); + $assignset = array_merge($assignset, array_values($assigncms)); + } +} + +// Loop through all assign activities. +foreach ($assignset as $assigncm) { + $assign = new \assign($assigncm->context, null, null); + $editpdfins = $assign->get_feedback_plugin_by_type('editpdf'); + + // The editpdf is disabled for this assign instance. Check for files to remove. + if (!$editpdfins->is_visible() || !$editpdfins->is_enabled()) { + $fileareas = $editpdfins->get_file_areas(); + foreach ($fileareas as $filearea => $notused) { + // Delete pdf files. + $fs->delete_area_files($assigncm->context->id, document_services::COMPONENT, $filearea); + } + cli_writeln("Assignment with id: {$assigncm->id} has had editpdf files removed if they existed."); + } else { + cli_writeln("Assignment with id: {$assigncm->id} has editpdf enabled. No files removed."); + } +} + +cli_writeln("Execution complete.");