Skip to content

Commit

Permalink
MDL-9506 Removed the debugging message for the fetch() methods. Added…
Browse files Browse the repository at this point in the history
… droplow and keephigh rules to grade_category::aggregate_grades(). The only question now is what to do when the droplow or keephigh field is larger than the number of grades for a given user/item combination. At the moment it just gives that value 0.
  • Loading branch information
nicolasconnault committed May 21, 2007
1 parent d0e84e1 commit ab53054
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 63 deletions.
1 change: 0 additions & 1 deletion lib/grade/grade_calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $grade_calculation;
}
} else {
debugging("No grade_calculation matching your criteria in the database.");
return false;
}
}
Expand Down
136 changes: 87 additions & 49 deletions lib/grade/grade_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $grade_category;
}
} else {
debugging("No grade_category matching your criteria in the database.");
return false;
}
}
Expand Down Expand Up @@ -410,49 +409,66 @@ function aggregate_grades($final_grade_sets) {
foreach ($set as $userid => $final_grade) {
$this->load_grade_item();
$value = standardise_score((float) $final_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);
$pooled_grades[$userid][] = $value;
$pooled_grades[$userid][] = (string) $value;
}
}

foreach ($pooled_grades as $userid => $grades) {
$aggregated_value = null;

switch ($this->aggregation) {
case GRADE_AGGREGATE_MEAN : // Arithmetic average
$num = count($grades);
$sum = array_sum($grades);
$aggregated_value = $sum / $num;
break;
case GRADE_AGGREGATE_MEDIAN : // Middle point value in the set: ignores frequencies
sort($grades);
$num = count($grades);
$halfpoint = intval($num / 2);

if($num % 2 == 0) {
$aggregated_value = ($grades[ceil($halfpoint)] + $grades[floor($halfpoint)]) / 2;
} else {
$aggregated_value = $grades[$halfpoint];
}

break;
case GRADE_AGGREGATE_MODE : // Value that occurs most frequently. Not always useful (all values are likely to be different)
// TODO implement or reject
break;
case GRADE_AGGREGATE_SUM : // I don't see much point to this one either
$aggregated_value = array_sum($grades);
break;
default:
$num = count($grades);
$sum = array_sum($grades);
$aggregated_value = $sum / $num;
break;
// Sort grades from lowest to largest
sort($grades, SORT_NUMERIC);

// Apply droplow or keephigh rule
if (!empty($this->droplow)) {
$reversed_grades = array_reverse($grades);
for ($i = 0; $i < $this->droplow; $i++) {
array_pop($reversed_grades);
}
$grades = array_reverse($reversed_grades);
} elseif (!empty($this->keephigh)) {
for ($i = 0; $i < $this->keephigh; $i++) {
array_pop($grades);
}
}

// If the gradevalue is null, we have a problem
if (empty($aggregated_value)) {
debugging("There was an error during the aggregation procedure, an empty value resulted.");
return false;
}
if (count($grades) > 1) {

switch ($this->aggregation) {
case GRADE_AGGREGATE_MEAN : // Arithmetic average
$num = count($grades);
$sum = array_sum($grades);
$aggregated_value = $sum / $num;
break;
case GRADE_AGGREGATE_MEDIAN : // Middle point value in the set: ignores frequencies
sort($grades);
$num = count($grades);
$halfpoint = intval($num / 2);

if($num % 2 == 0) {
$aggregated_value = ($grades[ceil($halfpoint)] + $grades[floor($halfpoint)]) / 2;
} else {
$aggregated_value = $grades[$halfpoint];
}

break;
case GRADE_AGGREGATE_MODE : // Value that occurs most frequently. Not always useful (all values are likely to be different)
// TODO implement or reject
break;
case GRADE_AGGREGATE_SUM : // I don't see much point to this one either
$aggregated_value = array_sum($grades);
break;
default:
$num = count($grades);
$sum = array_sum($grades);
$aggregated_value = $sum / $num;
break;
}
} elseif (count($grades) == 1) {
$aggregated_value = $grades[0];
} else {
// TODO what happens if the droplow and keephigh rules have deleted all grades?
$aggregated_value = 0;
}

$grade_raw = new grade_grades_raw();

Expand Down Expand Up @@ -633,29 +649,38 @@ function get_childrentype() {
}

/**
* Retrieves from DB, instantiates and saves the associated grade_item object.
* If no grade_item exists yet, create one.
* Uses get_grade_item to load or create a grade_item, then saves it as $this->grade_item.
* @return object Grade_item
*/
function load_grade_item() {
$this->grade_item = $this->get_grade_item();
return $this->grade_item;
}

/**
* Retrieves from DB and instantiates the associated grade_item object.
* If no grade_item exists yet, create one.
* @return object Grade_item
*/
function get_grade_item() {
$grade_items = get_records_select('grade_items', "iteminstance = $this->id AND itemtype = 'category'", null, '*', 0, 1);

if ($grade_items){
$params = current($grade_items);
$this->grade_item = new grade_item($params);
$grade_item = new grade_item($params);
} else {
$this->grade_item = new grade_item();
$grade_item = new grade_item();
}

// If the associated grade_item isn't yet created, do it now. But first try loading it, in case it exists in DB.
if (empty($this->grade_item->id)) {
$this->grade_item->iteminstance = $this->id;
$this->grade_item->itemtype = 'category';
$this->grade_item->insert();
$this->grade_item->update_from_db();
if (empty($grade_item->id)) {
$grade_item->iteminstance = $this->id;
$grade_item->itemtype = 'category';
$grade_item->insert();
$grade_item->update_from_db();
}

return $this->grade_item;
return $grade_item;
}

/**
Expand All @@ -665,11 +690,24 @@ function load_grade_item() {
*/
function load_parent_category() {
if (empty($this->parent_category) && !empty($this->parent)) {
$this->parent_category = new grade_category(array('id' => $this->parent));
$this->parent_category = $this->get_parent_category();
}
return $this->parent_category;
}


/**
* Uses $this->parent to instantiate and return a grade_category object.
* @return object Parent_category
*/
function get_parent_category() {
if (!empty($this->parent)) {
$parent_category = new grade_category(array('id' => $this->parent));
return $parent_category;
} else {
return null;
}
}

/**
* Sets this category as the parent for the given children.
* A number of constraints are necessary:
Expand Down
1 change: 0 additions & 1 deletion lib/grade/grade_grades_final.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $object;
}
} else {
debugging("No grade_grades_final matching your criteria in the database.");
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/grade/grade_grades_raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $object;
}
} else {
debugging("No grade_grades_raw matching your criteria in the database.");
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/grade/grade_grades_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $grade_text;
}
} else {
debugging("No grade_grades_text matching your criteria in the database.");
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/grade/grade_history.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $grade_history;
}
} else {
debugging("No grade_history matching your criteria in the database.");
return false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/grade/grade_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $grade_item;
}
} else {
debugging("No grade_item matching these criteria in the database.");
return false;
}
}
Expand Down Expand Up @@ -513,7 +512,7 @@ function save_raw($raw_grades, $howmodified='module', $note=NULL) {
$this->grade_grades_raw[$userid] = $raw_grade;
}
} else {
debugging("The data given to grade_item::save_raw($data) was not valid, it must be an arra of raw grades.");
debugging("The data given to grade_item::save_raw($raw_grades) was not valid, it must be an array of raw grades.");
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/grade/grade_object.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function insert() {
*/
function update_from_db() {
if (empty($this->id)) {
debugging("The object could not be used in its state to retrieve a matching record from the DB, because it's id field is not set.");
debugging("The object could not be used in its state to retrieve a matching record from the DB, because its id field is not set.");
return false;
} else {
$class = get_class($this);
Expand Down
1 change: 0 additions & 1 deletion lib/grade/grade_outcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $grade_outcome;
}
} else {
debugging("No matching grade_outcome in DB with the given criteria.");
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/grade/grade_scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $grade_scale;
}
} else {
debugging("No matching grade_scale in DB with the given criteria.");
return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/simpletest/testgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ function load_grade_categories() {
$grade_category->courseid = $this->courseid;
$grade_category->aggregation = GRADE_AGGREGATE_MEAN;
$grade_category->keephigh = 100;
$grade_category->droplow = 10;
$grade_category->droplow = 0;
$grade_category->hidden = 0;
$grade_category->timecreated = mktime();
$grade_category->timemodified = mktime();
Expand All @@ -401,7 +401,7 @@ function load_grade_categories() {
$grade_category->courseid = $this->courseid;
$grade_category->aggregation = GRADE_AGGREGATE_MEAN;
$grade_category->keephigh = 100;
$grade_category->droplow = 10;
$grade_category->droplow = 0;
$grade_category->hidden = 0;
$grade_category->parent = $this->grade_categories[0]->id;
$grade_category->timecreated = mktime();
Expand All @@ -418,7 +418,7 @@ function load_grade_categories() {
$grade_category->courseid = $this->courseid;
$grade_category->aggregation = GRADE_AGGREGATE_MEAN;
$grade_category->keephigh = 100;
$grade_category->droplow = 10;
$grade_category->droplow = 0;
$grade_category->hidden = 0;
$grade_category->parent = $this->grade_categories[0]->id;
$grade_category->timecreated = mktime();
Expand All @@ -437,7 +437,7 @@ function load_grade_categories() {
$grade_category->courseid = $this->courseid;
$grade_category->aggregation = GRADE_AGGREGATE_MEAN;
$grade_category->keephigh = 100;
$grade_category->droplow = 10;
$grade_category->droplow = 0;
$grade_category->hidden = 0;
$grade_category->timecreated = mktime();
$grade_category->timemodified = mktime();
Expand Down

0 comments on commit ab53054

Please sign in to comment.