From 67d4dfb84bb6a8ceebdf700817923a10d2a4d5ad Mon Sep 17 00:00:00 2001 From: jamiesensei Date: Sat, 5 Jul 2008 05:57:22 +0000 Subject: [PATCH] MDL-15545 "sql to fetch question states in overview report should use a JOIN instead of passing attempt ids in IN clause when downloading data with no paging" --- mod/quiz/report/overview/overview_table.php | 14 ++++--- mod/quiz/report/reportlib.php | 42 ++++++++++++++------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/mod/quiz/report/overview/overview_table.php b/mod/quiz/report/overview/overview_table.php index 938d440258..94a264a3c9 100644 --- a/mod/quiz/report/overview/overview_table.php +++ b/mod/quiz/report/overview/overview_table.php @@ -177,13 +177,17 @@ class quiz_report_overview_table extends table_sql { if ($gradedstatesbyattempt === null){ //get all the attempt ids we want to display on this page //or to export for download. - $attemptids = array(); - foreach ($this->rawdata as $attempt){ - if ($attempt->attemptuniqueid > 0){ - $attemptids[] = $attempt->attemptuniqueid; + if (!$this->is_downloading()) { + $attemptids = array(); + foreach ($this->rawdata as $attempt){ + if ($attempt->attemptuniqueid > 0){ + $attemptids[] = $attempt->attemptuniqueid; + } } + $gradedstatesbyattempt = quiz_get_newgraded_states($attemptids, true, 'qs.id, qs.grade, qs.event, qs.question, qs.attempt'); + } else { + $gradedstatesbyattempt = quiz_get_newgraded_states($this->sql, true, 'qs.id, qs.grade, qs.event, qs.question, qs.attempt'); } - $gradedstatesbyattempt = quiz_get_newgraded_states($attemptids, true, 'qs.id, qs.grade, qs.event, qs.question, qs.attempt'); } if (preg_match('/^qsgrade([0-9]+)$/', $colname, $matches)){ $questionid = $matches[1]; diff --git a/mod/quiz/report/reportlib.php b/mod/quiz/report/reportlib.php index ebc188f5f6..4a61787931 100644 --- a/mod/quiz/report/reportlib.php +++ b/mod/quiz/report/reportlib.php @@ -10,31 +10,45 @@ define('QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS', 3); * Get newest graded state or newest state for a number of attempts. Pass in the * uniqueid field from quiz_attempt table not the id. Use question_state_is_graded * function to check that the question is actually graded. + * @param array attemptidssql either an array of attemptids with numerical keys + * or an object with properties from, where and params. + * @param boolean idxattemptq true if a multidimensional array should be + * constructed with keys indexing array first by attempt and then by question + * id. */ -function quiz_get_newgraded_states($attemptids, $idxattemptq = true, $fields='qs.*'){ +function quiz_get_newgraded_states($attemptidssql, $idxattemptq = true, $fields='qs.*'){ global $CFG, $DB; - if ($attemptids){ - list($usql, $params) = $DB->get_in_or_equal($attemptids); + if ($attemptidssql && is_array($attemptidssql)){ + list($usql, $params) = $DB->get_in_or_equal($attemptidssql); $gradedstatesql = "SELECT $fields FROM " . "{question_sessions} qns, " . "{question_states} qs " . "WHERE qns.attemptid $usql AND " . "qns.newgraded = qs.id"; $gradedstates = $DB->get_records_sql($gradedstatesql, $params); - if ($idxattemptq){ - $gradedstatesbyattempt = array(); - foreach ($gradedstates as $gradedstate){ - if (!isset($gradedstatesbyattempt[$gradedstate->attempt])){ - $gradedstatesbyattempt[$gradedstate->attempt] = array(); - } - $gradedstatesbyattempt[$gradedstate->attempt][$gradedstate->question] = $gradedstate; + } else if ($attemptidssql && is_object($attemptidssql)){ + $gradedstatesql = "SELECT $fields FROM " . + $attemptidssql->from.",". + "{question_sessions} qns, " . + "{question_states} qs " . + "WHERE qns.attemptid = qa.uniqueid AND " . + $attemptidssql->where." AND ". + "qns.newgraded = qs.id"; + $gradedstates = $DB->get_records_sql($gradedstatesql, $attemptidssql->params); + } else { + return array(); + } + if ($idxattemptq){ + $gradedstatesbyattempt = array(); + foreach ($gradedstates as $gradedstate){ + if (!isset($gradedstatesbyattempt[$gradedstate->attempt])){ + $gradedstatesbyattempt[$gradedstate->attempt] = array(); } - return $gradedstatesbyattempt; - } else { - return $gradedstates; + $gradedstatesbyattempt[$gradedstate->attempt][$gradedstate->question] = $gradedstate; } + return $gradedstatesbyattempt; } else { - return array(); + return $gradedstates; } } -- 2.39.5