From 46566dd8c6ae26de008137169a7542c094d4bfd2 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Fri, 4 May 2007 07:40:51 +0000 Subject: [PATCH] MDL-9506 Moved unit tests in a simpletest subfolder so they can be automatically grouped. Added grade_history and grade_text object and another heap of small implementations. --- lib/grade/grade_grades_raw.php | 80 +++++++-- lib/grade/grade_grades_text.php | 111 ++++++++++++ lib/grade/grade_history.php | 134 ++++++++++++++ lib/grade/grade_object.php | 13 +- lib/grade/grade_outcome.php | 32 +++- lib/grade/grade_scale.php | 28 +++ lib/gradelib.php | 2 + .../grade/simpletest/testgradecalculation.php | 95 ++++++++++ .../grade/simpletest/testgradefinal.php | 97 ++++++++++ .../grade/simpletest/testgradehistory.php | 102 +++++++++++ .../grade/simpletest/testgradeoutcome.php | 94 ++++++++++ .../grade/simpletest/testgraderaw.php | 68 ++++++- .../grade/simpletest/testgradescale.php | 50 +++++- .../grade/simpletest/testgradetext.php | 106 +++++++++++ lib/simpletest/testgradelib.php | 166 ++++++++++++++---- 15 files changed, 1121 insertions(+), 57 deletions(-) create mode 100644 lib/grade/grade_grades_text.php create mode 100644 lib/grade/grade_history.php create mode 100644 lib/simpletest/grade/simpletest/testgradecalculation.php create mode 100644 lib/simpletest/grade/simpletest/testgradefinal.php create mode 100644 lib/simpletest/grade/simpletest/testgradehistory.php create mode 100644 lib/simpletest/grade/simpletest/testgradeoutcome.php create mode 100755 lib/simpletest/grade/simpletest/testgradetext.php diff --git a/lib/grade/grade_grades_raw.php b/lib/grade/grade_grades_raw.php index f79a07b46f..64d5e20a28 100644 --- a/lib/grade/grade_grades_raw.php +++ b/lib/grade/grade_grades_raw.php @@ -93,6 +93,14 @@ class grade_grades_raw extends grade_object { */ var $usermodified; + /** + * Additional textual information about this grade. It can be automatically generated + * from the module or entered manually by the teacher. This is kept in its own table + * for efficiency reasons, so it is encapsulated in its own object, and included in this raw grade object. + * @var object $text + */ + var $text; + /** * Constructor. Extends the basic functionality defined in grade_object. * @param array $params Can also be a standard object. @@ -104,8 +112,20 @@ class grade_grades_raw extends grade_object { $this->scale = new grade_scale(array('id' => $this->scaleid)); $this->scale->load_items(); } - } + // Load text object + $this->load_text(); + } + + /** + * Loads the grade_grades_text object linked to this raw grade, into the $this->text variable, if + * such record exists. Otherwise returns null. + */ + function load_text() { + if (!empty($this->id)) { + $this->text = grade_grades_text::fetch('gradesid', $this->id); + } + } /** * Finds and returns a grade_grades_raw object based on 1-3 field values. @@ -125,15 +145,50 @@ class grade_grades_raw extends grade_object { foreach ($object as $param => $value) { $this->$param = $value; } + + $this->load_text(); return $this; } else { $object = new grade_grades_raw($object); + + $object->load_text(); return $object; } } else { return false; } } + + /** + * Updates this grade with the given textual information. This will create a new grade_grades_text entry + * if none was previously in DB for this raw grade, or will update the existing one. + * @param string $information Further info like forum rating distribution 4/5/7/0/1 + * @param int $informationformat Text format for information + * @param string $feedback Manual feedback from the teacher. Could be a code like 'mi'. + * @param int $feedbackformat Text format for the feedback + * @return boolean Success or Failure + */ + function annotate($information, $informationformat=FORMAT_PLAIN, $feedback=NULL, $feedbackformat=FORMAT_PLAIN) { + $grade_text = new grade_grades_text(); + + $grade_text->gradesid = $this->id; + $grade_text->information = $information; + $grade_text->informationformat = $informationformat; + $grade_text->feedback = $feedback; + $grade_text->feedbackformat = $feedbackformat; + + $result = true; + + if (empty($this->text)) { + $result = $grade_text->insert(); + } else { + $result = $grade_text->update(); + } + + $this->text = $grade_text; + + return $result; + } /** * In addition to the normal updating set up in grade_object, this object also records @@ -155,19 +210,18 @@ class grade_grades_raw extends grade_object { } $result = parent::update(); - + + // Update grade_grades_text if changed + if (!empty($this->text)) { + $grade_text = grade_grades_text::fetch('gradesid', $this->id); + if ($this->text != $grade_text && $this->text->id == $grade_text->id) { + $result = $result & $this->text->update(); + } + } + if ($result) { - $logentry = new stdClass(); - $logentry->itemid = $this->itemid; - $logentry->userid = $this->userid; - $logentry->oldgrade = $oldgrade; - $logentry->newgrade = $this->gradevalue; - $logentry->note = $note; - $logentry->howmodified = $howmodified; - $logentry->timemodified = mktime(); - $logentry->usermodified = $USER->id; - - insert_record('grade_history', $logentry); + // TODO Handle history recording error, such as displaying a notice, but still return true + grade_history::insert_change($this, $oldgrade, $howmodified, $note); return true; } else { return false; diff --git a/lib/grade/grade_grades_text.php b/lib/grade/grade_grades_text.php new file mode 100644 index 0000000000..f2e7a9d73d --- /dev/null +++ b/lib/grade/grade_grades_text.php @@ -0,0 +1,111 @@ + $value) { + $this->$param = $value; + } + return $this; + } else { + $grade_text = new grade_grades_text($grade_text); + return $grade_text; + } + } else { + return false; + } + } +} +?> diff --git a/lib/grade/grade_history.php b/lib/grade/grade_history.php new file mode 100644 index 0000000000..2ee1a36597 --- /dev/null +++ b/lib/grade/grade_history.php @@ -0,0 +1,134 @@ + $value) { + $this->$param = $value; + } + return $this; + } else { + $grade_history = new grade_history($grade_history); + return $grade_history; + } + } else { + return false; + } + } + + /** + * Given a grade_grades_raw object and some other parameters, records the + * change of grade value for this object, and associated data. + * @static + * @param object $grade_raw + * @param float $oldgrade + * @param string $note + * @param string $howmodified + * @return boolean Success or Failure + */ + function insert_change($grade_raw, $oldgrade, $howmodified='manual', $note=NULL) { + global $USER; + $history = new grade_history(); + $history->itemid = $grade_raw->itemid; + $history->userid = $grade_raw->userid; + $history->oldgrade = $oldgrade; + $history->newgrade = $grade_raw->gradevalue; + $history->note = $note; + $history->howmodified = $howmodified; + $history->timemodified = mktime(); + $history->usermodified = $USER->id; + + return $history->insert(); + } +} +?> diff --git a/lib/grade/grade_object.php b/lib/grade/grade_object.php index 02ed32cd8c..8ef6e192df 100644 --- a/lib/grade/grade_object.php +++ b/lib/grade/grade_object.php @@ -82,10 +82,14 @@ class grade_object { * @return boolean */ function update() { - $result = update_record($this->table, $this); - if ($result) { - $this->timemodified = mktime(); + $this->timemodified = mktime(); + + if (!empty($this->usermodified)) { + global $USER; + $this->usermodified = $USER->id; } + + $result = update_record($this->table, $this); return $result; } @@ -110,6 +114,9 @@ class grade_object { unset($clone->$var); } } + + global $USER; + $this->usermodified = $USER->id; $this->id = insert_record($this->table, $clone, true); return $this->id; diff --git a/lib/grade/grade_outcome.php b/lib/grade/grade_outcome.php index 9567c614bb..707351935f 100644 --- a/lib/grade/grade_outcome.php +++ b/lib/grade/grade_outcome.php @@ -83,12 +83,42 @@ class grade_outcome extends 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_raw($params=NULL, $fetch=true) { + function grade_outcome($params=NULL, $fetch=true) { $this->grade_object($params, $fetch); if (!empty($this->scaleid)) { $this->scale = new grade_scale(array('id' => $this->scaleid)); $this->scale->load_items(); } } + + /** + * Finds and returns a grade_outcome 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 + * @param string $value2 + * @param string $field3 + * @param string $value3 + * @param string $fields + * @return object grade_outcome object or false if none found. + */ + function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { + if ($grade_outcome = get_record('grade_outcomes', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { + if (isset($this) && get_class($this) == 'grade_outcome') { + print_object($this); + foreach ($grade_outcome as $param => $value) { + $this->$param = $value; + } + return $this; + } else { + $grade_outcome = new grade_outcome($grade_outcome); + return $grade_outcome; + } + } else { + return false; + } + } } ?> diff --git a/lib/grade/grade_scale.php b/lib/grade/grade_scale.php index f5f56b3dbc..38264bc492 100644 --- a/lib/grade/grade_scale.php +++ b/lib/grade/grade_scale.php @@ -72,6 +72,34 @@ class grade_scale extends grade_object { */ var $description; + /** + * Finds and returns a grade_scale object based on 1-3 field values. + * + * @param string $field1 + * @param string $value1 + * @param string $field2 + * @param string $value2 + * @param string $field3 + * @param string $value3 + * @param string $fields + * @return object grade_scale object or false if none found. + */ + function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { + if ($grade_scale = get_record('scale', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { + if (isset($this) && get_class($this) == 'grade_scale') { + foreach ($grade_scale as $param => $value) { + $this->$param = $value; + } + return $this; + } else { + $grade_scale = new grade_scale($grade_scale); + return $grade_scale; + } + } else { + return false; + } + } + /** * Loads the scale's items into the $scale_items array. * There are three ways to achieve this: diff --git a/lib/gradelib.php b/lib/gradelib.php index fb9a627c59..259da1153a 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -48,6 +48,8 @@ require_once($CFG->libdir . '/grade/grade_grades_raw.php'); require_once($CFG->libdir . '/grade/grade_grades_final.php'); require_once($CFG->libdir . '/grade/grade_scale.php'); require_once($CFG->libdir . '/grade/grade_outcome.php'); +require_once($CFG->libdir . '/grade/grade_history.php'); +require_once($CFG->libdir . '/grade/grade_grades_text.php'); /** * Extracts from the gradebook all the grade items attached to the calling object. diff --git a/lib/simpletest/grade/simpletest/testgradecalculation.php b/lib/simpletest/grade/simpletest/testgradecalculation.php new file mode 100644 index 0000000000..b92a0ad1f8 --- /dev/null +++ b/lib/simpletest/grade/simpletest/testgradecalculation.php @@ -0,0 +1,95 @@ +libdir . '/simpletest/testgradelib.php'); + +class grade_calculation_test extends gradelib_test { + + function test_grade_calculation_construct() { + $params = new stdClass(); + + $params->itemid = $this->grade_items[0]->id; + $params->calculation = 'MEAN(1, 2)'; + + $grade_calculation = new grade_calculation($params, false); + $this->assertEqual($params->itemid, $grade_calculation->itemid); + $this->assertEqual($params->calculation, $grade_calculation->calculation); + } + + function test_grade_calculation_insert() { + $grade_calculation = new grade_calculation(); + $this->assertTrue(method_exists($grade_calculation, 'insert')); + + $grade_calculation->itemid = $this->grade_items[0]->id; + $grade_calculation->calculation = 'MEAN(1, 2)'; + + $grade_calculation->insert(); + + $last_grade_calculation = end($this->grade_calculations); + + $this->assertEqual($grade_calculation->id, $last_grade_calculation->id + 1); + $this->assertFalse(empty($grade_calculation->timecreated)); + $this->assertFalse(empty($grade_calculation->timemodified)); + $this->grade_calculations[] = $grade_calculation; + + } + + function test_grade_calculation_update() { + $grade_calculation = new grade_calculation($this->grade_calculations[0]); + $this->assertTrue(method_exists($grade_calculation, 'update')); + $grade_calculation->calculation = 'MEAN(1, 2)'; + $this->assertTrue($grade_calculation->update()); + $calculation = get_field('grade_calculations', 'calculation', 'id', $this->grade_calculations[0]->id); + $this->assertEqual($grade_calculation->calculation, $calculation); + } + + function test_grade_calculation_delete() { + $grade_calculation = new grade_calculation($this->grade_calculations[0]); + $this->assertTrue(method_exists($grade_calculation, 'delete')); + + $this->assertTrue($grade_calculation->delete()); + $this->assertFalse(get_record('grade_calculations', 'id', $grade_calculation->id)); + } + + function test_grade_calculation_fetch() { + $grade_calculation = new grade_calculation(); + $this->assertTrue(method_exists($grade_calculation, 'fetch')); + + $grade_calculation = grade_calculation::fetch('id', $this->grade_calculations[0]->id); + $this->assertEqual($this->grade_calculations[0]->id, $grade_calculation->id); + $this->assertEqual($this->grade_calculations[0]->calculation, $grade_calculation->calculation); + } + +} +?> diff --git a/lib/simpletest/grade/simpletest/testgradefinal.php b/lib/simpletest/grade/simpletest/testgradefinal.php new file mode 100644 index 0000000000..e822bf6058 --- /dev/null +++ b/lib/simpletest/grade/simpletest/testgradefinal.php @@ -0,0 +1,97 @@ +libdir . '/simpletest/testgradelib.php'); + +class grade_final_test extends gradelib_test { + + function test_grade_grades_final_construct() { + $params = new stdClass(); + + $params->itemid = $this->grade_items[0]->id; + $params->userid = 1; + $params->gradevalue = 88; + + $grade_grades_final = new grade_grades_final($params, false); + $this->assertEqual($params->itemid, $grade_grades_final->itemid); + $this->assertEqual($params->gradevalue, $grade_grades_final->gradevalue); + } + + function test_grade_grades_final_insert() { + $grade_grades_final = new grade_grades_final(); + $this->assertTrue(method_exists($grade_grades_final, 'insert')); + + $grade_grades_final->itemid = $this->grade_items[0]->id; + $grade_grades_final->userid = 1; + $grade_grades_final->gradevalue = 88; + + $grade_grades_final->insert(); + + $last_grade_grades_final = end($this->grade_grades_final); + + $this->assertEqual($grade_grades_final->id, $last_grade_grades_final->id + 1); + $this->assertFalse(empty($grade_grades_final->timecreated)); + $this->assertFalse(empty($grade_grades_final->timemodified)); + $this->grade_grades_final[] = $grade_grades_final; + + } + + function test_grade_grades_final_update() { + $grade_grades_final = new grade_grades_final($this->grade_grades_final[0]); + $this->assertTrue(method_exists($grade_grades_final, 'update')); + $grade_grades_final->gradevalue = 89; + $this->assertTrue($grade_grades_final->update()); + $gradevalue = get_field('grade_grades_final', 'gradevalue', 'id', $this->grade_grades_final[0]->id); + $this->assertEqual($grade_grades_final->gradevalue, $gradevalue); + } + + function test_grade_grades_final_delete() { + $grade_grades_final = new grade_grades_final($this->grade_grades_final[0]); + $this->assertTrue(method_exists($grade_grades_final, 'delete')); + + $this->assertTrue($grade_grades_final->delete()); + $this->assertFalse(get_record('grade_grades_final', 'id', $grade_grades_final->id)); + } + + function test_grade_grades_final_fetch() { + $grade_grades_final = new grade_grades_final(); + $this->assertTrue(method_exists($grade_grades_final, 'fetch')); + + $grade_grades_final = grade_grades_final::fetch('id', $this->grade_grades_final[0]->id); + $this->assertEqual($this->grade_grades_final[0]->id, $grade_grades_final->id); + $this->assertEqual($this->grade_grades_final[0]->gradevalue, $grade_grades_final->gradevalue); + } + +} +?> diff --git a/lib/simpletest/grade/simpletest/testgradehistory.php b/lib/simpletest/grade/simpletest/testgradehistory.php new file mode 100644 index 0000000000..2943da14f6 --- /dev/null +++ b/lib/simpletest/grade/simpletest/testgradehistory.php @@ -0,0 +1,102 @@ +libdir . '/simpletest/testgradelib.php'); + +class grade_history_test extends gradelib_test { + + function test_grade_history_construct() { + $params = new stdClass(); + + $params->itemid = $this->grade_items[0]->id; + $params->userid = 1; + $params->oldgrade = 88; + $params->newgrade = 90; + $params->note = 'Modified manually in testgradehistory.php'; + $params->howmodified = 'manual'; + + $grade_history = new grade_history($params, false); + $this->assertEqual($params->itemid, $grade_history->itemid); + $this->assertEqual($params->note, $grade_history->note); + } + + function test_grade_history_insert() { + $grade_history = new grade_history(); + $this->assertTrue(method_exists($grade_history, 'insert')); + + $grade_history->itemid = $this->grade_items[0]->id; + $grade_history->userid = 1; + $grade_history->oldgrade = 88; + $grade_history->newgrade = 90; + $grade_history->note = 'Modified manually in testgradehistory.php'; + $grade_history->howmodified = 'manual'; + + $grade_history->insert(); + + $last_grade_history = end($this->grade_history); + + $this->assertEqual($grade_history->id, $last_grade_history->id + 1); + $this->assertFalse(empty($grade_history->timecreated)); + $this->assertFalse(empty($grade_history->timemodified)); + $this->grade_history[] = $grade_history; + + } + + function test_grade_history_update() { + $grade_history = new grade_history($this->grade_history[0]); + $this->assertTrue(method_exists($grade_history, 'update')); + $grade_history->note = 'Modified manually in testgradehistory.php'; + $this->assertTrue($grade_history->update()); + $note = get_field('grade_history', 'note', 'id', $this->grade_history[0]->id); + $this->assertEqual($grade_history->note, $note); + } + + function test_grade_history_delete() { + $grade_history = new grade_history($this->grade_history[0]); + $this->assertTrue(method_exists($grade_history, 'delete')); + + $this->assertTrue($grade_history->delete()); + $this->assertFalse(get_record('grade_history', 'id', $grade_history->id)); + } + + function test_grade_history_fetch() { + $grade_history = new grade_history(); + $this->assertTrue(method_exists($grade_history, 'fetch')); + + $grade_history = grade_history::fetch('id', $this->grade_history[0]->id); + $this->assertEqual($this->grade_history[0]->id, $grade_history->id); + $this->assertEqual($this->grade_history[0]->note, $grade_history->note); + } +} +?> diff --git a/lib/simpletest/grade/simpletest/testgradeoutcome.php b/lib/simpletest/grade/simpletest/testgradeoutcome.php new file mode 100644 index 0000000000..af75394a4b --- /dev/null +++ b/lib/simpletest/grade/simpletest/testgradeoutcome.php @@ -0,0 +1,94 @@ +libdir . '/simpletest/testgradelib.php'); + +class grade_outcome_test extends gradelib_test { + + function test_grade_outcome_construct() { + $params = new stdClass(); + + $params->courseid = $this->courseid; + $params->shortname = 'Team work'; + + $grade_outcome = new grade_outcome($params, false); + $this->assertEqual($params->courseid, $grade_outcome->courseid); + $this->assertEqual($params->shortname, $grade_outcome->shortname); + } + + function test_grade_outcome_insert() { + $grade_outcome = new grade_outcome(); + $this->assertTrue(method_exists($grade_outcome, 'insert')); + + $grade_outcome->courseid = $this->courseid; + $grade_outcome->shortname = 'Team work'; + + $grade_outcome->insert(); + + $last_grade_outcome = end($this->grade_outcomes); + + $this->assertEqual($grade_outcome->id, $last_grade_outcome->id + 1); + $this->assertFalse(empty($grade_outcome->timecreated)); + $this->assertFalse(empty($grade_outcome->timemodified)); + $this->grade_outcomes[] = $grade_outcome; + + } + + function test_grade_outcome_update() { + $grade_outcome = new grade_outcome($this->grade_outcomes[0]); + $this->assertTrue(method_exists($grade_outcome, 'update')); + $grade_outcome->shortname = 'Team work'; + $this->assertTrue($grade_outcome->update()); + $shortname = get_field('grade_outcomes', 'shortname', 'id', $this->grade_outcomes[0]->id); + $this->assertEqual($grade_outcome->shortname, $shortname); + } + + function test_grade_outcome_delete() { + $grade_outcome = new grade_outcome($this->grade_outcomes[0]); + $this->assertTrue(method_exists($grade_outcome, 'delete')); + + $this->assertTrue($grade_outcome->delete()); + $this->assertFalse(get_record('grade_outcomes', 'id', $grade_outcome->id)); + } + + function test_grade_outcome_fetch() { + $grade_outcome = new grade_outcome(); + $this->assertTrue(method_exists($grade_outcome, 'fetch')); + + $grade_outcome = grade_outcome::fetch('id', $this->grade_outcomes[0]->id); + $this->assertEqual($this->grade_outcomes[0]->id, $grade_outcome->id); + $this->assertEqual($this->grade_outcomes[0]->shortname, $grade_outcome->shortname); + } +} +?> diff --git a/lib/simpletest/grade/simpletest/testgraderaw.php b/lib/simpletest/grade/simpletest/testgraderaw.php index e1e828ba8d..d67490605a 100755 --- a/lib/simpletest/grade/simpletest/testgraderaw.php +++ b/lib/simpletest/grade/simpletest/testgraderaw.php @@ -35,11 +35,68 @@ global $CFG; require_once($CFG->libdir . '/simpletest/testgradelib.php'); class grade_raw_test extends gradelib_test { - - function test_grade_raw_construct() { + + function test_grade_grades_raw_construct() { + $params = new stdClass(); + + $params->itemid = $this->grade_items[0]->id; + $params->userid = 1; + $params->gradevalue = 88; + $params->grademax = 110; + $params->grademin = 18; + + $grade_grades_raw = new grade_grades_raw($params, false); + $this->assertEqual($params->itemid, $grade_grades_raw->itemid); + $this->assertEqual($params->gradevalue, $grade_grades_raw->gradevalue); + } + + function test_grade_grades_raw_insert() { + $grade_grades_raw = new grade_grades_raw(); + $this->assertTrue(method_exists($grade_grades_raw, 'insert')); + $grade_grades_raw->itemid = $this->grade_items[0]->id; + $grade_grades_raw->userid = 1; + $grade_grades_raw->gradevalue = 88; + $grade_grades_raw->grademax = 110; + $grade_grades_raw->grademin = 18; + + $grade_grades_raw->insert(); + + $last_grade_grades_raw = end($this->grade_grades_raw); + + $this->assertEqual($grade_grades_raw->id, $last_grade_grades_raw->id + 1); + $this->assertFalse(empty($grade_grades_raw->timecreated)); + $this->assertFalse(empty($grade_grades_raw->timemodified)); + $this->grade_grades_raw[] = $grade_grades_raw; + } + function test_grade_grades_raw_update() { + $grade_grades_raw = new grade_grades_raw($this->grade_grades_raw[0]); + $this->assertTrue(method_exists($grade_grades_raw, 'update')); + + $this->assertTrue($grade_grades_raw->update(89)); + $gradevalue = get_field('grade_grades_raw', 'gradevalue', 'id', $this->grade_grades_raw[0]->id); + $this->assertEqual($grade_grades_raw->gradevalue, $gradevalue); + } + + function test_grade_grades_raw_delete() { + $grade_grades_raw = new grade_grades_raw($this->grade_grades_raw[0]); + $this->assertTrue(method_exists($grade_grades_raw, 'delete')); + + $this->assertTrue($grade_grades_raw->delete()); + $this->assertFalse(get_record('grade_grades_raw', 'id', $grade_grades_raw->id)); + } + + function test_grade_grades_raw_fetch() { + $grade_grades_raw = new grade_grades_raw(); + $this->assertTrue(method_exists($grade_grades_raw, 'fetch')); + + $grade_grades_raw = grade_grades_raw::fetch('id', $this->grade_grades_raw[0]->id); + $this->assertEqual($this->grade_grades_raw[0]->id, $grade_grades_raw->id); + $this->assertEqual($this->grade_grades_raw[0]->gradevalue, $grade_grades_raw->gradevalue); + } + /** * Make sure that an update of a grade_raw object also updates the history table. */ @@ -60,6 +117,13 @@ class grade_raw_test extends gradelib_test { $this->assertEqual($howmodified, $history_log->howmodified); $this->assertEqual($note, $history_log->note); } + + function test_grade_raw_annotate() { + + } + function test_grade_raw_load_text() { + + } } ?> diff --git a/lib/simpletest/grade/simpletest/testgradescale.php b/lib/simpletest/grade/simpletest/testgradescale.php index e1f2ad06bc..e4e75a67ca 100755 --- a/lib/simpletest/grade/simpletest/testgradescale.php +++ b/lib/simpletest/grade/simpletest/testgradescale.php @@ -36,7 +36,7 @@ require_once($CFG->libdir . '/simpletest/testgradelib.php'); class grade_scale_test extends gradelib_test { - function test_scale_constructor() { + function test_scale_construct() { $params = new stdClass(); $params->name = 'unittestscale3'; @@ -53,7 +53,55 @@ class grade_scale_test extends gradelib_test { $this->assertEqual($params->description, $scale->description); } + + function test_grade_scale_insert() { + $grade_scale = new grade_scale(); + $this->assertTrue(method_exists($grade_scale, 'insert')); + + $grade_scale->name = 'unittestscale3'; + $grade_scale->courseid = $this->courseid; + $grade_scale->userid = $this->userid; + $grade_scale->scale = 'Distinction, Very Good, Good, Pass, Fail'; + $grade_scale->description = 'This scale is used to mark standard assignments.'; + + $grade_scale->insert(); + + $last_grade_scale = end($this->scale); + + $this->assertEqual($grade_scale->id, $last_grade_scale->id + 1); + $this->assertTrue(!empty($grade_scale->timecreated)); + $this->assertTrue(!empty($grade_scale->timemodified)); + $this->scale[] = $grade_scale; + + } + + function test_grade_scale_update() { + $grade_scale = new grade_scale($this->scale[0]); + $this->assertTrue(method_exists($grade_scale, 'update')); + + $grade_scale->name = 'Updated info for this unittest grade_scale'; + $this->assertTrue($grade_scale->update()); + $name = get_field('scale', 'name', 'id', $this->scale[0]->id); + $this->assertEqual($grade_scale->name, $name); + } + + function test_grade_scale_delete() { + $grade_scale = new grade_scale($this->scale[0]); + $this->assertTrue(method_exists($grade_scale, 'delete')); + + $this->assertTrue($grade_scale->delete()); + $this->assertFalse(get_record('scale', 'id', $grade_scale->id)); + } + + function test_grade_scale_fetch() { + $grade_scale = new grade_scale(); + $this->assertTrue(method_exists($grade_scale, 'fetch')); + $grade_scale = grade_scale::fetch('id', $this->scale[0]->id); + $this->assertEqual($this->scale[0]->id, $grade_scale->id); + $this->assertEqual($this->scale[0]->name, $grade_scale->name); + } + function test_scale_load_items() { $scale = new grade_scale($this->scale[0]); $this->assertTrue(method_exists($scale, 'load_items')); diff --git a/lib/simpletest/grade/simpletest/testgradetext.php b/lib/simpletest/grade/simpletest/testgradetext.php new file mode 100755 index 0000000000..fa29636e94 --- /dev/null +++ b/lib/simpletest/grade/simpletest/testgradetext.php @@ -0,0 +1,106 @@ +libdir . '/simpletest/testgradelib.php'); + +class grade_text_test extends gradelib_test { + + function test_grade_grades_text_construct() { + $params = new stdClass(); + + $params->gradesid = $this->grade_grades_raw[0]->id; + $params->information = 'Thumbs down'; + $params->informationformat = FORMAT_PLAIN; + $params->feedback = 'Good, but not good enough..'; + $params->feedbackformat = FORMAT_PLAIN; + + $grade_grades_text = new grade_grades_text($params, false); + $this->assertEqual($params->gradesid, $grade_grades_text->gradesid); + $this->assertEqual($params->information, $grade_grades_text->information); + $this->assertEqual($params->informationformat, $grade_grades_text->informationformat); + $this->assertEqual($params->feedback, $grade_grades_text->feedback); + $this->assertEqual($params->feedbackformat, $grade_grades_text->feedbackformat); + } + + function test_grade_grades_text_insert() { + $grade_grades_text = new grade_grades_text(); + $this->assertTrue(method_exists($grade_grades_text, 'insert')); + + $grade_grades_text->gradesid = $this->grade_grades_raw[0]->id; + $grade_grades_text->information = 'Thumbs down'; + $grade_grades_text->informationformat = FORMAT_PLAIN; + $grade_grades_text->feedback = 'Good, but not good enough..'; + $grade_grades_text->feedbackformat = FORMAT_PLAIN; + + $grade_grades_text->insert(); + + $last_grade_grades_text = end($this->grade_grades_text); + + global $USER; + + $this->assertEqual($grade_grades_text->id, $last_grade_grades_text->id + 1); + $this->assertFalse(empty($grade_grades_text->timecreated)); + $this->assertFalse(empty($grade_grades_text->timemodified)); + $this->assertEqual($USER->id, $grade_grades_text->usermodified); + $this->grade_grades_text[] = $grade_grades_text; + + } + + function test_grade_grades_text_update() { + $grade_grades_text = new grade_grades_text($this->grade_grades_text[0]); + $this->assertTrue(method_exists($grade_grades_text, 'update')); + + $this->assertTrue($grade_grades_text->update(89)); + $information = get_field('grade_grades_text', 'information', 'id', $this->grade_grades_text[0]->id); + $this->assertEqual($grade_grades_text->information, $information); + } + + function test_grade_grades_text_delete() { + $grade_grades_text = new grade_grades_text($this->grade_grades_text[0]); + $this->assertTrue(method_exists($grade_grades_text, 'delete')); + + $this->assertTrue($grade_grades_text->delete()); + $this->assertFalse(get_record('grade_grades_text', 'id', $grade_grades_text->id)); + } + + function test_grade_grades_text_fetch() { + $grade_grades_text = new grade_grades_text(); + $this->assertTrue(method_exists($grade_grades_text, 'fetch')); + + $grade_grades_text = grade_grades_text::fetch('id', $this->grade_grades_text[0]->id); + $this->assertEqual($this->grade_grades_text[0]->id, $grade_grades_text->id); + $this->assertEqual($this->grade_grades_text[0]->information, $grade_grades_text->information); + } +} +?> diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 1675ca2de8..139291c80e 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -66,12 +66,12 @@ class gradelib_test extends UnitTestCase { var $tables = array('grade_categories', 'grade_items', 'grade_calculations', + 'scale', 'grade_grades_raw', 'grade_grades_final', 'grade_grades_text', 'grade_outcomes', - 'grade_history', - 'scale'); + 'grade_history'); var $grade_items = array(); var $grade_categories = array(); @@ -266,12 +266,77 @@ class gradelib_test extends UnitTestCase { } } +/** + * Load scale data into the database, and adds the corresponding objects to this class' variable. + */ + function load_scale() { + $scale = new stdClass(); + + $scale->name = 'unittestscale1'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Way off topic, Not very helpful, Fairly neutral, Fairly helpful, Supportive, Some good information, Perfect answer!'; + $scale->description = 'This scale defines some of qualities that make posts helpful within the Moodle help forums.\n Your feedback will help others see how their posts are being received.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale = new stdClass(); + + $scale->name = 'unittestscale2'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Distinction, Very Good, Good, Pass, Fail'; + $scale->description = 'This scale is used to mark standard assignments.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale = new stdClass(); + + $scale->name = 'unittestscale3'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Loner, Contentious, Disinterested, Participative, Follower, Leader'; + $scale->description = 'Describes the level of teamwork of a student.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale->name = 'unittestscale4'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Does not understand theory, Understands theory but fails practice, Manages through, Excels'; + $scale->description = 'Level of expertise at a technical task, with a theoretical framework.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale->name = 'unittestscale5'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Insufficient, Acceptable, Excellent.'; + $scale->description = 'Description of skills.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + } + /** * Load grade_grades_raw data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_grades_raw() { // Grades for grade_item 1 - $grade_raw = new stdClass(); $grade_raw->itemid = $this->grade_items[0]->id; $grade_raw->userid = 1; @@ -345,7 +410,8 @@ class gradelib_test extends UnitTestCase { $grade_raw = new stdClass(); $grade_raw->itemid = $this->grade_items[2]->id; $grade_raw->userid = 1; - $grade_raw->gradevalue = 61; + $grade_raw->gradescale = 2; + $grade_raw->scaleid = $this->scale[3]->id; $grade_raw->timecreated = mktime(); $grade_raw->timemodified = mktime(); @@ -356,7 +422,8 @@ class gradelib_test extends UnitTestCase { $grade_raw = new stdClass(); $grade_raw->itemid = $this->grade_items[2]->id; $grade_raw->userid = 2; - $grade_raw->gradevalue = 81; + $grade_raw->gradescale = 3; + $grade_raw->scaleid = $this->scale[3]->id; $grade_raw->timecreated = mktime(); $grade_raw->timemodified = mktime(); @@ -367,7 +434,8 @@ class gradelib_test extends UnitTestCase { $grade_raw = new stdClass(); $grade_raw->itemid = $this->grade_items[2]->id; $grade_raw->userid = 3; - $grade_raw->gradevalue = 49; + $grade_raw->gradescale = 1; + $grade_raw->scaleid = $this->scale[3]->id; $grade_raw->timecreated = mktime(); $grade_raw->timemodified = mktime(); @@ -498,51 +566,75 @@ class gradelib_test extends UnitTestCase { * Load grade_grades_text data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_grades_text() { + $grade_grades_text = new stdClass(); + + $grade_grades_text->gradesid = $this->grade_grades_raw[0]->id; + $grade_grades_text->information = 'Thumbs down'; + $grade_grades_text->informationformat = FORMAT_PLAIN; + $grade_grades_text->feedback = 'Good, but not good enough..'; + $grade_grades_text->feedbackformat = FORMAT_PLAIN; + if ($grade_grades_text->id = insert_record('grade_grades_text', $grade_grades_text)) { + $this->grade_grades_text[] = $grade_grades_text; + } } /** * Load grade_outcome data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_outcomes() { - + // Calculation for grade_item 1 + $grade_outcome = new stdClass(); + $grade_outcome->itemid = $this->grade_items[0]->id; + $grade_outcome->shortname = 'Team work'; + $grade_outcome->timecreated = mktime(); + $grade_outcome->timemodified = mktime(); + $grade_outcome->scaleid = $this->scale[2]->id; + + if ($grade_outcome->id = insert_record('grade_outcomes', $grade_outcome)) { + $this->grade_outcomes[] = $grade_outcome; + } + + // Calculation for grade_item 2 + $grade_outcome = new stdClass(); + $grade_outcome->itemid = $this->grade_items[1]->id; + $grade_outcome->shortname = 'Complete circuit board'; + $grade_outcome->timecreated = mktime(); + $grade_outcome->timemodified = mktime(); + $grade_outcome->scaleid = $this->scale[3]->id; + + if ($grade_outcome->id = insert_record('grade_outcomes', $grade_outcome)) { + $this->grade_outcomes[] = $grade_outcome; + } + + // Calculation for grade_item 3 + $grade_outcome = new stdClass(); + $grade_outcome->itemid = $this->grade_items[2]->id; + $grade_outcome->shortname = 'Debug Java program'; + $grade_outcome->timecreated = mktime(); + $grade_outcome->timemodified = mktime(); + $grade_outcome->scaleid = $this->scale[4]->id; + + if ($grade_outcome->id = insert_record('grade_outcomes', $grade_outcome)) { + $this->grade_outcomes[] = $grade_outcome; + } } /** * Load grade_history data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_history() { - - } - - /** - * Load scale data into the database, and adds the corresponding objects to this class' variable. - */ - function load_scale() { - $scale = new stdClass(); + $grade_history = new stdClass(); - $scale->name = 'unittestscale1'; - $scale->courseid = $this->courseid; - $scale->userid = $this->userid; - $scale->scale = 'Way off topic, Not very helpful, Fairly neutral, Fairly helpful, Supportive, Some good information, Perfect answer!'; - $scale->description = 'This scale defines some of qualities that make posts helpful within the Moodle help forums.\n Your feedback will help others see how their posts are being received.'; - $scale->timemodified = mktime(); - - if ($scale->id = insert_record('scale', $scale)) { - $this->scale[] = $scale; - } - - $scale = new stdClass(); - - $scale->name = 'unittestscale2'; - $scale->courseid = $this->courseid; - $scale->userid = $this->userid; - $scale->scale = 'Distinction, Very Good, Good, Pass, Fail'; - $scale->description = 'This scale is used to mark standard assignments.'; - $scale->timemodified = mktime(); - - if ($scale->id = insert_record('scale', $scale)) { - $this->scale[] = $scale; + $grade_history->itemid = $this->grade_items[0]->id; + $grade_history->userid = 1; + $grade_history->oldgrade = 88; + $grade_history->newgrade = 90; + $grade_history->note = 'Modified manually in testgradehistory.php'; + $grade_history->howmodified = 'manual'; + + if ($grade_history->id = insert_record('grade_history', $grade_history)) { + $this->grade_history[] = $grade_history; } } /** -- 2.39.5