calendar/lib: calendar_set_filters() use pre-fetched context and course recs
[moodle-pu.git] / course / user.php
blob4c270b1dc82d3527b28069d8a714daa48b07e2f6
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 require_login();
18 if (! $course = get_record("course", "id", $id)) {
19 error("Course id is incorrect.");
22 if (! $user = get_record("user", "id", $user)) {
23 error("User ID is incorrect");
26 $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
27 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
29 // if in either context, we can read report, then we can proceed
30 if (!(has_capability('moodle/site:viewreports', $coursecontext) or ($course->showreports and $USER->id == $user->id) or has_capability('moodle/user:viewuseractivitiesreport', $personalcontext))) {
31 error("You are not allowed to look at this page");
34 add_to_log($course->id, "course", "user report", "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode", "$user->id");
36 $stractivityreport = get_string("activityreport");
37 $strparticipants = get_string("participants");
38 $stroutline = get_string("outline");
39 $strcomplete = get_string("complete");
40 $stralllogs = get_string("alllogs");
41 $strtodaylogs = get_string("todaylogs");
42 $strmode = get_string($mode);
43 $fullname = fullname($user, true);
45 $navlinks = array();
46 if ($course->id != SITEID) {
47 $navlinks[] = array('name' => $strparticipants, 'link' => "../user/index.php?id=$course->id", 'type' => 'misc');
50 $navlinks[] = array('name' => $fullname, 'link' => "../user/view.php?id=$user->id&amp;course=$course->id", 'type' => 'misc');
51 $navlinks[] = array('name' => $stractivityreport, 'link' => null, 'type' => 'misc');
52 $navlinks[] = array('name' => $strmode, 'link' => null, 'type' => 'misc');
53 $navigation = build_navigation($navlinks);
55 print_header("$course->shortname: $stractivityreport ($mode)", $course->fullname, $navigation);
58 /// Print tabs at top
59 /// This same call is made in:
60 /// /user/view.php
61 /// /user/edit.php
62 /// /course/user.php
63 $currenttab = $mode;
64 $showroles = 1;
65 include($CFG->dirroot.'/user/tabs.php');
67 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
69 switch ($mode) {
70 case "grade":
71 $course = get_record('course', 'id', required_param('id', PARAM_INT));
72 if (!empty($course->showgrades)) {
73 require_once $CFG->dirroot.'/grade/lib.php';
74 require_once $CFG->dirroot.'/grade/report/user/lib.php';
75 $context = get_context_instance(CONTEXT_COURSE, $id);
76 /// return tracking object
77 $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$id, 'userid'=>$user->id));
78 // Create a report instance
79 $report = new grade_report_user($id, $gpr, $context, $user->id);
81 $gradetotal = 0;
82 $gradesum = 0;
84 // print the page
85 print_heading(get_string('modulename', 'gradereport_user'). ' - '.fullname($report->user));
87 if ($report->fill_table()) {
88 echo $report->print_table(true);
90 // print_student_grade($user, $course);
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 error(get_string('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 error(get_string('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 $table->head = array(get_string('periodending','moodle',$param->table),$param->line1,$param->line2,$param->line3);
172 foreach ($stats as $stat) {
173 if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
174 continue;
176 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
177 $a[] = $stat->line2;
178 $a[] = $stat->line3;
179 $table->data[] = $a;
181 print_table($table);
182 break;
183 case "outline" :
184 case "complete" :
185 default:
186 $sections = get_all_sections($course->id);
188 for ($i=0; $i<=$course->numsections; $i++) {
190 if (isset($sections[$i])) { // should always be true
192 $section = $sections[$i];
193 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible or !$course->hiddensections);
195 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
197 if ($section->sequence) {
198 echo '<div class="section">';
199 echo '<h2>';
200 switch ($course->format) {
201 case "weeks": print_string("week"); break;
202 case "topics": print_string("topic"); break;
203 default: print_string("section"); break;
205 echo " $i</h2>";
207 echo '<div class="content">';
209 if ($mode == "outline") {
210 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
213 $sectionmods = explode(",", $section->sequence);
214 foreach ($sectionmods as $sectionmod) {
215 if (empty($mods[$sectionmod])) {
216 continue;
218 $mod = $mods[$sectionmod];
220 if (empty($mod->visible)) {
221 continue;
224 $instance = get_record("$mod->modname", "id", "$mod->instance");
225 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
227 if (file_exists($libfile)) {
228 require_once($libfile);
230 switch ($mode) {
231 case "outline":
232 $user_outline = $mod->modname."_user_outline";
233 if (function_exists($user_outline)) {
234 $output = $user_outline($course, $user, $mod, $instance);
235 print_outline_row($mod, $instance, $output);
237 break;
238 case "complete":
239 $user_complete = $mod->modname."_user_complete";
240 if (function_exists($user_complete)) {
241 $image = "<img src=\"../mod/$mod->modname/icon.gif\" ".
242 "class=\"icon\" alt=\"$mod->modfullname\" />";
243 echo "<h4>$image $mod->modfullname: ".
244 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
245 format_string($instance->name,true)."</a></h4>";
247 ob_start();
249 echo "<ul>";
250 $user_complete($course, $user, $mod, $instance);
251 echo "</ul>";
253 $output = ob_get_contents();
254 ob_end_clean();
256 if (str_replace(' ', '', $output) != '<ul></ul>') {
257 echo $output;
260 break;
265 if ($mode == "outline") {
266 echo "</table>";
268 echo '</div>'; // content
269 echo '</div>'; // section
274 break;
278 print_footer($course);
281 function print_outline_row($mod, $instance, $result) {
282 global $CFG;
284 $image = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\" class=\"icon\" alt=\"$mod->modfullname\" />";
286 echo "<tr>";
287 echo "<td valign=\"top\">$image</td>";
288 echo "<td valign=\"top\" style=\"width:300\">";
289 echo " <a title=\"$mod->modfullname\"";
290 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
291 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
292 echo "<td valign=\"top\">";
293 if (isset($result->info)) {
294 echo "$result->info";
295 } else {
296 echo "<p style=\"text-align:center\">-</p>";
298 echo "</td>";
299 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
300 if (!empty($result->time)) {
301 $timeago = format_time(time() - $result->time);
302 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
304 echo "</tr>";