Skip to content
Browse files

MDL-29733 grade: Query for Identifying Grade Grades to Precreate is SLOW

  • Loading branch information...
1 parent 67ba008 commit d557253610c99af89c62a5b898742a5ee457f550 @jrchamp jrchamp committed with andyjdavis
Showing with 20 additions and 10 deletions.
  1. +20 −10 lib/grade/grade_item.php
View
30 lib/grade/grade_item.php
@@ -1670,16 +1670,26 @@ 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)) {
+ 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 {
+ // precreate grades - we need them to exist
+ $params = array($this->courseid, $this->id);
+ $sql = "SELECT go.userid
+ FROM {grade_grades} go
+ JOIN {grade_items} gi
+ ON (gi.id = go.itemid AND gi.courseid = ?)
+ GROUP BY go.userid
+ HAVING SUM(go.itemid = ?) = 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 d557253

Please sign in to comment.
Something went wrong with that request. Please try again.