]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15545 "sql to fetch question states in overview report should use a JOIN instead...
authorjamiesensei <jamiesensei>
Sat, 5 Jul 2008 05:57:22 +0000 (05:57 +0000)
committerjamiesensei <jamiesensei>
Sat, 5 Jul 2008 05:57:22 +0000 (05:57 +0000)
mod/quiz/report/overview/overview_table.php
mod/quiz/report/reportlib.php

index 938d440258af8ef5904471ee96a5bf31a5a283fb..94a264a3c9e39589c0679819e60f3a460e411773 100644 (file)
@@ -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];
index ebc188f5f6b11e14a7bf3baec24efdcb16814138..4a61787931c0a20431695d086174065f8d7e2321 100644 (file)
@@ -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;
     }
 }