]> git.mjollnir.org Git - moodle.git/commitdiff
Moved Penny's funky sql function into dmllib.php
authormoodler <moodler>
Tue, 26 Sep 2006 04:33:59 +0000 (04:33 +0000)
committermoodler <moodler>
Tue, 26 Sep 2006 04:33:59 +0000 (04:33 +0000)
course/report/participation/index.php
lib/dmllib.php
lib/statslib.php

index 3172794255c623a21ac2620587348eee901f3320..73cd187f317ca453484a19ad5b7db783df3b4b0c 100644 (file)
         $table->setup();
         
 
-        $primary_roles = stats_get_primary_role_subselect();
+        $primary_roles = sql_primary_role_subselect();   // In dmllib.php
         $sql = 'SELECT DISTINCT prs.userid, u.firstname,u.lastname,u.idnumber,count(l.action) as count FROM ('.$primary_roles.') prs'
             .' JOIN '.$CFG->prefix.'user u ON u.id = prs.userid LEFT JOIN '.$CFG->prefix.'log l ON prs.userid = l.userid '
             .' AND prs.courseid = l.course AND l.time > '.$timefrom.' AND l.course = '.$course->id.' AND l.module = \''.$module->name.'\' '
index 34aee54754c5c3206be4e55af2c0f0bb439599ee..f09ee9f5a78a69e1235e0ab9ac2ddbb655dc1feb 100644 (file)
@@ -1308,6 +1308,43 @@ function sql_order_by_text($fieldname, $numchars=32) {
     }
 }
 
+
+/**
+ * Returns SQL to be used as a subselect to find the primary role of users.  
+ * Penny (the author) is very keen for this to be implemented as a view in 
+ * future versions.
+ *
+ * eg if this function returns a string called $primaryroles, then you could:
+ * $sql = 'SELECT COUNT(DISTINCT prs.userid) FROM ('.$primary_roles.') prs 
+ *          WHERE prs.primary_roleid='.$role->id.' AND prs.courseid='.$course->id.
+ *          ' AND prs.contextlevel = '.CONTEXT_COURSE;
+ *
+ * @return string the piece of SQL code to be used in your FROM( ) statement.
+ */
+function sql_primary_role_subselect() {
+    global $CFG;
+    return 'SELECT ra.userid,
+                ra.roleid AS primary_roleid,
+                ra.contextid,
+                r.sortorder,
+                r.name,
+                r.description,
+                r.shortname,
+                c.instanceid AS courseid,
+                c.contextlevel
+            FROM '.$CFG->prefix.'role_assignments ra
+            INNER JOIN '.$CFG->prefix.'role r ON ra.roleid = r.id
+            INNER JOIN '.$CFG->prefix.'context c ON ra.contextid = c.id
+            WHERE NOT EXISTS ( 
+                              SELECT 1
+                              FROM '.$CFG->prefix.'role_assignments i_ra
+                              INNER JOIN '.$CFG->prefix.'role i_r ON i_ra.roleid = i_r.id
+                              WHERE ra.userid = i_ra.userid AND 
+                                     ra.contextid = i_ra.contextid AND 
+                                     i_r.sortorder < r.sortorder
+                              ) ';
+}
+
 /**
  * Prepare a SQL WHERE clause to select records where the given fields match the given values.
  *
index 7383fb33d480f35f0de6ee1eac952808d64db4a2..fdb1937039d5283b5e88eac677f5b911a9ef9c9a 100644 (file)
@@ -138,7 +138,7 @@ function stats_cron_daily () {
                 continue;
             }
             
-            $primary_roles = stats_get_primary_role_subselect();
+            $primary_roles = sql_primary_role_subselect();  // In dmllib.php
             foreach ($roles as $role) {
                 // ENROLMENT FIRST....
                 // ALL users with this role...
@@ -1315,28 +1315,4 @@ function stats_upgrade_table_for_roles ($period) {
     return true;
 }
 
-function stats_get_primary_role_subselect() {
-    global $CFG;
-    return 'SELECT ra.userid,
-                ra.roleid AS primary_roleid,
-                ra.contextid,
-                r.sortorder,
-                r.name,
-                r.description,
-                r.shortname,
-                c.instanceid AS courseid,
-                c.contextlevel
-            FROM '.$CFG->prefix.'role_assignments ra
-            INNER JOIN '.$CFG->prefix.'role r ON ra.roleid = r.id
-            INNER JOIN '.$CFG->prefix.'context c ON ra.contextid = c.id
-            WHERE NOT EXISTS ( 
-                              SELECT 1
-                              FROM '.$CFG->prefix.'role_assignments i_ra
-                              INNER JOIN '.$CFG->prefix.'role i_r ON i_ra.roleid = i_r.id
-                              WHERE ra.userid = i_ra.userid AND 
-                                     ra.contextid = i_ra.contextid AND 
-                                     i_r.sortorder < r.sortorder
-                              ) ';
-}
-
 ?>