From 869b26f3e311eae8fc09cc101855b0b5e93ed84b Mon Sep 17 00:00:00 2001 From: Stevani Andolo Date: Wed, 5 Apr 2023 11:09:42 +0800 Subject: [PATCH] MDL-77829 core: Added environment check for mod_assignment Decided to add an environment check before uninstalling the mod_assignment plugin to prevent data lost. --- admin/environment.xml | 5 +++++ lang/en/admin.php | 1 + lib/tests/upgradelib_test.php | 23 +++++++++++++++++++++++ lib/upgradelib.php | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/admin/environment.xml b/admin/environment.xml index 8ecafae5fb6..fe56aa2e253 100644 --- a/admin/environment.xml +++ b/admin/environment.xml @@ -4109,6 +4109,11 @@ + + + + + diff --git a/lang/en/admin.php b/lang/en/admin.php index 7e867c49572..bc9ede9e8e2 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -1460,6 +1460,7 @@ $string['xmlrpcmnetenabled'] = 'It has been detected that the Moodle Networking $string['xmlrpcwebserviceenabled'] = 'It has been detected that the XML-RPC Web Service protocol is enabled on your site. This feature relies on the PHP XML-RPC extension which is no longer maintained by PHP.'; $string['yuicomboloading'] = 'YUI combo loading'; $string['ziprequired'] = 'The Zip PHP extension is now required by Moodle, info-ZIP binaries or PclZip library are not used anymore.'; +$string['modassignmentinuse'] = 'It has been detected that your site is still using the Assignment 2.2 plugin. You may resolve this before upgrading by 1) Backing up your Assignment 2.2 activities and restoring them as new Assignment activities; or 2) Deleting the data from the assignment tables in the database.'; $string['caching'] = 'Caching'; diff --git a/lib/tests/upgradelib_test.php b/lib/tests/upgradelib_test.php index e6bc221f1c7..a846e1b9605 100644 --- a/lib/tests/upgradelib_test.php +++ b/lib/tests/upgradelib_test.php @@ -1526,4 +1526,27 @@ class upgradelib_test extends advanced_testcase { $this->assertEquals('admin_dir_usage', $result->getInfo()); $this->assertFalse($result->getStatus()); } + + /** + * Test the check_mod_assignment check if mod_assignment is still used. + * + * @covers ::check_mod_assignment + * @return void + */ + public function test_check_mod_assignment_is_used(): void { + global $DB; + + $this->resetAfterTest(); + $result = new environment_results('custom_checks'); + + if ($DB->get_manager()->table_exists('assignment')) { + $DB->insert_record('assignment', (object)['name' => 'test_assign', 'intro' => 'test_assign_intro']); + + $this->assertNotNull(check_mod_assignment($result)); + $this->assertEquals('Assignment 2.2 is in use', $result->getInfo()); + $this->assertFalse($result->getStatus()); + } else { + $this->assertTrue($result->getStatus()); + } + } } diff --git a/lib/upgradelib.php b/lib/upgradelib.php index c4fa412ca2e..e5ccf3ba17e 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -2766,3 +2766,22 @@ function check_xmlrpc_usage(environment_results $result): ?environment_results { return null; } + +/** + * Check whether the mod_assignment is currently being used. + * + * @param environment_results $result + * @return environment_results|null + */ +function check_mod_assignment(environment_results $result): ?environment_results { + global $DB; + + // Check the number of records. + if ($DB->get_manager()->table_exists('assignment') && $DB->count_records('assignment') > 0) { + $result->setInfo('Assignment 2.2 is in use'); + $result->setFeedbackStr('modassignmentinuse'); + return $result; + } + + return null; +}