Skip to content

Commit

Permalink
Issue #121 Move upgrade script to adhoc task.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Sep 5, 2024
1 parent 0f29c45 commit 57d7398
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
55 changes: 55 additions & 0 deletions classes/task/update_customfield_context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace mod_cms\task;

use core\task\adhoc_task;

/**
* Runs customfield context update.
*
* @package mod_cms
* @author Tomo Tsuyuki <[email protected]>
* @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]);
}
}
}
19 changes: 4 additions & 15 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -342,21 +344,8 @@ function xmldb_cms_upgrade($oldversion) {
}

if ($oldversion < 2024090301) {
// 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, 2024090301, 'cms');
}

Expand Down

0 comments on commit 57d7398

Please sign in to comment.