MDL-67842 questions: Cannot remove the idnumber from a question

This commit is contained in:
Tim Hunt
2020-01-30 17:29:23 +00:00
parent 7f697f7a44
commit 8ad3593d0c
2 changed files with 26 additions and 11 deletions
+11 -2
View File
@@ -24,7 +24,6 @@ Feature: A teacher can edit questions in the question bank
And I am on "Course 1" course homepage
And I navigate to "Question bank > Questions" in current page administration
@javascript
Scenario: Edit a previously created question
When I choose "Edit question" action for "Test question to be edited" in the question bank
And I set the following fields to these values:
@@ -36,7 +35,6 @@ Feature: A teacher can edit questions in the question bank
And "Edited question name" row "Created by" column of "categoryquestions" table should contain "Admin User"
And "Edited question name" row "Last modified by" column of "categoryquestions" table should contain "Teacher 1"
@javascript
Scenario: Editing a question can be cancelled
When I choose "Edit question" action for "Test question to be edited" in the question bank
And I set the field "Question name" to "Edited question name"
@@ -44,3 +42,14 @@ Feature: A teacher can edit questions in the question bank
Then I should see "Test question to be edited"
And "Test question to be edited" row "Created by" column of "categoryquestions" table should contain "Admin User"
And "Test question to be edited" row "Last modified by" column of "categoryquestions" table should contain "Admin User"
Scenario: A question can have its idnumber removed
Given the following "questions" exist:
| questioncategory | qtype | name | idnumber |
| Test questions | essay | Question with idnumber | frog |
And I reload the page
Then I should see "frog" in the "Question with idnumber" "table_row"
When I choose "Edit question" action for "Question with idnumber" in the question bank
And I set the field "ID number" to ""
And I press "id_submitbutton"
Then I should not see "frog" in the "Question with idnumber" "table_row"
+15 -9
View File
@@ -371,16 +371,22 @@ class question_type {
$question->defaultmark = $form->defaultmark;
}
if (isset($form->idnumber) && ((string) $form->idnumber !== '')) {
// While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
if (strpos($form->category, ',') !== false) {
list($category, $categorycontextid) = explode(',', $form->category);
if (isset($form->idnumber)) {
if ((string) $form->idnumber === '') {
$question->idnumber = null;
} else {
$category = $form->category;
}
if (!$DB->record_exists('question',
['idnumber' => $form->idnumber, 'category' => $category])) {
$question->idnumber = $form->idnumber;
// While this check already exists in the form validation,
// this is a backstop preventing unnecessary errors.
// Only set the idnumber if it has changed and will not cause a unique index violation.
if (strpos($form->category, ',') !== false) {
list($category, $categorycontextid) = explode(',', $form->category);
} else {
$category = $form->category;
}
if (!$DB->record_exists('question',
['idnumber' => $form->idnumber, 'category' => $category])) {
$question->idnumber = $form->idnumber;
}
}
}