MDL-11515:
[moodle-linuxchix.git] / course / user.php
bloba28f487dbf34b1d74658ec4fb190bb98fbf4a8fd
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($course);
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();
47 if ($course->id != SITEID) {
48 $navlinks[] = array('name' => $strparticipants, 'link' => "../user/index.php?id=$course->id", 'type' => 'misc');
51 $navlinks[] = array('name' => $fullname, 'link' => "../user/view.php?id=$user->id&amp;course=$course->id", 'type' => 'misc');
52 $navlinks[] = array('name' => $stractivityreport, 'link' => null, 'type' => 'misc');
53 $navlinks[] = array('name' => $strmode, 'link' => null, 'type' => 'misc');
54 $navigation = build_navigation($navlinks);
56 print_header("$course->shortname: $stractivityreport ($mode)", $course->fullname, $navigation);
59 /// Print tabs at top
60 /// This same call is made in:
61 /// /user/view.php
62 /// /user/edit.php
63 /// /course/user.php
64 $currenttab = $mode;
65 $showroles = 1;
66 include($CFG->dirroot.'/user/tabs.php');
68 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
70 switch ($mode) {
71 case "grade":
72 $course = get_record('course', 'id', required_param('id', PARAM_INT));
73 if (!empty($course->showgrades)) {
74 require_once $CFG->dirroot.'/grade/lib.php';
75 require_once $CFG->dirroot.'/grade/report/user/lib.php';
76 $context = get_context_instance(CONTEXT_COURSE, $id);
77 /// return tracking object
78 $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$id, 'userid'=>$user->id));
79 // Create a report instance
80 $report = new grade_report_user($id, $gpr, $context, $user->id);
82 $gradetotal = 0;
83 $gradesum = 0;
85 // print the page
86 print_heading(get_string('modulename', 'gradereport_user'). ' - '.fullname($report->user));
88 if ($report->fill_table()) {
89 echo $report->print_table(true);
91 // print_student_grade($user, $course);
93 break;
95 case "todaylogs" :
96 echo '<div class="graph">';
97 print_log_graph($course, $user->id, "userday.png");
98 echo '</div>';
99 print_log($course, $user->id, usergetmidnight(time()), "l.time DESC", $page, $perpage,
100 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
101 break;
103 case "alllogs" :
104 echo '<div class="graph">';
105 print_log_graph($course, $user->id, "usercourse.png");
106 echo '</div>';
107 print_log($course, $user->id, 0, "l.time DESC", $page, $perpage,
108 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
109 break;
110 case 'stats':
112 if (empty($CFG->enablestats)) {
113 error("Stats is not enabled.");
116 require_once($CFG->dirroot.'/lib/statslib.php');
118 $statsstatus = stats_check_uptodate($course->id);
119 if ($statsstatus !== NULL) {
120 notify ($statsstatus);
123 $earliestday = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_daily ORDER BY timeend');
124 $earliestweek = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_weekly ORDER BY timeend');
125 $earliestmonth = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_monthly ORDER BY timeend');
127 if (empty($earliestday)) $earliestday = time();
128 if (empty($earliestweek)) $earliestweek = time();
129 if (empty($earliestmonth)) $earliestmonth = time();
131 $now = stats_get_base_daily();
132 $lastweekend = stats_get_base_weekly();
133 $lastmonthend = stats_get_base_monthly();
135 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
137 if (empty($timeoptions)) {
138 error(get_string('nostatstodisplay'), $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
141 // use the earliest.
142 $time = array_pop(array_keys($timeoptions));
144 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
146 $param->table = 'user_'.$param->table;
148 $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
149 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
150 .' userid = '.$user->id
151 .' AND timeend >= '.$param->timeafter
152 .$param->extras
153 .' ORDER BY timeend DESC';
154 $stats = get_records_sql($sql);
156 if (empty($stats)) {
157 error(get_string('nostatstodisplay'), $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
160 // MDL-10818, do not display broken graph when user has no permission to view graph
161 if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_COURSE, $id)) ||
162 ($course->showreports and $USER->id == $user->id)) {
163 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>';
166 // What the heck is this about? -- MD
167 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
169 $table = new object();
170 $table->align = array('left','center','center','center');
171 $param->table = str_replace('user_','',$param->table);
172 $table->head = array(get_string('periodending','moodle',$param->table),$param->line1,$param->line2,$param->line3);
173 foreach ($stats as $stat) {
174 if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
175 continue;
177 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
178 $a[] = $stat->line2;
179 $a[] = $stat->line3;
180 $table->data[] = $a;
182 print_table($table);
183 break;
184 case "outline" :
185 case "complete" :
186 default:
187 $sections = get_all_sections($course->id);
189 for ($i=0; $i<=$course->numsections; $i++) {
191 if (isset($sections[$i])) { // should always be true
193 $section = $sections[$i];
194 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible or !$course->hiddensections);
196 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
198 if ($section->sequence) {
199 echo '<div class="section">';
200 echo '<h2>';
201 switch ($course->format) {
202 case "weeks": print_string("week"); break;
203 case "topics": print_string("topic"); break;
204 default: print_string("section"); break;
206 echo " $i</h2>";
208 echo '<div class="content">';
210 if ($mode == "outline") {
211 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
214 $sectionmods = explode(",", $section->sequence);
215 foreach ($sectionmods as $sectionmod) {
216 if (empty($mods[$sectionmod])) {
217 continue;
219 $mod = $mods[$sectionmod];
221 if (empty($mod->visible)) {
222 continue;
225 $instance = get_record("$mod->modname", "id", "$mod->instance");
226 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
228 if (file_exists($libfile)) {
229 require_once($libfile);
231 switch ($mode) {
232 case "outline":
233 $user_outline = $mod->modname."_user_outline";
234 if (function_exists($user_outline)) {
235 $output = $user_outline($course, $user, $mod, $instance);
236 print_outline_row($mod, $instance, $output);
238 break;
239 case "complete":
240 $user_complete = $mod->modname."_user_complete";
241 if (function_exists($user_complete)) {
242 $image = "<img src=\"../mod/$mod->modname/icon.gif\" ".
243 "class=\"icon\" alt=\"$mod->modfullname\" />";
244 echo "<h4>$image $mod->modfullname: ".
245 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
246 format_string($instance->name,true)."</a></h4>";
248 ob_start();
250 echo "<ul>";
251 $user_complete($course, $user, $mod, $instance);
252 echo "</ul>";
254 $output = ob_get_contents();
255 ob_end_clean();
257 if (str_replace(' ', '', $output) != '<ul></ul>') {
258 echo $output;
261 break;
266 if ($mode == "outline") {
267 echo "</table>";
269 echo '</div>'; // content
270 echo '</div>'; // section
275 break;
279 print_footer($course);
282 function print_outline_row($mod, $instance, $result) {
283 global $CFG;
285 $image = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\" class=\"icon\" alt=\"$mod->modfullname\" />";
287 echo "<tr>";
288 echo "<td valign=\"top\">$image</td>";
289 echo "<td valign=\"top\" style=\"width:300\">";
290 echo " <a title=\"$mod->modfullname\"";
291 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
292 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
293 echo "<td valign=\"top\">";
294 if (isset($result->info)) {
295 echo "$result->info";
296 } else {
297 echo "<p style=\"text-align:center\">-</p>";
299 echo "</td>";
300 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
301 if (!empty($result->time)) {
302 $timeago = format_time(time() - $result->time);
303 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
305 echo "</tr>";