* A calculation string used to compute the value displayed by a grade_item.
* There can be only one grade_calculation per grade_item (one-to-one).
*/
-class grade_calculation extends grade_object
-{
+class grade_calculation extends grade_object {
/**
- * The table name
+ * DB Table (used by grade_object).
* @var string $table
*/
var $table = 'grade_calculations';
+
+ /**
+ * Array of class variables that are not part of the DB table fields
+ * @var array $nonfields
+ */
+ var $nonfields = array('table', 'nonfields');
/**
* A reference to the grade_item this calculation belongs to.
*/
var $usermodified;
- /**
- * Constructor.
- * @param object $params Object or array of variables=>values to assign to this object upon creation
- */
- function grade_calculation($params = null)
- {
-
- }
-
/**
* Finds and returns a grade_calculation object based on 1-3 field values.
* @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
+ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") {
if ($grade_calculation = get_record('grade_calculations', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
- if ($static) {
+ if (!isset($this)) {
$grade_calculation = new grade_calculation($grade_calculation);
return $grade_calculation;
} else {
return false;
}
}
-
}
?>
require_once('grade_object.php');
-class grade_category extends grade_object
-{
+class grade_category extends grade_object {
/**
- * The table name
+ * DB Table (used by grade_object).
* @var string $table
*/
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');
+ var $nonfields = array('table', 'nonfields');
- /**
- * 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 course this category belongs to.
* @var array $children
*/
var $children;
-
- /**
- * Constructor
- * @param object $params an object with named parameters for this category.
- */
- function grade_category($params=NULL)
- {
- if (!empty($params) && (is_array($params) || is_object($params))) {
- foreach ($params as $param => $value) {
- if (in_object_vars($param, $this)) {
- $this->$param = $value;
- }
- }
-
- $this->set_defaults();
- }
- }
- /**
- * Finds and returns a grade_category object based on its ID number.
- *
- * @param int $id
- * @param boolean $static Unless set to true, this method will also set $this object with the returned values.
- * @return object grade_category object or false if none found.
- */
- function get_by_id($id, $static=false)
- {
- if ($static) {
- return grade_category::get_record(true, 'id', $id);
- } else {
- return $this->get_record(false, 'id', $id);
- }
- }
-
-
/**
* Finds and returns a grade_category 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 $fields
* @return object grade_category object or false if none found.
*/
- function get_record($static=false, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*")
+ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*")
{
- // In Moodle 2.0 (PHP5) we can replace table names with the static class var grade_category::$table
if ($grade_category = get_record('grade_categories', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
- if ($static) {
+ if (!isset($this)) {
$grade_category = new grade_category($grade_category);
return $grade_category;
} else {
* Class representing a grade item. It is responsible for handling its DB representation,
* modifying and returning its metadata.
*/
-class grade_item extends grade_object
-{
+class grade_item extends grade_object {
/**
- * The table name
+ * DB Table (used by grade_object).
* @var string $table
*/
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', 'calculation');
-
- /**
- * 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);
+ var $nonfields = array('table', 'nonfields', 'calculation');
/**
* The course this grade_item belongs to.
* Date until which to lock this grade_item. If null, 0 or false, grade_item is not locked. Locking prevents updating.
* @var int $locked
*/
- var $locked;
+ var $locked = false;
/**
* If set, the whole column will be recalculated, then this flag will be switched off.
*/
var $calculation;
- /**
- * Constructor
- * @param object $params an object with named parameters for this grade item.
- */
- function grade_item($params=NULL)
- {
- if (!empty($params) && (is_array($params) || is_object($params))) {
- foreach ($params as $param => $value) {
- if (in_object_vars($param, $this)) {
- $this->$param = $value;
- }
- }
- $this->set_defaults();
- }
- }
-
-
- /**
- * Finds and returns a grade_item object based on its ID number.
- *
- * @param int $id
- * @param boolean $static Unless set to true, this method will also set $this object with the returned values.
- * @return object grade_item object or false if none found.
- */
- function get_by_id($id, $static=false)
- {
- if ($static) {
- return grade_item::get_record(true, 'id', $id);
- } else {
- return $this->get_record(false, 'id', $id);
- }
- }
-
-
/**
* Finds and returns a grade_item 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 $fields
* @return object grade_item 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_item::$table
+ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") {
if ($grade_item = get_record('grade_items', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
- if ($static) {
+ if (!isset($this)) {
$grade_item = new grade_item($grade_item);
return $grade_item;
} else {
*
* @return mixed grades_Raw object if found, or false.
*/
- function get_raw()
- {
+ function get_raw() {
$grade_raw = get_record('grade_grades_raw', 'itemid', $this->id);
return $grade_raw;
}
*
* @return mixed grades_Final object if found, or false.
*/
- function get_final()
- {
+ function get_final() {
$grade_final = get_record('grade_grades_final', 'itemid', $this->id);
return $grade_final;
}
* @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($fetch = false)
- {
+ function get_calculation($fetch = false) {
if (!$fetch) {
return $this->calculation;
}
- $grade_calculation = grade_calculation::get_record(true, 'itemid', $this->id);
+ $grade_calculation = grade_calculation::fetch(true, 'itemid', $this->id);
if (empty($grade_calculation)) { // There is no calculation in DB
return false;
* @param string $calculation
* @return boolean
*/
- function set_calculation($calculation = null)
- {
+ function set_calculation($calculation = null) {
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)) {
}
} else { // Updating
$grade_calculation->calculation = $calculation;
+ $grade_calculation = new grade_calculation($grade_calculation);
$this->calculation = $grade_calculation;
return $grade_calculation->update();
}
*
* @return mixed grade_category object if applicable, NULL otherwise
*/
- function get_category()
- {
+ function get_category() {
if (!empty($this->categoryid)) {
$grade_category = new grade_category($this->category_id);
return $grade_category;
return null;
}
}
+
+ /**
+ * Returns the locked state of this grade_item (if the grade_item is locked OR no specific
+ * $userid is given) or the locked state of a specific grade within this item if a specific
+ * $userid is given and the grade_item is unlocked.
+ *
+ * @param int $userid
+ * @return boolean Locked state
+ */
+ function is_locked($userid=NULL) {
+ if ($this->locked || empty($userid)) {
+ return $this->locked; // This could be true or false (false only if no $userid given)
+ } else {
+ $final = $this->get_final($userid);
+ return $final->locked;
+ }
+ }
}
?>
* An abstract object that holds methods and attributes common to all grade_* objects defined here.
* @abstract
*/
-class grade_object
-{
- /**
- * The table name
- * @var string $table
- */
- var $table = null;
-
+class grade_object {
/**
* Array of class variables that are not part of the DB table fields
* @var array $nonfields
*/
- var $nonfields = array('table', 'nonfields', 'required_fields');
+ var $nonfields = array('nonfields', 'required_fields');
/**
* Array of required fields (keys) and their default values (values).
* @var int $timemodified
*/
var $timemodified;
-
- /**
- * Finds and returns a grade_object based on its ID number.
- *
- * @abstract
- * @param int $id
- * @param boolean $static Unless set to true, this method will also set $this object with the returned values.
- * @return object grade_object or false if none found.
- */
- function get_by_id($id, $static=false)
- {
- // Implemented in child objects
- }
-
/**
- * Finds and returns a grade_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_object or false if none found.
- */
- function get_record($static=false, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*")
- {
- // Implemented in child objects
+ * Constructor. Optionally (and by default) attempts to fetch corresponding row from DB.
+ * @param object $params an object with named parameters for this grade item.
+ * @param boolean $fetch Whether to fetch corresponding row from DB or not.
+ */
+ function grade_object($params=NULL, $fetch = true) {
+ if (!empty($params) && (is_array($params) || is_object($params))) {
+ $this->assign_to_this($params);
+
+ if ($fetch) {
+ $records = $this->fetch_all_using_this();
+ if ($records && count($records) > 0) {
+ $this->assign_to_this(current($records));
+ }
+ }
+ }
}
/**
*
* @return boolean
*/
- function update()
- {
- $this->set_defaults();
+ function update() {
$result = update_record($this->table, $this);
if ($result) {
$this->timemodified = mktime();
/**
* Deletes this object from the database.
*/
- function delete()
- {
+ function delete() {
return delete_records($this->table, 'id', $this->id);
}
- /**
- * 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()
- {
- $this->set_defaults();
+ function insert() {
$this->set_timecreated();
- $this->id = insert_record($this->table, $this, true);
+ $clone = $this;
+
+ // Unset non-set fields
+ foreach ($clone as $var => $val) {
+ if (empty($val)) {
+ unset($clone->$var);
+ }
+ }
+
+ $this->id = insert_record($this->table, $clone, true);
return $this->id;
}
* Uses the variables of this object to retrieve all matching objects from the DB.
* @return array $objects
*/
- function get_records_select()
- {
+ function fetch_all_using_this() {
$variables = get_object_vars($this);
$wheresql = '';
* @param boolean $override Whether to override an existing value for this field in the DB.
* @return boolean True if successful, false otherwise.
*/
- function set_timecreated($timestamp = null, $override = false)
- {
+ function set_timecreated($timestamp = null, $override = false) {
if (empty($timestamp)) {
$timestamp = mktime();
}
}
return $this->timecreated;
}
+
+ /**
+ * Given an associated array or object, cycles through each key/variable
+ * and assigns the value to the corresponding variable in this object.
+ */
+ function assign_to_this($params) {
+ foreach ($params as $param => $value) {
+ if (in_object_vars($param, $this)) {
+ $this->$param = $value;
+ }
+ }
+ }
+
}
?>
define('GRADE_AGGREGATE_SUM', 2);
define('GRADE_AGGREGATE_MODE', 3);
-require_once($CFG->libdir . '/gradebook/grade_category.php');
-require_once($CFG->libdir . '/gradebook/grade_item.php');
-require_once($CFG->libdir . '/gradebook/grade_calculation.php');
+require_once($CFG->libdir . '/grade/grade_category.php');
+require_once($CFG->libdir . '/grade/grade_item.php');
+require_once($CFG->libdir . '/grade/grade_calculation.php');
/**
* Extracts from the gradebook all the grade items attached to the calling object.
* @param int $idnumber grade item Primary Key
* @return array An array of grade items
*/
-function grade_get_items($courseid, $itemname=NULL, $itemtype=NULL, $itemmodule=NULL, $iteminstance=NULL, $itemnumber=NULL, $idnumber=NULL)
-{
- $grade_item = new grade_item();
- $grade_item->courseid = $courseid;
- $grade_item->itemname = $itemname;
- $grade_item->itemtype = $itemtype;
- $grade_item->itemmodule = $itemmodule;
- $grade_item->iteminstance = $iteminstance;
- $grade_item->itemnumber = $itemnumber;
- $grade_item->id = $idnumber;
-
- $grade_items = $grade_item->get_records_select();
+function grade_get_items($courseid, $itemname=NULL, $itemtype=NULL, $itemmodule=NULL, $iteminstance=NULL, $itemnumber=NULL, $idnumber=NULL) {
+ $grade_item = new grade_item(compact('courseid', 'itemname', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber', 'idnumber'), false);
+ $grade_items = $grade_item->fetch_all_using_this();
return $grade_items;
}
* @param
* @return mixed New grade_item id if successful
*/
-function grade_create_item($params)
-{
+function grade_create_item($params) {
$grade_item = new grade_item($params);
return $grade_item->insert();
}
* @param string $aggregation
* @return mixed New grade_category id if successful
*/
-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;
-
- $grade_category = new grade_category($params);
+function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN) {
+ $grade_category = new grade_category(compact('courseid', 'fullname', 'items', 'aggregation'));
return $grade_category->insert();
}
* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
-*
+* If no $userid is given, the method will always return the grade_item's locked state.
+* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
+* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
+* return the locked state of the specific grade.
+*
* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
* @param string $itemmodule 'forum, 'quiz', 'csv' etc
* @param int $iteminstance id of the item module
+* @param int $itemnumber Optional number of the item to check
* @param int $userid ID of the user who owns the grade
* @return boolean Whether the grade is locked or not
*/
-function grade_is_locked($itemtype, $itemmodule, $iteminstance, $userid=NULL)
-{
- $grade_item = grade_item::get_record(true, 'itemtype', $itemtype, 'itemmodule', $itemmodule, 'iteminstance', $iteminstance);
- if ($grade_item) {
- return $grade_item->locked;
- } else {
- return null;
- }
+function grade_is_locked($itemtype, $itemmodule, $iteminstance, $itemnumber=NULL, $userid=NULL) {
+ $grade_item = new grade_item(compact('itemtype', 'itemmodule', 'iteminstance', 'itemnumber'));
+ return $grade_item->is_locked($userid);
}
?>