From f1bfc8971f8516040c647dbece35bb643aef2807 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 10 Sep 2015 16:55:05 +0800 Subject: [PATCH] MDL-51250 course: Display the default section name This patch makes the default section name appear in brackets beside the 'Use default section name' checkbox. --- .../tests/moodle2_course_format_test.php | 15 +++ course/editsection.php | 13 ++- course/editsection_form.php | 13 ++- course/format/lib.php | 18 +++- course/format/topics/lib.php | 23 ++++- .../tests/behat/edit_delete_sections.feature | 16 ++++ .../topics/tests/format_topics_test.php | 85 +++++++++++++++++ course/format/upgrade.txt | 4 + course/format/weeks/lib.php | 17 +++- .../tests/behat/edit_delete_sections.feature | 16 ++++ .../format/weeks/tests/format_weeks_test.php | 91 +++++++++++++++++++ 11 files changed, 303 insertions(+), 8 deletions(-) diff --git a/backup/moodle2/tests/moodle2_course_format_test.php b/backup/moodle2/tests/moodle2_course_format_test.php index 787db969833..47b9971c62d 100644 --- a/backup/moodle2/tests/moodle2_course_format_test.php +++ b/backup/moodle2/tests/moodle2_course_format_test.php @@ -220,6 +220,21 @@ class core_backup_moodle2_course_format_testcase extends advanced_testcase { * Test course format that has 1 option. */ class format_test_cs_options extends format_topics { + /** + * Override method format_topics::get_default_section_name to prevent PHPUnit errors related to the nonexistent + * format_test_cs_options lang file. + * + * @param stdClass $section The section in question. + * @return string The section's name for display. + */ + public function get_default_section_name($section) { + if ($section->section == 0) { + return parent::get_default_section_name($section); + } else { + return get_string('sectionname', 'format_topics') . ' ' . $section->section; + } + } + public function section_format_options($foreditform = false) { return array( 'numdaystocomplete' => array( diff --git a/course/editsection.php b/course/editsection.php index ea4724aecc1..1aff7314de5 100644 --- a/course/editsection.php +++ b/course/editsection.php @@ -80,8 +80,17 @@ if ($deletesection) { } $editoroptions = array('context'=>$context ,'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true); -$mform = course_get_format($course->id)->editsection_form($PAGE->url, - array('cs' => $sectioninfo, 'editoroptions' => $editoroptions)); + +$courseformat = course_get_format($course); +$defaultsectionname = $courseformat->get_default_section_name($section); + +$customdata = array( + 'cs' => $sectioninfo, + 'editoroptions' => $editoroptions, + 'defaultsectionname' => $defaultsectionname +); +$mform = $courseformat->editsection_form($PAGE->url, $customdata); + // set current value, make an editable copy of section_info object // this will retrieve all format-specific options as well $initialdata = convert_to_array($sectioninfo); diff --git a/course/editsection_form.php b/course/editsection_form.php index c6144706b50..f4a9df4a467 100644 --- a/course/editsection_form.php +++ b/course/editsection_form.php @@ -25,7 +25,16 @@ class editsection_form extends moodleform { $elementgroup = array(); $elementgroup[] = $mform->createElement('text', 'name', '', array('size' => '30', 'maxlength' => '255')); - $elementgroup[] = $mform->createElement('checkbox', 'usedefaultname', '', get_string('sectionusedefaultname')); + + // Get default section name. + $defaultsectionname = $this->_customdata['defaultsectionname']; + if ($defaultsectionname) { + $defaultsectionname = ' [' . $defaultsectionname . ']'; + } + + $elementgroup[] = $mform->createElement('checkbox', 'usedefaultname', '', + get_string('sectionusedefaultname') . $defaultsectionname); + $mform->addGroup($elementgroup, 'name_group', get_string('sectionname'), ' ', false); $mform->addGroupRule('name_group', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255)))); @@ -103,7 +112,7 @@ class editsection_form extends moodleform { $data = parent::get_data(); if ($data !== null) { $editoroptions = $this->_customdata['editoroptions']; - if (!empty($data->usedefaultname)) { + if (!empty($data->usedefaultname) || empty(trim($data->name))) { $data->name = null; } $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, diff --git a/course/format/lib.php b/course/format/lib.php index 8d656106dc8..d5423f776cd 100644 --- a/course/format/lib.php +++ b/course/format/lib.php @@ -350,7 +350,23 @@ abstract class format_base { } else { $sectionnum = $section; } - return get_string('sectionname', 'format_'.$this->format) . ' ' . $sectionnum; + + if (get_string_manager()->string_exists('sectionname', 'format_' . $this->format)) { + return get_string('sectionname', 'format_' . $this->format) . ' ' . $sectionnum; + } + + // Return an empty string if there's no available section name string for the given format. + return ''; + } + + /** + * Returns the default section using format_base's implementation of get_section_name. + * + * @param int|stdClass $section Section object from database or just field course_sections section + * @return string The default value for the section name based on the given course format. + */ + public function get_default_section_name($section) { + return self::get_section_name($section); } /** diff --git a/course/format/topics/lib.php b/course/format/topics/lib.php index 034b58e00c2..228b1227dd6 100644 --- a/course/format/topics/lib.php +++ b/course/format/topics/lib.php @@ -57,10 +57,29 @@ class format_topics extends format_base { if ((string)$section->name !== '') { return format_string($section->name, true, array('context' => context_course::instance($this->courseid))); - } else if ($section->section == 0) { + } else { + return $this->get_default_section_name($section); + } + } + + /** + * 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 format_base::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. + * + * @param 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. return get_string('section0name', 'format_topics'); } else { - return get_string('topic').' '.$section->section; + // Use format_base::get_default_section_name implementation which + // will display the section name in "Topic n" format. + return parent::get_default_section_name($section); } } diff --git a/course/format/topics/tests/behat/edit_delete_sections.feature b/course/format/topics/tests/behat/edit_delete_sections.feature index 6973dea0fb8..150c5e3bfc2 100644 --- a/course/format/topics/tests/behat/edit_delete_sections.feature +++ b/course/format/topics/tests/behat/edit_delete_sections.feature @@ -24,6 +24,22 @@ Feature: Sections can be edited and deleted in topics format And I follow "Course 1" And I turn editing mode on + Scenario: View the default name of the general section in topics format + When I click on "Edit section" "link" in the "li#section-0" "css_element" + Then I should see "Use default section name [General]" + + Scenario: Edit the default name of the general section in topics format + When I click on "Edit section" "link" in the "li#section-0" "css_element" + And I set the following fields to these values: + | Use default section name | 0 | + | name | This is the general section | + And I press "Save changes" + Then I should see "This is the general section" in the "li#section-0" "css_element" + + Scenario: View the default name of the second section in topics format + When I click on "Edit topic" "link" in the "li#section-2" "css_element" + Then I should see "Use default section name [Topic 2]" + Scenario: Edit section summary in topics format When I edit the section "2" And I set the following fields to these values: diff --git a/course/format/topics/tests/format_topics_test.php b/course/format/topics/tests/format_topics_test.php index 09dea51a6cd..3253e45c5c6 100644 --- a/course/format/topics/tests/format_topics_test.php +++ b/course/format/topics/tests/format_topics_test.php @@ -61,4 +61,89 @@ class format_topics_testcase extends advanced_testcase { $this->assertEquals(8, count(get_fast_modinfo($course)->get_section_info_all())); $this->assertEquals(6, course_get_format($course)->get_course()->numsections); } + + /** + * Tests for format_topics::get_section_name method with default section names. + */ + public function test_get_section_name() { + global $DB; + $this->resetAfterTest(true); + + // Generate a course with 5 sections. + $generator = $this->getDataGenerator(); + $numsections = 5; + $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'), + array('createsections' => true)); + + // Get section names for course. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + + // Test get_section_name with default section names. + $courseformat = course_get_format($course); + foreach ($coursesections as $section) { + // Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name. + $this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section)); + } + } + + /** + * Tests for format_topics::get_section_name method with modified section names. + */ + public function test_get_section_name_customised() { + global $DB; + $this->resetAfterTest(true); + + // Generate a course with 5 sections. + $generator = $this->getDataGenerator(); + $numsections = 5; + $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'), + array('createsections' => true)); + + // Get section names for course. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + + // Modify section names. + $customname = "Custom Section"; + foreach ($coursesections as $section) { + $section->name = "$customname $section->section"; + $DB->update_record('course_sections', $section); + } + + // Requery updated section names then test get_section_name. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + $courseformat = course_get_format($course); + foreach ($coursesections as $section) { + // Assert that with modified section names, get_section_name returns the modified section name. + $this->assertEquals($section->name, $courseformat->get_section_name($section)); + } + } + + /** + * Tests for format_topics::get_default_section_name. + */ + public function test_get_default_section_name() { + global $DB; + $this->resetAfterTest(true); + + // Generate a course with 5 sections. + $generator = $this->getDataGenerator(); + $numsections = 5; + $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'), + array('createsections' => true)); + + // Get section names for course. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + + // Test get_default_section_name with default section names. + $courseformat = course_get_format($course); + foreach ($coursesections as $section) { + if ($section->section == 0) { + $sectionname = get_string('section0name', 'format_topics'); + $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); + } else { + $sectionname = get_string('sectionname', 'format_topics') . ' ' . $section->section; + $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); + } + } + } } diff --git a/course/format/upgrade.txt b/course/format/upgrade.txt index 2f216c202ae..fce6e1c20f3 100644 --- a/course/format/upgrade.txt +++ b/course/format/upgrade.txt @@ -7,6 +7,10 @@ Overview of this plugin type at http://docs.moodle.org/dev/Course_formats renderable menu or array of links. Plugin calls to section_edit_controls will now include the section edit control in the returned array. * The section name is now wrapped in a new span (.sectionname > span), process_sections method in format.js should be updated so .sectionname DOM node's wraps the section title in a span. You can look at how to implement the change in course/format/topics/format.js or MDL-48947. +* New method format_base::get_default_section_name retrieves the default section name for the given course format. The base + implementation basically uses the implementation of format_base::get_section_name. The method can be overridden in + format_base subclasses that use sections (i.e. format_topics, format_weeks). In relation to the changes made for the default + section name, the default section name is now being shown when editing the section information. === 2.9 === * Course formats may support deleting sections, see MDL-10405 for more details. diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 59a58b302f2..7bcf1f46ee7 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -55,7 +55,22 @@ class format_weeks extends format_base { if ((string)$section->name !== '') { // Return the name the user set. return format_string($section->name, true, array('context' => context_course::instance($this->courseid))); - } else if ($section->section == 0) { + } else { + return $this->get_default_section_name($section); + } + } + + /** + * Returns the default section name for the weekly course format. + * + * If the section number is 0, it will use the string with key = section0name from the course format's lang file. + * Otherwise, the default format of "[start date] - [end date]" will be returned. + * + * @param 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. return get_string('section0name', 'format_weeks'); } else { diff --git a/course/format/weeks/tests/behat/edit_delete_sections.feature b/course/format/weeks/tests/behat/edit_delete_sections.feature index 255fa837976..0e296c75df2 100644 --- a/course/format/weeks/tests/behat/edit_delete_sections.feature +++ b/course/format/weeks/tests/behat/edit_delete_sections.feature @@ -24,6 +24,22 @@ Feature: Sections can be edited and deleted in weeks format And I follow "Course 1" And I turn editing mode on + Scenario: View the default name of the general section in weeks format + When I click on "Edit section" "link" in the "li#section-0" "css_element" + Then I should see "Use default section name [General]" + + Scenario: Edit the default name of the general section in weeks format + When I click on "Edit section" "link" in the "li#section-0" "css_element" + And I set the following fields to these values: + | Use default section name | 0 | + | name | This is the general section | + And I press "Save changes" + Then I should see "This is the general section" in the "li#section-0" "css_element" + + Scenario: View the default name of the second section in weeks format + When I click on "Edit week" "link" in the "li#section-2" "css_element" + Then I should see "Use default section name [8 May - 14 May]" + Scenario: Edit section summary in weeks format When I click on "Edit week" "link" in the "li#section-2" "css_element" And I set the following fields to these values: diff --git a/course/format/weeks/tests/format_weeks_test.php b/course/format/weeks/tests/format_weeks_test.php index a388b59706e..ad014fc6913 100644 --- a/course/format/weeks/tests/format_weeks_test.php +++ b/course/format/weeks/tests/format_weeks_test.php @@ -61,4 +61,95 @@ class format_weeks_testcase extends advanced_testcase { $this->assertEquals(8, count(get_fast_modinfo($course)->get_section_info_all())); $this->assertEquals(6, course_get_format($course)->get_course()->numsections); } + + /** + * Tests for format_weeks::get_section_name method with default section names. + */ + public function test_get_section_name() { + global $DB; + $this->resetAfterTest(true); + + // Generate a course with 5 sections. + $generator = $this->getDataGenerator(); + $numsections = 5; + $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'), + array('createsections' => true)); + + // Get section names for course. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + + // Test get_section_name with default section names. + $courseformat = course_get_format($course); + foreach ($coursesections as $section) { + // Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name. + $this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section)); + } + } + + /** + * Tests for format_weeks::get_section_name method with modified section names. + */ + public function test_get_section_name_customised() { + global $DB; + $this->resetAfterTest(true); + + // Generate a course with 5 sections. + $generator = $this->getDataGenerator(); + $numsections = 5; + $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'), + array('createsections' => true)); + + // Get section names for course. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + + // Modify section names. + $customname = "Custom Section"; + foreach ($coursesections as $section) { + $section->name = "$customname $section->section"; + $DB->update_record('course_sections', $section); + } + + // Requery updated section names then test get_section_name. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + $courseformat = course_get_format($course); + foreach ($coursesections as $section) { + // Assert that with modified section names, get_section_name returns the modified section name. + $this->assertEquals($section->name, $courseformat->get_section_name($section)); + } + } + + /** + * Tests for format_weeks::get_default_section_name. + */ + public function test_get_default_section_name() { + global $DB; + $this->resetAfterTest(true); + + // Generate a course with 5 sections. + $generator = $this->getDataGenerator(); + $numsections = 5; + $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'), + array('createsections' => true)); + + // Get section names for course. + $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); + + // Test get_default_section_name with default section names. + $courseformat = course_get_format($course); + foreach ($coursesections as $section) { + if ($section->section == 0) { + $sectionname = get_string('section0name', 'format_weeks'); + $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); + } else { + $dates = $courseformat->get_section_dates($section); + $dates->end = ($dates->end - 86400); + $dateformat = get_string('strftimedateshort'); + $weekday = userdate($dates->start, $dateformat); + $endweekday = userdate($dates->end, $dateformat); + $sectionname = $weekday.' - '.$endweekday; + + $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); + } + } + } }