Skip to content

Commit

Permalink
MDL-70556 core db: change course fullname to 1333 chars
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timhunt committed Nov 15, 2024
1 parent 269a8a8 commit 68d3699
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 17 deletions.
4 changes: 2 additions & 2 deletions admin/tool/recyclebin/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/recyclebin/db" VERSION="20160315" COMMENT="XMLDB file for Moodle tool/recyclebin"
<XMLDB PATH="admin/tool/recyclebin/db" VERSION="20241115" COMMENT="XMLDB file for Moodle tool/recyclebin"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -26,7 +26,7 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="categoryid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="1333" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
Expand Down
65 changes: 65 additions & 0 deletions admin/tool/recyclebin/db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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;
}
20 changes: 20 additions & 0 deletions admin/tool/recyclebin/tests/category_bin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @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 {

Expand Down Expand Up @@ -74,6 +75,25 @@ public function test_pre_course_delete_hook(): void {
$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.
*/
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/recyclebin/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
8 changes: 6 additions & 2 deletions admin/tool/uploadcourse/classes/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,12 @@ public function prepare() {
}

// 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;
}

Expand Down
6 changes: 4 additions & 2 deletions backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1826,8 +1826,10 @@ public static function calculate_course_names($courseid, $fullname, $shortname)
}

// 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);
Expand Down
6 changes: 4 additions & 2 deletions backup/util/ui/classes/output/copy_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ public function definition() {
$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);
Expand Down
33 changes: 33 additions & 0 deletions course/classes/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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;

}
6 changes: 4 additions & 2 deletions course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function definition() {
$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);
Expand All @@ -67,7 +68,8 @@ function definition() {
$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);
Expand Down
6 changes: 4 additions & 2 deletions course/request_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ function definition() {

$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);
Expand Down
6 changes: 3 additions & 3 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="category" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="sortorder" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="254" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="1333" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="idnumber" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="summary" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
Expand Down Expand Up @@ -400,8 +400,8 @@
<TABLE NAME="course_request" COMMENT="course requests">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="254" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="1333" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="summary" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="summaryformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="category" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
Expand Down
71 changes: 71 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 68d3699

Please sign in to comment.