Skip to content

Commit 485bc50

Browse files
committed
Change refence of file.itemid to questionnaire_response_file.id
1 parent c4f93c5 commit 485bc50

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

classes/question/file.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function question_survey_display($formdata, $descendantsdata, $blankqu
8181
$elname = 'q' . $this->id;
8282
$draftitemid = file_get_submitted_draft_itemid($elname);
8383
$component = 'mod_questionnaire';
84-
$options = $this->get_file_manager_option();
84+
$options = self::get_file_manager_option();
8585
if ($draftitemid > 0) {
8686
file_prepare_draft_area($draftitemid, $this->context->id, $component, 'file', $this->id, $options);
8787
} else {
@@ -114,7 +114,7 @@ protected function question_survey_display($formdata, $descendantsdata, $blankqu
114114
*
115115
* @return array
116116
*/
117-
private function get_file_manager_option() {
117+
public static function get_file_manager_option() {
118118
return [
119119
'mainfile' => '',
120120
'subdirs' => false,

classes/responsetype/file.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ public static function answers_from_webform($responsedata, $question) {
4343
$record->responseid = $responsedata->rid;
4444
$record->questionid = $question->id;
4545

46-
file_save_draft_area_files($val, $question->context->id, 'mod_questionnaire', 'file', $question->id);
46+
file_save_draft_area_files($val, $question->context->id,
47+
'mod_questionnaire', 'file', $val,
48+
\mod_questionnaire\question\file::get_file_manager_option());
4749
$fs = get_file_storage();
48-
$files = $fs->get_area_files($question->context->id, 'mod_questionnaire', 'file', $question->id,
50+
$files = $fs->get_area_files($question->context->id, 'mod_questionnaire',
51+
'file', $val,
4952
"itemid, filepath, filename",
5053
false);
5154
if (!empty($files)) {
@@ -132,10 +135,44 @@ public function insert_response($responsedata) {
132135
$record->question_id = $this->question->id;
133136
$record->fileid = intval(clean_text($response->answers[$this->question->id][0]->value));
134137

135-
return $DB->insert_record(static::response_table(), $record);
136-
} else {
137-
return false;
138+
// When saving the draft file, the itemid was the same as the draftfileid. This must now be
139+
// corrected to the primary key that is questionaire_response_file.id to have a correct reference.
140+
$recordid = $DB->insert_record(static::response_table(), $record);
141+
if ($recordid) {
142+
$olditem = $DB->get_record('files', ['id' => $record->fileid], 'itemid');
143+
if (!$olditem) {
144+
return false;
145+
}
146+
$siblings = $DB->get_records('files',
147+
['component' => 'mod_questionnaire', 'itemid' => $olditem->itemid]);
148+
foreach ($siblings as $sibling) {
149+
if (!$this->fix_file_itemid($recordid, $sibling)) {
150+
return false;
151+
}
152+
}
153+
return $recordid;
154+
}
138155
}
156+
return false;
157+
}
158+
159+
/**
160+
* Update records in the table file with the new given itemid. To do this, the pathnamehash
161+
* needs to be recalculated as well.
162+
* @param int $recordid
163+
* @param \stdClass $filerecord
164+
* @return bool
165+
* @throws \dml_exception
166+
*/
167+
protected function fix_file_itemid(int $recordid, \stdClass $filerecord): bool {
168+
global $DB;
169+
$fs = get_file_storage();
170+
$file = $fs->get_file_instance($filerecord);
171+
$newhash = $fs->get_pathname_hash($filerecord->contextid, $filerecord->component,
172+
$filerecord->filearea, $recordid, $file->get_filepath(), $file->get_filename());
173+
$filerecord->itemid = $recordid;
174+
$filerecord->pathnamehash = $newhash;
175+
return $DB->update_record('files', $filerecord);
139176
}
140177

141178
/**

lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $for
555555
return false;
556556
}
557557
} else if ($filearea == 'file') {
558-
if (!$DB->record_exists('questionnaire_response_file', ['response_id' => $componentid])) {
558+
if (!$DB->record_exists('questionnaire_response_file', ['id' => $componentid])) {
559559
return false;
560560
}
561561
} else {

0 commit comments

Comments
 (0)