MDL-46171 assign: Peer review cleanups
authorDamyon Wiese <damyon@moodle.com>
Wed, 10 Sep 2014 05:46:31 +0000 (13:46 +0800)
committerDamyon Wiese <damyon@moodle.com>
Wed, 24 Sep 2014 07:07:36 +0000 (15:07 +0800)
* Remove hardcoded table names
* Remove some code that was left in after debugging
* Add some comments about grades with no submission
* Set submission->latest to 0 on restore (it will get fixed later)
* Changed get_records_sql to get_recordset_sql in restore.

mod/assign/backup/moodle2/restore_assign_stepslib.php
mod/assign/db/upgrade.php
mod/assign/tests/externallib_test.php

index db0f099..b603b49 100644 (file)
@@ -136,6 +136,9 @@ class restore_assign_activity_structure_step extends restore_activity_structure_
             $data->groupid = 0;
         }
 
+        // We will correct this in set_latest_submission_field() once all submissions are restored.
+        $data->latest = 0;
+
         $newitemid = $DB->insert_record('assign_submission', $data);
 
         // Note - the old contextid is required in order to be able to restore files stored in
@@ -232,7 +235,9 @@ class restore_assign_activity_structure_step extends restore_activity_structure_
     }
 
     /**
-     * For all submissions in this assignment, either set the submission->latest field to 1 for the latest attempts.
+     * For all submissions in this assignment, either set the
+     * submission->latest field to 1 for the latest attempts
+     * or create a new submission record for grades with no submission.
      *
      * @return void
      */
@@ -282,7 +287,8 @@ class restore_assign_activity_structure_step extends restore_activity_structure_
         }
 
         // Now check for records with a grade, but no submission record.
-        $records = $DB->get_records_sql('SELECT g.id, g.userid
+        // This happens when a teacher marks a student before they have submitted anything.
+        $records = $DB->get_recordset_sql('SELECT g.id, g.userid
                                            FROM {assign_grades} g
                                       LEFT JOIN {assign_submission} s
                                              ON s.assignment = g.assignment
@@ -302,6 +308,8 @@ class restore_assign_activity_structure_step extends restore_activity_structure_
             array_push($submissions, $submission);
         }
 
+        $records->close();
+
         $DB->insert_records('assign_submission', $submissions);
     }
 
index 2a6bd17..63f5b27 100755 (executable)
@@ -527,18 +527,18 @@ function xmldb_assign_upgrade($oldversion) {
 
         // Prevent running this multiple times.
 
-        $countsql = 'SELECT COUNT(id) FROM {assign_submission} WHERE latest = ?;';
+        $countsql = 'SELECT COUNT(id) FROM {assign_submission} WHERE latest = ?';
 
         $count = $DB->count_records_sql($countsql, array(1));
-        if ($count != 342234) {
+        if ($count == 0) {
 
             // Mark the latest attempt for every submission in mod_assign.
             $maxattemptsql = 'SELECT assignment, userid, groupid, max(attemptnumber) AS maxattempt
-                                FROM mdl23_assign_submission
+                                FROM {assign_submission}
                             GROUP BY assignment, groupid, userid';
 
             $maxattemptidssql = 'SELECT souter.id
-                                   FROM mdl23_assign_submission souter
+                                   FROM {assign_submission} souter
                                    JOIN (' . $maxattemptsql . ') sinner
                                      ON souter.assignment = sinner.assignment
                                     AND souter.userid = sinner.userid
@@ -548,6 +548,7 @@ function xmldb_assign_upgrade($oldversion) {
             $DB->set_field_select('assign_submission', 'latest', 1, $select);
 
             // Look for grade records with no submission record.
+            // This is when a teacher has marked a student before they submitted anything.
             $records = $DB->get_records_sql('SELECT g.id, g.assignment, g.userid
                                                FROM {assign_grades} g
                                           LEFT JOIN {assign_submission} s
index 9b33ebe..ceb7f0d 100644 (file)
@@ -985,8 +985,6 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
         // No warnings.
         $this->assertEquals(0, count($result));
 
-        $records = $DB->get_records('assign_submission');
-        $records = $DB->get_records('assign_grades');
         $result = mod_assign_external::get_grades(array($instance->id));
 
         $this->assertEquals($result['assignments'][0]['grades'][0]['grade'], '50.0');