]> git.mjollnir.org Git - moodle.git/commitdiff
more work on stats reports ...
authormjollnir_ <mjollnir_>
Sun, 24 Sep 2006 07:13:33 +0000 (07:13 +0000)
committermjollnir_ <mjollnir_>
Sun, 24 Sep 2006 07:13:33 +0000 (07:13 +0000)
admin/report/courseoverview/index.php
admin/report/courseoverview/reportsgraph.php
admin/report/stats/index.php
course/report/stats/graph.php
course/report/stats/index.php
course/report/stats/report.php
lib/statslib.php

index f83812e604f952227853e774e2327c5fcf137ca4..31ad73f6fa8f4dbf63082e55e93eff52dfbf07ff 100644 (file)
 
     if (!empty($report) && !empty($time)) {
         $param = stats_get_parameters($time,$report,SITEID,STATS_MODE_RANKED);
-
-        $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
-            ." WHERE timeend >= ".$param->timeafter
-            ." GROUP BY courseid "
-            .$param->extras
-            ." ORDER BY ".$param->orderby
-            ." LIMIT ".$numcourses;
+        
+        if (!empty($param->sql)) {
+            $sql = $param->sql ." LIMIT ".$numcourses;
+        } else {
+            $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
+                ." WHERE timeend >= ".$param->timeafter.' AND stattype = \'activity\''
+                ." GROUP BY courseid "
+                .$param->extras
+                ." ORDER BY ".$param->orderby
+                ." LIMIT ".$numcourses;
+        }
+        error_log($sql);
 
         $courses = get_records_sql($sql);
 
@@ -82,7 +87,7 @@
         } else {
             echo '<div align="center"><img src="'.$CFG->wwwroot.'/'.$CFG->admin.'/report/courseoverview/reportsgraph.php?time='.$time.'&report='.$report.'&numcourses='.$numcourses.'" /></div>';
 
-            $table = new object();
+            $table = new StdClass;
             $table->align = array('left','center','center','center');
             $table->head = array(get_string('course'),$param->line1);
             if (!empty($param->line2)) {
                     $a[] = $c->line2;
                 }
                 if (isset($c->line3)) {
-                    $a[] = $c->line3;
+                    $a[] = round($c->line3,2);
                 }
                 $table->data[] = $a;
             }
index 8c456d53fa6b45c03bf4718f57175e90daa26a42..c3c36181b1429de8ffcd8413d52923ae6629c6a2 100644 (file)
 
     $param = stats_get_parameters($time,$report,SITEID,STATS_MODE_RANKED);
 
-    $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
-        ." WHERE timeend >= ".$param->timeafter
-        ." GROUP BY courseid "
-        .$param->extras
-        ." ORDER BY ".$param->orderby
-        ." LIMIT ".$numcourses;
+    if (!empty($param->sql)) {
+        $sql = $param->sql ." LIMIT ".$numcourses;
+    } else {
+        $sql = "SELECT courseid,".$param->fields." FROM ".$CFG->prefix.'stats_'.$param->table
+            ." WHERE timeend >= ".$param->timeafter.' AND stattype = \'activity\''
+            ." GROUP BY courseid "
+            .$param->extras
+            ." ORDER BY ".$param->orderby
+            ." LIMIT ".$numcourses;
+    }
 
     $courses = get_records_sql($sql);
 
index 74b3d5f21d0e77ebd64801bfdd559faba1659b7f..a7d782e66fdde5e60c5ada607cc3ff07ddb66afb 100644 (file)
     $time     = optional_param('time', 0, PARAM_INT);
     $mode     = optional_param('mode', STATS_MODE_GENERAL, PARAM_INT);
     $userid   = optional_param('userid', 0, PARAM_INT);
+    $roleid   = 0;
+
+    if ($report > 50) {
+        $roleid = substr($report,1);
+        $report = 5;
+    }
 
     if ($report == STATS_REPORT_USER_LOGINS) {
         $courseid = SITEID; //override
     }
 
     if ($mode == STATS_MODE_RANKED) {
-        redirect($CFG->wwwroot.'/'.$CFG->admin.'/report.php?time='.$time);
+        redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time);
     }
 
     if (!$course = get_record("course","id",$courseid)) {
index b048341b3e590344a02ebe3957da965c954f604c..81d2ec8a33b6d6ce82e24cd7a0800ee350e02344 100644 (file)
@@ -37,7 +37,7 @@
     }
 
     $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
-    .' FROM '.$CFG->prefix.'stats_'.$param->table.'_tmp WHERE '
+    .' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
     .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
      .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
      .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
index af74091e92df1577ed7727974ebdad41449bcb66..49e8227c7aed337e1963c4eea0bcdfdb704c354d 100644 (file)
@@ -25,7 +25,7 @@
     }
 
     if ($mode == STATS_MODE_RANKED) {
-        redirect($CFG->wwwroot.'/'.$CFG->admin.'/report.php?time='.$time);
+        redirect($CFG->wwwroot.'/'.$CFG->admin.'/report/stats/index.php?time='.$time);
     }
 
     if (!$course = get_record("course","id",$courseid)) {
index 42740d5898a64d05121921546d0ad7774dd5647d..18b4831490aa6665644835eb1d8705f6604ca227 100644 (file)
     $table->width = '*';
 
     if ($mode == STATS_MODE_DETAILED) {
+        // if we have time, 
+        //    get all users from the stats table, joining on user, where they are smaller than us... or ourselves (or all, if admins) where timestuff.
+        // else
+        //    get all users in this course (role_assignments)
+        
+        
+
         if (!empty($time)) {
             $param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string.
-            $sql =  'SELECT DISTINCT s.userid,s.roleid,u.firstname,u.lastname,u.idnumber  FROM '.$CFG->prefix.'stats_user_'.$param->table.' s JOIN '.$CFG->prefix.'user u ON u.id = s.userid '
-                .'WHERE courseid = '.$course->id.' AND timeend >= '.$param->timeafter  . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '');
+            $sql =  'SELECT DISTINCT s.userid,s.roleid,u.firstname,u.lastname,u.idnumber 
+                     FROM '.$CFG->prefix.'stats_user_'.$param->table.' s JOIN '.$CFG->prefix.'user u ON u.id = s.userid 
+                     WHERE courseid = '.$course->id.' AND timeend >= '.$param->timeafter 
+                . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '');
             if (!has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
                 $sql .= ' AND (s.roleid = 1 OR s.userid = '.$USER->id .")";
             }
@@ -57,7 +66,9 @@
         }
         if (empty($time)) {
             if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
-                $sql = 'SELECT t.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid FROM '.$CFG->prefix.'user_teachers t JOIN '.$CFG->prefix.'user u ON u.id = t.userid WHERE course = '.$course->id;
+                $sql = 'SELECT t.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid 
+                        FROM '.$CFG->prefix.'user_teachers t 
+                        JOIN '.$CFG->prefix.'user u ON u.id = t.userid WHERE course = '.$course->id;
                 $moreusers = get_records_sql($sql);
                 foreach ($moreusers as $u) {
                     $users[$u->userid] = $course->teacher .' - '.fullname($u,true);
                                get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
                                get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
                                '<input type="submit" value="'.get_string('view').'" />') ;
-    } else {
+    } else if ($mode == STATS_MODE_RANKED) {
+        $table->align = array('left','left','left','left','left','left');
+        $table->data[] = array(get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
+                               get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true),
+                               '<input type="submit" value="'.get_string('view').'" />') ;
+    } else if ($mode == STATS_MODE_GENERAL) {
         $table->align = array('left','left','left','left','left','left','left');
         $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true),
                                get_string('statsreporttype'),choose_from_menu($reportoptions,'report',($report == 5) ? $report.$roleid : $report,'','','',true),
         if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) {
             error("This type of report is only available for the site course");
         }
+        $timesql = 
         $param = stats_get_parameters($time,$report,$course->id,$mode);
 
         if ($mode == STATS_MODE_DETAILED) {
             $param->table = 'user_'.$param->table;
         }
-        $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
-            .' FROM '.$CFG->prefix.'stats_'.$param->table.'_tmp WHERE '
-            .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
-            .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
-            .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
-            . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
-            .' timeend >= '.$param->timeafter
-            .' '.$param->extras
-            .' ORDER BY timeend DESC';
+        if (!empty($param->sql)) {
+            $sql = $param->sql;
+        } else {
+            $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
+                .' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
+                .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
+                .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
+                .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
+                . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
+                .' timeend >= '.$param->timeafter
+                .' '.$param->extras
+                .' ORDER BY timeend DESC';
+        }
 
         $stats = get_records_sql($sql);
 
index 97a7f4f9217f9efb1a2c412a9eb4c9a73d1bc0a7..3a67618784496d10441fc8af7daa5f3a1335a376 100644 (file)
@@ -579,7 +579,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
     }
 
     switch ($report) {
-    case STATS_REPORT_LOGINS: // done
+    // ******************** STATS_MODE_GENERAL ******************** //
+    case STATS_REPORT_LOGINS:
         $param->fields = 'timeend,sum(stat1) as line1,sum(stat2) as line2';
         $param->fieldscomplete = true;
         $param->stattype = 'logins';
@@ -589,7 +590,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
             $param->extras = 'GROUP BY timeend';
         }
         break;
-    case STATS_REPORT_READS: // done
+
+    case STATS_REPORT_READS:
         $param->fields = $db->Concat('timeend','roleid').' AS UNIQUE, 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';
@@ -601,7 +603,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
             $param->extras = 'GROUP BY timeend,roleid';
         }
         break;
-    case STATS_REPORT_WRITES: //done
+
+    case STATS_REPORT_WRITES: 
         $param->fields = $db->Concat('timeend','roleid').' AS UNIQUE, 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';
@@ -613,7 +616,8 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
             $param->extras = 'GROUP BY timeend,roleid';
         }
         break;
-    case STATS_REPORT_ACTIVITY: //done 
+
+    case STATS_REPORT_ACTIVITY:
         $param->fields = $db->Concat('timeend','roleid').' AS UNIQUE, 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';
@@ -624,29 +628,38 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
             $param->extras = 'GROUP BY timeend,roleid';
         }
         break;
+
     case STATS_REPORT_ACTIVITYBYROLE;
         $param->fields = 'stat1 AS line1, stat2 AS line2';
         $param->stattype = 'activity';
         $rolename = get_field('role','name','id',$roleid);
         $param->line1 = $rolename . get_string('statsreads');
         $param->line2 = $rolename . get_string('statswrites');
+        if ($courseid == SITEID) {
+            $param->extras = 'GROUP BY timeend';
+        }
         break;
+
+    // ******************** STATS_MODE_DETAILED ******************** //
     case STATS_REPORT_USER_ACTIVITY:
         $param->fields = 'statsreads as line1, statswrites as line2';
         $param->line1 = get_string('statsuserreads');
         $param->line2 = get_string('statsuserwrites');
         $param->stattype = 'activity';
         break;
+
     case STATS_REPORT_USER_ALLACTIVITY:
         $param->fields = 'statsreads+statswrites as line1';
         $param->line1 = get_string('statsuseractivity');
         $param->stattype = 'activity';
         break;
+
     case STATS_REPORT_USER_LOGINS:
         $param->fields = 'statsreads as line1';
         $param->line1 = get_string('statsuserlogins');
         $param->stattype = 'logins';
         break;
+
     case STATS_REPORT_USER_VIEW:
         $param->fields = 'statsreads as line1, statswrites as line2, statsreads+statswrites as line3';
         $param->line1 = get_string('statsuserreads');
@@ -654,50 +667,92 @@ function stats_get_parameters($time,$report,$courseid,$mode,$roleid=0) {
         $param->line3 = get_string('statsuseractivity');
         $param->stattype = 'activity';
         break;
+
+    // ******************** STATS_MODE_RANKED ******************** //
     case STATS_REPORT_ACTIVE_COURSES: 
-        $param->fields = 'sum(studentreads+studentwrites+teacherreads+teacherwrites) AS line1';
+        $param->fields = 'sum(stat1+stat2) AS line1';
+        $param->stattype = 'activity';
         $param->orderby = 'line1 DESC';
         $param->line1 = get_string('activity');
         $param->graphline = 'line1';
         break;
+
     case STATS_REPORT_ACTIVE_COURSES_WEIGHTED:
-        $param->fields = 'sum(studentreads+studentwrites+teacherreads+teacherwrites) AS line1,'
-            .'max(students+teachers) AS line2,'
-            .'sum(studentreads+studentwrites+teacherreads+teacherwrites)'.$real.'/max(students+teachers)'.$real.' AS line3';
-        $param->extras = 'HAVING max(students+teachers) != 0';
+        $threshold = 0;
         if (!empty($CFG->statsuserthreshold) && is_numeric($CFG->statsuserthreshold)) {
-            $param->extras .= ' AND max(students+teachers) > '.$CFG->statsuserthreshold;
+            $threshold = $CFG->statsuserthreshold;
         }
-        $param->orderby = 'line3 DESC';
+        $param->fields = '';
+        $param->sql = 'SELECT activity.courseid, activity.all_activity AS line1, enrolments.highest_enrolments AS line2,
+                        activity.all_activity / enrolments.highest_enrolments as line3 
+                       FROM (
+                            SELECT courseid, sum(stat1+stat2) AS all_activity 
+                            FROM '.$CFG->prefix.'stats_'.$param->table.'
+                            WHERE stattype=\'activity\' AND timeend >= '.$param->timeafter.'
+                            GROUP BY courseid
+                       ) activity
+                       INNER JOIN 
+                            (
+                            SELECT courseid, max(stat1) AS highest_enrolments 
+                            FROM '.$CFG->prefix.'stats_'.$param->table.'
+                            WHERE stattype=\'enrolments\' AND timeend >= '.$param->timeafter.'
+                            GROUP BY courseid
+                      ) enrolments
+                      ON (activity.courseid = enrolments.courseid)
+                      WhERE enrolments.highest_enrolments > '.$threshold.'
+                      ORDER BY line3 DESC';
         $param->line1 = get_string('activity');
         $param->line2 = get_string('users');
         $param->line3 = get_string('activityweighted');
         $param->graphline = 'line3';
         break;
+
     case STATS_REPORT_PARTICIPATORY_COURSES:
-        $param->fields = 'max(students+teachers) as line1,max(activestudents+activeteachers) AS line2,'
-            .'max(activestudents+activeteachers)'.$real.'/max(students+teachers)'.$real.' AS line3';
-        $param->extras = 'HAVING max(students+teachers) != 0';
+        $threshold = 0;
         if (!empty($CFG->statsuserthreshold) && is_numeric($CFG->statsuserthreshold)) {
-            $param->extras .= ' AND max(students+teachers) > '.$CFG->statsuserthreshold;
+            $threshold = $CFG->statsuserthreshold;
         }
-        $param->orderby = 'line3 DESC';
+        $param->fields = '';
+        $param->sql = 'SELECT courseid, ceil(avg(all_enrolments)) as line1,
+                         ceil(avg(active_enrolments)) as line2, avg(proportion_active) AS line3 
+                       FROM (
+                           SELECT courseid, timeend, sum(stat2) as active_enrolments, 
+                              sum(stat1) as all_enrolments, sum(stat2)'.$real.'/sum(stat1)'.$real.' as proportion_active 
+                           FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE stattype=\'enrolments\' 
+                           GROUP BY courseid, timeend
+                           HAVING sum(stat1) > '.$threshold.'
+                       ) aq 
+                       WHERE timeend >= '.$param->timeafter.'
+                       GROUP BY courseid
+                       ORDER BY line3 DESC';
+
         $param->line1 = get_string('users');
         $param->line2 = get_string('activeusers');
         $param->line3 = get_string('participationratio');
         $param->graphline = 'line3';
         break;
+
     case STATS_REPORT_PARTICIPATORY_COURSES_RW:
-        $param->fields = 'sum(studentreads+teacherreads) as line1,sum(studentwrites+teacherwrites) AS line2,'
-            .'sum(studentwrites+teacherwrites)'.$real.'/sum(studentreads+teacherreads)'.$real.' AS line3';
-        $param->extras = 'HAVING sum(studentreads+teacherreads) != 0';
-        $param->orderby = 'line3 DESC';
+        $param->fields = '';
+        $param->sql =  'SELECT courseid, sum(views) AS line1, sum(posts) AS line2,
+                           avg(proportion_active) AS line3
+                         FROM (
+                           SELECT courseid, timeend,sum(stat1) as views, sum(stat2) AS posts,
+                            sum(stat2)'.$real.'/sum(stat1)'.$real.' as proportion_active 
+                           FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE stattype=\'activity\' 
+                           GROUP BY courseid, timeend
+                           HAVING sum(stat1) > 0
+                       ) aq 
+                       WHERE timeend >= '.$param->timeafter.'
+                       GROUP BY courseid
+                       ORDER BY line3 DESC';
         $param->line1 = get_string('views');
         $param->line2 = get_string('posts');
         $param->line3 = get_string('participationratio');
         $param->graphline = 'line3';
         break;
     }
+
     /*
     if ($courseid == SITEID && $mode != STATS_MODE_RANKED) { // just aggregate all courses.
         $param->fields = preg_replace('/(?:sum)([a-zA-Z0-9+_]*)\W+as\W+([a-zA-Z0-9_]*)/i','sum($1) as $2',$param->fields);
@@ -910,7 +965,7 @@ function stats_get_report_options($courseid,$mode) {
     switch ($mode) {
     case STATS_MODE_GENERAL:
         $reportoptions[STATS_REPORT_ACTIVITY] = get_string('statsreport'.STATS_REPORT_ACTIVITY);
-        if ($context = get_record('context','instanceid',$courseid,'contextlevel',CONTEXT_COURSE)) {
+        if ($courseid != SITEID && $context = get_record('context','instanceid',$courseid,'contextlevel',CONTEXT_COURSE)) {
             $sql = 'SELECT r.id,r.name FROM '.$CFG->prefix.'role r JOIN '.$CFG->prefix.'stats_daily s ON s.roleid = r.id WHERE s.courseid = '.$courseid;
             if ($roles = get_records_sql($sql)) {
                 foreach ($roles as $role) {