|
| 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 | +} |
0 commit comments