MDL-46171 assign: And here is the benefit of the new column - simplify the SQL

This commit is contained in:
Damyon Wiese 2014-07-24 15:36:06 +08:00
parent 1523f9e0fb
commit ad030ab4a5
4 changed files with 32 additions and 84 deletions

View File

@ -129,23 +129,25 @@ class mod_assign_external extends external_api {
if (count ($requestedassignmentids) > 0) {
$placeholders = array();
list($inorequalsql, $placeholders) = $DB->get_in_or_equal($requestedassignmentids, SQL_PARAMS_NAMED);
list($inorequalsql2, $placeholders2) = $DB->get_in_or_equal($requestedassignmentids, SQL_PARAMS_NAMED);
$grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
FROM {assign_grades} mxg
WHERE mxg.assignment ' . $inorequalsql2 . ' GROUP BY mxg.userid';
$sql = "SELECT ag.id,
ag.assignment,
ag.userid,
ag.timecreated,
ag.timemodified,
ag.grader,
ag.grade,
ag.attemptnumber
FROM {assign_grades} ag, {assign_submission} as
WHERE as.assignment $inorequalsql
AND as.userid = ag.userid
AND as.latest = 1
AND as.attemptnumber = ag.attemptnumber
AND ag.timemodified >= :since
AND ag.assignment = as.assignment
ORDER BY ag.assignment, ag.id";
$sql = "SELECT ag.id,ag.assignment,ag.userid,ag.timecreated,ag.timemodified,".
"ag.grader,ag.grade,ag.attemptnumber ".
"FROM {assign_grades} ag ".
"JOIN ( " . $grademaxattempt . " ) gmx ON ag.userid = gmx.userid".
" WHERE ag.assignment ".$inorequalsql.
" AND ag.timemodified >= :since".
" AND ag.attemptnumber = gmx.maxattempt" .
" ORDER BY ag.assignment, ag.id";
$placeholders['since'] = $params['since'];
// Combine the parameters.
$placeholders += $placeholders2;
$rs = $DB->get_recordset_sql($sql, $placeholders);
$currentassignmentid = null;
$assignment = null;

View File

@ -129,8 +129,6 @@ class assign_grading_table extends table_sql implements renderable {
$params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
$params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
$params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
$params['assignmentid4'] = (int)$this->assignment->get_instance()->id;
$params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
$extrauserfields = get_extra_user_fields($this->assignment->get_context());
@ -151,23 +149,15 @@ class assign_grading_table extends table_sql implements renderable {
$fields .= 'uf.workflowstate as workflowstate, ';
$fields .= 'uf.allocatedmarker as allocatedmarker ';
$submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt
FROM {assign_submission} mxs
WHERE mxs.assignment = :assignmentid4 GROUP BY mxs.userid';
$grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
FROM {assign_grades} mxg
WHERE mxg.assignment = :assignmentid5 GROUP BY mxg.userid';
$from = '{user} u
LEFT JOIN ( ' . $submissionmaxattempt . ' ) smx ON u.id = smx.userid
LEFT JOIN ( ' . $grademaxattempt . ' ) gmx ON u.id = gmx.userid
LEFT JOIN {assign_submission} s ON
u.id = s.userid AND
s.assignment = :assignmentid1 AND
s.attemptnumber = smx.maxattempt
s.latest = 1
LEFT JOIN {assign_grades} g ON
u.id = g.userid AND
g.assignment = :assignmentid2 AND
g.attemptnumber = gmx.maxattempt
g.attemptnumber = s.attemptnumber
LEFT JOIN {assign_user_flags} uf ON u.id = uf.userid AND uf.assignment = :assignmentid3';
$userparams = array();

View File

@ -388,11 +388,6 @@ function assign_print_overview($courses, &$htmlarray) {
$context = context_module::instance($assignment->coursemodule);
if (has_capability('mod/assign:grade', $context)) {
if (!isset($unmarkedsubmissions)) {
$submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt, mxs.assignment
FROM {assign_submission} mxs
WHERE mxs.assignment ' . $sqlassignmentids . '
GROUP BY mxs.userid, mxs.assignment';
// Build up and array of unmarked submissions indexed by assignment id/ userid
// for use where the user has grading rights on assignment.
$dbparams = array_merge($assignmentidparams, array(ASSIGN_SUBMISSION_STATUS_SUBMITTED), $assignmentidparams);
@ -403,19 +398,16 @@ function assign_print_overview($courses, &$htmlarray) {
s.status as status,
g.timemodified as timegraded
FROM {assign_submission} s
LEFT JOIN ( ' . $submissionmaxattempt . ' ) smx ON
smx.userid = s.userid AND
smx.assignment = s.id
LEFT JOIN {assign_grades} g ON
s.userid = g.userid AND
s.assignment = g.assignment AND
g.attemptnumber = smx.maxattempt
g.attemptnumber = s.attemptnumber
WHERE
( g.timemodified is NULL OR
s.timemodified > g.timemodified ) AND
s.timemodified IS NOT NULL AND
s.status = ? AND
s.attemptnumber = smx.maxattempt AND
s.latest = 1 AND
s.assignment ' . $sqlassignmentids, $dbparams);
$unmarkedsubmissions = array();
@ -447,15 +439,8 @@ function assign_print_overview($courses, &$htmlarray) {
if (has_capability('mod/assign:submit', $context)) {
if (!isset($mysubmissions)) {
// This is nasty because we only want the last attempt.
$submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt, mxs.assignment
FROM {assign_submission} mxs
WHERE mxs.assignment ' . $sqlassignmentids . '
AND mxs.userid = ?
GROUP BY mxs.userid, mxs.assignment';
// Get all user submissions, indexed by assignment id.
$dbparams = array_merge($assignmentidparams, array($USER->id, $USER->id, $USER->id), $assignmentidparams);
$dbparams = array_merge(array($USER->id, $USER->id), $assignmentidparams);
$mysubmissions = $DB->get_records_sql('SELECT
a.id AS assignment,
a.nosubmissions AS nosubmissions,
@ -464,14 +449,12 @@ function assign_print_overview($courses, &$htmlarray) {
g.grade AS grade,
s.status AS status
FROM {assign} a
LEFT JOIN ( ' . $submissionmaxattempt . ' ) smx ON
smx.assignment = a.id
LEFT JOIN {assign_grades} g ON
g.assignment = a.id AND
g.userid = ? AND
g.attemptnumber = smx.maxattempt
g.attemptnumber = s.attemptnumber
LEFT JOIN {assign_submission} s ON
s.attemptnumber = smx.maxattempt AND
s.latest = 1 AND
s.assignment = a.id AND
s.userid = ?
WHERE a.id ' . $sqlassignmentids, $dbparams);
@ -530,6 +513,7 @@ function assign_print_recent_activity($course, $viewfullnames, $timestart) {
JOIN {modules} md ON md.id = cm.module
JOIN {user} u ON u.id = asb.userid
WHERE asb.timemodified > ? AND
asb.latest = 1 AND
a.course = ? AND
md.name = ?
ORDER BY asb.timemodified ASC", $dbparams)) {

View File

@ -1447,29 +1447,18 @@ class assign {
$currentgroup = groups_get_activity_group($this->get_course_module(), true);
list($esql, $params) = get_enrolled_sql($this->get_context(), 'mod/assign:submit', $currentgroup, true);
$submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt
FROM {assign_submission} mxs
WHERE mxs.assignment = :assignid2 GROUP BY mxs.userid';
$grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
FROM {assign_grades} mxg
WHERE mxg.assignment = :assignid3 GROUP BY mxg.userid';
$params['assignid'] = $this->get_instance()->id;
$params['assignid2'] = $this->get_instance()->id;
$params['assignid3'] = $this->get_instance()->id;
$params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
$sql = 'SELECT COUNT(s.userid)
FROM {assign_submission} s
LEFT JOIN ( ' . $submissionmaxattempt . ' ) smx ON s.userid = smx.userid
LEFT JOIN ( ' . $grademaxattempt . ' ) gmx ON s.userid = gmx.userid
LEFT JOIN {assign_grades} g ON
s.assignment = g.assignment AND
s.userid = g.userid AND
g.attemptnumber = gmx.maxattempt
g.attemptnumber = s.attemptnumber
JOIN(' . $esql . ') e ON e.id = s.userid
WHERE
s.attemptnumber = smx.maxattempt AND
s.latest = 1 AND
s.assignment = :assignid AND
s.timemodified IS NOT NULL AND
s.status = :submitted AND
@ -1563,15 +1552,10 @@ class assign {
$params['submissionstatus'] = $status;
if ($this->get_instance()->teamsubmission) {
$maxattemptsql = 'SELECT mxs.groupid, MAX(mxs.attemptnumber) AS maxattempt
FROM {assign_submission} mxs
WHERE mxs.assignment = :assignid2 GROUP BY mxs.groupid';
$sql = 'SELECT COUNT(s.groupid)
FROM {assign_submission} s
JOIN(' . $maxattemptsql . ') smx ON s.groupid = smx.groupid
WHERE
s.attemptnumber = smx.maxattempt AND
s.latest = 1 AND
s.assignment = :assignid AND
s.timemodified IS NOT NULL AND
s.userid = :groupuserid AND
@ -1585,9 +1569,8 @@ class assign {
$sql = 'SELECT COUNT(s.userid)
FROM {assign_submission} s
JOIN(' . $esql . ') e ON e.id = s.userid
JOIN(' . $maxattemptsql . ') smx ON s.userid = smx.userid
WHERE
s.attemptnumber = smx.maxattempt AND
s.latest = 1 AND
s.assignment = :assignid AND
s.timemodified IS NOT NULL AND
s.status = :submissionstatus';
@ -6944,18 +6927,9 @@ class assign {
$where = ' WHERE u.id != :userid ';
}
$submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt
FROM {assign_submission} mxs
WHERE mxs.assignment = :assignid1 GROUP BY mxs.userid';
$grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
FROM {assign_grades} mxg
WHERE mxg.assignment = :assignid2 GROUP BY mxg.userid';
// When the gradebook asks us for grades - only return the last attempt for each user.
$params = array('assignid1'=>$assignmentid,
'assignid2'=>$assignmentid,
'assignid3'=>$assignmentid,
'assignid4'=>$assignmentid,
'userid'=>$userid);
$graderesults = $DB->get_recordset_sql('SELECT
u.id as userid,
@ -6964,14 +6938,12 @@ class assign {
g.timemodified as dategraded,
g.grader as usermodified
FROM {user} u
LEFT JOIN ( ' . $submissionmaxattempt . ' ) smx ON u.id = smx.userid
LEFT JOIN ( ' . $grademaxattempt . ' ) gmx ON u.id = gmx.userid
LEFT JOIN {assign_submission} s
ON u.id = s.userid and s.assignment = :assignid3 AND
s.attemptnumber = smx.maxattempt
ON u.id = s.userid and s.assignment = :assignid1 AND
s.latest = 1
JOIN {assign_grades} g
ON u.id = g.userid and g.assignment = :assignid4 AND
g.attemptnumber = gmx.maxattempt' .
ON u.id = g.userid and g.assignment = :assignid2 AND
g.attemptnumber = s.attemptnumber' .
$where, $params);
foreach ($graderesults as $result) {