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