Skip to content

Commit 3ba7f31

Browse files
committed
MDL-56789 core: Recycle bin warn only if a grade item is being deleted
1 parent ec564a7 commit 3ba7f31

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

course/lib.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,16 +1289,36 @@ function course_module_flag_for_async_deletion($cmid) {
12891289
* Checks whether the given course has any course modules scheduled for adhoc deletion.
12901290
*
12911291
* @param int $courseid the id of the course.
1292+
* @param bool $onlygradable whether to check only gradable modules or all modules.
12921293
* @return bool true if the course contains any modules pending deletion, false otherwise.
12931294
*/
1294-
function course_modules_pending_deletion($courseid) {
1295+
function course_modules_pending_deletion($courseid, bool $onlygradable = false) : bool {
12951296
if (empty($courseid)) {
12961297
return false;
12971298
}
1299+
1300+
if ($onlygradable) {
1301+
// Fetch modules with grade items.
1302+
if (!$coursegradeitems = grade_item::fetch_all(['itemtype' => 'mod', 'courseid' => $courseid])) {
1303+
// Return early when there is none.
1304+
return false;
1305+
}
1306+
}
1307+
12981308
$modinfo = get_fast_modinfo($courseid);
12991309
foreach ($modinfo->get_cms() as $module) {
13001310
if ($module->deletioninprogress == '1') {
1301-
return true;
1311+
if ($onlygradable) {
1312+
// Check if the module being deleted is in the list of course modules with grade items.
1313+
foreach ($coursegradeitems as $coursegradeitem) {
1314+
if ($coursegradeitem->itemmodule == $module->modname && $coursegradeitem->iteminstance == $module->instance) {
1315+
// The module being deleted is within the gradable modules.
1316+
return true;
1317+
}
1318+
}
1319+
} else {
1320+
return true;
1321+
}
13021322
}
13031323
}
13041324
return false;

course/tests/courselib_test.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5100,19 +5100,22 @@ public function test_course_get_recent_courses() {
51005100
*/
51015101
public function provider_course_modules_pending_deletion() {
51025102
return [
5103-
['forum', true],
5104-
['assign', true],
5103+
['forum', false, true],
5104+
['assign', false, true],
5105+
['forum', true, false],
5106+
['assign', true, true],
51055107
];
51065108
}
51075109

51085110
/**
51095111
* Tests the function course_modules_pending_deletion.
51105112
*
51115113
* @param string $module The module we want to test with
5114+
* @param bool $gradable The value to pass to the gradable argument of the course_modules_pending_deletion function
51125115
* @param bool $expected The expected result
51135116
* @dataProvider provider_course_modules_pending_deletion
51145117
*/
5115-
public function test_course_modules_pending_deletion(string $module, bool $expected) {
5118+
public function test_course_modules_pending_deletion(string $module, bool $gradable, bool $expected) {
51165119
$this->resetAfterTest();
51175120

51185121
// Ensure recyclebin is enabled.
@@ -5125,6 +5128,6 @@ public function test_course_modules_pending_deletion(string $module, bool $expec
51255128
$moduleinstance = $generator->create_module($module, array('course' => $course->id));
51265129

51275130
course_delete_module($moduleinstance->cmid, true); // Try to delete the instance asynchronously.
5128-
$this->assertEquals($expected, course_modules_pending_deletion($course->id));
5131+
$this->assertEquals($expected, course_modules_pending_deletion($course->id, $gradable));
51295132
}
51305133
}

grade/lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ function print_grade_page_head($courseid, $active_type, $active_plugin=null,
986986

987987
// Put a warning on all gradebook pages if the course has modules currently scheduled for background deletion.
988988
require_once($CFG->dirroot . '/course/lib.php');
989-
if (course_modules_pending_deletion($courseid)) {
989+
if (course_modules_pending_deletion($courseid, true)) {
990990
\core\notification::add(get_string('gradesmoduledeletionpendingwarning', 'grades'),
991991
\core\output\notification::NOTIFY_WARNING);
992992
}

0 commit comments

Comments
 (0)