2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Display user activity reports for a course (totals)
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 use core\report_helper
;
28 require('../../config.php');
29 require_once($CFG->dirroot
.'/report/outline/locallib.php');
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
);
35 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST
);
37 $pageparams = array('id' => $id);
39 $pageparams['startdate'] = $startdate;
42 $pageparams['enddate'] = $enddate;
45 $PAGE->set_url('/report/outline/index.php', $pageparams);
46 $PAGE->set_pagelayout('report');
48 require_login($course);
49 $context = context_course
::instance($course->id
);
50 require_capability('report/outline:view', $context);
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()) {
57 $redir->remove_params(['startdate', 'enddate']);
60 if ($filter = $filterform->get_data()) {
62 if ($filter->filterstartdate
) {
63 $redir->param('startdate', $filter->filterstartdate
);
65 if ($filter->filterenddate
) {
66 $redir->param('enddate', $filter->filterenddate
);
71 // Trigger an activity report viewed event.
72 $event = \report_outline\event\activity_report_viewed
::create(array('context' => $context));
75 $showlastaccess = true;
76 $hiddenfields = explode(',', $CFG->hiddenuserfields
);
78 if (array_search('lastaccess', $hiddenfields) !== false and !has_capability('moodle/user:viewhiddendetails', $context)) {
79 $showlastaccess = false;
82 $stractivityreport = get_string('pluginname', 'report_outline');
83 $stractivity = get_string('activity');
84 $strlast = get_string('lastaccess');
85 $strreports = get_string('reports');
86 $strviews = get_string('views');
87 $strrelatedblogentries = get_string('relatedblogentries', 'blog');
89 $PAGE->set_title($course->shortname
.': '. $stractivityreport);
90 $PAGE->set_heading($course->fullname
);
91 echo $OUTPUT->header();
93 // Print selector drop down.
94 $pluginname = get_string('pluginname', 'report_outline');
95 report_helper
::print_report_selector($pluginname);
97 list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
99 // If no legacy and no internal log then don't proceed.
100 if (!$uselegacyreader && !$useinternalreader) {
101 echo $OUTPUT->box_start('generalbox', 'notice');
102 echo $OUTPUT->notification(get_string('nologreaderenabled', 'report_outline'));
103 echo $OUTPUT->box_end();
104 echo $OUTPUT->footer();
108 // We want to display the time we are beginning to get logs from in the heading.
109 // If we are using the legacy reader check the minimum time in that log table.
110 if ($uselegacyreader) {
111 $minlog = $DB->get_field_sql('SELECT min(time) FROM {log}');
114 // If we are using the internal reader check the minimum time in that table.
115 if ($useinternalreader) {
116 // If new log table has older data then don't use the minimum time obtained from the legacy table.
117 if (empty($minlog) ||
($minloginternalreader <= $minlog)) {
118 $minlog = $minloginternalreader;
122 $filterform->display();
124 echo $OUTPUT->container(get_string('computedfromlogs', 'admin', userdate($minlog)), 'loginfo');
126 $outlinetable = new html_table();
127 $outlinetable->attributes
['class'] = 'generaltable boxaligncenter';
128 $outlinetable->cellpadding
= 5;
129 $outlinetable->id
= 'outlinetable';
130 $outlinetable->head
= array($stractivity, $strviews);
132 if (!empty($CFG->enableblogs
) && $CFG->useblogassociations
) {
133 $outlinetable->head
[] = $strrelatedblogentries;
136 if ($showlastaccess) {
137 $outlinetable->head
[] = $strlast;
140 $modinfo = get_fast_modinfo($course);
142 // If using legacy log then get users from old table.
143 if ($uselegacyreader) {
144 // If we are going to use the internal (not legacy) log table, we should only get records
145 // from the legacy table that exist before we started adding logs to the new table.
146 $params = array('courseid' => $course->id
, 'action' => 'view%', 'visible' => 1);
148 if (!empty($minloginternalreader)) {
149 $limittime = ' AND time < :timeto ';
150 $params['timeto'] = $minloginternalreader;
153 $limittime .= ' AND time >= :startdate ';
154 $params['startdate'] = $startdate;
157 $limittime .= ' AND time < :enddate ';
158 $params['enddate'] = $enddate;
160 // Check if we need to show the last access.
162 if ($showlastaccess) {
163 $sqllasttime = ", MAX(time) AS lasttime";
165 $logactionlike = $DB->sql_like('l.action', ':action');
166 $sql = "SELECT cm.id, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
167 FROM {course_modules} cm
172 WHERE cm.course = :courseid
174 AND m.visible = :visible $limittime
176 $views = $DB->get_records_sql($sql, $params);
179 // Get record from sql_internal_table_reader and merge with records obtained from legacy log (if needed).
180 if ($useinternalreader) {
181 // Check if we need to show the last access.
183 if ($showlastaccess) {
184 $sqllasttime = ", MAX(timecreated) AS lasttime";
186 $params = array('courseid' => $course->id
, 'contextmodule' => CONTEXT_MODULE
);
189 $limittime .= ' AND timecreated >= :startdate ';
190 $params['startdate'] = $startdate;
193 $limittime .= ' AND timecreated < :enddate ';
194 $params['enddate'] = $enddate;
196 $sql = "SELECT contextinstanceid as cmid, COUNT('x') AS numviews, COUNT(DISTINCT userid) AS distinctusers $sqllasttime
197 FROM {" . $logtable . "} l
198 WHERE courseid = :courseid
201 AND contextlevel = :contextmodule
203 GROUP BY contextinstanceid";
204 $v = $DB->get_records_sql($sql, $params);
209 // Merge two view arrays.
210 foreach ($v as $key => $value) {
211 if (isset($views[$key]) && !empty($views[$key]->numviews
)) {
212 $views[$key]->numviews +
= $value->numviews
;
213 if ($value->lasttime
> $views[$key]->lasttime
) {
214 $views[$key]->lasttime
= $value->lasttime
;
217 $views[$key] = $value;
223 $prevsecctionnum = 0;
224 foreach ($modinfo->sections
as $sectionnum=>$section) {
225 foreach ($section as $cmid) {
226 $cm = $modinfo->cms
[$cmid];
227 if (!$cm->has_view()) {
230 if (!$cm->uservisible
) {
233 if ($prevsecctionnum != $sectionnum) {
234 $sectionrow = new html_table_row();
235 $sectionrow->attributes
['class'] = 'section';
236 $sectioncell = new html_table_cell();
237 $sectioncell->colspan
= count($outlinetable->head
);
239 $sectiontitle = get_section_name($course, $sectionnum);
241 $sectioncell->text
= $OUTPUT->heading($sectiontitle, 3);
242 $sectionrow->cells
[] = $sectioncell;
243 $outlinetable->data
[] = $sectionrow;
245 $prevsecctionnum = $sectionnum;
248 $dimmed = $cm->visible ?
'' : 'class="dimmed"';
249 $modulename = get_string('modulename', $cm->modname
);
251 $reportrow = new html_table_row();
252 $activitycell = new html_table_cell();
253 $activitycell->attributes
['class'] = 'activity';
255 $activityicon = $OUTPUT->pix_icon('monologo', $modulename, $cm->modname
, array('class'=>'icon'));
257 $attributes = array();
259 $attributes['class'] = 'dimmed';
262 $activitycell->text
= $activityicon . html_writer
::link("$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id", format_string($cm->name
), $attributes);
264 $reportrow->cells
[] = $activitycell;
266 $numviewscell = new html_table_cell();
267 $numviewscell->attributes
['class'] = 'numviews';
269 if (!empty($views[$cm->id
]->numviews
)) {
270 $numviewscell->text
= get_string('numviews', 'report_outline', $views[$cm->id
]);
272 $numviewscell->text
= '-';
275 $reportrow->cells
[] = $numviewscell;
277 if (!empty($CFG->enableblogs
) && $CFG->useblogassociations
) {
278 require_once($CFG->dirroot
.'/blog/lib.php');
279 $blogcell = new html_table_cell();
280 $blogcell->attributes
['class'] = 'blog';
281 if ($blogcount = blog_get_associated_count($course->id
, $cm->id
)) {
282 $blogurl = new moodle_url('/blog/index.php', array('modid' => $cm->id
));
283 $blogcell->text
= html_writer
::link($blogurl, $blogcount);
285 $blogcell->text
= '-';
287 $reportrow->cells
[] = $blogcell;
290 if ($showlastaccess) {
291 $lastaccesscell = new html_table_cell();
292 $lastaccesscell->attributes
['class'] = 'lastaccess';
294 if (isset($views[$cm->id
]->lasttime
)) {
295 $timeago = format_time(time() - $views[$cm->id
]->lasttime
);
296 $lastaccesscell->text
= userdate($views[$cm->id
]->lasttime
)." ($timeago)";
298 $reportrow->cells
[] = $lastaccesscell;
300 $outlinetable->data
[] = $reportrow;
303 echo html_writer
::table($outlinetable);
305 echo $OUTPUT->footer();