From 5e2274737c0d7ad3ae83631c85564b0503e04a5e Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Thu, 24 May 2007 03:32:20 +0000 Subject: [PATCH] MDL-9506 Added grademax extrapolation from scale info for grade_item, in insert() and update() methods. Corrected a few other minor bugs. --- lib/grade/grade_calculation.php | 2 +- lib/grade/grade_grades_final.php | 14 ++++++++++++++ lib/grade/grade_item.php | 19 +++++++++++++++++++ lib/grade/grade_tree.php | 28 +++++++++++++++++++++++----- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/lib/grade/grade_calculation.php b/lib/grade/grade_calculation.php index 1508dc89a2..e3b75fb1b4 100644 --- a/lib/grade/grade_calculation.php +++ b/lib/grade/grade_calculation.php @@ -7,7 +7,7 @@ // Moodle - Modular Object-Oriented Dynamic Learning Environment // // http://moodle.com // // // -// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com // +// Copyright (C) 2001-2007 Martin Dougiamas http://dougiamas.com // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // diff --git a/lib/grade/grade_grades_final.php b/lib/grade/grade_grades_final.php index 18221ce1e4..713113ffd0 100644 --- a/lib/grade/grade_grades_final.php +++ b/lib/grade/grade_grades_final.php @@ -150,6 +150,20 @@ class grade_grades_final extends grade_object { return false; } } + + /** + * Extends grade_object->insert() to prevent a final grade from being inserted if it already exists + * (If a final grade already has the same userid/itemid key combination). It will update it instead. + * + */ + function insert() { + $exists = count_records('grade_grades_final', 'userid', $this->userid, 'itemid', $this->itemid); + if ($exists) { + return $this->update(); + } else { + return parent::insert(); + } + } } ?> diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 51a9dcf3f0..06e4aedd34 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -352,6 +352,15 @@ class grade_item extends grade_object { $this->outcomeid = $this->outcome->id; } + // Retrieve scale and infer grademax from it + if (!empty($this->scaleid)) { + $this->load_scale(); + $this->scale->load_items(); + $this->grademax = count ($this->scale->scale_items); + $this->grademin = 0; + $this->gradetype = GRADE_TYPE_SCALE; + } + if (!empty($this->scale->id)) { $this->scaleid = $this->scale->id; } @@ -450,6 +459,15 @@ class grade_item extends grade_object { * @return int ID of the new grade_item record. */ function insert() { + // Retrieve scale and infer grademax from it + if (!empty($this->scaleid)) { + $this->load_scale(); + $this->scale->load_items(); + $this->grademax = count ($this->scale->scale_items); + $this->grademin = 0; + $this->gradetype = GRADE_TYPE_SCALE; + } + $result = parent::insert(); // Notify parent category of need to update. Note that a grade_item may not have a categoryid. @@ -554,6 +572,7 @@ class grade_item extends grade_object { $final_grade->gradevalue = $this->adjust_grade($raw_grade); $final_grade->itemid = $this->id; $final_grade->userid = $raw_grade->userid; + if ($final_grade->gradevalue > $this->grademax) { debugging("FINAL GRADE EXCEEDED grademax FIRST"); return false; diff --git a/lib/grade/grade_tree.php b/lib/grade/grade_tree.php index 8f642cd244..5a26316945 100644 --- a/lib/grade/grade_tree.php +++ b/lib/grade/grade_tree.php @@ -23,6 +23,11 @@ // // /////////////////////////////////////////////////////////////////////////// +require_once $CFG->libdir . '/grade/grade_category.php'; +require_once $CFG->libdir . '/grade/grade_item.php'; +require_once $CFG->libdir . '/grade/grade_grades_final.php'; +require_once $CFG->libdir . '/grade/grade_grades_raw.php'; + /** * This class represents a complete tree of categories, grade_items and final grades, * organises as an array primarily, but which can also be converted to other formats. @@ -91,7 +96,9 @@ class grade_tree { $this->tree_array = $this->get_tree($include_grades); } - $this->first_sortorder = key($this->tree_array); + if (!empty($this->tree_array)) { + $this->first_sortorder = key($this->tree_array); + } } /** @@ -508,15 +515,23 @@ class grade_tree { // Get ordered list of grade_items (not category type) $query = "SELECT * FROM $items_table WHERE itemtype <> 'category' $itemconstraint ORDER BY sortorder"; $grade_items = get_records_sql($query); - + + if (empty($grade_items)) { + return null; + } + // For every grade_item that doesn't have a parent category, create category fillers foreach ($grade_items as $itemid => $item) { if (empty($item->categoryid)) { $item = new grade_item($item); - $fillers[$item->sortorder] = $item; + if (empty($item->sortorder)) { + $fillers[] = $item; + } else { + $fillers[$item->sortorder] = $item; + } } } - + // Get all top categories $query = "SELECT $category_table.*, sortorder FROM $category_table, $items_table WHERE iteminstance = $category_table.id $catconstraint ORDER BY sortorder"; @@ -524,7 +539,10 @@ class grade_tree { $topcats = get_records_sql($query); if (empty($topcats)) { - return null; + $topcats = $grade_items; + $topcats[0] = new stdClass(); + $topcats[0]->sortorder = 0; + $topcats[0]->courseid = $this->courseid; } // If any of these categories has grade_items as children, create a topcategory filler with colspan=count(children) -- 2.39.5