Skip to content

Commit

Permalink
grade_item idnumber now self generated if needed.
Browse files Browse the repository at this point in the history
deleted flag implemented in grade_item::update method
added GRADE_TYPE_NONE to gradelib and updated grade_item object
Implemented multiple items (generation of itemnumber when not explicitly given)
  • Loading branch information
nicolasconnault committed Jun 7, 2007
1 parent c5b5f18 commit 1c307f2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 50 deletions.
36 changes: 19 additions & 17 deletions lib/grade/grade_category.php
Expand Up @@ -551,6 +551,19 @@ function can_add_child($child) {
}
}

/**
* Disassociates this category from its category parent(s). The object is then updated in DB.
* @return boolean Success or Failure
*/
function divorce_parent() {
$this->old_parent = $this->get_parent_category();
$this->parent = null;
$this->parent_category = null;
$this->depth = 1;
$this->path = '/' . $this->id;
return $this->update();
}

/**
* Looks at a path string (e.g. /2/45/56) and returns the depth level represented by this path (in this example, 3).
* If no string is given, it looks at the obect's path and assigns the resulting depth to its $depth variable.
Expand Down Expand Up @@ -770,19 +783,12 @@ function set_as_parent($children) {

// We passed all the checks, time to set the category as a parent.
foreach ($children as $child) {
if ($first_child_type == 'grade_item') {
$child->categoryid = $this->id;
if (!$child->update()) {
debugging("Could not set this category as a parent for one of its child grade_items, DB operation failed.");
return false;
}
} elseif ($first_child_type == 'grade_category') {
$child->parent = $this->id;
if (!$child->update()) {
debugging("Could not set this category as a parent for one of its child categories, DB operation failed.");
return false;
}
}
$child->divorce_parent();
$child->set_parent_id($this->id);
if (!$child->update()) {
debugging("Could not set this category as a parent for one of its children, DB operation failed.");
return false;
}
}

// TODO Assign correct sortorders to the newly assigned children and parent. Simply add 1 to all of them!
Expand Down Expand Up @@ -843,10 +849,6 @@ function get_parent_id() {
* @param id $parentid
*/
function set_parent_id($parentid) {
if ($this->parent != $parentid) {
$this->old_parent = $this->get_parent_category();
}

$this->parent = $parentid;
$this->path = grade_category::build_path($this);
$this->depth = $this->get_depth_from_path();
Expand Down
45 changes: 38 additions & 7 deletions lib/grade/grade_item.php
Expand Up @@ -229,6 +229,11 @@ function grade_item($params=NULL, $fetch=true) {
* In addition to update() as defined in grade_object, handle the grade_outcome and grade_scale objects.
*/
function update() {
// If item is flagged as deleted, only update that flag in DB. The other changes are ignored.
if (!empty($this->deleted) && $this->deleted) {
return set_field('grade_items', 'deleted', 1, 'id', $this->id);
}

if (!empty($this->outcome->id)) {
$this->outcomeid = $this->outcome->id;
}
Expand Down Expand Up @@ -397,6 +402,22 @@ function insert() {
$this->idnumber = rand(0,9999999999); // TODO replace rand() with proper random generator
}
}

// If a grade_item already exists with these itemtype, itemmodule and iteminstance
// but not itemnumber, generate an itemnumber.
if (empty($this->itemnumber) && !empty($this->itemtype) && !empty($this->itemmodule) && !empty($this->iteminstance)) {
$existing_item = get_record('grade_items',
'iteminstance', $this->iteminstance,
'itemmodule', $this->itemmodule,
'itemtype', $this->itemtype);

if (empty($existing_item->itemnumber)) {
$existing_item->itemnumber = 0;
}

$this->itemnumber = $existing_item->itemnumber + 1;
}


$result = parent::insert();

Expand Down Expand Up @@ -708,7 +729,18 @@ function flag_for_update() {

return $result;
}


/**
* Disassociates this item from its category parent(s). The object is then updated in DB.
* @return boolean Success or Failure
*/
function divorce_parent() {
$this->old_parent = $this->get_category();
$this->category = null;
$this->categoryid = null;
return $this->update();
}

/**
* Instantiates a grade_scale object whose data is retrieved from the DB,
* if this item's scaleid variable is set.
Expand Down Expand Up @@ -808,8 +840,11 @@ function get_standardised_final() {
$standardised_finals = array();

$final_grades = $this->load_final(true);
foreach ($final_grades as $userid => $final) {
$standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1);

if (!empty($final_grades)) {
foreach ($final_grades as $userid => $final) {
$standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1);
}
}

return $standardised_finals;
Expand Down Expand Up @@ -1002,10 +1037,6 @@ function get_parent_id() {
* @param int $parentid
*/
function set_parent_id($parentid) {
if ($this->categoryid != $parentid) {
$this->old_parent = $this->get_category();
}

$this->categoryid = $parentid;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/grade/grade_object.php
Expand Up @@ -7,7 +7,7 @@
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
// Copyright (C) 2001-2007 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
Expand Down Expand Up @@ -144,7 +144,7 @@ function update_from_db() {
$class = get_class($this);
$object = new $class(array('id' => $this->id));
foreach ($object as $var => $val) {
if ($this->$var != $val) {
if (!in_array($var, $this->nonfields) && $this->$var != $val) {
$this->$var = $val;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/grade/grade_tree.php
Expand Up @@ -95,12 +95,12 @@ class grade_tree {
* objects for the given courseid or the entire site if no courseid given. Full objects are instantiated
* by default, but this can be switched off. The tree is indexed by sortorder, to facilitate CRUD operations
* and renumbering.
* @param int $courseid
* @param int $courseid If null, a blank object is instantiated. If 0, all courses are retrieved in the entire site (can be very slow!)
* @param boolean $include_grades
* @param array $tree
*/
function grade_tree($courseid=NULL, $include_grades=false, $tree=NULL) {
if (empty($courseid)) {
if (is_null($courseid)) {
// empty object, do nothing
} else {
if ($courseid == 0) {
Expand Down
29 changes: 29 additions & 0 deletions lib/simpletest/grade/simpletest/testgradeitem.php
Expand Up @@ -82,6 +82,35 @@ function test_grade_item_insert() {
$this->assertEqual(11, $grade_item->sortorder);
}

function test_grade_item_generate_itemnumber() {
$grade_item = new grade_item($this->grade_items[0]);
$copy_grade_item = fullclone($grade_item);
$copy_grade_item->itemnumber = null;
$result_id = $copy_grade_item->insert();
$this->assertEqual(1, $copy_grade_item->itemnumber);

}

function test_grade_item_generate_idnumber() {

}

function test_grade_item_update_when_flagged_as_deleted() {

}

function test_grade_item_update_guess_outcomeid() {

}

function test_grade_item_update_default_gradetype() {

}

function test_grade_item_update_guess_scaleid() {

}

function test_grade_item_delete() {
$grade_item = new grade_item($this->grade_items[0]);
$this->assertTrue(method_exists($grade_item, 'delete'));
Expand Down
27 changes: 5 additions & 22 deletions lib/simpletest/testgradelib.php
Expand Up @@ -102,7 +102,6 @@ function create_test_tables() {
$table = new XMLDBTable('grade_items');

if (!table_exists($table)) {
/// Adding fields to table grade_items
$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);
Expand All @@ -113,7 +112,7 @@ function create_test_tables() {
$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('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
$table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1');
$table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
$table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
Expand All @@ -124,11 +123,10 @@ function create_test_tables() {
$table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('deleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);

/// Adding keys to table grade_items
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
$table->addKeyInfo('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id'));
Expand All @@ -144,7 +142,6 @@ function create_test_tables() {

if ($result && !table_exists($table)) {

/// Adding fields to table grade_categories
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
Expand All @@ -154,11 +151,8 @@ function create_test_tables() {
$table->addFieldInfo('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);

/// Adding keys to table grade_categories
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
$table->addKeyInfo('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id'));
Expand Down Expand Up @@ -272,7 +266,6 @@ function create_test_tables() {

if ($result && !table_exists($table)) {

/// Adding fields to table grade_grades_final
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
Expand All @@ -283,8 +276,6 @@ function create_test_tables() {
$table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);

/// Adding keys to table grade_grades_final
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
$table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
Expand All @@ -299,7 +290,6 @@ function create_test_tables() {

if ($result && !table_exists($table)) {

/// Adding fields to table grade_grades_raw
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
Expand All @@ -310,8 +300,6 @@ function create_test_tables() {
$table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);

/// Adding keys to table grade_grades_raw
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
$table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
Expand All @@ -327,19 +315,14 @@ function create_test_tables() {

if ($result && !table_exists($table)) {

/// Adding fields to table scale
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('scale', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');

/// Adding keys to table scale
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));

/// Adding indexes to table scale
$table->addIndexInfo('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid'));

/// Launch create table for scale
Expand Down Expand Up @@ -643,7 +626,7 @@ function load_grade_items() {
$grade_item->itemname = 'unittestorphangradeitem1';
$grade_item->itemtype = 'mod';
$grade_item->itemmodule = 'quiz';
$grade_item->iteminstance = 1;
$grade_item->iteminstance = 5;
$grade_item->gradetype = GRADE_TYPE_VALUE;
$grade_item->grademin = 10;
$grade_item->grademax = 120;
Expand All @@ -665,7 +648,7 @@ function load_grade_items() {
$grade_item->itemname = 'singleparentitem1';
$grade_item->itemtype = 'mod';
$grade_item->itemmodule = 'forum';
$grade_item->iteminstance = 3;
$grade_item->iteminstance = 7;
$grade_item->gradetype = GRADE_TYPE_SCALE;
$grade_item->scaleid = $this->scale[0]->id;
$grade_item->grademin = 0;
Expand All @@ -687,7 +670,7 @@ function load_grade_items() {
$grade_item->itemname = 'singleparentitem2';
$grade_item->itemtype = 'mod';
$grade_item->itemmodule = 'forum';
$grade_item->iteminstance = 3;
$grade_item->iteminstance = 9;
$grade_item->gradetype = GRADE_TYPE_VALUE;
$grade_item->grademin = 0;
$grade_item->grademax = 100;
Expand Down

0 comments on commit 1c307f2

Please sign in to comment.