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