From 8b0d254f0562330846f1a28d79e86866b0ce148f Mon Sep 17 00:00:00 2001 From: John Beedell Date: Thu, 5 Jan 2017 14:34:41 +0000 Subject: [PATCH] MDL-57511 Quiz: Attempts report shows non-unique debugging --- lib/enrollib.php | 4 ++++ mod/quiz/report/attemptsreport_table.php | 3 ++- mod/quiz/report/overview/tests/report_test.php | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/enrollib.php b/lib/enrollib.php index 452a8991ddc..8bb17af486c 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -1179,6 +1179,10 @@ function is_enrolled(context $context, $user = null, $withcapability = '', $only * Returns an array of joins, wheres and params that will limit the group of * users to only those enrolled and with given capability (if specified). * + * Note this join will return duplicate rows for users who have been enrolled + * several times (e.g. as manual enrolment, and as self enrolment). You may + * need to use a SELECT DISTINCT in your query (see get_enrolled_sql for example). + * * @param context $context * @param string $prefix optional, a prefix to the user id column * @param string|array $capability optional, may include a capability name, or array of names. diff --git a/mod/quiz/report/attemptsreport_table.php b/mod/quiz/report/attemptsreport_table.php index 87e31bcac95..539df5af3d3 100644 --- a/mod/quiz/report/attemptsreport_table.php +++ b/mod/quiz/report/attemptsreport_table.php @@ -389,7 +389,8 @@ abstract class quiz_attempts_report_table extends table_sql { public function base_sql(\core\dml\sql_join $allowedstudentsjoins) { global $DB; - $fields = $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') . ' AS uniqueid,'; + // Please note this uniqueid column is not the same as quiza.uniqueid. + $fields = 'DISTINCT ' . $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') . ' AS uniqueid,'; if ($this->qmsubselect) { $fields .= "\n(CASE WHEN $this->qmsubselect THEN 1 ELSE 0 END) AS gradedattempt,"; diff --git a/mod/quiz/report/overview/tests/report_test.php b/mod/quiz/report/overview/tests/report_test.php index 46cdafcfd35..79c19282eab 100644 --- a/mod/quiz/report/overview/tests/report_test.php +++ b/mod/quiz/report/overview/tests/report_test.php @@ -90,6 +90,14 @@ class quiz_overview_report_testcase extends advanced_testcase { $DB->insert_record('quiz_attempts', $data); } + // This line is not really necessary for the test asserts below, + // but what it does is add an extra user row returned by + // get_enrolled_with_capabilities_join because of a second enrolment. + // The extra row returned used to make $table->query_db complain + // about duplicate records. So this is really a test that an extra + // student enrolment does not cause duplicate records in this query. + $generator->enrol_user($student2->id, $course->id, null, 'self'); + // Actually getting the SQL to run is quite hard. Do a minimal set up of // some objects. $context = context_module::instance($quiz->cmid);