Skip to content

Commit 87fa24d

Browse files
committed
Merge branch 'MDL-56789-37' of git://github.com/rezaies/moodle into MOODLE_37_STABLE
2 parents 2666fb1 + db51a59 commit 87fa24d

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5092,4 +5092,50 @@ public function test_course_get_recent_courses() {
50925092
$this->assertCount(3, $result);
50935093
$this->assertArrayNotHasKey($courses[0]->id, $result);
50945094
}
5095+
5096+
/**
5097+
* Data provider for test_course_modules_pending_deletion.
5098+
*
5099+
* @return array An array of arrays contain test data
5100+
*/
5101+
public function provider_course_modules_pending_deletion() {
5102+
return [
5103+
'Non-gradable activity, check all' => [['forum'], 0, false, true],
5104+
'Gradable activity, check all' => [['assign'], 0, false, true],
5105+
'Non-gradable activity, check gradables' => [['forum'], 0, true, false],
5106+
'Gradable activity, check gradables' => [['assign'], 0, true, true],
5107+
'Non-gradable within multiple, check all' => [['quiz', 'forum', 'assign'], 1, false, true],
5108+
'Non-gradable within multiple, check gradables' => [['quiz', 'forum', 'assign'], 1, true, false],
5109+
'Gradable within multiple, check all' => [['quiz', 'forum', 'assign'], 2, false, true],
5110+
'Gradable within multiple, check gradables' => [['quiz', 'forum', 'assign'], 2, true, true],
5111+
];
5112+
}
5113+
5114+
/**
5115+
* Tests the function course_modules_pending_deletion.
5116+
*
5117+
* @param string[] $modules A complete list aff all available modules before deletion
5118+
* @param int $indextodelete The index of the module in the $modules array that we want to test with
5119+
* @param bool $gradable The value to pass to the gradable argument of the course_modules_pending_deletion function
5120+
* @param bool $expected The expected result
5121+
* @dataProvider provider_course_modules_pending_deletion
5122+
*/
5123+
public function test_course_modules_pending_deletion(array $modules, int $indextodelete, bool $gradable, bool $expected) {
5124+
$this->resetAfterTest();
5125+
5126+
// Ensure recyclebin is enabled.
5127+
set_config('coursebinenable', true, 'tool_recyclebin');
5128+
5129+
// Create course and modules.
5130+
$generator = $this->getDataGenerator();
5131+
$course = $generator->create_course();
5132+
5133+
$moduleinstances = [];
5134+
foreach ($modules as $module) {
5135+
$moduleinstances[] = $generator->create_module($module, array('course' => $course->id));
5136+
}
5137+
5138+
course_delete_module($moduleinstances[$indextodelete]->cmid, true); // Try to delete the instance asynchronously.
5139+
$this->assertEquals($expected, course_modules_pending_deletion($course->id, $gradable));
5140+
}
50955141
}

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)