From 076aead150a916e5e02896c11cbe4a9adc8b49cb Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 9 Feb 2024 15:53:33 +0100 Subject: [PATCH] MDL-80460 format_topics: Replace name from "Topic X" with "New section" From now on, the default section name for the topics course format is "New section" instead of "Topic x" (where x was the section number). A new item, initsections, has been added to the create_course() function in the testing_data_generator class, to let the generator rename the sections to "Section X" AMOS BEGIN CPY [sectionname,format_topics],[legacysectionname,format_topics] AMOS END --- course/format/topics/db/upgrade.php | 20 +++++++++ .../format/topics/lang/en/format_topics.php | 4 +- course/format/topics/lib.php | 15 +++---- .../topics/tests/format_topics_test.php | 2 +- course/format/topics/version.php | 2 +- course/format/upgrade.txt | 2 + course/tests/search/search_test.php | 2 +- lib/testing/generator/data_generator.php | 43 ++++++++++++++----- lib/testing/tests/testing_generator_test.php | 36 ++++++++++++++++ 9 files changed, 102 insertions(+), 24 deletions(-) diff --git a/course/format/topics/db/upgrade.php b/course/format/topics/db/upgrade.php index 4ff7f313035..8690e5f38a5 100644 --- a/course/format/topics/db/upgrade.php +++ b/course/format/topics/db/upgrade.php @@ -29,6 +29,8 @@ * @return bool result */ function xmldb_format_topics_upgrade($oldversion) { + global $DB; + // Automatically generated Moodle v4.1.0 release upgrade line. // Put any upgrade step following this. @@ -49,5 +51,23 @@ function xmldb_format_topics_upgrade($oldversion) { // Automatically generated Moodle v4.3.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2023100901) { + // During the migration to version 4.4, ensure that sections with null names are renamed to their corresponding + // previous 'Topic X' for continuity. + $newsectionname = $DB->sql_concat("'Topic '", 'section'); + $sql = << 0 AND (name IS NULL OR name = '') + AND course IN (SELECT id FROM {course} WHERE format = 'topics') + EOF; + $DB->execute( + sql: $sql, + ); + + // Main savepoint reached. + upgrade_plugin_savepoint(true, 2023100901, 'format', 'topics'); + } + return true; } diff --git a/course/format/topics/lang/en/format_topics.php b/course/format/topics/lang/en/format_topics.php index 30f6556cc7e..f1d0fcd8eac 100644 --- a/course/format/topics/lang/en/format_topics.php +++ b/course/format/topics/lang/en/format_topics.php @@ -24,6 +24,8 @@ $string['currentsection'] = 'Current section'; $string['hidefromothers'] = 'Hide'; +$string['legacysectionname'] = 'Topic'; +$string['newsection'] = 'New section'; $string['page-course-view-topics'] = 'Any course main page in custom sections format'; $string['page-course-view-topics-x'] = 'Any course page in custom sections format'; $string['pluginname'] = 'Custom sections'; @@ -34,5 +36,5 @@ $string['indentation_help'] = 'Allow teachers, and other users with the manage a $string['section_highlight_feedback'] = 'Section {$a->name} highlighted.'; $string['section_unhighlight_feedback'] = 'Highlighting removed from section {$a->name}.'; $string['section0name'] = 'General'; -$string['sectionname'] = 'Topic'; +$string['sectionname'] = 'Section'; $string['showfromothers'] = 'Show'; diff --git a/course/format/topics/lib.php b/course/format/topics/lib.php index b894e0be2f5..575280d85ce 100644 --- a/course/format/topics/lib.php +++ b/course/format/topics/lib.php @@ -76,21 +76,18 @@ class format_topics extends core_courseformat\base { * Returns the default section name for the topics course format. * * If the section number is 0, it will use the string with key = section0name from the course format's lang file. - * If the section number is not 0, the base implementation of course_format::get_default_section_name which uses - * the string with the key = 'sectionname' from the course format's lang file + the section number will be used. + * If the section number is not 0, it will consistently return the name 'newsection', disregarding the specific section number. * - * @param stdClass $section Section object from database or just field course_sections section + * @param int|stdClass $section Section object from database or just field course_sections section * @return string The default value for the section name. */ public function get_default_section_name($section) { - if ($section->section == 0) { - // Return the general section. + $section = $this->get_section($section); + if ($section->sectionnum == 0) { return get_string('section0name', 'format_topics'); - } else { - // Use course_format::get_default_section_name implementation which - // will display the section name in "Topic n" format. - return parent::get_default_section_name($section); } + + return get_string('newsection', 'format_topics'); } /** diff --git a/course/format/topics/tests/format_topics_test.php b/course/format/topics/tests/format_topics_test.php index d28b5ecc4e9..0d10c4346c7 100644 --- a/course/format/topics/tests/format_topics_test.php +++ b/course/format/topics/tests/format_topics_test.php @@ -118,7 +118,7 @@ class format_topics_test extends \advanced_testcase { $sectionname = get_string('section0name', 'format_topics'); $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); } else { - $sectionname = get_string('sectionname', 'format_topics') . ' ' . $section->section; + $sectionname = get_string('newsection', 'format_topics'); $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); } } diff --git a/course/format/topics/version.php b/course/format/topics/version.php index 807082e9e16..9aae2107b8f 100644 --- a/course/format/topics/version.php +++ b/course/format/topics/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023100900; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2023100901; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2023100400; // Requires this Moodle version. $plugin->component = 'format_topics'; // Full name of the plugin (used for diagnostics). diff --git a/course/format/upgrade.txt b/course/format/upgrade.txt index a0a07f5cc1b..b7c63e15108 100644 --- a/course/format/upgrade.txt +++ b/course/format/upgrade.txt @@ -52,6 +52,8 @@ and replaced by core_courseformat\base::set_sectionnum() and core_courseformat\b value when all the sections must be displayed (instead of 0). That way, section 0 (General), can be displayed on a single page too. * The course format setting has been updated to use a choice dropdown form element instead of a select element. Third-party plugins can now include the string 'plugin_description' to provide a description of the course format. +* A new item, initsections, has been added to the testing_data_generator::create_course() function, to let the generator rename +the sections to "Section X". === 4.3 === * New core_courseformat\output\activitybadge class that can be extended by any module to display content near the activity name. diff --git a/course/tests/search/search_test.php b/course/tests/search/search_test.php index 9e72b197331..7498ad1a7fd 100644 --- a/course/tests/search/search_test.php +++ b/course/tests/search/search_test.php @@ -372,7 +372,7 @@ class search_test extends \advanced_testcase { $this->assertEquals($course->id, $doc->get('courseid')); $this->assertFalse($doc->is_set('userid')); $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid')); - $this->assertEquals('Topic 3', $doc->get('title')); + $this->assertEquals('New section', $doc->get('title')); $this->assertEquals('Kermit', $doc->get('content')); // Test with user-set title. diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 25e8dae53b0..334a790dd6a 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -14,15 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Data generator. - * - * @package core - * @category test - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - defined('MOODLE_INTERNAL') || die(); /** @@ -357,9 +348,10 @@ EOD; /** * Create a test course - * @param array|stdClass $record + * @param array|stdClass $record Apart from the course information, the following can be also set: + * 'initsections' => bool for section name initialization, renaming them to "Section X". Default value is 0 (false). * @param array $options with keys: - * 'createsections'=>bool precreate all sections + * 'createsections' => bool precreate all sections * @return stdClass course record */ public function create_course($record=null, array $options=null) { @@ -427,12 +419,41 @@ EOD; } } + $initsections = !empty($record['initsections']); + unset($record['initsections']); + $course = create_course((object)$record); + if ($initsections) { + $this->init_sections($course); + } context_course::instance($course->id); return $course; } + /** + * Initializes sections for a specified course, such as configuring section names for courses using 'Section X'. + * + * @param stdClass $course The course object. + */ + private function init_sections(stdClass $course): void { + global $DB; + + $sections = $DB->get_records('course_sections', ['course' => $course->id], 'section'); + foreach ($sections as $section) { + if ($section->section != 0) { + $DB->set_field( + table: 'course_sections', + newfield: 'name', + newvalue: get_string('section', 'core') . ' ' . $section->section, + conditions: [ + 'id' => $section->id, + ], + ); + } + } + } + /** * Create course section if does not exist yet * @param array|stdClass $record must contain 'course' and 'section' attributes diff --git a/lib/testing/tests/testing_generator_test.php b/lib/testing/tests/testing_generator_test.php index 46ae0f9e926..7b6c6d519d1 100644 --- a/lib/testing/tests/testing_generator_test.php +++ b/lib/testing/tests/testing_generator_test.php @@ -23,6 +23,7 @@ namespace core; * @category phpunit * @copyright 2012 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \core\testing_data_generator */ class testing_generator_test extends \advanced_testcase { public function test_get_plugin_generator_good_case() { @@ -168,6 +169,7 @@ class testing_generator_test extends \advanced_testcase { $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3)); $this->assertEquals($course->id, $section->course); + $this->assertNull($section->name); $course = $generator->create_course(array('tags' => 'Cat, Dog')); $this->assertEquals(array('Cat', 'Dog'), array_values(\core_tag_tag::get_item_tags_array('core', 'course', $course->id))); @@ -176,6 +178,40 @@ class testing_generator_test extends \advanced_testcase { $this->assertNotEmpty($scale); } + /** + * Test case for the `test_create_course_initsections` method. + * + * This method tests the behavior of creating a course with initialized sections. + * It checks that the sections are renamed to "Section x" when `initsections` is true, + * and that the sections are not renamed when `initsections` is false. + * + * @covers ::create_course + */ + public function test_create_course_initsections(): void { + global $DB; + + $this->resetAfterTest(true); + $generator = $this->getDataGenerator(); + + // Check that the sections are renamed to "Section x" when initsections is true. + $course = $generator->create_course(['initsections' => 1]); + $sections = course_get_format($course)->get_sections(); + foreach ($sections as $section) { + if ($section->sectionnum > 0) { + $this->assertEquals(get_string('section') . ' ' . $section->sectionnum, $section->name); + } + } + + // Check that the sections are not renamed when initsections is false. + $course = $generator->create_course(['initsections' => 0]); + $sections = course_get_format($course)->get_sections(); + foreach ($sections as $section) { + if ($section->sectionnum > 0) { + $this->assertNull($section->name); + } + } + } + public function test_create_module() { global $CFG, $SITE, $DB;