|
Moodle
2.2.1
http://www.collinsharper.com
|
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 defined('MOODLE_INTERNAL') || die; 00027 00028 require_once(dirname(__FILE__).'/lib.php'); 00029 require_once($CFG->dirroot.'/lib/statslib.php'); 00030 00031 function report_stats_mode_menu($course, $mode, $time, $url) { 00032 global $CFG, $OUTPUT; 00033 /* 00034 $reportoptions = stats_get_report_options($course->id, $mode); 00035 $timeoptions = report_stats_timeoptions($mode); 00036 if (empty($timeoptions)) { 00037 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/view.php?id='.$course->id); 00038 } 00039 */ 00040 00041 $options = array(); 00042 $options[STATS_MODE_GENERAL] = get_string('statsmodegeneral'); 00043 $options[STATS_MODE_DETAILED] = get_string('statsmodedetailed'); 00044 if (has_capability('report/stats:view', get_context_instance(CONTEXT_SYSTEM))) { 00045 $options[STATS_MODE_RANKED] = get_string('reports'); 00046 } 00047 $popupurl = $url."?course=$course->id&time=$time"; 00048 $select = new single_select(new moodle_url($popupurl), 'mode', $options, $mode, null); 00049 $select->formid = 'switchmode'; 00050 return $OUTPUT->render($select); 00051 } 00052 00053 function report_stats_timeoptions($mode) { 00054 global $CFG, $DB; 00055 00056 if ($mode == STATS_MODE_DETAILED) { 00057 $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}'); 00058 $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}'); 00059 $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}'); 00060 } else { 00061 $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_daily}'); 00062 $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_weekly}'); 00063 $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_monthly}'); 00064 } 00065 00066 00067 if (empty($earliestday)) $earliestday = time(); 00068 if (empty($earliestweek)) $earliestweek = time(); 00069 if (empty($earliestmonth)) $earliestmonth = time(); 00070 00071 $now = stats_get_base_daily(); 00072 $lastweekend = stats_get_base_weekly(); 00073 $lastmonthend = stats_get_base_monthly(); 00074 00075 return stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth); 00076 } 00077 00078 function report_stats_report($course, $report, $mode, $user, $roleid, $time) { 00079 global $CFG, $DB, $OUTPUT; 00080 00081 if ($user) { 00082 $userid = $user->id; 00083 } else { 00084 $userid = 0; 00085 } 00086 00087 $courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname'); 00088 $courseoptions = array(); 00089 00090 foreach ($courses as $c) { 00091 $context = get_context_instance(CONTEXT_COURSE, $c->id); 00092 00093 if (has_capability('report/stats:view', $context)) { 00094 $courseoptions[$c->id] = format_string($c->shortname, true, array('context' => $context)); 00095 } 00096 } 00097 00098 $reportoptions = stats_get_report_options($course->id, $mode); 00099 $timeoptions = report_stats_timeoptions($mode); 00100 if (empty($timeoptions)) { 00101 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/view.php?id='.$course->id); 00102 } 00103 00104 $users = array(); 00105 $table = new html_table(); 00106 $table->width = 'auto'; 00107 00108 if ($mode == STATS_MODE_DETAILED) { 00109 $param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string (if we have time) 00110 00111 //TODO: lceanup this ugly mess 00112 $sql = 'SELECT DISTINCT s.userid, u.firstname, u.lastname, u.idnumber 00113 FROM {stats_user_'.$param->table.'} s 00114 JOIN {user} u ON u.id = s.userid 00115 WHERE courseid = '.$course->id 00116 . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '') 00117 . ((!empty($time)) ? ' AND timeend >= '.$param->timeafter : '') 00118 .' ORDER BY u.lastname, u.firstname ASC'; 00119 00120 if (!$us = $DB->get_records_sql($sql, $param->params)) { 00121 print_error('nousers'); 00122 } 00123 00124 foreach ($us as $u) { 00125 $users[$u->userid] = fullname($u, true); 00126 } 00127 00128 $table->align = array('left','left','left','left','left','left','left','left'); 00129 $table->data[] = array(get_string('course'),html_writer::select($courseoptions,'course',$course->id,false), 00130 get_string('users'),html_writer::select($users,'userid',$userid,false), 00131 get_string('statsreporttype'),html_writer::select($reportoptions,'report',($report == 5) ? $report.$roleid : $report,false), 00132 get_string('statstimeperiod'),html_writer::select($timeoptions,'time',$time,false), 00133 '<input type="submit" value="'.get_string('view').'" />') ; 00134 } else if ($mode == STATS_MODE_RANKED) { 00135 $table->align = array('left','left','left','left','left','left'); 00136 $table->data[] = array(get_string('statsreporttype'),html_writer::select($reportoptions,'report',($report == 5) ? $report.$roleid : $report,false), 00137 get_string('statstimeperiod'),html_writer::select($timeoptions,'time',$time,false), 00138 '<input type="submit" value="'.get_string('view').'" />') ; 00139 } else if ($mode == STATS_MODE_GENERAL) { 00140 $table->align = array('left','left','left','left','left','left','left'); 00141 $table->data[] = array(get_string('course'),html_writer::select($courseoptions,'course',$course->id,false), 00142 get_string('statsreporttype'),html_writer::select($reportoptions,'report',($report == 5) ? $report.$roleid : $report,false), 00143 get_string('statstimeperiod'),html_writer::select($timeoptions,'time',$time,false), 00144 '<input type="submit" value="'.get_string('view').'" />') ; 00145 } 00146 00147 echo '<form action="index.php" method="post">'."\n" 00148 .'<div>'."\n" 00149 .'<input type="hidden" name="mode" value="'.$mode.'" />'."\n"; 00150 00151 echo html_writer::table($table); 00152 00153 echo '</div>'; 00154 echo '</form>'; 00155 00156 if (!empty($report) && !empty($time)) { 00157 if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) { 00158 print_error('reportnotavailable'); 00159 } 00160 00161 $param = stats_get_parameters($time,$report,$course->id,$mode); 00162 00163 if ($mode == STATS_MODE_DETAILED) { 00164 $param->table = 'user_'.$param->table; 00165 } 00166 00167 if (!empty($param->sql)) { 00168 $sql = $param->sql; 00169 } else { 00170 //TODO: lceanup this ugly mess 00171 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields 00172 .' FROM {stats_'.$param->table.'} WHERE ' 00173 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') 00174 .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') 00175 .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '') 00176 . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') 00177 .' timeend >= '.$param->timeafter 00178 .' '.$param->extras 00179 .' ORDER BY timeend DESC'; 00180 } 00181 00182 $stats = $DB->get_records_sql($sql); 00183 00184 if (empty($stats)) { 00185 echo $OUTPUT->notification(get_string('statsnodata')); 00186 00187 } else { 00188 00189 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2))); 00190 00191 echo $OUTPUT->heading(format_string($course->shortname).' - '.get_string('statsreport'.$report) 00192 .((!empty($user)) ? ' '.get_string('statsreportforuser').' ' .fullname($user,true) : '') 00193 .((!empty($roleid)) ? ' '.$DB->get_field('role','name', array('id'=>$roleid)) : '')); 00194 00195 00196 if (empty($CFG->gdversion)) { 00197 echo "(".get_string("gdneed").")"; 00198 } else { 00199 if ($mode == STATS_MODE_DETAILED) { 00200 echo '<div class="graph"><img src="'.$CFG->wwwroot.'/report/stats/graph.php?mode='.$mode.'&course='.$course->id.'&time='.$time.'&report='.$report.'&userid='.$userid.'" alt="'.get_string('statisticsgraph').'" /></div'; 00201 } else { 00202 echo '<div class="graph"><img src="'.$CFG->wwwroot.'/report/stats/graph.php?mode='.$mode.'&course='.$course->id.'&time='.$time.'&report='.$report.'&roleid='.$roleid.'" alt="'.get_string('statisticsgraph').'" /></div>'; 00203 } 00204 } 00205 00206 $table = new html_table(); 00207 $table->align = array('left','center','center','center'); 00208 $param->table = str_replace('user_','',$param->table); 00209 switch ($param->table) { 00210 case 'daily' : $period = get_string('day'); break; 00211 case 'weekly' : $period = get_string('week'); break; 00212 case 'monthly': $period = get_string('month', 'form'); break; 00213 default : $period = ''; 00214 } 00215 $table->head = array(get_string('periodending','moodle',$period)); 00216 if (empty($param->crosstab)) { 00217 $table->head[] = $param->line1; 00218 if (!empty($param->line2)) { 00219 $table->head[] = $param->line2; 00220 } 00221 } 00222 if (!file_exists($CFG->dirroot.'/report/log/index.php')) { 00223 // bad luck, we can not link other report 00224 } else if (empty($param->crosstab)) { 00225 foreach ($stats as $stat) { 00226 $a = array(userdate($stat->timeend-(60*60*24),get_string('strftimedate'),$CFG->timezone),$stat->line1); 00227 if (isset($stat->line2)) { 00228 $a[] = $stat->line2; 00229 } 00230 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) { 00231 if (has_capability('report/log:view', get_context_instance(CONTEXT_COURSE, $course->id))) { 00232 $a[] = '<a href="'.$CFG->wwwroot.'/report/log/index.php?id='. 00233 $course->id.'&chooselog=1&showusers=1&showcourses=1&user=' 00234 .$userid.'&date='.usergetmidnight($stat->timeend-(60*60*24)).'">' 00235 .get_string('course').' ' .get_string('logs').'</a> '; 00236 } else { 00237 $a[] = ''; 00238 } 00239 } 00240 $table->data[] = $a; 00241 } 00242 } else { 00243 $data = array(); 00244 $roles = array(); 00245 $times = array(); 00246 $missedlines = array(); 00247 $rolenames = get_all_roles(); 00248 foreach ($rolenames as $r) { 00249 $rolenames[$r->id] = $r->name; 00250 } 00251 $rolenames = role_fix_names($rolenames, get_context_instance(CONTEXT_COURSE, $course->id)); 00252 foreach ($stats as $stat) { 00253 if (!empty($stat->zerofixed)) { 00254 $missedlines[] = $stat->timeend; 00255 } 00256 $data[$stat->timeend][$stat->roleid] = $stat->line1; 00257 if ($stat->roleid != 0) { 00258 if (!array_key_exists($stat->roleid,$roles)) { 00259 $roles[$stat->roleid] = $rolenames[$stat->roleid]; 00260 } 00261 } else { 00262 if (!array_key_exists($stat->roleid,$roles)) { 00263 $roles[$stat->roleid] = get_string('all'); 00264 } 00265 } 00266 if (!array_key_exists($stat->timeend,$times)) { 00267 $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone); 00268 } 00269 } 00270 00271 foreach ($data as $time => $rolesdata) { 00272 if (in_array($time,$missedlines)) { 00273 $rolesdata = array(); 00274 foreach ($roles as $roleid => $guff) { 00275 $rolesdata[$roleid] = 0; 00276 } 00277 } 00278 else { 00279 foreach (array_keys($roles) as $r) { 00280 if (!array_key_exists($r, $rolesdata)) { 00281 $rolesdata[$r] = 0; 00282 } 00283 } 00284 } 00285 krsort($rolesdata); 00286 $row = array_merge(array($times[$time]),$rolesdata); 00287 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) { 00288 if (has_capability('report/log:view', get_context_instance(CONTEXT_COURSE, $course->id))) { 00289 $row[] = '<a href="'.$CFG->wwwroot.'/report/log/index.php?id=' 00290 .$course->id.'&chooselog=1&showusers=1&showcourses=1&user='.$userid 00291 .'&date='.usergetmidnight($time-(60*60*24)).'">' 00292 .get_string('course').' ' .get_string('logs').'</a> '; 00293 } else { 00294 $row[] = ''; 00295 } 00296 } 00297 $table->data[] = $row; 00298 } 00299 krsort($roles); 00300 $table->head = array_merge($table->head,$roles); 00301 } 00302 $table->head[] = get_string('logs'); 00303 //if (!empty($lastrecord)) { 00304 //$lastrecord[] = $lastlink; 00305 //$table->data[] = $lastrecord; 00306 //} 00307 echo html_writer::table($table); 00308 } 00309 } 00310 }