MDL-9506 Number of important changes, restored unit test stability, on to next changes...

This commit is contained in:
nicolasconnault
2007-04-27 07:28:41 +00:00
parent 9d2cddf138
commit 3058964f85
5 changed files with 109 additions and 240 deletions
+10 -17
View File
@@ -29,13 +29,18 @@ require_once('grade_object.php');
* 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.
@@ -55,15 +60,6 @@ class grade_calculation extends grade_object
*/
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.
@@ -78,11 +74,9 @@ class grade_calculation extends grade_object
* @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 {
@@ -95,6 +89,5 @@ class grade_calculation extends grade_object
return false;
}
}
}
?>
+5 -51
View File
@@ -25,10 +25,9 @@
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';
@@ -37,17 +36,8 @@ class grade_category extends 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('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.
@@ -96,46 +86,11 @@ class grade_category extends grade_object
* @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
@@ -145,11 +100,10 @@ class grade_category extends grade_object
* @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 {
+31 -71
View File
@@ -29,34 +29,18 @@ require_once('grade_object.php');
* 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.
@@ -170,7 +154,7 @@ class grade_item extends grade_object
* 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.
@@ -184,44 +168,9 @@ class grade_item extends grade_object
*/
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
@@ -231,11 +180,9 @@ class grade_item extends grade_object
* @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 {
@@ -255,8 +202,7 @@ class grade_item extends grade_object
*
* @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;
}
@@ -266,8 +212,7 @@ class grade_item extends grade_object
*
* @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;
}
@@ -277,13 +222,12 @@ class grade_item extends grade_object
* @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;
@@ -303,8 +247,7 @@ class grade_item extends grade_object
* @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)) {
@@ -328,6 +271,7 @@ class grade_item extends grade_object
}
} else { // Updating
$grade_calculation->calculation = $calculation;
$grade_calculation = new grade_calculation($grade_calculation);
$this->calculation = $grade_calculation;
return $grade_calculation->update();
}
@@ -339,8 +283,7 @@ class grade_item extends grade_object
*
* @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;
@@ -348,5 +291,22 @@ class grade_item extends grade_object
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;
}
}
}
?>
+45 -66
View File
@@ -27,19 +27,12 @@
* 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).
@@ -64,37 +57,23 @@ class grade_object
* @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));
}
}
}
}
/**
@@ -102,9 +81,7 @@ class grade_object
*
* @return boolean
*/
function update()
{
$this->set_defaults();
function update() {
$result = update_record($this->table, $this);
if ($result) {
$this->timemodified = mktime();
@@ -115,35 +92,26 @@ class grade_object
/**
* 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;
}
@@ -151,8 +119,7 @@ class grade_object
* 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 = '';
@@ -178,8 +145,7 @@ class grade_object
* @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();
}
@@ -200,5 +166,18 @@ class grade_object
}
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;
}
}
}
}
?>
+18 -35
View File
@@ -37,9 +37,9 @@ define('GRADE_AGGREGATE_MEDIAN', 1);
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.
@@ -60,18 +60,9 @@ require_once($CFG->libdir . '/gradebook/grade_calculation.php');
* @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;
}
@@ -85,8 +76,7 @@ function grade_get_items($courseid, $itemname=NULL, $itemtype=NULL, $itemmodule=
* @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();
}
@@ -101,15 +91,8 @@ function grade_create_item($params)
* @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();
}
@@ -118,21 +101,21 @@ function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_
* 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);
}
?>