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