Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #121 Update old records from system context to cm context. #131

Merged
merged 8 commits into from
Dec 10, 2024
58 changes: 58 additions & 0 deletions classes/task/update_customfield_context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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;

// 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
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 mcc.area = 'cmsfield' 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]);
}
}
}
157 changes: 157 additions & 0 deletions classes/task/update_files_context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace mod_cms\task;

use core\task\adhoc_task;
use csv_export_writer;
use file_storage;
use moodle_exception;

/**
* Update the context of embedded files to match the context of the mod_cms module instance.
*
* @package mod_cms
* @author Alexander Van der Bellen <[email protected]>
* @copyright 2024 Catalyst IT Australia
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update_files_context extends adhoc_task {

/**
* Factory method to create a new update_files_context task.
*
* @param int|null $courseid Limit the task to a specific course or all courses if null.
* @param bool $dryrun Whether to run the task without making database changes.
* @return update_files_context The task instance.
*/
public static function instance(?int $courseid = null, bool $dryrun = true): update_files_context {
$task = new self();
$task->set_custom_data((object) ['courseid' => $courseid, 'dryrun' => $dryrun]);
return $task;
}

/**
* Run the task to delete course results for a user.
*/
public function execute(): void {
global $CFG;
$data = $this->get_custom_data();
$csv = self::update_contexts($data->courseid, $data->dryrun);
file_put_contents($CFG->dataroot . '/' . $csv->filename, $csv->print_csv_data(true));
}

/**
* Update mod_cms customfield_textarea embedded file contexts to match the context of the mod_cms module instance.
* @param int|null $courseid Limit the task to a specific course or all courses if null.
* @param bool $dryrun Whether to run the task without making database changes.
* @return csv_export_writer The CSV export writer containing the results of the task.
*/
private static function update_contexts(?int $courseid, bool $dryrun): csv_export_writer {
global $CFG, $DB;

require_once($CFG->libdir . '/csvlib.class.php');

$sql = "SELECT f.*, ctx.id AS ctxid, cms.course AS courseid
FROM {files} f
JOIN {customfield_data} cfd ON cfd.id = f.itemid
JOIN {customfield_field} cff ON cff.id = cfd.fieldid
JOIN {cms} cms ON cms.id = cfd.instanceid
JOIN {cms_types} cmst ON cmst.id = cms.typeid AND cmst.datasources LIKE '%fields%'
JOIN {course_modules} cm ON cm.instance = cms.id
JOIN {modules} m ON m.id = cm.module AND m.name = 'cms'
JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :modulecontextlevel
WHERE f.contextid = 1 AND f.component = 'customfield_textarea' AND f.filearea = 'value'";

$params = ['modulecontextlevel' => CONTEXT_MODULE];

if (!empty($courseid)) {
$sql .= " AND cms.course = :courseid";
$params['courseid'] = $courseid;
}

$csv = new csv_export_writer();
$csv->set_filename('mod_cms_update_files_context');
$csv->add_data([
'courseid',
'newpathnamehash',
'newcontextid',
'id',
'contenthash',
'pathnamehash',
'contextid',
'component',
'filearea',
'itemid',
'filepath',
'filename',
'timecreated',
'timemodified',
]);

$records = $DB->get_recordset_sql($sql, $params);
foreach ($records as $record) {
$newcontextid = $record->ctxid;

$newpathnamehash = file_storage::get_pathname_hash(
$newcontextid,
$record->component,
$record->filearea,
$record->itemid,
$record->filepath,
$record->filename
);

$csv->add_data([
$record->courseid,
$newpathnamehash,
$newcontextid,
$record->id,
$record->contenthash,
$record->pathnamehash,
$record->contextid,
$record->component,
$record->filearea,
$record->itemid,
$record->filepath,
$record->filename,
$record->timecreated,
$record->timemodified,
]);

// Update the record with the new context id and path name hash.
$record->contextid = $newcontextid;
$record->pathnamehash = $newpathnamehash;

if (!$dryrun) {
// Remove the ctxid and courseid fields.
unset($record->ctxid);
unset($record->courseid);

// Update the record in the database.
try {
$DB->update_record('files', $record);
} catch (moodle_exception $e) {
debugging('Failed to insert record into files table: ' . $e->getMessage(), DEBUG_DEVELOPER);
}
}
}

$records->close();

return $csv;
}
}
80 changes: 80 additions & 0 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 @@ -341,5 +343,83 @@ function xmldb_cms_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2023112100, 'cms');
}

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

if ($oldversion < 2024090302) {
$dbman = $DB->get_manager();
// 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
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, 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);
}
}
upgrade_mod_savepoint(true, 2024090302, 'cms');
}

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

if ($oldversion < 2024090305) {
// Run ad hoc task for updating contextid in the files table.
$task = \mod_cms\task\update_files_context::instance(null, false);
manager::queue_adhoc_task($task);

upgrade_mod_savepoint(true, 2024090305, '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 = 2024090301;
$plugin->version = 2024090305;
$plugin->requires = 2022112800; // Moodle 4.1 and above.
$plugin->supported = [401, 401]; // Moodle 4.1.
$plugin->component = 'mod_cms';
Expand Down
Loading