$this->$param = $value;
}
}
-
$this->set_defaults();
- $this->set_calculation();
}
}
/**
* Returns this object's calculation.
+ * @param boolean $fetch Whether to fetch the value from the DB or not (false == just use the object's value)
* @return mixed $calculation A string if found, false otherwise.
*/
- function get_calculation()
+ function get_calculation($fetch = false)
{
- $grade_calculation = get_record('grade_calculations', 'itemid', $this->id);
- if ($grade_calculation) {
- return $grade_calculation->calculation;
- } else {
+ if (!$fetch) {
+ return $this->calculation;
+ }
+
+ $grade_calculation = grade_calculation::get_record(true, 'itemid', $this->id);
+
+ if (empty($grade_calculation)) { // There is no calculation in DB
+ return false;
+ } elseif ($grade_calculation->calculation != $this->calculation->calculation) { // The object's calculation is not in sync with the DB (new value??)
+ $this->calculation = $grade_calculation;
return $grade_calculation;
+ } else { // The object's calculation is already in sync with the database
+ return $this->calculation;
}
}
*/
function set_calculation($calculation = null)
{
- if (empty($calculation)) {
- $this->calculation = new grade_calculation();
- } else {
-
+ if (empty($calculation)) { // We are setting this item object's calculation variable from the DB
+ $grade_calculation = $this->get_calculation(true);
+ if (empty($grade_calculation)) {
+ return false;
+ } else {
+ $this->calculation = $grade_calculation;
+ }
+ } else { // We are updating or creating the calculation entry in the DB
+ $grade_calculation = $this->get_calculation();
+
+ if (empty($grade_calculation)) { // Creating
+ $grade_calculation = new grade_calculation();
+ $grade_calculation->calculation = $calculation;
+ $grade_calculation->itemid = $this->id;
+
+ if ($grade_calculation->insert()) {
+ $this->calculation = $grade_calculation;
+ return true;
+ } else {
+ return false;
+ }
+ } else { // Updating
+ $grade_calculation->calculation = $calculation;
+ $this->calculation = $grade_calculation;
+ return $grade_calculation->update();
+ }
}
}
}
+
+ /**
+ * Finds and returns a grade_calculation object based on 1-3 field values.
+ *
+ * @param boolean $static Unless set to true, this method will also set $this object with the returned values.
+ * @param string $field1
+ * @param string $value1
+ * @param string $field2
+ * @param string $value2
+ * @param string $field3
+ * @param string $value3
+ * @param string $fields
+ * @return object grade_calculation object or false if none found.
+ */
+ function get_record($static=false, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*")
+ {
+ // In Moodle 2.0 (PHP5) we can replace table names with the static class var grade_calculation::$table
+ if ($grade_calculation = get_record('grade_calculations', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
+ if ($static) {
+ $grade_calculation = new grade_calculation($grade_calculation);
+ return $grade_calculation;
+ } else {
+ foreach ($grade_calculation as $param => $value) {
+ $this->$param = $value;
+ }
+ return $this;
+ }
+ } else {
+ return false;
+ }
+ }
}
require_once($CFG->libdir . '/dmllib.php');
class gradelib_test extends UnitTestCase {
-
+
+ /**
+ * Each database table receives a number of test entries. These are saved as
+ * arrays of stcClass objects available to this class. This means that
+ * every test has access to these test data. The order of the following array is
+ * crucial, because of the interrelationships between objects.
+ */
+ var $tables = array('grade_categories',
+ 'grade_items',
+ 'grade_calculations',
+ 'grade_grades_raw',
+ 'grade_grades_final',
+ 'grade_grades_text',
+ 'grade_outcomes',
+ 'grade_history');
+
var $grade_items = array();
var $grade_categories = array();
+ var $grade_calculations = array();
+ var $grade_grades_raw = array();
+ var $grade_grades_final = array();
+ var $grade_grades_text = array();
+ var $grade_outcomes = array();
+ var $grade_history = array();
+
var $courseid = 1;
+ var $userid = 1;
/**
* Create temporary entries in the database for these tests.
* data have to be artificially inseminated (:-) in the DB.
*/
function setUp()
+ {
+ foreach ($this->tables as $table) {
+ $function = "load_$table";
+ $this->$function();
+ }
+ }
+
+ /**
+ * Delete temporary entries from the database
+ */
+ function tearDown()
+ {
+ foreach ($this->tables as $table) {
+ foreach ($this->$table as $object) {
+ delete_records($table, 'id', $object->id);
+ }
+
+ // If data has been entered in DB for any table, unset corresponding array
+ if (count($this->$table) > 0) {
+ unset ($this->$table);
+ }
+ }
+ }
+
+ function load_grade_categories()
{
$grade_category = new stdClass();
if ($grade_category->id = insert_record('grade_categories', $grade_category)) {
$this->grade_categories[] = $grade_category;
- }
+ }
+ }
+ function load_grade_items()
+ {
$grade_item = new stdClass();
$grade_item->courseid = $this->courseid;
- $grade_item->categoryid = $grade_category->id;
+ $grade_item->categoryid = $this->grade_categories[0]->id;
$grade_item->itemname = 'unittestgradeitem1';
$grade_item->itemtype = 'mod';
$grade_item->itemmodule = 'quiz';
$grade_item->iteminfo = 'Grade item used for unit testing';
$grade_item->timecreated = mktime();
$grade_item->timemodified = mktime();
-
+
if ($grade_item->id = insert_record('grade_items', $grade_item)) {
$this->grade_items[] = $grade_item;
}
$grade_item = new stdClass();
$grade_item->courseid = $this->courseid;
- $grade_item->categoryid = $grade_category->id;
+ $grade_item->categoryid = $this->grade_categories[0]->id;
$grade_item->itemname = 'unittestgradeitem3';
$grade_item->itemtype = 'mod';
$grade_item->itemmodule = 'forum';
$grade_item->iteminfo = 'Grade item used for unit testing';
$grade_item->timecreated = mktime();
$grade_item->timemodified = mktime();
-
- // $grade_item->set_calculation('MEAN([unittestgradeitem1],[unittestgradeitem2])');
if ($grade_item->id = insert_record('grade_items', $grade_item)) {
$this->grade_items[] = $grade_item;
- }
+ }
+ }
+
+ function load_grade_calculations()
+ {
+ $grade_calculation = new stdClass();
+ $grade_calculation->itemid = $this->grade_items[0]->id;
+ $grade_calculation->calculation = 'MEAN([unittestgradeitem2], [unittestgradeitem3])';
+ $grade_calculation->timecreated = mktime();
+ $grade_calculation->timemodified = mktime();
+
+ if ($grade_calculation->id = insert_record('grade_calculations', $grade_calculation)) {
+ $this->grade_calculations[] = $grade_calculation;
+ $this->grade_items[0]->calculation = $grade_calculation;
+ }
}
+ function load_grade_grades_raw()
+ {
- /**
- * Delete temporary entries from the database
- */
- function tearDown()
+ }
+
+ function load_grade_grades_final()
{
- foreach ($this->grade_items as $grade_item) {
- delete_records('grade_items', 'id', $grade_item->id);
- }
- foreach ($this->grade_categories as $grade_category) {
- delete_records('grade_categories', 'id', $grade_category->id);
- }
- unset($this->grade_items);
- unset($this->grade_categories);
}
+
+ function load_grade_grades_text()
+ {
+ }
+
+ function load_grade_outcomes()
+ {
+
+ }
+
+ function load_grade_history()
+ {
+
+ }
+
+/**
+ * TESTS BEGIN HERE
+ */
function test_grade_get_items()
{
$grade_items = grade_get_items($this->courseid);
function test_grade_item_get_calculation()
{
+ $grade_item = new grade_item($this->grade_items[0]);
+ $grade_calculation = $grade_item->get_calculation();
+ $this->assertEqual($this->grade_calculations[0]->id, $grade_calculation->id);
+ }
+
+ function test_grade_item_set_calculation()
+ {
+ $grade_item = new grade_item($this->grade_items[1]);
+ $calculation = 'SUM([unittestgradeitem1], [unittestgradeitem3])';
+ $grade_item->set_calculation($calculation);
+ $new_calculation = $grade_item->get_calculation();
+ $this->assertEqual($calculation, $new_calculation->calculation);
}
function test_grade_item_get_category()