MDL-31987 Assignment module: Assignment count submissions correctly.
authorRossiani Wijaya <rwijaya@moodle.com>
Thu, 28 Jun 2012 05:53:19 +0000 (13:53 +0800)
committerRossiani Wijaya <rwijaya@moodle.com>
Mon, 2 Jul 2012 06:49:29 +0000 (14:49 +0800)
For advanced upload assignment, store file count of each submission
into assignment_submissions.numfiles

When counting submissions, if the assignment is open and tracking
drafts, only submissions which has send for marking are counted.
Otherwise, submissions which has numfiles > 0 are counted.

Also change a hardcoded 'submitted' to ASSIGNMENT_STATUS_SUBMITTED.

This patch was originally written by: Sunner Sun <sunner@gmail.com>.

I made some modifation to fixed count_real_submissions() query (ref: MDL-32207) in /mod/assignment/type/upload/assignment.class.php file

mod/assignment/db/upgrade.php
mod/assignment/type/upload/assignment.class.php
mod/assignment/version.php

index d9c46ea..256bda9 100644 (file)
@@ -32,6 +32,42 @@ function xmldb_assignment_upgrade($oldversion) {
     // Moodle v2.3.0 release upgrade line
     // Put any upgrade step following this
 
+    if ($oldversion < 2012062800) {
+        // Fixed/updated numfiles field in assignment_submissions table to count the actual
+        // number of files has been uploaded.
+        upgrade_set_timeout(600);  // increase excution time for in large sites
+        $fs = get_file_storage();
+
+        $selectcount = 'SELECT COUNT(s.id), cm.id AS cmid';
+        $select      = 'SELECT s.id, cm.id AS cmid';
+        $query       = "  FROM {assignment_submissions} s
+                    INNER JOIN {course_modules} cm
+                            ON s.assignment = cm.instance
+                          JOIN {assignment} a
+                            ON a.id = s.assignment
+                         WHERE a.assignmenttype in ('upload', 'uploadsingle') AND
+                               cm.module = (SELECT id
+                                              FROM {modules}
+                                             WHERE name = 'assignment')";
+
+        $countsubmissions = $DB->count_records_sql($selectcount. $query);
+        $submissions = $DB->get_recordset_sql($select. $query);
+
+        $pbar = new progress_bar('assignmentupgradenumfiles', 500, true);
+        $i = 0;
+        foreach ($submissions as $sub) {
+            $i++;
+            if ($context = context_module::instance($sub->cmid)) {
+                $sub->numfiles = count($fs->get_area_files($context->id, 'mod_assignment', 'submission', $sub->id, 'sortorder', false));
+                $DB->update_record('assignment_submissions', $sub);
+            }
+            $pbar->update($i, $countsubmissions, "Counting files of submissions ($i/$countsubmissions)");
+        }
+        $submissions->close();
+
+        // assignment savepoint reached
+        upgrade_mod_savepoint(true, 2012062800, 'assignment');
+    }
 
     return true;
 }
index 6971120..83afeb4 100644 (file)
@@ -395,8 +395,8 @@ class assignment_upload extends assignment_base {
 
     /**
      * Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
-     * This is necessary for advanced file uploads where we need to check that the data2 field is equal to "submitted" to determine
-     * if a submission is complete.
+     * This is necessary for tracked advanced file uploads where we need to check that the data2 field is equal to ASSIGNMENT_STATUS_SUBMITTED
+     * to determine if a submission is complete.
      *
      * @param  int $groupid (optional) If nonzero then count is restricted to this group
      * @return int          The number of submissions
@@ -411,13 +411,19 @@ class assignment_upload extends assignment_base {
         list($enroledsql, $params) = get_enrolled_sql($context, 'mod/assignment:view', $groupid);
         $params['assignmentid'] = $this->cm->instance;
 
-        // Get ids of users enrolled in the given course.
+        $query = '';
+        if ($this->drafts_tracked() and $this->isopen()) {
+            $query = ' AND ' . $DB->sql_compare_text('s.data2') . " = '"  . ASSIGNMENT_STATUS_SUBMITTED . "'";
+        } else {
+            // Count on submissions with files actually uploaded
+            $query = " AND s.numfiles > 0";
+        }
         return $DB->count_records_sql("SELECT COUNT('x')
                                          FROM {assignment_submissions} s
                                     LEFT JOIN {assignment} a ON a.id = s.assignment
                                    INNER JOIN ($enroledsql) u ON u.id = s.userid
-                                        WHERE s.assignment = :assignmentid AND
-                                              s.data2 = 'submitted'", $params);
+                                        WHERE s.assignment = :assignmentid" .
+                                              $query, $params);
     }
 
     function print_responsefiles($userid, $return=false) {
@@ -581,6 +587,7 @@ class assignment_upload extends assignment_base {
             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
             $updates = new stdClass();
             $updates->id = $submission->id;
+            $updates->numfiles = count($fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, 'sortorder', false));
             $updates->timemodified = time();
             $DB->update_record('assignment_submissions', $updates);
             add_to_log($this->course->id, 'assignment', 'upload',
index c7c7c2d..67a72be 100644 (file)
@@ -25,7 +25,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$module->version   = 2012061700;       // The current module version (Date: YYYYMMDDXX)
+$module->version   = 2012062800;       // The current module version (Date: YYYYMMDDXX)
 $module->requires  = 2012061700;    // Requires this Moodle version
 $module->component = 'mod_assignment'; // Full name of the plugin (used for diagnostics)
 $module->cron      = 60;