From 68d369917e729e49830fab614c02784bcb98211c Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 5 Sep 2024 14:43:26 +0200 Subject: [PATCH] MDL-70556 core db: change course fullname to 1333 chars This is mainly for the benefit of multilang users. As well as changing the database schema, it is also necessary to change where this is inforced in PHP code (forms, web services). This was missed when shortname was changed from 100 to 255 chars some time ago, but I have fixed that here. Note, I have not changed the legacy mnet code, since as far as I am aware that is deprecated. --- admin/tool/recyclebin/db/install.xml | 4 +- admin/tool/recyclebin/db/upgrade.php | 65 +++++++++++++++++ .../recyclebin/tests/category_bin_test.php | 20 ++++++ admin/tool/recyclebin/version.php | 2 +- admin/tool/uploadcourse/classes/course.php | 8 ++- backup/util/dbops/restore_dbops.class.php | 6 +- backup/util/ui/classes/output/copy_form.php | 6 +- course/classes/constants.php | 33 +++++++++ course/edit_form.php | 6 +- course/request_form.php | 6 +- lib/db/install.xml | 6 +- lib/db/upgrade.php | 71 +++++++++++++++++++ version.php | 2 +- 13 files changed, 218 insertions(+), 17 deletions(-) create mode 100644 admin/tool/recyclebin/db/upgrade.php create mode 100644 course/classes/constants.php diff --git a/admin/tool/recyclebin/db/install.xml b/admin/tool/recyclebin/db/install.xml index 2afaa3da05a..8aa6c9d45b8 100644 --- a/admin/tool/recyclebin/db/install.xml +++ b/admin/tool/recyclebin/db/install.xml @@ -1,5 +1,5 @@ - @@ -26,7 +26,7 @@ - + diff --git a/admin/tool/recyclebin/db/upgrade.php b/admin/tool/recyclebin/db/upgrade.php new file mode 100644 index 00000000000..e7a9b05f9c9 --- /dev/null +++ b/admin/tool/recyclebin/db/upgrade.php @@ -0,0 +1,65 @@ +. + +/** + * Upgrade script for tool_recyclebin. + * + * @package tool_recyclebin + * @copyright The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Upgrade the plugin. + * + * @param int $oldversion + * @return bool always true + */ +function xmldb_tool_recyclebin_upgrade($oldversion) { + global $DB; + + $dbman = $DB->get_manager(); + + // Automatically generated Moodle v4.1.0 release upgrade line. + // Put any upgrade step following this. + + // Automatically generated Moodle v4.2.0 release upgrade line. + // Put any upgrade step following this. + + // Automatically generated Moodle v4.3.0 release upgrade line. + // Put any upgrade step following this. + + // Automatically generated Moodle v4.4.0 release upgrade line. + // Put any upgrade step following this. + + // Automatically generated Moodle v4.5.0 release upgrade line. + // Put any upgrade step following this. + + if ($oldversion < 2024111500) { + + // Changing precision of field fullname on table tool_recyclebin_category to (255). + $table = new xmldb_table('tool_recyclebin_category'); + $field = new xmldb_field('fullname', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'shortname'); + + // Launch change of precision for field fullname. + $dbman->change_field_precision($table, $field); + + // Recyclebin savepoint reached. + upgrade_plugin_savepoint(true, 2024111500, 'tool', 'recyclebin'); + } + + return true; +} diff --git a/admin/tool/recyclebin/tests/category_bin_test.php b/admin/tool/recyclebin/tests/category_bin_test.php index 7876b7ac4b2..6205924806c 100644 --- a/admin/tool/recyclebin/tests/category_bin_test.php +++ b/admin/tool/recyclebin/tests/category_bin_test.php @@ -22,6 +22,7 @@ namespace tool_recyclebin; * @package tool_recyclebin * @copyright 2015 University of Kent * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \tool_recyclebin\category_bin */ class category_bin_test extends \advanced_testcase { @@ -74,6 +75,25 @@ class category_bin_test extends \advanced_testcase { $this->assertEquals(1, count($recyclebin->get_items())); } + public function test_delete_course_with_long_names(): void { + global $DB; + + // Create a course with its name at the upper limit of what is allowed. + $course = $this->getDataGenerator()->create_course([ + 'shortname' => str_repeat("S", \core_course\constants::SHORTNAME_MAXIMUM_LENGTH), + 'fullname' => str_repeat("F", \core_course\constants::FULLNAME_MAXIMUM_LENGTH), + ]); + + // Should have nothing in the recycle bin. + $this->assertEquals(0, $DB->count_records('tool_recyclebin_category')); + + delete_course($course, false); + + // Verify the course is now in the recycle bin. + $recyclebin = new \tool_recyclebin\category_bin($course->category); + $this->assertCount(1, $recyclebin->get_items()); + } + /** * Check that our hook is called when a course is deleted. */ diff --git a/admin/tool/recyclebin/version.php b/admin/tool/recyclebin/version.php index b16fa0b0b5c..b7511c5333a 100644 --- a/admin/tool/recyclebin/version.php +++ b/admin/tool/recyclebin/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024100700; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2024111500; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2024100100; // Requires this Moodle version. $plugin->component = 'tool_recyclebin'; // Full name of the plugin (used for diagnostics). diff --git a/admin/tool/uploadcourse/classes/course.php b/admin/tool/uploadcourse/classes/course.php index f808817ccfc..6600bf0b1be 100644 --- a/admin/tool/uploadcourse/classes/course.php +++ b/admin/tool/uploadcourse/classes/course.php @@ -509,8 +509,12 @@ class tool_uploadcourse_course { } // Ensure we don't overflow the maximum length of the fullname field. - if (!empty($coursedata['fullname']) && core_text::strlen($coursedata['fullname']) > 254) { - $this->error('invalidfullnametoolong', new lang_string('invalidfullnametoolong', 'tool_uploadcourse', 254)); + if ( + !empty($coursedata['fullname']) && + core_text::strlen($coursedata['fullname']) > \core_course\constants::FULLNAME_MAXIMUM_LENGTH + ) { + $this->error('invalidfullnametoolong', new lang_string('invalidfullnametoolong', 'tool_uploadcourse', + \core_course\constants::FULLNAME_MAXIMUM_LENGTH)); return false; } diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 42f3d18511a..7a164174d5b 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -1826,8 +1826,10 @@ abstract class restore_dbops { } // Ensure we don't overflow maximum length of name fields, in multi-byte safe manner. - $currentfullname = core_text::substr($fullname, 0, 254 - strlen($suffixfull)) . $suffixfull; - $currentshortname = core_text::substr($shortname, 0, 100 - strlen($suffixshort)) . $suffixshort; + $currentfullname = core_text::substr($fullname, 0, + \core_course\constants::FULLNAME_MAXIMUM_LENGTH - strlen($suffixfull)) . $suffixfull; + $currentshortname = core_text::substr($shortname, 0, + \core_course\constants::SHORTNAME_MAXIMUM_LENGTH - strlen($suffixshort)) . $suffixshort; $coursefull = $DB->get_record_select('course', 'fullname = ? AND id != ?', array($currentfullname, $courseid), '*', IGNORE_MULTIPLE); diff --git a/backup/util/ui/classes/output/copy_form.php b/backup/util/ui/classes/output/copy_form.php index ae24b6028e8..9c86d210523 100644 --- a/backup/util/ui/classes/output/copy_form.php +++ b/backup/util/ui/classes/output/copy_form.php @@ -89,13 +89,15 @@ class copy_form extends \moodleform { $mform->addElement('html', \html_writer::div(get_string('copycoursedesc', 'backup'), 'form-description mb-6')); // Course fullname. - $mform->addElement('text', 'fullname', get_string('fullnamecourse'), 'maxlength="254" size="50"'); + $mform->addElement('text', 'fullname', get_string('fullnamecourse'), + ['maxlength' => \core_course\constants::FULLNAME_MAXIMUM_LENGTH, 'size' => 50]); $mform->addHelpButton('fullname', 'fullnamecourse'); $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); $mform->setType('fullname', PARAM_TEXT); // Course shortname. - $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"'); + $mform->addElement('text', 'shortname', get_string('shortnamecourse'), + ['maxlength' => \core_course\constants::SHORTNAME_MAXIMUM_LENGTH, 'size' => 20]); $mform->addHelpButton('shortname', 'shortnamecourse'); $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client'); $mform->setType('shortname', PARAM_TEXT); diff --git a/course/classes/constants.php b/course/classes/constants.php new file mode 100644 index 00000000000..4242d77a838 --- /dev/null +++ b/course/classes/constants.php @@ -0,0 +1,33 @@ +. + +namespace core_course; + +/** + * Constants related to courses. + * + * @package core_course + * @copyright The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class constants { + /** @var int the length of the course.shortname field. */ + public const FULLNAME_MAXIMUM_LENGTH = 1333; + + /** @var int the length of the course.shortname field. */ + public const SHORTNAME_MAXIMUM_LENGTH = 255; + +} diff --git a/course/edit_form.php b/course/edit_form.php index a6e1f2edecf..adae645b40f 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -58,7 +58,8 @@ class course_edit_form extends moodleform { $mform->setType('returnurl', PARAM_LOCALURL); $mform->setConstant('returnurl', $returnurl); - $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"'); + $mform->addElement('text', 'fullname', get_string('fullnamecourse'), + ['maxlength' => \core_course\constants::FULLNAME_MAXIMUM_LENGTH, 'size' => 50]); $mform->addHelpButton('fullname', 'fullnamecourse'); $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); $mform->setType('fullname', PARAM_TEXT); @@ -67,7 +68,8 @@ class course_edit_form extends moodleform { $mform->setConstant('fullname', $course->fullname); } - $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"'); + $mform->addElement('text', 'shortname', get_string('shortnamecourse'), + ['maxlength' => \core_course\constants::SHORTNAME_MAXIMUM_LENGTH, 'size' => 20]); $mform->addHelpButton('shortname', 'shortnamecourse'); $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client'); $mform->setType('shortname', PARAM_TEXT); diff --git a/course/request_form.php b/course/request_form.php index 332da47852d..ede417d9d65 100644 --- a/course/request_form.php +++ b/course/request_form.php @@ -58,12 +58,14 @@ class course_request_form extends moodleform { $mform->addElement('header','coursedetails', get_string('courserequestdetails')); - $mform->addElement('text', 'fullname', get_string('fullnamecourse'), 'maxlength="254" size="50"'); + $mform->addElement('text', 'fullname', get_string('fullnamecourse'), + ['maxlength' => \core_course\constants::FULLNAME_MAXIMUM_LENGTH, 'size' => 50]); $mform->addHelpButton('fullname', 'fullnamecourse'); $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); $mform->setType('fullname', PARAM_TEXT); - $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"'); + $mform->addElement('text', 'shortname', get_string('shortnamecourse'), + ['maxlength' => \core_course\constants::SHORTNAME_MAXIMUM_LENGTH, 'size' => 20]); $mform->addHelpButton('shortname', 'shortnamecourse'); $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client'); $mform->setType('shortname', PARAM_TEXT); diff --git a/lib/db/install.xml b/lib/db/install.xml index 1c8bd15227d..dd261b09044 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -72,7 +72,7 @@ - + @@ -400,8 +400,8 @@ - - + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index a310c6ab9d6..90f76cebc9d 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1474,5 +1474,76 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2024110800.02); } + if ($oldversion < 2024111500.00) { + + // Changing precision of field fullname on table course to (1333). + $table = new xmldb_table('course'); + $field = new xmldb_field('fullname', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'sortorder'); + + // Launch change of precision for field fullname. + $dbman->change_field_precision($table, $field); + + // Main savepoint reached. + upgrade_main_savepoint(true, 2024111500.00); + } + + if ($oldversion < 2024111500.01) { + + // Changing precision of field fullname on table course_request to (1333). + $table = new xmldb_table('course_request'); + $field = new xmldb_field('fullname', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'id'); + + // Launch change of precision for field fullname. + $dbman->change_field_precision($table, $field); + + // Main savepoint reached. + upgrade_main_savepoint(true, 2024111500.01); + } + + // Now we want to change the precision of course_request.shortname. + // To do this, we need to first drop the index, then re-create it. + if ($oldversion < 2024111500.02) { + + // Define index shortname (not unique) to be dropped form course_request. + $table = new xmldb_table('course_request'); + $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, ['shortname']); + + // Conditionally launch drop index shortname. + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2024111500.02); + } + + if ($oldversion < 2024111500.03) { + + // Changing precision of field shortname on table course_request to (255). + $table = new xmldb_table('course_request'); + $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'fullname'); + + // Launch change of precision for field shortname. + $dbman->change_field_precision($table, $field); + + // Main savepoint reached. + upgrade_main_savepoint(true, 2024111500.03); + } + + if ($oldversion < 2024111500.04) { + + // Define index shortname (not unique) to be added to course_request. + $table = new xmldb_table('course_request'); + $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, ['shortname']); + + // Conditionally launch add index shortname. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2024111500.04); + } + return true; } diff --git a/version.php b/version.php index cf9d917a2c8..0c967829b46 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2024111500.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2024111500.04; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '5.0dev (Build: 20241115)'; // Human-friendly version name