Skip to content
9 changes: 9 additions & 0 deletions backup/moodle2/backup_questionnaire_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ protected function define_structure() {

$responsedate = new backup_nested_element('response_date', ['id'], ['response_id', 'question_id', 'response']);

$responsefiles = new backup_nested_element('response_files');

$responsefile = new backup_nested_element('response_file', ['id'], ['response_id', 'question_id', 'fileid']);

$responsemultiples = new backup_nested_element('response_multiples');

$responsemultiple = new backup_nested_element('response_multiple', ['id'], [
Expand Down Expand Up @@ -215,6 +219,9 @@ protected function define_structure() {
$response->add_child($responsedates);
$responsedates->add_child($responsedate);

$response->add_child($responsefiles);
$responsefiles->add_child($responsefile);

$response->add_child($responsemultiples);
$responsemultiples->add_child($responsemultiple);

Expand Down Expand Up @@ -256,6 +263,8 @@ protected function define_structure() {
$response->set_source_table('questionnaire_response', ['questionnaireid' => backup::VAR_PARENTID]);
$responsebool->set_source_table('questionnaire_response_bool', ['response_id' => backup::VAR_PARENTID]);
$responsedate->set_source_table('questionnaire_response_date', ['response_id' => backup::VAR_PARENTID]);
$responsefile->set_source_table('questionnaire_response_file', ['response_id' => backup::VAR_PARENTID]);
$responsefile->annotate_files('mod_questionnaire', 'response_file', null);
$responsemultiple->set_source_table('questionnaire_resp_multiple', ['response_id' => backup::VAR_PARENTID]);
$responseother->set_source_table('questionnaire_response_other', ['response_id' => backup::VAR_PARENTID]);
$responserank->set_source_table('questionnaire_response_rank', ['response_id' => backup::VAR_PARENTID]);
Expand Down
84 changes: 84 additions & 0 deletions backup/moodle2/restore_questionnaire_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class restore_questionnaire_activity_structure_step extends restore_activity_str
*/
protected $olddependencies = [];

/**
* @var array $responsefileids Contains new questionnaire_response_file.id in key and old id in value.
*/
protected $responsefileids = [];

/**
* Implementation of define_structure.
* @return mixed
Expand Down Expand Up @@ -127,6 +132,10 @@ protected function define_structure() {
'questionnaire_response_date',
'/activity/questionnaire/responses/response/response_dates/response_date'
);
$paths[] = new restore_path_element(
'questionnaire_response_file',
'/activity/questionnaire/responses/response/response_files/response_file'
);
$paths[] = new restore_path_element(
'questionnaire_response_multiple',
'/activity/questionnaire/responses/response/response_multiples/response_multiple'
Expand Down Expand Up @@ -381,6 +390,22 @@ protected function process_questionnaire_response_date($data) {
$DB->insert_record('questionnaire_response_date', $data);
}

/**
* Process file responses.
* @param array $data
* @throws dml_exception
*/
protected function process_questionnaire_response_file($data) {
Comment thread
mchurchward marked this conversation as resolved.
global $DB;

$data = (object)$data;
$data->response_id = $this->get_new_parentid('questionnaire_response');
$data->question_id = $this->get_mappingid('questionnaire_question', $data->question_id);
// Insert the questionnaire_response_file record.
$newid = $DB->insert_record('questionnaire_response_file', $data);
$this->responsefileids[$newid] = $data->id;
}

/**
* Process multiple responses.
* @param array $data
Expand Down Expand Up @@ -470,6 +495,64 @@ protected function process_questionnaire_response_text($data) {
$DB->insert_record('questionnaire_response_text', $data);
}

/**
* Handle related files for response file assignments and update the references between the
* records in the questionnaire_response_file and the files table.
*/
protected function handle_releated_files_for_response_file_assignments() {
global $DB;
// If there are no response file IDs mapped before, then there are no response files to handle.
if (empty($this->responsefileids)) {
return;
}
$newctx = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $this->task->get_old_contextid());
restore_dbops::send_files_to_pool(
$this->get_basepath(),
$this->get_restoreid(),
'mod_questionnaire',
'response_file',
$this->task->get_old_contextid(),
$this->task->get_userid()
);
// Lookup all file records that where just created for this context and component/filearea.
[$sql, $params] = $DB->get_in_or_equal(array_values($this->responsefileids), SQL_PARAMS_NAMED, 'id');
$params['component'] = 'mod_questionnaire';
$params['filearea'] = 'response_file';
$params['ctx'] = $newctx->newitemid;
$filerecords = $DB->get_records_select(
'files',
'contextid = :ctx AND component = :component AND filearea = :filearea AND itemid ' . $sql,
$params
);
$oldtonew = array_flip($this->responsefileids);
foreach ($filerecords as $filerecord) {
if (array_key_exists($filerecord->itemid, $oldtonew)) {
if ($filerecord->filename != '.') {
// We have a real file, use that file.id to update the questionnaire_response_file.fileid.
$DB->set_field(
'questionnaire_response_file',
'fileid',
$filerecord->id,
['id' => $oldtonew[$filerecord->itemid]]
);
}
// Update the itemid to the new response_file.id.
$filerecord->itemid = $oldtonew[$filerecord->itemid];
// Calculate new pathnamehash.
$filerecord->pathnamehash = \file_storage::get_pathname_hash(
$filerecord->contextid,
$filerecord->component,
$filerecord->filearea,
$filerecord->itemid,
$filerecord->filepath,
$filerecord->filename
);
// Adjust the files record with the new itemid and pathnamehash.
$DB->update_record('files', $filerecord);
}
}
}

/**
* Stuff to do after execution.
*/
Expand Down Expand Up @@ -520,6 +603,7 @@ protected function after_execute() {
$this->add_related_files('mod_questionnaire', 'question', 'questionnaire_question');
$this->add_related_files('mod_questionnaire', 'sectionheading', 'questionnaire_fb_sections');
$this->add_related_files('mod_questionnaire', 'feedback', 'questionnaire_feedback');
$this->handle_releated_files_for_response_file_assignments();

// Process any old rate question named degree choices after all questions and choices have been restored.
if ($this->task->get_old_moduleversion() < 2018110103) {
Expand Down
1 change: 1 addition & 0 deletions classes/feedback_section_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->dirroot . '/mod/questionnaire/lib.php');

#[\AllowDynamicProperties]
/**
* Print the form to add or edit a questionnaire-instance
*
Expand Down
2 changes: 1 addition & 1 deletion classes/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace mod_questionnaire;

/**
* Defines the file stoeage class for questionnaire.
* Defines the file storage class for questionnaire.
* @package mod_questionnaire
* @copyright 2020 onwards Mike Churchward (mike.churchward@poetopensource.org)
* @author Mike Churchward
Expand Down
1 change: 1 addition & 0 deletions classes/output/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public function print_preview_formend($url, $submitstr, $resetstr) {
$output .= \html_writer::start_tag('div');
$output .= \html_writer::empty_tag('input', ['type' => 'submit', 'name' => 'submit', 'value' => $submitstr,
'class' => 'btn btn-primary']);
$output .= \html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
$output .= ' ';
$output .= \html_writer::tag('a', $resetstr, ['href' => $url, 'class' => 'btn btn-secondary me-1']);
$output .= \html_writer::end_tag('div') . "\n";
Expand Down
Loading