Skip to content

Commit

Permalink
MDL-29733 grade: Query for Identifying Grade Grades to Precreate is SLOW
Browse files Browse the repository at this point in the history
  • Loading branch information
jrchamp authored and andyjdavis committed Oct 30, 2012
1 parent 67ba008 commit d557253
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/grade/grade_item.php
Expand Up @@ -1670,16 +1670,26 @@ public function compute($userid=null) {
return true; // no need to recalculate locked items return true; // no need to recalculate locked items
} }


// precreate grades - we need them to exist if ($userid) {
$params = array($this->courseid, $this->id, $this->id); $missing = array();
$sql = "SELECT DISTINCT go.userid if (!$DB->record_exists('grade_grades', array('itemid'=>$this->id, 'userid'=>$userid))) {
FROM {grade_grades} go $m = new stdClass();
JOIN {grade_items} gi $m->userid = $userid;
ON (gi.id = go.itemid AND gi.courseid = ?) $missing[] = $m;
LEFT OUTER JOIN {grade_grades} g }
ON (g.userid = go.userid AND g.itemid = ?) } else {
WHERE gi.id <> ? AND g.id IS NULL"; // precreate grades - we need them to exist
if ($missing = $DB->get_records_sql($sql, $params)) { $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) { foreach ($missing as $m) {
$grade = new grade_grade(array('itemid'=>$this->id, 'userid'=>$m->userid), false); $grade = new grade_grade(array('itemid'=>$this->id, 'userid'=>$m->userid), false);
$grade->grade_item =& $this; $grade->grade_item =& $this;
Expand Down

0 comments on commit d557253

Please sign in to comment.