Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 * Display user activity reports for a course (totals)
19
 *
20
 * @package    report
21
 * @subpackage outline
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
use core\report_helper;
27
 
28
require('../../config.php');
29
require_once($CFG->dirroot.'/report/outline/locallib.php');
30
 
31
$id = required_param('id',PARAM_INT);       // course id
32
$startdate = optional_param('startdate', null, PARAM_INT);
33
$enddate = optional_param('enddate', null, PARAM_INT);
34
 
35
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
36
 
37
$pageparams = array('id' => $id);
38
if ($startdate) {
39
    $pageparams['startdate'] = $startdate;
40
}
41
if ($enddate) {
42
    $pageparams['enddate'] = $enddate;
43
}
44
 
45
$PAGE->set_url('/report/outline/index.php', $pageparams);
46
$PAGE->set_pagelayout('report');
47
 
48
require_login($course);
49
$context = context_course::instance($course->id);
50
require_capability('report/outline:view', $context);
51
 
52
// Handle form to filter access logs by date.
53
$filterform = new \report_outline\filter_form();
54
$filterform->set_data(['id' => $course->id, 'filterstartdate' => $startdate, 'filterenddate' => $enddate]);
55
if ($filterform->is_cancelled()) {
56
    $redir = $PAGE->url;
57
    $redir->remove_params(['startdate', 'enddate']);
58
    redirect($redir);
59
}
60
if ($filter = $filterform->get_data()) {
61
    $redir = $PAGE->url;
62
    if ($filter->filterstartdate) {
63
        $redir->param('startdate', $filter->filterstartdate);
64
    }
65
    if ($filter->filterenddate) {
66
        $redir->param('enddate', $filter->filterenddate);
67
    }
68
    redirect($redir);
69
}
70
 
71
// Trigger an activity report viewed event.
72
$event = \report_outline\event\activity_report_viewed::create(array('context' => $context));
73
$event->trigger();
74
 
75
$showlastaccess = true;
1441 ariadna 76
$showblogs = !empty($CFG->enableblogs) && $CFG->useblogassociations;
1 efrain 77
$hiddenfields = explode(',', $CFG->hiddenuserfields);
78
 
79
if (array_search('lastaccess', $hiddenfields) !== false and !has_capability('moodle/user:viewhiddendetails', $context)) {
80
    $showlastaccess = false;
81
}
82
 
83
$stractivityreport = get_string('pluginname', 'report_outline');
84
$PAGE->set_title($course->shortname .': '. $stractivityreport);
85
$PAGE->set_heading($course->fullname);
86
echo $OUTPUT->header();
87
 
88
// Print selector drop down.
89
$pluginname = get_string('pluginname', 'report_outline');
90
report_helper::print_report_selector($pluginname);
91
 
92
list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
93
 
94
// If no legacy and no internal log then don't proceed.
95
if (!$uselegacyreader && !$useinternalreader) {
96
    echo $OUTPUT->box_start('generalbox', 'notice');
97
    echo $OUTPUT->notification(get_string('nologreaderenabled', 'report_outline'));
98
    echo $OUTPUT->box_end();
99
    echo $OUTPUT->footer();
100
    die();
101
}
102
 
103
// We want to display the time we are beginning to get logs from in the heading.
104
// If we are using the legacy reader check the minimum time in that log table.
105
if ($uselegacyreader) {
106
    $minlog = $DB->get_field_sql('SELECT min(time) FROM {log}');
107
}
108
 
109
// If we are using the internal reader check the minimum time in that table.
110
if ($useinternalreader) {
111
    // If new log table has older data then don't use the minimum time obtained from the legacy table.
112
    if (empty($minlog) || ($minloginternalreader <= $minlog)) {
113
        $minlog = $minloginternalreader;
114
    }
115
}
116
 
117
$filterform->display();
118
 
119
$modinfo = get_fast_modinfo($course);
120
 
121
// If using legacy log then get users from old table.
122
if ($uselegacyreader) {
123
    // If we are going to use the internal (not legacy) log table, we should only get records
124
    // from the legacy table that exist before we started adding logs to the new table.
125
    $params = array('courseid' => $course->id, 'action' => 'view%', 'visible' => 1);
126
    $limittime = '';
127
    if (!empty($minloginternalreader)) {
128
        $limittime = ' AND time < :timeto ';
129
        $params['timeto'] = $minloginternalreader;
130
    }
131
    if ($startdate) {
132
        $limittime .= ' AND time >= :startdate ';
133
        $params['startdate'] = $startdate;
134
    }
135
    if ($enddate) {
136
        $limittime .= ' AND time < :enddate ';
137
        $params['enddate'] = $enddate;
138
    }
139
    // Check if we need to show the last access.
140
    $sqllasttime = '';
141
    if ($showlastaccess) {
142
        $sqllasttime = ", MAX(time) AS lasttime";
143
    }
144
    $logactionlike = $DB->sql_like('l.action', ':action');
145
    $sql = "SELECT cm.id, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
146
              FROM {course_modules} cm
147
              JOIN {modules} m
148
                ON m.id = cm.module
149
              JOIN {log} l
150
                ON l.cmid = cm.id
151
             WHERE cm.course = :courseid
152
               AND $logactionlike
153
               AND m.visible = :visible $limittime
154
          GROUP BY cm.id";
155
    $views = $DB->get_records_sql($sql, $params);
156
}
157
 
158
// Get record from sql_internal_table_reader and merge with records obtained from legacy log (if needed).
159
if ($useinternalreader) {
160
    // Check if we need to show the last access.
161
    $sqllasttime = '';
162
    if ($showlastaccess) {
163
        $sqllasttime = ", MAX(timecreated) AS lasttime";
164
    }
165
    $params = array('courseid' => $course->id, 'contextmodule' => CONTEXT_MODULE);
166
    $limittime = '';
167
    if ($startdate) {
168
        $limittime .= ' AND timecreated >= :startdate ';
169
        $params['startdate'] = $startdate;
170
    }
171
    if ($enddate) {
172
        $limittime .= ' AND timecreated < :enddate ';
173
        $params['enddate'] = $enddate;
174
    }
175
    $sql = "SELECT contextinstanceid as cmid, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
176
              FROM {" . $logtable . "} l
177
             WHERE courseid = :courseid
178
               AND anonymous = 0
179
               AND crud = 'r'
180
               AND contextlevel = :contextmodule
181
               $limittime
182
          GROUP BY contextinstanceid";
183
    $v = $DB->get_records_sql($sql, $params);
184
 
185
    if (empty($views)) {
186
        $views = $v;
187
    } else {
188
        // Merge two view arrays.
189
        foreach ($v as $key => $value) {
190
            if (isset($views[$key]) && !empty($views[$key]->numviews)) {
191
                $views[$key]->numviews += $value->numviews;
192
                if ($value->lasttime > $views[$key]->lasttime) {
193
                    $views[$key]->lasttime = $value->lasttime;
194
                }
195
            } else {
196
                $views[$key] = $value;
197
            }
198
        }
199
    }
200
}
201
 
1441 ariadna 202
$activitieslist = new report_outline\output\activitieslist($modinfo, $views, $showlastaccess, $minlog, $showblogs);
203
echo $OUTPUT->render_from_template('report_outline/report', $activitieslist->export_for_template($OUTPUT));
1 efrain 204
echo $OUTPUT->footer();
205
 
206
 
207