Skip to content

Commit

Permalink
MDL-9506 Number of important changes, restored unit test stability, o…
Browse files Browse the repository at this point in the history
…n to next changes...
  • Loading branch information
nicolasconnault committed Apr 27, 2007
1 parent 9d2cddf commit 3058964
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 240 deletions.
27 changes: 10 additions & 17 deletions lib/grade/grade_calculation.php
Expand Up @@ -29,13 +29,18 @@
* 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.
Expand All @@ -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.
Expand All @@ -78,11 +74,9 @@ function grade_calculation($params = null)
* @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 {
Expand All @@ -95,6 +89,5 @@ function get_record($static=false, $field1, $value1, $field2='', $value2='', $fi
return false;
}
}

}
?>
56 changes: 5 additions & 51 deletions lib/grade/grade_category.php
Expand Up @@ -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';
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -145,11 +100,10 @@ function get_by_id($id, $static=false)
* @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 {
Expand Down
102 changes: 31 additions & 71 deletions lib/grade/grade_item.php
Expand Up @@ -29,34 +29,18 @@
* 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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -231,11 +180,9 @@ function get_by_id($id, $static=false)
* @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 {
Expand All @@ -255,8 +202,7 @@ function get_record($static=false, $field1, $value1, $field2='', $value2='', $fi
*
* @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;
}
Expand All @@ -266,8 +212,7 @@ function get_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;
}
Expand All @@ -277,13 +222,12 @@ function get_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;
Expand All @@ -303,8 +247,7 @@ function get_calculation($fetch = 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)) {
Expand All @@ -328,6 +271,7 @@ function set_calculation($calculation = null)
}
} else { // Updating
$grade_calculation->calculation = $calculation;
$grade_calculation = new grade_calculation($grade_calculation);
$this->calculation = $grade_calculation;
return $grade_calculation->update();
}
Expand All @@ -339,14 +283,30 @@ function set_calculation($calculation = null)
*
* @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;
} else {
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;
}
}
}
?>

0 comments on commit 3058964

Please sign in to comment.