]> git.mjollnir.org Git - moodle.git/commitdiff
This is a massive speed increase in journal_get_users_done.
authorgregb_cc <gregb_cc>
Wed, 22 Oct 2003 20:06:44 +0000 (20:06 +0000)
committergregb_cc <gregb_cc>
Wed, 22 Oct 2003 20:06:44 +0000 (20:06 +0000)
The fix requires UNION support in the database.  I've added a simple little
test to see if UNION queries work.

This change very much needs to be tested out by someone using MySQL 3.  If
it tests out there successfully, the supports_union code should be moved out
of this file and into datalib.  Ideally, someone will extend that test and
creatle get_records_union_sql, a function that would split up UNION queries
for databases that don't support them and do an array_merge (and proper
sorting, which is missing from my fix) on the result sets.

mod/journal/lib.php

index 65a7809b5b7118e3bbea2057d69c552d953936f9..b64b502a5e84753ad610c14c9752f75b0f128081 100644 (file)
@@ -283,19 +283,61 @@ function journal_get_participants($journalid) {
 }
 
 // SQL FUNCTIONS ///////////////////////////////////////////////////////////////////
+function supports_union(){
+    # not all databases support UNION.  Adodb doesn't seem to keep track of which do.
+    # this function should probably be moved to datalib once it has been thouroughly tested.
+    $qry = "SELECT 'a' as a UNION SELECT 'b' as a'";
+    $test = get_records_sql($qry);
+    return ($test['b']->a = 'b');
+    }
 
 function journal_get_users_done($journal) {
     global $CFG;
-    return get_records_sql("SELECT u.* 
-                              FROM {$CFG->prefix}user u, 
-                                   {$CFG->prefix}user_students s, 
-                                   {$CFG->prefix}user_teachers t, 
-                                   {$CFG->prefix}journal_entries j
-                             WHERE ((s.course = '$journal->course' AND s.userid = u.id) 
-                                OR  (t.course = '$journal->course' AND t.userid = u.id))
-                               AND u.id = j.userid 
-                               AND j.journal = '$journal->id'
-                          ORDER BY j.modified DESC");
+    if(supports_union()) {
+        # Yay!  The database supports UNION, so we can use this fast query
+        return get_records_sql ("(SELECT u.*, j.modified
+                                  FROM {$CFG->prefix}journal_entries j,
+                                       {$CFG->prefix}user u, 
+                                       {$CFG->prefix}user_students s
+                                 WHERE j.userid = u.id
+                                   AND s.userid = u.id 
+                                   AND j.journal = $journal->id
+                                   AND s.course = $journal->course)
+
+                                UNION
+                                              
+                                (SELECT u.*, j.modified
+                                  FROM {$CFG->prefix}journal_entries j,
+                                       {$CFG->prefix}user u, 
+                                       {$CFG->prefix}user_teachers t
+                                 WHERE j.userid = u.id
+                                   AND t.userid = u.id 
+                                   AND j.journal = $journal->id
+                                   AND t.course = $journal->course)
+                              ORDER BY j.modified DESC");
+    } else {
+        # Poo!  The database doesn't support UNION, so we are going to use this ugly hack
+        $s_journals = get_records_sql ("SELECT u.*
+                                  FROM {$CFG->prefix}journal_entries j,
+                                       {$CFG->prefix}user u, 
+                                       {$CFG->prefix}user_students s
+                                 WHERE j.userid = u.id
+                                   AND s.userid = u.id 
+                                   AND j.journal = $journal->id
+                                   AND s.course = $journal->course
+                              ORDER BY j.modified DESC");
+
+        $t_journals = get_records_sql ("SELECT u.*
+                                  FROM {$CFG->prefix}journal_entries j,
+                                       {$CFG->prefix}user u, 
+                                       {$CFG->prefix}user_teachers t
+                                 WHERE j.userid = u.id
+                                   AND t.userid = u.id 
+                                   AND j.journal = $journal->id
+                                   AND t.course = $journal->course)
+                              ORDER BY j.modified DESC");
+        return(array_merge($s_journals, $t_journals));
+    }
 }
 
 function journal_get_unmailed_graded($cutofftime) {