]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14679 towards reports and stats conversion
authorskodak <skodak>
Sat, 31 May 2008 14:35:58 +0000 (14:35 +0000)
committerskodak <skodak>
Sat, 31 May 2008 14:35:58 +0000 (14:35 +0000)
16 files changed:
admin/report/backups/index.php
admin/report/courseoverview/index.php
admin/report/courseoverview/reportsgraph.php
admin/report/stats/index.php
course/report/log/graph.php
course/report/log/index.php
course/report/log/lib.php
course/report/log/live.php
course/report/outline/index.php
course/report/participation/index.php
course/report/stats/graph.php
course/report/stats/index.php
course/report/stats/report.php
lib/dml/moodle_database.php
lib/dml/postgres7_adodb_moodle_database.php
lib/statslib.php

index a332670fc0eee5166ce397a9da98eb891658c292..a4f52946d50c26f03f2901016b468790ed51d97f 100644 (file)
@@ -49,7 +49,7 @@
         print_heading($backuploglaststatus);
         print_simple_box_start('center');
     /// Now, get every record from backup_courses
-        $courses = get_records("backup_courses");
+        $courses = $DB->get_records("backup_courses");
 
         if (!$courses) {
             notify(get_string('nologsfound'));
         print_simple_box_start('center');
 
     /// First, me get all the distinct backups for that course in backup_log
-        $executions = get_records_sql("SELECT DISTINCT laststarttime,laststarttime
-                                       FROM {$CFG->prefix}backup_log
-                                       WHERE courseid = '$courseid'
-                                       ORDER BY laststarttime DESC");
+        $executions = $DB->get_records_sql("SELECT DISTINCT laststarttime,laststarttime
+                                              FROM {backup_log}
+                                             WHERE courseid = ?
+                                          ORDER BY laststarttime DESC", array($courseid));
 
     /// Iterate over backup executions
         if (!$executions) {
                 print_simple_box("<center>".userdate($execution->laststarttime)."</center>", "center");
                 echo "</td>";
                 echo "</tr>";
-                $logs = get_records_sql("SELECT *
-                                         FROM {$CFG->prefix}backup_log
-                                         WHERE courseid = '$courseid'  AND
-                                               laststarttime = '$execution->laststarttime'
-                                         ORDER BY id");
+                $logs = $DB->get_records_sql("SELECT *
+                                                FROM {backup_log}
+                                               WHERE courseid = ? AND laststarttime = ?
+                                            ORDER BY id", array($courseid, $execution->laststarttime));
                 if ($logs) {
                     foreach ($logs as $log) {
                         echo "<tr>";
index 3977d3ab9692718796b0afb2c01bfb07dbbb41aa..fbb922b588cc0c19a8db9ea2b9c32ca14d6a784b 100644 (file)
         if (!empty($param->sql)) {
             $sql = $param->sql;
         } else {
-            $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
-                ." WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0"
-                ." GROUP BY courseid "
-                .$param->extras
-                ." ORDER BY ".$param->orderby;
+            $sql = "SELECT courseid,".$param->fields."
+                      FROM {".'stats_'.$param->table."}
+                     WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0
+                  GROUP BY courseid
+                           $param->extras
+                  ORDER BY $param->orderby";
         }
-        error_log($sql);
+        //error_log($sql);
 
-        $courses = get_records_sql($sql, 0, $numcourses);
+        $courses = $DB->get_records_sql($sql, $pram->params, 0, $numcourses);
 
         if (empty($courses)) {
             notify(get_string('statsnodata'));echo '</td></tr></table>';echo '<p>after notify</p>';
index a0c71c0faad6a8f04f72c9d821f25c997d39db79..c81bb1906851a39f97c209fe60f71b986d484102 100644 (file)
     if (!empty($param->sql)) {
         $sql = $param->sql;
     } else {
-        $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
-            ." WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0"
-            ." GROUP BY courseid "
-            .$param->extras
-            ." ORDER BY ".$param->orderby;
+        $sql = "SELECT courseid, $param->fields
+                  FROM {".'stats_'.$param->table."}
+                 WHERE timeend >= $param->timeafter AND stattype = 'activity' AND roleid = 0
+              GROUP BY courseid
+                       $param->extras
+              ORDER BY $param->orderby";
     }
 
-    $courses = get_records_sql($sql, 0, $numcourses);
+    $courses = $DB->get_records_sql($sql, $pram->$prams, 0, $numcourses);
 
     if (empty($courses)) {
         print_error('statsnodata', 'error', $CFG->wwwroot.'/'.$CFG->admin.'/report/course/index.php');
index 0d857c3284479203b9b35bb1f08d1098c1129997..86ce469ff0b6ee3502efc977e7133a1fb71d227a 100644 (file)
         redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time, '', 3);
     }
 
-    if (!$course = get_record("course","id",$courseid)) {
+    if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
         print_error('invalidcourse', 'error');
     }
 
     if (!empty($userid)) {
-        if (!$user = get_record('user','id',$userid)) {
+        if (!$user = $DB->get_record('user', array('id'=>$userid))) {
             print_error('invaliduser', 'error');
         }
     }
index c1f414f199022f0e938f0f1231407d09c0372b8b..11b4ba84a33fb14d752e04d1acc3903b3b86de41 100644 (file)
@@ -10,7 +10,7 @@
     $user = optional_param('user', 0, PARAM_INT);  // Student ID
     $date = optional_param('date', 0, PARAM_INT);  // A time of a day (in GMT)
 
-    if (! $course = get_record("course", "id", $id)) {
+    if (! $course = $DB->get_record("course", array("id"=>$id))) {
         print_error('invalidcourseid');
     }
 
@@ -23,7 +23,7 @@
     }
 
     if ($user) {
-        if (! $user = get_record("user", "id", $user)) {
+        if (! $user = $DB->get_record("user", array("id"=>$user))) {
             print_error("nousers");
         }
     }
index 7cfd457b76b8601b519017e670b257a51b6fd306..601cd4044b5da479751b711d5f0c8e8f59a878b6 100644 (file)
     $logformat   = optional_param('logformat', 'showashtml', PARAM_ALPHA);
 
     if ($hostid == $CFG->mnet_localhost_id) {
-        if (!$course = get_record('course', 'id', $id) ) {
+        if (!$course = $DB->get_record('course', array('id'=>$id))) {
             print_error('That\'s an invalid course id'.$id);
         }
     } else {
-        $course_stub       = array_pop(get_records_select('mnet_log', " hostid='$hostid' AND course='$id' ", '', '*', '', '1'));
+        $course_stub       = $DB->get_record('mnet_log', array('hostid'=>$hostid, 'course'=>$id), '*', true);
         $course->id        = $id;
         $course->shortname = $course_stub->coursename;
         $course->fullname  = $course_stub->coursename;
@@ -65,7 +65,7 @@
         $dateinfo = get_string('alldays');
 
         if ($user) {
-            if (!$u = get_record('user', 'id', $user) ) {
+            if (!$u = $DB->get_record('user', array('id'=>$user))) {
                 print_error('That\'s an invalid user!');
             }
             $userinfo = fullname($u, has_capability('moodle/site:viewfullnames', $context));
index d4c1773889f6c47af4303860b0eafbe2cc80ce2d..82297081cf43d256bf8a0170ad79f66eede2bba1 100644 (file)
@@ -9,12 +9,12 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
     $mnet_peer = new mnet_peer();
     $mnet_peer->set_id($hostid);
 
-    $sql = "select distinct course, hostid, coursename from {$CFG->prefix}mnet_log";
-    $courses = get_records_sql($sql);
+    $sql = "SELECT DISTINCT course, hostid, coursename FROM {mnet_log}";
+    $courses = $DB->get_records_sql($sql);
     $remotecoursecount = count($courses);
 
     // first check to see if we can override showcourses and showusers
-    $numcourses = $remotecoursecount + count_records_select("course", "", "COUNT(id)");
+    $numcourses = $remotecoursecount + $DB->count_records('course');
     if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
         $showcourses = 1;
     }
@@ -75,14 +75,14 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
                 h.id,
                 h.name
             from
-                {$CFG->prefix}mnet_host h,
-                {$CFG->prefix}mnet_log l
+                {mnet_host} h,
+                {mnet_log} l
             where
                 h.id = l.hostid
             order by
                 h.name";
 
-    if ($hosts = get_records_sql($sql)) {
+    if ($hosts = $DB->get_records_sql($sql)) {
         foreach($hosts as $host) {
             $hostarray[$host->id] = $host->name;
         }
@@ -96,7 +96,7 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
         $sites = array();
         if ($CFG->mnet_localhost_id == $hostid) {
             if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
-                if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) {
+                if ($ccc = $DB->get_records("course", null, "fullname","id,fullname,category")) {
                     foreach ($ccc as $cc) {
                         if ($cc->id == SITEID) {
                             $sites["$hostid/$cc->id"]   = format_string($cc->fullname).' ('.get_string('site').')';
@@ -108,8 +108,8 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
             }
         } else {
             if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
-                $sql = "select distinct course, coursename from {$CFG->prefix}mnet_log where hostid = '$hostid'";
-                if ($ccc = get_records_sql($sql)) {
+                $sql = "SELECT DISTINCT course, coursename FROM {mnet_log} where hostid = ?";
+                if ($ccc = $DB->get_records_sql($sql, array($hostid))) {
                     foreach ($ccc as $cc) {
                         if (1 == $cc->course) { // TODO: this might be wrong - site course may have another id
                             $sites["$hostid/$cc->course"]   = $cc->coursename.' ('.get_string('site').')';
@@ -247,7 +247,7 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
     else {
         $users = array();
         if (!empty($selecteduser)) {
-            $user = get_record('user','id',$selecteduser);
+            $user = $DB->get_record('user', array('id'=>$selecteduser));
             $users[$selecteduser] = fullname($user);
         }
         else {
@@ -278,7 +278,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate='today'
     global $USER, $CFG, $DB;
 
     // first check to see if we can override showcourses and showusers
-    $numcourses =  count_records_select("course", "", "COUNT(id)");
+    $numcourses =  $DB->count_records("course");
     if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
         $showcourses = 1;
     }
@@ -326,7 +326,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate='today'
     }
 
     if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
-        if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) {
+        if ($ccc = $DB->get_records("course", null, "fullname", "id,fullname,category")) {
             foreach ($ccc as $cc) {
                 if ($cc->category) {
                     $courses["$cc->id"] = format_string($cc->fullname);
@@ -459,7 +459,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate='today'
     else {
         $users = array();
         if (!empty($selecteduser)) {
-            $user = get_record('user','id',$selecteduser);
+            $user = $DB->get_record('user', array('id'=>$selecteduser));
             $users[$selecteduser] = fullname($user);
         }
         else {
index 859efe0fde4323566509b55e143e5f03ae0fc5f9..370b6a6d8a3e09109ce4c72c21298d581f4ade47 100644 (file)
@@ -7,7 +7,7 @@
     $id   = required_param('id', PARAM_INT);
     $page = optional_param('page', 0, PARAM_INT);     // which page to show
 
-    if (! $course = get_record("course", "id", $id) ) {
+    if (! $course = $DB->get_record("course", array("id"=>$id))) {
         print_error('invalidcourseid');
     }
 
index 27c2a2e9a00e76414f8aa3df55adfdca4f76cc9c..b05a990ab9bab4ffad9dab9e3bfee601b14e6d50 100644 (file)
@@ -7,7 +7,7 @@
 
     $id = required_param('id',PARAM_INT);       // course id
 
-    if (!$course = get_record('course', 'id', $id)) {
+    if (!$course = $DB->get_record('course', array('id'=>$id))) {
         print_error('invalidcourseid');
     }
 
     $modinfo = get_fast_modinfo($course);
 
     $sql = "SELECT cm.id, COUNT('x') AS numviews, MAX(time) AS lasttime
-              FROM {$CFG->prefix}course_modules cm
-                   JOIN {$CFG->prefix}modules m ON m.id = cm.module
-                   JOIN {$CFG->prefix}log l     ON l.cmid = cm.id
-             WHERE cm.course = $course->id AND l.action LIKE 'view%' AND m.visible = 1
+              FROM {course_modules} cm
+                   JOIN {modules} m ON m.id = cm.module
+                   JOIN {log} l     ON l.cmid = cm.id
+             WHERE cm.course = ? AND l.action LIKE 'view%' AND m.visible = 1
           GROUP BY cm.id";
-    $views = get_records_sql($sql);
+    $views = $DB->get_records_sql($sql, array($course->id));
 
     $ri = 0;
     $prevsecctionnum = 0;
index 9e24ac23c5f4395a51f41edf855749a81626bd68..54fc6dc938cc69c295209d82916c0bc58dfeb611 100644 (file)
         $action = ''; // default to all (don't restrict)
     }
 
-    if (!$course = get_record('course', 'id', $id)) {
+    if (!$course = $DB->get_record('course', array('id'=>$id))) {
         print_error('invalidcourse');
     }
 
-    if ($roleid != 0 and !$role = get_record('role', 'id', $roleid)) {
+    if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
         print_error('invalidrole');
     }
 
@@ -55,7 +55,7 @@
 
     $modinfo = get_fast_modinfo($course);
 
-    $modules = get_records_select('modules', "visible = 1 AND name <> 'label'", 'name ASC');
+    $modules = $DB->get_records_select('modules', "visible = 1 AND name <> 'label'", null, 'name ASC');
 
     $instanceoptions = array();
     foreach ($modules as $module) {
@@ -71,7 +71,7 @@
 
     $timeoptions = array();
     // get minimum log time for this course
-    $minlog = get_field_sql('SELECT min(time) FROM '.$CFG->prefix.'log WHERE course = '.$course->id);
+    $minlog = $DB->get_field_sql('SELECT min(time) FROM {log} WHERE course = ?', array($course->id));
 
     $now = usergetmidnight(time());
 
         $postfun = $cm->modname.'_get_post_actions';
 
         if (!function_exists($viewfun) || !function_exists($postfun)) {
-            error(get_string('modulemissingcode','error',$cm->modname), $baseurl);
+            print_error('modulemissingcode', 'error', $baseurl, $cm->modname);
         }
 
         $viewnames = $viewfun();
 
         switch ($action) {
             case 'view':
-                $actionsql = 'l.action IN (\''.implode('\',\'', $viewnames).'\' )';
+                $actions = $viewnames;
                 break;
             case 'post':
-                $actionsql = 'l.action IN (\''.implode('\',\'', $postnames).'\' )';
+                $actions = $postnames;
                 break;
             default:
                 // some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
-                $actionsql = 'l.action IN (\''.implode('\',\'', array_merge($viewnames, $postnames)).'\' )';
+                $actions = array_merge($viewnames, $postnames);
         }
 
+        list($actionsql, $params) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action0');
+        $actionsql = "l.action $actionsql"; 
+
         $relatedcontexts = get_related_contexts_string($context);
 
         $sql = "SELECT ra.userid, u.firstname, u.lastname, u.idnumber, COUNT(l.action) AS count
-                  FROM {$CFG->prefix}role_assignments ra
-                       JOIN {$CFG->prefix}user u           ON u.id = ra.userid
-                       LEFT OUTER JOIN {$CFG->prefix}log l ON l.userid = ra.userid
-                 WHERE ra.contextid $relatedcontexts AND ra.roleid = $roleid AND
+                  FROM {role_assignments} ra
+                       JOIN {user} u           ON u.id = ra.userid
+                       LEFT JOIN {log} l ON l.userid = ra.userid
+                 WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid AND
                        (l.id IS NULL OR
-                          (l.cmid = $instanceid AND l.time > $timefrom AND $actionsql)
+                          (l.cmid = :instanceid AND l.time > :timefrom AND $actionsql)
                        )";
+        $params['roleid'] = $roleid;
+        $params['instanceid'] = $instanceid;
+        $params['timefrom'] = $timefrom;
 
         if ($table->get_sql_where()) {
             $sql .= ' AND '.$table->get_sql_where(); //initial bar
                        FROM {$CFG->prefix}role_assignments ra
                             JOIN {$CFG->prefix}user u           ON u.id = ra.userid
                             LEFT OUTER JOIN {$CFG->prefix}log l ON l.userid = ra.userid
-                      WHERE ra.contextid $relatedcontexts AND ra.roleid = $roleid AND
+                      WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid AND
                             (l.id IS NULL OR
-                               (l.cmid = $instanceid AND l.time > $timefrom AND $actionsql)
+                               (l.cmid = :instanceid AND l.time > :timefrom AND $actionsql)
                             )";
 
-        $totalcount = count_records_sql($countsql);
+        $totalcount = $DB->count_records_sql($countsql, $params);
 
         if ($table->get_sql_where()) {
-            $matchcount = count_records_sql($countsql.' AND '.$table->get_sql_where());
+            $matchcount = $DB->count_records_sql($countsql.' AND '.$table->get_sql_where(), $params);
         } else {
             $matchcount = $totalcount;
         }
         $table->initialbars($totalcount > $perpage);
         $table->pagesize($perpage, $matchcount);
 
-        if (!$users = get_records_sql($sql, $table->get_page_start(), $table->get_page_size())) {
+        if (!$users = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
             $users = array(); // tablelib will handle saying 'Nothing to display' for us.
         }
 
index 732116e91cce9e7be05790091a19744ebd369c3b..f3a402fb38d9cd99c7c8443677f7bc19ff279ba7 100644 (file)
     $userid   = optional_param('userid', 0, PARAM_INT);
     $roleid   = optional_param('roleid',0,PARAM_INT);
 
-    if (!$course = get_record("course","id",$courseid)) {
+    if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
         print_error("invalidcourseid");
     }
 
     if (!empty($userid)) {
-        if (!$user = get_record('user','id',$userid)) {
+        if (!$user = $DB->get_record('user', array('id'=>$userid))) {
             print_error("nousers");
         }
     }
@@ -36,6 +36,7 @@
         $param->table = 'user_'.$param->table;
     }
 
+    // TODO: cleanup this ugly mess!
     $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
     .' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
     .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
@@ -46,7 +47,7 @@
     .' '.$param->extras
     .' ORDER BY timeend DESC';
 
-    $stats = get_records_sql($sql);
+    $stats = $DB->get_records_sql($sql, $param->params);
 
     $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
 
index 3f12f58f8837f955bccb87b77c9724dfd7034371..99a4a603a66af6a9c4671271dc398a73c88ca107 100644 (file)
         redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time);
     }
 
-    if (!$course = get_record("course","id",$courseid)) {
+    if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
         print_error("invalidcourseid");
     }
 
     if (!empty($userid)) {
-        if (!$user = get_record('user','id',$userid)) {
+        if (!$user = $DB->get_record('user', array('id'=>$userid))) {
             print_error("nousers");
         }
     }
index f85a32a967fa4868841190666d6ea8c9428b9ada..c2255bc8e38d579d8fe05144b73113e03507fd81 100644 (file)
@@ -22,6 +22,7 @@
     if ($mode == STATS_MODE_DETAILED) {
         $param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string (if we have time)
 
+        //TODO: lceanup this ugly mess
         $sql = 'SELECT DISTINCT s.userid, u.firstname, u.lastname, u.idnumber
                      FROM '.$CFG->prefix.'stats_user_'.$param->table.' s
                      JOIN '.$CFG->prefix.'user u ON u.id = s.userid
@@ -30,7 +31,7 @@
             . ((!empty($time)) ? ' AND timeend >= '.$param->timeafter : '')
             .' ORDER BY u.lastname, u.firstname ASC';
 
-        if (!$us = get_records_sql($sql)) {
+        if (!$us = $DB->get_records_sql($sql, $param->params)) {
             print_error('nousers');
         }
 
@@ -80,6 +81,7 @@
         if (!empty($param->sql)) {
             $sql = $param->sql;
         } else {
+            //TODO: lceanup this ugly mess
             $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
                 .' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
                 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
@@ -91,7 +93,7 @@
                 .' ORDER BY timeend DESC';
         }
 
-        $stats = get_records_sql($sql);
+        $stats = $DB->get_records_sql($sql, $params);
 
         if (empty($stats)) {
             notify(get_string('statsnodata'));
index f7bb9c4b6f94ae4d311de53778373674ca308119..d7292798639a3d49e9e172d9e6efd5a1f61eb601 100644 (file)
@@ -1112,6 +1112,20 @@ abstract class moodle_database {
         return ' ' . $fieldname . ' ';
     }
 
+    /**
+     * Returns the SQL to be used in order to CAST one CHAR column to REAL number.
+     *
+     * Be aware that the CHAR column you're trying to cast contains really
+     * numbers or the RDBMS will throw an error!
+     *
+     * @param string fieldname the name of the field to be casted
+     * @param boolean text to specify if the original column is one TEXT (CLOB) column (true). Defaults to false.
+     * @return string the piece of SQL code to be used in your statement.
+     */
+    public function sql_cast_char2real($fieldname, $text=false) {
+        return ' ' . $fieldname . ' ';
+    }
+
     /**
      * Returns the SQL text to be used to compare one TEXT (clob) column with
      * one varchar column, because some RDBMS doesn't support such direct
index 7d190705ca1ae0dd587442fb51e608df22fc029e..08e40d1375afcf49ee94f9989dc884c023577ace 100644 (file)
@@ -425,6 +425,10 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database {
         return ' CAST(' . $fieldname . ' AS INT) ';
     }
 
+    public function sql_cast_char2real($fieldname, $text=false) {
+        return " $fieldname::real ";
+    }
+
     /**
      * Does this driver suppoer regex syntax when searching
      */
index 2b848ea104ac980e11fe5d49a27b4b1fbf4a4d55..d5955ad87d30aeccac7b2bd9c2f7bc1c4a12f39f 100644 (file)
@@ -977,9 +977,10 @@ function stats_clean_old() {
 }
 
 function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
-    global $CFG,$db;
+    global $CFG, $DB;
 
     $param = new object();
+    $params->params = array();
 
     if ($time < 10) { // dailies
         // number of days to go back = 7* time
@@ -997,12 +998,6 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
 
     $param->extras = '';
 
-    // compatibility - if we're in postgres, cast to real for some reports.
-    $real = '';
-    if ($CFG->dbfamily == 'postgres') {
-        $real = '::real';
-    }
-
     switch ($report) {
     // ******************** STATS_MODE_GENERAL ******************** //
     case STATS_REPORT_LOGINS:
@@ -1017,33 +1012,33 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
         break;
 
     case STATS_REPORT_READS:
-        $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat1 as line1';
+        $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat1 as line1';
         $param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
         $param->aggregategroupby = 'roleid';
         $param->stattype = 'activity';
         $param->crosstab = true;
         $param->extras = 'GROUP BY timeend,roleid,stat1';
         if ($courseid == SITEID) {
-            $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1) as line1';
+            $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1) as line1';
             $param->extras = 'GROUP BY timeend,roleid';
         }
         break;
 
     case STATS_REPORT_WRITES:
-        $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat2 as line1';
+        $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, stat2 as line1';
         $param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
         $param->aggregategroupby = 'roleid';
         $param->stattype = 'activity';
         $param->crosstab = true;
         $param->extras = 'GROUP BY timeend,roleid,stat2';
         if ($courseid == SITEID) {
-            $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat2) as line1';
+            $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat2) as line1';
             $param->extras = 'GROUP BY timeend,roleid';
         }
         break;
 
     case STATS_REPORT_ACTIVITY:
-        $param->fields = sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1+stat2) as line1';
+        $param->fields = $DB->sql_concat('timeend','roleid').' AS uniqueid, timeend, roleid, sum(stat1+stat2) as line1';
         $param->fieldscomplete = true; // set this to true to avoid anything adding stuff to the list and breaking complex queries.
         $param->aggregategroupby = 'roleid';
         $param->stattype = 'activity';
@@ -1112,13 +1107,13 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
                         activity.all_activity / enrolments.highest_enrolments as line3
                        FROM (
                             SELECT courseid, (stat1+stat2) AS all_activity
-                              FROM '.$CFG->prefix.'stats_'.$param->table.'
+                              FROM {stats_'.$param->table.'}
                              WHERE stattype=\'activity\' AND timeend >= '.$param->timeafter.' AND roleid = 0
                        ) activity
                        INNER JOIN
                             (
                             SELECT courseid, max(stat1) AS highest_enrolments 
-                              FROM '.$CFG->prefix.'stats_'.$param->table.'
+                              FROM {stats_'.$param->table.'}
                              WHERE stattype=\'enrolments\' AND timeend >= '.$param->timeafter.' AND stat1 > '.$threshold.' 
                           GROUP BY courseid
                       ) enrolments
@@ -1140,8 +1135,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
                          ceil(avg(active_enrolments)) as line2, avg(proportion_active) AS line3
                        FROM (
                            SELECT courseid, timeend, stat2 as active_enrolments,
-                                  stat1 as all_enrolments, stat2'.$real.'/stat1'.$real.' as proportion_active
-                             FROM '.$CFG->prefix.'stats_'.$param->table.'
+                                  stat1 as all_enrolments, '.$DB->sql_cast_char2real('stat2').'/'.$DB->sql_cast_char2real('stat1').' AS proportion_active
+                             FROM {stats_'.$param->table.'}
                             WHERE stattype=\'enrolments\' AND roleid = 0 AND stat1 > '.$threshold.'
                        ) aq
                        WHERE timeend >= '.$param->timeafter.'
@@ -1160,8 +1155,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
                            avg(proportion_active) AS line3
                          FROM (
                            SELECT courseid, timeend, stat1 as views, stat2 AS posts,
-                                  stat2'.$real.'/stat1'.$real.' as proportion_active
-                             FROM '.$CFG->prefix.'stats_'.$param->table.'
+                                  '.$DB->sql_cast_char2real('stat2').'/'.$DB->sql_cast_char2real('stat1').' as proportion_active
+                             FROM {stats_'.$param->table.'}
                             WHERE stattype=\'activity\' AND roleid = 0 AND stat1 > 0
                        ) aq
                        WHERE timeend >= '.$param->timeafter.'
@@ -1181,7 +1176,7 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
     }
     */
     //TODO must add the SITEID reports to the rest of the reports.
-    return $param;
+    return array($sql, $params);
 }
 
 function stats_get_view_actions() {