[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Reports implementation 19 * 20 * @package report 21 * @subpackage stats 22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die; 27 28 require_once (__DIR__.'/lib.php'); 29 require_once($CFG->dirroot.'/lib/statslib.php'); 30 31 function report_stats_mode_menu($course, $mode, $time, $url) { 32 global $CFG, $OUTPUT; 33 /* 34 $reportoptions = stats_get_report_options($course->id, $mode); 35 $timeoptions = report_stats_timeoptions($mode); 36 if (empty($timeoptions)) { 37 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/view.php?id='.$course->id); 38 } 39 */ 40 41 $options = array(); 42 $options[STATS_MODE_GENERAL] = get_string('statsmodegeneral'); 43 $options[STATS_MODE_DETAILED] = get_string('statsmodedetailed'); 44 if (has_capability('report/stats:view', context_system::instance())) { 45 $options[STATS_MODE_RANKED] = get_string('reports'); 46 } 47 $popupurl = $url."?course=$course->id&time=$time"; 48 $select = new single_select(new moodle_url($popupurl), 'mode', $options, $mode, null); 49 $select->set_label(get_string('reports'), array('class' => 'accesshide')); 50 $select->formid = 'switchmode'; 51 return $OUTPUT->render($select); 52 } 53 54 function report_stats_timeoptions($mode) { 55 global $CFG, $DB; 56 57 if ($mode == STATS_MODE_DETAILED) { 58 $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}'); 59 $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}'); 60 $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}'); 61 } else { 62 $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_daily}'); 63 $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_weekly}'); 64 $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_monthly}'); 65 } 66 67 68 if (empty($earliestday)) $earliestday = time(); 69 if (empty($earliestweek)) $earliestweek = time(); 70 if (empty($earliestmonth)) $earliestmonth = time(); 71 72 $now = stats_get_base_daily(); 73 $lastweekend = stats_get_base_weekly(); 74 $lastmonthend = stats_get_base_monthly(); 75 76 return stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth); 77 } 78 79 function report_stats_report($course, $report, $mode, $user, $roleid, $time) { 80 global $CFG, $DB, $OUTPUT; 81 82 if ($user) { 83 $userid = $user->id; 84 } else { 85 $userid = 0; 86 } 87 88 $courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname'); 89 $courseoptions = array(); 90 91 foreach ($courses as $c) { 92 $context = context_course::instance($c->id); 93 94 if (has_capability('report/stats:view', $context)) { 95 $courseoptions[$c->id] = format_string($c->shortname, true, array('context' => $context)); 96 } 97 } 98 99 $reportoptions = stats_get_report_options($course->id, $mode); 100 $timeoptions = report_stats_timeoptions($mode); 101 if (empty($timeoptions)) { 102 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/view.php?id='.$course->id); 103 } 104 105 $users = array(); 106 $table = new html_table(); 107 $table->width = 'auto'; 108 109 if ($mode == STATS_MODE_DETAILED) { 110 $param = stats_get_parameters($time, null, $course->id, $mode); // we only care about the table and the time string (if we have time) 111 112 list($sort, $moreparams) = users_order_by_sql('u'); 113 $moreparams['courseid'] = $course->id; 114 $fields = user_picture::fields('u', array('idnumber')); 115 $sql = "SELECT DISTINCT $fields 116 FROM {stats_user_{$param->table}} s 117 JOIN {user} u ON u.id = s.userid 118 WHERE courseid = :courseid"; 119 if (!empty($param->stattype)) { 120 $sql .= " AND stattype = :stattype"; 121 $moreparams['stattype'] = $param->stattype; 122 } 123 if (!empty($time)) { 124 $sql .= " AND timeend >= :timeafter"; 125 $moreparams['timeafter'] = $param->timeafter; 126 } 127 $sql .= " ORDER BY {$sort}"; 128 129 if (!$us = $DB->get_records_sql($sql, array_merge($param->params, $moreparams))) { 130 print_error('nousers'); 131 } 132 foreach ($us as $u) { 133 $users[$u->id] = fullname($u, true); 134 } 135 136 $table->align = array('left','left','left','left','left','left','left','left'); 137 $table->data[] = array(html_writer::label(get_string('course'), 'menucourse'), html_writer::select($courseoptions, 'course', $course->id, false), 138 html_writer::label(get_string('users'), 'menuuserid'), html_writer::select($users, 'userid', $userid, false), 139 html_writer::label(get_string('statsreporttype'), 'menureport'), html_writer::select($reportoptions, 'report', ($report == 5) ? $report.$roleid : $report, false), 140 html_writer::label(get_string('statstimeperiod'), 'menutime'), html_writer::select($timeoptions, 'time', $time, false), 141 '<input type="submit" value="'.get_string('view').'" />') ; 142 } else if ($mode == STATS_MODE_RANKED) { 143 $table->align = array('left','left','left','left','left','left'); 144 $table->data[] = array(html_writer::label(get_string('statsreporttype'), 'menureport'), html_writer::select($reportoptions, 'report', ($report == 5) ? $report.$roleid : $report, false), 145 html_writer::label(get_string('statstimeperiod'), 'menutime'), html_writer::select($timeoptions, 'time', $time, false), 146 '<input type="submit" value="'.get_string('view').'" />') ; 147 } else if ($mode == STATS_MODE_GENERAL) { 148 $table->align = array('left','left','left','left','left','left','left'); 149 $table->data[] = array(html_writer::label(get_string('course'), 'menucourse'), html_writer::select($courseoptions, 'course', $course->id, false), 150 html_writer::label(get_string('statsreporttype'), 'menureport'), html_writer::select($reportoptions, 'report', ($report == 5) ? $report.$roleid : $report, false), 151 html_writer::label(get_string('statstimeperiod'), 'menutime'), html_writer::select($timeoptions, 'time', $time, false), 152 '<input type="submit" value="'.get_string('view').'" />') ; 153 } 154 155 echo '<form action="index.php" method="post">'."\n" 156 .'<div>'."\n" 157 .'<input type="hidden" name="mode" value="'.$mode.'" />'."\n"; 158 159 echo html_writer::table($table); 160 161 echo '</div>'; 162 echo '</form>'; 163 164 // Display the report if: 165 // - A report has been selected. 166 // - A time frame has been provided 167 // - If the mode is not detailed OR a valid user has been selected. 168 if (!empty($report) && !empty($time) && ($mode !== STATS_MODE_DETAILED || !empty($userid))) { 169 if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) { 170 print_error('reportnotavailable'); 171 } 172 173 $param = stats_get_parameters($time,$report,$course->id,$mode); 174 175 if ($mode == STATS_MODE_DETAILED) { 176 $param->table = 'user_'.$param->table; 177 } 178 179 if (!empty($param->sql)) { 180 $sql = $param->sql; 181 } else { 182 //TODO: lceanup this ugly mess 183 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields 184 .' FROM {stats_'.$param->table.'} WHERE ' 185 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') 186 .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') 187 .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '') 188 . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') 189 .' timeend >= '.$param->timeafter 190 .' '.$param->extras 191 .' ORDER BY timeend DESC'; 192 } 193 194 $stats = $DB->get_records_sql($sql); 195 196 if (empty($stats)) { 197 echo $OUTPUT->notification(get_string('statsnodata')); 198 199 } else { 200 201 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2))); 202 203 echo $OUTPUT->heading(format_string($course->shortname).' - '.get_string('statsreport'.$report) 204 .((!empty($user)) ? ' '.get_string('statsreportforuser').' ' .fullname($user,true) : '') 205 .((!empty($roleid)) ? ' '.$DB->get_field('role','name', array('id'=>$roleid)) : '')); 206 207 208 if ($mode == STATS_MODE_DETAILED) { 209 report_stats_print_chart($course->id, $report, $time, $mode, $userid); 210 } else { 211 report_stats_print_chart($course->id, $report, $time, $mode, null, $roleid); 212 } 213 214 $table = new html_table(); 215 $table->align = array('left','center','center','center'); 216 $param->table = str_replace('user_','',$param->table); 217 switch ($param->table) { 218 case 'daily' : $period = get_string('day'); break; 219 case 'weekly' : $period = get_string('week'); break; 220 case 'monthly': $period = get_string('month', 'form'); break; 221 default : $period = ''; 222 } 223 $table->head = array(get_string('periodending','moodle',$period)); 224 if (empty($param->crosstab)) { 225 $table->head[] = $param->line1; 226 if (!empty($param->line2)) { 227 $table->head[] = $param->line2; 228 } 229 } 230 if (!file_exists($CFG->dirroot.'/report/log/index.php')) { 231 // bad luck, we can not link other report 232 } else if (empty($param->crosstab)) { 233 foreach ($stats as $stat) { 234 $a = array(userdate($stat->timeend-(60*60*24),get_string('strftimedate'),$CFG->timezone),$stat->line1); 235 if (isset($stat->line2)) { 236 $a[] = $stat->line2; 237 } 238 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) { 239 if (has_capability('report/log:view', context_course::instance($course->id))) { 240 $a[] = '<a href="'.$CFG->wwwroot.'/report/log/index.php?id='. 241 $course->id.'&chooselog=1&showusers=1&showcourses=1&user=' 242 .$userid.'&date='.usergetmidnight($stat->timeend-(60*60*24)).'">' 243 .get_string('course').' ' .get_string('logs').'</a> '; 244 } else { 245 $a[] = ''; 246 } 247 } 248 $table->data[] = $a; 249 } 250 } else { 251 $data = array(); 252 $roles = array(); 253 $times = array(); 254 $missedlines = array(); 255 $coursecontext = context_course::instance($course->id); 256 $rolenames = role_fix_names(get_all_roles($coursecontext), $coursecontext, ROLENAME_ALIAS, true); 257 foreach ($stats as $stat) { 258 if (!empty($stat->zerofixed)) { 259 $missedlines[] = $stat->timeend; 260 } 261 $data[$stat->timeend][$stat->roleid] = $stat->line1; 262 if ($stat->roleid != 0) { 263 if (!array_key_exists($stat->roleid,$roles)) { 264 $roles[$stat->roleid] = $rolenames[$stat->roleid]; 265 } 266 } else { 267 if (!array_key_exists($stat->roleid,$roles)) { 268 $roles[$stat->roleid] = get_string('all'); 269 } 270 } 271 if (!array_key_exists($stat->timeend,$times)) { 272 $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone); 273 } 274 } 275 276 foreach ($data as $time => $rolesdata) { 277 if (in_array($time,$missedlines)) { 278 $rolesdata = array(); 279 foreach ($roles as $roleid => $guff) { 280 $rolesdata[$roleid] = 0; 281 } 282 } 283 else { 284 foreach (array_keys($roles) as $r) { 285 if (!array_key_exists($r, $rolesdata)) { 286 $rolesdata[$r] = 0; 287 } 288 } 289 } 290 krsort($rolesdata); 291 $row = array_merge(array($times[$time]),$rolesdata); 292 if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) { 293 if (has_capability('report/log:view', context_course::instance($course->id))) { 294 $row[] = '<a href="'.$CFG->wwwroot.'/report/log/index.php?id=' 295 .$course->id.'&chooselog=1&showusers=1&showcourses=1&user='.$userid 296 .'&date='.usergetmidnight($time-(60*60*24)).'">' 297 .get_string('course').' ' .get_string('logs').'</a> '; 298 } else { 299 $row[] = ''; 300 } 301 } 302 $table->data[] = $row; 303 } 304 krsort($roles); 305 $table->head = array_merge($table->head,$roles); 306 } 307 $table->head[] = get_string('logs'); 308 //if (!empty($lastrecord)) { 309 //$lastrecord[] = $lastlink; 310 //$table->data[] = $lastrecord; 311 //} 312 echo html_writer::table($table); 313 } 314 } 315 } 316 317 /** 318 * Fetch statistics data and generate a line chart. 319 * 320 * The statistic chart can be view, posts separated by roles and dates. 321 * 322 * @param int $courseid course id. 323 * @param int $report the report type constant eg. STATS_REPORT_LOGINS as defined on statslib. 324 * @param int $time timestamp of the selected time period. 325 * @param int $mode the report mode, eg. STATS_MODE_DETAILED as defined on statslib. 326 * @param int $userid selected user id. 327 * @param int $roleid selected role id. 328 */ 329 function report_stats_print_chart($courseid, $report, $time, $mode, $userid = 0, $roleid = 0) { 330 global $DB, $CFG, $OUTPUT; 331 332 $course = $DB->get_record("course", array("id" => $courseid), '*', MUST_EXIST); 333 $coursecontext = context_course::instance($course->id); 334 335 stats_check_uptodate($course->id); 336 337 $param = stats_get_parameters($time, $report, $course->id, $mode); 338 339 if (!empty($userid)) { 340 $param->table = 'user_' . $param->table; 341 } 342 343 // TODO: cleanup this ugly mess. 344 $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields 345 .' FROM {stats_'.$param->table.'} WHERE ' 346 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') 347 .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') 348 .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '') 349 . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') 350 .' timeend >= '.$param->timeafter 351 .' '.$param->extras 352 .' ORDER BY timeend DESC'; 353 $stats = $DB->get_records_sql($sql, $param->params); 354 $stats = stats_fix_zeros($stats, $param->timeafter, $param->table, (!empty($param->line2)), 355 (!empty($param->line3))); 356 $stats = array_reverse($stats); 357 358 $chart = new \core\chart_line(); 359 if (empty($param->crosstab)) { 360 $data = []; 361 $times = []; 362 foreach ($stats as $stat) { 363 // Build the array of formatted times indexed by timestamp used as labels. 364 if (!array_key_exists($stat->timeend, $times)) { 365 $times[$stat->timeend] = userdate($stat->timeend, get_string('strftimedate'), $CFG->timezone); 366 367 // Just add the data if the time hasn't been added yet. 368 // The number of lines of data must match the number of labels. 369 $data['line1'][] = $stat->line1; 370 if (isset($stat->line2)) { 371 $data['line2'][] = $stat->line2; 372 } 373 if (isset($stat->line3)) { 374 $data['line3'][] = $stat->line3; 375 } 376 } 377 } 378 foreach ($data as $line => $serie) { 379 $series = new \core\chart_series($param->{$line}, array_values($serie)); 380 $chart->add_series($series); 381 } 382 } else { 383 $data = array(); 384 $times = array(); 385 $roles = array(); 386 $missedlines = array(); 387 $rolenames = role_fix_names(get_all_roles($coursecontext), $coursecontext, ROLENAME_ALIAS, true); 388 389 foreach ($stats as $stat) { 390 $data[$stat->roleid][$stat->timeend] = $stat->line1; 391 if (!empty($stat->zerofixed)) { 392 $missedlines[] = $stat->timeend; 393 } 394 if ($stat->roleid != 0) { 395 if (!array_key_exists($stat->roleid, $roles)) { 396 $roles[$stat->roleid] = $rolenames[$stat->roleid]; 397 } 398 } else { 399 if (!array_key_exists($stat->roleid, $roles)) { 400 $roles[$stat->roleid] = get_string('all'); 401 } 402 } 403 404 // Build the array of formatted times indexed by timestamp used as labels. 405 if (!array_key_exists($stat->timeend, $times)) { 406 $times[$stat->timeend] = userdate($stat->timeend, get_string('strftimedate'), $CFG->timezone); 407 } 408 } 409 // Fill empty days with zero to avoid chart errors. 410 foreach (array_keys($times) as $t) { 411 foreach ($data as $roleid => $stuff) { 412 if (!array_key_exists($t, $stuff)) { 413 $data[$roleid][$t] = 0; 414 } 415 } 416 } 417 krsort($roles); 418 foreach ($roles as $roleid => $rolename) { 419 ksort($data[$roleid]); 420 $series = new \core\chart_series($rolename, array_values($data[$roleid])); 421 $chart->add_series($series); 422 } 423 } 424 $chart->set_labels(array_values($times)); 425 echo $OUTPUT->render_chart($chart, false); 426 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |