MDL-55031 core: Series color was not being exported
[moodle.git] / report / log / locallib.php
CommitLineData
dfab77a2 1<?php
dfab77a2 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 * This file contains functions used by the log reports
19 *
f82de750
AKA
20 * This files lists the functions that are used during the log report generation.
21 *
04252d3a 22 * @package report_log
033af4b7
PS
23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
dfab77a2 25 */
0a935fb3 26
033af4b7
PS
27defined('MOODLE_INTERNAL') || die;
28
fad8e024
PS
29if (!defined('REPORT_LOG_MAX_DISPLAY')) {
30 define('REPORT_LOG_MAX_DISPLAY', 150); // days
31}
32
1fcf0ca8 33require_once(__DIR__.'/lib.php');
033af4b7 34
f82de750
AKA
35/**
36 * This function is used to generate and display the log activity graph
37 *
04252d3a
AKA
38 * @global stdClass $CFG
39 * @param stdClass $course course instance
f82de750
AKA
40 * @param int $userid id of the user whose logs are needed
41 * @param string $type type of logs graph needed (usercourse.png/userday.png)
277c811f 42 * @param int $date timestamp in GMT (seconds since epoch)
ac8976c8 43 * @param string $logreader Log reader.
f82de750
AKA
44 * @return void
45 */
ac8976c8 46function report_log_print_graph($course, $userid, $type, $date=0, $logreader='') {
ac8976c8
RT
47 $logmanager = get_log_manager();
48 $readers = $logmanager->get_readers();
fad8e024 49
ac8976c8
RT
50 if (empty($logreader)) {
51 $reader = reset($readers);
52 } else {
53 $reader = $readers[$logreader];
54 }
59aebbed
DM
55 // If reader is not a sql_internal_table_reader and not legacy store then don't show graph.
56 if (!($reader instanceof \core\log\sql_internal_table_reader) && !($reader instanceof logstore_legacy\log\store)) {
cfb804d8 57 return array();
ac8976c8
RT
58 }
59
60 $url = new moodle_url('/report/log/graph.php', array('id' => $course->id, 'user' => $userid, 'type' => $type,
61 'date' => $date, 'logreader' => $logreader));
62 echo html_writer::empty_tag('img', array('src' => $url, 'alt' => ''));
63}
64
65/**
66 * Select all log records for a given course and user
67 *
68 * @param int $userid The id of the user as found in the 'user' table.
69 * @param int $courseid The id of the course as found in the 'course' table.
70 * @param string $coursestart unix timestamp representing course start date and time.
71 * @param string $logreader log reader to use.
72 * @return array
73 */
74function report_log_usercourse($userid, $courseid, $coursestart, $logreader = '') {
75 global $DB;
76
77 $logmanager = get_log_manager();
78 $readers = $logmanager->get_readers();
79 if (empty($logreader)) {
80 $reader = reset($readers);
81 } else {
82 $reader = $readers[$logreader];
83 }
84
59aebbed
DM
85 // If reader is not a sql_internal_table_reader and not legacy store then return.
86 if (!($reader instanceof \core\log\sql_internal_table_reader) && !($reader instanceof logstore_legacy\log\store)) {
ac8976c8
RT
87 return array();
88 }
89
90 $coursestart = (int)$coursestart; // Note: unfortunately pg complains if you use name parameter or column alias in GROUP BY.
91 if ($reader instanceof logstore_legacy\log\store) {
92 $logtable = 'log';
93 $timefield = 'time';
94 $coursefield = 'course';
43a7ac72
MG
95 // Anonymous actions are never logged in legacy log.
96 $nonanonymous = '';
ac8976c8 97 } else {
cfb804d8 98 $logtable = $reader->get_internal_log_table_name();
ac8976c8
RT
99 $timefield = 'timecreated';
100 $coursefield = 'courseid';
43a7ac72 101 $nonanonymous = 'AND anonymous = 0';
ac8976c8
RT
102 }
103
104 $params = array();
105 $courseselect = '';
106 if ($courseid) {
107 $courseselect = "AND $coursefield = :courseid";
108 $params['courseid'] = $courseid;
109 }
110 $params['userid'] = $userid;
111 return $DB->get_records_sql("SELECT FLOOR(($timefield - $coursestart)/" . DAYSECS . ") AS day, COUNT(*) AS num
112 FROM {" . $logtable . "}
113 WHERE userid = :userid
43a7ac72 114 AND $timefield > $coursestart $courseselect $nonanonymous
ac8976c8
RT
115 GROUP BY FLOOR(($timefield - $coursestart)/" . DAYSECS .")", $params);
116}
117
118/**
119 * Select all log records for a given course, user, and day
120 *
121 * @param int $userid The id of the user as found in the 'user' table.
122 * @param int $courseid The id of the course as found in the 'course' table.
123 * @param string $daystart unix timestamp of the start of the day for which the logs needs to be retrived
124 * @param string $logreader log reader to use.
125 * @return array
126 */
127function report_log_userday($userid, $courseid, $daystart, $logreader = '') {
128 global $DB;
129 $logmanager = get_log_manager();
130 $readers = $logmanager->get_readers();
131 if (empty($logreader)) {
132 $reader = reset($readers);
133 } else {
134 $reader = $readers[$logreader];
135 }
136
59aebbed
DM
137 // If reader is not a sql_internal_table_reader and not legacy store then return.
138 if (!($reader instanceof \core\log\sql_internal_table_reader) && !($reader instanceof logstore_legacy\log\store)) {
ac8976c8
RT
139 return array();
140 }
141
142 $daystart = (int)$daystart; // Note: unfortunately pg complains if you use name parameter or column alias in GROUP BY.
143
144 if ($reader instanceof logstore_legacy\log\store) {
145 $logtable = 'log';
146 $timefield = 'time';
147 $coursefield = 'course';
43a7ac72
MG
148 // Anonymous actions are never logged in legacy log.
149 $nonanonymous = '';
ac8976c8 150 } else {
cfb804d8 151 $logtable = $reader->get_internal_log_table_name();
ac8976c8
RT
152 $timefield = 'timecreated';
153 $coursefield = 'courseid';
43a7ac72 154 $nonanonymous = 'AND anonymous = 0';
ac8976c8
RT
155 }
156 $params = array('userid' => $userid);
157
158 $courseselect = '';
159 if ($courseid) {
160 $courseselect = "AND $coursefield = :courseid";
161 $params['courseid'] = $courseid;
162 }
163 return $DB->get_records_sql("SELECT FLOOR(($timefield - $daystart)/" . HOURSECS . ") AS hour, COUNT(*) AS num
164 FROM {" . $logtable . "}
165 WHERE userid = :userid
43a7ac72 166 AND $timefield > $daystart $courseselect $nonanonymous
ac8976c8 167 GROUP BY FLOOR(($timefield - $daystart)/" . HOURSECS . ") ", $params);
fad8e024 168}
ac8976c8 169
f82de750
AKA
170/**
171 * This function is used to generate and display Mnet selector form
172 *
04252d3a
AKA
173 * @global stdClass $USER
174 * @global stdClass $CFG
175 * @global stdClass $SITE
176 * @global moodle_database $DB
177 * @global core_renderer $OUTPUT
178 * @global stdClass $SESSION
f82de750
AKA
179 * @uses CONTEXT_SYSTEM
180 * @uses COURSE_MAX_COURSES_PER_DROPDOWN
181 * @uses CONTEXT_COURSE
182 * @uses SEPARATEGROUPS
04252d3a
AKA
183 * @param int $hostid host id
184 * @param stdClass $course course instance
185 * @param int $selecteduser id of the selected user
186 * @param string $selecteddate Date selected
187 * @param string $modname course_module->id
188 * @param string $modid number or 'site_errors'
189 * @param string $modaction an action as recorded in the logs
190 * @param int $selectedgroup Group to display
191 * @param int $showcourses whether to show courses if we're over our limit.
192 * @param int $showusers whether to show users if we're over our limit.
193 * @param string $logformat Format of the logs (downloadascsv, showashtml, downloadasods, downloadasexcel)
f82de750
AKA
194 * @return void
195 */
fad8e024 196function report_log_print_mnet_selector_form($hostid, $course, $selecteduser=0, $selecteddate='today',
c215b32b 197 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') {
198
928d4738 199 global $USER, $CFG, $SITE, $DB, $OUTPUT, $SESSION;
c215b32b 200 require_once $CFG->dirroot.'/mnet/peer.php';
aa6c1ced 201
c215b32b 202 $mnet_peer = new mnet_peer();
203 $mnet_peer->set_id($hostid);
204
29f83769 205 $sql = "SELECT DISTINCT course, hostid, coursename FROM {mnet_log}";
206 $courses = $DB->get_records_sql($sql);
c215b32b 207 $remotecoursecount = count($courses);
208
209 // first check to see if we can override showcourses and showusers
29f83769 210 $numcourses = $remotecoursecount + $DB->count_records('course');
c215b32b 211 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
212 $showcourses = 1;
213 }
aa6c1ced 214
21c08c63 215 $sitecontext = context_system::instance();
aa6c1ced 216
c215b32b 217 // Context for remote data is always SITE
218 // Groups for remote data are always OFF
219 if ($hostid == $CFG->mnet_localhost_id) {
21c08c63 220 $context = context_course::instance($course->id);
c215b32b 221
222 /// Setup for group handling.
223 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
928d4738 224 $selectedgroup = -1;
c215b32b 225 $showgroups = false;
928d4738 226 } else if ($course->groupmode) {
c215b32b 227 $showgroups = true;
928d4738 228 } else {
c215b32b 229 $selectedgroup = 0;
230 $showgroups = false;
231 }
232
928d4738 233 if ($selectedgroup === -1) {
234 if (isset($SESSION->currentgroup[$course->id])) {
235 $selectedgroup = $SESSION->currentgroup[$course->id];
236 } else {
237 $selectedgroup = groups_get_all_groups($course->id, $USER->id);
238 if (is_array($selectedgroup)) {
239 $selectedgroup = array_shift(array_keys($selectedgroup));
240 $SESSION->currentgroup[$course->id] = $selectedgroup;
241 } else {
242 $selectedgroup = 0;
243 }
244 }
245 }
246
c215b32b 247 } else {
248 $context = $sitecontext;
249 }
250
251 // Get all the possible users
252 $users = array();
253
ae1467bb 254 // Define limitfrom and limitnum for queries below
255 // If $showusers is enabled... don't apply limitfrom and limitnum
256 $limitfrom = empty($showusers) ? 0 : '';
257 $limitnum = empty($showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : '';
258
c215b32b 259 // If looking at a different host, we're interested in all our site users
5841aa91 260 if ($hostid == $CFG->mnet_localhost_id && $course->id != SITEID) {
a327f25e
AG
261 $courseusers = get_enrolled_users($context, '', $selectedgroup, 'u.id, ' . get_all_user_name_fields(true, 'u'),
262 null, $limitfrom, $limitnum);
c215b32b 263 } else {
79eaec48 264 // this may be a lot of users :-(
a327f25e
AG
265 $courseusers = $DB->get_records('user', array('deleted'=>0), 'lastaccess DESC', 'id, ' . get_all_user_name_fields(true),
266 $limitfrom, $limitnum);
c215b32b 267 }
268
269 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) {
270 $showusers = 1;
271 }
272
273 if ($showusers) {
274 if ($courseusers) {
275 foreach ($courseusers as $courseuser) {
276 $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
277 }
278 }
629e12fd 279 $users[$CFG->siteguest] = get_string('guestuser');
c215b32b 280 }
281
2c1833bd 282 // Get all the hosts that have log records
283 $sql = "select distinct
284 h.id,
285 h.name
286 from
29f83769 287 {mnet_host} h,
288 {mnet_log} l
2c1833bd 289 where
290 h.id = l.hostid
291 order by
292 h.name";
c215b32b 293
29f83769 294 if ($hosts = $DB->get_records_sql($sql)) {
dd2a21da 295 foreach($hosts as $host) {
296 $hostarray[$host->id] = $host->name;
297 }
c215b32b 298 }
299
300 $hostarray[$CFG->mnet_localhost_id] = $SITE->fullname;
301 asort($hostarray);
302
6770330d
PS
303 $dropdown = array();
304
c215b32b 305 foreach($hostarray as $hostid => $name) {
306 $courses = array();
307 $sites = array();
308 if ($CFG->mnet_localhost_id == $hostid) {
033af4b7 309 if (has_capability('report/log:view', $sitecontext) && $showcourses) {
31f40864 310 if ($ccc = $DB->get_records("course", null, "fullname","id,shortname,fullname,category")) {
c215b32b 311 foreach ($ccc as $cc) {
5577ceb3 312 if ($cc->id == SITEID) {
f054df17 313 $sites["$hostid/$cc->id"] = format_string($cc->fullname).' ('.get_string('site').')';
c215b32b 314 } else {
31f40864 315 $courses["$hostid/$cc->id"] = format_string(get_course_display_name_for_list($cc));
c215b32b 316 }
317 }
318 }
319 }
320 } else {
033af4b7 321 if (has_capability('report/log:view', $sitecontext) && $showcourses) {
29f83769 322 $sql = "SELECT DISTINCT course, coursename FROM {mnet_log} where hostid = ?";
323 if ($ccc = $DB->get_records_sql($sql, array($hostid))) {
c215b32b 324 foreach ($ccc as $cc) {
5577ceb3 325 if (1 == $cc->course) { // TODO: this might be wrong - site course may have another id
326 $sites["$hostid/$cc->course"] = $cc->coursename.' ('.get_string('site').')';
c215b32b 327 } else {
5577ceb3 328 $courses["$hostid/$cc->course"] = $cc->coursename;
c215b32b 329 }
330 }
331 }
332 }
333 }
334
335 asort($courses);
6770330d 336 $dropdown[] = array($name=>($sites + $courses));
c215b32b 337 }
338
339
340 $activities = array();
341 $selectedactivity = "";
342
9ecb50e6
MG
343 $modinfo = get_fast_modinfo($course);
344 if (!empty($modinfo->cms)) {
c215b32b 345 $section = 0;
78932978 346 $thissection = array();
9ecb50e6
MG
347 foreach ($modinfo->cms as $cm) {
348 if (!$cm->uservisible || !$cm->has_view()) {
c215b32b 349 continue;
350 }
9ecb50e6 351 if ($cm->sectionnum > 0 and $section <> $cm->sectionnum) {
78932978
AO
352 $activities[] = $thissection;
353 $thissection = array();
c215b32b 354 }
9ecb50e6
MG
355 $section = $cm->sectionnum;
356 $modname = strip_tags($cm->get_formatted_name());
2f1e464a
PS
357 if (core_text::strlen($modname) > 55) {
358 $modname = core_text::substr($modname, 0, 50)."...";
c215b32b 359 }
9ecb50e6
MG
360 if (!$cm->visible) {
361 $modname = "(".$modname.")";
c215b32b 362 }
78932978
AO
363 $key = get_section_name($course, $cm->sectionnum);
364 if (!isset($thissection[$key])) {
365 $thissection[$key] = array();
366 }
367 $thissection[$key][$cm->id] = $modname;
c215b32b 368
9ecb50e6
MG
369 if ($cm->id == $modid) {
370 $selectedactivity = "$cm->id";
c215b32b 371 }
372 }
78932978
AO
373 if (!empty($thissection)) {
374 $activities[] = $thissection;
375 }
c215b32b 376 }
377
033af4b7 378 if (has_capability('report/log:view', $sitecontext) && !$course->category) {
c215b32b 379 $activities["site_errors"] = get_string("siteerrors");
380 if ($modid === "site_errors") {
381 $selectedactivity = "site_errors";
382 }
383 }
384
385 $strftimedate = get_string("strftimedate");
386 $strftimedaydate = get_string("strftimedaydate");
387
388 asort($users);
389
390 // Prepare the list of action options.
391 $actions = array(
392 'view' => get_string('view'),
393 'add' => get_string('add'),
394 'update' => get_string('update'),
395 'delete' => get_string('delete'),
396 '-view' => get_string('allchanges')
397 );
398
399 // Get all the possible dates
400 // Note that we are keeping track of real (GMT) time and user time
401 // User time is only used in displays - all calcs and passing is GMT
402
403 $timenow = time(); // GMT
404
405 // What day is it now for the user, and when is midnight that day (in GMT).
406 $timemidnight = $today = usergetmidnight($timenow);
407
408 // Put today up the top of the list
b84a8425
ARN
409 $dates = array(
410 "0" => get_string('alldays'),
411 "$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate)
412 );
c215b32b 413
414 if (!$course->startdate or ($course->startdate > $timenow)) {
415 $course->startdate = $course->timecreated;
416 }
417
418 $numdates = 1;
419 while ($timemidnight > $course->startdate and $numdates < 365) {
420 $timemidnight = $timemidnight - 86400;
421 $timenow = $timenow - 86400;
422 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
423 $numdates++;
424 }
425
b84a8425 426 if ($selecteddate === "today") {
c215b32b 427 $selecteddate = $today;
428 }
429
033af4b7 430 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/report/log/index.php\" method=\"get\">\n";
5577ceb3 431 echo "<div>\n";//invisible fieldset here breaks wrapping
c215b32b 432 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
433 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
434 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
033af4b7 435 if (has_capability('report/log:view', $sitecontext) && $showcourses) {
aa6c1ced 436 $cid = empty($course->id)? '1' : $course->id;
2bbd896e 437 echo html_writer::label(get_string('selectacoursesite'), 'menuhost_course', false, array('class' => 'accesshide'));
6770330d 438 echo html_writer::select($dropdown, "host_course", $hostid.'/'.$cid);
c215b32b 439 } else {
440 $courses = array();
31f40864 441 $courses[$course->id] = get_course_display_name_for_list($course) . ((empty($course->category)) ? ' ('.get_string('site').') ' : '');
2bbd896e 442 echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide'));
d776d59e 443 echo html_writer::select($courses,"id",$course->id, false);
033af4b7 444 if (has_capability('report/log:view', $sitecontext)) {
fbaea88f 445 $a = new stdClass();
033af4b7 446 $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
c215b32b 447 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers";
448 print_string('logtoomanycourses','moodle',$a);
449 }
450 }
451
452 if ($showgroups) {
2c386f82 453 if ($cgroups = groups_get_all_groups($course->id)) {
c215b32b 454 foreach ($cgroups as $cgroup) {
455 $groups[$cgroup->id] = $cgroup->name;
456 }
457 }
458 else {
459 $groups = array();
460 }
2bbd896e 461 echo html_writer::label(get_string('selectagroup'), 'menugroup', false, array('class' => 'accesshide'));
d776d59e 462 echo html_writer::select($groups, "group", $selectedgroup, get_string("allgroups"));
c215b32b 463 }
464
465 if ($showusers) {
2bbd896e 466 echo html_writer::label(get_string('participantslist'), 'menuuser', false, array('class' => 'accesshide'));
d776d59e 467 echo html_writer::select($users, "user", $selecteduser, get_string("allparticipants"));
c215b32b 468 }
469 else {
470 $users = array();
471 if (!empty($selecteduser)) {
29f83769 472 $user = $DB->get_record('user', array('id'=>$selecteduser));
c215b32b 473 $users[$selecteduser] = fullname($user);
474 }
475 else {
476 $users[0] = get_string('allparticipants');
477 }
2bbd896e 478 echo html_writer::label(get_string('participantslist'), 'menuuser', false, array('class' => 'accesshide'));
d776d59e 479 echo html_writer::select($users, "user", $selecteduser, false);
74ec49a1 480 $a = new stdClass();
033af4b7 481 $a->url = "$CFG->wwwroot/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
c215b32b 482 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses";
483 print_string('logtoomanyusers','moodle',$a);
484 }
aa6c1ced 485
2bbd896e 486 echo html_writer::label(get_string('date'), 'menudate', false, array('class' => 'accesshide'));
b84a8425 487 echo html_writer::select($dates, "date", $selecteddate, false);
2bbd896e 488 echo html_writer::label(get_string('showreports'), 'menumodid', false, array('class' => 'accesshide'));
d776d59e 489 echo html_writer::select($activities, "modid", $selectedactivity, get_string("allactivities"));
2bbd896e 490 echo html_writer::label(get_string('actions'), 'menumodaction', false, array('class' => 'accesshide'));
d776d59e 491 echo html_writer::select($actions, 'modaction', $modaction, get_string("allactions"));
aa6c1ced 492
c215b32b 493 $logformats = array('showashtml' => get_string('displayonpage'),
494 'downloadascsv' => get_string('downloadtext'),
5577ceb3 495 'downloadasods' => get_string('downloadods'),
c215b32b 496 'downloadasexcel' => get_string('downloadexcel'));
2bbd896e 497 echo html_writer::label(get_string('logsformat', 'report_log'), 'menulogformat', false, array('class' => 'accesshide'));
d776d59e 498 echo html_writer::select($logformats, 'logformat', $logformat, false);
c215b32b 499 echo '<input type="submit" value="'.get_string('gettheselogs').'" />';
5577ceb3 500 echo '</div>';
501 echo '</form>';
c215b32b 502}