/**
* For a given set of items, create a category to group them together (if one doesn't yet exist).
* Modules may want to do this when they are created. However, the ultimate control is in the gradebook interface itself.
-*
+*
+* @param int $courseid
* @param string $fullname The name of the new category
* @param array $items An array of grade_items to group under the new category
* @param string $aggregation
* @return mixed New grade_category id if successful
*/
-function grade_create_category($fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN)
+function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN)
{
$params = new stdClass();
+ $params->courseid = $courseid;
$params->fullname = $fullname;
$params->items = $items;
$params->aggregation = $aggregation;
}
}
+/**
+ * Checks whether the given variable name is defined as a variable within the given object.
+ * @todo Move to moodlelib.php
+ * @note This will NOT work with stdClass objects, which have no class variables.
+ * @param string $var The variable name
+ * @param object $object The object to check
+ * @return boolean
+ */
+function in_object_vars($var, $object)
+{
+ $class_vars = get_class_vars(get_class($object));
+ $class_vars = array_keys($class_vars);
+ return in_array($var, $class_vars);
+}
+
/**
* Class representing a grade item. It is responsible for handling its DB representation,
* modifying and returning its metadata.
{
/**
* The table name
- * @var string $tablename
+ * @var string $table
*/
- var $tablename = 'grade_items';
+ var $table = 'grade_items';
+
+ /**
+ * Array of class variables that are not part of the DB table fields
+ * @var array $nonfields
+ */
+ var $nonfields = array('table', 'nonfields', 'required_fields');
+
+ /**
+ * Array of required fields (keys) and their default values (values).
+ * @var array $required_fields
+ */
+ var $required_fields = array('gradetype' => 0,
+ 'grademax' => 100.00000,
+ 'grademin' => 0.00000,
+ 'gradepass' => 0.00000,
+ 'multfactor' => 1.00000,
+ 'plusfactor' => 0.00000,
+ 'sortorder' => 0,
+ 'hidden' => 0,
+ 'locked' => 0,
+ 'needsupdate' => 0);
/**
* The grade_item PK.
*/
var $itemnumber;
+ /**
+ * Info and notes about this item.
+ * @var string $iteminfo
+ */
+ var $iteminfo;
+
/**
* The type of grade (0 = value, 1 = scale, 2 = text)
* @var int $gradetype
{
if (!empty($params) && (is_array($params) || is_object($params))) {
foreach ($params as $param => $value) {
- if (method_exists($this, $param)) {
+ if (in_object_vars($param, $this)) {
$this->$param = $value;
}
}
+
+ $this->set_defaults();
}
}
/**
- * Records this object in the Database.
+ * Replaces NULL values with defaults defined in the DB, for required fields.
+ * This should use the DB table METADATA, but to start with I am hard-coding it.
+ *
+ * @return void
+ */
+ function set_defaults()
+ {
+ foreach ($this->required_fields as $field => $default) {
+ if (is_null($this->$field)) {
+ $this->$field = $default;
+ }
+ }
+ }
+
+ /**
+ * Records this object in the Database, sets its id to the returned value, and returns that value.
* @return int PK ID if successful, false otherwise
*/
function insert()
{
- return insert_record($this->table, $this, true);
+ $this->set_defaults();
+ $this->id = insert_record($this->table, $this, true);
+ return $this->id;
}
-
+
+ /**
+ * Updates this object in the Database, based on its object variables. ID must be set.
+ *
+ * @return boolean
+ */
+ function update()
+ {
+ $this->set_defaults();
+ return update_record($this->table, $this);
+ }
+
/**
* Deletes this object from the database.
*/
$wheresql = '';
foreach ($variables as $var => $value) {
- if (!empty($value)) {
+ if (!empty($value) && !in_array($var, $this->nonfields)) {
$wheresql .= " $var = '$value' AND ";
}
}
{
/**
* The table name
- * @var string $tablename
+ * @var string $table
*/
- var $tablename = 'grade_categories';
+ var $table = 'grade_categories';
+ /**
+ * Array of class variables that are not part of the DB table fields
+ * @var array $nonfields
+ */
+ var $nonfields = array('table', 'nonfields', 'required_fields');
+
+ /**
+ * Array of required fields (keys) and their default values (values).
+ * @var array $required_fields
+ */
+ var $required_fields = array('aggregation' => 0,
+ 'keephigh' => 0,
+ 'fullname' => null,
+ 'droplow' => 0,
+ 'hidden' => 0);
/**
* The grade_category PK.
* @var int $id The grade_category PK
{
if (!empty($params) && (is_array($params) || is_object($params))) {
foreach ($params as $param => $value) {
- if (method_exists($this, $param)) {
+ if (in_object_vars($param, $this)) {
$this->$param = $value;
}
}
+
+ $this->set_defaults();
}
}
/**
- * Records this object in the Database.
+ * Replaces NULL values with defaults defined in the DB, for required fields.
+ * This should use the DB table METADATA, but to start with I am hard-coding it.
+ *
+ * @return void
+ */
+ function set_defaults()
+ {
+ foreach ($this->required_fields as $field => $default) {
+ if (is_null($this->$field)) {
+ $this->$field = $default;
+ }
+ }
+ }
+
+
+ /**
+ * Records this object in the Database, sets its id to the returned value, and returns that value.
* @return int PK ID if successful, false otherwise
*/
function insert()
{
- return insert_record($this->table, $this, true);
+ $this->set_defaults();
+ $this->id = insert_record($this->table, $this, true);
+ return $this->id;
}
+ /**
+ * Updates this object in the Database, based on its object variables. ID must be set.
+ *
+ * @return boolean
+ */
+ function update()
+ {
+ $this->set_defaults();
+ return update_record($this->table, $this);
+ }
/**
* Deletes this object from the database.
global $CFG;
require_once($CFG->libdir . '/simpletestlib.php');
require_once($CFG->libdir . '/gradelib.php');
+require_once($CFG->libdir . '/dmllib.php');
class gradelib_test extends UnitTestCase {
+ var $grade_items = array();
+ var $grade_categories = array();
+ var $courseid = 1;
+
/**
* Create temporary entries in the database for these tests.
+ * These tests have to work no matter the data currently in the database
+ * (meaning they should run on a brand new site). This means several items of
+ * data have to be artificially inseminated (:-) in the DB.
*/
function setUp()
{
-
+ $grade_category = new stdClass();
+
+ $grade_category->fullname = 'unittestcategory1';
+ $grade_category->courseid = $this->courseid;
+ $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
+ $grade_category->keephigh = 100;
+ $grade_category->droplow = 10;
+ $grade_category->hidden = 0;
+
+ if ($grade_category->id = insert_record('grade_categories', $grade_category)) {
+ $this->grade_categories[] = $grade_category;
+ }
+
+ $grade_item = new stdClass();
+
+ $grade_item->courseid = $this->courseid;
+ $grade_item->categoryid = $grade_category->id;
+ $grade_item->itemname = 'unittestgradeitem1';
+ $grade_item->itemtype = 'mod';
+ $grade_item->itemmodule = 'quiz';
+ $grade_item->iteminstance = 1;
+ $grade_item->iteminfo = 'Grade item used for unit testing';
+
+ 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->itemname = 'unittestgradeitem2';
+ $grade_item->itemtype = 'import';
+ $grade_item->itemmodule = 'assignment';
+ $grade_item->iteminstance = 2;
+ $grade_item->iteminfo = 'Grade item used for unit testing';
+ $grade_item->locked = mktime() + 240000;
+
+ 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->itemname = 'unittestgradeitem3';
+ $grade_item->itemtype = 'mod';
+ $grade_item->itemmodule = 'forum';
+ $grade_item->iteminstance = 3;
+ $grade_item->iteminfo = 'Grade item used for unit testing';
+
+ if ($grade_item->id = insert_record('grade_items', $grade_item)) {
+ $this->grade_items[] = $grade_item;
+ }
}
+
/**
* Delete temporary entries from the database
*/
function tearDown()
{
-
+ 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);
+ }
}
function test_grade_get_items()
{
- $courseid = 1;
- $itemmname = 'grade_item_1';
- $itemtype = 'mod';
- $itemmodule = 'quiz';
-
- $grade_items = grade_get_items($courseid, $itemname, $itemtype, $itemmodule);
+ $grade_items = grade_get_items($this->courseid);
$this->assertTrue(is_array($grade_items));
- $this->assertEqual(count($grade_items), 4);
+ $this->assertEqual(count($grade_items), 3);
}
function test_grade_create_item()
{
+ $params = new stdClass();
+ $params->courseid = $this->courseid;
+ $params->categoryid = $this->grade_categories[0]->id;
+ $params->itemname = 'unittestgradeitem4';
+ $params->itemtype = 'mod';
+ $params->itemmodule = 'database';
+ $params->iteminstance = 4;
+ $params->iteminfo = 'Grade item used for unit testing';
+
+ $params->id = grade_create_item($params);
+ $last_grade_item = end($this->grade_items);
+
+ $this->assertEqual($params->id, $last_grade_item->id + 1);
+ $this->grade_items[] = $params;
}
function test_grade_create_category()
{
+ $grade_category = new stdClass();
+
+ $grade_category->id = grade_create_category($this->courseid, 'unittestcategory2', $this->grade_items, GRADE_AGGREGATE_MEAN);
+ $last_grade_category = end($this->grade_categories);
+ $this->assertEqual($grade_category->id, $last_grade_category->id + 1);
+ $this->grade_categories[] = $grade_category;
}
function test_grade_is_locked()
+ {
+ $grade_item = $this->grade_items[0];
+ $this->assertFalse(grade_is_locked($grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance));
+ $grade_item = $this->grade_items[1];
+ $this->assertTrue(grade_is_locked($grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance));
+ }
+
+ function test_grade_item_construct()
+ {
+ $params = new stdClass();
+
+ $params->courseid = $this->courseid;
+ $params->categoryid = $this->grade_categories[0]->id;
+ $params->itemname = 'unittestgradeitem4';
+ $params->itemtype = 'mod';
+ $params->itemmodule = 'database';
+ $params->iteminstance = 4;
+ $params->iteminfo = 'Grade item used for unit testing';
+
+ $grade_item = new grade_item($params);
+
+ $this->assertEqual($params->courseid, $grade_item->courseid);
+ $this->assertEqual($params->categoryid, $grade_item->categoryid);
+ $this->assertEqual($params->itemname, $grade_item->itemname);
+ $this->assertEqual($params->itemtype, $grade_item->itemtype);
+ $this->assertEqual($params->itemmodule, $grade_item->itemmodule);
+ $this->assertEqual($params->iteminstance, $grade_item->iteminstance);
+ $this->assertEqual($params->iteminfo, $grade_item->iteminfo);
+ }
+
+ function test_grade_item_insert()
+ {
+ $grade_item = new grade_item();
+
+ $grade_item->courseid = $this->courseid;
+ $grade_item->categoryid = $this->grade_categories[0]->id;
+ $grade_item->itemname = 'unittestgradeitem4';
+ $grade_item->itemtype = 'mod';
+ $grade_item->itemmodule = 'quiz';
+ $grade_item->iteminstance = 1;
+ $grade_item->iteminfo = 'Grade item used for unit testing';
+
+ $grade_item->insert();
+
+ $last_grade_item = end($this->grade_items);
+
+ $this->assertEqual($grade_item->id, $last_grade_item->id + 1);
+ $this->grade_items[] = $grade_item;
+ }
+
+ function test_grade_item_delete()
+ {
+ $grade_item = new grade_item($this->grade_items[0]);
+ $this->assertTrue($grade_item->delete());
+ $this->assertFalse(get_record('grade_items', 'id', $grade_item->id));
+ }
+
+ function test_grade_item_update()
+ {
+ $grade_item = new grade_item($this->grade_items[0]);
+ $grade_item->iteminfo = 'Updated info for this unittest grade_item';
+ $this->assertTrue($grade_item->update());
+ $this->assertEqual($grade_item->iteminfo, get_field('grade_items', 'iteminfo', 'id', $grade_item->id));
+ }
+
+ function test_grade_item_get_by_id()
+ {
+
+ }
+
+ function test_grade_item_get_record()
+ {
+
+ }
+
+ function test_grade_item_get_records_select()
+ {
+
+ }
+
+ function test_grade_item_get_raw()
+ {
+
+ }
+
+ function test_grade_item_get_final()
+ {
+
+ }
+
+ function test_grade_item_get_calculation()
+ {
+
+ }
+
+ function test_grade_item_get_category()
+ {
+
+ }
+
+ function test_grade_category_construct()
+ {
+
+ }
+
+ function test_grade_category_insert()
+ {
+
+ }
+
+ function test_grade_category_update()
{
}
+
+ function test_grade_category_delete()
+ {
+
+ }
+
+ function test_grade_category_get_by_id()
+ {
+
+ }
+
+ function test_grade_category_get_record()
+ {
+
+ }
}
?>