Skip to content

Commit

Permalink
update existing files.itemid to questionnaire_response_file.id
Browse files Browse the repository at this point in the history
  • Loading branch information
srobotta committed Jan 18, 2024
1 parent 846b26c commit 7cba8d7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
9 changes: 6 additions & 3 deletions classes/responsetype/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function insert_response($responsedata) {
$record->question_id = $this->question->id;
$record->fileid = intval(clean_text($response->answers[$this->question->id][0]->value));

// When saving the draft file, the itemid was the same as the draftfileid. This must now be
// When saving the draft file, the itemid was the same as the draftitemid. This must now be
// corrected to the primary key that is questionaire_response_file.id to have a correct reference.
$recordid = $DB->insert_record(static::response_table(), $record);
if ($recordid) {
Expand All @@ -146,7 +146,7 @@ public function insert_response($responsedata) {
$siblings = $DB->get_records('files',
['component' => 'mod_questionnaire', 'itemid' => $olditem->itemid]);
foreach ($siblings as $sibling) {
if (!$this->fix_file_itemid($recordid, $sibling)) {
if (!self::fix_file_itemid($recordid, $sibling)) {
return false;
}
}
Expand All @@ -164,8 +164,11 @@ public function insert_response($responsedata) {
* @return bool
* @throws \dml_exception
*/
protected function fix_file_itemid(int $recordid, \stdClass $filerecord): bool {
public static function fix_file_itemid(int $recordid, \stdClass $filerecord): bool {
global $DB;
if ((int)$filerecord->itemid === $recordid) {
return true; // Reference is already good, nothing to do.
}
$fs = get_file_storage();
$file = $fs->get_file_instance($filerecord);
$newhash = $fs->get_pathname_hash($filerecord->contextid, $filerecord->component,
Expand Down
13 changes: 13 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,19 @@ function xmldb_questionnaire_upgrade($oldversion=0) {
upgrade_mod_savepoint(true, 2023101500, 'questionnaire');
}

if ($oldversion < 2023101501) {
// Upgrade files.itemid with questionnaire_response_file.id
$filesresponses = $DB->get_records('questionnaire_response_file', [], '', 'id,fileid');
$idmap = [];
foreach ($filesresponses as $fileresponse) {
$idmap[(int)$fileresponse->fileid] = (int)$fileresponse->id;
}
$filerecords = $DB->get_records_list('files', 'id', array_keys($idmap), 'id desc');
foreach ($filerecords as $filerecord) {
\mod_questionnaire\responsetype\file::fix_file_itemid($idmap[(int)$filerecord->id], $filerecord);
}
}

return $result;
}

Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023101500; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2023101501; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2022112800.00; // Moodle version (4.1.0).

$plugin->component = 'mod_questionnaire';

$plugin->release = '4.1.1 (Build - 2023101500)';
$plugin->release = '4.1.1 (Build - 2023101501)';
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 7cba8d7

Please sign in to comment.