Skip to content

Commit

Permalink
course-participation-report MDL-23603 Optimised SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Oct 18, 2010
1 parent d656dd9 commit 7d8e60f
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions course/report/participation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,35 +189,32 @@
}

list($actionsql, $params) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action0');
$actionsql = "l.action $actionsql";
$actionsql = "action $actionsql";

$relatedcontexts = get_related_contexts_string($context);

$sql = "SELECT ra.userid, u.firstname, u.lastname, u.idnumber, COUNT(l.action) AS count
FROM {role_assignments} ra
JOIN {user} u ON u.id = ra.userid
LEFT JOIN {log} l ON (l.userid = ra.userid AND l.cmid = :instanceid AND l.time > :timefrom AND $actionsql)
WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid";
$sql = "SELECT ra.userid, u.firstname, u.lastname, u.idnumber, l.actioncount AS count
FROM (SELECT * FROM {role_assignments} WHERE contextid $relatedcontexts AND roleid = :roleid ) ra
JOIN {user} u ON u.id = ra.userid
LEFT JOIN (
SELECT userid, COUNT(action) actioncount FROM {log} WHERE cmid = :instanceid AND time > :timefrom AND $actionsql GROUP BY userid
) l ON (l.userid = ra.userid)";
$params['roleid'] = $roleid;
$params['instanceid'] = $instanceid;
$params['timefrom'] = $timefrom;

list($twhere, $tparams) = $table->get_sql_where();
if ($twhere) {
$sql .= ' AND '.$twhere; //initial bar
$sql .= ' WHERE '.$twhere; //initial bar
$params = array_merge($params, $tparams);
}

$sql .= " GROUP BY ra.userid, u.firstname, u.lastname, u.idnumber";

if ($table->get_sql_sort()) {
$sql .= ' ORDER BY '.$table->get_sql_sort();
}

$countsql = "SELECT COUNT(DISTINCT(ra.userid))
FROM {role_assignments} ra
JOIN {user} u ON u.id = ra.userid
LEFT OUTER JOIN {log} l ON (l.userid = ra.userid AND l.cmid = :instanceid AND l.time > :timefrom AND $actionsql)
WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid";

$totalcount = $DB->count_records_sql($countsql, $params);
Expand Down

0 comments on commit 7d8e60f

Please sign in to comment.