MDL-10364 Added the showcalculations preference and re-factored preferences_form...
[moodle-pu.git] / course / user.php
blob653573d3c70a6ae838ca2c2c559367d4c96a72a8
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 if ($course->id != SITEID) {
46 print_header("$course->shortname: $stractivityreport ($mode)", $course->fullname,
47 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
48 <a href=\"../user/index.php?id=$course->id\">$strparticipants</a> ->
49 <a href=\"../user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a> ->
50 $stractivityreport -> $strmode");
51 } else {
52 print_header("$course->shortname: $stractivityreport ($mode)", $course->fullname,
53 "<a href=\"../user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a> ->
54 $stractivityreport -> $strmode");
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 print_student_grade($user, $course);
76 break;
78 case "todaylogs" :
79 echo '<div class="graph">';
80 print_log_graph($course, $user->id, "userday.png");
81 echo '</div>';
82 print_log($course, $user->id, usergetmidnight(time()), "l.time DESC", $page, $perpage,
83 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
84 break;
86 case "alllogs" :
87 echo '<div class="graph">';
88 print_log_graph($course, $user->id, "usercourse.png");
89 echo '</div>';
90 print_log($course, $user->id, 0, "l.time DESC", $page, $perpage,
91 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
92 break;
93 case 'stats':
95 if (empty($CFG->enablestats)) {
96 error("Stats is not enabled.");
99 require_once($CFG->dirroot.'/lib/statslib.php');
101 $statsstatus = stats_check_uptodate($course->id);
102 if ($statsstatus !== NULL) {
103 notify ($statsstatus);
106 $earliestday = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_daily ORDER BY timeend');
107 $earliestweek = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_weekly ORDER BY timeend');
108 $earliestmonth = get_field_sql('SELECT timeend FROM '.$CFG->prefix.'stats_user_monthly ORDER BY timeend');
110 if (empty($earliestday)) $earliestday = time();
111 if (empty($earliestweek)) $earliestweek = time();
112 if (empty($earliestmonth)) $earliestmonth = time();
114 $now = stats_get_base_daily();
115 $lastweekend = stats_get_base_weekly();
116 $lastmonthend = stats_get_base_monthly();
118 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
120 if (empty($timeoptions)) {
121 error(get_string('nostatstodisplay'), $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
124 // use the earliest.
125 $time = array_pop(array_keys($timeoptions));
127 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
129 $param->table = 'user_'.$param->table;
131 $sql = 'SELECT timeend,'.$param->fields.' FROM '.$CFG->prefix.'stats_'.$param->table.' WHERE '
132 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
133 .' userid = '.$user->id
134 .' AND timeend >= '.$param->timeafter
135 .$param->extras
136 .' ORDER BY timeend DESC';
137 $stats = get_records_sql($sql);
139 if (empty($stats)) {
140 error(get_string('nostatstodisplay'), $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
143 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>';
145 // What the heck is this about? -- MD
146 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
148 $table = new object();
149 $table->align = array('left','center','center','center');
150 $param->table = str_replace('user_','',$param->table);
151 $table->head = array(get_string('periodending','moodle',$param->table),$param->line1,$param->line2,$param->line3);
152 foreach ($stats as $stat) {
153 if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
154 continue;
156 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
157 $a[] = $stat->line2;
158 $a[] = $stat->line3;
159 $table->data[] = $a;
161 print_table($table);
162 break;
163 case "outline" :
164 case "complete" :
165 default:
166 $sections = get_all_sections($course->id);
168 for ($i=0; $i<=$course->numsections; $i++) {
170 if (isset($sections[$i])) { // should always be true
172 $section = $sections[$i];
173 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible or !$course->hiddensections);
175 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
177 if ($section->sequence) {
178 echo '<div class="section">';
179 echo '<h2>';
180 switch ($course->format) {
181 case "weeks": print_string("week"); break;
182 case "topics": print_string("topic"); break;
183 default: print_string("section"); break;
185 echo " $i</h2>";
187 echo '<div class="content">';
189 if ($mode == "outline") {
190 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
193 $sectionmods = explode(",", $section->sequence);
194 foreach ($sectionmods as $sectionmod) {
195 if (empty($mods[$sectionmod])) {
196 continue;
198 $mod = $mods[$sectionmod];
200 if (empty($mod->visible)) {
201 continue;
204 $instance = get_record("$mod->modname", "id", "$mod->instance");
205 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
207 if (file_exists($libfile)) {
208 require_once($libfile);
210 switch ($mode) {
211 case "outline":
212 $user_outline = $mod->modname."_user_outline";
213 if (function_exists($user_outline)) {
214 $output = $user_outline($course, $user, $mod, $instance);
215 print_outline_row($mod, $instance, $output);
217 break;
218 case "complete":
219 $user_complete = $mod->modname."_user_complete";
220 if (function_exists($user_complete)) {
221 $image = "<img src=\"../mod/$mod->modname/icon.gif\" ".
222 "class=\"icon\" alt=\"$mod->modfullname\" />";
223 echo "<h4>$image $mod->modfullname: ".
224 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
225 format_string($instance->name,true)."</a></h4>";
227 ob_start();
229 echo "<ul>";
230 $user_complete($course, $user, $mod, $instance);
231 echo "</ul>";
233 $output = ob_get_contents();
234 ob_end_clean();
236 if (str_replace(' ', '', $output) != '<ul></ul>') {
237 echo $output;
240 break;
245 if ($mode == "outline") {
246 echo "</table>";
248 echo '</div>'; // content
249 echo '</div>'; // section
254 break;
258 print_footer($course);
261 function print_outline_row($mod, $instance, $result) {
262 global $CFG;
264 $image = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\" class=\"icon\" alt=\"$mod->modfullname\" />";
266 echo "<tr>";
267 echo "<td valign=\"top\">$image</td>";
268 echo "<td valign=\"top\" style=\"width:300\">";
269 echo " <a title=\"$mod->modfullname\"";
270 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
271 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
272 echo "<td valign=\"top\">";
273 if (isset($result->info)) {
274 echo "$result->info";
275 } else {
276 echo "<p style=\"text-align:center\">-</p>";
278 echo "</td>";
279 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
280 if (!empty($result->time)) {
281 $timeago = format_time(time() - $result->time);
282 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
284 echo "</tr>";