Merge branch 'MDL-78474-master' of https://github.com/aydevworks/moodle

This commit is contained in:
Ilya Tregubov 2023-09-11 10:54:57 +08:00
commit d1f45f3b0e
No known key found for this signature in database
GPG key ID: 0F58186F748E55C1
7 changed files with 118 additions and 12 deletions

View file

@ -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"

View file

@ -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;

View file

@ -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.

View file

@ -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]));

View file

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

View file

@ -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

View file

@ -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' => '<p>Simple textual content<p>',
'format' => FORMAT_HTML,
'expected' => 'Test label 1'
],
'simple' => [
'name' => '',
'content' => '<p>Simple textual content<p>',
'format' => FORMAT_HTML,
'expected' => 'Simple textual content'
],
'empty' => [
'name' => '',
'content' => '',
'format' => FORMAT_HTML,
'expected' => 'Test label 1'
],
'withaudiocontent' => [
'name' => '',
'content' => '<p>Test with audio</p>
<p>&nbsp; &nbsp;<audio controls="controls">
<source src="@@PLUGINFILE@@/moodle-hit-song.mp3">
@ -253,6 +265,7 @@ class lib_test extends \advanced_testcase {
'expected' => 'Test with audio'
],
'withvideo' => [
'name' => '',
'content' => '<p>Test video</p>
<p>&nbsp;<video controls="controls">
<source src="https://www.youtube.com/watch?v=xxxyy">
@ -262,6 +275,7 @@ class lib_test extends \advanced_testcase {
'expected' => 'Test video https://www.youtube.com/watch?v=xxxyy'
],
'with video trimming' => [
'name' => '',
'content' => '<p>Test with video to be trimmed</p>
<p>&nbsp;<video controls="controls">
<source src="https://www.youtube.com/watch?v=xxxyy">
@ -271,46 +285,55 @@ class lib_test extends \advanced_testcase {
'expected' => 'Test with video to be trimmed https://www.youtube....'
],
'with plain text' => [
'name' => '',
'content' => 'Content with @@PLUGINFILE@@/moodle-hit-song.mp3 nothing',
'format' => FORMAT_HTML,
'expected' => 'Content with nothing'
],
'with several spaces' => [
'name' => '',
'content' => "Content with @@PLUGINFILE@@/moodle-hit-song.mp3 \r &nbsp; several spaces",
'format' => FORMAT_HTML,
'expected' => 'Content with several spaces'
],
'empty spaces' => [
'name' => '',
'content' => ' &nbsp; ',
'format' => FORMAT_HTML,
'expected' => 'Text and media area'
],
'only html' => [
'name' => '',
'content' => '<audio controls="controls"><source src=""></audio>',
'format' => FORMAT_HTML,
'expected' => 'Text and media area'
],
'markdown' => [
'name' => '',
'content' => "##Simple Title\n simple markdown format",
'format' => FORMAT_MARKDOWN,
'expected' => 'Simple Title simple markdown format'
],
'markdown with pluginfile' => [
'name' => '',
'content' => "##Simple Title\n simple markdown format @@PLUGINFILE@@/moodle-hit-song.mp3",
'format' => FORMAT_MARKDOWN,
'expected' => 'Simple Title simple markdown format'
],
'plain text' => [
'name' => '',
'content' => "Simple plain text @@PLUGINFILE@@/moodle-hit-song.mp3",
'format' => FORMAT_PLAIN,
'expected' => 'Simple plain text'
],
'moodle format text' => [
'name' => '',
'content' => "Simple plain text @@PLUGINFILE@@/moodle-hit-song.mp3",
'format' => FORMAT_MOODLE,
'expected' => 'Simple plain text'
],
'html format text' => [
'name' => '',
'content' => "<h1>Simple plain title</h1><p> with plain text</p> @@PLUGINFILE@@/moodle-hit-song.mp3",
'format' => FORMAT_HTML,
'expected' => 'Simple plain title with plain text'