Updated the 19 build version to 20081120
[moodle.git] / course / user.php
blob70ab61b8c542898ce3a9168090ea6b1f6c1467bb
1 <?php // $Id$
3 // Display user activity reports for a course
5 require_once("../config.php");
6 require_once("lib.php");
8 $modes = array("outline", "complete", "todaylogs", "alllogs");
10 $id = required_param('id',PARAM_INT); // course id
11 $user = required_param('user',PARAM_INT); // user id
12 $mode = optional_param('mode', "todaylogs", PARAM_ALPHA);
13 $page = optional_param('page', 0, PARAM_INT);
14 $perpage = optional_param('perpage', 100, PARAM_INT);
16 if (! $course = get_record("course", "id", $id)) {
17 error("Course id is incorrect.");
20 if (! $user = get_record("user", "id", $user)) {
21 error("User ID is incorrect");
24 require_login();
25 $COURSE = clone($course);
27 if ($user->deleted) {
28 print_header();
29 print_heading(get_string('userdeleted'));
30 print_footer();
31 die;
34 $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
35 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
37 // if in either context, we can read report, then we can proceed
38 if (!(has_capability('moodle/site:viewreports', $coursecontext) or ($course->showreports and $USER->id == $user->id) or has_capability('moodle/user:viewuseractivitiesreport', $personalcontext))) {
39 error("You are not allowed to look at this page");
42 add_to_log($course->id, "course", "user report", "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode", "$user->id");
44 $stractivityreport = get_string("activityreport");
45 $strparticipants = get_string("participants");
46 $stroutline = get_string("outline");
47 $strcomplete = get_string("complete");
48 $stralllogs = get_string("alllogs");
49 $strtodaylogs = get_string("todaylogs");
50 $strmode = get_string($mode);
51 $fullname = fullname($user, true);
53 $navlinks = array();
55 if ($course->id != SITEID && has_capability('moodle/course:viewparticipants', $coursecontext)) {
56 $navlinks[] = array('name' => $strparticipants, 'link' => "../user/index.php?id=$course->id", 'type' => 'misc');
59 $navlinks[] = array('name' => $fullname, 'link' => "../user/view.php?id=$user->id&amp;course=$course->id", 'type' => 'misc');
60 $navlinks[] = array('name' => $stractivityreport, 'link' => null, 'type' => 'misc');
61 $navlinks[] = array('name' => $strmode, 'link' => null, 'type' => 'misc');
62 $navigation = build_navigation($navlinks);
64 print_header("$course->shortname: $stractivityreport ($mode)", $course->fullname, $navigation);
67 /// Print tabs at top
68 /// This same call is made in:
69 /// /user/view.php
70 /// /user/edit.php
71 /// /course/user.php
72 $currenttab = $mode;
73 $showroles = 1;
74 include($CFG->dirroot.'/user/tabs.php');
76 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
78 switch ($mode) {
79 case "grade":
80 if (empty($CFG->grade_profilereport) or !file_exists($CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php')) {
81 $CFG->grade_profilereport = 'user';
83 require_once $CFG->libdir.'/gradelib.php';
84 require_once $CFG->dirroot.'/grade/lib.php';
85 require_once $CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php';
87 $course = get_record('course', 'id', required_param('id', PARAM_INT));
88 $functionname = 'grade_report_'.$CFG->grade_profilereport.'_profilereport';
89 if (function_exists($functionname)) {
90 $functionname($course, $user);
92 break;
94 case "todaylogs" :
95 echo '<div class="graph">';
96 print_log_graph($course, $user->id, "userday.png");
97 echo '</div>';
98 print_log($course, $user->id, usergetmidnight(time()), "l.time DESC", $page, $perpage,
99 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
100 break;
102 case "alllogs" :
103 echo '<div class="graph">';
104 print_log_graph($course, $user->id, "usercourse.png");
105 echo '</div>';
106 print_log($course, $user->id, 0, "l.time DESC", $page, $perpage,
107 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
108 break;
109 case 'stats':
111 if (empty($CFG->enablestats)) {
112 error("Stats is not enabled.");
115 require_once($CFG->dirroot.'/lib/statslib.php');
117 $statsstatus = stats_check_uptodate($course->id);
118 if ($statsstatus !== NULL) {
119 notify ($statsstatus);
122 $earliestday = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_daily ORDER BY timeend');
123 $earliestweek = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_weekly ORDER BY timeend');
124 $earliestmonth = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_monthly ORDER BY timeend');
126 if (empty($earliestday)) $earliestday = time();
127 if (empty($earliestweek)) $earliestweek = time();
128 if (empty($earliestmonth)) $earliestmonth = time();
130 $now = stats_get_base_daily();
131 $lastweekend = stats_get_base_weekly();
132 $lastmonthend = stats_get_base_monthly();
134 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
136 if (empty($timeoptions)) {
137 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
140 // use the earliest.
141 $time = array_pop(array_keys($timeoptions));
143 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
145 $param->table = 'user_'.$param->table;
147 $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
148 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
149 .' userid = '.$user->id
150 .' AND timeend >= '.$param->timeafter
151 .$param->extras
152 .' ORDER BY timeend DESC';
153 $stats = get_records_sql($sql);
155 if (empty($stats)) {
156 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
159 // MDL-10818, do not display broken graph when user has no permission to view graph
160 if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_COURSE, $id)) ||
161 ($course->showreports and $USER->id == $user->id)) {
162 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>';
165 // What the heck is this about? -- MD
166 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
168 $table = new object();
169 $table->align = array('left','center','center','center');
170 $param->table = str_replace('user_','',$param->table);
171 switch ($param->table) {
172 case 'daily' : $period = get_string('day'); break;
173 case 'weekly' : $period = get_string('week'); break;
174 case 'monthly': $period = get_string('month', 'form'); break;
175 default : $period = '';
177 $table->head = array(get_string('periodending','moodle',$period),$param->line1,$param->line2,$param->line3);
178 foreach ($stats as $stat) {
179 if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
180 continue;
182 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
183 $a[] = $stat->line2;
184 $a[] = $stat->line3;
185 $table->data[] = $a;
187 print_table($table);
188 break;
189 case "outline" :
190 case "complete" :
191 default:
192 $sections = get_all_sections($course->id);
194 for ($i=0; $i<=$course->numsections; $i++) {
196 if (isset($sections[$i])) { // should always be true
198 $section = $sections[$i];
199 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible or !$course->hiddensections);
201 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
203 if ($section->sequence) {
204 echo '<div class="section">';
205 echo '<h2>';
206 switch ($course->format) {
207 case "weeks": print_string("week"); break;
208 case "topics": print_string("topic"); break;
209 default: print_string("section"); break;
211 echo " $i</h2>";
213 echo '<div class="content">';
215 if ($mode == "outline") {
216 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
219 $sectionmods = explode(",", $section->sequence);
220 foreach ($sectionmods as $sectionmod) {
221 if (empty($mods[$sectionmod])) {
222 continue;
224 $mod = $mods[$sectionmod];
226 if (empty($mod->visible)) {
227 continue;
230 $instance = get_record("$mod->modname", "id", "$mod->instance");
231 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
233 if (file_exists($libfile)) {
234 require_once($libfile);
236 switch ($mode) {
237 case "outline":
238 $user_outline = $mod->modname."_user_outline";
239 if (function_exists($user_outline)) {
240 $output = $user_outline($course, $user, $mod, $instance);
241 print_outline_row($mod, $instance, $output);
243 break;
244 case "complete":
245 $user_complete = $mod->modname."_user_complete";
246 if (function_exists($user_complete)) {
247 $image = "<img src=\"../mod/$mod->modname/icon.gif\" ".
248 "class=\"icon\" alt=\"$mod->modfullname\" />";
249 echo "<h4>$image $mod->modfullname: ".
250 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
251 format_string($instance->name,true)."</a></h4>";
253 ob_start();
255 echo "<ul>";
256 $user_complete($course, $user, $mod, $instance);
257 echo "</ul>";
259 $output = ob_get_contents();
260 ob_end_clean();
262 if (str_replace(' ', '', $output) != '<ul></ul>') {
263 echo $output;
266 break;
271 if ($mode == "outline") {
272 echo "</table>";
274 echo '</div>'; // content
275 echo '</div>'; // section
280 break;
284 print_footer($course);
287 function print_outline_row($mod, $instance, $result) {
288 global $CFG;
290 $image = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\" class=\"icon\" alt=\"$mod->modfullname\" />";
292 echo "<tr>";
293 echo "<td valign=\"top\">$image</td>";
294 echo "<td valign=\"top\" style=\"width:300\">";
295 echo " <a title=\"$mod->modfullname\"";
296 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
297 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
298 echo "<td valign=\"top\">";
299 if (isset($result->info)) {
300 echo "$result->info";
301 } else {
302 echo "<p style=\"text-align:center\">-</p>";
304 echo "</td>";
305 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
306 if (!empty($result->time)) {
307 $timeago = format_time(time() - $result->time);
308 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
310 echo "</tr>";