Skip to content

Commit

Permalink
MDL-10233 improved static factory methods of grade obejcts fetch() an…
Browse files Browse the repository at this point in the history
…d fetch_all(); implemented default values of grade objects, fixed null values for some properties that are not required - unit tests included, tested with PHP4 and PHP5
  • Loading branch information
skodak committed Jun 24, 2007
1 parent 11ac5c3 commit f92dcad
Show file tree
Hide file tree
Showing 29 changed files with 446 additions and 397 deletions.
2 changes: 1 addition & 1 deletion grade/export/grade_export_form.php
Expand Up @@ -9,7 +9,7 @@ function definition (){
$mform->addElement('header', 'general', get_string('gradeitemsinc', 'grades')); // TODO: localize
$id = $this->_customdata['id']; // course id
$mform->addElement('hidden', 'id', $id);
if ($grade_items = grade_get_items($id)) {
if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) {
foreach ($grade_items as $grade_item) {
$element = new HTML_QuickForm_advcheckbox('itemids[]', null, $grade_item->itemname, array('selected'=>'selected'), array(0, $grade_item->id));
$element->setChecked(1);
Expand Down
2 changes: 1 addition & 1 deletion grade/export/lib.php
Expand Up @@ -127,7 +127,7 @@ function grade_export($id, $itemids = '') {
}
} else {
// else we get all items for this course
$gradeitems = grade_get_items($this->id);
$gradeitems = grade_grades::fetch_all(array('courseid'=>$this->id));
}

if ($gradeitems) {
Expand Down
2 changes: 1 addition & 1 deletion grade/export/txt/grade_export_txt_form.php
Expand Up @@ -9,7 +9,7 @@ function definition (){
$mform->addElement('header', 'general', 'Gradeitems to be included'); // TODO: localize
$id = $this->_customdata['id']; // course id
$mform->addElement('hidden', 'id', $id);
if ($grade_items = grade_get_items($id)) {
if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) {
foreach ($grade_items as $grade_item) {
$element = new HTML_QuickForm_advcheckbox('itemids[]', null, $grade_item->itemname, array('selected'=>'selected'), array(0, $grade_item->id));
$element->setChecked(1);
Expand Down
2 changes: 1 addition & 1 deletion grade/export/txt/index.php
Expand Up @@ -61,7 +61,7 @@
// print_gradeitem_selections($id);
// print all items for selections
// make this a standard function in lib maybe
if ($grade_items = grade_get_items($id)) {
if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) {
echo '<form action="index.php" method="post">';
echo '<div>';
foreach ($grade_items as $grade_item) {
Expand Down
2 changes: 1 addition & 1 deletion grade/import/grade_import_form.php
Expand Up @@ -66,7 +66,7 @@ function definition () {
include_once($CFG->libdir.'/gradelib.php');

if ($id) {
if ($grade_items = grade_get_items($id)) {
if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) {
foreach ($grade_items as $grade_item) {
$gradeitems[$grade_item->idnumber] = $grade_item->itemname;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/db/install.xml
Expand Up @@ -1241,12 +1241,12 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="courseid"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The course this item is part of" PREVIOUS="id" NEXT="categoryid"/>
<FIELD NAME="categoryid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="(optional) the category group this item belongs to" PREVIOUS="courseid" NEXT="itemname"/>
<FIELD NAME="itemname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="The name of this item (pushed in by the module)" PREVIOUS="categoryid" NEXT="itemtype"/>
<FIELD NAME="itemname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="The name of this item (pushed in by the module)" PREVIOUS="categoryid" NEXT="itemtype"/>
<FIELD NAME="itemtype" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="'mod', 'blocks', 'import', 'calculated' etc" PREVIOUS="itemname" NEXT="itemmodule"/>
<FIELD NAME="itemmodule" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="'forum', 'quiz', 'csv', etc" PREVIOUS="itemtype" NEXT="iteminstance"/>
<FIELD NAME="itemmodule" TYPE="char" LENGTH="30" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="'forum', 'quiz', 'csv', etc" PREVIOUS="itemtype" NEXT="iteminstance"/>
<FIELD NAME="iteminstance" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="id of the item module" PREVIOUS="itemmodule" NEXT="itemnumber"/>
<FIELD NAME="itemnumber" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="Can be used to distinguish multiple grades for an activity" PREVIOUS="iteminstance" NEXT="iteminfo"/>
<FIELD NAME="iteminfo" TYPE="text" LENGTH="medium" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Info and notes about this item XXX" PREVIOUS="itemnumber" NEXT="idnumber"/>
<FIELD NAME="iteminfo" TYPE="text" LENGTH="medium" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="Info and notes about this item XXX" PREVIOUS="itemnumber" NEXT="idnumber"/>
<FIELD NAME="idnumber" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="Arbitrary idnumber provided by the module responsible" PREVIOUS="iteminfo" NEXT="calculation"/>
<FIELD NAME="calculation" TYPE="text" LENGTH="medium" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="Formula describing how to derive this grade from other items, referring to them using giXXX where XXX is grade item id ... eg something like: =sin(square([#gi20#])) + [#gi30#]" PREVIOUS="idnumber" NEXT="gradetype"/>
<FIELD NAME="gradetype" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="1" SEQUENCE="false" ENUM="false" COMMENT="0 = none, 1 = value, 2 = scale, 3 = text" PREVIOUS="calculation" NEXT="grademax"/>
Expand Down
41 changes: 37 additions & 4 deletions lib/db/upgrade.php
Expand Up @@ -812,12 +812,12 @@ function xmldb_main_upgrade($oldversion=0) {
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
$table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
$table->addFieldInfo('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
$table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
$table->addFieldInfo('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
$table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0');
Expand Down Expand Up @@ -1025,7 +1025,7 @@ function xmldb_main_upgrade($oldversion=0) {
/// Define field path to be added to grade_categories
$table = new XMLDBTable('grade_categories');
$field = new XMLDBField('path');
$field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'depth');
$field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'depth');

/// Launch add field path
$result = $result && add_field($table, $field);
Expand Down Expand Up @@ -1333,6 +1333,39 @@ function xmldb_main_upgrade($oldversion=0) {
/// Launch add field calculation
$result = $result && add_field($table, $field);
}
}

if ($result && $oldversion < 2007062401) {

/// Changing nullability of field itemname on table grade_items to null
$table = new XMLDBTable('grade_items');
$field = new XMLDBField('itemname');
$field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'categoryid');

/// Launch change of nullability for field itemname
$result = $result && change_field_notnull($table, $field);

$field = new XMLDBField('itemmodule');
$field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null, 'itemtype');

/// Launch change of nullability for field itemname
$result = $result && change_field_notnull($table, $field);

$field = new XMLDBField('iteminfo');
$field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, 'itemnumber');

/// Launch change of nullability for field itemname
$result = $result && change_field_notnull($table, $field);


/// Changing nullability of field path on table grade_categories to null
$table = new XMLDBTable('grade_categories');
$field = new XMLDBField('path');
$field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'depth');

/// Launch change of nullability for field path
$result = $result && change_field_notnull($table, $field);


/// Remove the obsoleted unitttests tables - they will be recreated automatically
$tables = array('grade_categories',
Expand Down
67 changes: 35 additions & 32 deletions lib/grade/grade_category.php
Expand Up @@ -85,19 +85,19 @@ class grade_category extends grade_object {
* A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) .
* @var int $aggregation
*/
var $aggregation;
var $aggregation = GRADE_AGGREGATE_MEAN_ALL;

/**
* Keep only the X highest items.
* @var int $keephigh
*/
var $keephigh;
var $keephigh = 0;

/**
* Drop the X lowest items.
* @var int $droplow
*/
var $droplow;
var $droplow = 0;

/**
* Array of grade_items or grade_categories nested exactly 1 level below this category
Expand All @@ -119,18 +119,6 @@ class grade_category extends grade_object {
*/
var $grade_item;

/**
* Constructor. Extends the basic functionality defined in grade_object.
* @param array $params Can also be a standard object.
* @param boolean $fetch Whether or not to fetch the corresponding row from the DB.
* @param object $grade_item The associated grade_item object can be passed during construction.
*/
function grade_category($params=NULL, $fetch=true) {
$this->grade_object($params, $fetch);
$this->path = grade_category::build_path($this);
}


/**
* Builds this category's path string based on its parents (if any) and its own id number.
* This is typically done just before inserting this object in the DB for the first time,
Expand All @@ -152,22 +140,35 @@ function build_path($grade_category) {


/**
* Finds and returns a grade_category object based on 1-3 field values.
* Finds and returns a grade_category instance based on params.
* @static
*
* @param string $field1
* @param string $value1
* @param string $field2
* @param string $value2
* @param string $field3
* @param string $value3
* @param string $fields
* @return object grade_category object or false if none found.
*/
function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") {
if ($grade_category = get_record('grade_categories', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
$grade_category = new grade_category($grade_category);
return $grade_category;
* @param array $params associative arrays varname=>value
* @return object grade_category instance or false if none found.
*/
function fetch($params) {
if ($category = grade_object::fetch_helper('grade_categories', 'grade_category', $params)) {
$category->path = grade_category::build_path($category);
return $category;

} else {
return false;
}
}

/**
* Finds and returns all grade_category instances based on params.
* @static
*
* @param array $params associative arrays varname=>value
* @return array array of grade_category insatnces or false if none found.
*/
function fetch_all($params) {
if ($categories = grade_object::fetch_all_helper('grade_categories', 'grade_category', $params)) {
foreach ($categories as $key=>$value) {
$categories[$key]->path = grade_category::build_path($categories[$key]);
}
return $categories;

} else {
return false;
Expand Down Expand Up @@ -360,7 +361,7 @@ function generate_grades() {
$items = array();

foreach($depends_on as $dep) {
$items[$dep] = grade_item::fetch('id', $dep);
$items[$dep] = grade_item::fetch(array('id'=>$dep));
}

// where to look for final grades - include or grade item too
Expand Down Expand Up @@ -712,9 +713,11 @@ function get_grade_item() {
return false;
}

$grade_item = new grade_item(array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id), false);
if (!$grade_items = $grade_item->fetch_all_using_this()) {
$params = array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id);

if (!$grade_items = grade_item::fetch_all($params)) {
// create a new one
$grade_item = new grade_item($params, false);
$grade_item->gradetype = GRADE_TYPE_VALUE;
$grade_item->insert();

Expand Down
56 changes: 23 additions & 33 deletions lib/grade/grade_grades.php
Expand Up @@ -67,13 +67,13 @@ class grade_grades extends grade_object {
* The maximum allowable grade when this grade was created.
* @var float $rawgrademax
*/
var $rawgrademax;
var $rawgrademax = 100;

/**
* The minimum allowable grade when this grade was created.
* @var float $rawgrademin
*/
var $rawgrademin;
var $rawgrademin = 0;

/**
* id of the scale, if this grade is based on a scale.
Expand Down Expand Up @@ -105,34 +105,25 @@ class grade_grades extends grade_object {
* 0 if visible, 1 always hidden or date not visible until
* @var float $hidden
*/
var $hidden;
var $hidden = 0;

/**
* 0 not locked, date when the item was locked
* @var float locked
*/
var $locked;
var $locked = 0;

/**
* 0 no automatic locking, date when to lock the grade automatically
* @var float $locktime
*/
var $locktime;
var $locktime = 0;

/**
* Exported flag
* @var boolean $exported
*/
var $exported;

/**
* Constructor. Extends the basic functionality defined in grade_object.
* @param array $params Can also be a standard object.
* @param boolean $fetch Wether or not to fetch the corresponding row from the DB.
*/
function grade_grades($params=NULL, $fetch=true) {
$this->grade_object($params, $fetch);
}
var $exported = 0;

/**
* Loads the grade_grades_text object linked to this grade (through the intersection of itemid and userid), and
Expand All @@ -141,7 +132,7 @@ function grade_grades($params=NULL, $fetch=true) {
*/
function load_text() {
if (empty($this->grade_grades_text->id)) {
$this->grade_grades_text = grade_grades_text::fetch('itemid', $this->itemid, 'userid', $this->userid);
$this->grade_grades_text = grade_grades_text::fetch(array('itemid'=>$this->itemid, 'userid'=>$this->userid));
}

return $this->grade_grades_text;
Expand All @@ -153,7 +144,7 @@ function load_text() {
*/
function load_grade_item() {
if (empty($this->grade_item) && !empty($this->itemid)) {
$this->grade_item = grade_item::fetch('id', $this->itemid);
$this->grade_item = grade_item::fetch(array('id'=>$this->itemid));
}
return $this->grade_item;
}
Expand Down Expand Up @@ -214,26 +205,25 @@ function set_locked($lockedstate) {
}

/**
* Finds and returns a grade_grades object based on 1-3 field values.
* Finds and returns a grade_grades instance based on params.
* @static
*
* @param string $field1
* @param string $value1
* @param string $field2
* @param string $value2
* @param string $field3
* @param string $value3
* @param string $fields
* @return object grade_category object or false if none found.
* @param array $params associative arrays varname=>value
* @return object grade_grades instance or false if none found.
*/
function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") {
if ($object = get_record('grade_grades', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
$object = new grade_grades($object);
return $object;
function fetch($params) {
return grade_object::fetch_helper('grade_grades', 'grade_grades', $params);
}

} else {
return false;
}
/**
* Finds and returns all grade_grades instances based on params.
* @static
*
* @param array $params associative arrays varname=>value
* @return array array of grade_grades insatnces or false if none found.
*/
function fetch_all($params) {
return grade_object::fetch_all_helper('grade_grades', 'grade_grades', $params);
}

/**
Expand Down

0 comments on commit f92dcad

Please sign in to comment.