diff --git a/course/format/tests/behat/course_courseindex.feature b/course/format/tests/behat/course_courseindex.feature index ac2face58c4..d45931f5f9e 100644 --- a/course/format/tests/behat/course_courseindex.feature +++ b/course/format/tests/behat/course_courseindex.feature @@ -346,15 +346,29 @@ Feature: Course index depending on role | label | Activity sample 4 | Test label | C1 | sample4 | 2 | # Check resources without URL, as labels, are displayed in the CI and the link goes to the main page when it is clicked. When I am on the "sample1" "Activity" page logged in as "student1" - Then I should see "Test label" in the "#courseindex" "css_element" - And I click on "Test label" "link" in the "#courseindex" "css_element" + Then I should see "Activity sample 4" in the "#courseindex" "css_element" + And I click on "Activity sample 4" "link" in the "#courseindex" "css_element" And I should see "Test label" in the "region-main" "region" And I should see "Activity sample 2" in the "region-main" "region" # Check resources without URL, as labels, are displayed for teachers too, and the link is working even when edit mode is on. And I am on the "sample1" "Activity" page logged in as "teacher1" - And I should see "Test label" in the "#courseindex" "css_element" + And I should see "Activity sample 4" in the "#courseindex" "css_element" And I turn editing mode on - And I should see "Test label" in the "#courseindex" "css_element" - And I click on "Test label" "link" in the "#courseindex" "css_element" + And I should see "Activity sample 4" in the "#courseindex" "css_element" + And I click on "Activity sample 4" "link" in the "#courseindex" "css_element" And I should see "Test label" in the "region-main" "region" And I should see "Activity sample 2" in the "region-main" "region" + + @javascript + Scenario: Course index behaviour for labels with name or without name + # Add two labels to the course (one with name and one without name). + Given the following "activities" exist: + | activity | name | intro | course | idnumber | section | + | label | Activity sample 5 | Test label 1 | C1 | sample4 | 2 | + | label | | Test label 2 | C1 | sample5 | 2 | + When I am on the "Course 1" course page logged in as teacher1 + And I should see "Topic 2" in the "courseindex-content" "region" + # Label name should be displayed if it is set. + And I should see "Activity sample 5" in the "courseindex-content" "region" + # Label intro text should be displayed if label name is not set. + And I should see "Test label 2" in the "courseindex-content" "region" diff --git a/lib/testing/generator/module_generator.php b/lib/testing/generator/module_generator.php index c2946ca84b0..a4913e7f180 100644 --- a/lib/testing/generator/module_generator.php +++ b/lib/testing/generator/module_generator.php @@ -247,6 +247,10 @@ abstract class testing_module_generator extends component_generator_base { // Fill the name and intro with default values (if missing). if (empty($record->name)) { $record->name = get_string('pluginname', $this->get_modulename()).' '.$this->instancecount; + // Module label can be created without name specified. It will get its name from the intro's text. + if ($this->get_modulename() === 'label') { + $record->name = ''; + } } if (empty($record->introeditor) && empty($record->intro)) { $record->intro = 'Test '.$this->get_modulename().' ' . $this->instancecount; diff --git a/mod/label/lang/en/label.php b/mod/label/lang/en/label.php index 0a50c887442..5856a0b9363 100644 --- a/mod/label/lang/en/label.php +++ b/mod/label/lang/en/label.php @@ -43,6 +43,8 @@ $string['indicator:socialbreadthdef_help'] = 'The participant has reached this p $string['indicator:socialbreadthdef_link'] = 'Learning_analytics_indicators#Social_breadth'; $string['label:addinstance'] = 'Add a new Text and media area'; $string['label:view'] = 'View Text and media area'; +$string['labelname'] = 'Name'; +$string['labelname_help'] = 'Will be shown in course index, activity completion, etc. If left empty, will automatically be generated from the first characters of the text.'; $string['labeltext'] = 'Text'; $string['modulename'] = 'Text and media area'; $string['modulename_help'] = 'The Text and media area enables you to display text and multimedia on the course page. diff --git a/mod/label/lib.php b/mod/label/lib.php index a9258f8d459..aff5cbceaa0 100644 --- a/mod/label/lib.php +++ b/mod/label/lib.php @@ -34,6 +34,11 @@ define("LABEL_MAX_NAME_LENGTH", 50); * @return string */ function get_label_name($label) { + // Return label name if not empty. + if ($label->name) { + return $label->name; + } + $context = context_module::instance($label->coursemodule); $intro = format_text($label->intro, $label->introformat, ['filter' => false, 'context' => $context]); $name = html_to_text(format_string($intro, true, ['context' => $context])); diff --git a/mod/label/mod_form.php b/mod/label/mod_form.php index fec23dfcd57..e84417e6133 100644 --- a/mod/label/mod_form.php +++ b/mod/label/mod_form.php @@ -37,6 +37,16 @@ class mod_label_mod_form extends moodleform_mod { $mform = $this->_form; $mform->addElement('header', 'generalhdr', get_string('general')); + + // Add element for name. + $mform->addElement('text', 'name', get_string('labelname', 'label'), array('size' => '64')); + if (!empty($CFG->formatstringstriptags)) { + $mform->setType('name', PARAM_TEXT); + } else { + $mform->setType('name', PARAM_CLEANHTML); + } + $mform->addHelpButton('name', 'labelname', 'label'); + $this->standard_intro_elements(get_string('labeltext', 'label')); // Label does not add "Show description" checkbox meaning that 'intro' is always shown on the course page. @@ -51,4 +61,22 @@ class mod_label_mod_form extends moodleform_mod { } + /** + * Override validation in order to make name field non-required. + * + * @param array $data + * @param array $files + * @return array + */ + public function validation($data, $files) { + $errors = parent::validation($data, $files); + // Name field should not be required. + if (array_key_exists('name', $errors)) { + if ($errors['name'] === get_string('required')) { + unset($errors['name']); + } + } + return $errors; + } + } diff --git a/mod/label/tests/behat/label_name.feature b/mod/label/tests/behat/label_name.feature new file mode 100644 index 00000000000..fd827efe8ed --- /dev/null +++ b/mod/label/tests/behat/label_name.feature @@ -0,0 +1,30 @@ +@mod @mod_label + +Feature: Set label name + As a teacher + I should be able to create a label activity and set a name + + Background: + Given the following "courses" exist: + | fullname | shortname | category | + | Test | C1 | 0 | + And the following "users" exist: + | username | firstname | lastname | email | + | teacher | Teacher | Frist | teacher1@example.com | + And the following "course enrolments" exist: + | user | course | role | + | teacher | C1 | editingteacher | + And the following "activities" exist: + | activity | course | section | intro | idnumber | + | label | C1 | 1 | Intro Text | C1LABEL1 | + + Scenario: label name input box should be shown and can be set + When I log in as "teacher" + And I am on "Test" course homepage + And "Intro Text" activity should be visible + And I am on the "Intro Text" "label activity editing" page logged in as teacher + And I should see "Name" in the "General" "fieldset" + And I set the field "Name" to "Test Label 1" + And I press "Save and return to course" + And I am on "Test" course homepage + Then "Test Label 1" activity should be visible diff --git a/mod/label/tests/lib_test.php b/mod/label/tests/lib_test.php index 90f16ed8464..f6c5afd8946 100644 --- a/mod/label/tests/lib_test.php +++ b/mod/label/tests/lib_test.php @@ -207,23 +207,26 @@ class lib_test extends \advanced_testcase { /** * Check label name with different content inserted in the label intro. * - * @param string $labelcontent - * @param string $labelformat - * @param string $expectedlabelname + * @param string $name + * @param string $content + * @param string $format + * @param string $expectedname * @return void * @covers \get_label_name * @dataProvider label_get_name_data_provider */ - public function test_label_get_label_name(string $labelcontent, string $labelformat, string $expectedlabelname): void { + public function test_label_get_label_name(string $name, string $content, string $format, string $expectedname): void { $course = $this->getDataGenerator()->create_course(); // When creating the module, get_label_name is called and fills label->name. $label = $this->getDataGenerator()->create_module('label', [ + 'name' => $name, 'course' => $course->id, - 'intro' => $labelcontent, - 'introformat' => $labelformat + 'intro' => $content, + 'introformat' => $format ] ); - $this->assertEquals($expectedlabelname, $label->name); + + $this->assertEquals($expectedname, $label->name); } /** @@ -233,17 +236,26 @@ class lib_test extends \advanced_testcase { */ public function label_get_name_data_provider(): array { return [ + 'withlabelname' => [ + 'name' => 'Test label 1', + 'content' => '
Simple textual content
', + 'format' => FORMAT_HTML, + 'expected' => 'Test label 1' + ], 'simple' => [ + 'name' => '', 'content' => '
Simple textual content
', 'format' => FORMAT_HTML, 'expected' => 'Simple textual content' ], 'empty' => [ + 'name' => '', 'content' => '', 'format' => FORMAT_HTML, 'expected' => 'Test label 1' ], 'withaudiocontent' => [ + 'name' => '', 'content' => '
Test with audio