Permalink
Browse files

MDL-31987 Assignment module: Assignment count submissions correctly.

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
  • Loading branch information...
1 parent 116714e commit b9fdddbabd79b3c6d2810950a7c78344049a5faf @rwijaya rwijaya committed Jun 28, 2012
Showing with 50 additions and 6 deletions.
  1. +37 −0 mod/assignment/db/upgrade.php
  2. +12 −5 mod/assignment/type/upload/assignment.class.php
  3. +1 −1 mod/assignment/version.php
@@ -160,6 +160,43 @@ function xmldb_assignment_upgrade($oldversion) {
// Moodle v2.2.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;
}
@@ -395,8 +395,8 @@ function process_feedback() {
/**
* 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 @@ function count_real_submissions($groupid=0) {
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 @@ function upload_file($mform, $options) {
$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',
@@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();
-$module->version = 2011112900; // The current module version (Date: YYYYMMDDXX)
+$module->version = 2012062800; // The current module version (Date: YYYYMMDDXX)
$module->requires = 2011112900; // Requires this Moodle version
$module->component = 'mod_assignment'; // Full name of the plugin (used for diagnostics)
$module->cron = 60;

0 comments on commit b9fdddb

Please sign in to comment.