. /** * Reports implementation * * @package report * @subpackage stats * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die; require_once(dirname(__FILE__).'/lib.php'); require_once($CFG->dirroot.'/lib/statslib.php'); function report_stats_mode_menu($course, $mode, $time, $url) { global $CFG, $OUTPUT; /* $reportoptions = stats_get_report_options($course->id, $mode); $timeoptions = report_stats_timeoptions($mode); if (empty($timeoptions)) { print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/view.php?id='.$course->id); } */ $options = array(); $options[STATS_MODE_GENERAL] = get_string('statsmodegeneral'); $options[STATS_MODE_DETAILED] = get_string('statsmodedetailed'); if (has_capability('report/stats:view', context_system::instance())) { $options[STATS_MODE_RANKED] = get_string('reports'); } $popupurl = $url."?course=$course->id&time=$time"; $select = new single_select(new moodle_url($popupurl), 'mode', $options, $mode, null); $select->set_label(get_string('reports'), array('class' => 'accesshide')); $select->formid = 'switchmode'; return $OUTPUT->render($select); } function report_stats_timeoptions($mode) { global $CFG, $DB; if ($mode == STATS_MODE_DETAILED) { $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_daily}'); $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_weekly}'); $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_user_monthly}'); } else { $earliestday = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_daily}'); $earliestweek = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_weekly}'); $earliestmonth = $DB->get_field_sql('SELECT MIN(timeend) FROM {stats_monthly}'); } if (empty($earliestday)) $earliestday = time(); if (empty($earliestweek)) $earliestweek = time(); if (empty($earliestmonth)) $earliestmonth = time(); $now = stats_get_base_daily(); $lastweekend = stats_get_base_weekly(); $lastmonthend = stats_get_base_monthly(); return stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth); } function report_stats_report($course, $report, $mode, $user, $roleid, $time) { global $CFG, $DB, $OUTPUT; if ($user) { $userid = $user->id; } else { $userid = 0; } $courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname'); $courseoptions = array(); foreach ($courses as $c) { $context = context_course::instance($c->id); if (has_capability('report/stats:view', $context)) { $courseoptions[$c->id] = format_string($c->shortname, true, array('context' => $context)); } } $reportoptions = stats_get_report_options($course->id, $mode); $timeoptions = report_stats_timeoptions($mode); if (empty($timeoptions)) { print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/view.php?id='.$course->id); } $users = array(); $table = new html_table(); $table->width = 'auto'; if ($mode == STATS_MODE_DETAILED) { $param = stats_get_parameters($time, null, $course->id, $mode); // we only care about the table and the time string (if we have time) list($sort, $moreparams) = users_order_by_sql('u'); $moreparams['courseid'] = $course->id; $fields = user_picture::fields('u', array('idnumber')); $sql = "SELECT DISTINCT $fields FROM {stats_user_{$param->table}} s JOIN {user} u ON u.id = s.userid WHERE courseid = :courseid"; if (!empty($param->stattype)) { $sql .= " AND stattype = :stattype"; $moreparams['stattype'] = $param->stattype; } if (!empty($time)) { $sql .= " AND timeend >= :timeafter"; $moreparams['timeafter'] = $param->timeafter; } $sql .= " ORDER BY {$sort}"; if (!$us = $DB->get_records_sql($sql, array_merge($param->params, $moreparams))) { print_error('nousers'); } foreach ($us as $u) { $users[$u->id] = fullname($u, true); } $table->align = array('left','left','left','left','left','left','left','left'); $table->data[] = array(html_writer::label(get_string('course'), 'menucourse'), html_writer::select($courseoptions, 'course', $course->id, false), html_writer::label(get_string('users'), 'menuuserid'), html_writer::select($users, 'userid', $userid, false), html_writer::label(get_string('statsreporttype'), 'menureport'), html_writer::select($reportoptions, 'report', ($report == 5) ? $report.$roleid : $report, false), html_writer::label(get_string('statstimeperiod'), 'menutime'), html_writer::select($timeoptions, 'time', $time, false), '') ; } else if ($mode == STATS_MODE_RANKED) { $table->align = array('left','left','left','left','left','left'); $table->data[] = array(html_writer::label(get_string('statsreporttype'), 'menureport'), html_writer::select($reportoptions, 'report', ($report == 5) ? $report.$roleid : $report, false), html_writer::label(get_string('statstimeperiod'), 'menutime'), html_writer::select($timeoptions, 'time', $time, false), '') ; } else if ($mode == STATS_MODE_GENERAL) { $table->align = array('left','left','left','left','left','left','left'); $table->data[] = array(html_writer::label(get_string('course'), 'menucourse'), html_writer::select($courseoptions, 'course', $course->id, false), html_writer::label(get_string('statsreporttype'), 'menureport'), html_writer::select($reportoptions, 'report', ($report == 5) ? $report.$roleid : $report, false), html_writer::label(get_string('statstimeperiod'), 'menutime'), html_writer::select($timeoptions, 'time', $time, false), '') ; } echo '
'; // Display the report if: // - A report has been selected. // - A time frame has been provided // - If the mode is not detailed OR a valid user has been selected. if (!empty($report) && !empty($time) && ($mode !== STATS_MODE_DETAILED || !empty($userid))) { if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) { print_error('reportnotavailable'); } $param = stats_get_parameters($time,$report,$course->id,$mode); if ($mode == STATS_MODE_DETAILED) { $param->table = 'user_'.$param->table; } if (!empty($param->sql)) { $sql = $param->sql; } else { //TODO: lceanup this ugly mess $sql = 'SELECT '.((empty($param->fieldscomplete)) ? 'id,roleid,timeend,' : '').$param->fields .' FROM {stats_'.$param->table.'} WHERE ' .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') .((!empty($roleid)) ? ' roleid = '.$roleid.' AND ' : '') . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') .' timeend >= '.$param->timeafter .' '.$param->extras .' ORDER BY timeend DESC'; } $stats = $DB->get_records_sql($sql); if (empty($stats)) { echo $OUTPUT->notification(get_string('statsnodata')); } else { $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2))); echo $OUTPUT->heading(format_string($course->shortname).' - '.get_string('statsreport'.$report) .((!empty($user)) ? ' '.get_string('statsreportforuser').' ' .fullname($user,true) : '') .((!empty($roleid)) ? ' '.$DB->get_field('role','name', array('id'=>$roleid)) : '')); if ($mode == STATS_MODE_DETAILED) { echo '