mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-35127 mod_assign More efficient use of expensive database queries
Moving two relatively expensive DB queries so they are only executed when needed instead of on every function call. They will still only execute once, and not repeatedly during execution of the loop.
This commit is contained in:
parent
a09484cf77
commit
7ef965ce50
1 changed files with 35 additions and 32 deletions
|
@ -312,6 +312,24 @@ function assign_print_overview($courses, &$htmlarray) {
|
||||||
//
|
//
|
||||||
list($sqlassignmentids, $assignmentidparams) = $DB->get_in_or_equal($assignmentids);
|
list($sqlassignmentids, $assignmentidparams) = $DB->get_in_or_equal($assignmentids);
|
||||||
|
|
||||||
|
foreach ($assignments as $assignment) {
|
||||||
|
// Do not show assignments that are not open
|
||||||
|
if (!in_array($assignment->id, $assignmentids)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$str = '<div class="assign overview"><div class="name">'.$strassignment. ': '.
|
||||||
|
'<a '.($assignment->visible ? '':' class="dimmed"').
|
||||||
|
'title="'.$strassignment.'" href="'.$CFG->wwwroot.
|
||||||
|
'/mod/assign/view.php?id='.$assignment->coursemodule.'">'.
|
||||||
|
format_string($assignment->name).'</a></div>';
|
||||||
|
if ($assignment->duedate) {
|
||||||
|
$str .= '<div class="info">'.$strduedate.': '.userdate($assignment->duedate).'</div>';
|
||||||
|
} else {
|
||||||
|
$str .= '<div class="info">'.$strduedateno.'</div>';
|
||||||
|
}
|
||||||
|
$context = context_module::instance($assignment->coursemodule);
|
||||||
|
if (has_capability('mod/assign:grade', $context)) {
|
||||||
|
if (!isset($unmarkedsubmissions)) {
|
||||||
// Build up and array of unmarked submissions indexed by assignment id/ userid
|
// Build up and array of unmarked submissions indexed by assignment id/ userid
|
||||||
// for use where the user has grading rights on assignment.
|
// for use where the user has grading rights on assignment.
|
||||||
$dbparams = array_merge(array(ASSIGN_SUBMISSION_STATUS_SUBMITTED), $assignmentidparams);
|
$dbparams = array_merge(array(ASSIGN_SUBMISSION_STATUS_SUBMITTED), $assignmentidparams);
|
||||||
|
@ -337,31 +355,7 @@ function assign_print_overview($courses, &$htmlarray) {
|
||||||
$unmarkedsubmissions[$rd->assignment][$rd->userid] = $rd->id;
|
$unmarkedsubmissions[$rd->assignment][$rd->userid] = $rd->id;
|
||||||
}
|
}
|
||||||
$rs->close();
|
$rs->close();
|
||||||
|
|
||||||
|
|
||||||
// get all user submissions, indexed by assignment id
|
|
||||||
$mysubmissions = $DB->get_records_sql("SELECT a.id AS assignment, a.nosubmissions AS nosubmissions, g.timemodified AS timemarked, g.grader AS grader, g.grade AS grade, s.status AS status
|
|
||||||
FROM {assign} a LEFT JOIN {assign_grades} g ON g.assignment = a.id AND g.userid = ? LEFT JOIN {assign_submission} s ON s.assignment = a.id AND s.userid = ?
|
|
||||||
WHERE a.id $sqlassignmentids", array_merge(array($USER->id, $USER->id), $assignmentidparams));
|
|
||||||
|
|
||||||
foreach ($assignments as $assignment) {
|
|
||||||
// Do not show assignments that are not open
|
|
||||||
if (!in_array($assignment->id, $assignmentids)) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
$str = '<div class="assign overview"><div class="name">'.$strassignment. ': '.
|
|
||||||
'<a '.($assignment->visible ? '':' class="dimmed"').
|
|
||||||
'title="'.$strassignment.'" href="'.$CFG->wwwroot.
|
|
||||||
'/mod/assign/view.php?id='.$assignment->coursemodule.'">'.
|
|
||||||
format_string($assignment->name).'</a></div>';
|
|
||||||
if ($assignment->duedate) {
|
|
||||||
$str .= '<div class="info">'.$strduedate.': '.userdate($assignment->duedate).'</div>';
|
|
||||||
} else {
|
|
||||||
$str .= '<div class="info">'.$strduedateno.'</div>';
|
|
||||||
}
|
|
||||||
$context = context_module::instance($assignment->coursemodule);
|
|
||||||
if (has_capability('mod/assign:grade', $context)) {
|
|
||||||
|
|
||||||
// count how many people can submit
|
// count how many people can submit
|
||||||
$submissions = 0; // init
|
$submissions = 0; // init
|
||||||
if ($students = get_enrolled_users($context, 'mod/assign:view', 0, 'u.id')) {
|
if ($students = get_enrolled_users($context, 'mod/assign:view', 0, 'u.id')) {
|
||||||
|
@ -377,6 +371,15 @@ function assign_print_overview($courses, &$htmlarray) {
|
||||||
$str .= '<div class="details"><a href="'.$link.'">'.get_string('submissionsnotgraded', 'assign', $submissions).'</a></div>';
|
$str .= '<div class="details"><a href="'.$link.'">'.get_string('submissionsnotgraded', 'assign', $submissions).'</a></div>';
|
||||||
}
|
}
|
||||||
} if (has_capability('mod/assign:submit', $context)) {
|
} if (has_capability('mod/assign:submit', $context)) {
|
||||||
|
if (!isset($mysubmissions)) {
|
||||||
|
// get all user submissions, indexed by assignment id
|
||||||
|
$mysubmissions = $DB->get_records_sql("SELECT a.id AS assignment, a.nosubmissions AS nosubmissions, g.timemodified
|
||||||
|
AS timemarked, g.grader AS grader, g.grade AS grade, s.status AS status
|
||||||
|
FROM {assign} a LEFT JOIN {assign_grades} g ON g.assignment = a.id AND
|
||||||
|
g.userid = ? LEFT JOIN {assign_submission} s ON s.assignment = a.id AND
|
||||||
|
s.userid = ? WHERE a.id $sqlassignmentids",
|
||||||
|
array_merge(array($USER->id, $USER->id), $assignmentidparams));
|
||||||
|
}
|
||||||
$str .= '<div class="details">';
|
$str .= '<div class="details">';
|
||||||
$str .= get_string('mysubmission', 'assign');
|
$str .= get_string('mysubmission', 'assign');
|
||||||
$submission = $mysubmissions[$assignment->id];
|
$submission = $mysubmissions[$assignment->id];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue