From 83d73914d5e22533af2ffbd115ed1aed7eede8da Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Fri, 15 Nov 2024 12:04:47 +0800 Subject: [PATCH 1/5] Revert "Apply update to cms types that use "fields"" This reverts commit 60f5426b919e56df3f0b73893ed67e2fde7d75f9. --- db/upgrade.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/db/upgrade.php b/db/upgrade.php index 18f2c4a..765a8ca 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -422,9 +422,8 @@ function xmldb_cms_upgrade($oldversion) { $sql = "SELECT f.*, ctx.id as ctxid FROM {files} f JOIN {customfield_data} cfd ON cfd.id = f.itemid - JOIN {customfield_field} cff ON cff.id = cfd.fieldid + JOIN {customfield_field} cff ON cff.id = cfd.fieldid AND cff.shortname = 'overview' JOIN {cms} cms ON cms.id = cfd.instanceid - JOIN {cms_types} cmst ON cmst.id = cms.typeid AND cmst.datasources LIKE '%fields%' JOIN {course_modules} cm ON cm.instance = cms.id JOIN {modules} m ON m.id = cm.module AND m.name = 'cms' JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :modulecontextlevel From e41ea1ea5e8c603b5ccd26d792d0280e643d2bb2 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Fri, 15 Nov 2024 12:04:53 +0800 Subject: [PATCH 2/5] Revert "CI Fix" This reverts commit b358bb0c32068fd16a01c00e31bc5a165687ee99. --- db/upgrade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/upgrade.php b/db/upgrade.php index 765a8ca..e4ee9da 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -416,7 +416,7 @@ function xmldb_cms_upgrade($oldversion) { if ($oldversion < 2024090304) { // Update files belonging to mod_cms overview section content types to use the course module context id. // Update the pathnamehash as well, otherwise files will display as missing until the content is edited and saved. - // Records are effectively copied, the old records remain in case anything relies on them and they can be used to + // Records are effectively copied, the old records remain in case anything relies on them and they can be used to // cross reference the new records if needed. $sql = "SELECT f.*, ctx.id as ctxid From 5bc76684f0987140c9df1288dd0d93d11d56fe70 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Fri, 15 Nov 2024 12:05:01 +0800 Subject: [PATCH 3/5] Revert "Use CONTEXT_MODULE constant in sql" This reverts commit a434ef86bdbe244023245974303ab1370f98c5d6. --- db/upgrade.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/db/upgrade.php b/db/upgrade.php index e4ee9da..5bcb211 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -426,11 +426,10 @@ function xmldb_cms_upgrade($oldversion) { JOIN {cms} cms ON cms.id = cfd.instanceid JOIN {course_modules} cm ON cm.instance = cms.id JOIN {modules} m ON m.id = cm.module AND m.name = 'cms' - JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :modulecontextlevel + JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = 70 WHERE f.contextid = 1 AND f.component = 'customfield_textarea' AND f.filearea = 'value'"; - $params = ['modulecontextlevel' => CONTEXT_MODULE]; - $records = $DB->get_recordset_sql($sql, $params); + $records = $DB->get_recordset_sql($sql); foreach ($records as $record) { // Update the record with the new context id and path name hash. $record->contextid = $record->ctxid; From 8388f7e12ee476a8d86e16a6dd36c26f77790de5 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Fri, 15 Nov 2024 12:05:07 +0800 Subject: [PATCH 4/5] Revert "Fix broken file references in "overview" content types" This reverts commit fd780075968d7b1ef795308d28910a388de34f55. --- db/upgrade.php | 44 -------------------------------------------- 1 file changed, 44 deletions(-) diff --git a/db/upgrade.php b/db/upgrade.php index 5bcb211..b56cb62 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -413,49 +413,5 @@ function xmldb_cms_upgrade($oldversion) { upgrade_mod_savepoint(true, 2024090303, 'cms'); } - if ($oldversion < 2024090304) { - // Update files belonging to mod_cms overview section content types to use the course module context id. - // Update the pathnamehash as well, otherwise files will display as missing until the content is edited and saved. - // Records are effectively copied, the old records remain in case anything relies on them and they can be used to - // cross reference the new records if needed. - - $sql = "SELECT f.*, ctx.id as ctxid - FROM {files} f - JOIN {customfield_data} cfd ON cfd.id = f.itemid - JOIN {customfield_field} cff ON cff.id = cfd.fieldid AND cff.shortname = 'overview' - JOIN {cms} cms ON cms.id = cfd.instanceid - JOIN {course_modules} cm ON cm.instance = cms.id - JOIN {modules} m ON m.id = cm.module AND m.name = 'cms' - JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = 70 - WHERE f.contextid = 1 AND f.component = 'customfield_textarea' AND f.filearea = 'value'"; - - $records = $DB->get_recordset_sql($sql); - foreach ($records as $record) { - // Update the record with the new context id and path name hash. - $record->contextid = $record->ctxid; - $record->pathnamehash = file_storage::get_pathname_hash( - $record->contextid, - $record->component, - $record->filearea, - $record->itemid, - $record->filepath, - $record->filename - ); - - // Remove the record id and ctxid fields. - unset($record->ctxid); - unset($record->id); - - // Create a new record. - try { - $DB->insert_record('files', $record); - } catch (moodle_exception $e) { - debugging('Failed to insert record into files table: ' . $e->getMessage(), DEBUG_DEVELOPER); - } - } - - upgrade_mod_savepoint(true, 2024090304, 'cms'); - } - return true; } From 7a158266eaadfd1fd60db577ecd47a2ff0d1a1ed Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Tue, 12 Nov 2024 23:19:29 +0800 Subject: [PATCH 5/5] Rune files table upgrade in ad hoc task --- classes/task/update_files_context.php | 155 ++++++++++++++++++++++++++ db/upgrade.php | 8 ++ 2 files changed, 163 insertions(+) create mode 100644 classes/task/update_files_context.php diff --git a/classes/task/update_files_context.php b/classes/task/update_files_context.php new file mode 100644 index 0000000..ede158f --- /dev/null +++ b/classes/task/update_files_context.php @@ -0,0 +1,155 @@ +. + +namespace mod_cms\task; + +use core\task\adhoc_task; +use csv_export_writer; +use file_storage; +use moodle_exception; + +/** + * Update the context of embedded files to match the context of the mod_cms module instance. + * + * @package mod_cms + * @author Alexander Van der Bellen + * @copyright 2024 Catalyst IT Australia + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class update_files_context extends adhoc_task { + + /** + * Factory method to create a new update_files_context task. + * + * @param int|null $courseid Limit the task to a specific course or all courses if null. + * @param bool $dryrun Whether to run the task without making database changes. + * @return update_files_context The task instance. + */ + public static function instance(?int $courseid = null, bool $dryrun = true): update_files_context { + $task = new self(); + $task->set_custom_data((object) ['courseid' => $courseid, 'dryrun' => $dryrun]); + return $task; + } + + /** + * Run the task to delete course results for a user. + */ + public function execute(): void { + global $CFG; + $data = $this->get_custom_data(); + $csv = self::update_contexts($data->courseid, $data->dryrun); + file_put_contents($CFG->dataroot . '/' . $csv->filename, $csv->print_csv_data(true)); + } + + /** + * Update mod_cms customfield_textarea embedded file contexts to match the context of the mod_cms module instance. + * @param int|null $courseid Limit the task to a specific course or all courses if null. + * @param bool $dryrun Whether to run the task without making database changes. + * @return csv_export_writer The CSV export writer containing the results of the task. + */ + private static function update_contexts(?int $courseid, bool $dryrun): csv_export_writer { + global $DB; + + $sql = "SELECT f.*, ctx.id AS ctxid, cms.course AS courseid + FROM {files} f + JOIN {customfield_data} cfd ON cfd.id = f.itemid + JOIN {customfield_field} cff ON cff.id = cfd.fieldid + JOIN {cms} cms ON cms.id = cfd.instanceid + JOIN {cms_types} cmst ON cmst.id = cms.typeid AND cmst.datasources LIKE '%fields%' + JOIN {course_modules} cm ON cm.instance = cms.id + JOIN {modules} m ON m.id = cm.module AND m.name = 'cms' + JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :modulecontextlevel + WHERE f.contextid = 1 AND f.component = 'customfield_textarea' AND f.filearea = 'value'"; + + $params = ['modulecontextlevel' => CONTEXT_MODULE]; + + if (!empty($courseid)) { + $sql .= " AND cms.course = :courseid"; + $params['courseid'] = $courseid; + } + + $csv = new csv_export_writer(); + $csv->set_filename('mod_cms_update_files_context'); + $csv->add_data([ + 'courseid', + 'newpathnamehash', + 'newcontextid', + 'id', + 'contenthash', + 'pathnamehash', + 'contextid', + 'component', + 'filearea', + 'itemid', + 'filepath', + 'filename', + 'timecreated', + 'timemodified', + ]); + + $records = $DB->get_recordset_sql($sql, $params); + foreach ($records as $record) { + $newcontextid = $record->ctxid; + + $newpathnamehash = file_storage::get_pathname_hash( + $newcontextid, + $record->component, + $record->filearea, + $record->itemid, + $record->filepath, + $record->filename + ); + + $csv->add_data([ + $record->courseid, + $newpathnamehash, + $newcontextid, + $record->id, + $record->contenthash, + $record->pathnamehash, + $record->contextid, + $record->component, + $record->filearea, + $record->itemid, + $record->filepath, + $record->filename, + $record->timecreated, + $record->timemodified, + ]); + + // Update the record with the new context id and path name hash. + $record->contextid = $newcontextid; + $record->pathnamehash = $newpathnamehash; + + if (!$dryrun) { + // Remove the ctxid and courseid fields. + unset($record->ctxid); + unset($record->courseid); + + // Update the record in the database. + try { + $DB->update_record('files', $record); + } catch (moodle_exception $e) { + debugging('Failed to insert record into files table: ' . $e->getMessage(), DEBUG_DEVELOPER); + } + } + } + + $records->close(); + + return $csv; + } +} diff --git a/db/upgrade.php b/db/upgrade.php index b56cb62..8c243e0 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -413,5 +413,13 @@ function xmldb_cms_upgrade($oldversion) { upgrade_mod_savepoint(true, 2024090303, 'cms'); } + if ($oldversion < 2024090304) { + // Run ad hoc task for updating contextid in the files table. + $task = \mod_cms\task\update_files_context::instance(null, false); + manager::queue_adhoc_task($task); + + upgrade_mod_savepoint(true, 2024090304, 'cms'); + } + return true; }