From 7be335841cee16ba201e1fed9d133bb25db8f5fb Mon Sep 17 00:00:00 2001 From: Tomo Tsuyuki Date: Tue, 7 May 2024 16:01:45 +1000 Subject: [PATCH 1/6] Issue #121 Update old records from system context to cm context. --- db/upgrade.php | 30 ++++++++++++++++++++++++++++++ version.php | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/db/upgrade.php b/db/upgrade.php index a584342..f0e2b4b 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -341,5 +341,35 @@ function xmldb_cms_upgrade($oldversion) { upgrade_mod_savepoint(true, 2023112100, 'cms'); } + if ($oldversion < 2023120101) { + // Update query for setting. + $sql = "UPDATE {customfield_data} mcd1 + SET contextid = sub.mcid + FROM ( + SELECT mcd.id mcdid, mcd.contextid mcdcontextid, mc.id mcid + FROM {customfield_data} mcd + JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid + JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid + JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = ( + SELECT id FROM {modules} WHERE name = 'cms' + ) + JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = 70 + WHERE mcd.id IN ( + SELECT mcd.id + FROM {customfield_data} mcd + JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid + JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid + JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = ( + SELECT id FROM {modules} WHERE name = 'cms' + ) + JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = 70 + WHERE mcf.type = 'textarea' AND mcc.component = 'mod_cms' AND mcd.contextid != mc.id + ) + ) sub + WHERE mcd1.id = sub.mcdid"; + $DB->execute($sql); + upgrade_mod_savepoint(true, 2023120101, 'cms'); + } + return true; } diff --git a/version.php b/version.php index 17c6f1b..da880d2 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023120100; +$plugin->version = 2023120101; $plugin->requires = 2020061500; // Moodle 3.9.0 and above. $plugin->supported = [39, 401]; // Moodle 3.9 to 4.1 inclusive. $plugin->component = 'mod_cms'; From 7f2cc209d1463f1111950b012c5badba757fa610 Mon Sep 17 00:00:00 2001 From: Tomo Tsuyuki Date: Fri, 10 May 2024 12:09:36 +1000 Subject: [PATCH 2/6] Issue #121 Use Moodle db function to update the records. --- db/upgrade.php | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/db/upgrade.php b/db/upgrade.php index f0e2b4b..58d1002 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -342,32 +342,21 @@ function xmldb_cms_upgrade($oldversion) { } if ($oldversion < 2023120101) { - // Update query for setting. - $sql = "UPDATE {customfield_data} mcd1 - SET contextid = sub.mcid - FROM ( - SELECT mcd.id mcdid, mcd.contextid mcdcontextid, mc.id mcid - FROM {customfield_data} mcd - JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid - JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid - JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = ( - SELECT id FROM {modules} WHERE name = 'cms' - ) - JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = 70 - WHERE mcd.id IN ( - SELECT mcd.id - FROM {customfield_data} mcd - JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid - JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid - JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = ( - SELECT id FROM {modules} WHERE name = 'cms' - ) - JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = 70 - WHERE mcf.type = 'textarea' AND mcc.component = 'mod_cms' AND mcd.contextid != mc.id - ) - ) sub - WHERE mcd1.id = sub.mcdid"; - $DB->execute($sql); + // 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 + JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid + JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid + JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = ( + SELECT id FROM mdl_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"; + $records = $DB->get_records_sql($sql); + // Update records with correct contextid. + foreach ($records as $record) { + $DB->set_field('customfield_data', 'contextid', $record->mcid, ['id' => $record->mcdid]); + } upgrade_mod_savepoint(true, 2023120101, 'cms'); } From ac6bb99f9292a1c3e7be505fda79d57df4908a45 Mon Sep 17 00:00:00 2001 From: Tomo Tsuyuki Date: Mon, 20 May 2024 18:43:53 +1000 Subject: [PATCH 3/6] Issue #121 Move upgrade script to adhoc task. --- classes/task/update_customfield_context.php | 55 +++++++++++++++++++++ db/upgrade.php | 19 ++----- 2 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 classes/task/update_customfield_context.php diff --git a/classes/task/update_customfield_context.php b/classes/task/update_customfield_context.php new file mode 100644 index 0000000..7560d15 --- /dev/null +++ b/classes/task/update_customfield_context.php @@ -0,0 +1,55 @@ +. + +namespace mod_cms\task; + +use core\task\adhoc_task; + +/** + * Runs customfield context update. + * + * @package mod_cms + * @author Tomo Tsuyuki + * @copyright 2023 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class update_customfield_context extends adhoc_task { + + /** + * Run the task. + * + * @return void + */ + public function execute() { + global $DB; + + // 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 + JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid + JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid + JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = ( + 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"; + $records = $DB->get_records_sql($sql); + // Update records with correct contextid. + foreach ($records as $record) { + $DB->set_field('customfield_data', 'contextid', $record->mcid, ['id' => $record->mcdid]); + } + } +} diff --git a/db/upgrade.php b/db/upgrade.php index 58d1002..133c728 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -23,11 +23,13 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use core\task\manager; use mod_cms\local\lib; use mod_cms\local\model\cms_types; use mod_cms\local\model\cms; use mod_cms\local\datasource\fields; use mod_cms\local\datasource\userlist; +use mod_cms\task\update_customfield_context; /** * Function to upgrade mod_cms database @@ -342,21 +344,8 @@ function xmldb_cms_upgrade($oldversion) { } if ($oldversion < 2023120101) { - // 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 - JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid - JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid - JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = ( - SELECT id FROM mdl_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"; - $records = $DB->get_records_sql($sql); - // Update records with correct contextid. - foreach ($records as $record) { - $DB->set_field('customfield_data', 'contextid', $record->mcid, ['id' => $record->mcdid]); - } + // Add adhoc task to update contextid in customfield records. + manager::queue_adhoc_task(new update_customfield_context()); upgrade_mod_savepoint(true, 2023120101, 'cms'); } From c0341a20d38e5607827b5cf683bd8ba6290be749 Mon Sep 17 00:00:00 2001 From: Tomo Tsuyuki Date: Tue, 4 Jun 2024 19:18:27 +1000 Subject: [PATCH 4/6] Issue #121 Update valuetrust if exists. --- db/upgrade.php | 20 ++++++++++++++++++++ version.php | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/db/upgrade.php b/db/upgrade.php index 133c728..d2f7215 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -349,5 +349,25 @@ function xmldb_cms_upgrade($oldversion) { upgrade_mod_savepoint(true, 2023120101, 'cms'); } + if ($oldversion < 2023120102) { + $dbman = $DB->get_manager(); + // Conditionally launch add field valuetrust. + if ($dbman->field_exists('customfield_data', 'valuetrust')) { + $sql = "SELECT mcd.id mcdid + 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 mcd.valuetrust = 0"; + $records = $DB->get_records_sql($sql); + $mcdids = array_keys($records); + foreach (array_chunk($mcdids, 100) as $ids) { + [$sql, $params] = $DB->get_in_or_equal($ids); + $sql = 'UPDATE {customfield_data} SET valuetrust = 1 WHERE id ' . $sql; + $DB->execute($sql, $params); + } + } + upgrade_mod_savepoint(true, 2023120102, 'cms'); + } + return true; } diff --git a/version.php b/version.php index da880d2..23b7bc8 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023120101; +$plugin->version = 2023120102; $plugin->requires = 2020061500; // Moodle 3.9.0 and above. $plugin->supported = [39, 401]; // Moodle 3.9 to 4.1 inclusive. $plugin->component = 'mod_cms'; From 62afeb20a91e0862bbbb2c99146ce2800a17dc9c Mon Sep 17 00:00:00 2001 From: Tomo Tsuyuki Date: Fri, 7 Jun 2024 12:07:39 +1000 Subject: [PATCH 5/6] Issue #121 Update contextid for userlist customfield records. --- classes/task/update_customfield_context.php | 5 ++- db/upgrade.php | 48 ++++++++++++++++++++- version.php | 2 +- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/classes/task/update_customfield_context.php b/classes/task/update_customfield_context.php index 7560d15..cf0bac8 100644 --- a/classes/task/update_customfield_context.php +++ b/classes/task/update_customfield_context.php @@ -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 @@ -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) { diff --git a/db/upgrade.php b/db/upgrade.php index d2f7215..cafb400 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -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 @@ -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; } diff --git a/version.php b/version.php index 23b7bc8..74c78a9 100644 --- a/version.php +++ b/version.php @@ -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'; From 65722ab827a3b6f239d700ef724a4478d374f7a3 Mon Sep 17 00:00:00 2001 From: Tomo Tsuyuki Date: Tue, 11 Jun 2024 10:20:17 +1000 Subject: [PATCH 6/6] Issue #121 Increase chunk from 100 to 1000. --- db/upgrade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/upgrade.php b/db/upgrade.php index cafb400..af44f7b 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -360,7 +360,7 @@ function xmldb_cms_upgrade($oldversion) { WHERE mcc.component = 'mod_cms' AND mcd.valuetrust = 0"; $records = $DB->get_records_sql($sql); $mcdids = array_keys($records); - foreach (array_chunk($mcdids, 100) as $ids) { + foreach (array_chunk($mcdids, 1000) as $ids) { [$sql, $params] = $DB->get_in_or_equal($ids); $sql = 'UPDATE {customfield_data} SET valuetrust = 1 WHERE id ' . $sql; $DB->execute($sql, $params);