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
This commit is contained in:
@@ -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 = <<<EOF
|
||||
UPDATE {course_sections}
|
||||
SET name = $newsectionname
|
||||
WHERE section > 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;
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user