Skip to content

Commit

Permalink
MDL-47064 Grades: Make the contribution to total column work
Browse files Browse the repository at this point in the history
Part of: MDL-46576
  • Loading branch information
Damyon Wiese authored and abgreeve committed Oct 3, 2014
1 parent 53771c4 commit 446467f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 317 deletions.
108 changes: 61 additions & 47 deletions grade/report/user/lib.php
Expand Up @@ -350,7 +350,7 @@ function fill_table() {
return true;
}

private function fill_table_recursive(&$element) {
private function fill_table_recursive(&$element, &$aggregationhints = array()) {
global $DB, $CFG;

$type = $element['type'];
Expand Down Expand Up @@ -620,52 +620,16 @@ private function fill_table_recursive(&$element) {
$data['contributiontocoursetotal']['class'] = $class;
$data['contributiontocoursetotal']['content'] = '-';
$data['contributiontocoursetotal']['headers'] = "$header_cat $header_row contributiontocoursetotal";
/**
if (($type != 'categoryitem') && ($type != 'courseitem')) {
$weight = $grade_grade->get_aggregation_weight($grade_object);
if (is_numeric($weight)) {
$me = $grade_grade->grade_item;
$percentoftotal = $hint;
$validpercent = true;
$parent = null;
while ((!$me->is_course_item()) && ($validpercent)) {
// The parent of a category grade item is itself (yes - how odd).
// This means we need to use the parent of the grade_category if it exists.
if (!empty($parent)) {
$parent = $parent->get_parent_category();
} else {
$parent = $me->get_parent_category();
}
$parentgradeitem = $parent->load_grade_item();
$parentgradegrade = grade_grade::fetch(array('itemid'=>$parentgradeitem->id, 'userid'=>$this->user->id));
if (!$parentgradegrade) {
$validpercent = false;
continue;
}
$hint = $parentgradegrade->get_aggregation_hint($parentgradeitem);
$me = $parentgradeitem;
if (!is_numeric($hint)) {
// It's OK for the course grade item to not have a usedinaggregation value.
$validpercent = $parentgradeitem->is_course_item();
continue;
}
$thispercent = $hint;
$percentoftotal *= $thispercent;
}
if ($validpercent) {
$grademin = $grade_grade->grade_item->grademin;
$grademax = $grade_grade->grade_item->grademax;
$finalgrade = $grade_grade->finalgrade;
$contribution = format_float(((($finalgrade-$grademin)/($grademax-$grademin)*($percentoftotal * 100.0))),2);
$data['contributiontocoursetotal']['content'] = $contribution;
}
}
} **/
$hint = $grade_grade->get_aggregation_hint($grade_object);
if ($hint && is_numeric($hint)) {
$data['contributiontocoursetotal']['content'] = $hint;
$data['contributiontocoursetotal']['content'] .= ' ' . $grade_grade->finalgrade . ' ' . $grade_grade->rawgrademin . ' ' . $grade_grade->rawgrademax;

$hint['grademax'] = $grade_grade->grade_item->grademax;
$hint['grademin'] = $grade_grade->grade_item->grademin;
$hint['grade'] = $gradeval;
$parent = $grade_object->load_parent_category();
if ($grade_object->is_category_item()) {
$parent = $parent->load_parent_category();
}
$hint['parent'] = $parent->load_grade_item()->id;
$aggregationhints[$grade_grade->itemid] = $hint;
}
}
}
Expand All @@ -692,7 +656,57 @@ private function fill_table_recursive(&$element) {
/// Recursively iterate through all child elements
if (isset($element['children'])) {
foreach ($element['children'] as $key=>$child) {
$this->fill_table_recursive($element['children'][$key]);
$this->fill_table_recursive($element['children'][$key], $aggregationhints);
}
}

if ($this->showcontributiontocoursetotal && ($type == 'category' && $depth == 1)) {
// We should have collected all the hints by now - walk the tree again and build the contributions column.

$this->fill_contributions_column($element, $aggregationhints);
}
}

public function fill_contributions_column($element, $aggregationhints) {

/// Recursively iterate through all child elements
if (isset($element['children'])) {
foreach ($element['children'] as $key=>$child) {
$this->fill_contributions_column($element['children'][$key], $aggregationhints);
}
} else if ($element['type'] == 'item') {
$grade_object = $element['object'];
$itemid = $grade_object->id;
if (isset($aggregationhints[$itemid])) {

$graderange = $aggregationhints[$itemid]['grademax'] - $aggregationhints[$itemid]['grademin'];
$gradeval = ($aggregationhints[$itemid]['grade'] - $aggregationhints[$itemid]['grademin']) / $graderange;

$parent = $aggregationhints[$itemid]['parent'];
do {
if (!is_null($aggregationhints[$itemid]['weight'])) {
$gradeval *= $aggregationhints[$itemid]['weight'];
}

if (isset($aggregationhints[$itemid]['parent']) &&
$aggregationhints[$itemid]['parent'] != $itemid) {
$parent = $aggregationhints[$itemid]['parent'];
$itemid = $parent;
} else {
$parent = false;
}
} while ($parent);
$gradeval *= $aggregationhints[$itemid]['grademax'];

$header_row = "row_{$grade_object->id}_{$this->user->id}";
foreach ($this->tabledata as $key => $row) {
if (isset($row['itemname']) &&
($row['itemname']['id'] == $header_row)) {
$decimals = $grade_object->get_decimals();
$this->tabledata[$key]['contributiontocoursetotal']['content'] = format_float($gradeval, $decimals, true);
break;
}
}
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion grade/tests/behat/grade_aggregation.feature
Expand Up @@ -265,14 +265,40 @@ Feature: We can use calculated grade totals
And I turn editing mode off
Then I should see "152.68 (24.43 %)" in the ".course" "css_element"
And I navigate to "Course grade settings" node in "Grade administration > Settings"
And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items"
And I set the field "report_overview_showtotalsifcontainhidden" to "Show totals excluding hidden items"
And I set the field "report_user_showtotalsifcontainhidden" to "Show totals excluding hidden items"
And I set the field "Show contribution to course total" to "Show"
And I set the field "Show weightings" to "Show"
And I press "Save changes"
And I set the field "Grade report" to "User report"
And the following should exist in the "user-grade" table:
| Grade item | Calculated weight | Grade | Range |
| Test assignment five | 28.57 % | 10.00 (50.00 %) | 020 |
| Test assignment six | 50.00 % | 5.00 (50.00 %) | 010 |
| Test assignment seven | 21.43 % | - | 015 |
| Test assignment eight | 66.67 % | 10.00 (50.00 %) | 020 |
| Test assignment nine | 33.33 % | 5.00 (50.00 %) | 010 |
| Test assignment ten | -( Empty ) | - | 015 |
| Test assignment one | 48.00 % | 60.00 (20.00 %) | 0300 |
| Test assignment two | 16.00 % | 20.00 (20.00 %) | 0100 |
| Test assignment three | 24.00 %( Extra credit ) | 40.00 (26.67 %) | 0150 |
| Test assignment four | 24.00 % | - | 0150 |
And I log out
And I log in as "student1"
And I follow "Course 1"
And I follow "Grades"
And I set the field "Grade report" to "Overview report"
And I should see "113.75 (23.45 %)" in the "overview-grade" "table"
And I set the field "Grade report" to "User report"
And the following should exist in the "user-grade" table:
| Grade item | Calculated weight | Grade | Range |
| Test assignment six | 70.00 % | 5.00 (50.00 %) | 010 |
| Test assignment seven | 30.00 % | - | 015 |
| Test assignment nine | 100.00 % | 5.00 (50.00 %) | 010 |
| Test assignment ten | -( Empty ) | - | 015 |
| Test assignment one | 61.86 % | 60.00 (20.00 %) | 0300 |
| Test assignment three | 30.93 %( Extra credit ) | 40.00 (26.67 %) | 0150 |
| Test assignment four | 30.93 % | - | 0150 |

@javascript
Scenario: Natural aggregation with drop lowest
Expand Down

0 comments on commit 446467f

Please sign in to comment.