Skip to content

Commit ac6bb99

Browse files
TomoTsuyukiFragonite
authored andcommitted
Issue #121 Move upgrade script to adhoc task.
1 parent 7f2cc20 commit ac6bb99

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_cms\task;
18+
19+
use core\task\adhoc_task;
20+
21+
/**
22+
* Runs customfield context update.
23+
*
24+
* @package mod_cms
25+
* @author Tomo Tsuyuki <[email protected]>
26+
* @copyright 2023 Catalyst IT
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class update_customfield_context extends adhoc_task {
30+
31+
/**
32+
* Run the task.
33+
*
34+
* @return void
35+
*/
36+
public function execute() {
37+
global $DB;
38+
39+
// Collect records which have wrong contextid in the customfield data.
40+
$sql = "SELECT mcd.id mcdid, mcd.contextid mcdcontextid, mc.id mcid
41+
FROM {customfield_data} mcd
42+
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
43+
JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid
44+
JOIN {course_modules} mcm ON mcm.instance = mcd.instanceid AND mcm.module = (
45+
SELECT id FROM {modules} WHERE name = 'cms'
46+
)
47+
JOIN {context} mc ON mc.instanceid = mcm.id AND contextlevel = " . CONTEXT_MODULE . "
48+
WHERE mcc.component = 'mod_cms' AND mcd.contextid != mc.id";
49+
$records = $DB->get_records_sql($sql);
50+
// Update records with correct contextid.
51+
foreach ($records as $record) {
52+
$DB->set_field('customfield_data', 'contextid', $record->mcid, ['id' => $record->mcdid]);
53+
}
54+
}
55+
}

db/upgrade.php

+4-15
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2424
*/
2525

26+
use core\task\manager;
2627
use mod_cms\local\lib;
2728
use mod_cms\local\model\cms_types;
2829
use mod_cms\local\model\cms;
2930
use mod_cms\local\datasource\fields;
3031
use mod_cms\local\datasource\userlist;
32+
use mod_cms\task\update_customfield_context;
3133

3234
/**
3335
* Function to upgrade mod_cms database
@@ -342,21 +344,8 @@ function xmldb_cms_upgrade($oldversion) {
342344
}
343345

344346
if ($oldversion < 2023120101) {
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-
}
347+
// Add adhoc task to update contextid in customfield records.
348+
manager::queue_adhoc_task(new update_customfield_context());
360349
upgrade_mod_savepoint(true, 2023120101, 'cms');
361350
}
362351

0 commit comments

Comments
 (0)