Skip to content

Commit f760b19

Browse files
TomoTsuyukiFragonite
authored andcommitted
Issue #121 Update contextid for userlist customfield records.
1 parent 18d02d0 commit f760b19

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

classes/task/update_customfield_context.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class update_customfield_context extends adhoc_task {
3636
public function execute() {
3737
global $DB;
3838

39+
// Record {customfield_data}.instanceid is from {cms}.id.
40+
// Record {customfield_data}.contextid is from contextid of {course_modules}, which is linked to {cms}.
41+
3942
// Collect records which have wrong contextid in the customfield data.
4043
$sql = "SELECT mcd.id mcdid, mcd.contextid mcdcontextid, mc.id mcid
4144
FROM {customfield_data} mcd
@@ -45,7 +48,7 @@ public function execute() {
4548
SELECT id FROM {modules} WHERE name = 'cms'
4649
)
4750
JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = " . CONTEXT_MODULE . "
48-
WHERE mcc.component = 'mod_cms' AND mcd.contextid != mc.id";
51+
WHERE mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcd.contextid != mc.id";
4952
$records = $DB->get_records_sql($sql);
5053
// Update records with correct contextid.
5154
foreach ($records as $record) {

db/upgrade.php

+46-2
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,14 @@ function xmldb_cms_upgrade($oldversion) {
344344
}
345345

346346
if ($oldversion < 2023120101) {
347-
// Add adhoc task to update contextid in customfield records.
347+
// Add adhoc task to update contextid in customfield records for 'cmsfield' type.
348348
manager::queue_adhoc_task(new update_customfield_context());
349349
upgrade_mod_savepoint(true, 2023120101, 'cms');
350350
}
351351

352352
if ($oldversion < 2023120102) {
353353
$dbman = $DB->get_manager();
354-
// Conditionally launch add field valuetrust.
354+
// Update valuetrust if exists for mod_cms (both 'cmsfield' and 'cmsuserlist').
355355
if ($dbman->field_exists('customfield_data', 'valuetrust')) {
356356
$sql = "SELECT mcd.id mcdid
357357
FROM {customfield_data} mcd
@@ -369,5 +369,49 @@ function xmldb_cms_upgrade($oldversion) {
369369
upgrade_mod_savepoint(true, 2023120102, 'cms');
370370
}
371371

372+
if ($oldversion < 2023120103) {
373+
// Update contextid in customfield records for 'cmsuserlist' type.
374+
// {customfield_data}.instanceid" is from one of id from userlistinstanceids which is JSON encoded in {cms}.customdata.
375+
// "userlistinstanceids" is a unique id for the customfield_data.
376+
// New ID is from userlistmaxinstanceid which is JSON encoded in the {cms_types}.customdata" and add 1 to use.
377+
$sql = "SELECT mc.id, mc.customdata, mcx.id contextid
378+
FROM {cms} mc
379+
JOIN {course_modules} mcm ON mc.id = mcm.instance AND mcm.module = (
380+
SELECT id FROM {modules} WHERE name = 'cms'
381+
)
382+
JOIN {context} mcx ON mcx.instanceid = mcm.id AND mcx.contextlevel = " . CONTEXT_MODULE . "
383+
WHERE mc.customdata LIKE '%userlistinstanceids%'";
384+
$cmsrecords = $DB->get_records_sql($sql);
385+
386+
// Load userlist from customfield_data and set to array.
387+
$userlist = [];
388+
foreach ($cmsrecords as $cmsrecord) {
389+
$customdata = json_decode($cmsrecord->customdata);
390+
foreach ($customdata->userlistinstanceids as $instanceid) {
391+
$userlist[$instanceid] = [
392+
'id' => $cmsrecord->id,
393+
'contextid' => $cmsrecord->contextid,
394+
];
395+
}
396+
}
397+
398+
// Check cmsuserlist records and set correct contextid if it's different one.
399+
$sql = "SELECT mcd.id, mcd.instanceid, mcd.contextid
400+
FROM {customfield_data} mcd
401+
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
402+
JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid
403+
WHERE mcc.component = 'mod_cms' AND mcc.area = 'cmsuserlist'";
404+
$cmsuserlistdata = $DB->get_records_sql($sql);
405+
foreach ($cmsuserlistdata as $cmsuserlist) {
406+
if (!empty($userlist[$cmsuserlist->instanceid])) {
407+
if ($userlist[$cmsuserlist->instanceid]['contextid'] != $cmsuserlist->contextid) {
408+
$DB->set_field('customfield_data', 'contextid', $userlist[$cmsuserlist->instanceid]['contextid'],
409+
['id' => $cmsuserlist->id]);
410+
}
411+
}
412+
}
413+
upgrade_mod_savepoint(true, 2023120103, 'cms');
414+
}
415+
372416
return true;
373417
}

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
defined('MOODLE_INTERNAL') || die();
2727

28-
$plugin->version = 2023120102;
28+
$plugin->version = 2023120103;
2929
$plugin->requires = 2020061500; // Moodle 3.9.0 and above.
3030
$plugin->supported = [39, 401]; // Moodle 3.9 to 4.1 inclusive.
3131
$plugin->component = 'mod_cms';

0 commit comments

Comments
 (0)