Merge branch 'MDL-56789-36' of git://github.com/rezaies/moodle into MOODLE_36_STABLE
This commit is contained in:
+22
-2
@@ -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;
|
||||
|
||||
@@ -5092,4 +5092,50 @@ class core_course_courselib_testcase extends advanced_testcase {
|
||||
$this->assertCount(3, $result);
|
||||
$this->assertArrayNotHasKey($courses[0]->id, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_course_modules_pending_deletion.
|
||||
*
|
||||
* @return array An array of arrays contain test data
|
||||
*/
|
||||
public function provider_course_modules_pending_deletion() {
|
||||
return [
|
||||
'Non-gradable activity, check all' => [['forum'], 0, false, true],
|
||||
'Gradable activity, check all' => [['assign'], 0, false, true],
|
||||
'Non-gradable activity, check gradables' => [['forum'], 0, true, false],
|
||||
'Gradable activity, check gradables' => [['assign'], 0, true, true],
|
||||
'Non-gradable within multiple, check all' => [['quiz', 'forum', 'assign'], 1, false, true],
|
||||
'Non-gradable within multiple, check gradables' => [['quiz', 'forum', 'assign'], 1, true, false],
|
||||
'Gradable within multiple, check all' => [['quiz', 'forum', 'assign'], 2, false, true],
|
||||
'Gradable within multiple, check gradables' => [['quiz', 'forum', 'assign'], 2, true, true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the function course_modules_pending_deletion.
|
||||
*
|
||||
* @param string[] $modules A complete list aff all available modules before deletion
|
||||
* @param int $indextodelete The index of the module in the $modules array that 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(array $modules, int $indextodelete, bool $gradable, bool $expected) {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Ensure recyclebin is enabled.
|
||||
set_config('coursebinenable', true, 'tool_recyclebin');
|
||||
|
||||
// Create course and modules.
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
|
||||
$moduleinstances = [];
|
||||
foreach ($modules as $module) {
|
||||
$moduleinstances[] = $generator->create_module($module, array('course' => $course->id));
|
||||
}
|
||||
|
||||
course_delete_module($moduleinstances[$indextodelete]->cmid, true); // Try to delete the instance asynchronously.
|
||||
$this->assertEquals($expected, course_modules_pending_deletion($course->id, $gradable));
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user