Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c351cf2799
Fetching contributors…

Cannot retrieve contributors at this time

file 159 lines (119 sloc) 4.836 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
<?PHP // $Id$
      // Produces a graph of log accesses

    require("../config.php");
    require("lib.php");
    require("$CFG->libdir/graphlib.php");

    require_variable($id); // Course ID
    require_variable($type); // Graph Type
    optional_variable($user); // Student ID
    optional_variable($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)) {
        if (! ($type == "usercourse.png" and $user == $USER->id) ) {
            error("Sorry, you aren't allowed to see this.");
        }
    }

    if ($user) {
        if (! $user = get_record("user", "id", $user)) {
            error("Can not find that user");
        }
    }


    $timenow = time();

    switch ($type) {
     case "usercourse.png":

       $COURSE_MAX_LOG_DISPLAY = $COURSE_MAX_LOG_DISPLAY * 3600 * 24; // seconds
       if ($timenow - $course->startdate > $COURSE_MAX_LOG_DISPLAY) {
           $course->startdate = $timenow - $COURSE_MAX_LOG_DISPLAY;
       }
       $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_records_sql("SELECT floor((`time` - $coursestart)/86400) as day,
count(*) as num FROM log
WHERE user = '$user->id'
AND course = '$course->id'
AND `time` > '$coursestart'
GROUP BY day ")) {
           foreach ($rawlogs as $rawlog) {
               $logs[$rawlog->day] = $rawlog->num;
           }
       }

       $maxlogs = max($logs);


       $graph = new graph(750, 400);

       $a->coursename = $course->shortname;
       $a->username = "$user->firstname $user->lastname";
       $graph->parameter['title'] = get_string("hitsoncourse", "", $a);

       $graph->x_data = $days;

       $graph->y_data['logs'] = $logs;
       $graph->y_format['logs'] = array('colour' => 'blue','line' => 'line');
       $graph->y_label_left = get_string("hits");
       $graph->label_size = "6";

       $graph->y_order = array('logs');

       
       $graph->parameter['shadow'] = 'none';

       error_reporting(5); // ignore most warnings such as font problems etc
       $graph->draw_stack();

       break;

     case "userday.png":

       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;
           $hh = (int)userdate($hour, "%I");
           $hours[$i] = userdate($hour, "$hh %p");
       }

       if ($rawlogs = get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour,
count(*) as num FROM log
WHERE user = '$user->id'
AND course = '$course->id'
AND `time` > '$daystart'
GROUP BY hour ")) {
           foreach ($rawlogs as $rawlog) {
               $logs[$rawlog->hour] = $rawlog->num;
           }
       }

       $maxlogs = max($logs);

       $graph = new graph(750, 400);

       $a->coursename = $course->shortname;
       $a->username = "$user->firstname $user->lastname";
       $graph->parameter['title'] = get_string("hitsoncoursetoday", "", $a);

       $graph->x_data = $hours;

       $graph->y_data['logs'] = $logs;
       $graph->y_format['logs'] = array('colour' => 'blue','bar' => 'fill','legend' =>'actual','bar_size' => 0.9);
       $graph->y_label_left = get_string("hits");
       $graph->label_size = "6";

       $graph->y_order = array('logs');

       
       $graph->parameter['shadow'] = 'none';

       error_reporting(5); // ignore most warnings such as font problems etc
       $graph->draw_stack();

       break;

     default:
       break;
   }

?>
Something went wrong with that request. Please try again.