MDL-52873 forms: Generate unique id attributes for modgrade elements

The previous method of generating the id attribute of the elements
within the modgrade group did not take the name of the modgrade field
into account. So if there were multiple fields of the modgrade type
added into a form (not a common case yet still valid), elements created
within the group were assigned same id attributes.

The patch introduces a new method for generating the id attribute of
modgrade elements. The new method takes the name of the modgrade group
into account and returns the id in the format similar to the default one
returned by HTML_QuickForm_element::_generateId().

The patch changes the generated id attribute. Apart from the
block_activity_results' behat feature files, not other places seem to
rely on the exact value.
This commit is contained in:
David Mudrák 2016-01-22 13:43:42 +01:00
parent eddec36d49
commit d84c64b7d6
7 changed files with 67 additions and 14 deletions

View file

@ -91,15 +91,15 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group{
$langscale = get_string('modgradetypescale', 'grades');
$scaleselect = @MoodleQuickForm::createElement('select', 'modgrade_scale', $langscale, $scales, $attributes);
$scaleselect->setHiddenLabel = false;
$scaleselect->_generateId();
$scaleselectid = $scaleselect->getAttribute('id');
$scaleselectid = $this->generate_modgrade_subelement_id('modgrade_scale');
$scaleselect->updateAttributes(array('id' => $scaleselectid));
// Maximum grade textbox.
$langmaxgrade = get_string('modgrademaxgrade', 'grades');
$maxgrade = @MoodleQuickForm::createElement('text', 'modgrade_point', $langmaxgrade, array());
$maxgrade->setHiddenLabel = false;
$maxgrade->_generateId();
$maxgradeid = $maxgrade->getAttribute('id');
$maxgradeid = $this->generate_modgrade_subelement_id('modgrade_point');
$maxgrade->updateAttributes(array('id' => $maxgradeid));
// Grade type select box.
$gradetype = array(
@ -110,7 +110,8 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group{
$langtype = get_string('modgradetype', 'grades');
$typeselect = @MoodleQuickForm::createElement('select', 'modgrade_type', $langtype, $gradetype, $attributes, true);
$typeselect->setHiddenLabel = false;
$typeselect->_generateId();
$typeselectid = $this->generate_modgrade_subelement_id('modgrade_type');
$typeselect->updateAttributes(array('id' => $typeselectid));
// Add elements.
@ -315,4 +316,17 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group{
return parent::onQuickFormEvent($event, $arg, $caller);
}
/**
* Generates the id attribute for the subelement of the modgrade group.
*
* Uses algorithm similar to what {@link HTML_QuickForm_element::_generateId()}
* does but takes the name of the wrapping modgrade group into account.
*
* @param string $subname the name of the HTML_QuickForm_element in this modgrade group
* @return string
*/
protected function generate_modgrade_subelement_id($subname) {
$gid = str_replace(array('[', ']'), array('_', ''), $this->getName());
return clean_param('id_'.$gid.'_'.$subname, PARAM_ALPHANUMEXT);
}
}