Skip to content

Commit

Permalink
MDL-68652 core_grades: Refactor grades functions.
Browse files Browse the repository at this point in the history
get_gradable_users now has extra param to retrieve only active
users. Grade reports hase their own grade_report::get_gradable_users
since they decided whether to inlcude active users from report preferences
  • Loading branch information
ilyatregubov committed Dec 5, 2023
1 parent d18acd7 commit 175eb51
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
11 changes: 2 additions & 9 deletions grade/lib.php
Expand Up @@ -786,17 +786,10 @@ function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
*
* @param int $courseid The course ID.
* @param int|null $groupid The group ID (optional).
* @param bool $onlyactiveenrol Include only active enrolments.
* @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);

function get_gradable_users(int $courseid, ?int $groupid = null, bool $onlyactiveenrol = false): array {
$course = get_course($courseid);
$gui = new graded_users_iterator($course, null, $groupid);
$gui->require_active_enrolment($onlyactiveenrol);
Expand Down
21 changes: 21 additions & 0 deletions grade/report/lib.php
Expand Up @@ -932,5 +932,26 @@ public function item_types(): array {

return $modnames;
}

/**
* 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.
*/
public static function get_gradable_users(int $courseid, ?int $groupid = null): array {
global $CFG;
require_once($CFG->dirroot . '/grade/lib.php');

$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);

return get_gradable_users($courseid, $groupid, $onlyactiveenrol);
}

}

3 changes: 2 additions & 1 deletion grade/report/singleview/classes/local/screen/grade.php
Expand Up @@ -24,6 +24,7 @@

namespace gradereport_singleview\local\screen;

use grade_report;
use gradereport_singleview\local\ui\range;
use gradereport_singleview\local\ui\bulk_insert;
use grade_grade;
Expand Down Expand Up @@ -151,7 +152,7 @@ public function original_definition(): array {
*/
public function init($selfitemisempty = false) {

$this->items = $this->load_users();
$this->items = grade_report::get_gradable_users($this->courseid, $this->groupid);
$this->totalitemcount = count($this->items);

if ($selfitemisempty) {
Expand Down
2 changes: 2 additions & 0 deletions grade/report/singleview/classes/local/screen/screen.php
Expand Up @@ -25,6 +25,7 @@
namespace gradereport_singleview\local\screen;

use context_course;
use grade_report;
use moodle_url;
use html_writer;
use grade_structure;
Expand All @@ -33,6 +34,7 @@
use stdClass;

defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/grade/report/lib.php');

/**
* Abstract class used as a base for the 3 screens.
Expand Down
2 changes: 1 addition & 1 deletion grade/report/singleview/classes/local/screen/user.php
Expand Up @@ -102,7 +102,7 @@ public function item_type(): string {
public function init($selfitemisempty = false) {

if (!$selfitemisempty) {
$validusers = $this->load_users();
$validusers = \grade_report::get_gradable_users($this->courseid, $this->groupid);
if (!isset($validusers[$this->itemid])) {
// If the passed user id is not valid, show the first user from the list instead.
$this->item = reset($validusers);
Expand Down
6 changes: 4 additions & 2 deletions grade/report/singleview/index.php
Expand Up @@ -27,6 +27,7 @@
require_once('../../../config.php');
require_once($CFG->dirroot.'/lib/gradelib.php');
require_once($CFG->dirroot.'/grade/lib.php');
require_once($CFG->dirroot.'/grade/report/lib.php');

$courseid = required_param('id', PARAM_INT);
$groupid = optional_param('group', null, PARAM_INT);
Expand Down Expand Up @@ -90,7 +91,8 @@
// If there is a stored user item (last viewed) in a session variable, bypass the user select zero state
// and display this user item. Also, make sure that the stored last viewed user is part of the current
// list of gradable users in this course.
if ($lastvieweduseritemid && array_key_exists($lastvieweduseritemid, get_gradable_users($courseid, $currentgroup))) {
if ($lastvieweduseritemid &&
array_key_exists($lastvieweduseritemid, grade_report::get_gradable_users($courseid, $currentgroup))) {
$itemtype = 'user';
$itemid = $lastvieweduseritemid;
} else {
Expand All @@ -103,7 +105,7 @@
}
// If the item id (user id) cannot be defined or the user id is not part of the list of gradable users,
// display the user select zero state.
if (is_null($itemid) || !array_key_exists($itemid, get_gradable_users($courseid, $currentgroup))) {
if (is_null($itemid) || !array_key_exists($itemid, grade_report::get_gradable_users($courseid, $currentgroup))) {
$itemtype = 'user_select';
}
break;
Expand Down
2 changes: 1 addition & 1 deletion grade/report/user/index.php
Expand Up @@ -113,7 +113,7 @@
$userid = $lastvieweduserid;
}

$gradableusers = get_gradable_users($courseid, $currentgroup);
$gradableusers = grade_report::get_gradable_users($courseid, $currentgroup);
// Validate whether the requested user is a valid gradable user in this course. If, not display the user select
// zero state.
if (empty($gradableusers) || ($userid && !array_key_exists($userid, $gradableusers))) {
Expand Down
8 changes: 4 additions & 4 deletions grade/tests/lib_test.php
Expand Up @@ -751,7 +751,7 @@ public function test_get_gradable_users() {
// 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);
$gradableusers = \grade_report::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.
Expand All @@ -761,17 +761,17 @@ public function test_get_gradable_users() {

$this->setUser($teacher);
// Should return all gradable users (including suspended enrolments).
$gradableusers = get_gradable_users($course->id);
$gradableusers = \grade_report::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);
$gradableusers = \grade_report::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);
$gradableusers = \grade_report::get_gradable_users($course->id, $group2->id);
$this->assertEqualsCanonicalizing([$student3->id], array_keys($gradableusers));
}
}
1 change: 1 addition & 0 deletions grade/upgrade.txt
Expand Up @@ -2,6 +2,7 @@ This file describes API changes in /grade/* ;
Information provided here is intended especially for developers.

=== 4.2.3 ===
* The function get_gradable_users() in grade/lib.php has extra param now to retrieve only active enrolments.
* The grade `itemname` property contained in the return structure of the following external methods is now PARAM_CLEANHTML:
- `core_grades_get_gradeitems`
- `gradereport_user_get_grade_items`
Expand Down

0 comments on commit 175eb51

Please sign in to comment.