Skip to content

Commit

Permalink
Fix issue andrewhancox#4 Use course context for file record
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Dec 12, 2024
1 parent 11a770a commit f807e10
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
6 changes: 3 additions & 3 deletions classes/data_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function instance_form_save(stdClass $datanew) {
$this->save();
}

$context = $this->get_field()->get_handler()->get_configuration_context();
$context = $this->get_context();
file_save_draft_area_files(
$datanew->{$fieldname},
$context->id,
Expand All @@ -96,7 +96,7 @@ public function get_default_value() {
}

public function instance_form_before_set_data($data) {
$context = $this->get_field()->get_handler()->get_configuration_context();
$context = $this->get_context();
$draftideditor = file_get_submitted_draft_itemid($this->get_form_element_name());
file_prepare_draft_area($draftideditor, $context->id, 'customfield_file',
'value', $this->get('id'), $this->get_filemanageroptions());
Expand All @@ -111,7 +111,7 @@ public function instance_form_before_set_data($data) {
public function export_value() {
global $OUTPUT, $PAGE;

$context = $this->get_field()->get_handler()->get_configuration_context();
$context = $this->get_context();
$fs = get_file_storage();

$files = $fs->get_area_files($context->id, 'customfield_file', "value",
Expand Down
4 changes: 0 additions & 4 deletions classes/field_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public function delete(): bool {
global $DB;
$fs = get_file_storage();

// Delete files in the defaultvalue.
$fs->delete_area_files($this->get_handler()->get_configuration_context()->id, 'customfield_file',
'defaultvalue', $this->get('id'));

// Delete files in the data. We can not use $fs->delete_area_files_select() because context may be different.
$params = ['component' => 'customfield_file', 'filearea' => 'value', 'fieldid' => $this->get('id')];
$where = "component = :component AND filearea = :filearea
Expand Down
60 changes: 60 additions & 0 deletions classes/task/update_file_context.php
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]);
}
}
}
44 changes: 44 additions & 0 deletions db/upgrade.php
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;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'customfield_file';
$plugin->version = 2023121400;
$plugin->version = 2023121401;
$plugin->requires = 2020060900;
$plugin->release = 2020061501;
$plugin->maturity = MATURITY_STABLE;

0 comments on commit f807e10

Please sign in to comment.