MDL-72350 core: Added environment check for mod_assignment

Decided to add an environment check before uninstalling the
mod_assignment plugin to prevent data lost.
This commit is contained in:
Stevani Andolo 2023-03-13 17:21:51 +08:00
parent 7310e99dda
commit 26d78531e7
4 changed files with 64 additions and 2 deletions

View File

@ -4109,6 +4109,11 @@
</CUSTOM_CHECK>
<CUSTOM_CHECK file="lib/upgradelib.php" function="check_xmlrpc_usage" level="optional">
</CUSTOM_CHECK>
<CUSTOM_CHECK file="lib/upgradelib.php" function="check_mod_assignment" level="required">
<FEEDBACK>
<ON_ERROR message="modassignmentinuse" />
</FEEDBACK>
</CUSTOM_CHECK>
</CUSTOM_CHECKS>
</MOODLE>
</COMPATIBILITY_MATRIX>

View File

@ -1570,6 +1570,7 @@ $string['xmlrpcwebserviceenabled'] = 'It has been detected that the XML-RPC Web
$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['manageqbanks'] = 'Manage question bank plugins';
$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';

View File

@ -1354,6 +1354,42 @@ class upgradelib_test extends advanced_testcase {
$this->assertFalse($result->getStatus());
}
/**
* Test the check_mod_assignment check if mod_assignment is still used.
*
* @return void
*/
public function test_check_mod_assignment_is_used(): void {
global $CFG, $DB;
$this->resetAfterTest();
$result = new environment_results('custom_checks');
if (file_exists("{$CFG->dirroot}/mod/assignment/version.php")) {
// This is for when the test is run on sites where mod_assignment is most likely reinstalled.
$this->assertNull(check_mod_assignment($result));
$this->assertTrue($result->getStatus());
} else {
// This is for when the test is run on sites with mod_assignment now gone.
$this->assertFalse($DB->get_manager()->table_exists('assignment'));
$this->assertNull(check_mod_assignment($result));
// Then we can simulate a scenario here where the assignment records are still present during the upgrade
// by recreating the assignment table and adding a record to it.
$dbman = $DB->get_manager();
$table = new xmldb_table('assignment');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
$table->add_field('name', XMLDB_TYPE_CHAR, '255');
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$dbman->create_table($table);
$DB->insert_record('assignment', (object)['name' => 'test_assign']);
$this->assertNotNull(check_mod_assignment($result));
$this->assertEquals('Assignment 2.2 is in use', $result->getInfo());
$this->assertFalse($result->getStatus());
}
}
/**
* Data provider of usermenu items.
*

View File

@ -1161,7 +1161,7 @@ function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
/**
* Log_display description function used during install and upgrade.
*
* @param string $component name of component (moodle, mod_assignment, etc.)
* @param string $component name of component (moodle, etc.)
* @return void
*/
function log_update_descriptions($component) {
@ -1218,7 +1218,7 @@ function log_update_descriptions($component) {
/**
* Web service discovery function used during install and upgrade.
* @param string $component name of component (moodle, mod_assignment, etc.)
* @param string $component name of component (moodle, etc.)
* @return void
*/
function external_update_descriptions($component) {
@ -2796,3 +2796,23 @@ 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 $CFG, $DB;
// Check the number of records.
if (!file_exists("{$CFG->dirroot}/mod/assignment/version.php") && $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;
}