Skip to content

Commit 9ca2c8d

Browse files
peterRdPeter
authored andcommitted
MDL-55997 core_reports: modify user stats query to fix duplicate rows
1 parent 078699a commit 9ca2c8d

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/statslib.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,11 +1196,13 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
11961196
break;
11971197

11981198
case STATS_REPORT_USER_VIEW:
1199-
$param->fields = 'statsreads as line1, statswrites as line2, statsreads+statswrites as line3';
1199+
$param->fields = 'timeend, SUM(statsreads) AS line1, SUM(statswrites) AS line2, SUM(statsreads+statswrites) AS line3';
1200+
$param->fieldscomplete = true;
12001201
$param->line1 = get_string('statsuserreads');
12011202
$param->line2 = get_string('statsuserwrites');
12021203
$param->line3 = get_string('statsuseractivity');
12031204
$param->stattype = 'activity';
1205+
$param->extras = "GROUP BY timeend";
12041206
break;
12051207

12061208
// ******************** STATS_MODE_RANKED ******************** //

report/stats/user.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,33 @@
124124

125125
$param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
126126
$params = $param->params;
127-
128127
$param->table = 'user_'.$param->table;
129128

130-
$sql = 'SELECT id, timeend,'.$param->fields.' FROM {stats_'.$param->table.'} WHERE '
131-
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
132-
.' userid = '.$user->id.' AND timeend >= '.$param->timeafter .$param->extras
133-
.' ORDER BY timeend DESC';
134-
$stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!
129+
// Build the conditions and parameters.
130+
$wheres = [
131+
"userid = :userid",
132+
"timeend >= :timeend",
133+
];
134+
$params['userid'] = $user->id;
135+
$params['timeend'] = $param->timeafter;
136+
// Add condition for course ID when specified.
137+
if ($course->id != SITEID) {
138+
$wheres[] = "courseid = :courseid";
139+
$params['courseid'] = $course->id;
140+
}
141+
// Combine the conditions.
142+
$wheresql = implode(" AND ", $wheres);
143+
144+
// Build the query.
145+
$sql = "
146+
SELECT {$param->fields}
147+
FROM {stats_{$param->table}}
148+
WHERE {$wheresql}
149+
{$param->extras}
150+
ORDER BY timeend DESC";
151+
152+
// Fetch the stats data.
153+
$stats = $DB->get_records_sql($sql, $params);
135154

136155
if (empty($stats)) {
137156
print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');

0 commit comments

Comments
 (0)