Skip to content

Commit

Permalink
MDL-9137 Applied parts of Petr's patch on the gradebook objects. Unit…
Browse files Browse the repository at this point in the history
… tests unaffected.
  • Loading branch information
nicolasconnault committed May 28, 2007
1 parent 526e1a8 commit d990776
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
78 changes: 48 additions & 30 deletions lib/grade/grade_grades_raw.php
Expand Up @@ -37,7 +37,7 @@ class grade_grades_raw extends grade_object {
* Array of class variables that are not part of the DB table fields
* @var array $nonfields
*/
var $nonfields = array('table', 'nonfields', 'scale');
var $nonfields = array('table', 'nonfields', 'scale', 'grade_grades_text', 'grade_item', 'feedback', 'feedbackformat');

/**
* The id of the grade_item this raw grade belongs to.
Expand Down Expand Up @@ -168,8 +168,6 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
return $this;
} else {
$object = new grade_grades_raw($object);

$object->load_text();
return $object;
}
} else {
Expand All @@ -180,32 +178,42 @@ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='',
/**
* 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 string $information Further info like forum rating distribution 4/5/7/0/1; null means keep as is
* @param int $informationformat Text format for information
* @param string $feedback Manual feedback from the teacher. Could be a code like 'mi'.
* @param string $feedback Manual feedback from the teacher. Could be a code like 'mi'; null means keep as is
* @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();
function annotate($information=NULL, $informationformat=FORMAT_PLAIN, $feedback=NULL, $feedbackformat=FORMAT_PLAIN) {
if (is_null($information) && is_null($feedback)) {
// Nothing to do
return true;
}

$grade_text->itemid = $this->itemid;
$grade_text->userid = $this->userid;
$grade_text->information = $information;
$grade_text->informationformat = $informationformat;
$grade_text->feedback = $feedback;
$grade_text->feedbackformat = $feedbackformat;
if (!$grade_text = $this->load_text()) {
$grade_text = new grade_grades_text();

$result = true;
$grade_text->itemid = $this->itemid;
$grade_text->userid = $this->userid;
$grade_text->information = $information;
$grade_text->informationformat = $informationformat;
$grade_text->feedback = $feedback;
$grade_text->feedbackformat = $feedbackformat;

if (empty($this->text)) {
$result = $grade_text->insert();

} else {
if (!is_null($informationformat)) {
$grade_text->information = $information;
$grade_text->informationformat = $feedbackformat;
}
if (!is_null($feedback)) {
$grade_text->feedback = $feedback;
$grade_text->feedbackformat = $feedbackformat;
}
$result = $grade_text->update();
}

$this->text = $grade_text;

return $result;
}

Expand All @@ -214,42 +222,47 @@ function annotate($information, $informationformat=FORMAT_PLAIN, $feedback=NULL,
* its pre-update value and its new value in the grade_history table. The grade_item
* object is also flagged for update.
*
* @param float $newgrade The new gradevalue of this object
* @param float $newgrade The new gradevalue of this object, false if no change
* @param string $howmodified What caused the modification? manual/module/import/cron...
* @param string $note A note attached to this modification.
* @return boolean Success or Failure.
*/
function update($newgrade, $howmodified='manual', $note=NULL) {
global $USER;
$oldgrade = $this->gradevalue;
$this->gradevalue = $newgrade;

if (!empty($this->scale->id)) {
$this->scaleid = $this->scale->id;
$this->grademin = 0;
$this->scale->load_items();
$this->grademax = count($this->scale->scale_items);
}

$trackhistory = false;

if ($newgrade != $this->gradevalue) {
$trackhistory = true;
$oldgrade = $this->gradevalue;
$this->gradevalue = $newgrade;
}

$result = parent::update();

// Update grade_grades_text if changed
if (!empty($this->text)) {
$grade_text = grade_grades_text::fetch('userid', $this->userid, 'itemid', $this->itemid);
if ($this->text != $grade_text && $this->text->id == $grade_text->id) {
$result = $result & $this->text->update();
}
if ($result && !empty($this->feedback)) {
$result = $this->annotate(NULL, NULL, $this->feedback, $this->feedbackformat);
}

if ($result) {
if ($result && $trackhistory) {
// TODO Handle history recording error, such as displaying a notice, but still return true
grade_history::insert_change($this, $oldgrade, $howmodified, $note);

// Notify parent grade_item of need to update
$this->load_grade_item();
$result = $result && $this->grade_item->flag_for_update();

return $result;
$result = $this->grade_item->flag_for_update();
}

if ($result) {
return true;
} else {
debugging("Could not update a raw grade in the database.");
return false;
Expand All @@ -275,8 +288,13 @@ function insert() {
// Notify parent grade_item of need to update
$this->load_grade_item();
$result = $result && $this->grade_item->flag_for_update();

// Update grade_grades_text if specified
if ($result && !empty($this->feedback)) {
$result = $this->annotate(NULL, NULL, $this->feedback, $this->feedbackformat);
}

return $result;
return $result && $this->grade_item->flag_for_update();
}

/**
Expand Down
7 changes: 4 additions & 3 deletions lib/grade/grade_object.php
Expand Up @@ -90,7 +90,7 @@ function update() {
$this->usermodified = $USER->id;
}

return update_record($this->table, $this);
return update_record($this->table, addslashes_recursive($this));
}

/**
Expand All @@ -108,7 +108,8 @@ function insert() {
global $USER;

if (!empty($this->id)) { // Object already exists, so let's do an update instead
$this->update();
debugging("Grade object already exists!");
return false;
}

$this->timecreated = $this->timemodified = time();
Expand All @@ -126,7 +127,7 @@ function insert() {
}
}

return $this->id = insert_record($this->table, $clonethis, true);
return $this->id = insert_record($this->table, addslashes_recursive($clonethis), true);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions lib/gradelib.php
Expand Up @@ -92,7 +92,12 @@ function grade_get_items($courseid, $itemname=NULL, $itemtype=NULL, $itemmodule=
*/
function grade_create_item($params) {
$grade_item = new grade_item($params);
return $grade_item->insert();

if (empty($grade_item->id)) {
return $grade_item->insert();
} else {
return $grade_item->id;
}
}

/**
Expand All @@ -107,7 +112,12 @@ function grade_create_item($params) {
*/
function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN) {
$grade_category = new grade_category(compact('courseid', 'fullname', 'items', 'aggregation'));
return $grade_category->insert();

if (empty($grade_category->id)) {
return $grade_category->insert();
} else {
return $grade_category->id;
}
}


Expand Down
1 change: 0 additions & 1 deletion lib/simpletest/grade/simpletest/testgraderaw.php
Expand Up @@ -96,7 +96,6 @@ function test_grade_grades_raw_update() {
// Check the grade_item's needsupdate variable first
$grade_grades_raw->load_grade_item();
$this->assertFalse($grade_grades_raw->grade_item->needsupdate);

$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);
Expand Down

0 comments on commit d990776

Please sign in to comment.