forked from andrewhancox/moodle-customfield_file
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue andrewhancox#4 Use course context for file record
- Loading branch information
1 parent
11a770a
commit f807e10
Showing
5 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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 customfield_file\task; | ||
|
||
use core\task\adhoc_task; | ||
|
||
/** | ||
* Runs file context update. | ||
* | ||
* @package customfield_file | ||
* @author Tomo Tsuyuki <[email protected]> | ||
* @copyright 2024 Catalyst IT | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class update_file_context extends adhoc_task { | ||
|
||
/** | ||
* Run the task. | ||
* | ||
* @return void | ||
*/ | ||
public function execute() { | ||
global $DB; | ||
|
||
// Collect records which have wrong contextid in the file data. | ||
$sql = "SELECT mf.id mfid, mcd.contextid mcdcontextid, mf.itemid, mf.filepath, mf.filename | ||
FROM {customfield_data} mcd | ||
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid | ||
JOIN {customfield_category} mcc ON mcc.id = mcf.categoryid | ||
JOIN {files} mf ON mf.itemid = mcd.id | ||
WHERE mcc.component = 'core_course' AND mcc.area = 'course' | ||
AND mcf.type = 'file' | ||
AND mf.component = 'customfield_file' AND mf.filearea = 'value' | ||
AND mcd.contextid != mf.contextid"; | ||
$records = $DB->get_records_sql($sql); | ||
$fs = get_file_storage(); | ||
|
||
// Update records with correct contextid and pathnamehash. | ||
foreach ($records as $record) { | ||
$DB->set_field('files', 'contextid', $record->mcdcontextid, ['id' => $record->mfid]); | ||
$pathnamehash = $fs->get_pathname_hash($record->mcdcontextid, 'customfield_file', 'value', | ||
$record->itemid, $record->filepath, $record->filename); | ||
$DB->set_field('files', 'pathnamehash', $pathnamehash, ['id' => $record->mfid]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?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 <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Database upgrade script | ||
* | ||
* @package customfield_file | ||
* @author Tomo Tsuyuki <[email protected]> | ||
* @copyright 2024 Catalyst IT | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
use core\task\manager; | ||
use customfield_file\task\update_file_context; | ||
|
||
/** | ||
* Function to upgrade customfield database | ||
* | ||
* @param int $oldversion the version we are upgrading from | ||
* @return bool result | ||
*/ | ||
function xmldb_customfield_file_upgrade($oldversion) { | ||
|
||
if ($oldversion < 2023121401) { | ||
// Add adhoc task to update contextid in file records. | ||
manager::queue_adhoc_task(new update_file_context()); | ||
upgrade_plugin_savepoint(true, 2023121401, 'customfield', 'file'); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters