Skip to content

Commit 76708fc

Browse files
authored
GHI384 Fix for required checkbox issue and 'other' option (#460)
* GHI384 Ensure an option is checked * For required checkbox questions, having an 'other' should not allow no selection to be made. * If other checkbox is selected, ensure text has been entered. * Create Behat test to verify. * GHI384 Adding test for missing text. * GHI384 New CI commands and CI fixes.
1 parent dbb66b8 commit 76708fc

File tree

3 files changed

+98
-32
lines changed

3 files changed

+98
-32
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ on: [push, pull_request]
44
jobs:
55
test:
66
runs-on: 'ubuntu-latest'
7+
8+
services:
9+
postgres:
10+
image: postgres:13
11+
env:
12+
POSTGRES_USER: 'postgres'
13+
POSTGRES_HOST_AUTH_METHOD: 'trust'
14+
ports:
15+
- 5432:5432
16+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
17+
mariadb:
18+
image: mariadb:10
19+
env:
20+
MYSQL_USER: 'root'
21+
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
22+
MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
23+
MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"
24+
25+
ports:
26+
- 3306:3306
27+
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3
28+
729
strategy:
830
fail-fast: false
931
matrix:
@@ -24,34 +46,9 @@ jobs:
2446
moodle-branch: 'MOODLE_39_STABLE'
2547
database: 'pgsql'
2648

27-
services:
28-
postgres:
29-
image: postgres:10
30-
env:
31-
POSTGRES_USER: 'postgres'
32-
POSTGRES_HOST_AUTH_METHOD: 'trust'
33-
options: >-
34-
--health-cmd pg_isready
35-
--health-interval 10s
36-
--health-timeout 5s
37-
--health-retries 3
38-
ports:
39-
- 5432:5432
40-
41-
mariadb:
42-
image: mariadb:10
43-
env:
44-
MYSQL_USER: 'root'
45-
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
46-
MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
47-
MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"
48-
ports:
49-
- 3306:3306
50-
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3
51-
5249
steps:
5350
- name: Checkout
54-
uses: actions/checkout@v2
51+
uses: actions/checkout@v3
5552
with:
5653
path: plugin
5754

@@ -61,6 +58,7 @@ jobs:
6158
php-version: ${{ matrix.php }}
6259
extensions: ${{ matrix.extensions }}
6360
ini-values: max_input_vars=5000
61+
# none to use phpdbg fallback. Specify pcov (Moodle 3.10 and up) or xdebug to use them instead.
6462
coverage: none
6563

6664
- name: Deploy moodle-plugin-ci

classes/question/check.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ protected function response_survey_display($response) {
197197
return $resptags;
198198
}
199199

200+
/**
201+
* Check question's form data for complete response.
202+
*
203+
* @param object $responsedata The data entered into the response.
204+
* @return boolean
205+
*/
206+
public function response_complete($responsedata) {
207+
if (isset($responsedata->{'q'.$this->id}) && $this->required() &&
208+
is_array($responsedata->{'q'.$this->id})) {
209+
foreach ($responsedata->{'q' . $this->id} as $key => $choice) {
210+
// If only an 'other' choice is selected and empty, question is not completed.
211+
if ((strpos($key, 'o') === 0) && empty($choice)) {
212+
return false;
213+
} else {
214+
return true;
215+
}
216+
}
217+
}
218+
return parent::response_complete($responsedata);
219+
}
220+
200221
/**
201222
* Check question's form data for valid response. Override this is type has specific format requirements.
202223
*
@@ -218,16 +239,15 @@ public function response_valid($responsedata) {
218239
}
219240
}
220241
} else if (isset($responsedata->{'q'.$this->id})) {
221-
foreach ($responsedata->{'q'.$this->id} as $answer) {
222-
if (strpos($answer, 'other_') !== false) {
242+
foreach ($responsedata->{'q'.$this->id} as $key => $answer) {
243+
if (strpos($key, 'o') === 0) {
223244
// ..."other" choice is checked but text box is empty.
224-
$othercontent = "q".$this->id.substr($answer, 5);
225-
if (trim($responsedata->$othercontent) == false) {
245+
$okey = substr($key, 1);
246+
if (isset($responsedata->{'q'.$this->id}[$okey]) && empty(trim($answer))) {
226247
$valid = false;
227248
break;
228249
}
229-
$nbrespchoices++;
230-
} else if (is_numeric($answer)) {
250+
} else if (is_numeric($key)) {
231251
$nbrespchoices++;
232252
}
233253
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@mod @mod_questionnaire
2+
Feature: Checkbox questions can have other options that can be typed in.
3+
4+
Background: Add a checkbox question to a questionnaire with an 'other' option.
5+
Given the following "users" exist:
6+
| username | firstname | lastname | email |
7+
| teacher1 | Teacher | 1 | teacher1@example.com |
8+
| student1 | Student | 1 | student1@example.com |
9+
And the following "courses" exist:
10+
| fullname | shortname | category |
11+
| Course 1 | C1 | 0 |
12+
And the following "course enrolments" exist:
13+
| user | course | role |
14+
| teacher1 | C1 | editingteacher |
15+
| student1 | C1 | student |
16+
And the following "activities" exist:
17+
| activity | name | description | course | idnumber |
18+
| questionnaire | Test questionnaire | Test questionnaire description | C1 | questionnaire0 |
19+
And I log in as "teacher1"
20+
And I am on "Course 1" course homepage
21+
And I follow "Test questionnaire"
22+
And I navigate to "Questions" in current page administration
23+
And I add a "Check Boxes" question and I fill the form with:
24+
| Question Name | Q1 |
25+
| Yes | y |
26+
| Question Text | Select one or two choices only |
27+
| Possible answers | One,Two,Three,Four,!other |
28+
And I add a "Check Boxes" question and I fill the form with:
29+
| Question Name | Q2 |
30+
| No | n |
31+
| Question Text | Select one or two choices only |
32+
| Possible answers | Red,Blue,Yellow,Green,!other=Other colour |
33+
And I log out
34+
35+
@javascript
36+
Scenario: Student must enter a valid value when "other" is selected.
37+
And I log in as "student1"
38+
And I am on "Course 1" course homepage
39+
And I follow "Test questionnaire"
40+
And I navigate to "Answer the questions..." in current page administration
41+
Then I should see "Select one or two choices only"
42+
And I press "Submit questionnaire"
43+
Then I should see "Please answer required question #1"
44+
And I set the field "Other" to "checked"
45+
And I set the field "Other colour" to "checked"
46+
And I press "Submit questionnaire"
47+
Then I should see "There is something wrong with your answer to questions:"
48+
And I should see "#1. #2."

0 commit comments

Comments
 (0)