From e37eb30a091296e0fb474742038241b3343491c9 Mon Sep 17 00:00:00 2001 From: Ilya Tregubov Date: Fri, 3 Nov 2023 13:36:59 +0800 Subject: [PATCH] MDL-76588 gradereport_summary: Summary report hack This is a hack since average is not correct for summary report when we have suspended users. But since we don't want to change display to user - it is set to match 4.1 behavior. --- .../classes/local/entities/grade_items.php | 3 +- grade/report/summary/lib.php | 13 +++++++ grade/tests/behat/grade_average.feature | 19 ++++++--- grade/tests/lib_test.php | 39 +++++++++---------- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/grade/report/summary/classes/local/entities/grade_items.php b/grade/report/summary/classes/local/entities/grade_items.php index f2a10ec9171..1733a0c0df3 100644 --- a/grade/report/summary/classes/local/entities/grade_items.php +++ b/grade/report/summary/classes/local/entities/grade_items.php @@ -93,7 +93,8 @@ class grade_items extends base { ); $this->report = new grade_report_summary($this->course->id, $gpr, $context); - $this->ungradedcounts = $this->report->ungraded_counts(); + $showonlyactiveenrol = $this->report->show_only_active(); + $this->ungradedcounts = $this->report->ungraded_counts(false, false, $showonlyactiveenrol); $columns = $this->get_all_columns(); foreach ($columns as $column) { diff --git a/grade/report/summary/lib.php b/grade/report/summary/lib.php index a47a313b665..90fbc1d3d1d 100644 --- a/grade/report/summary/lib.php +++ b/grade/report/summary/lib.php @@ -74,4 +74,17 @@ class grade_report_summary extends grade_report { public function process_data($data) { } + /** + * To check if we only need to include active enrolments. + * + * @return bool + */ + public function show_only_active(): bool { + + // Limit to users with an active enrolment. + $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol); + $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol); + return $showonlyactiveenrol || + !has_capability('moodle/course:viewsuspendedusers', $this->context); + } } diff --git a/grade/tests/behat/grade_average.feature b/grade/tests/behat/grade_average.feature index 9f26f233178..290bbf2f657 100644 --- a/grade/tests/behat/grade_average.feature +++ b/grade/tests/behat/grade_average.feature @@ -61,7 +61,12 @@ Feature: Average grades are displayed in the gradebook | -1- | -2- | -3- | | Overall average | 26.67 | 26.67 | And I am on the "Course 1" "grades > Grade summary > View" page - And I should see "26.67" in the "Manual item 1" "table_row" + # Average is (10 + 20 + 30)/3 = 30.00 for manual since + # 1. Hidden items are NOT included on grader report. + # 2. There is a bug when we have suspended users in the course so here Student 2 is included. + # So the average is not write when preference is either not set or set to 0. + # Possibly this should be changed later to match grader report. + And I should see "30.00" in the "Manual item 1" "table_row" And I am on the "Course 1" "grades > Grader report > View" page logged in as "teacher2" And the following "user preferences" exist: @@ -72,7 +77,8 @@ Feature: Average grades are displayed in the gradebook | -1- | -2- | -3- | | Overall average | 26.67 | 26.67 | And I am on the "Course 1" "grades > Grade summary > View" page - And I should see "26.67" in the "Manual item 1" "table_row" + # Average is (10 + 30)/2 = 20.00 for manual (when preference is set to 1 set average is correct). + And I should see "20.00" in the "Manual item 1" "table_row" And the following "user preferences" exist: | user | preference | value | | teacher2 | grade_report_showonlyactiveenrol | 0 | @@ -81,7 +87,8 @@ Feature: Average grades are displayed in the gradebook | -1- | -2- | -3- | | Overall average | 25.00 | 25.00 | And I am on the "Course 1" "grades > Grade summary > View" page - And I should see "25.00" in the "Manual item 1" "table_row" + # Average is (10 + 30)/2 = 20.00 for manual (when preference is set to 0 set average is NOT correct). + And I should see "20.00" in the "Manual item 1" "table_row" # Check the user grade table When I am on the "Course 1" "grades > user > View" page logged in as "student1" @@ -100,7 +107,7 @@ Feature: Average grades are displayed in the gradebook | -1- | -2- | -3- | | Overall average | 25.00 | 25.00 | And I am on the "Course 1" "grades > Grade summary > View" page - And I should see "25.00" in the "Manual item 1" "table_row" + And I should see "20.00" in the "Manual item 1" "table_row" And the following "user preferences" exist: | user | preference | value | | teacher2 | grade_report_showonlyactiveenrol | 1 | @@ -109,14 +116,14 @@ Feature: Average grades are displayed in the gradebook | -1- | -2- | -3- | | Overall average | 26.67 | 26.67 | And I am on the "Course 1" "grades > Grade summary > View" page - And I should see "26.67" in the "Manual item 1" "table_row" + And I should see "20.00" in the "Manual item 1" "table_row" And I am on the "Course 1" "grades > Grader report > View" page logged in as "teacher1" And the following should exist in the "user-grades" table: | -1- | -2- | -3- | | Overall average | 25.00 | 25.00 | And I am on the "Course 1" "grades > Grade summary > View" page - And I should see "25.00" in the "Manual item 1" "table_row" + And I should see "20.00" in the "Manual item 1" "table_row" And I am on the "Course 1" "grades > user > View" page logged in as "student1" And the following should exist in the "user-grade" table: diff --git a/grade/tests/lib_test.php b/grade/tests/lib_test.php index c579aaf6118..54a0793c17b 100644 --- a/grade/tests/lib_test.php +++ b/grade/tests/lib_test.php @@ -25,11 +25,10 @@ namespace core_grades; use assign; -use block_globalsearch\globalsearch_test; use cm_info; use grade_item; use grade_plugin_return; -use grade_report_summary; +use grade_report_grader; defined('MOODLE_INTERNAL') || die(); @@ -291,7 +290,7 @@ class lib_test extends \advanced_testcase { $gpr1 = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course1, ] ); @@ -299,13 +298,13 @@ class lib_test extends \advanced_testcase { $gpr2 = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course2, ] ); - $report1 = new grade_report_summary($course1->id, $gpr1, $context1); - $report2 = new grade_report_summary($course2->id, $gpr2, $context2); + $report1 = new grade_report_grader($course1->id, $gpr1, $context1); + $report2 = new grade_report_grader($course2->id, $gpr2, $context2); $ungradedcounts = []; $ungradedcounts[$course1->id] = $report1->ungraded_counts(false); @@ -420,12 +419,12 @@ class lib_test extends \advanced_testcase { $gpr = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course, ] ); - $report = new grade_report_summary($course->id, $gpr, $context); + $report = new grade_report_grader($course->id, $gpr, $context); $ungradedcounts = $report->ungraded_counts(false, $hidden); @@ -541,7 +540,7 @@ class lib_test extends \advanced_testcase { $gpr1 = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course, 'groupid' => $group1->id, ] @@ -550,14 +549,14 @@ class lib_test extends \advanced_testcase { $gpr2 = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course, 'groupid' => $group2->id, ] ); - $report1 = new grade_report_summary($course->id, $gpr1, $context); - $report2 = new grade_report_summary($course->id, $gpr2, $context); + $report1 = new grade_report_grader($course->id, $gpr1, $context); + $report2 = new grade_report_grader($course->id, $gpr2, $context); $ungradedcounts = []; $ungradedcounts[$group1->id] = $report1->ungraded_counts(true); @@ -692,12 +691,12 @@ class lib_test extends \advanced_testcase { $gpr = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course, ] ); - $report = new grade_report_summary($course->id, $gpr, $context); + $report = new grade_report_grader($course->id, $gpr, $context); $showonlyactiveenrol = $report->show_only_active(); $ungradedcounts = $report->ungraded_counts(false, false, $showonlyactiveenrol); @@ -853,12 +852,12 @@ class lib_test extends \advanced_testcase { $gpr = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course, ] ); - $report = new grade_report_summary($course->id, $gpr, $context); + $report = new grade_report_grader($course->id, $gpr, $context); $ungradedcounts = $report->ungraded_counts(false); $ungradedcounts['report']['meanselection'] = $meanselection; @@ -939,24 +938,24 @@ class lib_test extends \advanced_testcase { $gpr = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course1, ] ); - $report1 = new grade_report_summary($course1->id, $gpr, $context); + $report1 = new grade_report_grader($course1->id, $gpr, $context); $context = \context_course::instance($course2->id); $gpr = new grade_plugin_return( [ 'type' => 'report', - 'plugin' => 'summary', + 'plugin' => 'grader', 'course' => $course2, ] ); - $report2 = new grade_report_summary($course2->id, $gpr, $context); + $report2 = new grade_report_grader($course2->id, $gpr, $context); $gradeitems1 = $report1->item_types(); $gradeitems2 = $report2->item_types();