From df94fd71953653803f9f67a7bcbc979907bf6c07 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Mon, 4 Nov 2024 22:25:33 +0800 Subject: [PATCH 1/4] Fix broken file references in "overview" content types --- db/upgrade.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ version.php | 2 +- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/db/upgrade.php b/db/upgrade.php index b56cb62..5bcb211 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -413,5 +413,49 @@ 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; } diff --git a/version.php b/version.php index 6ee8741..a22a8ab 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024090303; +$plugin->version = 2024090304; $plugin->requires = 2022112800; // Moodle 4.1 and above. $plugin->supported = [401, 401]; // Moodle 4.1. $plugin->component = 'mod_cms'; From bdb9a89dc1e757cfe999b91ce0a737ac16932589 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Mon, 4 Nov 2024 22:28:05 +0800 Subject: [PATCH 2/4] Use CONTEXT_MODULE constant in sql --- db/upgrade.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/db/upgrade.php b/db/upgrade.php index 5bcb211..e4ee9da 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -426,10 +426,11 @@ 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 = 70 + 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]; - $records = $DB->get_recordset_sql($sql); + $records = $DB->get_recordset_sql($sql, $params); foreach ($records as $record) { // Update the record with the new context id and path name hash. $record->contextid = $record->ctxid; From 14311bb2095d4e6fa669f2afa70beb7659dd9b4a Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Tue, 5 Nov 2024 17:26:02 +0800 Subject: [PATCH 3/4] CI Fix --- db/upgrade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/upgrade.php b/db/upgrade.php index e4ee9da..765a8ca 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 c2338b367088aaf18543209831eb4305188e1463 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Tue, 5 Nov 2024 18:57:28 +0800 Subject: [PATCH 4/4] Apply update to cms types that use "fields" --- db/upgrade.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db/upgrade.php b/db/upgrade.php index 765a8ca..18f2c4a 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -422,8 +422,9 @@ 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 AND cff.shortname = 'overview' + 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