3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once('grade_object.php');
28 class grade_grade
extends grade_object
{
34 var $table = 'grade_grades';
37 * Array of class variables that are not part of the DB table fields
38 * @var array $nonfields
40 var $nonfields = array('table', 'nonfields', 'required_fields', 'grade_grade_text', 'grade_item');
43 * The id of the grade_item this grade belongs to.
49 * The grade_item object referenced by $this->itemid.
50 * @var object $grade_item
55 * The id of the user this grade belongs to.
61 * The grade value of this raw grade, if such was provided by the module.
62 * @var float $rawgrade
67 * The maximum allowable grade when this grade was created.
68 * @var float $rawgrademax
70 var $rawgrademax = 100;
73 * The minimum allowable grade when this grade was created.
74 * @var float $rawgrademin
79 * id of the scale, if this grade is based on a scale.
80 * @var int $rawscaleid
85 * The userid of the person who last modified this grade.
86 * @var int $usermodified
91 * Additional textual information about this grade. It can be automatically generated
92 * from the module or entered manually by the teacher. This is kept in its own table
93 * for efficiency reasons, so it is encapsulated in its own object, and included in this grade object.
94 * @var object $grade_grade_text
96 var $grade_grade_text;
99 * The final value of this grade.
100 * @var float $finalgrade
105 * 0 if visible, 1 always hidden or date not visible until
111 * 0 not locked, date when the item was locked
117 * 0 no automatic locking, date when to lock the grade automatically
118 * @var float $locktime
124 * @var boolean $exported
130 * @var boolean $overridden
135 * Grade excluded from aggregation functions
136 * @var boolean $excluded
141 * Loads the grade_grade_text object linked to this grade (through the intersection of itemid and userid), and
142 * saves it as a class variable for this final object.
145 function load_text() {
146 if (empty($this->id
)) {
147 return false; // text can not be attached to non existing grade
150 if (empty($this->grade_grade_text
->id
)) {
151 $this->grade_grade_text
= grade_grade_text
::fetch(array('gradeid'=>$this->id
));
154 return $this->grade_grade_text
;
158 * Loads the grade_item object referenced by $this->itemid and saves it as $this->grade_item for easy access.
159 * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
160 * @return object grade_item.
162 function load_grade_item($grade_item=null) {
163 if (!empty($grade_item) && get_class($grade_item) == 'grade_item') {
164 $this->grade_item
= $grade_item;
165 } elseif (empty($this->grade_item
) && !empty($this->itemid
)) {
166 $this->grade_item
= grade_item
::fetch(array('id'=>$this->itemid
));
168 return $this->grade_item
;
172 * Is grading object editable?
173 * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
176 function is_editable($grade_item=null) {
177 if ($this->is_locked($grade_item)) {
181 $grade_item = $this->load_grade_item($grade_item);
183 if ($grade_item->gradetype
== GRADE_TYPE_NONE
) {
191 * Check grade lock status. Uses both grade item lock and grade lock.
192 * Internally any date in locked field (including future ones) means locked,
193 * the date is stored for logging purposes only.
195 * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
196 * @return boolean true if locked, false if not
198 function is_locked($grade_item=null) {
199 $this->load_grade_item($grade_item);
201 return !empty($this->locked
) or $this->grade_item
->is_locked();
205 * Checks if grade overridden
208 function is_overridden() {
209 return !empty($this->overridden
);
213 * Set the overridden status of grade
214 * @param boolean $state requested overridden state
215 * @return boolean true is db state changed
217 function set_overridden($state) {
218 if (empty($this->overridden
) and $state) {
219 $this->overridden
= time();
223 } else if (!empty($this->overridden
) and !$state) {
224 $this->overridden
= 0;
232 * Checks if grade excluded from aggregation functions
235 function is_excluded() {
236 return !empty($this->excluded
);
240 * Set the excluded status of grade
241 * @param boolean $state requested excluded state
242 * @return boolean true is db state changed
244 function set_excluded($state) {
245 if (empty($this->excluded
) and $state) {
246 $this->excluded
= time();
250 } else if (!empty($this->excluded
) and !$state) {
259 * Lock/unlock this grade.
261 * @param boolean $lockstate true means lock, false unlock grade
262 * @return boolean true if sucessful, false if can not set new lock state for grade
264 function set_locked($lockedstate) {
265 $this->load_grade_item();
268 if (!empty($this->locked
)) {
269 return true; // already locked
272 if ($this->grade_item
->needsupdate
) {
273 //can not lock grade if final not calculated!
277 $this->locked
= time();
283 if (empty($this->locked
)) {
284 return true; // not locked
287 if ($this->grade_item
->is_locked()) {
291 // remove the locked flag
302 * Set the locktime for this grade.
304 * @param int $locktime timestamp for lock to activate
305 * @return boolean true if sucessful, false if can not set new lock state for grade
307 function set_locktime($locktime) {
310 // if current locktime is before, no need to reset
312 if ($this->locktime
&& $this->locktime
<= $locktime) {
317 if ($this->grade_item->needsupdate) {
318 //can not lock grade if final not calculated!
323 $this->locktime
= $locktime;
330 // remove the locktime timestamp
340 * Check grade lock status. Uses both grade item lock and grade lock.
341 * Internally any date in hidden field (including future ones) means hidden,
342 * the date is stored for logging purposes only.
344 * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
345 * @return boolean true if hidden, false if not
347 function is_hidden($grade_item=null) {
348 $this->load_grade_item($grade_item);
350 return $this->hidden
== 1 or $this->hidden
> time() or $this->grade_item
->is_hidden();
354 * Set the hidden status of grade, 0 mean visible, 1 always hidden, number means date to hide until.
355 * @param int $hidden new hidden status
357 function set_hidden($hidden) {
358 $this->hidden
= $hidden;
363 * Finds and returns a grade_grade instance based on params.
366 * @param array $params associative arrays varname=>value
367 * @return object grade_grade instance or false if none found.
369 function fetch($params) {
370 return grade_object
::fetch_helper('grade_grades', 'grade_grade', $params);
374 * Finds and returns all grade_grade instances based on params.
377 * @param array $params associative arrays varname=>value
378 * @return array array of grade_grade insatnces or false if none found.
380 function fetch_all($params) {
381 return grade_object
::fetch_all_helper('grade_grades', 'grade_grade', $params);
386 * Delete grade together with feedback.
387 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
388 * @return boolean success
390 function delete($source=null) {
391 if ($text = $this->load_text()) {
392 $text->delete($source);
394 return parent
::delete($source);
398 * Updates this grade with the given textual information. This will create a new grade_grade_text entry
399 * if none was previously in DB for this raw grade, or will update the existing one.
400 * @param string $information Manual information from the teacher. Could be a code like 'mi'
401 * @param int $informationformat Text format for the information
402 * @return boolean Success or Failure
404 function update_information($information, $informationformat) {
407 if (empty($this->grade_grade_text
->id
)) {
408 $this->grade_grade_text
= new grade_grade_text();
410 $this->grade_grade_text
->gradeid
= $this->id
;
411 $this->grade_grade_text
->userid
= $this->userid
;
412 $this->grade_grade_text
->information
= $information;
413 $this->grade_grade_text
->informationformat
= $informationformat;
415 return $this->grade_grade_text
->insert();
418 if ($this->grade_grade_text
->information
!= $information
419 or $this->grade_grade_text
->informationformat
!= $informationformat) {
421 $this->grade_grade_text
->information
= $information;
422 $this->grade_grade_text
->informationformat
= $informationformat;
423 return $this->grade_grade_text
->update();
431 * Updates this grade with the given textual information. This will create a new grade_grade_text entry
432 * if none was previously in DB for this raw grade, or will update the existing one.
433 * @param string $feedback Manual feedback from the teacher. Could be a code like 'mi'
434 * @param int $feedbackformat Text format for the feedback
435 * @return boolean Success or Failure
437 function update_feedback($feedback, $feedbackformat, $usermodified=null) {
442 if (empty($usermodified)) {
443 $usermodified = $USER->id
;
446 if (empty($this->grade_grade_text
->id
)) {
447 $this->grade_grade_text
= new grade_grade_text();
449 $this->grade_grade_text
->gradeid
= $this->id
;
450 $this->grade_grade_text
->feedback
= $feedback;
451 $this->grade_grade_text
->feedbackformat
= $feedbackformat;
452 $this->grade_grade_text
->usermodified
= $usermodified;
454 return $this->grade_grade_text
->insert();
457 if ($this->grade_grade_text
->feedback
!= $feedback
458 or $this->grade_grade_text
->feedbackformat
!= $feedbackformat) {
460 $this->grade_grade_text
->feedback
= $feedback;
461 $this->grade_grade_text
->feedbackformat
= $feedbackformat;
462 $this->grade_grade_text
->usermodified
= $usermodified;
464 return $this->grade_grade_text
->update();
472 * Given a float value situated between a source minimum and a source maximum, converts it to the
473 * corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
474 * for the formula :-)
477 * @param float $rawgrade
478 * @param float $source_min
479 * @param float $source_max
480 * @param float $target_min
481 * @param float $target_max
482 * @return float Converted value
484 function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) {
485 if (is_null($rawgrade)) {
489 $factor = ($rawgrade - $source_min) / ($source_max - $source_min);
490 $diff = $target_max - $target_min;
491 $standardised_value = $factor * $diff +
$target_min;
492 return $standardised_value;
496 * Returns the grade letter this grade falls under, as they are set up in the given array.
497 * @param array $letters An array of grade boundaries with associated letters
498 * @param float $gradevalue The value to convert. If not given, will use instantiated object
499 * @param float $grademin If not given, will look up the grade_item's grademin
500 * @param float $grademax If not given, will look up the grade_item's grademax
501 * @return string Grade letter
503 function get_letter($letters, $gradevalue=null, $grademin=null, $grademax=null) {
504 if (is_null($grademin) ||
is_null($grademax)) {
506 debugging("Tried to call grade_grade::get_letter statically without giving an explicit grademin or grademax!");
509 $this->load_grade_item();
510 $grademin = $this->grade_item
->grademin
;
511 $grademax = $this->grade_item
->grademax
;
514 if (is_null($gradevalue)) {
516 debugging("Tried to call grade_grade::get_letter statically without giving an explicit gradevalue!");
519 $gradevalue = $this->finalgrade
;
521 // Standardise grade first
522 $grade = grade_grade
::standardise_score($gradevalue, $grademin, $grademax, 0, 100);
524 // Sort the letters by descending boundaries (100-0)
526 foreach ($letters as $boundary => $letter) {
527 if ($grade >= $boundary) {