From 42756192d7cc38a17c7153bca1ff19e89bedaef1 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Tue, 16 Feb 2016 15:08:19 +0800 Subject: [PATCH] MDL-53129 mod_feedback: forms cleanup and behat tests --- mod/feedback/analysis.php | 4 +- mod/feedback/complete.php | 2 +- mod/feedback/edit.php | 9 +- mod/feedback/edit_form.php | 23 --- mod/feedback/item/label/label_form.php | 2 +- mod/feedback/item/multichoice/lib.php | 6 +- .../item/multichoice/multichoice_form.php | 8 +- mod/feedback/item/multichoicerated/lib.php | 2 +- .../multichoicerated_form.php | 12 +- mod/feedback/item/textarea/lib.php | 4 +- mod/feedback/lang/en/feedback.php | 3 +- mod/feedback/lib.php | 1 - .../tests/behat/behat_mod_feedback.php | 72 +++++++++ .../tests/behat/question_types.feature | 139 ++++++++++++++++++ .../tests/behat/show_nonrespondents.feature | 2 +- 15 files changed, 236 insertions(+), 53 deletions(-) create mode 100644 mod/feedback/tests/behat/behat_mod_feedback.php create mode 100644 mod/feedback/tests/behat/question_types.feature diff --git a/mod/feedback/analysis.php b/mod/feedback/analysis.php index e69966fccec..6fa61e59342 100644 --- a/mod/feedback/analysis.php +++ b/mod/feedback/analysis.php @@ -133,7 +133,7 @@ if ($mygroupid > 0 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES) { } } -echo '
'; +echo '
'; if ($check_anonymously) { $itemnr = 0; //print the items in an analysed form @@ -160,7 +160,7 @@ if ($check_anonymously) { 'insufficient_responses', 'feedback', '', '', 3); } -echo '
'; +echo ''; echo $OUTPUT->box_end(); echo $OUTPUT->footer(); diff --git a/mod/feedback/complete.php b/mod/feedback/complete.php index 6ee053efd03..07319070527 100644 --- a/mod/feedback/complete.php +++ b/mod/feedback/complete.php @@ -417,7 +417,7 @@ if ($feedback_can_submit) { //print the items if (is_array($feedbackitems)) { echo $OUTPUT->box_start('feedback_form'); - echo '
'; + echo ''; echo ''; echo $OUTPUT->box_start('feedback_anonymousinfo'); switch ($feedback->anonymous) { diff --git a/mod/feedback/edit.php b/mod/feedback/edit.php index b0627443578..00258bee8df 100644 --- a/mod/feedback/edit.php +++ b/mod/feedback/edit.php @@ -140,10 +140,6 @@ if (is_array($feedbackitems)) { $lastposition++; -//The add_item-form -$add_item_form = new feedback_edit_add_question_form('edit_item.php'); -$add_item_form->set_data(array('cmid'=>$id, 'position'=>$lastposition)); - //The use_template-form $use_template_form = new feedback_edit_use_template_form('use_templ.php'); $use_template_form->set_feedbackdata(array('course' => $course)); @@ -237,7 +233,10 @@ if ($do_show == 'templates') { /////////////////////////////////////////////////////////////////////////// if ($do_show == 'edit') { - $add_item_form->display(); + $select = new single_select(new moodle_url('/mod/feedback/edit_item.php', array('cmid' => $id, 'position' => $lastposition)), + 'typ', feedback_load_feedback_items_options()); + $select->label = get_string('add_item', 'mod_feedback'); + echo $OUTPUT->render($select); if (is_array($feedbackitems)) { $itemnr = 0; diff --git a/mod/feedback/edit_form.php b/mod/feedback/edit_form.php index 708ca5ae7e1..7de1cba864b 100644 --- a/mod/feedback/edit_form.php +++ b/mod/feedback/edit_form.php @@ -29,29 +29,6 @@ if (!defined('MOODLE_INTERNAL')) { require_once($CFG->libdir.'/formslib.php'); -class feedback_edit_add_question_form extends moodleform { - public function definition() { - $mform = $this->_form; - - //headline - $mform->addElement('header', 'general', get_string('content')); - // visible elements - $feedback_names_options = feedback_load_feedback_items_options(); - - $attributes = 'onChange="M.core_formchangechecker.set_form_submitted(); this.form.submit()"'; - $mform->addElement('select', 'typ', '', $feedback_names_options, $attributes); - - // hidden elements - $mform->addElement('hidden', 'cmid'); - $mform->setType('cmid', PARAM_INT); - $mform->addElement('hidden', 'position'); - $mform->setType('position', PARAM_INT); - - // buttons - $mform->addElement('submit', 'add_item', get_string('add_item', 'feedback'), array('class' => 'hiddenifjs')); - } -} - class feedback_edit_use_template_form extends moodleform { private $feedbackdata; diff --git a/mod/feedback/item/label/label_form.php b/mod/feedback/item/label/label_form.php index dd04e53ccc1..b015cd15555 100644 --- a/mod/feedback/item/label/label_form.php +++ b/mod/feedback/item/label/label_form.php @@ -39,7 +39,7 @@ class feedback_label_form extends feedback_item_form { $mform->setType('label', PARAM_ALPHA); $mform->addElement('header', 'general', get_string($this->type, 'feedback')); - $mform->addElement('editor', 'presentation_editor', '', null, $presentationoptions); + $mform->addElement('editor', 'presentation_editor', get_string('labelcontents', 'feedback'), null, $presentationoptions); $mform->setType('presentation_editor', PARAM_RAW); parent::definition(); diff --git a/mod/feedback/item/multichoice/lib.php b/mod/feedback/item/multichoice/lib.php index 6332f6e10fe..8a5269863df 100644 --- a/mod/feedback/item/multichoice/lib.php +++ b/mod/feedback/item/multichoice/lib.php @@ -244,7 +244,7 @@ class feedback_item_multichoice extends feedback_item_base { $quotient = number_format(($val->quotient * 100), 2, $sep_dec, $sep_thous); $str_quotient = ''; if ($val->quotient > 0) { - $str_quotient = ' ('. $quotient . ' %)'; + $str_quotient = ' ('. $quotient . ' %)'; } echo ''; echo ' @@ -252,7 +252,7 @@ class feedback_item_multichoice extends feedback_item_base { -  '.$val->answercount.$str_quotient.' + '.$val->answercount.$str_quotient.' '; echo ''; } @@ -717,7 +717,7 @@ class feedback_item_multichoice extends feedback_item_base { diff --git a/mod/feedback/item/multichoice/multichoice_form.php b/mod/feedback/item/multichoice/multichoice_form.php index 6dd192dd6d9..b9b8785caed 100644 --- a/mod/feedback/item/multichoice/multichoice_form.php +++ b/mod/feedback/item/multichoice/multichoice_form.php @@ -64,12 +64,10 @@ class feedback_multichoice_form extends feedback_item_form { 'hidenoselect', get_string('hide_no_select_option', 'feedback')); - $mform->addElement('static', - 'hint', - get_string('multichoice_values', 'feedback'), - get_string('use_one_line_for_each_value', 'feedback')); + $mform->addElement('textarea', 'values', get_string('multichoice_values', 'feedback'), + 'wrap="virtual" rows="10" cols="65"'); - $mform->addElement('textarea', 'values', '', 'wrap="virtual" rows="10" cols="65"'); + $mform->addElement('static', 'hint', '', get_string('use_one_line_for_each_value', 'feedback')); parent::definition(); $this->set_data($item); diff --git a/mod/feedback/item/multichoicerated/lib.php b/mod/feedback/item/multichoicerated/lib.php index a2bc2b2a8fa..79c2078d421 100644 --- a/mod/feedback/item/multichoicerated/lib.php +++ b/mod/feedback/item/multichoicerated/lib.php @@ -226,7 +226,7 @@ class feedback_item_multichoicerated extends feedback_item_base { echo ''; echo $val->answercount; if ($val->quotient > 0) { - echo ' ('.$quotient.' %)'; + echo ' ('.$quotient.' %)'; } else { echo ''; } diff --git a/mod/feedback/item/multichoicerated/multichoicerated_form.php b/mod/feedback/item/multichoicerated/multichoicerated_form.php index 0c1632a9c81..365edd73f6f 100644 --- a/mod/feedback/item/multichoicerated/multichoicerated_form.php +++ b/mod/feedback/item/multichoicerated/multichoicerated_form.php @@ -63,16 +63,16 @@ class feedback_multichoicerated_form extends feedback_item_form { 'hidenoselect', get_string('hide_no_select_option', 'feedback')); - $mform->addElement('static', - 'hint', - get_string('multichoice_values', 'feedback'), - get_string('use_one_line_for_each_value', 'feedback')); - $this->values = $mform->addElement('textarea', 'values', - '', + get_string('multichoice_values', 'feedback'), 'wrap="virtual" rows="10" cols="65"'); + $mform->addElement('static', + 'hint', + '', + get_string('use_one_line_for_each_value', 'feedback')); + parent::definition(); $this->set_data($item); diff --git a/mod/feedback/item/textarea/lib.php b/mod/feedback/item/textarea/lib.php index e086e1259a7..66d76ff54b3 100644 --- a/mod/feedback/item/textarea/lib.php +++ b/mod/feedback/item/textarea/lib.php @@ -155,10 +155,8 @@ class feedback_item_textarea extends feedback_item_base { echo ''; foreach ($values as $value) { echo ''; - echo ''; + echo ''; echo '-  '; - echo ''; - echo ''; echo str_replace("\n", '
', $value->value); echo ''; echo ''; diff --git a/mod/feedback/lang/en/feedback.php b/mod/feedback/lang/en/feedback.php index a50f1f829cb..11120fa96f2 100644 --- a/mod/feedback/lang/en/feedback.php +++ b/mod/feedback/lang/en/feedback.php @@ -22,7 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string['add_item'] = 'Add question to activity'; +$string['add_item'] = 'Add question'; $string['add_pagebreak'] = 'Add a page break'; $string['adjustment'] = 'Adjustment'; $string['after_submit'] = 'After submission'; @@ -143,6 +143,7 @@ To keep the feedback anonymous, a minimum of 2 responses must be done.'; $string['item_label'] = 'Label'; $string['item_name'] = 'Question'; $string['label'] = 'Label'; +$string['labelcontents'] = 'Contents'; $string['line_values'] = 'Rating'; $string['mapcourseinfo'] = 'This is a site-wide feedback that is available to all courses using the feedback block. You can however limit the courses to which it will appear by mapping them. Search the course and map it to this feedback.'; $string['mapcoursenone'] = 'No courses mapped. Feedback available to all courses'; diff --git a/mod/feedback/lib.php b/mod/feedback/lib.php index 0b20af457ba..1b68cd5168f 100644 --- a/mod/feedback/lib.php +++ b/mod/feedback/lib.php @@ -1418,7 +1418,6 @@ function feedback_load_feedback_items_options() { $feedback_options[$fn] = get_string($fn, 'feedback'); } asort($feedback_options); - $feedback_options = array_merge( array(' ' => get_string('select')), $feedback_options ); return $feedback_options; } diff --git a/mod/feedback/tests/behat/behat_mod_feedback.php b/mod/feedback/tests/behat/behat_mod_feedback.php new file mode 100644 index 00000000000..d6c4b528cbb --- /dev/null +++ b/mod/feedback/tests/behat/behat_mod_feedback.php @@ -0,0 +1,72 @@ +. + +/** + * Steps definitions related to mod_feedback. + * + * @package mod_feedback + * @category test + * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); + +use Behat\Behat\Context\Step\Given as Given, + Behat\Gherkin\Node\TableNode as TableNode, + Behat\Mink\Exception\ExpectationException as ExpectationException; + +/** + * Steps definitions related to mod_feedback. + * + * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_feedback extends behat_base { + + /** + * Adds a question to the existing feedback with filling the form. + * + * The form for creating a question should be on one page. + * + * @When /^I add a "(?P(?:[^"]|\\")*)" question to the feedback with:$/ + * @param string $questiontype + * @param TableNode $questiondata with data for filling the add question form + */ + public function i_add_question_to_the_feedback_with($questiontype, TableNode $questiondata) { + $rv = array(); + $questiontype = $this->escape($questiontype); + $additem = $this->escape(get_string('add_item', 'feedback')); + $rv[] = new Given("I select \"{$questiontype}\" from the \"{$additem}\" singleselect"); + + $newdata = new TableNode(); + $rows = $questiondata->getRows(); + foreach ($rows as $row) { + foreach ($row as $key => $value) { + $row[$key] = preg_replace('|\\\\n|', "\n", $value); + } + $newdata->addRow($row); + } + $rv[] = new Given('I set the following fields to these values:', $newdata); + + $saveitem = $this->escape(get_string('save_item', 'feedback')); + $rv[] = new Given("I press \"{$saveitem}\""); + + return $rv; + } +} diff --git a/mod/feedback/tests/behat/question_types.feature b/mod/feedback/tests/behat/question_types.feature new file mode 100644 index 00000000000..585e993c791 --- /dev/null +++ b/mod/feedback/tests/behat/question_types.feature @@ -0,0 +1,139 @@ +@mod @mod_feedback +Feature: Test creating different types of feedback questions + In order to create feedbacks + As a teacher + I need to be able to add different question types + + Background: + Given the following "users" exist: + | username | firstname | lastname | + | teacher1 | Teacher | 1 | + | student1 | Student | 1 | + | student2 | Student | 2 | + And the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + | student2 | C1 | student | + And I log in as "admin" + And I navigate to "Manage activities" node in "Site administration > Plugins > Activity modules" + And I click on "Show" "link" in the "Feedback" "table_row" + And I log out + And the following "activities" exist: + | activity | name | course | idnumber | + | feedback | Learning experience | C1 | feedback0 | + When I log in as "teacher1" + And I follow "Course 1" + And I follow "Learning experience" + And I follow "Edit questions" + And I add a "Information" question to the feedback with: + | Question | this is an information question | + | Label | info | + | Information-Type | Course | + And I add a "Label" question to the feedback with: + | Contents | label text | + And I add a "Longer text answer" question to the feedback with: + | Question | this is a longer text answer | + | Label | longertext | + And I add a "Multiple choice" question to the feedback with: + | Question | this is a multiple choice 1 | + | Label | multichoice1 | + | Multiple choice type | Multiple choice - single answer | + | Multiple choice values | option a\noption b\noption c | + And I add a "Multiple choice" question to the feedback with: + | Question | this is a multiple choice 2 | + | Label | multichoice2 | + | Multiple choice type | Multiple choice - multiple answers | + | Hide the "Not selected" option | Yes | + | Multiple choice values | option d\noption e\noption f | + And I add a "Multiple choice" question to the feedback with: + | Question | this is a multiple choice 3 | + | Label | multichoice3 | + | Multiple choice type | Multiple choice - single answer allowed (dropdownlist) | + | Multiple choice values | option g\noption h\noption i | + And I add a "Multiple choice (rated)" question to the feedback with: + | Question | this is a multiple choice rated | + | Label | multichoice4 | + | Multiple choice type | Multiple choice - single answer | + | Multiple choice values | 0/option k\n1/option l\n5/option m | + And I add a "Numeric answer" question to the feedback with: + | Question | this is a numeric answer | + | Label | numeric | + | Range to | 100 | + And I add a "Short text answer" question to the feedback with: + | Question | this is a short text answer | + | Label | shorttext | + | Maximum characters accepted | 200 | + And I log out + And I log in as "student1" + And I follow "Course 1" + And I follow "Learning experience" + And I follow "Answer the questions..." + And I set the following fields to these values: + | this is a longer text answer | my long answer | + | option b | 1 | + | option d | 1 | + | option f | 1 | + | this is a multiple choice 3 | option h | + | option l | 1 | + | this is a numeric answer (0 - 100) | 35 | + | this is a short text answer | hello | + And I press "Submit your answers" + And I log out + And I log in as "student2" + And I follow "Course 1" + And I follow "Learning experience" + And I follow "Answer the questions..." + And I set the following fields to these values: + | this is a longer text answer | lots of feedbacks | + | option a | 1 | + | option d | 1 | + | option e | 1 | + | this is a multiple choice 3 | option i | + | option m | 1 | + | this is a numeric answer (0 - 100) | 71 | + | this is a short text answer | no way | + And I press "Submit your answers" + And I log out + When I log in as "teacher1" + And I follow "Course 1" + And I follow "Learning experience" + And I follow "Analysis" + And I should see "Submitted answers: 2" + And I should see "Questions: 8" + And I log out + And I log in as "teacher1" + And I follow "Course 1" + And I follow "Learning experience" + And I follow "Analysis" + And I should see "C1" in the "(info)" "table" + And I should see "my long answer" in the "(longertext)" "table" + And I should see "lots of feedbacks" in the "(longertext)" "table" + #And I should see "1 (50.00 %)" in the "option a:" "table_row" // TODO: MDL-46891 + #And I should see "1 (50.00 %)" in the "option b:" "table_row" // TODO: MDL-46891 + And I should see "2 (100.00 %)" in the "option d:" "table_row" + And I should see "1 (50.00 %)" in the "option e:" "table_row" + And I should see "1 (50.00 %)" in the "option f:" "table_row" + And I should see "0" in the "option g:" "table_row" + And I should not see "%" in the "option g:" "table_row" + And I should see "1 (50.00 %)" in the "option h:" "table_row" + And I should see "1 (50.00 %)" in the "option i:" "table_row" + And I should see "0" in the "option k (0):" "table_row" + And I should not see "%" in the "option k (0):" "table_row" + And I should see "1 (50.00 %)" in the "option l (1):" "table_row" + And I should see "1 (50.00 %)" in the "option m (5):" "table_row" + And I should see "Average: 3.00" in the "(multichoice4)" "table" + And I should see "35.00" in the "(numeric)" "table" + And I should see "71.00" in the "(numeric)" "table" + And I should see "Average: 53.00" in the "(numeric)" "table" + And I should see "no way" in the "(shorttext)" "table" + And I should see "hello" in the "(shorttext)" "table" + And I log out + + Scenario: Create different types of questions in feedback with javascript disabled + + @javascript + Scenario: Create different types of questions in feedback with javascript enabled diff --git a/mod/feedback/tests/behat/show_nonrespondents.feature b/mod/feedback/tests/behat/show_nonrespondents.feature index 6de0a2349b7..e675f0068c7 100644 --- a/mod/feedback/tests/behat/show_nonrespondents.feature +++ b/mod/feedback/tests/behat/show_nonrespondents.feature @@ -51,7 +51,7 @@ Feature: Show users who have not responded to the feedback survey | Access restrictions | Grouping: GX1 | And I follow "Frogs" And I follow "Edit questions" - And I set the field "id_typ" to "Short text answer" + And I set the field "Add question" to "Short text answer" And I set the following fields to these values: | Question | Y/N? | And I press "Save question"