Skip to content

Commit 1df20f1

Browse files
committed
update existing files.itemid to questionnaire_response_file.id
1 parent 485bc50 commit 1df20f1

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

classes/responsetype/file.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function insert_response($responsedata) {
135135
$record->question_id = $this->question->id;
136136
$record->fileid = intval(clean_text($response->answers[$this->question->id][0]->value));
137137

138-
// When saving the draft file, the itemid was the same as the draftfileid. This must now be
138+
// When saving the draft file, the itemid was the same as the draftitemid. This must now be
139139
// corrected to the primary key that is questionaire_response_file.id to have a correct reference.
140140
$recordid = $DB->insert_record(static::response_table(), $record);
141141
if ($recordid) {
@@ -146,7 +146,7 @@ public function insert_response($responsedata) {
146146
$siblings = $DB->get_records('files',
147147
['component' => 'mod_questionnaire', 'itemid' => $olditem->itemid]);
148148
foreach ($siblings as $sibling) {
149-
if (!$this->fix_file_itemid($recordid, $sibling)) {
149+
if (!self::fix_file_itemid($recordid, $sibling)) {
150150
return false;
151151
}
152152
}
@@ -164,8 +164,11 @@ public function insert_response($responsedata) {
164164
* @return bool
165165
* @throws \dml_exception
166166
*/
167-
protected function fix_file_itemid(int $recordid, \stdClass $filerecord): bool {
167+
public static function fix_file_itemid(int $recordid, \stdClass $filerecord): bool {
168168
global $DB;
169+
if ((int)$filerecord->itemid === $recordid) {
170+
return true; // Reference is already good, nothing to do.
171+
}
169172
$fs = get_file_storage();
170173
$file = $fs->get_file_instance($filerecord);
171174
$newhash = $fs->get_pathname_hash($filerecord->contextid, $filerecord->component,

db/upgrade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,20 @@ function xmldb_questionnaire_upgrade($oldversion=0) {
10351035
upgrade_mod_savepoint(true, 2023101500, 'questionnaire');
10361036
}
10371037

1038+
if ($oldversion < 2023101501) {
1039+
// Upgrade files.itemid with questionnaire_response_file.id
1040+
$filesresponses = $DB->get_records('questionnaire_response_file', [], '', 'id,fileid');
1041+
$idmap = [];
1042+
foreach ($filesresponses as $fileresponse) {
1043+
$idmap[(int)$fileresponse->fileid] = (int)$fileresponse->id;
1044+
}
1045+
$filerecords = $DB->get_records_list('files', 'id', array_keys($idmap), 'id desc');
1046+
foreach ($filerecords as $filerecord) {
1047+
\mod_questionnaire\responsetype\file::fix_file_itemid($idmap[(int)$filerecord->id], $filerecord);
1048+
}
1049+
upgrade_mod_savepoint(true, 2023101501, 'questionnaire');
1050+
}
1051+
10381052
return true;
10391053
}
10401054

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

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

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

3131
$plugin->component = 'mod_questionnaire';
3232

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

0 commit comments

Comments
 (0)