diff --git a/backup/moodle2/backup_questionnaire_stepslib.php b/backup/moodle2/backup_questionnaire_stepslib.php index 430fe151f..2a5280c18 100644 --- a/backup/moodle2/backup_questionnaire_stepslib.php +++ b/backup/moodle2/backup_questionnaire_stepslib.php @@ -85,6 +85,10 @@ protected function define_structure() { $responsedate = new backup_nested_element('response_date', array('id'), array('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', array('id'), array( @@ -137,6 +141,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); @@ -178,6 +185,8 @@ protected function define_structure() { $response->set_source_table('questionnaire_response', array('questionnaireid' => backup::VAR_PARENTID)); $responsebool->set_source_table('questionnaire_response_bool', array('response_id' => backup::VAR_PARENTID)); $responsedate->set_source_table('questionnaire_response_date', array('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', array('response_id' => backup::VAR_PARENTID)); $responseother->set_source_table('questionnaire_response_other', array('response_id' => backup::VAR_PARENTID)); $responserank->set_source_table('questionnaire_response_rank', array('response_id' => backup::VAR_PARENTID)); diff --git a/backup/moodle2/restore_questionnaire_stepslib.php b/backup/moodle2/restore_questionnaire_stepslib.php index 769383eac..ea327cd5a 100644 --- a/backup/moodle2/restore_questionnaire_stepslib.php +++ b/backup/moodle2/restore_questionnaire_stepslib.php @@ -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 @@ -92,6 +97,8 @@ protected function define_structure() { '/activity/questionnaire/responses/response/response_bools/response_bool'); $paths[] = new restore_path_element('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'); $paths[] = new restore_path_element('questionnaire_response_other', @@ -336,6 +343,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) { + 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 @@ -425,6 +448,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. */ @@ -473,6 +554,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) { diff --git a/classes/feedback_section_form.php b/classes/feedback_section_form.php index e566d04c5..2c85d7f83 100644 --- a/classes/feedback_section_form.php +++ b/classes/feedback_section_form.php @@ -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 * diff --git a/classes/file_storage.php b/classes/file_storage.php index 329e9dffe..58a5dc102 100644 --- a/classes/file_storage.php +++ b/classes/file_storage.php @@ -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 diff --git a/classes/output/renderer.php b/classes/output/renderer.php index a35cdb944..538deadce 100755 --- a/classes/output/renderer.php +++ b/classes/output/renderer.php @@ -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 mr-1']); $output .= \html_writer::end_tag('div') . "\n"; diff --git a/classes/question/file.php b/classes/question/file.php new file mode 100644 index 000000000..5622b4d48 --- /dev/null +++ b/classes/question/file.php @@ -0,0 +1,227 @@ +. + +namespace mod_questionnaire\question; +use core_media_manager; +use form_filemanager; +use mod_questionnaire\responsetype\response\response; +use moodle_url; +use MoodleQuickForm; + +/** + * This file contains the parent class for text question types. + * + * @author Laurent David + * @author Martin Cornu-Mansuy + * @copyright 2023 onward CALL Learning + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + * @package mod_questionnaire + */ +class file extends question { + /** + * Get name. + * + * @return string + */ + public function helpname() { + return 'file'; + } + + /** + * Override and return a form template if provided. Output of question_survey_display is iterpreted based on this. + * + * @return boolean | string + */ + public function question_template() { + return false; + } + + /** + * Override and return a response template if provided. Output of response_survey_display is iterpreted based on this. + * + * @return boolean | string + */ + public function response_template() { + return false; + } + + /** + * Get response class. + * + * @return object|string + */ + protected function responseclass() { + return '\\mod_questionnaire\\responsetype\\file'; + } + + /** + * Survey display output. + * + * @param response $response + * @param object $descendantsdata + * @param bool $blankquestionnaire + * @return string + */ + protected function question_survey_display($response, $descendantsdata, $blankquestionnaire = false) { + global $CFG, $PAGE; + require_once($CFG->libdir . '/filelib.php'); + + $elname = 'q' . $this->id; + // If there is a response and the response is resumed, get the original itemid. + $itemid = ( + isset($response->answers[$this->id]) && + !empty($response->answers[$this->id]) && + isset($_REQUEST['resume']) && + $_REQUEST['resume'] === '1' + ) ? (int)$response->answers[$this->id][0]->id : 0; + // Prepare the draft area. + $draftitemid = file_get_submitted_draft_itemid($elname); + file_prepare_draft_area( + $draftitemid, + $this->context->id, + 'mod_questionnaire', + 'response_file', + $itemid, + self::get_file_manager_option() + ); + // Filemanager form element implementation is far from optimal, we need to rework this if we ever fix it... + require_once("$CFG->dirroot/lib/form/filemanager.php"); + $fm = new form_filemanager((object)self::get_file_manager_option($draftitemid)); + $output = $PAGE->get_renderer('core', 'files'); + $html = '
' . + $output->render($fm) . + '' . + '
'; + return $html; + } + + /** + * Check question's form data for complete response. + * @param \stdClass $responsedata The data entered into the response. + * @return bool + */ + public function response_complete($responsedata) { + $answered = false; + // If $responsedata is a response object, look through the answers. + if ( + is_a($responsedata, 'mod_questionnaire\responsetype\response\response') && + isset($responsedata->answers[$this->id]) && + !empty($responsedata->answers[$this->id]) + ) { + $answer = reset($responsedata->answers[$this->id]); + $answered = ((int)$answer->value > 0); + } else if (isset($responsedata->{'q' . $this->id})) { // If $responsedata is webform data, check that it is not empty. + $draftitemid = (int)$responsedata->{'q' . $this->id}; + if ($draftitemid > 0) { + $info = file_get_draft_area_info($draftitemid); + $answered = $info['filecount'] > 0; + } + } + return !($this->required() && ($this->deleted == 'n') && !$answered); + } + + /** + * Get file manager options, here allow only one image or pdf file, without + * creating subdirectories. + * + * @param ?int $draftitemid + * @return array + */ + public static function get_file_manager_option(?int $draftitemid = 0) { + return [ + 'subdirs' => false, + 'accepted_types' => ['image', 'document'], + 'maxfiles' => 1, + 'itemid' => $draftitemid, + ]; + } + + /** + * Response display output, e.g. render the file image/pdf or show a linkt to download it. + * + * @param \stdClass $data + * @return string + */ + protected function response_survey_display($data) { + global $PAGE, $CFG; + require_once($CFG->libdir . '/filelib.php'); + require_once($CFG->libdir . '/resourcelib.php'); + if (isset($data->answers[$this->id])) { + $answer = reset($data->answers[$this->id]); + } else { + return ''; + } + $fs = get_file_storage(); + $file = $fs->get_file_by_id($answer->value); + $code = ''; + + if ($file) { + // There is a file. + $moodleurl = moodle_url::make_pluginfile_url( + $file->get_contextid(), + $file->get_component(), + $file->get_filearea(), + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename() + ); + + $mimetype = $file->get_mimetype(); + $title = ''; + + $mediamanager = core_media_manager::instance($PAGE); + $embedoptions = [ + core_media_manager::OPTION_TRUSTED => true, + core_media_manager::OPTION_BLOCK => true, + ]; + + if (file_mimetype_in_typegroup($mimetype, 'web_image')) { // It's an image. + $code = resourcelib_embed_image($moodleurl->out(), $title); + } else if ($mimetype === 'application/pdf') { // PDF document. + $code = resourcelib_embed_pdf($moodleurl->out(), $title, get_string('view')); + } else if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) { // Media (audio/video) file. + $code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions); + } else { // We need a way to discover if we are loading remote docs inside an iframe. + $moodleurl->param('embed', 1); + // Anything else - just try object tag enlarged as much as possible. + $code = resourcelib_embed_general($moodleurl, $title, get_string('view'), $mimetype); + } + } + return '
' . $code . '
'; + } + + /** + * Add the length element as hidden. + * + * @param \MoodleQuickForm $mform + * @param string $helpname + * @return \MoodleQuickForm + */ + protected function form_length(MoodleQuickForm $mform, $helpname = '') { + return question::form_length_hidden($mform); + } + + /** + * Add the precise element as hidden. + * + * @param \MoodleQuickForm $mform + * @param string $helpname + * @return \MoodleQuickForm + */ + protected function form_precise(MoodleQuickForm $mform, $helpname = '') { + return question::form_precise_hidden($mform); + } +} diff --git a/classes/question/numerical.php b/classes/question/numerical.php index d86bbedbc..994e000b5 100644 --- a/classes/question/numerical.php +++ b/classes/question/numerical.php @@ -83,7 +83,7 @@ protected function question_survey_display($response, $descendantsdata, $blankqu // Numeric. $questiontags = new \stdClass(); $precision = $this->precise; - $a = new \StdClass(); + $a = new \stdClass(); if (isset($response->answers[$this->id][0])) { $mynumber = $response->answers[$this->id][0]->value; if ($mynumber != '') { diff --git a/classes/question/question.php b/classes/question/question.php index 86690220a..72eb03c47 100644 --- a/classes/question/question.php +++ b/classes/question/question.php @@ -42,6 +42,7 @@ define('QUESDATE', 9); define('QUESNUMERIC', 10); define('QUESSLIDER', 11); +define('QUESFILE', 12); define('QUESPAGEBREAK', 99); define('QUESSECTIONTEXT', 100); @@ -122,6 +123,7 @@ abstract class question { QUESDROP => 'drop', QUESRATE => 'rate', QUESDATE => 'date', + QUESFILE => 'file', QUESNUMERIC => 'numerical', QUESPAGEBREAK => 'pagebreak', QUESSECTIONTEXT => 'sectiontext', diff --git a/classes/responsetype/file.php b/classes/responsetype/file.php new file mode 100644 index 000000000..116afa16c --- /dev/null +++ b/classes/responsetype/file.php @@ -0,0 +1,450 @@ +. + +namespace mod_questionnaire\responsetype; + +use mod_questionnaire\db\bulk_sql_config; +use moodle_url; + +/** + * Class for text response types. + * + * @author Laurent David + * @author Martin Cornu-Mansuy + * @copyright 2023 onward CALL Learning + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + * @package mod_questionnaire + */ +class file extends responsetype { + /** + * Provide an array of answer objects from web form data for the question. + * + * @param \stdClass $responsedata All of the responsedata as an object. + * @param \mod_questionnaire\question\question $question + * @return array \mod_questionnaire\responsetype\answer\answer An array of answer objects. + */ + public static function answers_from_webform($responsedata, $question) { + $answers = []; + if (isset($responsedata->{'q' . $question->id}) && (strlen($responsedata->{'q' . $question->id}) > 0)) { + $val = $responsedata->{'q' . $question->id}; + $record = new \stdClass(); + $record->responseid = $responsedata->rid; + $record->questionid = $question->id; + + file_save_draft_area_files( + $val, + $question->context->id, + 'mod_questionnaire', + 'response_file', + $val, + \mod_questionnaire\question\file::get_file_manager_option() + ); + $fs = get_file_storage(); + $files = $fs->get_area_files( + $question->context->id, + 'mod_questionnaire', + 'response_file', + $val, + "itemid, filepath, filename", + false + ); + if (!empty($files)) { + $file = reset($files); + $record->value = $file->get_id(); + $answers[] = answer\answer::create_from_data($record); + } else { + self::delete_old_response((int)$question->id, (int)$record->responseid); + } + } + return $answers; + } + + /** + * Return an array of answers by question/choice for the given response. Must be implemented by the subclass. + * + * @param int $rid The response id. + * @return array + */ + public static function response_select($rid) { + global $DB; + + $values = []; + $sql = 'SELECT q.id, q.content, a.fileid as aresponse ' . + 'FROM {' . static::response_table() . '} a, {questionnaire_question} q ' . + 'WHERE a.response_id=? AND a.question_id=q.id '; + $records = $DB->get_records_sql($sql, [$rid]); + foreach ($records as $qid => $row) { + unset($row->id); + $row = (array) $row; + $newrow = []; + foreach ($row as $key => $val) { + if (!is_numeric($key)) { + $newrow[] = $val; + } + } + $values[$qid] = $newrow; + $val = array_pop($values[$qid]); + array_push($values[$qid], $val, $val); + } + + return $values; + } + + /** + * Return an array of answer objects by question for the given response id. + * THIS SHOULD REPLACE response_select. + * + * @param int $rid The response id. + * @return array array answer + * @throws \dml_exception + */ + public static function response_answers_by_question($rid) { + global $DB; + + $answers = []; + $sql = 'SELECT id, response_id as responseid, question_id as questionid, 0 as choiceid, fileid as value ' . + 'FROM {' . static::response_table() . '} ' . + 'WHERE response_id = ? '; + $records = $DB->get_records_sql($sql, [$rid]); + foreach ($records as $record) { + $answers[$record->questionid][] = answer\answer::create_from_data($record); + } + + return $answers; + } + + /** + * Delete old entries from the questionnaire_response_file table and also the corresponding entries + * in the files table. + * @param int $questionid + * @param int $responseid + * @return void + * @throws \dml_exception + */ + public static function delete_old_response(int $questionid, int $responseid) { + global $DB; + // Check, if we have an old response file from a former attempt. + $record = $DB->get_record(static::response_table(), [ + 'response_id' => $responseid, + 'question_id' => $questionid, + ]); + if ($record) { + // Old record found, then delete all referenced entries in the files table and then delete this entry. + $DB->delete_records( + 'files', + [ + 'component' => 'mod_questionnaire', + 'filearea' => 'response_file', + 'itemid' => $record->id, + ] + ); + $DB->delete_records(self::response_table(), ['id' => $record->id]); + } + } + + /** + * Insert a provided response to the question. + * + * @param \mod_questionnaire\responsetype\response\response|\stdClass $responsedata + * @return bool|int + * @throws \dml_exception + */ + public function insert_response($responsedata) { + global $DB; + + if (!$responsedata instanceof \mod_questionnaire\responsetype\response\response) { + $response = \mod_questionnaire\responsetype\response\response::response_from_webform($responsedata, [$this->question]); + } else { + $response = $responsedata; + } + + if (!empty($response) && isset($response->answers[$this->question->id][0])) { + $record = new \stdClass(); + $record->response_id = $response->id; + $record->question_id = $this->question->id; + $record->fileid = intval(clean_text($response->answers[$this->question->id][0]->value)); + + // Delete any previous attempts. + self::delete_old_response((int)$this->question->id, (int)$response->id); + + // 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) { + $olditem = $DB->get_record('files', ['id' => $record->fileid], 'itemid'); + if (!$olditem) { + return false; + } + $siblings = $DB->get_records( + 'files', + [ + 'component' => 'mod_questionnaire', + 'filearea' => 'response_file', + 'itemid' => $olditem->itemid, + ] + ); + foreach ($siblings as $sibling) { + if (!self::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 + */ + 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, + $filerecord->filearea, + $recordid, + $file->get_filepath(), + $file->get_filename() + ); + $filerecord->itemid = $recordid; + $filerecord->pathnamehash = $newhash; + return $DB->update_record('files', $filerecord); + } + + /** + * Provide the necessary response data table name. Should probably always be used with late static binding 'static::' form + * rather than 'self::' form to allow for class extending. + * + * @return string response table name. + */ + public static function response_table() { + return 'questionnaire_response_file'; + } + + /** + * Provide a template for results screen if defined. + * + * @param bool $pdf + * @return mixed The template string or false/ + */ + public function results_template($pdf = false) { + if ($pdf) { + return 'mod_questionnaire/resultspdf_text'; + } else { + return 'mod_questionnaire/results_text'; + } + } + + /** + * Provide the result information for the specified result records. + * + * @param int|array $rids - A single response id, or array. + * @param string $sort - Optional display sort. + * @param boolean $anonymous - Whether or not responses are anonymous. + * @return string - Display output. + */ + public function display_results($rids = false, $sort = '', $anonymous = false) { + if (is_array($rids)) { + $prtotal = 1; + } else if (is_int($rids)) { + $prtotal = 0; + } + if ($rows = $this->get_results($rids, $anonymous)) { + $numrespondents = count($rids); + $numresponses = count($rows); + $pagetags = $this->get_results_tags($rows, $numrespondents, $numresponses, $prtotal); + } else { + $pagetags = ""; + } + return $pagetags; + } + + /** + * Provide the result information for the specified result records. + * + * @param int|array $rids - A single response id, or array. + * @param boolean $anonymous - Whether or not responses are anonymous. + * @return array - Array of data records. + */ + public function get_results($rids = false, $anonymous = false) { + global $DB; + + $rsql = ''; + if (!empty($rids)) { + [$rsql, $params] = $DB->get_in_or_equal($rids); + $rsql = ' AND response_id ' . $rsql; + } + + if ($anonymous) { + $sql = 'SELECT t.id, t.fileid, r.submitted AS submitted, ' . + 'r.questionnaireid, r.id AS rid ' . + 'FROM {' . static::response_table() . '} t, ' . + '{questionnaire_response} r ' . + 'WHERE question_id=' . $this->question->id . $rsql . + ' AND t.response_id = r.id ' . + 'ORDER BY r.submitted DESC'; + } else { + $sql = 'SELECT t.id, t.fileid, r.submitted AS submitted, r.userid, u.username AS username, ' . + 'u.id as usrid, ' . + 'r.questionnaireid, r.id AS rid ' . + 'FROM {' . static::response_table() . '} t, ' . + '{questionnaire_response} r, ' . + '{user} u ' . + 'WHERE question_id=' . $this->question->id . $rsql . + ' AND t.response_id = r.id' . + ' AND u.id = r.userid ' . + 'ORDER BY u.lastname, u.firstname, r.submitted'; + } + return $DB->get_records_sql($sql, $params); + } + + /** + * Override the results tags function for templates for questions with dates. + * + * @param array $weights + * @param int $participants Number of questionnaire participants. + * @param int $respondents Number of question respondents. + * @param bool $showtotals + * @param string $sort + * @return \stdClass + */ + public function get_results_tags($weights, $participants, $respondents, $showtotals = 1, $sort = '') { + $pagetags = new \stdClass(); + if ($respondents == 0) { + return $pagetags; + } + + // If array element is an object, outputting non-numeric responses. + if (is_object(reset($weights))) { + global $CFG, $SESSION, $questionnaire, $DB; + $viewsingleresponse = $questionnaire->capabilities->viewsingleresponse; + $nonanonymous = $questionnaire->respondenttype != 'anonymous'; + if ($viewsingleresponse && $nonanonymous) { + $currentgroupid = ''; + if (isset($SESSION->questionnaire->currentgroupid)) { + $currentgroupid = $SESSION->questionnaire->currentgroupid; + } + $url = $CFG->wwwroot . '/mod/questionnaire/report.php?action=vresp&sid=' . $questionnaire->survey->id . + '¤tgroupid=' . $currentgroupid; + } + $users = []; + $evencolor = false; + foreach ($weights as $row) { + $response = new \stdClass(); + $fs = get_file_storage(); + $file = $fs->get_file_by_id($row->fileid); + + if ($file) { + // There is a file. + $imageurl = moodle_url::make_pluginfile_url( + $file->get_contextid(), + $file->get_component(), + $file->get_filearea(), + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename() + ); + + $response->text = \html_writer::link($imageurl, $file->get_filename()); + if ($viewsingleresponse && $nonanonymous) { + $rurl = $url . '&rid=' . $row->rid . '&individualresponse=1'; + $title = userdate($row->submitted); + if (!isset($users[$row->userid])) { + $users[$row->userid] = $DB->get_record('user', ['id' => $row->userid]); + } + $response->respondent = + '' . fullname($users[$row->userid]) . ''; + } + } else { + $response->respondent = ''; + } + // The 'evencolor' attribute is used by the PDF template. + $response->evencolor = $evencolor; + $pagetags->responses[] = (object) ['response' => $response]; + $evencolor = !$evencolor; + } + + if ($showtotals == 1) { + $pagetags->total = new \stdClass(); + $pagetags->total->total = "$respondents/$participants"; + } + } else { + $nbresponses = 0; + $sum = 0; + $strtotal = get_string('totalofnumbers', 'questionnaire'); + $straverage = get_string('average', 'questionnaire'); + + if (!empty($weights) && is_array($weights)) { + ksort($weights); + $evencolor = false; + foreach ($weights as $text => $num) { + $response = new \stdClass(); + $response->text = $text; + $response->respondent = $num; + // The 'evencolor' attribute is used by the PDF template. + $response->evencolor = $evencolor; + $nbresponses += $num; + $sum += $text * $num; + $evencolor = !$evencolor; + $pagetags->responses[] = (object) ['response' => $response]; + } + + $response = new \stdClass(); + $response->text = $sum; + $response->respondent = $strtotal; + $response->evencolor = $evencolor; + $pagetags->responses[] = (object) ['response' => $response]; + $evencolor = !$evencolor; + + $response = new \stdClass(); + $response->respondent = $straverage; + $avg = $sum / $nbresponses; + $response->text = sprintf('%.' . $this->question->precise . 'f', $avg); + $response->evencolor = $evencolor; + $pagetags->responses[] = (object) ['response' => $response]; + $evencolor = !$evencolor; + + if ($showtotals == 1) { + $pagetags->total = new \stdClass(); + $pagetags->total->total = "$respondents/$participants"; + $pagetags->total->evencolor = $evencolor; + } + } + } + + return $pagetags; + } + + /** + * Configure bulk sql + * + * @return bulk_sql_config + */ + protected function bulk_sql_config() { + return new bulk_sql_config(static::response_table(), 'qrt', false, false, false); + } +} diff --git a/classes/responsetype/response/response.php b/classes/responsetype/response/response.php index 070160d28..71fefd4f7 100644 --- a/classes/responsetype/response/response.php +++ b/classes/responsetype/response/response.php @@ -101,7 +101,7 @@ public static function create_from_data($responsedata) { * * @param \stdClass $responsedata All of the responsedata as an object. * @param array $questions - * @return bool|response A response object. + * @return response A response object. */ public static function response_from_webform($responsedata, $questions) { global $USER; @@ -169,5 +169,6 @@ public function add_questions_answers() { $this->answers += \mod_questionnaire\responsetype\boolean::response_answers_by_question($this->id); $this->answers += \mod_questionnaire\responsetype\date::response_answers_by_question($this->id); $this->answers += \mod_questionnaire\responsetype\text::response_answers_by_question($this->id); + $this->answers += \mod_questionnaire\responsetype\file::response_answers_by_question($this->id); } } diff --git a/db/install.php b/db/install.php index 55b0eda4e..f36a626e3 100644 --- a/db/install.php +++ b/db/install.php @@ -100,6 +100,13 @@ function xmldb_questionnaire_install() { $questiontype->response_table = 'response_text'; $id = $DB->insert_record('questionnaire_question_type', $questiontype); + $questiontype = new stdClass(); + $questiontype->typeid = 12; + $questiontype->type = 'File'; + $questiontype->has_choices = 'n'; + $questiontype->response_table = 'response_file'; + $id = $DB->insert_record('questionnaire_question_type', $questiontype); + $questiontype = new stdClass(); $questiontype->typeid = 99; $questiontype->type = 'Page Break'; diff --git a/db/install.xml b/db/install.xml index 49f51f097..cf7236018 100644 --- a/db/install.xml +++ b/db/install.xml @@ -226,6 +226,21 @@ + + + + + + + + + + + + + + +
diff --git a/db/upgrade.php b/db/upgrade.php index 756582807..0cd12cf0b 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -1042,6 +1042,39 @@ function xmldb_questionnaire_upgrade($oldversion=0) { upgrade_mod_savepoint(true, 2024080100.01, 'questionnaire'); } + if ($oldversion < 2024080100.02) { + $questiontype = new stdClass(); + $questiontype->typeid = 12; + $questiontype->type = 'File'; + $questiontype->has_choices = 'n'; + $questiontype->response_table = 'response_file'; + $id = $DB->insert_record('questionnaire_question_type', $questiontype); + + // Define table questionnaire_response_file to be created. + $table = new xmldb_table('questionnaire_response_file'); + + // Adding fields to table questionnaire_response_file. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('response_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('question_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table questionnaire_response_file. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('file_fk', XMLDB_KEY_FOREIGN, ['fileid'], 'files', ['id']); + + // Adding indexes to table questionnaire_response_file. + $table->add_index('response_question', XMLDB_INDEX_NOTUNIQUE, ['response_id', 'question_id']); + + // Conditionally launch create table for questionnaire_response_file. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Questionnaire savepoint reached. + upgrade_mod_savepoint(true, 2024080100.02, 'questionnaire'); + } + return true; } diff --git a/lang/en/questionnaire.php b/lang/en/questionnaire.php index 2da21d836..b4f6e996c 100644 --- a/lang/en/questionnaire.php +++ b/lang/en/questionnaire.php @@ -260,6 +260,8 @@ respondent. Default values are 20 characters for the Input Box width and 25 characters for the maximum length of text entered.'; +$string['file'] = 'File'; +$string['file_help'] = 'Allow user to submit a file (only simple, virus safe formats are allowed)'; $string['finished'] = 'You have answered all the questions in this questionnaire!'; $string['firstrespondent'] = 'First Respondent'; $string['formateditor'] = 'HTML editor'; diff --git a/lib.php b/lib.php index 8b2c5fc8c..676353598 100644 --- a/lib.php +++ b/lib.php @@ -521,11 +521,12 @@ function questionnaire_scale_used_anywhere($scaleid) { * @param string $filearea * @param array $args * @param bool $forcedownload + * @param mixed $options * @return bool false if file not found, does not return if found - justsend the file * * $forcedownload is unused, but API requires it. Suppress PHPMD warning. */ -function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { +function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options) { global $DB; if ($context->contextlevel != CONTEXT_MODULE) { @@ -534,7 +535,7 @@ function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $for require_course_login($course, true, $cm); - $fileareas = ['intro', 'info', 'thankbody', 'question', 'feedbacknotes', 'sectionheading', 'feedback']; + $fileareas = ['intro', 'info', 'thankbody', 'question', 'feedbacknotes', 'sectionheading', 'feedback', 'response_file']; if (!in_array($filearea, $fileareas)) { return false; } @@ -553,6 +554,10 @@ function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $for if (!$DB->record_exists('questionnaire_feedback', ['id' => $componentid])) { return false; } + } else if ($filearea == 'response_file') { + if (!$DB->record_exists('questionnaire_response_file', ['id' => $componentid])) { + return false; + } } else { if (!$DB->record_exists('questionnaire_survey', ['id' => $componentid])) { return false; @@ -571,7 +576,7 @@ function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $for } // Finally send the file. - send_stored_file($file, 0, 0, true); // Download MUST be forced - security! + send_stored_file($file, null, 0, $forcedownload, $options); // Download MUST be forced - security! } /** * Adds module specific settings to the settings block diff --git a/locallib.php b/locallib.php index 4685b6fa7..b4c4859d7 100644 --- a/locallib.php +++ b/locallib.php @@ -399,6 +399,7 @@ function questionnaire_delete_response($response, $questionnaire='') { $DB->delete_records('questionnaire_response_rank', array('response_id' => $rid)); $DB->delete_records('questionnaire_resp_single', array('response_id' => $rid)); $DB->delete_records('questionnaire_response_text', array('response_id' => $rid)); + $DB->delete_records('questionnaire_response_file', array('response_id' => $rid)); $status = $status && $DB->delete_records('questionnaire_response', array('id' => $rid)); @@ -429,6 +430,7 @@ function questionnaire_delete_responses($qid) { $DB->delete_records('questionnaire_response_rank', ['question_id' => $qid]); $DB->delete_records('questionnaire_resp_single', ['question_id' => $qid]); $DB->delete_records('questionnaire_response_text', ['question_id' => $qid]); + $DB->delete_records('questionnaire_response_file', ['question_id' => $qid]); return true; } @@ -588,6 +590,8 @@ function questionnaire_get_type ($id) { return get_string('numeric', 'questionnaire'); case 11: return get_string('slider', 'questionnaire'); + case 12: + return get_string('file', 'questionnaire'); case 100: return get_string('sectiontext', 'questionnaire'); case 99: diff --git a/questionnaire.class.php b/questionnaire.class.php index b117e281f..acd5f683a 100644 --- a/questionnaire.class.php +++ b/questionnaire.class.php @@ -150,10 +150,10 @@ public function add_survey($sid = 0, $survey = null) { * Adding questions to the object. * @param bool $sid */ - public function add_questions($sid = false) { + public function add_questions($sid = 0) { global $DB; - if ($sid === false) { + if ($sid === 0) { $sid = $this->sid; } @@ -1659,7 +1659,7 @@ public function survey_print_render($courseid, $message = '', $referer='', $rid= $dependants = []; } $this->questions[$questionid]->set_isprint($referer === 'print'); - $output .= $this->renderer->question_output($this->questions[$questionid], $this->responses[0] ?? [], + $output .= $this->renderer->question_output($this->questions[$questionid], $this->responses[0] ?? new \mod_questionnaire\responsetype\response\response(), $i++, null, $dependants); $this->page->add_to_page('questions', $output); $output = ''; diff --git a/report.php b/report.php index a627ec4bb..71b0fe25b 100755 --- a/report.php +++ b/report.php @@ -23,6 +23,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * */ + require_once("../../config.php"); require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php'); diff --git a/tests/behat/add_questions.feature b/tests/behat/add_questions.feature index 54b4fdf05..4f020693b 100644 --- a/tests/behat/add_questions.feature +++ b/tests/behat/add_questions.feature @@ -99,4 +99,8 @@ Feature: Add questions to a questionnaire activity And I should see "Choose yes or no" And I set the field "id_type_id" to "----- Page Break -----" And I press "Add selected question type" + And I add a "File" question and I fill the form with: + | Question Name | Q10 | + | Yes | Yes | + | Question Text | Add a file as an answer | Then I should see "[----- Page Break -----]" diff --git a/tests/behat/behat_mod_questionnaire.php b/tests/behat/behat_mod_questionnaire.php index 5f779f800..e8d92bf98 100644 --- a/tests/behat/behat_mod_questionnaire.php +++ b/tests/behat/behat_mod_questionnaire.php @@ -120,7 +120,8 @@ public function i_add_a_question_and_i_fill_the_form_with($questiontype, TableNo 'Rate (scale 1..5)', 'Text Box', 'Yes/No', - 'Slider'); + 'Slider', + 'File'); if (!in_array($questiontype, $validtypes)) { throw new ExpectationException('Invalid question type specified.', $this->getSession()); @@ -481,4 +482,192 @@ protected function get_cm_by_questionnaire_name(string $name): stdClass { $questionnaire = $this->get_questionnaire_by_name($name); return get_coursemodule_from_instance('questionnaire', $questionnaire->id, $questionnaire->course); } + + /** + * Uploads a file to the specified filemanager leaving other fields in upload form default. + * + * The paths should be relative to moodle codebase. + * + * @When /^I upload "(?P(?:[^"]|\\")*)" to questionnaire "(?P(?:[^"]|\\")*)" filemanager$/ + * @param string $filepath + * @param string $question + */ + public function i_upload_file_to_questionnaire_question_filemanager($filepath, $question) { + $this->upload_file_to_question_filemanager_questionnaire($filepath, $question, new TableNode([]), false); + } + + /** + * Try to get the filemanager node of a given question. + * + * @param string $question + * @return \Behat\Mink\Element\NodeElement|null + */ + protected function get_filepicker_node(string $question) { + // More info about the problem (in case there is a problem). + $exception = new ExpectationException('The filepicker for the question with text "' . $question . + '" can not be found', $this->getSession()); + + $filepickercontainer = $this->find( + 'xpath', + "//p[contains(.,'" . $question . "')]" . + "//parent::div[contains(concat(' ', normalize-space(@class), ' '), ' no-overflow ')]" . + "//parent::div[contains(concat(' ', normalize-space(@class), ' '), ' qn-question ')]" . + "//following::div[contains(concat(' ', normalize-space(@class), ' '), ' qn-answer ')]" . + "//descendant::*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']", + $exception + ); + + return $filepickercontainer; + } + + /** + * Uploads a file to filemanager + * + * @param string $filepath Normally a path relative to $CFG->dirroot, but can be an absolute path too. + * @param string $question A question text. + * @param TableNode $data Data to fill in upload form + * @param false|string $overwriteaction false if we don't expect that file with the same name already exists, + * or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel") + * @throws DriverException + * @throws ExpectationException Thrown by behat_base::find + */ + protected function upload_file_to_question_filemanager_questionnaire( + $filepath, + $question, + TableNode $data, + $overwriteaction = false + ) { + global $CFG; + + if (!$this->has_tag('_file_upload')) { + throw new DriverException('File upload tests must have the @_file_upload tag on either the scenario or feature.'); + } + + $filemanagernode = $this->get_filepicker_node($question); + + // Opening the select repository window and selecting the upload repository. + $this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload')); + + // Ensure all the form is ready. + $noformexception = new ExpectationException('The upload file form is not ready', $this->getSession()); + $this->find( + 'xpath', + "//div[contains(concat(' ', normalize-space(@class), ' '), ' container ')]" . + "[contains(concat(' ', normalize-space(@class), ' '), ' repository_upload ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-upload-form ')]" . + "/descendant::form", + $noformexception + ); + // After this we have the elements we want to interact with. + + // Form elements to interact with. + $file = $this->find_file('repo_upload_file'); + + // Attaching specified file to the node. + // Replace 'admin/' if it is in start of path with $CFG->admin . + if (substr($filepath, 0, 6) === 'admin/') { + $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $CFG->admin . + DIRECTORY_SEPARATOR . substr($filepath, 6); + } + $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath); + if (!is_readable($filepath)) { + $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath; + if (!is_readable($filepath)) { + throw new ExpectationException('The file to be uploaded does not exist.', $this->getSession()); + } + } + $file->attachFile($filepath); + + // Fill the form in Upload window. + $datahash = $data->getRowsHash(); + + // The action depends on the field type. + foreach ($datahash as $locator => $value) { + $field = behat_field_manager::get_form_field_from_label($locator, $this); + + // Delegates to the field class. + $field->set_value($value); + } + + // Submit the file. + $submit = $this->find_button(get_string('upload', 'repository')); + $submit->press(); + + // We wait for all the JS to finish as it is performing an action. + $this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS); + + if ($overwriteaction !== false) { + $overwritebutton = $this->find_button($overwriteaction); + $this->ensure_node_is_visible($overwritebutton); + $overwritebutton->click(); + + // We wait for all the JS to finish. + $this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS); + } + } + + /** + * Opens the filepicker modal window and selects the repository. + * + * @param NodeElement $filemanagernode The filemanager or filepicker form element DOM node. + * @param mixed $repositoryname The repo name. + * @return void + * @throws ExpectationException Thrown by behat_base::find + */ + protected function open_add_file_window($filemanagernode, $repositoryname) { + $exception = new ExpectationException('No files can be added to the specified filemanager', $this->getSession()); + + // We should deal with single-file and multiple-file filemanagers, + // catching the exception thrown by behat_base::find() in case is not multiple. + $this->execute('behat_general::i_click_on_in_the', [ + 'div.fp-btn-add a, input.fp-btn-choose', 'css_element', + $filemanagernode, 'NodeElement', + ]); + + // Wait for the default repository (if any) to load. This checks that + // the relevant div exists and that it does not include the loading image. + $this->ensure_element_exists( + "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" . + "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" . + "[not(descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content-loading ')])]", + 'xpath_element' + ); + + // Getting the repository link and opening it. + $repoexception = + new ExpectationException('The "' . $repositoryname . '" repository has not been found', $this->getSession()); + + // Avoid problems with both double and single quotes in the same string. + $repositoryname = behat_context_helper::escape($repositoryname); + + // Here we don't need to look inside the selected element because there can only be one modal window. + // Apparently there are some of these repo elements. So if the first one is not visible, check out + // the next one. + $repositorylinks = $this->find_all( + 'xpath', + "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-area ')]" . + "//descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]" . + "[normalize-space(.)=$repositoryname]", + $repoexception + ); + + foreach ($repositorylinks as $repositorylink) { + try { + $this->ensure_node_is_visible($repositorylink); + } catch (Exception $exception) { + $repositorylink = $exception; + } + } + if ($repositorylink instanceof \Exception) { + throw new $repositorylink(); + } + // Selecting the repo. + if (!$repositorylink->getParent()->getParent()->hasClass('active')) { + // If the repository link is active, then the repository is already loaded. + // Clicking it while it's active causes issues, so only click it when it isn't (see MDL-51014). + $this->execute('behat_general::i_click_on', [$repositorylink, 'NodeElement']); + } + } } diff --git a/tests/behat/file_question.feature b/tests/behat/file_question.feature new file mode 100644 index 000000000..855d83504 --- /dev/null +++ b/tests/behat/file_question.feature @@ -0,0 +1,80 @@ +@mod @mod_questionnaire +Feature: Add a question requiring a file upload in questionnaire. + In order to use this plugin + As a teacher + I need to add a a file question to a questionnaire created in my course + and a student answers to it. Then the file has to be accessible. + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And the following "activities" exist: + | activity | name | description | course | idnumber | resume | navigate | + | questionnaire | Test questionnaire | Test questionnaire description | C1 | questionnaire0 | 1 | 1 | + + @javascript @_file_upload + Scenario: Add a single file question to a questionnaire and view an answer with an uploaded file. + Given I log in as "teacher1" + When I am on the "Test questionnaire" "questionnaire activity" page + And I navigate to "Questions" in current page administration + And I should see "Add questions" + And I add a "File" question and I fill the form with: + | Question Name | File question | + | Yes | Yes | + | Question Text | Add a file as an answer | + And I log out + And I log in as "student1" + And I am on the "Test questionnaire" "questionnaire activity" page + And I navigate to "Answer the questions..." in current page administration + And I upload "mod/questionnaire/tests/fixtures/testfilequestion.pdf" to questionnaire "Add a file as an answer" filemanager + And I press "Submit questionnaire" + And I should see "Thank you for completing this Questionnaire" + And I press "Continue" + And I should see "View your response(s)" + And ".resourcecontent.resourcepdf" "css_element" should exist + And I log out + And I log in as "teacher1" + And I am on the "Test questionnaire" "questionnaire activity" page + And I navigate to "View all responses" in current page administration + Then I should see "testfilequestion.pdf" + + @javascript @_file_upload + Scenario: Add two file questions to a questionnaire and view an answer with two uploaded file. + Given I log in as "teacher1" + When I am on the "Test questionnaire" "questionnaire activity" page + And I navigate to "Questions" in current page administration + And I should see "Add questions" + And I add a "File" question and I fill the form with: + | Question Name | File question one | + | Yes | Yes | + | Question Text | Add a first file as an answer | + And I add a "File" question and I fill the form with: + | Question Name | File question two | + | Yes | Yes | + | Question Text | Add a second file as an answer | + And I log out + And I log in as "student1" + And I am on the "Test questionnaire" "questionnaire activity" page + And I navigate to "Answer the questions..." in current page administration + And I upload "mod/questionnaire/tests/fixtures/testfilequestion.pdf" to questionnaire "Add a first file as an answer" filemanager + And I upload "mod/questionnaire/tests/fixtures/testfilequestion2.pdf" to questionnaire "Add a second file as an answer" filemanager + And I press "Submit questionnaire" + And I should see "Thank you for completing this Questionnaire" + And I press "Continue" + And I should see "View your response(s)" + And ".resourcecontent.resourcepdf" "css_element" should exist + And I log out + And I log in as "teacher1" + And I am on the "Test questionnaire" "questionnaire activity" page + And I navigate to "View all responses" in current page administration + Then I should see "testfilequestion.pdf" + And I should see "testfilequestion2.pdf" diff --git a/tests/behat/file_question_multiple.feature b/tests/behat/file_question_multiple.feature new file mode 100644 index 000000000..dfff7c6bf --- /dev/null +++ b/tests/behat/file_question_multiple.feature @@ -0,0 +1,94 @@ +@mod @mod_questionnaire +Feature: Add a question requiring more than one file upload questions in a questionnaire. + In order to use this plugin + As a student + I need to add two files in a questionnaire and be able to save and resume the questionnaire. + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And the following "activities" exist: + | activity | name | description | course | idnumber | resume | navigate | + | questionnaire | Test questionnaire multiple files | Test questionnaire description | C1 | questionnaire0 | 1 | 1 | + And I log in as "teacher1" + And I am on "Course 1" course homepage + And I follow "Test questionnaire multiple files" + And I navigate to "Questions" in current page administration + And I add a "Check Boxes" question and I fill the form with: + | Question Name | Q1 | + | Yes | Yes | + | Question Text | Please answer Q1 | + | Possible answers | yes,no,may be | + And I add a "File" question and I fill the form with: + | Question Name | File question One | + | Yes | Yes | + | Question Text | Add file1 as an answer | + And I add a "File" question and I fill the form with: + | Question Name | File question Two | + | Yes | No | + | Question Text | Add file2 as an answer | + And I log out + + @javascript @_file_upload + Scenario: Add one file to the questionnaire and verify that the uploaded files exists in the filepicker. + Given I log in as "student1" + When I am on the "Test questionnaire multiple files" "questionnaire activity" page + And I navigate to "Answer the questions..." in current page administration + And I upload "mod/questionnaire/tests/fixtures/testfilequestion.pdf" to questionnaire "Add file1 as an answer" filemanager + And I press "Submit questionnaire" + And I should not see "Thank you for completing this Questionnaire" + And I should see "Please answer required question #1." + And I should see "testfilequestion.pdf" in the "(//*[contains(@class, 'filemanager-container')])[1]//*[contains(@class, 'fp-filename')]" "xpath" + And I should see "You can drag and drop files here to add them." in the "(//*[contains(@class, 'filemanager-container')])[2]//*[contains(@class, 'dndupload-message')]" "xpath" + And I upload "mod/questionnaire/tests/fixtures/testfilequestion2.pdf" to questionnaire "Add file2 as an answer" filemanager + And I press "Submit questionnaire" + And I should not see "Thank you for completing this Questionnaire" + And I should see "Please answer required question #1." + And I set the field "may be" to "checked" + And I press "Submit questionnaire" + And I should see "Thank you for completing this Questionnaire" + And I press "Continue" + And I should see "View your response(s)" + And ".resourcecontent.resourcepdf" "css_element" should exist + And I log out + And I log in as "teacher1" + And I am on the "Test questionnaire multiple files" "questionnaire activity" page + And I navigate to "View all responses" in current page administration + Then I should see "testfilequestion.pdf" + And I should see "testfilequestion2.pdf" + + @javascript @_file_upload + Scenario: Add one file at a time when the questionnaire is saved and resumed and check that the filepicker contains the uploaded files. + Given I log in as "student1" + When I am on the "Test questionnaire multiple files" "questionnaire activity" page + And I navigate to "Answer the questions..." in current page administration + And I upload "mod/questionnaire/tests/fixtures/testfilequestion.pdf" to questionnaire "Add file2 as an answer" filemanager + And I press "Save and exit" + And I should see "Your progress has been saved." + And I click on "//a[contains(@class, 'btn-primary')][contains(@href, 'resume=1')]" "xpath_element" + And I should see "testfilequestion.pdf" in the "(//*[contains(@class, 'filemanager-container')])[2]//*[contains(@class, 'fp-filename')]" "xpath" + And I should see "You can drag and drop files here to add them." in the "(//*[contains(@class, 'filemanager-container')])[1]//*[contains(@class, 'dndupload-message')]" "xpath" + And I upload "mod/questionnaire/tests/fixtures/testfilequestion2.pdf" to questionnaire "Add file1 as an answer" filemanager + And I press "Save and exit" + And I should see "Your progress has been saved." + And I click on "//a[contains(@class, 'btn-primary')][contains(@href, 'resume=1')]" "xpath_element" + And I should see "testfilequestion2.pdf" in the "(//*[contains(@class, 'filemanager-container')])[1]//*[contains(@class, 'fp-filename')]" "xpath" + And I should see "testfilequestion.pdf" in the "(//*[contains(@class, 'filemanager-container')])[2]//*[contains(@class, 'fp-filename')]" "xpath" + And I set the field "may be" to "checked" + And I press "Submit questionnaire" + And I should see "Thank you for completing this Questionnaire" + And I log out + And I log in as "teacher1" + And I am on the "Test questionnaire multiple files" "questionnaire activity" page + And I navigate to "View all responses" in current page administration + Then I should see "testfilequestion.pdf" + And I should see "testfilequestion2.pdf" diff --git a/tests/custom_completion_test.php b/tests/custom_completion_test.php index e46ce656e..8a9da2ae1 100644 --- a/tests/custom_completion_test.php +++ b/tests/custom_completion_test.php @@ -117,7 +117,7 @@ public function test_get_state(string $rule, int $available, ?bool $submitted, ? /** * Test for get_defined_custom_rules(). * - * @covers \mod_questionnaire\completion\custom_completion + * @covers \mod_questionnaire\completion\custom_completion::get_defined_custom_rules */ public function test_get_defined_custom_rules() { $rules = custom_completion::get_defined_custom_rules(); @@ -128,7 +128,7 @@ public function test_get_defined_custom_rules() { /** * Test for get_defined_custom_rule_descriptions(). * - * @covers \mod_questionnaire\completion\custom_completion + * @covers \mod_questionnaire\completion\custom_completion::get_custom_rule_descriptions */ public function test_get_custom_rule_descriptions() { // Get defined custom rules. @@ -156,7 +156,7 @@ public function test_get_custom_rule_descriptions() { /** * Test for is_defined(). * - * @covers \mod_questionnaire\completion\custom_completion + * @covers \mod_questionnaire\completion\custom_completion::is_defined */ public function test_is_defined() { // Build a mock cm_info instance. diff --git a/tests/fixtures/testfilequestion.pdf b/tests/fixtures/testfilequestion.pdf new file mode 100644 index 000000000..90588c301 Binary files /dev/null and b/tests/fixtures/testfilequestion.pdf differ diff --git a/tests/fixtures/testfilequestion2.pdf b/tests/fixtures/testfilequestion2.pdf new file mode 100644 index 000000000..90588c301 Binary files /dev/null and b/tests/fixtures/testfilequestion2.pdf differ diff --git a/version.php b/version.php index 68c2822ec..01eae003e 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024080100.01; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2024080100.02; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2024042200.00; // Moodle version (4.4.0). $plugin->component = 'mod_questionnaire'; diff --git a/view.php b/view.php index c0181ce56..2f7557424 100644 --- a/view.php +++ b/view.php @@ -17,7 +17,7 @@ /** * This main view page for a questionnaire. * - * @package mod_questionnaire + * @package mod_questionnaire * @copyright 2016 Mike Churchward (mike.churchward@poetgroup.org) * @author Mike Churchward * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later