From 030605e57a119afb265fa0988acade78cc6204da Mon Sep 17 00:00:00 2001 From: vyshane Date: Tue, 6 Jun 2006 04:15:04 +0000 Subject: [PATCH] Merge from 1.6. Refactored this report module so that we can use it as an admin report module. --- course/report/stats/graph.php | 1 + course/report/stats/index.php | 193 ++------------------------------- course/report/stats/lib.php | 64 +++++++++++ course/report/stats/report.php | 142 ++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 181 deletions(-) create mode 100644 course/report/stats/lib.php create mode 100644 course/report/stats/report.php diff --git a/course/report/stats/graph.php b/course/report/stats/graph.php index 5af5603464..610710bad2 100644 --- a/course/report/stats/graph.php +++ b/course/report/stats/graph.php @@ -40,6 +40,7 @@ .' timeend >= '.$param->timeafter .$param->extras .' ORDER BY timeend DESC'; + $stats = get_records_sql($sql); $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3))); diff --git a/course/report/stats/index.php b/course/report/stats/index.php index 4db426d227..e45c200acd 100644 --- a/course/report/stats/index.php +++ b/course/report/stats/index.php @@ -2,6 +2,7 @@ require_once('../../../config.php'); require_once($CFG->dirroot.'/lib/statslib.php'); + require_once($CFG->dirroot.'/course/report/stats/lib.php'); if (empty($CFG->enablestats)) { error("Stats is not enabled."); @@ -37,194 +38,24 @@ } add_to_log($course->id, "course", "report stats", "report/stats/index.php?course=$course->id", $course->id); - stats_check_uptodate($course->id); - + + $strreports = get_string("reports"); $strstats = get_string('stats'); - - $reportoptions = stats_get_report_options($courseid,$mode); - $courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname'); - $courseoptions = array(); + $menu = report_stats_mode_menu($course, $mode, $time); - foreach ($courses as $c) { - if (isteacher($c->id)) { - $courseoptions[$c->id] = $c->shortname; - } - } + $crumb = "id\">$course->shortname -> + id\">$strreports -> + $strstats"; - $tableprefix = $CFG->prefix.'stats_'; - - if ($mode == STATS_MODE_DETAILED) { - $tableprefix = $CFG->prefix.'stats_user_'; - } - - $earliestday = get_field_sql('SELECT timeend FROM '.$tableprefix.'daily ORDER BY timeend LIMIT 1'); - $earliestweek = get_field_sql('SELECT timeend FROM '.$tableprefix.'weekly ORDER BY timeend LIMIT 1'); - $earliestmonth = get_field_sql('SELECT timeend FROM '.$tableprefix.'monthly ORDER BY timeend LIMIT 1'); + print_header("$course->shortname: $strstats", "$course->fullname", + $crumb, '', '', true, ' ', $menu); - 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(); - - $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth); - - if (empty($timeoptions)) { - error(get_string('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 (isadmin()) { - $options[STATS_MODE_RANKED] = get_string('reports'); - } - - $menu = choose_from_menu($options,'mode',$mode,'','this.form.submit();',0,true); - - $menu = '
'."\n" - .''."\n" - .''."\n" - .$menu."\n".'
'; - - print_header("$course->shortname: $strstats", "$course->fullname", - "id\">$course->shortname -> - id\">$strreports -> - $strstats", '','',true,' ',$menu); - - echo '
'."\n" - .''."\n"; - - $table->width = '*'; - - if ($mode == STATS_MODE_DETAILED) { - if (!empty($time)) { - $param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string. - $sql = 'SELECT DISTINCT s.userid,s.roleid,u.firstname,u.lastname,u.idnumber FROM '.$CFG->prefix.'stats_user_'.$param->table.' s JOIN '.$CFG->prefix.'user u ON u.id = s.userid ' - .'WHERE courseid = '.$course->id.' AND timeend >= '.$param->timeafter . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : ''); - if (!isadmin()) { - $sql .= ' AND (s.roleid = 1 OR s.userid = '.$USER->id .")"; - } - $sql .= " ORDER BY s.roleid "; - } else { - $sql = 'SELECT s.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid FROM '.$CFG->prefix.'user_students s JOIN '.$CFG->prefix.'user u ON u.id = s.userid WHERE course = '.$course->id; - } - - if (!$us = get_records_sql($sql)) { - error('Cannot enter detailed view: No users found for this course.'); - } - $admins = get_admins(); - foreach ($us as $u) { - $role = $course->student; - if ($u->roleid == 2) { - $role = $course->teacher; - } - if (array_key_exists($u->userid,$admins)) { - $role = get_string('admin'); - } - $users[$u->userid] = $role.' - '.fullname($u,true); - } - if (empty($time)) { - if (isadmin()) { - $sql = 'SELECT t.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid FROM '.$CFG->prefix.'user_teachers t JOIN '.$CFG->prefix.'user u ON u.id = t.userid WHERE course = '.$course->id; - $moreusers = get_records_sql($sql); - foreach ($moreusers as $u) { - $users[$u->userid] = $course->teacher .' - '.fullname($u,true); - } - } else { - $users[$USER->id] = $course->teacher.' - '.fullname($USER,true); - } - } - - // make sure we sort so teachers are at the top. - if (strcmp($course->student,$course->teacher) < 0) { - arsort($users); - } else { - asort($users); - } - - $table->align = array('left','left','left','left','left','left','left','left'); - $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true), - get_string('users'),choose_from_menu($users,'userid',$userid,'','','',true), - get_string('statsreporttype'),choose_from_menu($reportoptions,'report',$report,'','','',true), - get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true), - '') ; - } else { - $table->align = array('left','left','left','left','left','left','left'); - $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true), - get_string('statsreporttype'),choose_from_menu($reportoptions,'report',$report,'','','',true), - get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true), - '') ; - } - - - print_table($table); - echo '
'; - - if (!empty($report) && !empty($time)) { - if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) { - error("This type of report is only available for the site course"); - } - $param = stats_get_parameters($time,$report,$course->id,$mode); - if ($mode == STATS_MODE_DETAILED) { - $param->table = 'user_'.$param->table; - } - $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE ' - .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') - .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') - . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') - .' timeend >= '.$param->timeafter - .$param->extras - .' ORDER BY timeend DESC'; - - $stats = get_records_sql($sql); - - if (empty($stats)) { - error(get_string('statsnodata'.((!empty($user)) ? 'user' : '')),$CFG->wwwroot.'/stats/index.php?course='.$course->id.'&mode='.$mode.'&time='.$time); - } - - $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2))); - - print_heading($course->shortname.' - '.get_string('statsreport'.$report).((!empty($user)) ? ' '.get_string('statsreportforuser').' ' .fullname($user,true) : '')); - - if (empty($CFG->gdversion)) { - echo "(".get_string("gdneed").")"; - } else { - if ($mode == STATS_MODE_DETAILED) { - echo '
'; - } else { - echo '
'; - } - } - - $table = new object(); - $table->align = array('left','center','center','center'); - $param->table = str_replace('user_','',$param->table); - $table->head = array(get_string('periodending','moodle',$param->table),$param->line1); - if (!empty($param->line2)) { - $table->head[] = $param->line2; - } - $table->head[] = get_string('logs'); - - foreach ($stats as $stat) { - $a = array(userdate($stat->timeend-(60*60*24),get_string('strftimedate'),$CFG->timezone),$stat->line1); - if (isset($stat->line2)) { - $a[] = $stat->line2; - } - if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) { - $a[] = ''.get_string('course').' ' .get_string('logs').' '; - } - $table->data[] = $a; - } - print_table($table); - } + + require_once($CFG->dirroot.'/course/report/stats/report.php'); print_footer(); - -?> +?> \ No newline at end of file diff --git a/course/report/stats/lib.php b/course/report/stats/lib.php new file mode 100644 index 0000000000..be2f16efb4 --- /dev/null +++ b/course/report/stats/lib.php @@ -0,0 +1,64 @@ +dirroot.'/lib/statslib.php'); + + + function report_stats_mode_menu($course, $mode, $time) { + global $CFG; + + $reportoptions = stats_get_report_options($course->id, $mode); + $timeoptions = report_stats_timeoptions($mode); + + if (empty($timeoptions)) { + error(get_string('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 (isadmin()) { + $options[STATS_MODE_RANKED] = get_string('reports'); + } + + $menu = choose_from_menu($options,'mode',$mode,'','this.form.submit();',0,true); + + $menu = '
'."\n" + .''."\n" + .''."\n" + .$menu."\n".'
'; + + return $menu; + } + + + function report_stats_timeoptions($mode) { + global $CFG; + + $tableprefix = $CFG->prefix.'stats_'; + + if ($mode == STATS_MODE_DETAILED) { + $tableprefix = $CFG->prefix.'stats_user_'; + } + + $earliestday = get_field_sql('SELECT timeend FROM '.$tableprefix.'daily ORDER BY timeend LIMIT 1'); + $earliestweek = get_field_sql('SELECT timeend FROM '.$tableprefix.'weekly ORDER BY timeend LIMIT 1'); + $earliestmonth = get_field_sql('SELECT timeend FROM '.$tableprefix.'monthly ORDER BY timeend LIMIT 1'); + + 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); + } + + +?> \ No newline at end of file diff --git a/course/report/stats/report.php b/course/report/stats/report.php new file mode 100644 index 0000000000..b413fb2903 --- /dev/null +++ b/course/report/stats/report.php @@ -0,0 +1,142 @@ +id)) { + $courseoptions[$c->id] = $c->shortname; + } + } + + + echo '
'."\n" + .''."\n"; + + $reportoptions = stats_get_report_options($course->id, $mode); + $timeoptions = report_stats_timeoptions($mode); + $table->width = '*'; + + if ($mode == STATS_MODE_DETAILED) { + if (!empty($time)) { + $param = stats_get_parameters($time,null,$course->id,$mode); // we only care about the table and the time string. + $sql = 'SELECT DISTINCT s.userid,s.roleid,u.firstname,u.lastname,u.idnumber FROM '.$CFG->prefix.'stats_user_'.$param->table.' s JOIN '.$CFG->prefix.'user u ON u.id = s.userid ' + .'WHERE courseid = '.$course->id.' AND timeend >= '.$param->timeafter . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : ''); + if (!isadmin()) { + $sql .= ' AND (s.roleid = 1 OR s.userid = '.$USER->id .")"; + } + $sql .= " ORDER BY s.roleid "; + } else { + $sql = 'SELECT s.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid FROM '.$CFG->prefix.'user_students s JOIN '.$CFG->prefix.'user u ON u.id = s.userid WHERE course = '.$course->id; + } + + if (!$us = get_records_sql($sql)) { + error('Cannot enter detailed view: No users found for this course.'); + } + $admins = get_admins(); + foreach ($us as $u) { + $role = $course->student; + if ($u->roleid == 2) { + $role = $course->teacher; + } + if (array_key_exists($u->userid,$admins)) { + $role = get_string('admin'); + } + $users[$u->userid] = $role.' - '.fullname($u,true); + } + if (empty($time)) { + if (isadmin()) { + $sql = 'SELECT t.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid FROM '.$CFG->prefix.'user_teachers t JOIN '.$CFG->prefix.'user u ON u.id = t.userid WHERE course = '.$course->id; + $moreusers = get_records_sql($sql); + foreach ($moreusers as $u) { + $users[$u->userid] = $course->teacher .' - '.fullname($u,true); + } + } else { + $users[$USER->id] = $course->teacher.' - '.fullname($USER,true); + } + } + + // make sure we sort so teachers are at the top. + if (strcmp($course->student,$course->teacher) < 0) { + arsort($users); + } else { + asort($users); + } + + $table->align = array('left','left','left','left','left','left','left','left'); + $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true), + get_string('users'),choose_from_menu($users,'userid',$userid,'','','',true), + get_string('statsreporttype'),choose_from_menu($reportoptions,'report',$report,'','','',true), + get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true), + '') ; + } else { + $table->align = array('left','left','left','left','left','left','left'); + $table->data[] = array(get_string('course'),choose_from_menu($courseoptions,'course',$course->id,'','','',true), + get_string('statsreporttype'),choose_from_menu($reportoptions,'report',$report,'','','',true), + get_string('statstimeperiod'),choose_from_menu($timeoptions,'time',$time,'','','',true), + '') ; + } + + + print_table($table); + echo '
'; + + if (!empty($report) && !empty($time)) { + if ($report == STATS_REPORT_LOGINS && $course->id != SITEID) { + error("This type of report is only available for the site course"); + } + $param = stats_get_parameters($time,$report,$course->id,$mode); + if ($mode == STATS_MODE_DETAILED) { + $param->table = 'user_'.$param->table; + } + $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE ' + .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ') + .((!empty($userid)) ? ' userid = '.$userid.' AND ' : '') + . ((!empty($param->stattype)) ? ' stattype = \''.$param->stattype.'\' AND ' : '') + .' timeend >= '.$param->timeafter + .$param->extras + .' ORDER BY timeend DESC'; + + $stats = get_records_sql($sql); + + if (empty($stats)) { + error(get_string('statsnodata'.((!empty($user)) ? 'user' : '')),$CFG->wwwroot.'/stats/index.php?course='.$course->id.'&mode='.$mode.'&time='.$time); + } + + $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2))); + + print_heading($course->shortname.' - '.get_string('statsreport'.$report).((!empty($user)) ? ' '.get_string('statsreportforuser').' ' .fullname($user,true) : '')); + + if (empty($CFG->gdversion)) { + echo "(".get_string("gdneed").")"; + } else { + if ($mode == STATS_MODE_DETAILED) { + echo '
'; + } else { + echo '
'; + } + } + + $table = new object(); + $table->align = array('left','center','center','center'); + $param->table = str_replace('user_','',$param->table); + $table->head = array(get_string('periodending','moodle',$param->table),$param->line1); + if (!empty($param->line2)) { + $table->head[] = $param->line2; + } + $table->head[] = get_string('logs'); + + foreach ($stats as $stat) { + $a = array(userdate($stat->timeend-(60*60*24),get_string('strftimedate'),$CFG->timezone),$stat->line1); + if (isset($stat->line2)) { + $a[] = $stat->line2; + } + if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) { + $a[] = ''.get_string('course').' ' .get_string('logs').' '; + } + $table->data[] = $a; + } + print_table($table); + } + +?> \ No newline at end of file -- 2.39.5