Added hooks to qtype import/export
[moodle-linuxchix.git] / lib / grade / grade_grade.php
blobecb43047d83d0251dbb9d03e4aa6c02d75a21294
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
11 // //
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. //
16 // //
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: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once('grade_object.php');
28 class grade_grade extends grade_object {
30 /**
31 * The DB table.
32 * @var string $table
34 var $table = 'grade_grades';
36 /**
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');
42 /**
43 * The id of the grade_item this grade belongs to.
44 * @var int $itemid
46 var $itemid;
48 /**
49 * The grade_item object referenced by $this->itemid.
50 * @var object $grade_item
52 var $grade_item;
54 /**
55 * The id of the user this grade belongs to.
56 * @var int $userid
58 var $userid;
60 /**
61 * The grade value of this raw grade, if such was provided by the module.
62 * @var float $rawgrade
64 var $rawgrade;
66 /**
67 * The maximum allowable grade when this grade was created.
68 * @var float $rawgrademax
70 var $rawgrademax = 100;
72 /**
73 * The minimum allowable grade when this grade was created.
74 * @var float $rawgrademin
76 var $rawgrademin = 0;
78 /**
79 * id of the scale, if this grade is based on a scale.
80 * @var int $rawscaleid
82 var $rawscaleid;
84 /**
85 * The userid of the person who last modified this grade.
86 * @var int $usermodified
88 var $usermodified;
90 /**
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;
98 /**
99 * The final value of this grade.
100 * @var float $finalgrade
102 var $finalgrade;
105 * 0 if visible, 1 always hidden or date not visible until
106 * @var float $hidden
108 var $hidden = 0;
111 * 0 not locked, date when the item was locked
112 * @var float locked
114 var $locked = 0;
117 * 0 no automatic locking, date when to lock the grade automatically
118 * @var float $locktime
120 var $locktime = 0;
123 * Exported flag
124 * @var boolean $exported
126 var $exported = 0;
129 * Overridden flag
130 * @var boolean $overridden
132 var $overridden = 0;
135 * Grade excluded from aggregation functions
136 * @var boolean $excluded
138 var $excluded = 0;
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.
143 * @return 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;
165 return 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?
181 * @return boolean
183 function is_editable() {
184 if ($this->is_locked()) {
185 return false;
188 $grade_item = $this->load_grade_item();
190 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
191 return false;
194 return true;
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
212 * @return boolean
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();
226 $this->update();
227 return true;
229 } else if (!empty($this->overridden) and !$state) {
230 $this->overridden = 0;
231 $this->update();
232 return true;
234 return false;
238 * Checks if grade excluded from aggregation functions
239 * @return boolean
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();
253 $this->update();
254 return true;
256 } else if (!empty($this->excluded) and !$state) {
257 $this->excluded = 0;
258 $this->update();
259 return true;
261 return false;
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();
275 if ($lockedstate) {
276 if ($this->grade_item->needsupdate) {
277 //can not lock grade if final not calculated!
278 return false;
281 $this->locked = time();
282 $this->update();
284 return true;
286 } else {
287 if (!empty($this->locked) and $this->locktime < time()) {
288 //we have to reset locktime or else it would lock up again
289 $this->locktime = 0;
292 // remove the locked flag
293 $this->locked = 0;
294 $this->update();
296 if ($refresh) {
297 //refresh when unlocking
298 $this->grade_item->refresh_grades($this->userid);
301 return true;
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
308 * @return void
310 function check_locktime_all($items) {
311 global $CFG;
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');
325 rs_close($rs);
330 * Set the locktime for this grade.
332 * @param int $locktime timestamp for lock to activate
333 * @return void
335 function set_locktime($locktime) {
336 $this->locktime = $locktime;
337 $this->update();
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;
353 } else {
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;
378 $this->update();
382 * Finds and returns a grade_grade instance based on params.
383 * @static
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.
394 * @static
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) {
424 $this->load_text();
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();
436 } else {
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();
443 } else {
444 return true;
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) {
457 global $USER;
459 $this->load_text();
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();
475 } else {
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();
484 } else {
485 return true;
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 :-)
495 * @static
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)) {
505 return null;
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)) {
524 if (!isset($this)) {
525 debugging("Tried to call grade_grade::get_letter statically without giving an explicit grademin or grademax!");
526 return false;
528 $this->load_grade_item();
529 $grademin = $this->grade_item->grademin;
530 $grademax = $this->grade_item->grademax;
533 if (is_null($gradevalue)) {
534 if (!isset($this)) {
535 debugging("Tried to call grade_grade::get_letter statically without giving an explicit gradevalue!");
536 return false;
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)
544 krsort($letters);
545 foreach ($letters as $boundary => $letter) {
546 if ($grade >= $boundary) {
547 return $letter;
550 return '-';