From a8995b34df272187ee9d55a5c027a58437ad661a Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Mon, 30 Apr 2007 04:49:25 +0000 Subject: [PATCH] MDL-9506 Added grade_grades_raw as a proper object used by grade_item. Also added a global function in gradelib for updating grade_grades_raw objects. --- lib/grade/grade_grades_raw.php | 35 +++++++++++++++++ lib/grade/grade_item.php | 68 +++++++++++++++++++++++++++++---- lib/gradelib.php | 29 ++++++++++++++ lib/simpletest/testgradelib.php | 11 ++++-- 4 files changed, 133 insertions(+), 10 deletions(-) diff --git a/lib/grade/grade_grades_raw.php b/lib/grade/grade_grades_raw.php index dc054f02d1..d98b096a27 100644 --- a/lib/grade/grade_grades_raw.php +++ b/lib/grade/grade_grades_raw.php @@ -124,6 +124,41 @@ class grade_grades_raw extends grade_object { return false; } } + + /** + * In addition to the normal updating set up in grade_object, this object also records + * its pre-update value and its new value in the grade_history table. + * + * @param float $newgrade The new gradevalue of this object + * @param string $howmodified What caused the modification? manual/module/import/cron... + * @param string $note A note attached to this modification. + * @return boolean Success or Failure. + */ + function update($newgrade, $howmodified='manual', $note=NULL) { + global $USER; + $oldgrade = $this->gradevalue; + $this->gradevalue = $newgrade; + + $result = parent::update(); + + if ($result) { + $logentry = new stdClass(); + $logentry->itemid = $this->itemid; + $logentry->userid = $this->userid; + $logentry->oldgrade = $oldgrade; + $logentry->newgrade = $this->gradevalue; + $logentry->note = $note; + $logentry->howmodified = $howmodified; + $logentry->timemodified = mktime(); + $logentry->usermodified = $USER->id; + + insert_record('grade_history', $logentry); + return true; + } else { + return false; + } + + } } ?> diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 947f648181..029ca1d8f4 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -40,7 +40,7 @@ class grade_item extends grade_object { * Array of class variables that are not part of the DB table fields * @var array $nonfields */ - var $nonfields = array('table', 'nonfields', 'calculation'); + var $nonfields = array('table', 'nonfields', 'calculation', 'grade_grades_raw'); /** * The course this grade_item belongs to. @@ -167,6 +167,12 @@ class grade_item extends grade_object { * @var string $calculation */ var $calculation; + + /** + * Array of grade_grades_raw objects linked to this grade_item. They are indexed by userid. + * @var array $grade_grades_raw + */ + var $grade_grades_raw = array(); /** * Finds and returns a grade_item object based on 1-3 field values. @@ -203,13 +209,30 @@ class grade_item extends grade_object { * @return mixed An array of all raw_grades (stdClass objects) for this grade_item, or a single raw_grade. */ function get_raw($userid=NULL) { - $grade_raw = null; + if (empty($this->grade_grades_raw)) { + $this->load_raw(); + } + + $grade_raw_array = null; if (!empty($userid)) { - $grade_raw = get_record('grade_grades_raw', 'itemid', $this->id, 'userid', $userid); + $r = get_record('grade_grades_raw', 'itemid', $this->id, 'userid', $userid); + $grade_raw_array[$r->userid] = new grade_grades_raw($r); } else { - $grade_raw = get_records('grade_grades_raw', 'itemid', $this->id); + $grade_raw_array = $this->grade_grades_raw; + } + return $grade_raw_array; + } + + /** + * Loads all the grade_grades_raw objects for this grade_item from the DB into grade_item::$grade_grades_raw array. + * @return array grade_grades_raw objects + */ + function load_raw() { + $grade_raw_array = get_records('grade_grades_raw', 'itemid', $this->id); + foreach ($grade_raw_array as $r) { + $this->grade_grades_raw[$r->userid] = new grade_grades_raw($r); } - return $grade_raw; + return $this->grade_grades_raw; } /** @@ -295,8 +318,7 @@ class grade_item extends grade_object { */ function get_category() { if (!empty($this->categoryid)) { - $grade_category = new grade_category($this->category_id); - return $grade_category; + return grade_category::fetch('id', $this->categoryid); } else { return null; } @@ -318,5 +340,37 @@ class grade_item extends grade_object { return $final->locked; } } + + /** + * Performs the necessary calculations on the grades_raw referenced by this grade_item, + * and stores the results in grade_grades_final. Performs this for only one userid if + * requested. Also resets the needs_update flag once successfully performed. + * + * @param int $userid + * @param string $howmodified What caused the modification? manual/module/import/cron... + * @param string $note A note attached to this modification. + * @return boolean Success or failure + */ + function update_final_grade($userid=NULL, $howmodified='manual', $note=NULL) { + if (empty($this->grade_grades_raw)) { + $this->load_raw(); + } + + $grade_raw_array = array(); + + if (!empty($userid)) { + $grade_raw_array[$userid] = $this->grade_grades_raw[$userid]; + } else { + $grade_raw_array = $this->grade_grades_raw; + } + + // TODO implement parsing of formula and calculation MDL-9643 + foreach ($grade_raw_array as $r) { + $newgradevalue = 0; // TODO replace '0' with calculated value + $r->update($newgradevalue, $howmodified, $note); + } + + return true; + } } ?> diff --git a/lib/gradelib.php b/lib/gradelib.php index f5aa91bced..b47462b454 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -40,6 +40,7 @@ define('GRADE_AGGREGATE_MODE', 3); require_once($CFG->libdir . '/grade/grade_category.php'); require_once($CFG->libdir . '/grade/grade_item.php'); require_once($CFG->libdir . '/grade/grade_calculation.php'); +require_once($CFG->libdir . '/grade/grade_grades_raw.php'); /** * Extracts from the gradebook all the grade items attached to the calling object. @@ -118,4 +119,32 @@ function grade_is_locked($itemtype, $itemmodule, $iteminstance, $itemnumber=NULL return $grade_item->is_locked($userid); } +/** + * Updates all grade_grades_final for each grade_item matching the given attributes. + * The search is further restricted, so that only grade_items that have needs_update == TRUE + * or that use calculation are retrieved. + * + * @param int $courseid + * @param int $gradeitemid + * @return int Number of grade_items updated + */ +function grade_update_final_grades($courseid=NULL, $gradeitemid=NULL) { + $grade_item = new grade_item(); + $grade_item->courseid = $courseid; + $grade_item->id = $gradeitemid; + $grade_items = $grade_item->fetch_all_using_this(); + + $count = 0; + + foreach ($grade_items as $gi) { + $calculation = $gi->get_calculation(); + if (!empty($calculation) || $gi->needsupdate) { + if ($gi->update_final_grade()) { + $count++; + } + } + } + + return $count; +} ?> diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 9cdc55c2a0..3821d04f96 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -634,8 +634,9 @@ class gradelib_test extends UnitTestCase { $grade_item = new grade_item($this->grade_items[0]); $this->assertTrue(method_exists($grade_item, 'get_raw')); - $raw_grade = $grade_item->get_raw($this->userid); - $this->assertEqual(1, count($raw_grade)); + $raw_grades = $grade_item->get_raw($this->userid); + $raw_grade = current($raw_grades); + $this->assertEqual(1, count($raw_grades)); $this->assertEqual($this->grade_grades_raw[0]->gradevalue, $raw_grade->gradevalue); } @@ -686,7 +687,11 @@ class gradelib_test extends UnitTestCase { } function test_grade_item_get_category() { - + $grade_item = new grade_item($this->grade_items[0]); + $this->assertTrue(method_exists($grade_item, 'get_category')); + + $category = $grade_item->get_category(); + $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname); } // GRADE_CATEGORY OBJECT -- 2.39.5