Skip to content

Commit

Permalink
MDL-55997 core_reports: modify user stats query to fix duplicate rows
Browse files Browse the repository at this point in the history
  • Loading branch information
peterRd authored and Peter committed Jun 12, 2019
1 parent 383aec8 commit 60eaa9b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/statslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,11 +1196,13 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
break;

case STATS_REPORT_USER_VIEW:
$param->fields = 'statsreads as line1, statswrites as line2, statsreads+statswrites as line3';
$param->fields = 'timeend, SUM(statsreads) AS line1, SUM(statswrites) AS line2, SUM(statsreads+statswrites) AS line3';
$param->fieldscomplete = true;
$param->line1 = get_string('statsuserreads');
$param->line2 = get_string('statsuserwrites');
$param->line3 = get_string('statsuseractivity');
$param->stattype = 'activity';
$param->extras = "GROUP BY timeend";
break;

// ******************** STATS_MODE_RANKED ******************** //
Expand Down
31 changes: 25 additions & 6 deletions report/stats/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,33 @@

$param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
$params = $param->params;

$param->table = 'user_'.$param->table;

$sql = 'SELECT id, timeend,'.$param->fields.' FROM {stats_'.$param->table.'} WHERE '
.(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
.' userid = '.$user->id.' AND timeend >= '.$param->timeafter .$param->extras
.' ORDER BY timeend DESC';
$stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!
// Build the conditions and parameters.
$wheres = [
"userid = :userid",
"timeend >= :timeend",
];
$params['userid'] = $user->id;
$params['timeend'] = $param->timeafter;
// Add condition for course ID when specified.
if ($course->id != SITEID) {
$wheres[] = "courseid = :courseid";
$params['courseid'] = $course->id;
}
// Combine the conditions.
$wheresql = implode(" AND ", $wheres);

// Build the query.
$sql = "
SELECT {$param->fields}
FROM {stats_{$param->table}}
WHERE {$wheresql}
{$param->extras}
ORDER BY timeend DESC";

// Fetch the stats data.
$stats = $DB->get_records_sql($sql, $params);

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

0 comments on commit 60eaa9b

Please sign in to comment.