]> git.mjollnir.org Git - moodle.git/commitdiff
Added real rough new report for logs - needs work. Shane?
authormoodler <moodler>
Thu, 9 Mar 2006 09:45:57 +0000 (09:45 +0000)
committermoodler <moodler>
Thu, 9 Mar 2006 09:45:57 +0000 (09:45 +0000)
course/report/log/graph.php [new file with mode: 0644]
course/report/log/index.php [new file with mode: 0644]
course/report/log/lib.php [new file with mode: 0644]
course/report/log/live.php [new file with mode: 0644]
course/report/log/mod.php [new file with mode: 0644]

diff --git a/course/report/log/graph.php b/course/report/log/graph.php
new file mode 100644 (file)
index 0000000..b04ef87
--- /dev/null
@@ -0,0 +1,183 @@
+<?php // $Id$
+      // Produces a graph of log accesses
+
+    require_once("../config.php");
+    require_once("lib.php");
+    require_once("$CFG->libdir/graphlib.php");
+
+    $id = required_param('id',PARAM_INT);    // Course ID
+    $type = required_param('type');  // Graph Type
+    $user = optional_param('user',0,PARAM_INT);  // Student ID
+    $date = optional_param('date');  // A time of a day (in GMT)
+
+    if (! $course = get_record("course", "id", $id)) {
+        error("Course is misconfigured");
+    }
+
+    require_login($course->id);
+
+    if (! (isteacher($course->id) or ($course->showreports and $USER->id == $user))) {
+        error("Sorry, you aren't allowed to see this.");
+    }
+
+    if ($user) {
+        if (! $user = get_record("user", "id", $user)) {
+            error("Can not find that user");
+        }
+    }
+
+    $logs = array();
+
+    $timenow = time();
+
+    switch ($type) {
+     case "usercourse.png":
+
+       $site = get_site();
+        
+       if ($course->id == $site->id) {
+           $courseselect = 0;
+       } else {
+           $courseselect = $course->id;
+       }
+
+       $maxseconds = COURSE_MAX_LOG_DISPLAY * 3600 * 24;  // seconds
+       //$maxseconds = 60 * 3600 * 24;  // seconds
+       if ($timenow - $course->startdate > $maxseconds) {
+           $course->startdate = $timenow - $maxseconds;
+       }
+
+       if (!empty($CFG->loglifetime)) {
+           $maxseconds = $CFG->loglifetime * 3600 * 24;  // seconds
+           if ($timenow - $course->startdate > $maxseconds) {
+               $course->startdate = $timenow - $maxseconds;
+           }
+       }
+
+       $timestart = $coursestart = usergetmidnight($course->startdate);
+
+       if ((($timenow - $timestart)/86400.0) > 40) {
+           $reducedays = 7;
+       } else {
+           $reducedays = 0;
+       }
+
+       $i = 0;
+       while ($timestart < $timenow) {
+           $timefinish = $timestart + 86400;
+           if ($reducedays) {
+               if ($i % $reducedays) {
+                   $days[$i] = "";
+               } else {
+                   $days[$i] = userdate($timestart, "%a %d %b");
+               }
+           } else {
+               $days[$i] = userdate($timestart, "%a %d %b");
+           }
+           $logs[$i] = 0;
+           $i++;
+           $timestart = $timefinish;
+       }
+
+       if ($rawlogs = get_logs_usercourse($user->id, $courseselect, $coursestart)) {
+           foreach ($rawlogs as $rawlog) {
+               $logs[$rawlog->day] = $rawlog->num;
+           }
+       }
+
+       $graph = new graph(750, 400);
+
+       $a->coursename = $course->shortname;
+       $a->username = fullname($user, true);
+       $graph->parameter['title'] = get_string("hitsoncourse", "", $a);
+
+       $graph->x_data           = $days;
+
+       $graph->y_data['logs']   = $logs;
+       $graph->y_order = array('logs');
+
+       if (!empty($CFG->preferlinegraphs)) {
+           $graph->y_format['logs'] = array('colour' => 'blue','line' => 'line');
+       } else {
+           $graph->y_format['logs'] = array('colour' => 'blue','bar' => 'fill','bar_size' => 0.6);
+           $graph->parameter['bar_spacing'] = 0;
+       }
+
+
+       $graph->parameter['y_label_left']     = get_string("hits");
+       $graph->parameter['label_size']       = "12";
+       $graph->parameter['x_axis_angle']     = 90;
+       $graph->parameter['x_label_angle']    = 0;
+       $graph->parameter['tick_length'] = 0;
+
+       
+       $graph->parameter['shadow']          = 'none';
+
+       error_reporting(5); // ignore most warnings such as font problems etc
+       $graph->draw_stack();
+
+       break;
+
+     case "userday.png":
+
+       $site = get_site();
+        
+       if ($course->id == $site->id) {
+           $courseselect = 0;
+       } else {
+           $courseselect = $course->id;
+       }
+
+       if ($date) {
+           $daystart = usergetmidnight($date);
+       } else {
+           $daystart = usergetmidnight(time());
+       }
+       $dayfinish = $daystart + 86400;
+
+       for ($i=0; $i<=23; $i++) {
+           $logs[$i] = 0;
+           $hour = $daystart + $i * 3600;
+           $hours[$i] = $i;
+       }
+
+       if ($rawlogs = get_logs_userday($user->id, $courseselect, $daystart)) {
+           foreach ($rawlogs as $rawlog) {
+               $logs[$rawlog->hour] = $rawlog->num;
+           }
+       }
+
+       $graph = new graph(750, 400);
+
+       $a->coursename = $course->shortname;
+       $a->username = fullname($user, true);
+       $graph->parameter['title'] = get_string("hitsoncoursetoday", "", $a);
+
+       $graph->x_data           = $hours;
+
+       $graph->y_data['logs']   = $logs;
+       $graph->y_order = array('logs');
+
+       if (!empty($CFG->preferlinegraphs)) {
+           $graph->y_format['logs'] = array('colour' => 'blue','line' => 'line');
+       } else {
+           $graph->y_format['logs'] = array('colour' => 'blue','bar' => 'fill','bar_size' => 0.9);
+       }
+
+       $graph->parameter['y_label_left']     = get_string("hits");
+       $graph->parameter['label_size']       = "12";
+       $graph->parameter['x_axis_angle']     = 0;
+       $graph->parameter['x_label_angle']    = 0;
+
+       $graph->parameter['shadow']          = 'none';
+
+       error_reporting(5); // ignore most warnings such as font problems etc
+       $graph->draw_stack();
+
+       break;
+
+     default:
+       break;
+   }
+
+?>
diff --git a/course/report/log/index.php b/course/report/log/index.php
new file mode 100644 (file)
index 0000000..1ca3795
--- /dev/null
@@ -0,0 +1,102 @@
+<?php // $Id$
+      // Displays different views of the logs.
+
+    require_once('../../../config.php');
+    require_once('../../lib.php');
+    require_once('lib.php');
+
+    $id = required_param('id',PARAM_INT);// Course ID
+    $group = optional_param('group', -1, PARAM_INT); // Group to display
+    $user = optional_param('user', 0, PARAM_INT); // User to display
+    $date = optional_param('date', 0, PARAM_CLEAN); // Date to display
+    $modname = optional_param('modname', ''); // course_module->id
+    $modid = optional_param('modid', ''); // course_module->id
+    $modaction = optional_param('modaction', ''); // an action as recorded in the logs
+    $page = optional_param('page', '0', PARAM_INT);     // which page to show
+    $perpage = optional_param('perpage', '100', PARAM_INT); // how many per page 
+    $showcourses = optional_param('showcourses',0,PARAM_INT); // whether to show courses if we're over our limit.
+    $showusers = optional_param('showusers',0,PARAM_INT); // whether to show users if we're over our limit.
+    $chooselog = optional_param('chooselog',0,PARAM_INT);
+
+    require_login();
+
+    if (! $course = get_record('course', 'id', $id) ) {
+        error('That\'s an invalid course id');
+    }
+
+    if (! isteacher($course->id)) {
+        error('Only teachers can view logs');
+    }
+
+    if (! $course->category) {
+        if (!isadmin()) {
+            error('Only administrators can look at the site logs');
+        }
+    }
+
+    $strlogs = get_string('logs');
+    $stradministration = get_string('administration');
+
+    session_write_close();
+
+    if (!empty($chooselog)) {
+        $userinfo = get_string('allparticipants');
+        $dateinfo = get_string('alldays');
+
+        if ($user) {
+            if (!$u = get_record('user', 'id', $user) ) {
+                error('That\'s an invalid user!');
+            }
+            $userinfo = fullname($u, isteacher($course->id));
+        }
+        if ($date) {
+            $dateinfo = userdate($date, get_string('strftimedaydate'));
+        }
+
+        if ($course->category) {
+            print_header($course->shortname .': '. $strlogs, $course->fullname, 
+                         "<a href=\"view.php?id=$course->id\">$course->shortname</a> ->
+                          <a href=\"log.php?id=$course->id\">$strlogs</a> -> $userinfo, $dateinfo", '');
+        } else {
+            print_header($course->shortname .': '. $strlogs, $course->fullname, 
+                         "<a href=\"../$CFG->admin/index.php\">$stradministration</a> ->
+                          <a href=\"log.php?id=$course->id\">$strlogs</a> -> $userinfo, $dateinfo", '');
+        }
+        
+        print_heading("$course->fullname: $userinfo, $dateinfo (".usertimezone().")");
+
+        print_log_selector_form($course, $user, $date, $modname, $modid, $modaction, $group, $showcourses, $showusers);
+        
+        echo '<p align="center">[ <a href="'.$CFG->wwwroot.'/course/stats.php?course='.$course->id.'">'.get_string('seealsostats').'</a> ]</p>';
+
+        print_log($course, $user, $date, 'l.time DESC', $page, $perpage, 
+                  "log.php?id=$course->id&amp;chooselog=1&amp;user=$user&amp;date=$date&amp;modid=$modid&amp;modaction=$modaction&amp;group=$group", 
+                  $modname, $modid, $modaction, $group);
+
+    } else {
+        if ($course->category) {
+            print_header($course->shortname .': '. $strlogs, $course->fullname, 
+                     "<a href=\"view.php?id=$course->id\">$course->shortname</a> -> $strlogs", '');
+        } else {
+            print_header($course->shortname .': '. $strlogs, $course->fullname, 
+                     "<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> $strlogs", '');
+        }
+
+        print_heading(get_string('chooselogs') .':');
+
+        print_log_selector_form($course, $user, $date, $modname, $modid, $modaction, $group, $showcourses, $showusers);
+
+        echo '<br />';
+        print_heading(get_string('chooselivelogs') .':');
+
+        echo '<center><h3>';
+        link_to_popup_window('/course/loglive.php?id='. $course->id,'livelog', get_string('livelogs'), 500, 800);
+        echo '</h3></center>';
+
+    }
+
+    print_footer($course);
+
+    exit;
+
+?>
diff --git a/course/report/log/lib.php b/course/report/log/lib.php
new file mode 100644 (file)
index 0000000..c748d06
--- /dev/null
@@ -0,0 +1,199 @@
+<?php
+
+function print_log_selector_form($course, $selecteduser=0, $selecteddate="today",
+                                 $modname="", $modid=0, $modaction="", $selectedgroup=-1,$showcourses=0,$showusers=0) {
+
+    global $USER, $CFG;
+
+    // first check to see if we can override showcourses and showusers
+    $numcourses =  count_records_select("course", "", "COUNT(id)");
+    if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
+        $showcourses = 1;
+    }
+   
+    /// Setup for group handling.
+    $isteacher = isteacher($course->id);
+    $isteacheredit = isteacheredit($course->id);
+    if ($course->groupmode == SEPARATEGROUPS and !$isteacheredit) {
+        $selectedgroup = get_current_group($course->id);
+        $showgroups = false;
+    }
+    else if ($course->groupmode) {
+        $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup;
+        $showgroups = true;
+    }
+    else {
+        $selectedgroup = 0;
+        $showgroups = false;
+    }
+
+    // Get all the possible users
+    $users = array();
+
+    if ($course->category) {
+        if ($selectedgroup) {   // If using a group, only get users in that group.
+            $courseusers = get_group_users($selectedgroup, 'u.lastname ASC', '', 'u.id, u.firstname, u.lastname, u.idnumber');
+        } else {
+            $courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname, u.idnumber');
+        }
+    } else {
+        $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname, u.idnumber");
+    }
+    
+    if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) {
+        $showusers = 1;
+    }
+
+    if ($showusers) {
+        if ($courseusers) {
+            foreach ($courseusers as $courseuser) {
+                $users[$courseuser->id] = fullname($courseuser, $isteacher);
+            }
+        }
+        if ($guest = get_guest()) {
+            $users[$guest->id] = fullname($guest);
+        }
+    }
+
+    if (isadmin() && $showcourses) {
+        if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) {
+            foreach ($ccc as $cc) {
+                if ($cc->category) {
+                    $courses["$cc->id"] = "$cc->fullname";
+                } else {
+                    $courses["$cc->id"] = " $cc->fullname (Site)";
+                }
+            }
+        }
+        asort($courses);
+    }
+
+    $activities = array();
+    $selectedactivity = "";
+
+    if ($modinfo = unserialize($course->modinfo)) {
+        $section = 0;
+        if ($course->format == 'weeks') {  // Bodgy
+            $strsection = get_string("week");
+        } else {
+            $strsection = get_string("topic");
+        }
+        foreach ($modinfo as $mod) {
+            if ($mod->mod == "label") {
+                continue;
+            }
+            if ($mod->section > 0 and $section <> $mod->section) {
+                $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
+            }
+            $section = $mod->section;
+            $mod->name = strip_tags(format_string(urldecode($mod->name),true));
+            if (strlen($mod->name) > 55) {
+                $mod->name = substr($mod->name, 0, 50)."...";
+            }
+            if (!$mod->visible) {
+                $mod->name = "(".$mod->name.")";
+            }
+            $activities["$mod->cm"] = $mod->name;
+
+            if ($mod->cm == $modid) {
+                $selectedactivity = "$mod->cm";
+            }
+        }
+    }
+
+    if (isadmin() && !$course->category) {
+        $activities["site_errors"] = get_string("siteerrors");
+        if ($modid === "site_errors") {
+            $selectedactivity = "site_errors";
+        }
+    }
+
+    $strftimedate = get_string("strftimedate");
+    $strftimedaydate = get_string("strftimedaydate");
+
+    asort($users);
+
+    // Get all the possible dates
+    // Note that we are keeping track of real (GMT) time and user time
+    // User time is only used in displays - all calcs and passing is GMT
+
+    $timenow = time(); // GMT
+
+    // What day is it now for the user, and when is midnight that day (in GMT).
+    $timemidnight = $today = usergetmidnight($timenow);
+
+    // Put today up the top of the list
+    $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
+
+    if (!$course->startdate or ($course->startdate > $timenow)) {
+        $course->startdate = $course->timecreated;
+    }
+
+    $numdates = 1;
+    while ($timemidnight > $course->startdate and $numdates < 365) {
+        $timemidnight = $timemidnight - 86400;
+        $timenow = $timenow - 86400;
+        $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
+        $numdates++;
+    }
+
+    if ($selecteddate == "today") {
+        $selecteddate = $today;
+    }
+
+    echo "<center>\n";
+    echo "<form action=\"log.php\" method=\"get\">\n";
+    echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
+    echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
+    echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
+    if (isadmin() && $showcourses) { 
+        choose_from_menu ($courses, "id", $course->id, "");
+    } else {
+        //        echo '<input type="hidden" name="id" value="'.$course->id.'" />';
+        $courses = array();
+        $courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' (Site) ' : '');
+        choose_from_menu($courses,"id",$course->id,false);
+        if (isadmin()) {
+            $a->url = "log.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
+                ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers";
+            print_string('logtoomanycourses','moodle',$a);
+        }
+    }
+
+    if ($showgroups) {
+        if ($cgroups = get_groups($course->id)) {
+            foreach ($cgroups as $cgroup) {
+                $groups[$cgroup->id] = $cgroup->name;
+            }
+        }
+        else {
+            $groups = array();
+        }
+        choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") );
+    }
+
+    if ($showusers) {
+        choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
+    }
+    else {
+        $users = array();
+        if (!empty($selecteduser)) {
+            $user = get_record('user','id',$selecteduser);
+            $users[$selecteduser] = fullname($user);
+        }
+        else {
+            $users[0] = get_string('allparticipants');
+        }
+        choose_from_menu($users,'user',$selecteduser,false);
+        $a->url = "log.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
+            ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses";
+        print_string('logtoomanyusers','moodle',$a);
+    }
+    choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
+    choose_from_menu ($activities, "modid", $selectedactivity, get_string("allactivities"), "", "");
+    echo '<input type="submit" value="'.get_string('showtheselogs').'" />';
+    echo "</form>";
+    echo "</center>";
+}
+
+?>
diff --git a/course/report/log/live.php b/course/report/log/live.php
new file mode 100644 (file)
index 0000000..9ddbf94
--- /dev/null
@@ -0,0 +1,44 @@
+<?php // $Id$
+      //  Displays live view of recent logs
+
+    require_once("../config.php");
+    require_once("lib.php");
+
+    $id   = required_param('id', PARAM_INT);
+    $page = optional_param('page', 0, PARAM_INT);     // which page to show
+
+    require_login();
+
+    if (! $course = get_record("course", "id", $id) ) {
+        error("That's an invalid course id");
+    }
+
+    if (! isteacher($course->id)) {
+        error("Only teachers can view logs");
+    }
+
+    session_write_close();
+
+    // we override the default framename so header/footer
+    // links open in a new window 
+    if (empty($CFG->framename) || $CFG->framename==='_top') {
+        $CFG->framename = '_blank';
+    }
+
+    $strlivelogs = get_string("livelogs");
+    $strupdatesevery = get_string("updatesevery", "moodle", COURSE_LIVELOG_REFRESH);
+
+    print_header("$strlivelogs ($strupdatesevery)", "$strlivelogs", "", "", 
+                 '<meta http-equiv="Refresh" content="'.COURSE_LIVELOG_REFRESH.'; url=loglive.php?id='.$course->id.'" />');
+
+    $user=0;
+    $date=time() - 3600;
+
+    print_log($course, $user, $date, "l.time DESC", $page, 500,
+              "loglive.php?id=$course->id&amp;user=$user&amp;date=$date");
+
+    print_footer($course);
+
+    exit;
+
+?>
diff --git a/course/report/log/mod.php b/course/report/log/mod.php
new file mode 100644 (file)
index 0000000..c4dacef
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+    if (!defined('MOODLE_INTERNAL')) {
+        die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
+    }
+
+    $strlogs = get_string('logs');
+    echo "<center><a href=\"{$CFG->wwwroot}/course/report/logs/index.php?id={$course->id}\">";
+    echo "$strlogs</a></center>\n";
+
+?>