3 function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $selecteddate='today',
4 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') {
6 global $USER, $CFG, $SITE;
7 require_once $CFG->dirroot
.'/mnet/peer.php';
9 $mnet_peer = new mnet_peer();
10 $mnet_peer->set_id($hostid);
12 $sql = "select distinct course, hostid, coursename from {$CFG->prefix}mnet_log";
13 $courses = get_records_sql($sql);
14 $remotecoursecount = count($courses);
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) {
22 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
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
);
29 /// Setup for group handling.
30 if ($course->groupmode
== SEPARATEGROUPS
and !has_capability('moodle/site:accessallgroups', $context)) {
31 $selectedgroup = get_current_group($course->id
);
34 else if ($course->groupmode
) {
35 $selectedgroup = ($selectedgroup == -1) ?
get_current_group($course->id
) : $selectedgroup;
44 $context = $sitecontext;
47 // Get all the possible users
50 // If looking at a different host, we're interested in all our site users
51 if ($hostid == $CFG->mnet_localhost_id
&& $course->id
!= SITEID
) {
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');
55 $courseusers = get_course_users($course->id
, '', '', 'u.id, u.firstname, u.lastname, u.idnumber');
58 $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname, u.idnumber");
61 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN
&& !$showusers) {
67 foreach ($courseusers as $courseuser) {
68 $users[$courseuser->id
] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
71 if ($guest = get_guest()) {
72 $users[$guest->id
] = fullname($guest);
76 // Get all the hosts that have log records
77 $sql = "select distinct
81 {$CFG->prefix}mnet_host h,
82 {$CFG->prefix}mnet_log l
88 if ($hosts = get_records_sql($sql)) {
89 foreach($hosts as $host) {
90 $hostarray[$host->id
] = $host->name
;
94 $hostarray[$CFG->mnet_localhost_id
] = $SITE->fullname
;
97 foreach($hostarray as $hostid => $name) {
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->id
== SITEID
) {
105 $sites["$hostid/$cc->id"] = format_string($cc->fullname
).' ('.get_string('site').')';
107 $courses["$hostid/$cc->id"] = format_string($cc->fullname
);
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
) { // TODO: this might be wrong - site course may have another id
118 $sites["$hostid/$cc->course"] = $cc->coursename
.' ('.get_string('site').')';
120 $courses["$hostid/$cc->course"] = $cc->coursename
;
128 $dropdown[$name] = $sites +
$courses;
132 $activities = array();
133 $selectedactivity = "";
135 /// Casting $course->modinfo to string prevents one notice when the field is null
136 if ($modinfo = unserialize((string)$course->modinfo
)) {
138 if ($course->format
== 'weeks') { // Bodgy
139 $strsection = get_string("week");
141 $strsection = get_string("topic");
143 foreach ($modinfo as $mod) {
144 if ($mod->mod
== "label") {
147 if ($mod->section
> 0 and $section <> $mod->section
) {
148 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
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)."...";
155 if (!$mod->visible
) {
156 $mod->name
= "(".$mod->name
.")";
158 $activities["$mod->cm"] = $mod->name
;
160 if ($mod->cm
== $modid) {
161 $selectedactivity = "$mod->cm";
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";
173 $strftimedate = get_string("strftimedate");
174 $strftimedaydate = get_string("strftimedaydate");
178 // Prepare the list of action options.
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')
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
191 $timenow = time(); // GMT
193 // What day is it now for the user, and when is midnight that day (in GMT).
194 $timemidnight = $today = usergetmidnight($timenow);
196 // Put today up the top of the list
197 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
199 if (!$course->startdate
or ($course->startdate
> $timenow)) {
200 $course->startdate
= $course->timecreated
;
204 while ($timemidnight > $course->startdate
and $numdates < 365) {
205 $timemidnight = $timemidnight - 86400;
206 $timenow = $timenow - 86400;
207 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
211 if ($selecteddate == "today") {
212 $selecteddate = $today;
215 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/course/report/log/index.php\" method=\"get\">\n";
216 echo "<div>\n";//invisible fieldset here breaks wrapping
217 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
218 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
219 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
220 if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
221 $cid = empty($course->id
)?
'1' : $course->id
;
222 choose_from_menu_nested($dropdown, "host_course", $hostid.'/'.$cid, "");
225 $courses[$course->id
] = $course->fullname
. ((empty($course->category
)) ?
' ('.get_string('site').') ' : '');
226 choose_from_menu($courses,"id",$course->id
,false);
227 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);
236 if ($cgroups = get_groups($course->id
)) {
237 foreach ($cgroups as $cgroup) {
238 $groups[$cgroup->id
] = $cgroup->name
;
244 choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") );
248 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
252 if (!empty($selecteduser)) {
253 $user = get_record('user','id',$selecteduser);
254 $users[$selecteduser] = fullname($user);
257 $users[0] = get_string('allparticipants');
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);
264 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
265 choose_from_menu ($activities, "modid", $selectedactivity, get_string("allactivities"), "", "");
266 choose_from_menu ($actions, 'modaction', $modaction, get_string("allactions"));
268 $logformats = array('showashtml' => get_string('displayonpage'),
269 'downloadascsv' => get_string('downloadtext'),
270 'downloadasods' => get_string('downloadods'),
271 'downloadasexcel' => get_string('downloadexcel'));
272 choose_from_menu ($logformats, 'logformat', $logformat, false);
273 echo '<input type="submit" value="'.get_string('gettheselogs').'" />';
278 function print_log_selector_form($course, $selecteduser=0, $selecteddate='today',
279 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') {
283 // first check to see if we can override showcourses and showusers
284 $numcourses = count_records_select("course", "", "COUNT(id)");
285 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN
&& !$showcourses) {
289 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
290 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
292 /// Setup for group handling.
293 if ($course->groupmode
== SEPARATEGROUPS
and !has_capability('moodle/site:accessallgroups', $context)) {
294 $selectedgroup = get_current_group($course->id
);
297 else if ($course->groupmode
) {
298 $selectedgroup = ($selectedgroup == -1) ?
get_current_group($course->id
) : $selectedgroup;
306 // Get all the possible users
309 if ($course->id
!= SITEID
) {
310 if ($selectedgroup) { // If using a group, only get users in that group.
311 $courseusers = get_group_users($selectedgroup, 'u.lastname ASC', '', 'u.id, u.firstname, u.lastname, u.idnumber');
313 $courseusers = get_course_users($course->id
, '', '', 'u.id, u.firstname, u.lastname, u.idnumber');
316 $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname, u.idnumber");
319 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN
&& !$showusers) {
325 foreach ($courseusers as $courseuser) {
326 $users[$courseuser->id
] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
329 if ($guest = get_guest()) {
330 $users[$guest->id
] = fullname($guest);
334 if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
335 if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) {
336 foreach ($ccc as $cc) {
338 $courses["$cc->id"] = format_string($cc->fullname
);
340 $courses["$cc->id"] = format_string($cc->fullname
) . ' (Site)';
347 $activities = array();
348 $selectedactivity = "";
350 /// Casting $course->modinfo to string prevents one notice when the field is null
351 if ($modinfo = unserialize((string)$course->modinfo
)) {
353 if ($course->format
== 'weeks') { // Bodgy
354 $strsection = get_string("week");
356 $strsection = get_string("topic");
358 foreach ($modinfo as $mod) {
359 if ($mod->mod
== "label") {
362 if ($mod->section
> 0 and $section <> $mod->section
) {
363 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
365 $section = $mod->section
;
366 $mod->name
= strip_tags(format_string(urldecode($mod->name
),true));
367 if (strlen($mod->name
) > 55) {
368 $mod->name
= substr($mod->name
, 0, 50)."...";
370 if (!$mod->visible
) {
371 $mod->name
= "(".$mod->name
.")";
373 $activities["$mod->cm"] = $mod->name
;
375 if ($mod->cm
== $modid) {
376 $selectedactivity = "$mod->cm";
381 if (has_capability('moodle/site:viewreports', $sitecontext) && ($course->id
== SITEID
)) {
382 $activities["site_errors"] = get_string("siteerrors");
383 if ($modid === "site_errors") {
384 $selectedactivity = "site_errors";
388 $strftimedate = get_string("strftimedate");
389 $strftimedaydate = get_string("strftimedaydate");
393 // Prepare the list of action options.
395 'view' => get_string('view'),
396 'add' => get_string('add'),
397 'update' => get_string('update'),
398 'delete' => get_string('delete'),
399 '-view' => get_string('allchanges')
402 // Get all the possible dates
403 // Note that we are keeping track of real (GMT) time and user time
404 // User time is only used in displays - all calcs and passing is GMT
406 $timenow = time(); // GMT
408 // What day is it now for the user, and when is midnight that day (in GMT).
409 $timemidnight = $today = usergetmidnight($timenow);
411 // Put today up the top of the list
412 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
414 if (!$course->startdate
or ($course->startdate
> $timenow)) {
415 $course->startdate
= $course->timecreated
;
419 while ($timemidnight > $course->startdate
and $numdates < 365) {
420 $timemidnight = $timemidnight - 86400;
421 $timenow = $timenow - 86400;
422 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
426 if ($selecteddate == "today") {
427 $selecteddate = $today;
430 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/course/report/log/index.php\" method=\"get\">\n";
432 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
433 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
434 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
435 if (has_capability('moodle/site:viewreports', $sitecontext) && $showcourses) {
436 choose_from_menu ($courses, "id", $course->id
, "");
438 // echo '<input type="hidden" name="id" value="'.$course->id.'" />';
440 $courses[$course->id
] = $course->fullname
. (($course->id
== SITEID
) ?
' ('.get_string('site').') ' : '');
441 choose_from_menu($courses,"id",$course->id
,false);
442 if (has_capability('moodle/site:viewreports', $sitecontext)) {
444 $a->url
= "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
445 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers";
446 print_string('logtoomanycourses','moodle',$a);
451 if ($cgroups = get_groups($course->id
)) {
452 foreach ($cgroups as $cgroup) {
453 $groups[$cgroup->id
] = $cgroup->name
;
459 choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") );
463 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
467 if (!empty($selecteduser)) {
468 $user = get_record('user','id',$selecteduser);
469 $users[$selecteduser] = fullname($user);
472 $users[0] = get_string('allparticipants');
474 choose_from_menu($users, 'user', $selecteduser, false);
476 $a->url
= "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
477 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses";
478 print_string('logtoomanyusers','moodle',$a);
480 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
481 choose_from_menu ($activities, "modid", $selectedactivity, get_string("allactivities"), "", "");
482 choose_from_menu ($actions, 'modaction', $modaction, get_string("allactions"));
484 $logformats = array('showashtml' => get_string('displayonpage'),
485 'downloadascsv' => get_string('downloadtext'),
486 'downloadasods' => get_string('downloadods'),
487 'downloadasexcel' => get_string('downloadexcel'));
488 choose_from_menu ($logformats, 'logformat', $logformat, false);
489 echo '<input type="submit" value="'.get_string('gettheselogs').'" />';