blog MDL-19792 Upgraded two deprecate functions
[moodle.git] / course / report / log / lib.php
CommitLineData
dfab77a2 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file contains functions used by the log reports
20 *
d9cb06dc 21 * @package course
dfab77a2 22 * @copyright 1999 onwards Martin Dougiamas http://moodle.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
0a935fb3 25
c215b32b 26function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $selecteddate='today',
27 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') {
28
5387640a 29 global $USER, $CFG, $SITE, $DB, $OUTPUT;
c215b32b 30 require_once $CFG->dirroot.'/mnet/peer.php';
31
32 $mnet_peer = new mnet_peer();
33 $mnet_peer->set_id($hostid);
34
29f83769 35 $sql = "SELECT DISTINCT course, hostid, coursename FROM {mnet_log}";
36 $courses = $DB->get_records_sql($sql);
c215b32b 37 $remotecoursecount = count($courses);
38
39 // first check to see if we can override showcourses and showusers
29f83769 40 $numcourses = $remotecoursecount + $DB->count_records('course');
c215b32b 41 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
42 $showcourses = 1;
43 }
44
09ee7b0d 45 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
c215b32b 46
47 // Context for remote data is always SITE
48 // Groups for remote data are always OFF
49 if ($hostid == $CFG->mnet_localhost_id) {
50 $context = get_context_instance(CONTEXT_COURSE, $course->id);
51
52 /// Setup for group handling.
53 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
54 $selectedgroup = get_current_group($course->id);
55 $showgroups = false;
56 }
57 else if ($course->groupmode) {
58 $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup;
59 $showgroups = true;
60 }
61 else {
62 $selectedgroup = 0;
63 $showgroups = false;
64 }
65
66 } else {
67 $context = $sitecontext;
68 }
69
70 // Get all the possible users
71 $users = array();
72
ae1467bb 73 // Define limitfrom and limitnum for queries below
74 // If $showusers is enabled... don't apply limitfrom and limitnum
75 $limitfrom = empty($showusers) ? 0 : '';
76 $limitnum = empty($showusers) ? COURSE_MAX_USERS_PER_DROPDOWN + 1 : '';
77
c215b32b 78 // If looking at a different host, we're interested in all our site users
5841aa91 79 if ($hostid == $CFG->mnet_localhost_id && $course->id != SITEID) {
ae1467bb 80 $courseusers = get_users_by_capability($context, 'moodle/course:view', 'u.id, u.firstname, u.lastname, u.idnumber', 'lastname ASC, firstname ASC', $limitfrom, $limitnum, $selectedgroup,'', false);
c215b32b 81 } else {
79eaec48 82 // this may be a lot of users :-(
ae1467bb 83 $courseusers = $DB->get_records('user', array('deleted'=>0), 'lastaccess DESC', 'id, firstname, lastname, idnumber', $limitfrom, $limitnum);
c215b32b 84 }
85
86 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) {
87 $showusers = 1;
88 }
89
90 if ($showusers) {
91 if ($courseusers) {
92 foreach ($courseusers as $courseuser) {
93 $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
94 }
95 }
d9cb06dc 96 if ($guest = get_complete_user_data('username', 'guest')) {
c215b32b 97 $users[$guest->id] = fullname($guest);
98 }
99 }
100
2c1833bd 101 // Get all the hosts that have log records
102 $sql = "select distinct
103 h.id,
104 h.name
105 from
29f83769 106 {mnet_host} h,
107 {mnet_log} l
2c1833bd 108 where
109 h.id = l.hostid
110 order by
111 h.name";
c215b32b 112
29f83769 113 if ($hosts = $DB->get_records_sql($sql)) {
dd2a21da 114 foreach($hosts as $host) {
115 $hostarray[$host->id] = $host->name;
116 }
c215b32b 117 }
118
119 $hostarray[$CFG->mnet_localhost_id] = $SITE->fullname;
120 asort($hostarray);
121
122 foreach($hostarray as $hostid => $name) {
123 $courses = array();
124 $sites = array();
125 if ($CFG->mnet_localhost_id == $hostid) {
a2e4bf7f 126 if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) {
29f83769 127 if ($ccc = $DB->get_records("course", null, "fullname","id,fullname,category")) {
c215b32b 128 foreach ($ccc as $cc) {
5577ceb3 129 if ($cc->id == SITEID) {
f054df17 130 $sites["$hostid/$cc->id"] = format_string($cc->fullname).' ('.get_string('site').')';
c215b32b 131 } else {
f054df17 132 $courses["$hostid/$cc->id"] = format_string($cc->fullname);
c215b32b 133 }
134 }
135 }
136 }
137 } else {
a2e4bf7f 138 if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) {
29f83769 139 $sql = "SELECT DISTINCT course, coursename FROM {mnet_log} where hostid = ?";
140 if ($ccc = $DB->get_records_sql($sql, array($hostid))) {
c215b32b 141 foreach ($ccc as $cc) {
5577ceb3 142 if (1 == $cc->course) { // TODO: this might be wrong - site course may have another id
143 $sites["$hostid/$cc->course"] = $cc->coursename.' ('.get_string('site').')';
c215b32b 144 } else {
5577ceb3 145 $courses["$hostid/$cc->course"] = $cc->coursename;
c215b32b 146 }
147 }
148 }
149 }
150 }
151
152 asort($courses);
153 $dropdown[$name] = $sites + $courses;
154 }
155
156
157 $activities = array();
158 $selectedactivity = "";
159
160/// Casting $course->modinfo to string prevents one notice when the field is null
161 if ($modinfo = unserialize((string)$course->modinfo)) {
162 $section = 0;
163 if ($course->format == 'weeks') { // Bodgy
164 $strsection = get_string("week");
165 } else {
166 $strsection = get_string("topic");
167 }
168 foreach ($modinfo as $mod) {
169 if ($mod->mod == "label") {
170 continue;
171 }
172 if ($mod->section > 0 and $section <> $mod->section) {
173 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
174 }
175 $section = $mod->section;
176 $mod->name = strip_tags(format_string(urldecode($mod->name),true));
177 if (strlen($mod->name) > 55) {
178 $mod->name = substr($mod->name, 0, 50)."...";
179 }
180 if (!$mod->visible) {
181 $mod->name = "(".$mod->name.")";
182 }
183 $activities["$mod->cm"] = $mod->name;
184
185 if ($mod->cm == $modid) {
186 $selectedactivity = "$mod->cm";
187 }
188 }
189 }
190
a2e4bf7f 191 if (has_capability('coursereport/log:view', $sitecontext) && !$course->category) {
c215b32b 192 $activities["site_errors"] = get_string("siteerrors");
193 if ($modid === "site_errors") {
194 $selectedactivity = "site_errors";
195 }
196 }
197
198 $strftimedate = get_string("strftimedate");
199 $strftimedaydate = get_string("strftimedaydate");
200
201 asort($users);
202
203 // Prepare the list of action options.
204 $actions = array(
205 'view' => get_string('view'),
206 'add' => get_string('add'),
207 'update' => get_string('update'),
208 'delete' => get_string('delete'),
209 '-view' => get_string('allchanges')
210 );
211
212 // Get all the possible dates
213 // Note that we are keeping track of real (GMT) time and user time
214 // User time is only used in displays - all calcs and passing is GMT
215
216 $timenow = time(); // GMT
217
218 // What day is it now for the user, and when is midnight that day (in GMT).
219 $timemidnight = $today = usergetmidnight($timenow);
220
221 // Put today up the top of the list
222 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
223
224 if (!$course->startdate or ($course->startdate > $timenow)) {
225 $course->startdate = $course->timecreated;
226 }
227
228 $numdates = 1;
229 while ($timemidnight > $course->startdate and $numdates < 365) {
230 $timemidnight = $timemidnight - 86400;
231 $timenow = $timenow - 86400;
232 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
233 $numdates++;
234 }
235
236 if ($selecteddate == "today") {
237 $selecteddate = $today;
238 }
239
5577ceb3 240 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/course/report/log/index.php\" method=\"get\">\n";
241 echo "<div>\n";//invisible fieldset here breaks wrapping
c215b32b 242 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
243 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
244 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
a2e4bf7f 245 if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) {
c215b32b 246 $cid = empty($course->id)? '1' : $course->id;
7b1f2c82 247 $select = html_select::make($dropdown, "host_course", $hostid.'/'.$cid);
0b280bba 248 $select->nested = true;
249 echo $OUTPUT->select($select);
c215b32b 250 } else {
251 $courses = array();
5577ceb3 252 $courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' ('.get_string('site').') ' : '');
5387640a 253 echo $OUTPUT->select(html_select::make($courses,"id",$course->id, false));
a2e4bf7f 254 if (has_capability('coursereport/log:view', $sitecontext)) {
5577ceb3 255 $a = new object();
c215b32b 256 $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
257 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers";
258 print_string('logtoomanycourses','moodle',$a);
259 }
260 }
261
262 if ($showgroups) {
2c386f82 263 if ($cgroups = groups_get_all_groups($course->id)) {
c215b32b 264 foreach ($cgroups as $cgroup) {
265 $groups[$cgroup->id] = $cgroup->name;
266 }
267 }
268 else {
269 $groups = array();
270 }
5387640a 271 echo $OUTPUT->select(html_select::make($groups, "group", $selectedgroup, get_string("allgroups")));
c215b32b 272 }
273
274 if ($showusers) {
5387640a 275 echo $OUTPUT->select(html_select::make($users, "user", $selecteduser, get_string("allparticipants")));
c215b32b 276 }
277 else {
278 $users = array();
279 if (!empty($selecteduser)) {
29f83769 280 $user = $DB->get_record('user', array('id'=>$selecteduser));
c215b32b 281 $users[$selecteduser] = fullname($user);
282 }
283 else {
284 $users[0] = get_string('allparticipants');
285 }
5387640a 286 echo $OUTPUT->select(html_select::make($users, "user", $selecteduser, false));
c215b32b 287 $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
288 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses";
289 print_string('logtoomanyusers','moodle',$a);
290 }
5387640a 291
292 echo $OUTPUT->select(html_select::make($dates, "date", $selecteddate, get_string("alldays")));
293
294 $select = html_select::make($activities, "modid", $selectedactivity);
295 $select->nothinglabel = get_string("allactivities");
296 $select->nothingvalue = '';
297 echo $OUTPUT->select($select);
298
299 echo $OUTPUT->select(html_select::make($actions, 'modaction', $modaction, get_string("allactions")));
c215b32b 300
301 $logformats = array('showashtml' => get_string('displayonpage'),
302 'downloadascsv' => get_string('downloadtext'),
5577ceb3 303 'downloadasods' => get_string('downloadods'),
c215b32b 304 'downloadasexcel' => get_string('downloadexcel'));
5387640a 305 echo $OUTPUT->select(html_select::make($logformats, 'logformat', $logformat, false));
c215b32b 306 echo '<input type="submit" value="'.get_string('gettheselogs').'" />';
5577ceb3 307 echo '</div>';
308 echo '</form>';
c215b32b 309}
310
92890025 311function print_log_selector_form($course, $selecteduser=0, $selecteddate='today',
312 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') {
0a935fb3 313
5387640a 314 global $USER, $CFG, $DB, $OUTPUT;
0a935fb3 315
316 // first check to see if we can override showcourses and showusers
29f83769 317 $numcourses = $DB->count_records("course");
0a935fb3 318 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
319 $showcourses = 1;
320 }
51792df0 321
09ee7b0d 322 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
d02eeded 323 $context = get_context_instance(CONTEXT_COURSE, $course->id);
324
0a935fb3 325 /// Setup for group handling.
d02eeded 326 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
0a935fb3 327 $selectedgroup = get_current_group($course->id);
328 $showgroups = false;
329 }
330 else if ($course->groupmode) {
331 $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup;
332 $showgroups = true;
333 }
334 else {
335 $selectedgroup = 0;
336 $showgroups = false;
337 }
338
339 // Get all the possible users
340 $users = array();
341
1936c10e 342 if ($course->id != SITEID) {
72c6b04a 343 $courseusers = get_users_by_capability($context, 'moodle/course:view', 'u.id, u.firstname, u.lastname, u.idnumber', 'lastname ASC, firstname ASC', '','',$selectedgroup,null, false);
344} else {
79eaec48 345 // this may be a lot of users :-(
0f56d929 346 $courseusers = $DB->get_records('user', array('deleted'=>0), 'lastaccess DESC', 'id, firstname, lastname, idnumber');
0a935fb3 347 }
72c6b04a 348
0a935fb3 349 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) {
350 $showusers = 1;
351 }
352
353 if ($showusers) {
354 if ($courseusers) {
355 foreach ($courseusers as $courseuser) {
d02eeded 356 $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
0a935fb3 357 }
358 }
d9cb06dc 359 if ($guest = get_complete_user_data('username', 'guest')) {
0a935fb3 360 $users[$guest->id] = fullname($guest);
361 }
362 }
363
a2e4bf7f 364 if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) {
29f83769 365 if ($ccc = $DB->get_records("course", null, "fullname", "id,fullname,category")) {
0a935fb3 366 foreach ($ccc as $cc) {
367 if ($cc->category) {
f054df17 368 $courses["$cc->id"] = format_string($cc->fullname);
0a935fb3 369 } else {
f054df17 370 $courses["$cc->id"] = format_string($cc->fullname) . ' (Site)';
0a935fb3 371 }
372 }
373 }
374 asort($courses);
375 }
376
377 $activities = array();
378 $selectedactivity = "";
379
fea43a7f 380/// Casting $course->modinfo to string prevents one notice when the field is null
381 if ($modinfo = unserialize((string)$course->modinfo)) {
0a935fb3 382 $section = 0;
383 if ($course->format == 'weeks') { // Bodgy
384 $strsection = get_string("week");
385 } else {
386 $strsection = get_string("topic");
387 }
388 foreach ($modinfo as $mod) {
389 if ($mod->mod == "label") {
390 continue;
391 }
392 if ($mod->section > 0 and $section <> $mod->section) {
393 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
394 }
395 $section = $mod->section;
396 $mod->name = strip_tags(format_string(urldecode($mod->name),true));
397 if (strlen($mod->name) > 55) {
398 $mod->name = substr($mod->name, 0, 50)."...";
399 }
400 if (!$mod->visible) {
401 $mod->name = "(".$mod->name.")";
402 }
403 $activities["$mod->cm"] = $mod->name;
404
405 if ($mod->cm == $modid) {
406 $selectedactivity = "$mod->cm";
407 }
408 }
409 }
410
a2e4bf7f 411 if (has_capability('coursereport/log:view', $sitecontext) && ($course->id == SITEID)) {
0a935fb3 412 $activities["site_errors"] = get_string("siteerrors");
413 if ($modid === "site_errors") {
414 $selectedactivity = "site_errors";
415 }
416 }
417
418 $strftimedate = get_string("strftimedate");
419 $strftimedaydate = get_string("strftimedaydate");
420
421 asort($users);
422
ee35e0b8 423 // Prepare the list of action options.
424 $actions = array(
425 'view' => get_string('view'),
426 'add' => get_string('add'),
427 'update' => get_string('update'),
428 'delete' => get_string('delete'),
429 '-view' => get_string('allchanges')
430 );
431
0a935fb3 432 // Get all the possible dates
433 // Note that we are keeping track of real (GMT) time and user time
434 // User time is only used in displays - all calcs and passing is GMT
435
436 $timenow = time(); // GMT
437
438 // What day is it now for the user, and when is midnight that day (in GMT).
439 $timemidnight = $today = usergetmidnight($timenow);
440
441 // Put today up the top of the list
442 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
443
444 if (!$course->startdate or ($course->startdate > $timenow)) {
445 $course->startdate = $course->timecreated;
446 }
447
448 $numdates = 1;
449 while ($timemidnight > $course->startdate and $numdates < 365) {
450 $timemidnight = $timemidnight - 86400;
451 $timenow = $timenow - 86400;
452 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
453 $numdates++;
454 }
455
456 if ($selecteddate == "today") {
457 $selecteddate = $today;
458 }
459
5577ceb3 460 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/course/report/log/index.php\" method=\"get\">\n";
461 echo "<div>\n";
0a935fb3 462 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
463 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
464 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
a2e4bf7f 465 if (has_capability('coursereport/log:view', $sitecontext) && $showcourses) {
5387640a 466 echo $OUTPUT->select(html_select::make($courses, "id", $course->id, false));
0a935fb3 467 } else {
468 // echo '<input type="hidden" name="id" value="'.$course->id.'" />';
469 $courses = array();
5577ceb3 470 $courses[$course->id] = $course->fullname . (($course->id == SITEID) ? ' ('.get_string('site').') ' : '');
5387640a 471 echo $OUTPUT->select(html_select::make($courses,"id",$course->id, false));
a2e4bf7f 472 if (has_capability('coursereport/log:view', $sitecontext)) {
5577ceb3 473 $a = new object();
4d70ff5c 474 $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
0a935fb3 475 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers";
476 print_string('logtoomanycourses','moodle',$a);
477 }
478 }
479
480 if ($showgroups) {
2c386f82 481 if ($cgroups = groups_get_all_groups($course->id)) {
0a935fb3 482 foreach ($cgroups as $cgroup) {
483 $groups[$cgroup->id] = $cgroup->name;
484 }
485 }
486 else {
487 $groups = array();
488 }
5387640a 489 echo $OUTPUT->select(html_select::make($groups, "group", $selectedgroup, get_string("allgroups")));
0a935fb3 490 }
491
492 if ($showusers) {
5387640a 493 echo $OUTPUT->select(html_select::make($users, "user", $selecteduser, get_string("allparticipants")));
0a935fb3 494 }
495 else {
496 $users = array();
497 if (!empty($selecteduser)) {
29f83769 498 $user = $DB->get_record('user', array('id'=>$selecteduser));
0a935fb3 499 $users[$selecteduser] = fullname($user);
500 }
501 else {
502 $users[0] = get_string('allparticipants');
503 }
5387640a 504 echo $OUTPUT->select(html_select::make($users, "user", $selecteduser, false));
5577ceb3 505 $a = new object();
4d70ff5c 506 $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
0a935fb3 507 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses";
508 print_string('logtoomanyusers','moodle',$a);
509 }
5387640a 510 echo $OUTPUT->select(html_select::make($dates, "date", $selecteddate, get_string("alldays")));
511
512 $select = html_select::make($activities, "modid", $selectedactivity);
513 $select->nothinglabel = get_string("allactivities");
514 $select->nothingvalue = '';
515 echo $OUTPUT->select($select);
516
517 echo $OUTPUT->select(html_select::make($actions, 'modaction', $modaction, get_string("allactions")));
92890025 518
519 $logformats = array('showashtml' => get_string('displayonpage'),
520 'downloadascsv' => get_string('downloadtext'),
ea49a66c 521 'downloadasods' => get_string('downloadods'),
92890025 522 'downloadasexcel' => get_string('downloadexcel'));
5387640a 523
524 echo $OUTPUT->select(html_select::make($logformats, 'logformat', $logformat, false));
92890025 525 echo '<input type="submit" value="'.get_string('gettheselogs').'" />';
5577ceb3 526 echo '</div>';
527 echo '</form>';
0a935fb3 528}
529
dfab77a2 530/**
531 * This function extends the navigation with the report items
532 *
533 * @param navigation_node $navigation The navigation node to extend
534 * @param stdClass $course The course to object for the report
535 * @param stdClass $context The context of the course
536 */
537function log_report_extend_navigation($navigation, $course, $context) {
538 global $CFG, $OUTPUT;
539 if (has_capability('coursereport/log:view', $context)) {
540 $url = new moodle_url($CFG->wwwroot.'/course/report/log/index.php', array('id'=>$course->id));
541 $navigation->add(get_string('log:view', 'coursereport_log'), $url, navigation_node::TYPE_SETTING, null, null, $OUTPUT->old_icon_url('i/report'));
542 }
543 if (has_capability('coursereport/log:viewlive', $context)) {
544 $livelogs = get_string('livelogs');
545 $link = html_link::make('/course/report/log/live.php?id='. $course->id, $livelogs);
546 $link->add_action(new popup_action('click', $link->url, 'livelog', array('height' => 500, 'width' => 800)));
547 $navigation->add($livelogs, $link, navigation_node::TYPE_SETTING, null, null, $OUTPUT->old_icon_url('i/report'));
548 }
549}