Skip to content

Commit

Permalink
Merge branch '21_MDL-30724_count_real_submissions_eloy_suggests' of g…
Browse files Browse the repository at this point in the history
…it://github.com/gerrywastaken/moodle into MOODLE_21_STABLE
  • Loading branch information
Sam Hemelryk committed Mar 5, 2012
2 parents 3885259 + 9af65cb commit d5f1dab
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 37 deletions.
74 changes: 42 additions & 32 deletions mod/assignment/lib.php
Expand Up @@ -1749,15 +1749,31 @@ function prepare_new_submission($userid, $teachermodified=false) {
function get_submissions($sort='', $dir='DESC') { function get_submissions($sort='', $dir='DESC') {
return assignment_get_all_submissions($this->assignment, $sort, $dir); return assignment_get_all_submissions($this->assignment, $sort, $dir);
} }

/** /**
* Counts all real assignment submissions by ENROLLED students (not empty ones) * Counts all complete (real) assignment submissions by enrolled students
* *
* @param int $groupid optional If nonzero then count is restricted to this group * @param int $groupid (optional) If nonzero then count is restricted to this group
* @return int The number of submissions * @return int The number of submissions
*/ */
function count_real_submissions($groupid=0) { function count_real_submissions($groupid=0) {
return assignment_count_real_submissions($this->cm, $groupid); global $CFG;
global $DB;

// Grab the context assocated with our course module
$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);

// Get ids of users enrolled in the given course.
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.
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.timemodified > 0", $params);
} }


/** /**
Expand Down Expand Up @@ -3299,44 +3315,38 @@ function assignment_get_unmailed_submissions($starttime, $endtime) {
} }


/** /**
* Counts all real assignment submissions by ENROLLED students (not empty ones) * Counts all complete (real) assignment submissions by enrolled students for the given course modeule.
* *
* There are also assignment type methods count_real_submissions() which in the default * @deprecated Since Moodle 2.2 MDL-abc - Please do not use this function any more.
* implementation simply call this function. * @param cm_info $cm The course module that we wish to perform the count on.
* @param $groupid int optional If nonzero then count is restricted to this group * @param int $groupid (optional) If nonzero then count is restricted to this group
* @return int The number of submissions * @return int The number of submissions
*/ */
function assignment_count_real_submissions($cm, $groupid=0) { function assignment_count_real_submissions($cm, $groupid=0) {
global $CFG, $DB; global $CFG, $DB;


$context = get_context_instance(CONTEXT_MODULE, $cm->id); // Grab the assignment type for the given course module
$assignmenttype = $DB->get_field($cm->modname, 'assignmenttype', array('id' => $cm->instance));


// this is all the users with this capability set, in this context or higher // Create the expected class file path and class name for the returned assignemnt type
if ($users = get_enrolled_users($context, 'mod/assignment:view', $groupid, 'u.id')) { $filename = "{$CFG->dirroot}/mod/assignment/type/{$assignmenttype}/assignment.class.php";
$users = array_keys($users); $classname = "assignment_{$assignmenttype}";
}


// if groupmembersonly used, remove users who are not in any group // If the file exists and the class is not already loaded we require the class file
if ($users and !empty($CFG->enablegroupmembersonly) and $cm->groupmembersonly) { if (file_exists($file) && !class_exists($class)) {
if ($groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id')) { require_once($file);
$users = array_intersect($users, array_keys($groupingusers));
}
} }

// If the required class is still not loaded then we revert to assignment base
if (empty($users)) { if (!class_exists($class)) {
return 0; $classname = 'assignment_base';
} }
$instance = new $classname;


$userlists = implode(',', $users); // Attach the course module to the assignment type instance and then call the method for counting submissions

$instance->cm = $cm;
return $DB->count_records_sql("SELECT COUNT('x') return $instance->count_real_submissions($groupid);
FROM {assignment_submissions}
WHERE assignment = ? AND
timemodified > 0 AND
userid IN ($userlists)", array($cm->instance));
} }



/** /**
* Return all assignment submissions by ENROLLED students (even empty) * Return all assignment submissions by ENROLLED students (even empty)
* *
Expand Down
29 changes: 28 additions & 1 deletion mod/assignment/type/upload/assignment.class.php
Expand Up @@ -379,6 +379,34 @@ function process_feedback() {
parent::process_feedback($mform); parent::process_feedback($mform);
} }


/**
* 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.
*
* @param int $groupid (optional) If nonzero then count is restricted to this group
* @return int The number of submissions
*/
function count_real_submissions($groupid=0) {
global $CFG;
global $DB;

// Grab the context assocated with our course module
$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);

// Get ids of users enrolled in the given course.
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.
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);
}

function print_responsefiles($userid, $return=false) { function print_responsefiles($userid, $return=false) {
global $CFG, $USER, $OUTPUT, $PAGE; global $CFG, $USER, $OUTPUT, $PAGE;


Expand All @@ -405,7 +433,6 @@ function print_responsefiles($userid, $return=false) {
echo $output; echo $output;
} }



/** /**
* Upload files * Upload files
* upload_file function requires moodle form instance and file manager options * upload_file function requires moodle form instance and file manager options
Expand Down
34 changes: 30 additions & 4 deletions mod/assignment/type/uploadsingle/assignment.class.php
Expand Up @@ -92,7 +92,6 @@ function view() {
$this->view_footer(); $this->view_footer();
} }



function process_feedback() { function process_feedback() {
if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data? if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data?
return false; return false;
Expand All @@ -101,7 +100,35 @@ function process_feedback() {
$offset = required_param('offset', PARAM_INT); $offset = required_param('offset', PARAM_INT);
$mform = $this->display_submission($offset, $userid, false); $mform = $this->display_submission($offset, $userid, false);
parent::process_feedback($mform); parent::process_feedback($mform);
} }

/**
* Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
* This is necessary for simple file uploads where we need to check that the numfiles field is greater than zero 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
*/
function count_real_submissions($groupid=0) {
global $CFG;
global $DB;

// Grab the context assocated with our course module
$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);

// Get ids of users enrolled in the given course.
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.
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.numfiles > 0", $params);
}


function print_responsefiles($userid, $return=false) { function print_responsefiles($userid, $return=false) {
global $CFG, $USER, $OUTPUT, $PAGE; global $CFG, $USER, $OUTPUT, $PAGE;
Expand Down Expand Up @@ -155,7 +182,6 @@ function view_upload_form() {
echo $OUTPUT->box_end(); echo $OUTPUT->box_end();
} }



function upload($mform) { function upload($mform) {
$action = required_param('action', PARAM_ALPHA); $action = required_param('action', PARAM_ALPHA);
switch ($action) { switch ($action) {
Expand Down Expand Up @@ -434,4 +460,4 @@ function definition() {
// buttons // buttons
$this->add_action_buttons(false, get_string('uploadthisfile')); $this->add_action_buttons(false, get_string('uploadthisfile'));
} }
} }

0 comments on commit d5f1dab

Please sign in to comment.