### Eclipse Workspace Patch 1.0
#P moodle19b
Index: version.php
===================================================================
RCS file: /cvsroot/moodle/moodle/version.php,v
retrieving revision 1.563.2.286
diff -u -r1.563.2.286 version.php
--- version.php	24 Oct 2008 01:56:23 -0000	1.563.2.286
+++ version.php	24 Oct 2008 21:55:57 -0000
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2007101530;  // YYYYMMDD      = date of the 1.9 branch (don't change)
+    $version = 2007101531;  // YYYYMMDD      = date of the 1.9 branch (don't change)
                             //         X     = release number 1.9.[0,1,2,3...]
                             //          Y.YY = micro-increments between releases
 
Index: lib/db/upgrade.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/db/upgrade.php,v
retrieving revision 1.154.2.42
diff -u -r1.154.2.42 upgrade.php
--- lib/db/upgrade.php	13 Oct 2008 21:45:24 -0000	1.154.2.42
+++ lib/db/upgrade.php	24 Oct 2008 21:56:00 -0000
@@ -3073,6 +3073,69 @@
         }
     }
 
+    if ($result && $oldversion < 2007101531) {
+
+        notify('Cleaning empty gradebook records, this may take a while...', 'notifysuccess');
+
+        if (!empty($CFG->gradebookroles)) {
+             if (strpos($CFG->gradebookroles, ',') === false) {
+                 $gradebookroles = " = {$CFG->gradebookroles}";
+             } else {
+                 $gradebookroles = " IN ({$CFG->gradebookroles})";
+             }
+
+            //find all course with calculations - this is a simplification, somebody might have removed the formulas, sorry
+            $sql = "SELECT DISTINCT courseid AS id
+                      FROM {$CFG->prefix}grade_items
+                     WHERE calculation IS NOT NULL";
+
+            if ($rs = get_recordset_sql($sql)) {
+                while ($course = rs_fetch_next_record($rs)) {
+                    $courseid = $course->id;
+                    $relatedcontexts = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $courseid));
+
+                    // delete candidates are users that are not gradeable anymore
+                    $candidates_sql = "SELECT DISTINCT g.userid AS id
+                                         FROM {$CFG->prefix}grade_grades g
+                                         JOIN {$CFG->prefix}grade_items gi ON g.itemid = gi.id
+                                         LEFT JOIN {$CFG->prefix}role_assignments ra
+                                              ON (g.userid = ra.userid AND ra.roleid $gradebookroles AND ra.contextid $relatedcontexts)
+                                        WHERE ra.id IS NULL";
+
+                    if ($rs2 = get_recordset_sql($candidates_sql)) {
+                        $db->debug = false;
+                        while ($user = rs_fetch_next_record($rs2)) {
+                            $userid = $user->id;
+                            $used_sql =  "SELECT 'x'
+                                            FROM {$CFG->prefix}grade_grades g
+                                            JOIN {$CFG->prefix}grade_items gi ON (g.itemid = gi.id AND gi.courseid = $courseid)
+                                           WHERE g.userid = $userid
+                                                 AND (g.overridden = 1
+                                                      OR (gi.itemtype = 'manual' AND gi.calculation IS NULL
+                                                          AND (g.finalgrade IS NOT NULL OR g.feedback IS NOT NULL)))";
+
+                            // delete bogus grades - no overridden/manual grade already present
+                            if (!record_exists_sql($used_sql)) {
+                                $deletesql = "DELETE
+                                                FROM {$CFG->prefix}grade_grades
+                                               WHERE userid = $userid AND itemid IN (SELECT gi.id
+                                                                                       FROM {$CFG->prefix}grade_items gi
+                                                                                      WHERE gi.courseid = $courseid)";
+                                execute_sql($deletesql, false);
+                            }
+                        }
+                        rs_close($rs2);
+                        $db->debug = true;
+                    }
+                }
+                rs_close($rs);
+            }
+        }
+
+    /// Main savepoint reached
+        upgrade_main_savepoint($result, 2007101531);
+    }
+
     return $result;
 }
 
