Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * @package     report_overviewstats
19
 * @copyright   2013 David Mudrak <david@moodle.com>
20
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 */
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
require_once($CFG->libdir.'/accesslib.php');
26
 
27
/**
28
 * Reports various users related charts and figures
29
 */
30
class report_overviewstats_chart_logins extends report_overviewstats_chart {
31
 
32
    /**
33
     * @return array
34
     */
35
    public function get_content() {
36
 
37
        $this->prepare_data();
38
 
39
        $title = get_string('chart-logins', 'report_overviewstats');
40
        $titleperday = get_string('chart-logins-perday', 'report_overviewstats');
41
 
42
        return array($title => array(
43
            $titleperday => html_writer::tag('div', '', array(
44
                'id' => 'chart_logins_perday',
45
                'class' => 'chartplaceholder',
46
                'style' => 'min-height: 300px;',
47
            )),
48
        ));
49
    }
50
 
51
    /**
52
     * @see parent
53
     */
54
    public function inject_page_requirements(moodle_page $page) {
55
 
56
        $this->prepare_data();
57
 
58
        $page->requires->yui_module(
59
            'moodle-report_overviewstats-charts',
60
            'M.report_overviewstats.charts.logins.init',
61
            array($this->data)
62
        );
63
    }
64
 
65
    /**
66
     * Prepares data to report
67
     */
68
    protected function prepare_data() {
69
        global $DB, $CFG;
70
 
71
        if (!is_null($this->data)) {
72
            return;
73
        }
74
 
75
        $now = strtotime('today midnight');
76
 
77
        $lastmonth = array();
78
        for ($i = 30; $i >= 0; $i--) {
79
            $lastmonth[$now - $i * DAYSECS] = array();
80
        }
81
 
82
        if ($CFG->branch >= 27) {
83
            $logmanger = get_log_manager();
84
            if ($CFG->branch >= 29) {
85
                $readers = $logmanger->get_readers('\core\log\sql_reader');
86
            } else {
87
                $readers = $logmanger->get_readers('\core\log\sql_select_reader');
88
            }
89
            $reader = reset($readers);
90
            $params = array('component' => 'core',
91
                            'eventname' => '\core\event\user_loggedin',
92
                            'guestid' => $CFG->siteguest,
93
                            'timestart' => $now - 30 * DAYSECS);
94
            $select = "component = :component AND eventname = :eventname AND userid <> :guestid AND timecreated >= :timestart";
95
            $rs = $reader->get_events_select($select, $params, 'timecreated DESC', 0, 0);
96
 
97
            foreach ($rs as $record) {
98
                foreach (array_reverse($lastmonth, true) as $timestamp => $loggedin) {
99
                    $date = usergetdate($timestamp);
100
                    if ($record->timecreated >= $timestamp) {
101
                        $lastmonth[$timestamp][$record->userid] = true;
102
                        break;
103
                    }
104
                }
105
            }
106
 
107
        } else {
108
            $sql = "SELECT time, userid
109
                      FROM {log}
110
                     WHERE time >= :timestart
111
                       AND userid <> :guestid
112
                       AND action = 'login'";
113
 
114
            $params = array('timestart' => $now - 30 * DAYSECS, 'guestid' => $CFG->siteguest);
115
 
116
            $rs = $DB->get_recordset_sql($sql, $params);
117
 
118
            foreach ($rs as $record) {
119
                foreach (array_reverse($lastmonth, true) as $timestamp => $loggedin) {
120
                    $date = usergetdate($timestamp);
121
                    if ($record->time >= $timestamp) {
122
                        $lastmonth[$timestamp][$record->userid] = true;
123
                        break;
124
                    }
125
                }
126
            }
127
            $rs->close();
128
        }
129
 
130
        $this->data = array(
131
            'perday' => array(),
132
        );
133
 
134
        $format = get_string('strftimedateshort', 'core_langconfig');
135
 
136
        foreach ($lastmonth as $timestamp => $loggedin) {
137
            $date = userdate($timestamp, $format);
138
            $this->data['perday'][] = array('date' => $date, 'loggedin' => count($loggedin));
139
        }
140
        // Ignore today's stats (not complete yet).
141
        array_pop($this->data['perday']);
142
    }
143
}