Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

gradebook MDL-20946 fixed average calculation where users have multip…

…le gradeable roles
  • Loading branch information...
commit 656d17c2c6fcce33a414e6930eada6d9edf26242 1 parent 3d3ead8
@andyjdavis andyjdavis authored stronk7 committed
Showing with 95 additions and 84 deletions.
  1. +27 −13 grade/lib.php
  2. +43 −50 grade/report/grader/lib.php
  3. +25 −21 grade/report/user/lib.php
View
40 grade/lib.php
@@ -85,10 +85,16 @@ public function init() {
return false;
}
+ $coursecontext = get_context_instance(CONTEXT_COURSE, $this->course->id);
+ $relatedcontexts = get_related_contexts_string($coursecontext);
+
list($gradebookroles_sql, $params) =
$DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
- $relatedcontexts = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $this->course->id));
+ //limit to users with an active enrolment
+ list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext);
+
+ $params = array_merge($params, $enrolledparams);
if ($this->groupid) {
$groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
@@ -123,31 +129,39 @@ public function init() {
// $params contents: gradebookroles and groupid (for $groupwheresql)
$users_sql = "SELECT u.* $ofields
FROM {user} u
- INNER JOIN {role_assignments} ra ON u.id = ra.userid
+ JOIN ($enrolledsql) je ON je.id = u.id
$groupsql
- WHERE u.deleted=0
- AND ra.roleid $gradebookroles_sql
- AND ra.contextid $relatedcontexts
+ JOIN (
+ SELECT DISTINCT ra.userid
+ FROM {role_assignments} ra
+ WHERE ra.roleid $gradebookroles_sql
+ AND ra.contextid $relatedcontexts
+ ) rainner ON rainner.userid = u.id
+ WHERE u.deleted = 0
$groupwheresql
ORDER BY $order";
-
$this->users_rs = $DB->get_recordset_sql($users_sql, $params);
if (!empty($this->grade_items)) {
$itemids = array_keys($this->grade_items);
list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items0');
$params = array_merge($params, $grades_params);
+ // $params contents: gradebookroles, enrolledparams, groupid (for $groupwheresql) and itemids
- // $params contents: gradebookroles, groupid (for $groupwheresql) and itemids
$grades_sql = "SELECT g.* $ofields
FROM {grade_grades} g
- INNER JOIN {user} u ON g.userid = u.id
- INNER JOIN {role_assignments} ra ON u.id = ra.userid
+ JOIN {user} u ON g.userid = u.id
+ JOIN ($enrolledsql) je ON je.id = u.id
$groupsql
- WHERE ra.roleid $gradebookroles_sql
- AND ra.contextid $relatedcontexts
- $groupwheresql
- AND g.itemid $itemidsql
+ JOIN (
+ SELECT DISTINCT ra.userid
+ FROM {role_assignments} ra
+ WHERE ra.roleid $gradebookroles_sql
+ AND ra.contextid $relatedcontexts
+ ) rainnner ON rainner.userid = u.id
+ WHERE u.deleted = 0
+ AND g.itemid $itemidsql
+ $groupwheresql
ORDER BY $order, g.itemid ASC";
$this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
} else {
View
93 grade/report/grader/lib.php
@@ -342,55 +342,47 @@ public function load_users() {
//fields we need from the user table
$userfields = user_picture::fields('u', array('idnumber'));
+ $sortjoin = $sort = $params = null;
+
//if the user has clicked one of the sort asc/desc arrows
if (is_numeric($this->sortitemid)) {
$params = array_merge(array('gitemid'=>$this->sortitemid), $gradebookrolesparams, $this->groupwheresql_params, $enrolledparams);
- // the MAX() magic is required in order to please PG
- $sort = "MAX(g.finalgrade) $this->sortorder";
- $sql = "SELECT $userfields
- FROM {user} u
- JOIN ($enrolledsql) je
- ON je.id = u.id
- JOIN {role_assignments} ra
- ON ra.userid = u.id
- $this->groupsql
- LEFT JOIN {grade_grades} g
- ON (g.userid = u.id AND g.itemid = :gitemid)
- WHERE ra.roleid $gradebookrolessql
- AND u.deleted = 0
- AND ra.contextid ".get_related_contexts_string($this->context)."
- $this->groupwheresql
- GROUP BY $userfields
- ORDER BY $sort";
+ $sortjoin = "LEFT JOIN {grade_grades} g ON g.userid = u.id AND g.itemid = $this->sortitemid";
+ $sort = "g.finalgrade $this->sortorder";
} else {
+ $sortjoin = '';
switch($this->sortitemid) {
case 'lastname':
- $sort = "u.lastname $this->sortorder, u.firstname $this->sortorder"; break;
+ $sort = "u.lastname $this->sortorder, u.firstname $this->sortorder";
+ break;
case 'firstname':
- $sort = "u.firstname $this->sortorder, u.lastname $this->sortorder"; break;
+ $sort = "u.firstname $this->sortorder, u.lastname $this->sortorder";
+ break;
case 'idnumber':
default:
- $sort = "u.idnumber $this->sortorder"; break;
+ $sort = "u.idnumber $this->sortorder";
+ break;
}
$params = array_merge($gradebookrolesparams, $this->groupwheresql_params, $enrolledparams);
-
- $sql = "SELECT DISTINCT $userfields
- FROM {user} u
- JOIN ($enrolledsql) je
- ON je.id = u.id
- JOIN {role_assignments} ra
- ON u.id = ra.userid
- $this->groupsql
- WHERE ra.roleid $gradebookrolessql
- AND u.deleted = 0
- AND ra.contextid ".get_related_contexts_string($this->context)."
- $this->groupwheresql
- ORDER BY $sort";
}
+ $sql = "SELECT $userfields
+ FROM {user} u
+ JOIN ($enrolledsql) je ON je.id = u.id
+ $this->groupsql
+ $sortjoin
+ JOIN (
+ SELECT DISTINCT ra.userid
+ FROM {role_assignments} ra
+ WHERE ra.roleid IN ($this->gradebookroles)
+ AND ra.contextid " . get_related_contexts_string($this->context) . "
+ ) rainner ON rainner.userid = u.id
+ AND u.deleted = 0
+ $this->groupwheresql
+ ORDER BY $sort";
$this->users = $DB->get_records_sql($sql, $params, $this->get_pref('studentsperpage') * $this->page, $this->get_pref('studentsperpage'));
@@ -1294,25 +1286,25 @@ public function get_right_avg_row($rows=array(), $grouponly=false) {
$params = array_merge(array('courseid'=>$this->courseid), $gradebookrolesparams, $enrolledparams, $groupwheresqlparams);
// find sums of all grade items in course
- $SQL = "SELECT g.itemid, SUM(g.finalgrade) AS sum
+ $sql = "SELECT g.itemid, SUM(g.finalgrade) AS sum
FROM {grade_items} gi
- JOIN {grade_grades} g
- ON g.itemid = gi.id
- JOIN {user} u
- ON u.id = g.userid
- JOIN ($enrolledsql) je
- ON je.id = u.id
- JOIN {role_assignments} ra
- ON ra.userid = u.id
+ JOIN {grade_grades} g ON g.itemid = gi.id
+ JOIN {user} u ON u.id = g.userid
+ JOIN ($enrolledsql) je ON je.id = u.id
+ JOIN (
+ SELECT DISTINCT ra.userid
+ FROM {role_assignments} ra
+ WHERE ra.roleid $gradebookrolessql
+ AND ra.contextid " . get_related_contexts_string($this->context) . "
+ ) rainner ON rainner.userid = u.id
$groupsql
WHERE gi.courseid = :courseid
- AND ra.roleid $gradebookrolessql
- AND ra.contextid ".get_related_contexts_string($this->context)."
- AND g.finalgrade IS NOT NULL
- $groupwheresql
- GROUP BY g.itemid";
+ AND u.deleted = 0
+ AND g.finalgrade IS NOT NULL
+ $groupwheresql
+ GROUP BY g.itemid";
$sumarray = array();
- if ($sums = $DB->get_records_sql($SQL, $params)) {
+ if ($sums = $DB->get_records_sql($sql, $params)) {
foreach ($sums as $itemid => $csum) {
$sumarray[$itemid] = $csum->sum;
}
@@ -1320,7 +1312,7 @@ public function get_right_avg_row($rows=array(), $grouponly=false) {
// MDL-10875 Empty grades must be evaluated as grademin, NOT always 0
// This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table)
- $SQL = "SELECT gi.id, COUNT(u.id) AS count
+ $sql = "SELECT gi.id, COUNT(DISTINCT u.id) AS count
FROM {grade_items} gi
CROSS JOIN {user} u
JOIN ($enrolledsql) je
@@ -1333,11 +1325,12 @@ public function get_right_avg_row($rows=array(), $grouponly=false) {
WHERE gi.courseid = :courseid
AND ra.roleid $gradebookrolessql
AND ra.contextid ".get_related_contexts_string($this->context)."
+ AND u.deleted = 0
AND g.id IS NULL
$groupwheresql
GROUP BY gi.id";
- $ungradedcounts = $DB->get_records_sql($SQL, $params);
+ $ungradedcounts = $DB->get_records_sql($sql, $params);
$avgrow = new html_table_row();
$avgrow->attributes['class'] = 'avg';
View
46 grade/report/user/lib.php
@@ -618,19 +618,21 @@ function calculate_averages() {
// find sums of all grade items in course
$sql = "SELECT gg.itemid, SUM(gg.finalgrade) AS sum
FROM {grade_items} gi
- JOIN {grade_grades} gg
- ON gg.itemid = gi.id
- JOIN ($enrolledsql) je
- ON je.id = gg.userid
- JOIN {role_assignments} ra
- ON ra.userid = gg.userid
+ JOIN {grade_grades} gg ON gg.itemid = gi.id
+ JOIN {user} u ON u.id = gg.userid
+ JOIN ($enrolledsql) je ON je.id = gg.userid
+ JOIN (
+ SELECT DISTINCT ra.userid
+ FROM {role_assignments} ra
+ WHERE ra.roleid $gradebookrolessql
+ AND ra.contextid " . get_related_contexts_string($this->context) . "
+ ) rainner ON rainner.userid = u.id
$groupsql
WHERE gi.courseid = :courseid
- AND ra.roleid $gradebookrolessql
- AND ra.contextid ".get_related_contexts_string($this->context)."
- AND gg.finalgrade IS NOT NULL
- AND gg.hidden = 0
- $groupwheresql
+ AND u.deleted = 0
+ AND gg.finalgrade IS NOT NULL
+ AND gg.hidden = 0
+ $groupwheresql
GROUP BY gg.itemid";
$sum_array = array();
@@ -649,19 +651,21 @@ function calculate_averages() {
$sql = "SELECT gi.id, COUNT(u.id) AS count
FROM {grade_items} gi
JOIN {user} u
- JOIN ($enrolledsql) je
- ON je.id = u.id
- JOIN {role_assignments} ra
- ON ra.userid = u.id
+ JOIN ($enrolledsql) je ON je.id = u.id
+ JOIN (
+ SELECT DISTINCT ra.userid
+ FROM {role_assignments} ra
+ WHERE ra.roleid $gradebookrolessql
+ AND ra.contextid " . get_related_contexts_string($this->context) . "
+ ) rainner ON rainner.userid = u.id
LEFT JOIN {grade_grades} gg
ON (gg.itemid = gi.id AND gg.userid = u.id AND gg.finalgrade IS NOT NULL AND gg.hidden = 0)
$groupsql
- WHERE gi.courseid = :courseid
- AND ra.roleid $gradebookrolessql
- AND ra.contextid ".get_related_contexts_string($this->context)."
- AND gg.finalgrade IS NULL
- $groupwheresql
- GROUP BY gi.id";
+ WHERE gi.courseid = :courseid
+ AND u.deleted = 0
+ AND gg.finalgrade IS NULL
+ $groupwheresql
+ GROUP BY gi.id";
$ungraded_counts = $DB->get_records_sql($sql, $params);
Please sign in to comment.
Something went wrong with that request. Please try again.