3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Definitions of grade grade class
22 * @copyright 2006 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once('grade_object.php');
30 class grade_grade extends grade_object {
36 public $table = 'grade_grades';
39 * Array of required table fields, must start with 'id'.
40 * @var array $required_fields
42 public $required_fields = array('id', 'itemid', 'userid', 'rawgrade', 'rawgrademax', 'rawgrademin',
43 'rawscaleid', 'usermodified', 'finalgrade', 'hidden', 'locked',
44 'locktime', 'exported', 'overridden', 'excluded', 'timecreated', 'timemodified');
47 * Array of optional fields with default values (these should match db defaults)
48 * @var array $optional_fields
50 public $optional_fields = array('feedback'=>null, 'feedbackformat'=>0, 'information'=>null, 'informationformat'=>0);
53 * The id of the grade_item this grade belongs to.
59 * The grade_item object referenced by $this->itemid.
60 * @var object $grade_item
65 * The id of the user this grade belongs to.
71 * The grade value of this raw grade, if such was provided by the module.
72 * @var float $rawgrade
77 * The maximum allowable grade when this grade was created.
78 * @var float $rawgrademax
80 public $rawgrademax = 100;
83 * The minimum allowable grade when this grade was created.
84 * @var float $rawgrademin
86 public $rawgrademin = 0;
89 * id of the scale, if this grade is based on a scale.
90 * @var int $rawscaleid
95 * The userid of the person who last modified this grade.
96 * @var int $usermodified
101 * The final value of this grade.
102 * @var float $finalgrade
107 * 0 if visible, 1 always hidden or date not visible until
113 * 0 not locked, date when the item was locked
119 * 0 no automatic locking, date when to lock the grade automatically
120 * @var float $locktime
122 public $locktime = 0;
126 * @var boolean $exported
128 public $exported = 0;
132 * @var boolean $overridden
134 public $overridden = 0;
137 * Grade excluded from aggregation functions
138 * @var boolean $excluded
140 public $excluded = 0;
143 * TODO: HACK: create a new field datesubmitted - the date of submission if any
144 * @var boolean $timecreated
146 public $timecreated = null;
149 * TODO: HACK: create a new field dategraded - the date of grading
150 * @var boolean $timemodified
152 public $timemodified = null;
156 * Returns array of grades for given grade_item+users.
157 * @param object $grade_item
158 * @param array $userids
159 * @param bool $include_missing include grades that do not exist yet
160 * @return array userid=>grade_grade array
162 public static function fetch_users_grades($grade_item, $userids, $include_missing=true) {
165 // hmm, there might be a problem with length of sql query
166 // if there are too many users requested - we might run out of memory anyway
168 $count = count($userids);
169 if ($count > $limit) {
170 $half = (int)($count/2);
171 $first = array_slice($userids, 0, $half);
172 $second = array_slice($userids, $half);
173 return grade_grade::fetch_users_grades($grade_item, $first, $include_missing) + grade_grade::fetch_users_grades($grade_item, $second, $include_missing);
176 list($user_ids_cvs, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'uid0');
177 $params['giid'] = $grade_item->id;
179 if ($grade_records = $DB->get_records_select('grade_grades', "itemid=:giid AND userid $user_ids_cvs", $params)) {
180 foreach ($grade_records as $record) {
181 $result[$record->userid] = new grade_grade($record, false);
184 if ($include_missing) {
185 foreach ($userids as $userid) {
186 if (!array_key_exists($userid, $result)) {
187 $grade_grade = new grade_grade();
188 $grade_grade->userid = $userid;
189 $grade_grade->itemid = $grade_item->id;
190 $result[$userid] = $grade_grade;
199 * Loads the grade_item object referenced by $this->itemid and saves it as $this->grade_item for easy access.
200 * @return object grade_item.
202 public function load_grade_item() {
203 if (empty($this->itemid)) {
204 debugging('Missing itemid');
205 $this->grade_item = null;
209 if (empty($this->grade_item)) {
210 $this->grade_item = grade_item::fetch(array('id'=>$this->itemid));
212 } else if ($this->grade_item->id != $this->itemid) {
213 debugging('Itemid mismatch');
214 $this->grade_item = grade_item::fetch(array('id'=>$this->itemid));
217 return $this->grade_item;
221 * Is grading object editable?
224 public function is_editable() {
225 if ($this->is_locked()) {
229 $grade_item = $this->load_grade_item();
231 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
239 * Check grade lock status. Uses both grade item lock and grade lock.
240 * Internally any date in locked field (including future ones) means locked,
241 * the date is stored for logging purposes only.
243 * @return boolean true if locked, false if not
245 public function is_locked() {
246 $this->load_grade_item();
247 if (empty($this->grade_item)) {
248 return !empty($this->locked);
250 return !empty($this->locked) or $this->grade_item->is_locked();
255 * Checks if grade overridden
258 public function is_overridden() {
259 return !empty($this->overridden);
263 * Returns timestamp of submission related to this grade,
264 * might be null if not submitted.
267 public function get_datesubmitted() {
268 //TODO: HACK - create new fields in 2.0
269 return $this->timecreated;
273 * Returns timestamp when last graded,
274 * might be null if no grade present.
277 public function get_dategraded() {
278 //TODO: HACK - create new fields in 2.0
279 if (is_null($this->finalgrade) and is_null($this->feedback)) {
280 return null; // no grade == no date
281 } else if ($this->overridden) {
282 return $this->overridden;
284 return $this->timemodified;
289 * Set the overridden status of grade
290 * @param boolean $state requested overridden state
291 * @param boolean $refresh refresh grades from external activities if needed
292 * @return boolean true is db state changed
294 public function set_overridden($state, $refresh = true) {
295 if (empty($this->overridden) and $state) {
296 $this->overridden = time();
300 } else if (!empty($this->overridden) and !$state) {
301 $this->overridden = 0;
305 //refresh when unlocking
306 $this->grade_item->refresh_grades($this->userid);
315 * Checks if grade excluded from aggregation functions
318 public function is_excluded() {
319 return !empty($this->excluded);
323 * Set the excluded status of grade
324 * @param boolean $state requested excluded state
325 * @return boolean true is db state changed
327 public function set_excluded($state) {
328 if (empty($this->excluded) and $state) {
329 $this->excluded = time();
333 } else if (!empty($this->excluded) and !$state) {
342 * Lock/unlock this grade.
344 * @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
345 * @param boolean $cascade ignored param
346 * @param boolean $refresh refresh grades when unlocking
347 * @return boolean true if successful, false if can not set new lock state for grade
349 public function set_locked($lockedstate, $cascade=false, $refresh=true) {
350 $this->load_grade_item();
353 if ($this->grade_item->needsupdate) {
354 //can not lock grade if final not calculated!
358 $this->locked = time();
364 if (!empty($this->locked) and $this->locktime < time()) {
365 //we have to reset locktime or else it would lock up again
369 // remove the locked flag
373 if ($refresh and !$this->is_overridden()) {
374 //refresh when unlocking and not overridden
375 $this->grade_item->refresh_grades($this->userid);
383 * Lock the grade if needed - make sure this is called only when final grades are valid
384 * @param array $items array of all grade item ids
387 public function check_locktime_all($items) {
390 $now = time(); // no rounding needed, this is not supposed to be called every 10 seconds
391 list($usql, $params) = $DB->get_in_or_equal($items);
393 $rs = $DB->get_recordset_select('grade_grades', "itemid $usql AND locked = 0 AND locktime > 0 AND locktime < ?", $params);
394 foreach ($rs as $grade) {
395 $grade_grade = new grade_grade($grade, false);
396 $grade_grade->locked = time();
397 $grade_grade->update('locktime');
403 * Set the locktime for this grade.
405 * @param int $locktime timestamp for lock to activate
408 public function set_locktime($locktime) {
409 $this->locktime = $locktime;
414 * Set the locktime for this grade.
416 * @return int $locktime timestamp for lock to activate
418 public function get_locktime() {
419 $this->load_grade_item();
421 $item_locktime = $this->grade_item->get_locktime();
423 if (empty($this->locktime) or ($item_locktime and $item_locktime < $this->locktime)) {
424 return $item_locktime;
427 return $this->locktime;
432 * Check grade hidden status. Uses data from both grade item and grade.
433 * @return boolean true if hidden, false if not
435 public function is_hidden() {
436 $this->load_grade_item();
437 if (empty($this->grade_item)) {
438 return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time());
440 return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time()) or $this->grade_item->is_hidden();
445 * Check grade hidden status. Uses data from both grade item and grade.
446 * @return boolean true if hiddenuntil, false if not
448 public function is_hiddenuntil() {
449 $this->load_grade_item();
451 if ($this->hidden == 1 or $this->grade_item->hidden == 1) {
452 return false; //always hidden
455 if ($this->hidden > 1 or $this->grade_item->hidden > 1) {
463 * Check grade hidden status. Uses data from both grade item and grade.
464 * @return int 0 means visible, 1 hidden always, timestamp hidden until
466 public function get_hidden() {
467 $this->load_grade_item();
469 $item_hidden = $this->grade_item->get_hidden();
471 if ($item_hidden == 1) {
474 } else if ($item_hidden == 0) {
475 return $this->hidden;
478 if ($this->hidden == 0) {
480 } else if ($this->hidden == 1) {
482 } else if ($this->hidden > $item_hidden) {
483 return $this->hidden;
491 * Set the hidden status of grade, 0 mean visible, 1 always hidden, number means date to hide until.
492 * @param boolean $cascade ignored
493 * @param int $hidden new hidden status
495 public function set_hidden($hidden, $cascade=false) {
496 $this->hidden = $hidden;
501 * Finds and returns a grade_grade instance based on params.
504 * @param array $params associative arrays varname=>value
505 * @return object grade_grade instance or false if none found.
507 public static function fetch($params) {
508 return grade_object::fetch_helper('grade_grades', 'grade_grade', $params);
512 * Finds and returns all grade_grade instances based on params.
515 * @param array $params associative arrays varname=>value
516 * @return array array of grade_grade instances or false if none found.
518 public static function fetch_all($params) {
519 return grade_object::fetch_all_helper('grade_grades', 'grade_grade', $params);
523 * Given a float value situated between a source minimum and a source maximum, converts it to the
524 * corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
525 * for the formula :-)
528 * @param float $rawgrade
529 * @param float $source_min
530 * @param float $source_max
531 * @param float $target_min
532 * @param float $target_max
533 * @return float Converted value
535 public static function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) {
536 if (is_null($rawgrade)) {
540 if ($source_max == $source_min or $target_min == $target_max) {
541 // prevent division by 0
545 $factor = ($rawgrade - $source_min) / ($source_max - $source_min);
546 $diff = $target_max - $target_min;
547 $standardised_value = $factor * $diff + $target_min;
548 return $standardised_value;
552 * Return array of grade item ids that are either hidden or indirectly depend
553 * on hidden grades, excluded grades are not returned.
554 * THIS IS A REALLY BIG HACK! to be replaced by conditional aggregation of hidden grades in 2.0
557 * @param array $grades all course grades of one user, & used for better internal caching
558 * @param array $items $grade_items array of grade items, & used for better internal caching
561 public static function get_hiding_affected(&$grade_grades, &$grade_items) {
564 if (count($grade_grades) !== count($grade_items)) {
565 print_error('invalidarraysize', 'debug', '', 'grade_grade::get_hiding_affected()!');
568 $dependson = array();
570 $unknown = array(); // can not find altered
571 $altered = array(); // altered grades
573 $hiddenfound = false;
574 foreach($grade_grades as $itemid=>$unused) {
575 $grade_grade =& $grade_grades[$itemid];
576 if ($grade_grade->is_excluded()) {
577 //nothing to do, aggregation is ok
578 } else if ($grade_grade->is_hidden()) {
580 $altered[$grade_grade->itemid] = null;
581 } else if ($grade_grade->is_locked() or $grade_grade->is_overridden()) {
582 // no need to recalculate locked or overridden grades
584 $dependson[$grade_grade->itemid] = $grade_items[$grade_grade->itemid]->depends_on();
585 if (!empty($dependson[$grade_grade->itemid])) {
586 $todo[] = $grade_grade->itemid;
591 return array('unknown'=>array(), 'altered'=>array());
595 $hidden_precursors = null;
596 for($i=0; $i<$max; $i++) {
598 foreach($todo as $key=>$do) {
599 $hidden_precursors = array_intersect($dependson[$do], $unknown);
600 if ($hidden_precursors) {
601 // this item depends on hidden grade indirectly
607 } else if (!array_intersect($dependson[$do], $todo)) {
608 $hidden_precursors = array_intersect($dependson[$do], array_keys($altered));
609 if (!$hidden_precursors) {
610 // hiding does not affect this grade
616 // depends on altered grades - we should try to recalculate if possible
617 if ($grade_items[$do]->is_calculated() or
618 (!$grade_items[$do]->is_category_item() and !$grade_items[$do]->is_course_item())
626 $grade_category = $grade_items[$do]->load_item_category();
629 foreach ($dependson[$do] as $itemid) {
630 if (array_key_exists($itemid, $altered)) {
631 //nulling an altered precursor
632 $values[$itemid] = $altered[$itemid];
633 } elseif (empty($values[$itemid])) {
634 $values[$itemid] = $grade_grades[$itemid]->finalgrade;
638 foreach ($values as $itemid=>$value) {
639 if ($grade_grades[$itemid]->is_excluded()) {
640 unset($values[$itemid]);
643 $values[$itemid] = grade_grade::standardise_score($value, $grade_items[$itemid]->grademin, $grade_items[$itemid]->grademax, 0, 1);
646 if ($grade_category->aggregateonlygraded) {
647 foreach ($values as $itemid=>$value) {
648 if (is_null($value)) {
649 unset($values[$itemid]);
653 foreach ($values as $itemid=>$value) {
654 if (is_null($value)) {
655 $values[$itemid] = 0;
661 $grade_category->apply_limit_rules($values, $grade_items);
662 asort($values, SORT_NUMERIC);
664 // let's see we have still enough grades to do any statistics
665 if (count($values) == 0) {
666 // not enough attempts yet
667 $altered[$do] = null;
673 $agg_grade = $grade_category->aggregate_values($values, $grade_items);
675 // recalculate the rawgrade back to requested range
676 $finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $grade_items[$do]->grademin, $grade_items[$do]->grademax);
678 $finalgrade = $grade_items[$do]->bounded_grade($finalgrade);
680 $altered[$do] = $finalgrade;
693 return array('unknown'=>$unknown, 'altered'=>$altered);
697 * Returns true if the grade's value is superior or equal to the grade item's gradepass value, false otherwise.
698 * @param object $grade_item An optional grade_item of which gradepass value we can use, saves having to load the grade_grade's grade_item
701 public function is_passed($grade_item = null) {
702 if (empty($grade_item)) {
703 if (!isset($this->grade_item)) {
704 $this->load_grade_item();
707 $this->grade_item = $grade_item;
708 $this->itemid = $grade_item->id;
711 // Return null if finalgrade is null
712 if (is_null($this->finalgrade)) {
716 // Return null if gradepass == grademin or gradepass is null
717 if (is_null($this->grade_item->gradepass) || $this->grade_item->gradepass == $this->grade_item->grademin) {
721 return $this->finalgrade >= $this->grade_item->gradepass;
724 public function insert($source=null) {
725 // TODO: dategraded hack - do not update times, they are used for submission and grading
726 //$this->timecreated = $this->timemodified = time();
727 return parent::insert($source);
731 * In addition to update() as defined in grade_object rounds the float numbers using php function,
732 * the reason is we need to compare the db value with computed number to skip updates if possible.
733 * @param string $source from where was the object inserted (mod/forum, manual, etc.)
734 * @return boolean success
736 public function update($source=null) {
737 $this->rawgrade = grade_floatval($this->rawgrade);
738 $this->finalgrade = grade_floatval($this->finalgrade);
739 $this->rawgrademin = grade_floatval($this->rawgrademin);
740 $this->rawgrademax = grade_floatval($this->rawgrademax);
741 return parent::update($source);
745 * Used to notify the completion system (if necessary) that a user's grade
746 * has changed, and clear up a possible score cache.
747 * @param bool deleted True if grade was actually deleted
749 function notify_changed($deleted) {
750 global $USER, $SESSION, $CFG,$COURSE, $DB;
752 // Grades may be cached in user session
753 if ($USER->id == $this->userid) {
754 unset($SESSION->gradescorecache[$this->itemid]);
757 // Ignore during restore
758 // TODO There should be a proper way to determine when we are in restore
759 // so that this hack looking for a $restore global is not needed.
761 if (!empty($restore->backup_unique_code)) {
765 require_once($CFG->libdir.'/completionlib.php');
767 // Bail out immediately if completion is not enabled for site (saves loading
769 if (!completion_info::is_enabled_for_site()) {
773 // Load information about grade item
774 $this->load_grade_item();
776 // Only course-modules have completion data
777 if ($this->grade_item->itemtype!='mod') {
781 // Use $COURSE if available otherwise get it via item fields
782 if(!empty($COURSE) && $COURSE->id == $this->grade_item->courseid) {
785 $course = $DB->get_record('course', array('id'=>$this->grade_item->courseid));
788 // Bail out if completion is not enabled for course
789 $completion = new completion_info($course);
790 if (!$completion->is_enabled()) {
795 $cm = get_coursemodule_from_instance($this->grade_item->itemmodule,
796 $this->grade_item->iteminstance, $this->grade_item->courseid);
797 // If the course-module doesn't exist, display a warning...
799 // ...unless the grade is being deleted in which case it's likely
800 // that the course-module was just deleted too, so that's okay.
802 debugging("Couldn't find course-module for module '" .
803 $this->grade_item->itemmodule . "', instance '" .
804 $this->grade_item->iteminstance . "', course '" .
805 $this->grade_item->courseid . "'");
810 // Pass information on to completion system
811 $completion->inform_grade_changed($cm, $this->grade_item, $this, $deleted);