Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/report/stats/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->dirroot.'/report/stats/locallib.php');
00028 require_once($CFG->dirroot.'/lib/graphlib.php');
00029 
00030 $courseid = required_param('course', PARAM_INT);
00031 $report   = required_param('report', PARAM_INT);
00032 $time     = required_param('time', PARAM_INT);
00033 $mode     = required_param('mode', PARAM_INT);
00034 $userid   = optional_param('userid', 0, PARAM_INT);
00035 $roleid   = optional_param('roleid',0,PARAM_INT);
00036 
00037 $url = new moodle_url('/report/stats/graph.php', array('course'=>$courseid, 'report'=>$report, 'time'=>$time, 'mode'=>$mode, 'userid'=>$userid, 'roleid'=>$roleid));
00038 $PAGE->set_url($url);
00039 
00040 $course = $DB->get_record("course", array("id"=>$courseid), '*', MUST_EXIST);
00041 $coursecontext   = context_course::instance($course->id);
00042 $PAGE->set_context($coursecontext);
00043 
00044 if (!empty($userid)) {
00045     $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
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     if (!report_stats_can_access_user_report($user, $course, true)) {
00058         require_capability('report/stats:view', $coursecontext);
00059     }
00060 
00061 } else {
00062     require_capability('report/stats:view', $coursecontext);
00063 }
00064 
00065 add_to_log($course->id, 'course', 'report stats', "report/stats/graph.php?userid=$userid&id=$course->id&mode=$mode&roleid=$roleid", $course->id);
00066 
00067 stats_check_uptodate($course->id);
00068 
00069 $param = stats_get_parameters($time,$report,$course->id,$mode);
00070 
00071 if (!empty($userid)) {
00072     $param->table = 'user_'.$param->table;
00073 }
00074 
00075 // TODO: cleanup this ugly mess!
00076 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields
00077 .' FROM {stats_'.$param->table.'} WHERE '
00078 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
00079  .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '')
00080  .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '')
00081  . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '')
00082  .' timeend >= '.$param->timeafter
00083 .' '.$param->extras
00084 .' ORDER BY timeend DESC';
00085 
00086 $stats = $DB->get_records_sql($sql, $param->params);
00087 
00088 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
00089 
00090 $stats = array_reverse($stats);
00091 
00092 $graph = new graph(750,400);
00093 
00094 $graph->parameter['legend'] = 'outside-right';
00095 $graph->parameter['legend_size'] = 10;
00096 $graph->parameter['x_axis_angle'] = 90;
00097 $graph->parameter['title'] = false; // moodle will do a nicer job.
00098 $graph->y_tick_labels = null;
00099 
00100 if (empty($param->crosstab)) {
00101     foreach ($stats as $stat) {
00102         $graph->x_data[] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
00103         $graph->y_data['line1'][] = $stat->line1;
00104         if (isset($stat->line2)) {
00105             $graph->y_data['line2'][] = $stat->line2;
00106         }
00107         if (isset($stat->line3)) {
00108             $graph->y_data['line3'][] = $stat->line3;
00109         }
00110     }
00111     $graph->y_order = array('line1');
00112     $graph->y_format['line1'] = array('colour' => 'blue','line' => 'line','legend' => $param->line1);
00113     if (!empty($param->line2)) {
00114         $graph->y_order[] = 'line2';
00115         $graph->y_format['line2'] = array('colour' => 'green','line' => 'line','legend' => $param->line2);
00116     }
00117     if (!empty($param->line3)) {
00118         $graph->y_order[] = 'line3';
00119         $graph->y_format['line3'] = array('colour' => 'red','line' => 'line','legend' => $param->line3);
00120     }
00121     $graph->y_tick_labels = false;
00122 
00123 } else {
00124     $data = array();
00125     $times = array();
00126     $roles = array();
00127     $missedlines = array();
00128     $rolenames = get_all_roles();
00129     foreach ($rolenames as $r) {
00130         $rolenames[$r->id] = $r->name;
00131     }
00132     $rolenames = role_fix_names($rolenames, get_context_instance(CONTEXT_COURSE, $course->id));
00133     foreach ($stats as $stat) {
00134         $data[$stat->roleid][$stat->timeend] = $stat->line1;
00135         if (!empty($stat->zerofixed)) {
00136             $missedlines[] = $stat->timeend;
00137         }
00138         if ($stat->roleid != 0) {
00139             if (!array_key_exists($stat->roleid,$roles)) {
00140                 $roles[$stat->roleid] = $rolenames[$stat->roleid];
00141             }
00142         } else {
00143             if (!array_key_exists($stat->roleid,$roles)) {
00144                 $roles[$stat->roleid] = get_string('all');
00145             }
00146         }
00147         if (!array_key_exists($stat->timeend,$times)) {
00148             $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
00149         }
00150     }
00151     foreach (array_keys($times) as $t) {
00152         foreach ($data as $roleid => $stuff) {
00153             if (!array_key_exists($t, $stuff)) {
00154                 $data[$roleid][$t] = 0;
00155             }
00156         }
00157     }
00158 
00159     $roleid = 0;
00160     krsort($roles); // the same sorting as in table below graph
00161 
00162     $colors = array('green', 'blue', 'red', 'purple', 'yellow', 'olive', 'navy', 'maroon', 'gray', 'ltred', 'ltltred', 'ltgreen', 'ltltgreen', 'orange', 'ltorange', 'ltltorange', 'lime', 'ltblue', 'ltltblue', 'fuchsia', 'aqua', 'grayF0', 'grayEE', 'grayDD', 'grayCC', 'gray33', 'gray66', 'gray99');
00163     $colorindex = 0;
00164 
00165     foreach ($roles as $roleid=>$rname) {
00166         ksort($data[$roleid]);
00167         $graph->y_order[] = $roleid+1;
00168         if ($roleid) {
00169             $color = $colors[$colorindex++];
00170             $colorindex = $colorindex % count($colors);
00171         } else {
00172             $color = 'black';
00173         }
00174         $graph->y_format[$roleid+1] = array('colour' => $color, 'line' => 'line','legend' => $rname);
00175     }
00176     foreach (array_keys($data[$roleid]) as $time) {
00177         $graph->x_data[] = $times[$time];
00178     }
00179     foreach ($data as $roleid => $t) {
00180         foreach ($t as $time => $data) {
00181             $graph->y_data[$roleid+1][] = $data;
00182         }
00183     }
00184 }
00185 
00186 $graph->draw_stack();
00187 
 All Data Structures Namespaces Files Functions Variables Enumerations