merged fix for 5493
[moodle.git] / course / report / log / lib.php
CommitLineData
1f411afd 1<?php // $Id$
0a935fb3 2
3function print_log_selector_form($course, $selecteduser=0, $selecteddate="today",
4 $modname="", $modid=0, $modaction="", $selectedgroup=-1,$showcourses=0,$showusers=0) {
5
6 global $USER, $CFG;
7
8 // first check to see if we can override showcourses and showusers
9 $numcourses = count_records_select("course", "", "COUNT(id)");
10 if ($numcourses < COURSE_MAX_COURSES_PER_DROPDOWN && !$showcourses) {
11 $showcourses = 1;
12 }
13
14 /// Setup for group handling.
15 $isteacher = isteacher($course->id);
16 $isteacheredit = isteacheredit($course->id);
17 if ($course->groupmode == SEPARATEGROUPS and !$isteacheredit) {
18 $selectedgroup = get_current_group($course->id);
19 $showgroups = false;
20 }
21 else if ($course->groupmode) {
22 $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup;
23 $showgroups = true;
24 }
25 else {
26 $selectedgroup = 0;
27 $showgroups = false;
28 }
29
30 // Get all the possible users
31 $users = array();
32
33 if ($course->category) {
34 if ($selectedgroup) { // If using a group, only get users in that group.
35 $courseusers = get_group_users($selectedgroup, 'u.lastname ASC', '', 'u.id, u.firstname, u.lastname, u.idnumber');
36 } else {
37 $courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname, u.idnumber');
38 }
39 } else {
40 $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname, u.idnumber");
41 }
42
43 if (count($courseusers) < COURSE_MAX_USERS_PER_DROPDOWN && !$showusers) {
44 $showusers = 1;
45 }
46
47 if ($showusers) {
48 if ($courseusers) {
49 foreach ($courseusers as $courseuser) {
50 $users[$courseuser->id] = fullname($courseuser, $isteacher);
51 }
52 }
53 if ($guest = get_guest()) {
54 $users[$guest->id] = fullname($guest);
55 }
56 }
57
58 if (isadmin() && $showcourses) {
59 if ($ccc = get_records("course", "", "", "fullname","id,fullname,category")) {
60 foreach ($ccc as $cc) {
61 if ($cc->category) {
62 $courses["$cc->id"] = "$cc->fullname";
63 } else {
64 $courses["$cc->id"] = " $cc->fullname (Site)";
65 }
66 }
67 }
68 asort($courses);
69 }
70
71 $activities = array();
72 $selectedactivity = "";
73
74 if ($modinfo = unserialize($course->modinfo)) {
75 $section = 0;
76 if ($course->format == 'weeks') { // Bodgy
77 $strsection = get_string("week");
78 } else {
79 $strsection = get_string("topic");
80 }
81 foreach ($modinfo as $mod) {
82 if ($mod->mod == "label") {
83 continue;
84 }
85 if ($mod->section > 0 and $section <> $mod->section) {
86 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
87 }
88 $section = $mod->section;
89 $mod->name = strip_tags(format_string(urldecode($mod->name),true));
90 if (strlen($mod->name) > 55) {
91 $mod->name = substr($mod->name, 0, 50)."...";
92 }
93 if (!$mod->visible) {
94 $mod->name = "(".$mod->name.")";
95 }
96 $activities["$mod->cm"] = $mod->name;
97
98 if ($mod->cm == $modid) {
99 $selectedactivity = "$mod->cm";
100 }
101 }
102 }
103
104 if (isadmin() && !$course->category) {
105 $activities["site_errors"] = get_string("siteerrors");
106 if ($modid === "site_errors") {
107 $selectedactivity = "site_errors";
108 }
109 }
110
111 $strftimedate = get_string("strftimedate");
112 $strftimedaydate = get_string("strftimedaydate");
113
114 asort($users);
115
ee35e0b8 116 // Prepare the list of action options.
117 $actions = array(
118 'view' => get_string('view'),
119 'add' => get_string('add'),
120 'update' => get_string('update'),
121 'delete' => get_string('delete'),
122 '-view' => get_string('allchanges')
123 );
124
0a935fb3 125 // Get all the possible dates
126 // Note that we are keeping track of real (GMT) time and user time
127 // User time is only used in displays - all calcs and passing is GMT
128
129 $timenow = time(); // GMT
130
131 // What day is it now for the user, and when is midnight that day (in GMT).
132 $timemidnight = $today = usergetmidnight($timenow);
133
134 // Put today up the top of the list
135 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
136
137 if (!$course->startdate or ($course->startdate > $timenow)) {
138 $course->startdate = $course->timecreated;
139 }
140
141 $numdates = 1;
142 while ($timemidnight > $course->startdate and $numdates < 365) {
143 $timemidnight = $timemidnight - 86400;
144 $timenow = $timenow - 86400;
145 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
146 $numdates++;
147 }
148
149 if ($selecteddate == "today") {
150 $selecteddate = $today;
151 }
152
153 echo "<center>\n";
4d70ff5c 154 echo "<form action=\"$CFG->wwwroot/course/report/log/index.php\" method=\"get\">\n";
0a935fb3 155 echo "<input type=\"hidden\" name=\"chooselog\" value=\"1\" />\n";
156 echo "<input type=\"hidden\" name=\"showusers\" value=\"$showusers\" />\n";
157 echo "<input type=\"hidden\" name=\"showcourses\" value=\"$showcourses\" />\n";
158 if (isadmin() && $showcourses) {
159 choose_from_menu ($courses, "id", $course->id, "");
160 } else {
161 // echo '<input type="hidden" name="id" value="'.$course->id.'" />';
162 $courses = array();
163 $courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' (Site) ' : '');
164 choose_from_menu($courses,"id",$course->id,false);
165 if (isadmin()) {
4d70ff5c 166 $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
0a935fb3 167 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showcourses=1&showusers=$showusers";
168 print_string('logtoomanycourses','moodle',$a);
169 }
170 }
171
172 if ($showgroups) {
173 if ($cgroups = get_groups($course->id)) {
174 foreach ($cgroups as $cgroup) {
175 $groups[$cgroup->id] = $cgroup->name;
176 }
177 }
178 else {
179 $groups = array();
180 }
181 choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") );
182 }
183
184 if ($showusers) {
185 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
186 }
187 else {
188 $users = array();
189 if (!empty($selecteduser)) {
190 $user = get_record('user','id',$selecteduser);
191 $users[$selecteduser] = fullname($user);
192 }
193 else {
194 $users[0] = get_string('allparticipants');
195 }
196 choose_from_menu($users,'user',$selecteduser,false);
4d70ff5c 197 $a->url = "$CFG->wwwroot/course/report/log/index.php?chooselog=0&group=$selectedgroup&user=$selecteduser"
0a935fb3 198 ."&id=$course->id&date=$selecteddate&modid=$selectedactivity&showusers=1&showcourses=$showcourses";
199 print_string('logtoomanyusers','moodle',$a);
200 }
201 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
202 choose_from_menu ($activities, "modid", $selectedactivity, get_string("allactivities"), "", "");
ee35e0b8 203 choose_from_menu ($actions, 'modaction', $modaction, get_string("allactions"));
0a935fb3 204 echo '<input type="submit" value="'.get_string('showtheselogs').'" />';
205 echo "</form>";
206 echo "</center>";
207}
208
209?>