From f057a2aef9b4af9f1cb46f0a077958e0748d7afa Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Wed, 29 Mar 2023 10:59:13 +0800 Subject: [PATCH] MDL-76445 grade: Helper that returns all gradable users in a course --- grade/lib.php | 31 ++++++++++++++++++++ grade/tests/lib_test.php | 63 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/grade/lib.php b/grade/lib.php index 1d68991383a..f069d54da72 100644 --- a/grade/lib.php +++ b/grade/lib.php @@ -781,6 +781,37 @@ function grade_get_plugin_info($courseid, $active_type, $active_plugin) { return $plugin_info; } +/** + * Load a valid list of gradable users in a course. + * + * @param int $courseid The course ID. + * @param int|null $groupid The group ID (optional). + * @return array $users A list of enrolled gradable users. + */ +function get_gradable_users(int $courseid, ?int $groupid = null): array { + global $CFG; + + $context = context_course::instance($courseid); + // Create a graded_users_iterator because it will properly check the groups etc. + $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol); + $onlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol) || + !has_capability('moodle/course:viewsuspendedusers', $context); + + $course = get_course($courseid); + $gui = new graded_users_iterator($course, null, $groupid); + $gui->require_active_enrolment($onlyactiveenrol); + $gui->init(); + + // Flatten the users. + $users = []; + while ($user = $gui->next_user()) { + $users[$user->user->id] = $user->user; + } + $gui->close(); + + return $users; +} + /** * A simple class containing info about grade plugins. * Can be subclassed for special rules diff --git a/grade/tests/lib_test.php b/grade/tests/lib_test.php index a8173ef80b1..eedff6ddc07 100644 --- a/grade/tests/lib_test.php +++ b/grade/tests/lib_test.php @@ -711,4 +711,67 @@ class lib_test extends \advanced_testcase { $this->assertArrayHasKey('assign', $gradeitems2); $this->assertArrayHasKey('manual', $gradeitems2); } + + /** + * Test get_gradable_users() function. + * + * @covers ::get_gradable_users + */ + public function test_get_gradable_users() { + global $DB; + + $this->setAdminUser(); + $this->resetAfterTest(true); + + $roleteacher = $DB->get_record('role', ['shortname' => 'teacher'], '*', MUST_EXIST); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + $coursecontext = \context_course::instance($course->id); + // Create groups. + $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]); + $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]); + // Create and enrol a teacher and some students into the course. + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $student3 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + // Add student1 and student2 to group1. + $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $student1->id]); + $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $student2->id]); + // Add student3 to group2. + $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $student3->id]); + + // Perform a regrade before creating the report. + grade_regrade_final_grades($course->id); + // Should return all gradable users (only students). + $gradableusers = get_gradable_users($course->id); + $this->assertEqualsCanonicalizing([$student1->id, $student2->id, $student3->id], array_keys($gradableusers)); + + // Now, let's suspend the enrolment of student2. + $this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student', 'manual', 0, 0, ENROL_USER_SUSPENDED); + // Should return only the active gradable users (student1 and student3). + $gradableusers = get_gradable_users($course->id); + $this->assertEqualsCanonicalizing([$student1->id, $student3->id], array_keys($gradableusers)); + + // Give teacher 'viewsuspendedusers' capability and set a preference to display suspended users. + assign_capability('moodle/course:viewsuspendedusers', CAP_ALLOW, $roleteacher->id, $coursecontext, true); + set_user_preference('grade_report_showonlyactiveenrol', false, $teacher); + accesslib_clear_all_caches_for_unit_testing(); + + $this->setUser($teacher); + // Should return all gradable users (including suspended enrolments). + $gradableusers = get_gradable_users($course->id); + $this->assertEqualsCanonicalizing([$student1->id, $student2->id, $student3->id], array_keys($gradableusers)); + + // Reactivate the course enrolment of student2. + $this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student', 'manual', 0, 0, ENROL_USER_ACTIVE); + $this->setAdminUser(); + // Should return all gradable users from group1 (student1 and student2). + $gradableusers = get_gradable_users($course->id, $group1->id); + $this->assertEqualsCanonicalizing([$student1->id, $student2->id], array_keys($gradableusers)); + // Should return all gradable users from group2 (student3). + $gradableusers = get_gradable_users($course->id, $group2->id); + $this->assertEqualsCanonicalizing([$student3->id], array_keys($gradableusers)); + } }