Skip to content

Commit 73f788f

Browse files
committed
MDL-84981 mod_assign: Fix hidden group bug
1 parent 8b7a871 commit 73f788f

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

mod/assign/classes/penalty_recalculator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public static function recalculate_penalty(context $context, ?array $userids, in
4545
// Queue a task for the course.
4646
$courseid = $context->instanceid;
4747
$assignids = $DB->get_fieldset('assign', 'id', ['course' => $courseid]);
48-
recalculate_penalties::queue($assignids, $userids, $usermodified);
48+
if (!empty($assignids)) {
49+
recalculate_penalties::queue($assignids, $userids, $usermodified);
50+
}
4951
break;
5052

5153
case CONTEXT_SYSTEM:

mod/assign/tests/penalty_test.php

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
namespace mod_assign;
1818

19+
use context_course;
20+
use core\task\manager;
1921
use core_component;
22+
use core_grades\penalty_exemption;
2023
use core_grades\penalty_manager;
2124
use grade_item;
2225
use mod_assign_test_generator;
@@ -40,6 +43,16 @@ final class penalty_test extends \advanced_testcase {
4043
// Use the generator helper.
4144
use mod_assign_test_generator;
4245

46+
/**
47+
* Reset the test environment after each test.
48+
*
49+
* @return void
50+
*/
51+
protected function setUp(): void {
52+
parent::setUp();
53+
$this->resetAfterTest();
54+
}
55+
4356
/**
4457
* Set up test
4558
*
@@ -76,7 +89,6 @@ protected function set_up_test(): array {
7689
* @covers ::assign_supports
7790
*/
7891
public function test_penalty_support(): void {
79-
$this->resetAfterTest();
8092
$this->setAdminUser();
8193

8294
// Assign should be in the supported list.
@@ -150,7 +162,6 @@ public function test_apply_penalty(
150162
): void {
151163
global $DB;
152164

153-
$this->resetAfterTest();
154165
[$course, $student] = $this->set_up_test();
155166

156167
// Assignment.
@@ -224,8 +235,6 @@ public function test_apply_penalty(
224235
public function test_recalculate_penalty(): void {
225236
global $DB;
226237

227-
$this->resetAfterTest();
228-
229238
[$course, $student] = $this->set_up_test();
230239

231240
// Assignment.
@@ -276,4 +285,51 @@ public function test_recalculate_penalty(): void {
276285
// Check the grade.
277286
$this->assertEquals(50, $gradeitem->get_final($student->id)->finalgrade);
278287
}
288+
289+
/**
290+
* Test recalculating penalties for hidden groups and users.
291+
*
292+
* @return void
293+
*/
294+
public function test_recalculate_penalties(): void {
295+
global $DB;
296+
297+
// Setup the course.
298+
$course = $this->getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS]);
299+
$context = context_course::instance($course->id);
300+
$this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
301+
$group = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'visibility' => GROUPS_VISIBILITY_NONE]);
302+
303+
// Setup users.
304+
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
305+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
306+
groups_add_member($group, $student);
307+
308+
// Enable grade penalties for the assign module.
309+
penalty_manager::enable_module('assign');
310+
311+
// Log in as the teacher.
312+
$this->setUser($teacher);
313+
314+
// Check initial capabilities.
315+
$this->assertNotEmpty(groups_get_members($group->id));
316+
317+
// Update capabilities to prevent viewing hidden groups.
318+
$roleid = $DB->get_field('role', 'id', ['shortname' => 'teacher']);
319+
assign_capability('moodle/course:viewhiddengroups', CAP_PREVENT, $roleid, $context->id);
320+
321+
// Check updated capabilities.
322+
$this->assertEmpty(groups_get_members($group->id));
323+
324+
// Create an exemption, triggering penalty recalculation.
325+
$exemption = penalty_exemption::exempt_group($group->id, $context->id);
326+
$this->assertCount(1, manager::get_adhoc_tasks(\mod_assign\task\recalculate_penalties::class));
327+
328+
// Log in as admin to always view hidden groups.
329+
$this->setAdminUser();
330+
331+
// Delete the exemption, triggering penalty recalculation.
332+
$exemption->delete();
333+
$this->assertCount(2, manager::get_adhoc_tasks(\mod_assign\task\recalculate_penalties::class));
334+
}
279335
}

0 commit comments

Comments
 (0)