Skip to content

Commit

Permalink
MDL-56789 core: Recycle bin warn only if a grade item is being deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Aug 7, 2019
1 parent ec564a7 commit 3ba7f31
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
24 changes: 22 additions & 2 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1289,16 +1289,36 @@ function course_module_flag_for_async_deletion($cmid) {
* Checks whether the given course has any course modules scheduled for adhoc deletion.
*
* @param int $courseid the id of the course.
* @param bool $onlygradable whether to check only gradable modules or all modules.
* @return bool true if the course contains any modules pending deletion, false otherwise.
*/
function course_modules_pending_deletion($courseid) {
function course_modules_pending_deletion($courseid, bool $onlygradable = false) : bool {
if (empty($courseid)) {
return false;
}

if ($onlygradable) {
// Fetch modules with grade items.
if (!$coursegradeitems = grade_item::fetch_all(['itemtype' => 'mod', 'courseid' => $courseid])) {
// Return early when there is none.
return false;
}
}

$modinfo = get_fast_modinfo($courseid);
foreach ($modinfo->get_cms() as $module) {
if ($module->deletioninprogress == '1') {
return true;
if ($onlygradable) {
// Check if the module being deleted is in the list of course modules with grade items.
foreach ($coursegradeitems as $coursegradeitem) {
if ($coursegradeitem->itemmodule == $module->modname && $coursegradeitem->iteminstance == $module->instance) {
// The module being deleted is within the gradable modules.
return true;
}
}
} else {
return true;
}
}
}
return false;
Expand Down
11 changes: 7 additions & 4 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5100,19 +5100,22 @@ public function test_course_get_recent_courses() {
*/
public function provider_course_modules_pending_deletion() {
return [
['forum', true],
['assign', true],
['forum', false, true],
['assign', false, true],
['forum', true, false],
['assign', true, true],
];
}

/**
* Tests the function course_modules_pending_deletion.
*
* @param string $module The module we want to test with
* @param bool $gradable The value to pass to the gradable argument of the course_modules_pending_deletion function
* @param bool $expected The expected result
* @dataProvider provider_course_modules_pending_deletion
*/
public function test_course_modules_pending_deletion(string $module, bool $expected) {
public function test_course_modules_pending_deletion(string $module, bool $gradable, bool $expected) {
$this->resetAfterTest();

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

course_delete_module($moduleinstance->cmid, true); // Try to delete the instance asynchronously.
$this->assertEquals($expected, course_modules_pending_deletion($course->id));
$this->assertEquals($expected, course_modules_pending_deletion($course->id, $gradable));
}
}
2 changes: 1 addition & 1 deletion grade/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ function print_grade_page_head($courseid, $active_type, $active_plugin=null,

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

0 comments on commit 3ba7f31

Please sign in to comment.