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

@ -550,6 +550,33 @@ class core_formslib_testcase extends advanced_testcase {
$data = $mform->get_data();
$this->assertSame($expectedvalues, (array) $data);
}
/**
* MDL-52873
*/
public function test_multiple_modgrade_fields() {
$this->resetAfterTest(true);
$form = new formslib_multiple_modgrade_form();
ob_start();
$form->display();
$html = ob_get_clean();
$this->assertTag(array('id' => 'fgroup_id_grade1'), $html);
$this->assertTag(array('id' => 'id_grade1_modgrade_type'), $html);
$this->assertTag(array('id' => 'id_grade1_modgrade_point'), $html);
$this->assertTag(array('id' => 'id_grade1_modgrade_scale'), $html);
$this->assertTag(array('id' => 'fgroup_id_grade2'), $html);
$this->assertTag(array('id' => 'id_grade2_modgrade_type'), $html);
$this->assertTag(array('id' => 'id_grade2_modgrade_point'), $html);
$this->assertTag(array('id' => 'id_grade2_modgrade_scale'), $html);
$this->assertTag(array('id' => 'fgroup_id_grade_3'), $html);
$this->assertTag(array('id' => 'id_grade_3_modgrade_type'), $html);
$this->assertTag(array('id' => 'id_grade_3_modgrade_point'), $html);
$this->assertTag(array('id' => 'id_grade_3_modgrade_scale'), $html);
}
}
@ -822,3 +849,15 @@ class formslib_clean_value extends moodleform {
'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT)), 'repeatablenamedgroup', 'add', 0);
}
}
/**
* Used to test that modgrade fields get unique id attributes.
*/
class formslib_multiple_modgrade_form extends moodleform {
public function definition() {
$mform = $this->_form;
$mform->addElement('modgrade', 'grade1', 'Grade 1');
$mform->addElement('modgrade', 'grade2', 'Grade 2');
$mform->addElement('modgrade', 'grade[3]', 'Grade 3');
}
}