MDL-62708 question: Add idnumbers to question and question category

This commit is contained in:
John Beedell 2018-07-04 12:21:44 +01:00
parent 6902f39141
commit 6189fda47f
24 changed files with 502 additions and 13 deletions

View file

@ -115,8 +115,8 @@ class core_question_backup_testcase extends advanced_testcase {
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
$context = context_coursecat::instance($category1->id);
$qcat = $qgen->create_question_category(array('contextid' => $context->id));
$question1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
$question2 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
$question1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id, 'idnumber' => 'q1'));
$question2 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id, 'idnumber' => 'q2'));
// Tag the questions with 2 question tags and 2 course level question tags.
$qcontext = context::instance_by_id($qcat->contextid);
@ -144,10 +144,11 @@ class core_question_backup_testcase extends advanced_testcase {
// The questions should remain in the question category they were which is
// a question category belonging to a course category context.
$questions = $DB->get_records('question', array('category' => $qcat->id));
$questions = $DB->get_records('question', array('category' => $qcat->id), 'idnumber');
$this->assertCount(2, $questions);
// Retrieve tags for each question and check if they are assigned at the right context.
$qcount = 1;
foreach ($questions as $question) {
$tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
@ -162,6 +163,10 @@ class core_question_backup_testcase extends advanced_testcase {
}
$this->assertEquals($expected, $tag->taginstancecontextid);
}
// Also check idnumbers have been backed up and restored.
$this->assertEquals('q' . $qcount, $question->idnumber);
$qcount++;
}
// Now, again, delete everything including the course category.
@ -203,4 +208,4 @@ class core_question_backup_testcase extends advanced_testcase {
}
}
}
}

View file

@ -0,0 +1,122 @@
@core @core_question
Feature: A teacher can put questions with idnumbers in categories with idnumbers in the question bank
In order to organize my questions
As a teacher
I create and edit categories and move questions between them (now with idnumbers)
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | weeks |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And I log in as "teacher1"
And I am on "Course 1" course homepage
Scenario: A new question category can only be created with a unique idnumber for a context
# Note need to create the top category each time.
When the following "question categories" exist:
| contextlevel | reference | questioncategory | name | idnumber |
| Course | C1 | Top | top | |
| Course | C1 | top | Used category | c1used |
And I navigate to "Question bank > Categories" in current page administration
And I set the following fields to these values:
| Name | Sub used category |
| Parent category | Used category |
| Category info | Created as a test |
| ID number | c1used |
And I press "Add category"
# Standard warning.
Then I should see "This ID number is already in use"
# Correction to a unique idnumber for the context.
And I set the field "ID number" to "c1unused"
And I press "Add category"
Then I should see "Sub used category (0)"
And I should see "Created as a test" in the "Sub used category" "list_item"
Scenario: A question category can be edited and saved without changing the idnumber
When the following "question categories" exist:
| contextlevel | reference | questioncategory | name | idnumber |
| Course | C1 | Top | top | |
| Course | C1 | top | Used category | c1used |
And I navigate to "Question bank > Categories" in current page administration
And I click on "Edit" "link" in the "Used category" "list_item"
And I press "Save changes"
Then I should not see "This ID number is already in use"
Scenario: A question can only have a unique idnumber within a category
When the following "question categories" exist:
| contextlevel | reference | questioncategory | name | idnumber |
| Course | C1 | Top | top | |
| Course | C1 | top | Used category | c1used |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext | idnumber |
| Used category | essay | Test question 1 | Write about whatever you want | q1 |
| Used category | essay | Test question 2 | Write about whatever you want | q2 |
And I navigate to "Question bank > Questions" in current page administration
And I click on "Edit" "link" in the "Test question 2" "table_row"
And I set the field "ID number" to "q1"
And I press "submitbutton"
# This is the standard form warning reminding the user that the idnumber needs to be unique for a category.
Then I should see "This ID number is already in use"
Scenario: A question can be edited and saved without changing the idnumber
When the following "question categories" exist:
| contextlevel | reference | questioncategory | name | idnumber |
| Course | C1 | Top | top | |
| Course | C1 | top | Used category | c1used |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext | idnumber |
| Used category | essay | Test question 1 | Write about whatever you want | q1 |
And I navigate to "Question bank > Questions" in current page administration
And I click on "Edit" "link" in the "Test question 1" "table_row"
And I press "Save changes"
Then I should not see "This ID number is already in use"
Scenario: Question idnumber conficts found when saving to a different category.
When the following "question categories" exist:
| contextlevel | reference | questioncategory | name |
| Course | C1 | Top | top |
| Course | C1 | top | Category 1 |
| Course | C1 | top | Category 2 |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext | idnumber |
| Category 1 | essay | Question to edit | Write about whatever you want | q1 |
| Category 2 | essay | Other question | Write about whatever you want | q2 |
And I navigate to "Question bank > Questions" in current page administration
And I click on "Edit" "link" in the "Question to edit" "table_row"
And I set the following fields to these values:
| Use this category | 0 |
| ID number | q2 |
| Save in category | Category 2 |
And I press "Save changes"
Then I should see "This ID number is already in use"
@javascript
Scenario: Moving a question between categories can force a change to the idnumber
And the following "question categories" exist:
| contextlevel | reference | questioncategory | name | idnumber |
| Course | C1 | Top | top | |
| Course | C1 | top | Subcategory | c1sub |
| Course | C1 | top | Used category | c1used |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext | idnumber |
| Used category | essay | Test question 1 | Write about whatever you want | q1 |
| Used category | essay | Test question 2 | Write about whatever you want | q2 |
| Subcategory | essay | Test question 3 | Write about whatever you want | q3 |
When I navigate to "Question bank > Questions" in current page administration
And I click on "Edit" "link" in the "Test question 3" "table_row"
# The q1 idnumber is allowed for this question while it is in the Subcategory.
And I set the field "ID number" to "q1"
And I press "submitbutton"
# Javascript is required for the next step.
And I click on "Test question 3" "checkbox" in the "Test question 3" "table_row"
And I set the field "Question category" to "Used category"
And I press "Move to >>"
And I click on "Edit" "link" in the "Test question 3" "table_row"
# The question just moved into this category needs to have a unique idnumber, so a number is appended.
Then the field "ID number" matches value "q1_1"

View file

@ -51,6 +51,7 @@ class core_question_generator extends component_generator_base {
'infoformat' => FORMAT_HTML,
'stamp' => make_unique_id_code(),
'sortorder' => 999,
'idnumber' => null
);
$record = $this->datagenerator->combine_defaults_and_record($defaults, $record);
@ -87,6 +88,7 @@ class core_question_generator extends component_generator_base {
$question->category = $fromform->category;
$question->qtype = $qtype;
$question->createdby = 0;
$question->idnumber = null;
return $this->update_question($question, $which, $overrides);
}

View file

@ -51,4 +51,33 @@ class core_question_generator_testcase extends advanced_testcase {
$this->assertSame('My category', $cat->name);
$this->assertSame(1, $cat->sortorder);
}
public function test_idnumbers_in_categories_and_questions() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
list($category, $course, $qcat, $questions) = $generator->setup_course_and_questions();
// Check default idnumbers in question_category and questions.
$this->assertNull($qcat->idnumber);
$this->assertNull($questions[0]->idnumber);
$this->assertNull($questions[1]->idnumber);
// Check created idnumbers.
$qcat1 = $generator->create_question_category(array(
'name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat'));
$this->assertSame('myqcat', $qcat1->idnumber);
$quest1 = $generator->update_question($questions[0], null, ['idnumber' => 'myquest']);
$this->assertSame('myquest', $quest1->idnumber);
$quest3 = $generator->create_question('shortanswer', null,
['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => 'myquest_3']);
$this->assertSame('myquest_3', $quest3->idnumber);
// Check idnumbers of questions moved. Note have to use load_question_data or we only get to see old cached data.
question_move_questions_to_category([$quest1->id], $qcat1->id);
$this->assertSame('myquest', question_bank::load_question_data($quest1->id)->idnumber);
// Can only change idnumber of quest2 once quest1 has been moved to another category.
$quest2 = $generator->update_question($questions[1], null, ['idnumber' => 'myquest']);
question_move_questions_to_category([$quest2->id], $qcat1->id);
$this->assertSame('myquest_4', question_bank::load_question_data($quest2->id)->idnumber);
// Check can add an idnumber of 0.
$quest4 = $generator->create_question('shortanswer', null, ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => '0']);
$this->assertSame('0', $quest4->idnumber);
}
}