Skip to content

Commit

Permalink
Issue #121 Update contextid for userlist customfield records.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki authored and Fragonite committed Aug 29, 2024
1 parent 18d02d0 commit f760b19
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
5 changes: 4 additions & 1 deletion classes/task/update_customfield_context.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class update_customfield_context extends adhoc_task {
public function execute() {
global $DB;

// Record {customfield_data}.instanceid is from {cms}.id.
// Record {customfield_data}.contextid is from contextid of {course_modules}, which is linked to {cms}.

// Collect records which have wrong contextid in the customfield data.
$sql = "SELECT mcd.id mcdid, mcd.contextid mcdcontextid, mc.id mcid
FROM {customfield_data} mcd
Expand All @@ -45,7 +48,7 @@ public function execute() {
SELECT id FROM {modules} WHERE name = 'cms'
)
JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = " . CONTEXT_MODULE . "
WHERE mcc.component = 'mod_cms' AND mcd.contextid != mc.id";
WHERE mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcd.contextid != mc.id";
$records = $DB->get_records_sql($sql);
// Update records with correct contextid.
foreach ($records as $record) {
Expand Down
48 changes: 46 additions & 2 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,14 @@ function xmldb_cms_upgrade($oldversion) {
}

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

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

if ($oldversion < 2023120103) {
// Update contextid in customfield records for 'cmsuserlist' type.
// {customfield_data}.instanceid" is from one of id from userlistinstanceids which is JSON encoded in {cms}.customdata.
// "userlistinstanceids" is a unique id for the customfield_data.
// New ID is from userlistmaxinstanceid which is JSON encoded in the {cms_types}.customdata" and add 1 to use.
$sql = "SELECT mc.id, mc.customdata, mcx.id contextid
FROM {cms} mc
JOIN {course_modules} mcm ON mc.id = mcm.instance AND mcm.module = (
SELECT id FROM {modules} WHERE name = 'cms'
)
JOIN {context} mcx ON mcx.instanceid = mcm.id AND mcx.contextlevel = " . CONTEXT_MODULE . "
WHERE mc.customdata LIKE '%userlistinstanceids%'";
$cmsrecords = $DB->get_records_sql($sql);

// Load userlist from customfield_data and set to array.
$userlist = [];
foreach ($cmsrecords as $cmsrecord) {
$customdata = json_decode($cmsrecord->customdata);
foreach ($customdata->userlistinstanceids as $instanceid) {
$userlist[$instanceid] = [
'id' => $cmsrecord->id,
'contextid' => $cmsrecord->contextid,
];
}
}

// Check cmsuserlist records and set correct contextid if it's different one.
$sql = "SELECT mcd.id, mcd.instanceid, mcd.contextid
FROM {customfield_data} mcd
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid
WHERE mcc.component = 'mod_cms' AND mcc.area = 'cmsuserlist'";
$cmsuserlistdata = $DB->get_records_sql($sql);
foreach ($cmsuserlistdata as $cmsuserlist) {
if (!empty($userlist[$cmsuserlist->instanceid])) {
if ($userlist[$cmsuserlist->instanceid]['contextid'] != $cmsuserlist->contextid) {
$DB->set_field('customfield_data', 'contextid', $userlist[$cmsuserlist->instanceid]['contextid'],
['id' => $cmsuserlist->id]);
}
}
}
upgrade_mod_savepoint(true, 2023120103, 'cms');
}

return true;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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

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

0 comments on commit f760b19

Please sign in to comment.