adding some strings
[moodle-linuxchix.git] / course / report / log / lib.php
blob132de869679f2bff18d5233a16b195b6517a1312
1 <?php // $Id$
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) {
19 $showcourses = 1;
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);
32 $showgroups = false;
34 else if ($course->groupmode) {
35 $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup;
36 $showgroups = true;
38 else {
39 $selectedgroup = 0;
40 $showgroups = false;
43 } else {
44 $context = $sitecontext;
47 // Get all the possible users
48 $users = array();
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');
54 } else {
55 $courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname, u.idnumber');
57 } else {
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) {
62 $showusers = 1;
65 if ($showusers) {
66 if ($courseusers) {
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
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";
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;
95 asort($hostarray);
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->id == SITEID) {
105 $sites["$hostid/$cc->id"] = format_string($cc->fullname).' ('.get_string('site').')';
106 } else {
107 $courses["$hostid/$cc->id"] = format_string($cc->fullname);
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) { // TODO: this might be wrong - site course may have another id
118 $sites["$hostid/$cc->course"] = $cc->coursename.' ('.get_string('site').')';
119 } else {
120 $courses["$hostid/$cc->course"] = $cc->coursename;
127 asort($courses);
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)) {
137 $section = 0;
138 if ($course->format == 'weeks') { // Bodgy
139 $strsection = get_string("week");
140 } else {
141 $strsection = get_string("topic");
143 foreach ($modinfo as $mod) {
144 if ($mod->mod == "label") {
145 continue;
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");
176 asort($users);
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')
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;
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++;
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, "");
223 } else {
224 $courses = array();
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)) {
228 $a = new object();
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);
235 if ($showgroups) {
236 if ($cgroups = get_groups($course->id)) {
237 foreach ($cgroups as $cgroup) {
238 $groups[$cgroup->id] = $cgroup->name;
241 else {
242 $groups = array();
244 choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") );
247 if ($showusers) {
248 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
250 else {
251 $users = array();
252 if (!empty($selecteduser)) {
253 $user = get_record('user','id',$selecteduser);
254 $users[$selecteduser] = fullname($user);
256 else {
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').'" />';
274 echo '</div>';
275 echo '</form>';
278 function print_log_selector_form($course, $selecteduser=0, $selecteddate='today',
279 $modname="", $modid=0, $modaction='', $selectedgroup=-1, $showcourses=0, $showusers=0, $logformat='showashtml') {
281 global $USER, $CFG;
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) {
286 $showcourses = 1;
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);
295 $showgroups = false;
297 else if ($course->groupmode) {
298 $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup;
299 $showgroups = true;
301 else {
302 $selectedgroup = 0;
303 $showgroups = false;
306 // Get all the possible users
307 $users = array();
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');
312 } else {
313 $courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname, u.idnumber');
315 } else {
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) {
320 $showusers = 1;
323 if ($showusers) {
324 if ($courseusers) {
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) {
337 if ($cc->category) {
338 $courses["$cc->id"] = format_string($cc->fullname);
339 } else {
340 $courses["$cc->id"] = format_string($cc->fullname) . ' (Site)';
344 asort($courses);
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)) {
352 $section = 0;
353 if ($course->format == 'weeks') { // Bodgy
354 $strsection = get_string("week");
355 } else {
356 $strsection = get_string("topic");
358 foreach ($modinfo as $mod) {
359 if ($mod->mod == "label") {
360 continue;
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");
391 asort($users);
393 // Prepare the list of action options.
394 $actions = array(
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;
418 $numdates = 1;
419 while ($timemidnight > $course->startdate and $numdates < 365) {
420 $timemidnight = $timemidnight - 86400;
421 $timenow = $timenow - 86400;
422 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
423 $numdates++;
426 if ($selecteddate == "today") {
427 $selecteddate = $today;
430 echo "<form class=\"logselectform\" action=\"$CFG->wwwroot/course/report/log/index.php\" method=\"get\">\n";
431 echo "<div>\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, "");
437 } else {
438 // echo '<input type="hidden" name="id" value="'.$course->id.'" />';
439 $courses = array();
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)) {
443 $a = new object();
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);
450 if ($showgroups) {
451 if ($cgroups = get_groups($course->id)) {
452 foreach ($cgroups as $cgroup) {
453 $groups[$cgroup->id] = $cgroup->name;
456 else {
457 $groups = array();
459 choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") );
462 if ($showusers) {
463 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
465 else {
466 $users = array();
467 if (!empty($selecteduser)) {
468 $user = get_record('user','id',$selecteduser);
469 $users[$selecteduser] = fullname($user);
471 else {
472 $users[0] = get_string('allparticipants');
474 choose_from_menu($users, 'user', $selecteduser, false);
475 $a = new object();
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').'" />';
490 echo '</div>';
491 echo '</form>';