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;
+ }
+
+ }
}
?>
* 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.
* @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.
* @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;
}
/**
*/
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;
}
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;
+ }
}
?>
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.
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;
+}
?>
$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);
}
}
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