Skip to content

Commit

Permalink
Change refence of file.itemid to questionnaire_response_file.id
Browse files Browse the repository at this point in the history
  • Loading branch information
srobotta authored and lucaboesch committed Apr 4, 2024
1 parent e4edc48 commit 79f2b73
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions classes/question/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function question_survey_display($formdata, $descendantsdata, $blankqu
$elname = 'q' . $this->id;
$draftitemid = file_get_submitted_draft_itemid($elname);
$component = 'mod_questionnaire';
$options = $this->get_file_manager_option();
$options = self::get_file_manager_option();
if ($draftitemid > 0) {
file_prepare_draft_area($draftitemid, $this->context->id, $component, 'file', $this->id, $options);
} else {
Expand Down Expand Up @@ -114,7 +114,7 @@ protected function question_survey_display($formdata, $descendantsdata, $blankqu
*
* @return array
*/
private function get_file_manager_option() {
public static function get_file_manager_option() {
return [
'mainfile' => '',
'subdirs' => false,
Expand Down
47 changes: 42 additions & 5 deletions classes/responsetype/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ public static function answers_from_webform($responsedata, $question) {
$record->responseid = $responsedata->rid;
$record->questionid = $question->id;

file_save_draft_area_files($val, $question->context->id, 'mod_questionnaire', 'file', $question->id);
file_save_draft_area_files($val, $question->context->id,
'mod_questionnaire', 'file', $val,
\mod_questionnaire\question\file::get_file_manager_option());
$fs = get_file_storage();
$files = $fs->get_area_files($question->context->id, 'mod_questionnaire', 'file', $question->id,
$files = $fs->get_area_files($question->context->id, 'mod_questionnaire',
'file', $val,
"itemid, filepath, filename",
false);
if (!empty($files)) {
Expand Down Expand Up @@ -132,10 +135,44 @@ public function insert_response($responsedata) {
$record->question_id = $this->question->id;
$record->fileid = intval(clean_text($response->answers[$this->question->id][0]->value));

return $DB->insert_record(static::response_table(), $record);
} else {
return false;
// When saving the draft file, the itemid was the same as the draftfileid. 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) {
$olditem = $DB->get_record('files', ['id' => $record->fileid], 'itemid');
if (!$olditem) {
return false;
}
$siblings = $DB->get_records('files',
['component' => 'mod_questionnaire', 'itemid' => $olditem->itemid]);
foreach ($siblings as $sibling) {
if (!$this->fix_file_itemid($recordid, $sibling)) {
return false;
}
}
return $recordid;
}
}
return false;
}

/**
* Update records in the table file with the new given itemid. To do this, the pathnamehash
* needs to be recalculated as well.
* @param int $recordid
* @param \stdClass $filerecord
* @return bool
* @throws \dml_exception
*/
protected function fix_file_itemid(int $recordid, \stdClass $filerecord): bool {
global $DB;
$fs = get_file_storage();
$file = $fs->get_file_instance($filerecord);
$newhash = $fs->get_pathname_hash($filerecord->contextid, $filerecord->component,
$filerecord->filearea, $recordid, $file->get_filepath(), $file->get_filename());
$filerecord->itemid = $recordid;
$filerecord->pathnamehash = $newhash;
return $DB->update_record('files', $filerecord);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $for
return false;
}
} else if ($filearea == 'file') {
if (!$DB->record_exists('questionnaire_response_file', ['response_id' => $componentid])) {
if (!$DB->record_exists('questionnaire_response_file', ['id' => $componentid])) {
return false;
}
} else {
Expand Down

0 comments on commit 79f2b73

Please sign in to comment.