Skip to content

Commit

Permalink
Merge branch 'MDL-75642-400' of https://github.com/BruceGoodGuy/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_400_STABLE
  • Loading branch information
rezaies committed Oct 13, 2022
2 parents a2487cb + 3a886db commit a85d347
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mod/quiz/attemptlib.php
Expand Up @@ -271,7 +271,7 @@ public function get_cm() {
/**
* Get the quiz context.
*
* @return context the module context for this quiz.
* @return context_module the module context for this quiz.
*/
public function get_context() {
return $this->context;
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/classes/output/edit_renderer.php
Expand Up @@ -823,7 +823,7 @@ public function get_action_icon(structure $structure, int $slot, \moodle_url $pa
$structure->get_question_in_slot($slot),
null, null, $qtype);
}
if ($structure->can_be_edited()) {
if ($structure->can_be_edited() && $structure->has_use_capability($slot)) {
$questionicons .= $this->question_remove_icon($structure, $slot, $pageurl);
}
$questionicons .= $this->marked_out_of_field($structure, $slot);
Expand Down
19 changes: 19 additions & 0 deletions mod/quiz/classes/structure.php
Expand Up @@ -244,6 +244,25 @@ public function is_real_question($slotnumber) {
return $this->get_question_in_slot($slotnumber)->length != 0;
}

/**
* Does the current user have '...use' capability over the question(s) in a given slot?
*
*
* @param int $slotnumber the index of the slot in question.
* @return bool true if they have the required capability.
*/
public function has_use_capability(int $slotnumber): bool {
$slot = $this->slotsinorder[$slotnumber];
if (is_numeric($slot->questionid)) {
// Non-random question.
return question_has_capability_on($this->get_question_by_id($slot->questionid), 'use');
} else {
// Random question.
$context = \context::instance_by_id($slot->contextid);
return has_capability('moodle/question:useall', $context);
}
}

/**
* Get the course id that the quiz belongs to.
* @return int the course.id for the quiz.
Expand Down
9 changes: 8 additions & 1 deletion mod/quiz/edit_rest.php
Expand Up @@ -156,7 +156,7 @@
foreach ($ids as $id) {
$slot = $DB->get_record('quiz_slots', array('quizid' => $quiz->id, 'id' => $id),
'*', MUST_EXIST);
if (quiz_has_question_use($quiz, $slot->slot)) {
if ($structure->has_use_capability($slot->slot)) {
$structure->remove_slot($slot->slot);
}
}
Expand Down Expand Up @@ -192,6 +192,13 @@
if (!$slot = $DB->get_record('quiz_slots', array('quizid' => $quiz->id, 'id' => $id))) {
throw new moodle_exception('AJAX commands.php: Bad slot ID '.$id);
}

if (!$structure->has_use_capability($slot->slot)) {
$slotdetail = $structure->get_slot_by_id($slot->id);
$context = context::instance_by_id($slotdetail->contextid);
throw new required_capability_exception($context,
'moodle/question:useall', 'nopermissions', '');
}
$structure->remove_slot($slot->slot);
quiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
Expand Down
58 changes: 58 additions & 0 deletions mod/quiz/tests/behat/editing_remove_multiple_questions.feature
Expand Up @@ -236,3 +236,61 @@ Feature: Edit quiz page - remove multiple questions
And I click on "Delete selected" "button"

Then I should see "Cannot remove questions"

@javascript
Scenario: Delete multiple random questions from sections.
Given the following "questions" exist:
| questioncategory | qtype | name | questiontext |
| Test questions | truefalse | Question A | First question |
| Test questions | truefalse | Question B | Second question |
| Test questions | truefalse | Question C | Third question |
| Test questions | truefalse | Question D | Fourth question |
| Test questions | truefalse | Question E | Fifth question |
| Test questions | truefalse | Question F | Sixth question |
And I am on the "Quiz 1" "mod_quiz > Edit" page

When I open the "last" add to quiz menu
And I follow "a random question"
And I set the field "Number of random questions" to "3"
And I press "Add random question"
And I click on "Select multiple items" "button"
And I click on "selectquestion-1" "checkbox"
And I click on "selectquestion-2" "checkbox"
And I click on "Delete selected" "button"
And I click on "Yes" "button" in the "Confirm" "dialogue"
# To make sure question is deleted completely.
And I reload the page
Then I should see "Random (Test questions)" on quiz page "1"
And I should not see "Random (Test questions)" on quiz page "2"
And I should not see "Random (Test questions)" on quiz page "3"
And I should see "Total of marks: 1.00"
And I should see "Questions: 1"

@javascript
Scenario: Delete all random questions by checking select all.
Given the following "questions" exist:
| questioncategory | qtype | name | questiontext |
| Test questions | truefalse | Question A | First question |
| Test questions | truefalse | Question B | Second question |
| Test questions | truefalse | Question C | Third question |
| Test questions | truefalse | Question D | Fourth question |
| Test questions | truefalse | Question E | Fifth question |
| Test questions | truefalse | Question F | Sixth question |
And I am on the "Quiz 1" "mod_quiz > Edit" page

# Delete all questions in page. Page contains multiple questions.
When I open the "last" add to quiz menu
And I follow "a random question"
And I set the field "Number of random questions" to "3"
And I press "Add random question"
And I click on "Select multiple items" "button"
And I press "Select all"
And I click on "Delete selected" "button"
And I click on "Yes" "button" in the "Confirm" "dialogue"
# To make sure question is deleted completely.
And I reload the page
Then I should not see "Random (Test questions)" on quiz page "1"
And I should not see "Random (Test questions)" on quiz page "2"
And I should not see "Random (Test questions)" on quiz page "3"
And I should see "Total of marks: 0.00"
And I should see "Questions: 0"
35 changes: 35 additions & 0 deletions mod/quiz/tests/structure_test.php
Expand Up @@ -958,4 +958,39 @@ public function test_get_version_choices_for_slot() {
$this->assertFalse($versiondata[2]->selected);
$this->assertFalse($versiondata[3]->selected);
}

/**
* Test the current user have '...use' capability over the question(s) in a given slot.
*
* @covers ::has_use_capability
*/
public function test_has_use_capability() {
$this->resetAfterTest();

// Create a quiz with question.
$quizobj = $this->create_test_quiz([]);
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category(['contextid' => $quizobj->get_context()->id]);
$q = $questiongenerator->create_question('essay', null,
['category' => $cat->id, 'name' => 'This is essay question']);
quiz_add_quiz_question($q->id, $quizobj->get_quiz());

// Create the quiz object.
$structure = structure::create_for_quiz($quizobj);
$slots = $structure->get_slots();

// Get slot.
$slotid = array_pop($slots)->slot;

$course = $quizobj->get_course();
$generator = $this->getDataGenerator();
$teacher = $generator->create_and_enrol($course, 'editingteacher');
$student = $generator->create_and_enrol($course);

$this->setUser($teacher);
$this->assertTrue($structure->has_use_capability($slotid));

$this->setUser($student);
$this->assertFalse($structure->has_use_capability($slotid));
}
}

0 comments on commit a85d347

Please sign in to comment.