Skip to content

Commit

Permalink
MDL-14967 Upgraded gradebook code and unit tests. 4 failing tests in …
Browse files Browse the repository at this point in the history
…grade_item to fix.
  • Loading branch information
nicolasconnault committed May 23, 2008
1 parent 3a4ff63 commit da3801e
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 748 deletions.
148 changes: 76 additions & 72 deletions lib/grade/grade_category.php

Large diffs are not rendered by default.

107 changes: 54 additions & 53 deletions lib/grade/grade_grade.php

Large diffs are not rendered by default.

242 changes: 123 additions & 119 deletions lib/grade/grade_item.php

Large diffs are not rendered by default.

68 changes: 34 additions & 34 deletions lib/grade/grade_object.php
Expand Up @@ -27,45 +27,47 @@
* An abstract object that holds methods and attributes common to all grade_* objects defined here.
* @abstract
*/
class grade_object {
abstract class grade_object {
public $table;

/**
* Array of required table fields, must start with 'id'.
* @var array $required_fields
*/
var $required_fields = array('id', 'timecreated', 'timemodified');
public $required_fields = array('id', 'timecreated', 'timemodified');

/**
* Array of optional fields with default values - usually long text information that is not always needed.
* If you want to create an instance without optional fields use: new grade_object($only_required_fields, false);
* @var array $optional_fields
*/
var $optional_fields = array();
public $optional_fields = array();

/**
* The PK.
* @var int $id
*/
var $id;
public $id;

/**
* The first time this grade_object was created.
* @var int $timecreated
*/
var $timecreated;
public $timecreated;

/**
* The last time this grade_object was modified.
* @var int $timemodified
*/
var $timemodified;
public $timemodified;

/**
* Constructor. Optionally (and by default) attempts to fetch corresponding row from DB.
* @param array $params an array with required parameters for this grade object.
* @param boolean $fetch Whether to fetch corresponding row from DB or not,
* optional fields might not be defined if false used
*/
function grade_object($params=NULL, $fetch=true) {
public function __construct($params=NULL, $fetch=true) {
if (!empty($params) and (is_array($params) or is_object($params))) {
if ($fetch) {
if ($data = $this->fetch($params)) {
Expand All @@ -89,15 +91,16 @@ function grade_object($params=NULL, $fetch=true) {
* If id present (==instance exists in db) fetches data from db.
* Defaults are used for new instances.
*/
function load_optional_fields() {
public function load_optional_fields() {
global $DB;
foreach ($this->optional_fields as $field=>$default) {
if (array_key_exists($field, $this)) {
continue;
}
if (empty($this->id)) {
$this->$field = $default;
} else {
$this->$field = get_field($this->table, $field, 'id', $this->id);
$this->$field = $DB->get_field($this->table, $field, array('id', $this->id));
}
}
}
Expand All @@ -109,9 +112,7 @@ function load_optional_fields() {
* @param array $params associative arrays varname=>value
* @return object grade_object instance or false if none found.
*/
function fetch($params) {
print_error('mustbeoveride', 'debug', '', 'fetch()');
}
public static abstract function fetch($params);

/**
* Finds and returns all grade_object instances based on params.
Expand All @@ -120,16 +121,14 @@ function fetch($params) {
* @param array $params associative arrays varname=>value
* @return array array of grade_object insatnces or false if none found.
*/
function fetch_all($params) {
print_error('mustbeoveride', 'debug', '', 'fetch_all()');
}
public static abstract function fetch_all($params);

/**
* Factory method - uses the parameters to retrieve matching instance from the DB.
* @static final protected
* @return mixed object instance or false if not found
*/
function fetch_helper($table, $classname, $params) {
protected static function fetch_helper($table, $classname, $params) {
if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) {
if (count($instances) > 1) {
// we should not tolerate any errors here - problems might appear later
Expand All @@ -146,7 +145,7 @@ function fetch_helper($table, $classname, $params) {
* @static final protected
* @return mixed array of object instances or false if not found
*/
function fetch_all_helper($table, $classname, $params) {
protected static function fetch_all_helper($table, $classname, $params) {
$instance = new $classname();

$classvars = (array)$instance;
Expand All @@ -173,7 +172,8 @@ function fetch_all_helper($table, $classname, $params) {
$wheresql = implode("AND", $wheresql);
}

if ($datas = get_records_select($table, $wheresql, 'id')) {
global $DB;
if ($datas = $DB->get_records_select($table, $wheresql, array('id'))) {
$result = array();
foreach($datas as $data) {
$instance = new $classname();
Expand All @@ -192,8 +192,8 @@ function fetch_all_helper($table, $classname, $params) {
* @param string $source from where was the object updated (mod/forum, manual, etc.)
* @return boolean success
*/
function update($source=null) {
global $USER, $CFG;
public function update($source=null) {
global $USER, $CFG, $DB;

if (empty($this->id)) {
debugging('Can not update grade object, no id!');
Expand All @@ -202,7 +202,7 @@ function update($source=null) {

$data = $this->get_record_data();

if (!update_record($this->table, addslashes_recursive($data))) {
if (!$DB->update_record($this->table, addslashes_recursive($data))) {
return false;
}

Expand All @@ -213,7 +213,7 @@ function update($source=null) {
$data->source = $source;
$data->timemodified = time();
$data->userlogged = $USER->id;
insert_record($this->table.'_history', addslashes_recursive($data));
$DB->insert_record($this->table.'_history', addslashes_recursive($data));
}

return true;
Expand All @@ -224,8 +224,8 @@ function update($source=null) {
* @param string $source from where was the object deleted (mod/forum, manual, etc.)
* @return boolean success
*/
function delete($source=null) {
global $USER, $CFG;
public function delete($source=null) {
global $USER, $CFG, $DB;

if (empty($this->id)) {
debugging('Can not delete grade object, no id!');
Expand All @@ -243,7 +243,7 @@ function delete($source=null) {
$data->source = $source;
$data->timemodified = time();
$data->userlogged = $USER->id;
insert_record($this->table.'_history', addslashes_recursive($data));
$DB->insert_record($this->table.'_history', addslashes_recursive($data));
}
return true;

Expand All @@ -255,7 +255,7 @@ function delete($source=null) {
/**
* Returns object with fields and values that are defined in database
*/
function get_record_data() {
public function get_record_data() {
$data = new object();
// we need to do this to prevent infinite loops in addslashes_recursive - grade_item -> category ->grade_item
foreach ($this as $var=>$value) {
Expand All @@ -277,8 +277,8 @@ function get_record_data() {
* @param string $source from where was the object inserted (mod/forum, manual, etc.)
* @return int PK ID if successful, false otherwise
*/
function insert($source=null) {
global $USER, $CFG;
public function insert($source=null) {
global $USER, $CFG, $DB;

if (!empty($this->id)) {
debugging("Grade object already exists!");
Expand All @@ -287,7 +287,7 @@ function insert($source=null) {

$data = $this->get_record_data();

if (!$this->id = insert_record($this->table, addslashes_recursive($data))) {
if (!$this->id = $DB->insert_record($this->table, addslashes_recursive($data))) {
debugging("Could not insert object into db");
return false;
}
Expand All @@ -304,7 +304,7 @@ function insert($source=null) {
$data->source = $source;
$data->timemodified = time();
$data->userlogged = $USER->id;
insert_record($this->table.'_history', addslashes_recursive($data));
$DB->insert_record($this->table.'_history', addslashes_recursive($data));
}

return $this->id;
Expand All @@ -316,13 +316,13 @@ function insert($source=null) {
* the object. This is different from the update() function, which acts on the DB record
* based on the object.
*/
function update_from_db() {
public function update_from_db() {
if (empty($this->id)) {
debugging("The object could not be used in its state to retrieve a matching record from the DB, because its id field is not set.");
return false;
}

if (!$params = get_record($this->table, 'id', $this->id)) {
global $DB;
if (!$params = $DB->get_record($this->table, array('id' => $this->id))) {
debugging("Object with this id:{$this->id} does not exist in table:{$this->table}, can not update from db!");
return false;
}
Expand All @@ -337,7 +337,7 @@ function update_from_db() {
* and assigns the value to the corresponding variable in this object.
* @static final
*/
function set_properties(&$instance, $params) {
public static function set_properties(&$instance, $params) {
$params = (array) $params;
foreach ($params as $var => $value) {
if (in_array($var, $instance->required_fields) or array_key_exists($var, $instance->optional_fields)) {
Expand Down

0 comments on commit da3801e

Please sign in to comment.