Example #1
0
/**
 * Gather course overview data and print the chart.
 *
 * @param int $report represents the report type field on the course overview report filter.
 * @param int $time represents the time period field on the course overview report filter.
 * @param int $numcourses represents the number of courses field on the course overview report filter.
 * @return void
 */
function report_courseoverview_print_chart($report, $time, $numcourses)
{
    global $DB, $OUTPUT, $PAGE;
    $param = stats_get_parameters($time, $report, SITEID, STATS_MODE_RANKED);
    if (!empty($param->sql)) {
        $sql = $param->sql;
    } else {
        $sql = "SELECT courseid, {$param->fields}\n                  FROM {" . 'stats_' . $param->table . "}\n                 WHERE timeend >= {$param->timeafter}\n                   AND stattype = 'activity'\n                   AND roleid = 0\n              GROUP BY courseid\n                   {$param->extras}\n              ORDER BY {$param->orderby}";
    }
    $courses = $DB->get_records_sql($sql, $param->params, 0, $numcourses);
    if (empty($courses)) {
        $PAGE->set_url('/report/courseoverview/index.php');
        print_error('statsnodata', 'error', $PAGE->url->out());
    }
    $data = [];
    $i = 0;
    foreach ($courses as $c) {
        $data['labels'][$i] = $DB->get_field('course', 'shortname', array('id' => $c->courseid));
        // Line3 represents the third column of the report table except for the most active users report.
        // It is a float number and can be participation radio or activity per user.
        if (isset($c->line3)) {
            $data['series'][$i] = round($c->line3, 2);
        } else {
            $data['series'][$i] = $c->{$param->graphline};
        }
        $i++;
    }
    $chart = new \core\chart_bar();
    $series = new \core\chart_series($param->{$param->graphline}, $data['series']);
    $chart->add_series($series);
    $chart->set_labels($data['labels']);
    echo $OUTPUT->render($chart);
}
Example #2
0
 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)) {
     print_error('nostatstodisplay', '', $CFG->wwwroot . '/course/user.php?id=' . $course->id . '&user='******'&mode=outline');
 }
 // use the earliest.
 $time = array_pop(array_keys($timeoptions));
 $param = stats_get_parameters($time, STATS_REPORT_USER_VIEW, $course->id, 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 ') . ' userid = ' . $user->id . ' AND timeend >= ' . $param->timeafter . $param->extras . ' ORDER BY timeend DESC';
 $stats = get_records_sql($sql);
 if (empty($stats)) {
     print_error('nostatstodisplay', '', $CFG->wwwroot . '/course/user.php?id=' . $course->id . '&user='******'&mode=outline');
 }
 // MDL-10818, do not display broken graph when user has no permission to view graph
 if ($myreports or has_capability('coursereport/stats:view', $coursecontext)) {
     echo '<center><img src="' . $CFG->wwwroot . '/course/report/stats/graph.php?mode=' . STATS_MODE_DETAILED . '&course=' . $course->id . '&time=' . $time . '&report=' . STATS_REPORT_USER_VIEW . '&userid=' . $user->id . '" alt="' . get_string('statisticsgraph') . '" /></center>';
 }
 // What the heck is this about?   -- MD
 $stats = stats_fix_zeros($stats, $param->timeafter, $param->table, !empty($param->line2), !empty($param->line3));
 $table = new object();
 $table->align = array('left', 'center', 'center', 'center');
 $param->table = str_replace('user_', '', $param->table);
Example #3
0
<?php

// $Id: reportsgraph.php,v 1.8.2.3 2008/11/29 14:30:58 skodak Exp $
require_once '../../../config.php';
require_once $CFG->dirroot . '/lib/statslib.php';
require_once $CFG->dirroot . '/lib/graphlib.php';
$report = required_param('report', PARAM_INT);
$time = required_param('time', PARAM_INT);
$numcourses = required_param('numcourses', PARAM_INT);
require_login();
require_capability('report/courseoverview:view', get_context_instance(CONTEXT_SYSTEM));
stats_check_uptodate();
$param = stats_get_parameters($time, $report, SITEID, STATS_MODE_RANKED);
if (!empty($param->sql)) {
    $sql = $param->sql;
} else {
    $sql = "SELECT courseid," . $param->fields . " FROM " . $CFG->prefix . 'stats_' . $param->table . " WHERE timeend >= {$param->timeafter} AND stattype = 'activity' AND roleid = 0" . " GROUP BY courseid " . $param->extras . " ORDER BY " . $param->orderby;
}
$courses = get_records_sql($sql, 0, $numcourses);
if (empty($courses)) {
    print_error('statsnodata', "", $CFG->wwwroot . '/' . $CFG->admin . '/report/courseoverview/index.php');
}
$graph = new graph(750, 400);
$graph->parameter['legend'] = 'outside-right';
$graph->parameter['legend_size'] = 10;
$graph->parameter['x_axis_angle'] = 90;
$graph->parameter['title'] = false;
// moodle will do a nicer job.
$graph->y_tick_labels = null;
$graph->offset_relation = null;
if ($report != STATS_REPORT_ACTIVE_COURSES) {
Example #4
0
        require_login($course);
    }

    if (!report_stats_can_access_user_report($user, $course, true)) {
        require_capability('report/stats:view', $coursecontext);
    }

} else {
    require_capability('report/stats:view', $coursecontext);
}

add_to_log($course->id, 'course', 'report stats', "report/stats/graph.php?userid=$userid&id=$course->id&mode=$mode&roleid=$roleid", $course->id);

stats_check_uptodate($course->id);

$param = stats_get_parameters($time,$report,$course->id,$mode);

if (!empty($userid)) {
    $param->table = 'user_'.$param->table;
}

// TODO: cleanup 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';
Example #5
0
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)

        //TODO: lceanup this ugly mess
        $sql = 'SELECT DISTINCT s.userid, u.firstname, u.lastname, u.idnumber
                     FROM {stats_user_'.$param->table.'} s
                     JOIN {user} u ON u.id = s.userid
                     WHERE courseid = '.$course->id
            . ((!empty($param->stattype)) ? ' AND stattype = \''.$param->stattype.'\'' : '')
            . ((!empty($time)) ? ' AND timeend >= '.$param->timeafter : '')
            .' ORDER BY u.lastname, u.firstname ASC';

        if (!$us = $DB->get_records_sql($sql, $param->params)) {
            print_error('nousers');
        }

        foreach ($us as $u) {
            $users[$u->userid] = fullname($u, true);
        }

        $table->align = array('left','left','left','left','left','left','left','left');
        $table->data[] = array(get_string('course'),html_writer::select($courseoptions,'course',$course->id,false),
                               get_string('users'),html_writer::select($users,'userid',$userid,false),
                               get_string('statsreporttype'),html_writer::select($reportoptions,'report',($report == 5) ? $report.$roleid : $report,false),
                               get_string('statstimeperiod'),html_writer::select($timeoptions,'time',$time,false),
                               '<input type="submit" value="'.get_string('view').'" />') ;
    } else if ($mode == STATS_MODE_RANKED) {
        $table->align = array('left','left','left','left','left','left');
        $table->data[] = array(get_string('statsreporttype'),html_writer::select($reportoptions,'report',($report == 5) ? $report.$roleid : $report,false),
                               get_string('statstimeperiod'),html_writer::select($timeoptions,'time',$time,false),
                               '<input type="submit" value="'.get_string('view').'" />') ;
    } else if ($mode == STATS_MODE_GENERAL) {
        $table->align = array('left','left','left','left','left','left','left');
        $table->data[] = array(get_string('course'),html_writer::select($courseoptions,'course',$course->id,false),
                               get_string('statsreporttype'),html_writer::select($reportoptions,'report',($report == 5) ? $report.$roleid : $report,false),
                               get_string('statstimeperiod'),html_writer::select($timeoptions,'time',$time,false),
                               '<input type="submit" value="'.get_string('view').'" />') ;
    }

    echo '<form action="index.php" method="post">'."\n"
        .'<div>'."\n"
        .'<input type="hidden" name="mode" value="'.$mode.'" />'."\n";

    echo html_writer::table($table);

    echo '</div>';
    echo '</form>';

    if (!empty($report) && !empty($time)) {
        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 (empty($CFG->gdversion)) {
                echo "(".get_string("gdneed").")";
            } else {
                if ($mode == STATS_MODE_DETAILED) {
                    echo '<div class="graph"><img src="'.$CFG->wwwroot.'/report/stats/graph.php?mode='.$mode.'&amp;course='.$course->id.'&amp;time='.$time.'&amp;report='.$report.'&amp;userid='.$userid.'" alt="'.get_string('statisticsgraph').'" /></div';
                } else {
                    echo '<div class="graph"><img src="'.$CFG->wwwroot.'/report/stats/graph.php?mode='.$mode.'&amp;course='.$course->id.'&amp;time='.$time.'&amp;report='.$report.'&amp;roleid='.$roleid.'" alt="'.get_string('statisticsgraph').'" /></div>';
                }
            }

            $table = new html_table();
            $table->align = array('left','center','center','center');
            $param->table = str_replace('user_','',$param->table);
            switch ($param->table) {
                case 'daily'  : $period = get_string('day'); break;
                case 'weekly' : $period = get_string('week'); break;
                case 'monthly': $period = get_string('month', 'form'); break;
                default : $period = '';
            }
            $table->head = array(get_string('periodending','moodle',$period));
            if (empty($param->crosstab)) {
                $table->head[] = $param->line1;
                if (!empty($param->line2)) {
                    $table->head[] = $param->line2;
                }
            }
            if (!file_exists($CFG->dirroot.'/report/log/index.php')) {
                // bad luck, we can not link other report
            } else if (empty($param->crosstab)) {
                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)) {
                        if (has_capability('report/log:view', context_course::instance($course->id))) {
                            $a[] = '<a href="'.$CFG->wwwroot.'/report/log/index.php?id='.
                                $course->id.'&amp;chooselog=1&amp;showusers=1&amp;showcourses=1&amp;user='******'&amp;date='.usergetmidnight($stat->timeend-(60*60*24)).'">'
                                .get_string('course').' ' .get_string('logs').'</a>&nbsp;';
                        } else {
                            $a[] = '';
                        }
                    }
                    $table->data[] = $a;
                }
            } else {
                $data = array();
                $roles = array();
                $times = array();
                $missedlines = array();
                $coursecontext = context_course::instance($course->id);
                $rolenames = role_fix_names(get_all_roles($coursecontext), $coursecontext, ROLENAME_ALIAS, true);
                foreach ($stats as $stat) {
                    if (!empty($stat->zerofixed)) {
                        $missedlines[] = $stat->timeend;
                    }
                    $data[$stat->timeend][$stat->roleid] = $stat->line1;
                    if ($stat->roleid != 0) {
                        if (!array_key_exists($stat->roleid,$roles)) {
                            $roles[$stat->roleid] = $rolenames[$stat->roleid];
                        }
                    } else {
                        if (!array_key_exists($stat->roleid,$roles)) {
                            $roles[$stat->roleid] = get_string('all');
                        }
                    }
                    if (!array_key_exists($stat->timeend,$times)) {
                        $times[$stat->timeend] = userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone);
                    }
                }

                foreach ($data as $time => $rolesdata) {
                    if (in_array($time,$missedlines)) {
                        $rolesdata = array();
                        foreach ($roles as $roleid => $guff) {
                            $rolesdata[$roleid] = 0;
                        }
                    }
                    else {
                        foreach (array_keys($roles) as $r) {
                            if (!array_key_exists($r, $rolesdata)) {
                                $rolesdata[$r] = 0;
                            }
                        }
                    }
                    krsort($rolesdata);
                    $row = array_merge(array($times[$time]),$rolesdata);
                    if (empty($CFG->loglifetime) || ($stat->timeend-(60*60*24)) >= (time()-60*60*24*$CFG->loglifetime)) {
                        if (has_capability('report/log:view', context_course::instance($course->id))) {
                            $row[] = '<a href="'.$CFG->wwwroot.'/report/log/index.php?id='
                                .$course->id.'&amp;chooselog=1&amp;showusers=1&amp;showcourses=1&amp;user='******'&amp;date='.usergetmidnight($time-(60*60*24)).'">'
                                .get_string('course').' ' .get_string('logs').'</a>&nbsp;';
                        } else {
                            $row[] = '';
                        }
                    }
                    $table->data[] = $row;
                }
                krsort($roles);
                $table->head = array_merge($table->head,$roles);
            }
            $table->head[] = get_string('logs');
            //if (!empty($lastrecord)) {
                //$lastrecord[] = $lastlink;
                //$table->data[] = $lastrecord;
            //}
            echo html_writer::table($table);
        }
    }
}
Example #6
0
/**
 * Fetch statistics data and generate a line chart.
 *
 * The statistic chart can be view, posts separated by roles and dates.
 *
 * @param int $courseid course id.
 * @param int $report the report type constant eg. STATS_REPORT_LOGINS as defined on statslib.
 * @param int $time timestamp of the selected time period.
 * @param int $mode the report mode, eg. STATS_MODE_DETAILED as defined on statslib.
 * @param int $userid selected user id.
 * @param int $roleid selected role id.
 */
function report_stats_print_chart($courseid, $report, $time, $mode, $userid = 0, $roleid = 0)
{
    global $DB, $CFG, $OUTPUT;
    $course = $DB->get_record("course", array("id" => $courseid), '*', MUST_EXIST);
    $coursecontext = context_course::instance($course->id);
    stats_check_uptodate($course->id);
    $param = stats_get_parameters($time, $report, $course->id, $mode);
    if (!empty($userid)) {
        $param->table = 'user_' . $param->table;
    }
    // TODO: cleanup 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, $param->params);
    $stats = stats_fix_zeros($stats, $param->timeafter, $param->table, !empty($param->line2), !empty($param->line3));
    $stats = array_reverse($stats);
    $chart = new \core\chart_line();
    if (empty($param->crosstab)) {
        $data = [];
        $times = [];
        foreach ($stats as $stat) {
            // Build the array of formatted times indexed by timestamp used as labels.
            if (!array_key_exists($stat->timeend, $times)) {
                $times[$stat->timeend] = userdate($stat->timeend, get_string('strftimedate'), $CFG->timezone);
                // Just add the data if the time hasn't been added yet.
                // The number of lines of data must match the number of labels.
                $data['line1'][] = $stat->line1;
                if (isset($stat->line2)) {
                    $data['line2'][] = $stat->line2;
                }
                if (isset($stat->line3)) {
                    $data['line3'][] = $stat->line3;
                }
            }
        }
        foreach ($data as $line => $serie) {
            $series = new \core\chart_series($param->{$line}, array_values($serie));
            $chart->add_series($series);
        }
    } else {
        $data = array();
        $times = array();
        $roles = array();
        $missedlines = array();
        $rolenames = role_fix_names(get_all_roles($coursecontext), $coursecontext, ROLENAME_ALIAS, true);
        foreach ($stats as $stat) {
            $data[$stat->roleid][$stat->timeend] = $stat->line1;
            if (!empty($stat->zerofixed)) {
                $missedlines[] = $stat->timeend;
            }
            if ($stat->roleid != 0) {
                if (!array_key_exists($stat->roleid, $roles)) {
                    $roles[$stat->roleid] = $rolenames[$stat->roleid];
                }
            } else {
                if (!array_key_exists($stat->roleid, $roles)) {
                    $roles[$stat->roleid] = get_string('all');
                }
            }
            // Build the array of formatted times indexed by timestamp used as labels.
            if (!array_key_exists($stat->timeend, $times)) {
                $times[$stat->timeend] = userdate($stat->timeend, get_string('strftimedate'), $CFG->timezone);
            }
        }
        // Fill empty days with zero to avoid chart errors.
        foreach (array_keys($times) as $t) {
            foreach ($data as $roleid => $stuff) {
                if (!array_key_exists($t, $stuff)) {
                    $data[$roleid][$t] = 0;
                }
            }
        }
        krsort($roles);
        foreach ($roles as $roleid => $rolename) {
            ksort($data[$roleid]);
            $series = new \core\chart_series($rolename, array_values($data[$roleid]));
            $chart->add_series($series);
        }
    }
    $chart->set_labels(array_values($times));
    echo $OUTPUT->render_chart($chart, false);
}