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