Permalink
Browse files

Merge branch 'MDL-29733_master' of git://github.com/andyjdavis/moodle…

… into MOODLE_24_STABLE
  • Loading branch information...
2 parents e8e9393 + 415b15c commit cd281ce1b41aa06962d049dfa3c1695a996ea452 Sam Hemelryk committed Dec 17, 2012
Showing with 21 additions and 10 deletions.
  1. +21 −10 lib/grade/grade_item.php
View
@@ -1730,16 +1730,27 @@ public function compute($userid=null) {
return true; // no need to recalculate locked items
}
- // precreate grades - we need them to exist
- $params = array($this->courseid, $this->id, $this->id);
- $sql = "SELECT DISTINCT go.userid
- FROM {grade_grades} go
- JOIN {grade_items} gi
- ON (gi.id = go.itemid AND gi.courseid = ?)
- LEFT OUTER JOIN {grade_grades} g
- ON (g.userid = go.userid AND g.itemid = ?)
- WHERE gi.id <> ? AND g.id IS NULL";
- if ($missing = $DB->get_records_sql($sql, $params)) {
+ // Precreate grades - we need them to exist
+ if ($userid) {
+ $missing = array();
+ if (!$DB->record_exists('grade_grades', array('itemid'=>$this->id, 'userid'=>$userid))) {
+ $m = new stdClass();
+ $m->userid = $userid;
+ $missing[] = $m;
+ }
+ } else {
+ // Find any users who have grades for some but not all grade items in this course
+ $params = array('gicourseid' => $this->courseid, 'ggitemid' => $this->id);
+ $sql = "SELECT gg.userid
+ FROM {grade_grades} gg
+ JOIN {grade_items} gi
+ ON (gi.id = gg.itemid AND gi.courseid = :gicourseid)
+ GROUP BY gg.userid
+ HAVING SUM(CASE WHEN gg.itemid = :ggitemid THEN 1 ELSE 0 END) = 0";
+ $missing = $DB->get_records_sql($sql, $params);
+ }
+
+ if ($missing) {
foreach ($missing as $m) {
$grade = new grade_grade(array('itemid'=>$this->id, 'userid'=>$m->userid), false);
$grade->grade_item =& $this;

0 comments on commit cd281ce

Please sign in to comment.