Skip to content

Commit

Permalink
MDL-47681 core_grades: Ignore weights of grade items 'none' or 'text'
Browse files Browse the repository at this point in the history
It can happen that some grade items with the type 'None' have a max
grade different than 0, in which case we would have set a weight for
them which is wrong as they must not have any. This patch always
ignores the grade items 'None' and 'Text' when calculating the
weights and locks the weight UI fields for those items too.
  • Loading branch information
Frederic Massart committed Oct 16, 2014
1 parent 799190c commit f8c8f98
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion grade/edit/tree/item_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,15 @@ function definition() {

$mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
$mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
$mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_NONE);
$mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_TEXT);

$mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
$mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
$mform->setType('aggregationcoef2', PARAM_RAW);
$mform->disabledIf('aggregationcoef2', 'weightoverride');
$mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_NONE);
$mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_TEXT);

$options = array();
$coefstring = '';
Expand Down Expand Up @@ -300,7 +304,8 @@ function definition_after_data() {
}
$mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
}

$mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_NONE);
$mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_TEXT);
$mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parent_category->id);
}

Expand Down
3 changes: 2 additions & 1 deletion grade/edit/tree/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,8 @@ public function get_item_cell($item, $params) {
$itemcell = parent::get_item_cell($item, $params);
$itemcell->text = ' ';

if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))
&& !in_array($params['element']['object']->gradetype, array(GRADE_TYPE_NONE, GRADE_TYPE_TEXT))) {
$itemcell->text = grade_edit_tree::get_weight_input($item);
}

Expand Down
11 changes: 11 additions & 0 deletions lib/grade/grade_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,11 @@ private function auto_update_weights() {
$gradeitem = $child['object']->load_grade_item();
}

if ($gradeitem->gradetype == GRADE_TYPE_NONE || $gradeitem->gradetype == GRADE_TYPE_TEXT) {
// Text items and none items do not have a weight.
continue;
}

// Record the ID and the weight for this grade item.
$overridearray[$gradeitem->id] = array();
$overridearray[$gradeitem->id]['extracredit'] = intval($gradeitem->aggregationcoef);
Expand Down Expand Up @@ -1363,6 +1368,12 @@ private function auto_update_weights() {
$gradeitem = $child['object']->load_grade_item();
}

if ($gradeitem->gradetype == GRADE_TYPE_NONE || $gradeitem->gradetype == GRADE_TYPE_TEXT) {
// Text items and none items do not have a weight, no need to set their weight to
// zero as they must never be used during aggregation.
continue;
}

if (!$gradeitem->weightoverride) {
// Calculations with a grade maximum of zero will cause problems. Just set the weight to zero.
if ($totaloverriddenweight >= 1 || $totalnonoverriddengrademax == 0 || $gradeitem->grademax == 0) {
Expand Down

0 comments on commit f8c8f98

Please sign in to comment.