Skip to content

Commit

Permalink
Merge branch 'MDL-52873-master-modgradeid' of git://github.com/mudrd8…
Browse files Browse the repository at this point in the history
…mz/moodle
  • Loading branch information
David Monllao committed Feb 2, 2016
2 parents 51f3220 + d84c64b commit 367cf39
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 14 deletions.
Expand Up @@ -35,6 +35,6 @@ Feature: The activity results block displays student scores
When I follow "Test assignment"
And I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| id_modgrade_type | None |
| id_grade_modgrade_type | None |
And I press "Save and return to course"
Then I should see "Error: the activity selected uses a grading method that is not supported by this block." in the "Activity results" "block"
Expand Up @@ -39,8 +39,8 @@ Feature: The activity results block displays student scores as scales
| Assignment name | Test assignment |
| Description | Offline text |
| assignsubmission_file_enabled | 0 |
| id_modgrade_type | Scale |
| id_modgrade_scale | My Scale |
| id_grade_modgrade_type | Scale |
| id_grade_modgrade_scale | My Scale |
And I follow "Course 1"
And I navigate to "Grades" node in "Course administration"
And I turn editing mode on
Expand Down
Expand Up @@ -56,8 +56,8 @@ Feature: The activity results block displays student scores as scales
| Assignment name | Test assignment |
| Description | Offline text |
| assignsubmission_file_enabled | 0 |
| id_modgrade_type | Scale |
| id_modgrade_scale | My Scale |
| id_grade_modgrade_type | Scale |
| id_grade_modgrade_scale | My Scale |
| Group mode | Separate groups |
And I follow "Course 1"
And I navigate to "Grades" node in "Course administration"
Expand Down
Expand Up @@ -39,8 +39,8 @@ Feature: The activity results block displays student scores as scales
| Assignment name | Test assignment |
| Description | Offline text |
| assignsubmission_file_enabled | 0 |
| id_modgrade_type | Scale |
| id_modgrade_scale | My Scale |
| id_grade_modgrade_type | Scale |
| id_grade_modgrade_scale | My Scale |
And I follow "Course 1"
And I navigate to "Grades" node in "Course administration"
And I turn editing mode on
Expand Down
Expand Up @@ -56,8 +56,8 @@ Feature: The activity results block displays student scores as scales
| Assignment name | Test assignment |
| Description | Offline text |
| assignsubmission_file_enabled | 0 |
| id_modgrade_type | Scale |
| id_modgrade_scale | My Scale |
| id_grade_modgrade_type | Scale |
| id_grade_modgrade_scale | My Scale |
| Group mode | Separate groups |
And I follow "Course 1"
And I navigate to "Grades" node in "Course administration"
Expand Down
24 changes: 19 additions & 5 deletions lib/form/modgrade.php
Expand Up @@ -91,15 +91,15 @@ public function _createElements() {
$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(
Expand All @@ -110,7 +110,8 @@ public function _createElements() {
$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.

Expand Down Expand Up @@ -315,4 +316,17 @@ public function onQuickFormEvent($event, $arg, &$caller) {
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);
}
}
39 changes: 39 additions & 0 deletions lib/tests/formslib_test.php
Expand Up @@ -550,6 +550,33 @@ public function test_type_cleaning() {
$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);
}
}


Expand Down Expand Up @@ -822,3 +849,15 @@ public function definition() {
'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');
}
}

0 comments on commit 367cf39

Please sign in to comment.