Skip to content

Commit

Permalink
Restoring some of the static calls which were converted unnecessarily
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasconnault committed Nov 13, 2007
1 parent 7506339 commit 9a68cff
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 70 deletions.
15 changes: 8 additions & 7 deletions lib/grade/grade_category.php
Expand Up @@ -157,8 +157,10 @@ function build_path($grade_category) {
if (empty($grade_category->parent)) {
return '/'.$grade_category->id.'/';
} else {
$parent = $this->lib_wrapper->get_record('grade_categories', 'id', $grade_category->parent);
return $this->build_path($parent).$grade_category->id.'/';
$obj = grade_object::get_instance('grade_category');
$parent = $obj->lib_wrapper->get_record('grade_categories', 'id', $grade_category->parent);
var_dump($parent);
return grade_category::build_path($parent).$grade_category->id.'/';
}
}

Expand Down Expand Up @@ -197,7 +199,7 @@ function update($source=null) {

// force recalculation of path;
if (empty($this->path)) {
$this->path = $this->build_path($this);
$this->path = grade_category::build_path($this);
$this->depth = substr_count($this->path, '/') - 1;
}

Expand Down Expand Up @@ -519,8 +521,7 @@ function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded)
unset($grade_values[$itemid]);
continue;
}
$grade_grade = grade_object::get_instance('grade_grade');
$grade_values[$itemid] = $grade_grade->standardise_score($v, $items[$itemid]->grademin, $items[$itemid]->grademax, 0, 1);
$grade_values[$itemid] = grade_grade::standardise_score($v, $items[$itemid]->grademin, $items[$itemid]->grademax, 0, 1);
}

// use min grade if grade missing for these types
Expand Down Expand Up @@ -557,8 +558,7 @@ function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded)
$grade->rawgrade = null; // categories do not use raw grades

// recalculate the rawgrade back to requested range
$grade_grade= grade_object::get_instance('grade_grade');
$finalgrade = $grade_grade->standardise_score($agg_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);
$finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);

if (!is_null($finalgrade)) {
$grade->finalgrade = bounded_number($this->grade_item->grademin, $finalgrade, $this->grade_item->grademax);
Expand Down Expand Up @@ -1080,6 +1080,7 @@ function set_locked($lockedstate, $cascade=false, $refresh=true) {
if ($cascade) {
//process all children - items and categories
$grade_item = grade_object::get_instance('grade_item');
var_dump($grade_item);
if ($children = $grade_item->fetch_all(array('categoryid'=>$this->id))) {
foreach($children as $child) {
$child->set_locked($lockedstate, true, false);
Expand Down
6 changes: 2 additions & 4 deletions lib/grade/grade_grade.php
Expand Up @@ -556,8 +556,6 @@ function standardise_score($rawgrade, $source_min, $source_max, $target_min, $ta
*/
function get_hiding_affected(&$grade_grades, &$grade_items) {
global $CFG;

$obj = grade_object::get_instance('grade_grade');

if (count($grade_grades) !== count($grade_items)) {
error('Incorrent size of arrays in params of grade_grade::get_hiding_affected()!');
Expand Down Expand Up @@ -632,7 +630,7 @@ function get_hiding_affected(&$grade_grades, &$grade_items) {
unset($values[$itemid]);
continue;
}
$values[$itemid] = $obj->standardise_score($value, $grade_items[$itemid]->grademin, $grade_items[$itemid]->grademax, 0, 1);
$values[$itemid] = grade_grade::standardise_score($value, $grade_items[$itemid]->grademin, $grade_items[$itemid]->grademax, 0, 1);
}

if ($grade_category->aggregateonlygraded) {
Expand Down Expand Up @@ -665,7 +663,7 @@ function get_hiding_affected(&$grade_grades, &$grade_items) {
$agg_grade = $grade_category->aggregate_values($values, $grade_items);

// recalculate the rawgrade back to requested range
$finalgrade = $obj->standardise_score($agg_grade, 0, 1, $grade_items[$do]->grademin, $grade_items[$do]->grademax);
$finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $grade_items[$do]->grademin, $grade_items[$do]->grademax);

if (!is_null($finalgrade)) {
$finalgrade = bounded_number($grade_items[$do]->grademin, $finalgrade, $grade_items[$do]->grademax);
Expand Down
6 changes: 2 additions & 4 deletions lib/grade/grade_item.php
Expand Up @@ -715,8 +715,7 @@ function adjust_raw_grade($rawgrade, $rawmin, $rawmax) {
// Standardise score to the new grade range
// NOTE: this is not compatible with current assignment grading
if ($rawmin != $this->grademin or $rawmax != $this->grademax) {
$grade_grade = grade_object::get_instance('grade_grade');
$rawgrade = $grade_grade->standardise_score($rawgrade, $rawmin, $rawmax, $this->grademin, $this->grademax);
$rawgrade = grade_grade::standardise_score($rawgrade, $rawmin, $rawmax, $this->grademin, $this->grademax);
}

// Apply other grade_item factors
Expand All @@ -741,8 +740,7 @@ function adjust_raw_grade($rawgrade, $rawmin, $rawmax) {
// Convert scale if needed
// NOTE: this is not compatible with current assignment grading
if ($rawmin != $this->grademin or $rawmax != $this->grademax) {
$grade_grade = grade_object::get_instance('grade_grade');
$rawgrade = $grade_grade->standardise_score($rawgrade, $rawmin, $rawmax, $this->grademin, $this->grademax);
$rawgrade = grade_grade::standardise_score($rawgrade, $rawmin, $rawmax, $this->grademin, $this->grademax);
}

return (int)bounded_number(0, round($rawgrade+0.00001), $this->grademax);
Expand Down
9 changes: 2 additions & 7 deletions lib/grade/grade_object.php
Expand Up @@ -376,13 +376,8 @@ function get_instance($class, $params=NULL, $fetch=true, $set=false, $object=nul
}

if (in_array($class, $classes)) {
// Only set the static instance if it isn't already a mock object
if (!isset($grade_instances[$class]) || get_class($grade_instances[$class]) != get_class($object)) {
$grade_instances[$class] =& $object;
return true;
} else {
return false;
}
$grade_instances[$class] =& $object;
return true;
} else {
debugging("grade_object::set_instance was given an object that is not supported ($class)!");
return false;
Expand Down
51 changes: 19 additions & 32 deletions lib/grade/simpletest/testgradecategory.php
Expand Up @@ -50,16 +50,17 @@ function test_grade_category_build_path() {
$this->assertTrue(method_exists($grade_category, 'build_path'));

// Mock get_record of parent category (2) then (1)
$grade_category->lib_wrapper->expectCallCount('get_record', 2);
$obj = grade_object::get_instance('grade_category');
$obj->lib_wrapper->expectCallCount('get_record', 2);
$parent2 = new stdClass();
$parent2->parent = 1;
$parent2->id = 2;
$grade_category->lib_wrapper->setReturnValueAt(0, 'get_record', $parent2);
$obj->lib_wrapper->setReturnValueAt(0, 'get_record', $parent2);
$parent1 = new stdClass();
$parent1->parent = null;
$parent1->id = 1;
$grade_category->lib_wrapper->setReturnValueAt(1, 'get_record', $parent1);

$obj->lib_wrapper->setReturnValueAt(1, 'get_record', $parent1);
var_dump($grade_category);
$path = $grade_category->build_path($grade_category);
$this->assertEqual($grade_category->path, $path);
}
Expand Down Expand Up @@ -203,23 +204,20 @@ function test_grade_category_qualifies_for_regrading() {

$obj = grade_object::get_instance('grade_category');
$obj->expectCallCount('fetch', 4, array(array('id' => $grade_category->id)));
$obj->setReturnValueAt(0, 'fetch', fullclone($grade_category));
$obj->setReturnValue('fetch', fullclone($grade_category));
$grade_item = new stdClass();
$grade_item->lib_wrapper = new mock_lib_wrapper();
$grade_item->aggregation = GRADE_AGGREGATE_MEAN;

$this->assertFalse($grade_category->qualifies_for_regrading());

$grade_category->aggregation = GRADE_AGGREGATE_MAX;
$obj->setReturnValueAt(1, 'fetch', fullclone($grade_category));
$this->assertTrue($grade_category->qualifies_for_regrading());

$grade_category->droplow = 99;
$obj->setReturnValueAt(2, 'fetch', fullclone($grade_category));
$this->assertTrue($grade_category->qualifies_for_regrading());

$grade_category->keephigh = 99;
$obj->setReturnValueAt(3, 'fetch', fullclone($grade_category));
$this->assertTrue($grade_category->qualifies_for_regrading());
}

Expand Down Expand Up @@ -490,21 +488,25 @@ function test_grade_category_set_parent() {
$grade_category_template->parent = 2;

// Test when parent category is not found in DB
$this->reset_mocks();
$grade_category = fullclone($grade_category_template);
$grade_category->expectOnce('is_course_category');
$grade_category->setReturnValue('is_course_category', false);
$grade_category->expectOnce('fetch', array(array('id' => 1, 'courseid' => $this->courseid)));
$grade_category->setReturnValue('fetch', false);
$obj = grade_object::get_instance('grade_category');
$obj->expectOnce('fetch', array(array('id' => 1, 'courseid' => $this->courseid)));
$obj->setReturnValue('fetch', false);
$grade_category->expectNever('update');
$grade_category->expectNever('force_regrading');
$this->assertFalse($grade_category->set_parent(1));

// Test when parent category is found in DB
$this->reset_mocks();
$grade_category = fullclone($grade_category_template);
$grade_category->expectOnce('is_course_category');
$grade_category->setReturnValue('is_course_category', false);
$grade_category->expectOnce('fetch', array(array('id' => 1, 'courseid' => $this->courseid)));
$grade_category->setReturnValue('fetch', $this->grade_categories[0]);
$obj = grade_object::get_instance('grade_category');
$obj->expectOnce('fetch', array(array('id' => 1, 'courseid' => $this->courseid)));
$obj->setReturnValue('fetch', $this->grade_categories[0]);
$grade_category->expectOnce('force_regrading', array());
$grade_category->expectOnce('update');
$grade_category->setReturnValue('update', true);
Expand All @@ -520,27 +522,11 @@ function test_grade_category_fetch_course_category() {

// Test method when course category already exists
$grade_category->expectNever('instantiate_new_grade_category');
$grade_category->expectOnce('fetch', array(array('courseid' => $this->courseid, 'parent' => null)));
$obj = grade_object::get_instance('grade_category');
$obj->setReturnValue('fetch', $this->grade_categories[0]);
$this->assertEqual($this->grade_categories[0], $grade_category->fetch_course_category($this->courseid));

// Test method when course category does not exists
$grade_category = new mock_grade_category_for_fetch_course_category($this);
$grade_category->lib_wrapper = new mock_lib_wrapper();
$grade_category->expectOnce('instantiate_new_grade_category');
$grade_category->expectOnce('fetch', array(array('courseid' => $this->courseid, 'parent' => null)));
$grade_category->setReturnValue('fetch', false);
$course_category = new mock_grade_category_for_fetch_course_category($this);
$course_category->lib_wrapper = new mock_lib_wrapper();
$course_category->expectOnce('insert_course_category', array($this->courseid));
$grade_category->setReturnValue('instantiate_new_grade_category', $course_category);

// Instantiate proper objects (current ones are mock objects)
$cat1 = new grade_category($course_category, false);
$cat2 = new grade_category($grade_category->fetch_course_category($this->courseid), false);
$this->assertEqual($cat1, $cat2);

// Test method when course category does not exists
}


Expand All @@ -554,7 +540,7 @@ function test_grade_category_set_locked() {
// Test non-cascading set_locked
Mock::generatePartial('grade_category', 'mock_grade_category_for_set_locked', $methods_to_mock);
$grade_item = new mock_grade_item();
$grade_item->expectOnce('set_locked', array($lockedstate, $cascade, true));
$grade_item->expectCallCount('set_locked', 2, array($lockedstate, $cascade, true));
$grade_item->setReturnValue('set_locked', true);
$grade_item->expectNever('fetch_all');
$grade_category = new mock_grade_category_for_set_locked($this);
Expand All @@ -568,8 +554,9 @@ function test_grade_category_set_locked() {
// Test cascading set_locked
$cascading = true;
$grade_category = new mock_grade_category_for_set_locked($this);
$grade_category->expectOnce('fetch_all');
$grade_category->setReturnValue('fetch_all', array(fullclone($grade_item), fullclone($grade_item)));
$obj = grade_object::get_instance('grade_item');
$obj->expectOnce('fetch_all');
$obj->setReturnValue('fetch_all', array(fullclone($grade_item), fullclone($grade_item)));
$grade_category->expectOnce('load_grade_item');
$grade_category->grade_item = $grade_item;

Expand Down
32 changes: 16 additions & 16 deletions lib/simpletest/fixtures/gradetest.php
Expand Up @@ -112,22 +112,7 @@ function setUp() {
$CFG->grade_aggregatesubcats = -1;
$CFG->disablegradehistory = false;
$this->real_db = fullclone($db);
// $this->reset_mocks();
$mock_gi = new mock_grade_item_partial($this);
$mock_gc = new mock_grade_category_partial($this);
$mock_gg = new mock_grade_grade_partial($this);
$mock_go = new mock_grade_outcome_partial($this);
$mock_gs = new mock_grade_scale_partial($this);
$mock_gi->lib_wrapper = new mock_lib_wrapper();
$mock_gc->lib_wrapper = new mock_lib_wrapper();
$mock_gg->lib_wrapper = new mock_lib_wrapper();
$mock_go->lib_wrapper = new mock_lib_wrapper();
$mock_gs->lib_wrapper = new mock_lib_wrapper();
grade_object::get_instance('grade_item', null, false, true, $mock_gi);
grade_object::get_instance('grade_category', null, false, true, $mock_gc);
grade_object::get_instance('grade_grade', null, false, true, $mock_gg);
grade_object::get_instance('grade_outcome', null, false, true, $mock_go);
grade_object::get_instance('grade_scale', null, false, true, $mock_gs);
$this->reset_mocks();
}

/**
Expand All @@ -151,6 +136,21 @@ function reset_mocks() {
$this->rs->EOF = false;
$db->setReturnReference('Execute', $this->rs);
$db->setReturnReference('SelectLimit', $this->rs);
$mock_gi = new mock_grade_item_partial($this);
$mock_gc = new mock_grade_category_partial($this);
$mock_gg = new mock_grade_grade_partial($this);
$mock_go = new mock_grade_outcome_partial($this);
$mock_gs = new mock_grade_scale_partial($this);
$mock_gi->lib_wrapper = new mock_lib_wrapper();
$mock_gc->lib_wrapper = new mock_lib_wrapper();
$mock_gg->lib_wrapper = new mock_lib_wrapper();
$mock_go->lib_wrapper = new mock_lib_wrapper();
$mock_gs->lib_wrapper = new mock_lib_wrapper();
grade_object::get_instance('grade_item', null, false, true, $mock_gi);
grade_object::get_instance('grade_category', null, false, true, $mock_gc);
grade_object::get_instance('grade_grade', null, false, true, $mock_gg);
grade_object::get_instance('grade_outcome', null, false, true, $mock_go);
grade_object::get_instance('grade_scale', null, false, true, $mock_gs);
}

/**
Expand Down

0 comments on commit 9a68cff

Please sign in to comment.