MDL-78474 mod_label: include new field for name

This commit is contained in:
stemiwe 2023-06-14 15:03:48 +02:00 committed by Alex Yeung
parent 00f0613f99
commit d73d56f070
4 changed files with 36 additions and 1 deletions

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

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2023042400; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2023042401; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2023041800; // Requires this Moodle version.
$plugin->component = 'mod_label'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;