Skip to content

Commit

Permalink
Add file question type
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentdavid authored and lucaboesch committed Feb 24, 2024
1 parent fb458ad commit 2ed2853
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
34 changes: 16 additions & 18 deletions classes/question/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,19 @@ protected function question_survey_display($formdata, $descendantsdata, $blankqu
// 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");

$fmoptions = array_merge(
$options,
[
'client_id' => uniqid(),
'itemid' => $draftitemid,
'target' => $this->id,
'name' => $elname
]
);
$fm = new form_filemanager((object) $fmoptions);
$options->client_id = uniqid();
$options->itemid = $draftitemid;
$options->target = $this->id;
$options->name = $elname;
$fm = new form_filemanager($options);
$output = $PAGE->get_renderer('core', 'files');
$html = $output->render($fm);
$html = '<div class="form-filemanager" data-fieldtype="filemanager">' .
$output->render($fm) .
'</div>';

$html .= '<input value="' . $draftitemid . '" name="' . $elname . '" type="hidden" />';
/* $html .= '<input value="' . $draftitemid . '" name="' . $elname . '" type="hidden" />';
$html .= '<input value="" id="' . $this->id . '" type="hidden" />';
*/

return $html;
}
Expand All @@ -115,12 +113,12 @@ protected function question_survey_display($formdata, $descendantsdata, $blankqu
* @return array
*/
public static function get_file_manager_option() {
return [
'mainfile' => '',
'subdirs' => false,
'accepted_types' => array('image', '.pdf'),
'maxfiles' => 1,
];
$options = new \stdClass();
$options->mainfile = '';
$options->subdirs = false;
$options->accepted_types = ['image', '.pdf'];
$options->maxfiles = 1;
return $options;
}

/**
Expand Down
64 changes: 50 additions & 14 deletions tests/behat/file_question.feature
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
@mod @mod_questionnaire
Feature: In questionnaire, we can add a question requiring a file upload.
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 |
| 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 |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| 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 |
| activity | name | description | course | idnumber | resume | navigate |
| questionnaire | Test questionnaire | Test questionnaire description | C1 | questionnaire0 | 1 | 1 |

@javascript @_file_upload
Scenario: As a teacher, I create a questionnaire in my course with a file question and a student answers to it. Then the file has to be accessible.
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 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 filemanager
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"
Expand All @@ -42,3 +46,35 @@ Feature: In questionnaire, we can add a question requiring a file upload.
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"

0 comments on commit 2ed2853

Please sign in to comment.