Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/report/log/graph.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 require("../../config.php");
00027 require_once("$CFG->libdir/graphlib.php");
00028 require_once($CFG->dirroot.'/report/log/locallib.php');
00029 
00030 $id   = required_param('id', PARAM_INT);       // Course ID
00031 $type = required_param('type', PARAM_FILE);    // Graph Type
00032 $user = required_param('user', PARAM_INT);     // Student ID
00033 $date = optional_param('date', 0, PARAM_INT);  // A time of a day (in GMT)
00034 
00035 $url = new moodle_url('/report/log/graph.php', array('id'=>$id,'type'=>$type,'user'=>$user,'date'=>$date));
00036 $PAGE->set_url($url);
00037 
00038 if ($type !== "usercourse.png" and $type !== "userday.png") {
00039     $type = 'userday.png';
00040 }
00041 
00042 $course = $DB->get_record("course", array("id"=>$id), '*', MUST_EXIST);
00043 $user = $DB->get_record("user", array("id"=>$user, 'deleted'=>0), '*', MUST_EXIST);
00044 
00045 $coursecontext   = context_course::instance($course->id);
00046 $personalcontext = context_user::instance($user->id);
00047 
00048 if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
00049         and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
00050     //TODO: do not require parents to be enrolled in courses - this is a hack!
00051     require_login();
00052     $PAGE->set_course($course);
00053 } else {
00054     require_login($course);
00055 }
00056 
00057 list($all, $today) = report_log_can_access_user_report($user, $course);
00058 
00059 if ($type === "userday.png") {
00060     if (!$today) {
00061         require_capability('report/log:viewtoday', $coursecontext);
00062     }
00063 } else {
00064     if (!$all) {
00065         require_capability('report/log:view', $coursecontext);
00066     }
00067 }
00068 
00069 add_to_log($course->id, 'course', 'report log', "report/log/graph.php?user=$user->id&id=$course->id&type=$type&date=$date", $course->id);
00070 
00071 $logs = array();
00072 
00073 $timenow = time();
00074 
00075 if ($type === "usercourse.png") {
00076 
00077    $site = get_site();
00078 
00079    if ($course->id == $site->id) {
00080        $courseselect = 0;
00081    } else {
00082        $courseselect = $course->id;
00083    }
00084 
00085    $maxseconds = REPORT_LOG_MAX_DISPLAY * 3600 * 24;  // seconds
00086    //$maxseconds = 60 * 3600 * 24;  // seconds
00087    if ($timenow - $course->startdate > $maxseconds) {
00088        $course->startdate = $timenow - $maxseconds;
00089    }
00090 
00091    if (!empty($CFG->loglifetime)) {
00092        $maxseconds = $CFG->loglifetime * 3600 * 24;  // seconds
00093        if ($timenow - $course->startdate > $maxseconds) {
00094            $course->startdate = $timenow - $maxseconds;
00095        }
00096    }
00097 
00098    $timestart = $coursestart = usergetmidnight($course->startdate);
00099 
00100    if ((($timenow - $timestart)/86400.0) > 40) {
00101        $reducedays = 7;
00102    } else {
00103        $reducedays = 0;
00104    }
00105 
00106    $days = array();
00107    $i = 0;
00108    while ($timestart < $timenow) {
00109        $timefinish = $timestart + 86400;
00110        if ($reducedays) {
00111            if ($i % $reducedays) {
00112                $days[$i] = "";
00113            } else {
00114                $days[$i] = userdate($timestart, "%a %d %b");
00115            }
00116        } else {
00117            $days[$i] = userdate($timestart, "%a %d %b");
00118        }
00119        $logs[$i] = 0;
00120        $i++;
00121        $timestart = $timefinish;
00122    }
00123 
00124    if ($rawlogs = get_logs_usercourse($user->id, $courseselect, $coursestart)) {
00125        foreach ($rawlogs as $rawlog) {
00126            $logs[$rawlog->day] = $rawlog->num;
00127        }
00128    }
00129 
00130    $graph = new graph(750, 400);
00131 
00132    $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
00133    $a->username = fullname($user, true);
00134    $graph->parameter['title'] = get_string("hitsoncourse", "", $a);
00135 
00136    $graph->x_data           = $days;
00137 
00138    $graph->y_data['logs']   = $logs;
00139    $graph->y_order = array('logs');
00140 
00141    if (!empty($CFG->preferlinegraphs)) {
00142        $graph->y_format['logs'] = array('colour' => 'blue','line' => 'line');
00143    } else {
00144        $graph->y_format['logs'] = array('colour' => 'blue','bar' => 'fill','bar_size' => 0.6);
00145        $graph->parameter['bar_spacing'] = 0;
00146    }
00147 
00148 
00149    $graph->parameter['y_label_left']     = get_string("hits");
00150    $graph->parameter['label_size']       = "12";
00151    $graph->parameter['x_axis_angle']     = 90;
00152    $graph->parameter['x_label_angle']    = 0;
00153    $graph->parameter['tick_length'] = 0;
00154 
00155 
00156    $graph->parameter['shadow']          = 'none';
00157 
00158    error_reporting(5); // ignore most warnings such as font problems etc
00159    $graph->draw_stack();
00160 
00161 } else {
00162 
00163    $site = get_site();
00164 
00165    if ($course->id == $site->id) {
00166        $courseselect = 0;
00167    } else {
00168        $courseselect = $course->id;
00169    }
00170 
00171    if ($date) {
00172        $daystart = usergetmidnight($date);
00173    } else {
00174        $daystart = usergetmidnight(time());
00175    }
00176    $dayfinish = $daystart + 86400;
00177 
00178    $hours = array();
00179    for ($i=0; $i<=23; $i++) {
00180        $logs[$i] = 0;
00181        $hour = $daystart + $i * 3600;
00182        $hours[$i] = $i;
00183    }
00184 
00185    if ($rawlogs = get_logs_userday($user->id, $courseselect, $daystart)) {
00186        foreach ($rawlogs as $rawlog) {
00187            $logs[$rawlog->hour] = $rawlog->num;
00188        }
00189    }
00190 
00191    $graph = new graph(750, 400);
00192 
00193    $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
00194    $a->username = fullname($user, true);
00195    $graph->parameter['title'] = get_string("hitsoncoursetoday", "", $a);
00196 
00197    $graph->x_data           = $hours;
00198 
00199    $graph->y_data['logs']   = $logs;
00200    $graph->y_order = array('logs');
00201 
00202    if (!empty($CFG->preferlinegraphs)) {
00203        $graph->y_format['logs'] = array('colour' => 'blue','line' => 'line');
00204    } else {
00205        $graph->y_format['logs'] = array('colour' => 'blue','bar' => 'fill','bar_size' => 0.9);
00206    }
00207 
00208    $graph->parameter['y_label_left']     = get_string("hits");
00209    $graph->parameter['label_size']       = "12";
00210    $graph->parameter['x_axis_angle']     = 0;
00211    $graph->parameter['x_label_angle']    = 0;
00212 
00213    $graph->parameter['shadow']          = 'none';
00214 
00215    error_reporting(5); // ignore most warnings such as font problems etc
00216    $graph->draw_stack();
00217 }
00218 
 All Data Structures Namespaces Files Functions Variables Enumerations