3 // Display user activity reports for a course
5 require_once("../config.php");
6 require_once("lib.php");
8 $id = required_param('id',PARAM_INT
); // course id
9 $user = required_param('user',PARAM_INT
); // user id
10 $mode = optional_param('mode', "todaylogs", PARAM_ALPHA
);
11 $page = optional_param('page', 0, PARAM_INT
);
12 $perpage = optional_param('perpage', 100, PARAM_INT
);
14 if (! $course = get_record("course", "id", $id)) {
15 error("Course id is incorrect.");
18 if (! $user = get_record("user", "id", $user)) {
19 error("User ID is incorrect");
22 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
23 $personalcontext = get_context_instance(CONTEXT_USER
, $user->id
);
26 if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and !has_capability('moodle/course:view', $coursecontext)) {
27 // do not require parents to be enrolled in courses ;-)
28 course_setup($course);
30 require_login($course);
35 print_heading(get_string('userdeleted'));
40 // prepare list of allowed modes
41 $myreports = ($course->showreports
and $USER->id
== $user->id
);
42 $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
46 if ($myreports or $anyreport or has_capability('coursereport/outline:view', $coursecontext)) {
50 if ($myreports or $anyreport or has_capability('coursereport/outline:view', $coursecontext)) {
51 $modes[] = 'complete';
54 if ($myreports or $anyreport or has_capability('coursereport/log:viewtoday', $coursecontext)) {
55 $modes[] = 'todaylogs';
58 if ($myreports or $anyreport or has_capability('coursereport/log:view', $coursecontext)) {
62 if ($myreports or $anyreport or has_capability('coursereport/stats:view', $coursecontext)) {
66 if (has_capability('moodle/grade:viewall', $coursecontext)) {
67 //ok - can view all course grades
70 } else if ($course->showgrades
and $user->id
== $USER->id
and has_capability('moodle/grade:view', $coursecontext)) {
71 //ok - can view own grades
74 } else if ($course->showgrades
and has_capability('moodle/grade:viewall', $personalcontext)) {
75 // ok - can view grades of this user - parent most probably
78 } else if ($course->showgrades
and $anyreport) {
79 // ok - can view grades of this user - parent most probably
84 require_capability('moodle/user:viewuseractivitiesreport', $personalcontext);
87 if (!in_array($mode, $modes)) {
88 // forbidden or non-exitent mode
89 $mode = reset($modes);
92 add_to_log($course->id
, "course", "user report", "user.php?id=$course->id&user=$user->id&mode=$mode", "$user->id");
94 $stractivityreport = get_string("activityreport");
95 $strparticipants = get_string("participants");
96 $stroutline = get_string("outline");
97 $strcomplete = get_string("complete");
98 $stralllogs = get_string("alllogs");
99 $strtodaylogs = get_string("todaylogs");
100 $strmode = get_string($mode);
101 $fullname = fullname($user, true);
105 if ($course->id
!= SITEID
&& has_capability('moodle/course:viewparticipants', $coursecontext)) {
106 $navlinks[] = array('name' => $strparticipants, 'link' => "../user/index.php?id=$course->id", 'type' => 'misc');
109 $navlinks[] = array('name' => $fullname, 'link' => "../user/view.php?id=$user->id&course=$course->id", 'type' => 'misc');
110 $navlinks[] = array('name' => $stractivityreport, 'link' => null, 'type' => 'misc');
111 $navlinks[] = array('name' => $strmode, 'link' => null, 'type' => 'misc');
112 $navigation = build_navigation($navlinks);
114 print_header("$course->shortname: $stractivityreport ($mode)", $course->fullname
, $navigation);
117 /// Print tabs at top
118 /// This same call is made in:
124 include($CFG->dirroot
.'/user/tabs.php');
128 if (empty($CFG->grade_profilereport
) or !file_exists($CFG->dirroot
.'/grade/report/'.$CFG->grade_profilereport
.'/lib.php')) {
129 $CFG->grade_profilereport
= 'user';
131 require_once $CFG->libdir
.'/gradelib.php';
132 require_once $CFG->dirroot
.'/grade/lib.php';
133 require_once $CFG->dirroot
.'/grade/report/'.$CFG->grade_profilereport
.'/lib.php';
135 $functionname = 'grade_report_'.$CFG->grade_profilereport
.'_profilereport';
136 if (function_exists($functionname)) {
137 $functionname($course, $user);
142 echo '<div class="graph">';
143 print_log_graph($course, $user->id
, "userday.png");
145 print_log($course, $user->id
, usergetmidnight(time()), "l.time DESC", $page, $perpage,
146 "user.php?id=$course->id&user=$user->id&mode=$mode");
150 echo '<div class="graph">';
151 print_log_graph($course, $user->id
, "usercourse.png");
153 print_log($course, $user->id
, 0, "l.time DESC", $page, $perpage,
154 "user.php?id=$course->id&user=$user->id&mode=$mode");
158 if (empty($CFG->enablestats
)) {
159 error("Stats is not enabled.");
162 require_once($CFG->dirroot
.'/lib/statslib.php');
164 $statsstatus = stats_check_uptodate($course->id
);
165 if ($statsstatus !== NULL) {
166 notify ($statsstatus);
169 $earliestday = get_field_sql('SELECT timeend FROM '.$CFG->prefix
.'stats_user_daily ORDER BY timeend');
170 $earliestweek = get_field_sql('SELECT timeend FROM '.$CFG->prefix
.'stats_user_weekly ORDER BY timeend');
171 $earliestmonth = get_field_sql('SELECT timeend FROM '.$CFG->prefix
.'stats_user_monthly ORDER BY timeend');
173 if (empty($earliestday)) $earliestday = time();
174 if (empty($earliestweek)) $earliestweek = time();
175 if (empty($earliestmonth)) $earliestmonth = time();
177 $now = stats_get_base_daily();
178 $lastweekend = stats_get_base_weekly();
179 $lastmonthend = stats_get_base_monthly();
181 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
183 if (empty($timeoptions)) {
184 print_error('nostatstodisplay', '', $CFG->wwwroot
.'/course/user.php?id='.$course->id
.'&user='.$user->id
.'&mode=outline');
188 $time = array_pop(array_keys($timeoptions));
190 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW
,$course->id
,STATS_MODE_DETAILED
);
192 $param->table
= 'user_'.$param->table
;
194 $sql = 'SELECT timeend,'.$param->fields
.' FROM '.$CFG->prefix
.'stats_'.$param->table
.' WHERE '
195 .(($course->id
== SITEID
) ?
'' : ' courseid = '.$course->id
.' AND ')
196 .' userid = '.$user->id
197 .' AND timeend >= '.$param->timeafter
199 .' ORDER BY timeend DESC';
200 $stats = get_records_sql($sql);
203 print_error('nostatstodisplay', '', $CFG->wwwroot
.'/course/user.php?id='.$course->id
.'&user='.$user->id
.'&mode=outline');
206 // MDL-10818, do not display broken graph when user has no permission to view graph
207 if ($myreports or has_capability('coursereport/stats:view', $coursecontext)) {
208 echo '<center><img src="'.$CFG->wwwroot
.'/course/report/stats/graph.php?mode='.STATS_MODE_DETAILED
.'&course='.$course->id
.'&time='.$time.'&report='.STATS_REPORT_USER_VIEW
.'&userid='.$user->id
.'" alt="'.get_string('statisticsgraph').'" /></center>';
211 // What the heck is this about? -- MD
212 $stats = stats_fix_zeros($stats,$param->timeafter
,$param->table
,(!empty($param->line2
)),(!empty($param->line3
)));
214 $table = new object();
215 $table->align
= array('left','center','center','center');
216 $param->table
= str_replace('user_','',$param->table
);
217 switch ($param->table
) {
218 case 'daily' : $period = get_string('day'); break;
219 case 'weekly' : $period = get_string('week'); break;
220 case 'monthly': $period = get_string('month', 'form'); break;
221 default : $period = '';
223 $table->head
= array(get_string('periodending','moodle',$period),$param->line1
,$param->line2
,$param->line3
);
224 foreach ($stats as $stat) {
225 if (!empty($stat->zerofixed
)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
228 $a = array(userdate($stat->timeend
,get_string('strftimedate'),$CFG->timezone
),$stat->line1
);
238 get_all_mods($course->id
, $mods, $modnames, $modnamesplural, $modnamesused);
239 $sections = get_all_sections($course->id
);
241 for ($i=0; $i<=$course->numsections
; $i++
) {
243 if (isset($sections[$i])) { // should always be true
245 $section = $sections[$i];
246 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible
or !$course->hiddensections
);
248 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
250 if ($section->sequence
) {
251 echo '<div class="section">';
253 switch ($course->format
) {
254 case "weeks": print_string("week"); break;
255 case "topics": print_string("topic"); break;
256 default: print_string("section"); break;
260 echo '<div class="content">';
262 if ($mode == "outline") {
263 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
266 $sectionmods = explode(",", $section->sequence
);
267 foreach ($sectionmods as $sectionmod) {
268 if (empty($mods[$sectionmod])) {
271 $mod = $mods[$sectionmod];
273 if (empty($mod->visible
)) {
277 $instance = get_record("$mod->modname", "id", "$mod->instance");
278 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
280 if (file_exists($libfile)) {
281 require_once($libfile);
285 $user_outline = $mod->modname
."_user_outline";
286 if (function_exists($user_outline)) {
287 $output = $user_outline($course, $user, $mod, $instance);
288 print_outline_row($mod, $instance, $output);
292 $user_complete = $mod->modname
."_user_complete";
293 if (function_exists($user_complete)) {
294 $image = "<img src=\"../mod/$mod->modname/icon.gif\" ".
295 "class=\"icon\" alt=\"$mod->modfullname\" />";
296 echo "<h4>$image $mod->modfullname: ".
297 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
298 format_string($instance->name
,true)."</a></h4>";
303 $user_complete($course, $user, $mod, $instance);
306 $output = ob_get_contents();
309 if (str_replace(' ', '', $output) != '<ul></ul>') {
318 if ($mode == "outline") {
321 echo '</div>'; // content
322 echo '</div>'; // section
329 // can not be reached ;-)
333 print_footer($course);
336 function print_outline_row($mod, $instance, $result) {
339 $image = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\" class=\"icon\" alt=\"$mod->modfullname\" />";
342 echo "<td valign=\"top\">$image</td>";
343 echo "<td valign=\"top\" style=\"width:300\">";
344 echo " <a title=\"$mod->modfullname\"";
345 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name
,true)."</a></td>";
346 echo "<td> </td>";
347 echo "<td valign=\"top\">";
348 if (isset($result->info
)) {
349 echo "$result->info";
351 echo "<p style=\"text-align:center\">-</p>";
354 echo "<td> </td>";
355 if (!empty($result->time
)) {
356 $timeago = format_time(time() - $result->time
);
357 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time
)." ($timeago)</td>";