@@ -342,32 +342,21 @@ function xmldb_cms_upgrade($oldversion) {
342
342
}
343
343
344
344
if ($ oldversion < 2023120101 ) {
345
- // Update query for setting.
346
- $ sql = "UPDATE {customfield_data} mcd1
347
- SET contextid = sub.mcid
348
- FROM (
349
- SELECT mcd.id mcdid, mcd.contextid mcdcontextid, mc.id mcid
350
- FROM {customfield_data} mcd
351
- JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
352
- JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid
353
- JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = (
354
- SELECT id FROM {modules} WHERE name = 'cms'
355
- )
356
- JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = 70
357
- WHERE mcd.id IN (
358
- SELECT mcd.id
359
- FROM {customfield_data} mcd
360
- JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
361
- JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid
362
- JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = (
363
- SELECT id FROM {modules} WHERE name = 'cms'
364
- )
365
- JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = 70
366
- WHERE mcf.type = 'textarea' AND mcc.component = 'mod_cms' AND mcd.contextid != mc.id
367
- )
368
- ) sub
369
- WHERE mcd1.id = sub.mcdid " ;
370
- $ DB ->execute ($ sql );
345
+ // Collect records which have wrong contextid in the customfield data.
346
+ $ sql = "SELECT mcd.id mcdid, mcd.contextid mcdcontextid, mc.id mcid
347
+ FROM {customfield_data} mcd
348
+ JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
349
+ JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid
350
+ JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = (
351
+ SELECT id FROM mdl_modules WHERE name = 'cms'
352
+ )
353
+ JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = " . CONTEXT_MODULE . "
354
+ WHERE mcc.component = 'mod_cms' AND mcd.contextid != mc.id " ;
355
+ $ records = $ DB ->get_records_sql ($ sql );
356
+ // Update records with correct contextid.
357
+ foreach ($ records as $ record ) {
358
+ $ DB ->set_field ('customfield_data ' , 'contextid ' , $ record ->mcid , ['id ' => $ record ->mcdid ]);
359
+ }
371
360
upgrade_mod_savepoint (true , 2023120101 , 'cms ' );
372
361
}
373
362
0 commit comments