diff --git a/mod/quiz/report/overview/overview_table.php b/mod/quiz/report/overview/overview_table.php index 938d440258a..94a264a3c9e 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 ebc188f5f6b..4a61787931c 100644 --- a/mod/quiz/report/reportlib.php +++ b/mod/quiz/report/reportlib.php @@ -10,32 +10,46 @@ 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; - } - return $gradedstatesbyattempt; - } else { - return $gradedstates; - } + } 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(); + } + $gradedstatesbyattempt[$gradedstate->attempt][$gradedstate->question] = $gradedstate; + } + return $gradedstatesbyattempt; + } else { + return $gradedstates; + } } function quiz_get_average_grade_for_questions($quiz, $userids){