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 * @return object grade_item.
161 function load_grade_item() {
162 if (empty($this->itemid
)) {
163 debugging('Missing itemid');
164 $this->grade_item
= null;
168 if (empty($this->grade_item
)) {
169 $this->grade_item
= grade_item
::fetch(array('id'=>$this->itemid
));
171 } else if ($this->grade_item
->id
!= $this->itemid
) {
172 debugging('Itemid mismatch');
173 $this->grade_item
= grade_item
::fetch(array('id'=>$this->itemid
));
176 return $this->grade_item
;
180 * Is grading object editable?
183 function is_editable() {
184 if ($this->is_locked()) {
188 $grade_item = $this->load_grade_item();
190 if ($grade_item->gradetype
== GRADE_TYPE_NONE
) {
198 * Check grade lock status. Uses both grade item lock and grade lock.
199 * Internally any date in locked field (including future ones) means locked,
200 * the date is stored for logging purposes only.
202 * @return boolean true if locked, false if not
204 function is_locked() {
205 $this->load_grade_item();
207 return !empty($this->locked
) or $this->grade_item
->is_locked();
211 * Checks if grade overridden
214 function is_overridden() {
215 return !empty($this->overridden
);
219 * Set the overridden status of grade
220 * @param boolean $state requested overridden state
221 * @return boolean true is db state changed
223 function set_overridden($state) {
224 if (empty($this->overridden
) and $state) {
225 $this->overridden
= time();
229 } else if (!empty($this->overridden
) and !$state) {
230 $this->overridden
= 0;
238 * Checks if grade excluded from aggregation functions
241 function is_excluded() {
242 return !empty($this->excluded
);
246 * Set the excluded status of grade
247 * @param boolean $state requested excluded state
248 * @return boolean true is db state changed
250 function set_excluded($state) {
251 if (empty($this->excluded
) and $state) {
252 $this->excluded
= time();
256 } else if (!empty($this->excluded
) and !$state) {
265 * Lock/unlock this grade.
267 * @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
268 * @param boolean $cascade ignored param
269 * @param boolean $refresh refresh grades when unlocking
270 * @return boolean true if sucessful, false if can not set new lock state for grade
272 function set_locked($lockedstate, $cascade=false, $refresh=true) {
273 $this->load_grade_item();
276 if ($this->grade_item
->needsupdate
) {
277 //can not lock grade if final not calculated!
281 $this->locked
= time();
287 if (!empty($this->locked
) and $this->locktime
< time()) {
288 //we have to reset locktime or else it would lock up again
292 // remove the locked flag
297 //refresh when unlocking
298 $this->grade_item
->refresh_grades($this->userid
);
306 * Lock the grade if needed - make sure this is called only when final grades are valid
307 * @param array $items array of all grade item ids
310 function check_locktime_all($items) {
313 $items_sql = implode(',', $items);
315 $now = time(); // no rounding needed, this is not supposed to be called every 10 seconds
317 if ($rs = get_recordset_select('grade_grades', "itemid IN ($items_sql) AND locked = 0 AND locktime > 0 AND locktime < $now")) {
318 if ($rs->RecordCount() > 0) {
319 while ($grade = rs_fetch_next_record($rs)) {
320 $grade_grade = new grade_grade($grade, false);
321 $grade_grade->locked
= time();
322 $grade_grade->update('locktime');
330 * Set the locktime for this grade.
332 * @param int $locktime timestamp for lock to activate
335 function set_locktime($locktime) {
336 $this->locktime
= $locktime;
341 * Set the locktime for this grade.
343 * @return int $locktime timestamp for lock to activate
345 function get_locktime() {
346 $this->load_grade_item();
348 $item_locktime = $this->grade_item
->get_locktime();
350 if (empty($this->locktime
) or ($item_locktime and $item_locktime < $this->locktime
)) {
351 return $item_locktime;
354 return $this->locktime
;
359 * Check grade lock status. Uses both grade item lock and grade lock.
360 * Internally any date in hidden field (including future ones) means hidden,
361 * the date is stored for logging purposes only.
363 * @param object $grade_item An optional grade_item given to avoid having to reload one from the DB
364 * @return boolean true if hidden, false if not
366 function is_hidden($grade_item=null) {
367 $this->load_grade_item($grade_item);
369 return $this->hidden
== 1 or $this->hidden
> time() or $this->grade_item
->is_hidden();
373 * Set the hidden status of grade, 0 mean visible, 1 always hidden, number means date to hide until.
374 * @param int $hidden new hidden status
376 function set_hidden($hidden) {
377 $this->hidden
= $hidden;
382 * Finds and returns a grade_grade instance based on params.
385 * @param array $params associative arrays varname=>value
386 * @return object grade_grade instance or false if none found.
388 function fetch($params) {
389 return grade_object
::fetch_helper('grade_grades', 'grade_grade', $params);
393 * Finds and returns all grade_grade instances based on params.
396 * @param array $params associative arrays varname=>value
397 * @return array array of grade_grade insatnces or false if none found.
399 function fetch_all($params) {
400 return grade_object
::fetch_all_helper('grade_grades', 'grade_grade', $params);
405 * Delete grade together with feedback.
406 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
407 * @return boolean success
409 function delete($source=null) {
410 if ($text = $this->load_text()) {
411 $text->delete($source);
413 return parent
::delete($source);
417 * Updates this grade with the given textual information. This will create a new grade_grade_text entry
418 * if none was previously in DB for this raw grade, or will update the existing one.
419 * @param string $information Manual information from the teacher. Could be a code like 'mi'
420 * @param int $informationformat Text format for the information
421 * @return boolean Success or Failure
423 function update_information($information, $informationformat) {
426 if (empty($this->grade_grade_text
->id
)) {
427 $this->grade_grade_text
= new grade_grade_text();
429 $this->grade_grade_text
->gradeid
= $this->id
;
430 $this->grade_grade_text
->userid
= $this->userid
;
431 $this->grade_grade_text
->information
= $information;
432 $this->grade_grade_text
->informationformat
= $informationformat;
434 return $this->grade_grade_text
->insert();
437 if ($this->grade_grade_text
->information
!= $information
438 or $this->grade_grade_text
->informationformat
!= $informationformat) {
440 $this->grade_grade_text
->information
= $information;
441 $this->grade_grade_text
->informationformat
= $informationformat;
442 return $this->grade_grade_text
->update();
450 * Updates this grade with the given textual information. This will create a new grade_grade_text entry
451 * if none was previously in DB for this raw grade, or will update the existing one.
452 * @param string $feedback Manual feedback from the teacher. Could be a code like 'mi'
453 * @param int $feedbackformat Text format for the feedback
454 * @return boolean Success or Failure
456 function update_feedback($feedback, $feedbackformat, $usermodified=null) {
461 if (empty($usermodified)) {
462 $usermodified = $USER->id
;
465 if (empty($this->grade_grade_text
->id
)) {
466 $this->grade_grade_text
= new grade_grade_text();
468 $this->grade_grade_text
->gradeid
= $this->id
;
469 $this->grade_grade_text
->feedback
= $feedback;
470 $this->grade_grade_text
->feedbackformat
= $feedbackformat;
471 $this->grade_grade_text
->usermodified
= $usermodified;
473 return $this->grade_grade_text
->insert();
476 if ($this->grade_grade_text
->feedback
!= $feedback
477 or $this->grade_grade_text
->feedbackformat
!= $feedbackformat) {
479 $this->grade_grade_text
->feedback
= $feedback;
480 $this->grade_grade_text
->feedbackformat
= $feedbackformat;
481 $this->grade_grade_text
->usermodified
= $usermodified;
483 return $this->grade_grade_text
->update();
491 * Given a float value situated between a source minimum and a source maximum, converts it to the
492 * corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
493 * for the formula :-)
496 * @param float $rawgrade
497 * @param float $source_min
498 * @param float $source_max
499 * @param float $target_min
500 * @param float $target_max
501 * @return float Converted value
503 function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) {
504 if (is_null($rawgrade)) {
508 $factor = ($rawgrade - $source_min) / ($source_max - $source_min);
509 $diff = $target_max - $target_min;
510 $standardised_value = $factor * $diff +
$target_min;
511 return $standardised_value;
515 * Returns the grade letter this grade falls under, as they are set up in the given array.
516 * @param array $letters An array of grade boundaries with associated letters
517 * @param float $gradevalue The value to convert. If not given, will use instantiated object
518 * @param float $grademin If not given, will look up the grade_item's grademin
519 * @param float $grademax If not given, will look up the grade_item's grademax
520 * @return string Grade letter
522 function get_letter($letters, $gradevalue=null, $grademin=null, $grademax=null) {
523 if (is_null($grademin) ||
is_null($grademax)) {
525 debugging("Tried to call grade_grade::get_letter statically without giving an explicit grademin or grademax!");
528 $this->load_grade_item();
529 $grademin = $this->grade_item
->grademin
;
530 $grademax = $this->grade_item
->grademax
;
533 if (is_null($gradevalue)) {
535 debugging("Tried to call grade_grade::get_letter statically without giving an explicit gradevalue!");
538 $gradevalue = $this->finalgrade
;
540 // Standardise grade first
541 $grade = grade_grade
::standardise_score($gradevalue, $grademin, $grademax, 0, 100);
543 // Sort the letters by descending boundaries (100-0)
545 foreach ($letters as $boundary => $letter) {
546 if ($grade >= $boundary) {