Skip to content
Browse files

MDL-37973 gradebook Return a maxgrade of '-' if grade type is TEXT

Including tests for correct return from
grade_edit_tree_column_range->get_item_cell()
  • Loading branch information...
1 parent 444433b commit c41e70525be1310c8ec91c50d976732ad08ae26e @merrill-oakland merrill-oakland committed Sep 11, 2013
Showing with 78 additions and 10 deletions.
  1. +5 −2 grade/edit/tree/lib.php
  2. +73 −8 grade/tests/edittree_test.php
View
7 grade/edit/tree/lib.php
@@ -793,9 +793,12 @@ public function get_category_cell($category, $levelclass, $params) {
public function get_item_cell($item, $params) {
global $DB, $OUTPUT;
- // If the parent aggregation is Sum of Grades, this cannot be changed
+ // If the parent aggregation is Sum of Grades, we should show the number, even for scales, as that value is used...
+ // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
$parent_cat = $item->get_parent_category();
- if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
+ if ($item->gradetype == GRADE_TYPE_TEXT) {
+ $grademax = ' - ';
+ } else if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
$grademax = format_float($item->grademax, $item->get_decimals());
} elseif ($item->gradetype == GRADE_TYPE_SCALE) {
$scale = $DB->get_record('scale', array('id' => $item->scaleid));
View
81 grade/tests/edittree_test.php
@@ -32,21 +32,86 @@
/**
* Tests grade_edit_tree (deals with the data on the categories and items page in the gradebook)
*/
-class gradeedittreelib_testcase extends basic_testcase {
- var $courseid = 1;
- var $context = null;
- var $grade_edit_tree = null;
-
+class gradeedittreelib_testcase extends advanced_testcase {
public function test_format_number() {
- $numinput = array( 0, 1, 1.01, '1.010', 1.2345);
+ $numinput = array(0, 1, 1.01, '1.010', 1.2345);
$numoutput = array(0.0, 1.0, 1.01, 1.01, 1.2345);
- for ($i=0; $i<sizeof($numinput); $i++) {
+ for ($i = 0; $i < count($numinput); $i++) {
$msg = 'format_number() testing '.$numinput[$i].' %s';
- $this->assertEquals(grade_edit_tree::format_number($numinput[$i]),$numoutput[$i],$msg);
+ $this->assertEquals(grade_edit_tree::format_number($numinput[$i]), $numoutput[$i], $msg);
}
}
+ public function test_grade_edit_tree_column_range_get_item_cell() {
+ global $DB, $CFG;
+
+ $this->resetAfterTest(true);
+
+ // Make some things we need.
+ $scale = $this->getDataGenerator()->create_scale();
+ $course = $this->getDataGenerator()->create_course();
+ $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
+ $modulecontext = context_module::instance($assign->id);
+ // The generator returns a dummy object, lets get the real assign object.
+ $assign = new assign($modulecontext, false, false);
+ $cm = $assign->get_course_module();
+
+ // Get range column.
+ $column = grade_edit_tree_column::factory('range');
+
+ $gradeitemparams = array(
+ 'itemtype' => 'mod',
+ 'itemmodule' => $cm->modname,
+ 'iteminstance' => $cm->instance,
+ 'courseid' => $cm->course,
+ 'itemnumber' => 0
+ );
+
+ // Lets set the grade to something we know.
+ $instance = $assign->get_instance();
+ $instance->grade = 70;
+ $instance->instance = $instance->id;
+ $assign->update_instance($instance);
+
+ $gradeitem = grade_item::fetch($gradeitemparams);
+ $cell = $column->get_item_cell($gradeitem, array());
+
+ $this->assertEquals(GRADE_TYPE_VALUE, $gradeitem->gradetype);
+ $this->assertEquals(null, $gradeitem->scaleid);
+ $this->assertEquals(70.0, (float) $cell->text, "Grade text is 70", 0.01);
+
+ // Now change it to a scale.
+ $instance = $assign->get_instance();
+ $instance->grade = -($scale->id);
+ $instance->instance = $instance->id;
+ $assign->update_instance($instance);
+
+ $gradeitem = grade_item::fetch($gradeitemparams);
+ $cell = $column->get_item_cell($gradeitem, array());
+
+ // Make the expected scale text.
+ $scaleitems = null;
+ $scaleitems = explode(',', $scale->scale);
+ $scalestring = end($scaleitems) . ' (' . count($scaleitems) . ')';
+
+ $this->assertEquals(GRADE_TYPE_SCALE, $gradeitem->gradetype);
+ $this->assertEquals($scale->id, $gradeitem->scaleid);
+ $this->assertEquals($scalestring, $cell->text, "Grade text matches scale");
+
+ // Now change it to no grade.
+ $instance = $assign->get_instance();
+ $instance->grade = 0;
+ $instance->instance = $instance->id;
+ $assign->update_instance($instance);
+
+ $gradeitem = grade_item::fetch($gradeitemparams);
+ $cell = $column->get_item_cell($gradeitem, array());
+
+ $this->assertEquals(GRADE_TYPE_TEXT, $gradeitem->gradetype);
+ $this->assertEquals(null, $gradeitem->scaleid);
+ $this->assertEquals(' - ', $cell->text, 'Grade text matches empty value of " - "');
+ }
}

0 comments on commit c41e705

Please sign in to comment.
Something went wrong with that request. Please try again.