Removing the obsolete plugins which depend on alredy removed libs
[moodle.git] / course / user.php
CommitLineData
24f2b65a 1<?php // $Id$
f9903ed0 2
97c270e9 3// Display user activity reports for a course
4
b0e3a925 5 require_once("../config.php");
6 require_once("lib.php");
f9903ed0 7
40343df7 8 $modes = array("outline", "complete", "todaylogs", "alllogs");
9
f2a365b9 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);
96dcfb56 14 $perpage = optional_param('perpage', 100, PARAM_INT);
f9903ed0 15
579d45b4 16 if (!$course = $DB->get_record('course', array('id'=>$id))) {
dbf91d9d 17 print_error('invalidcourseid', 'error');
f9903ed0 18 }
5bf04b36 19
6bb08163 20 if (! $user = $DB->get_record("user", array("id"=>$user))) {
dbf91d9d 21 print_error('invaliduserid', 'error');
f9903ed0 22 }
5bf04b36 23
f5fc83e8 24 require_login();
934545eb 25 $COURSE = clone($course);
6bb91986 26
f5fc83e8 27 if ($user->deleted) {
28 print_header();
29 print_heading(get_string('userdeleted'));
30 print_footer();
31 die;
32 }
33
3924b988 34 $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
4b10f08b 35 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
3924b988 36
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))) {
dbf91d9d 39 print_error('nopermissiontoviewpage', 'error');
85a6e76c 40 }
41
0be6f678 42 add_to_log($course->id, "course", "user report", "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode", "$user->id");
f9903ed0 43
a2ab3b05 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);
6f2e07e8 51 $fullname = fullname($user, true);
a2ab3b05 52
0be6f678 53 $navlinks = array();
2f6c662f 54
b26adbef 55 if ($course->id != SITEID && has_capability('moodle/course:viewparticipants', $coursecontext)) {
0be6f678 56 $navlinks[] = array('name' => $strparticipants, 'link' => "../user/index.php?id=$course->id", 'type' => 'misc');
a2ab3b05 57 }
f9a0ea69 58
0be6f678 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);
63
64 print_header("$course->shortname: $stractivityreport ($mode)", $course->fullname, $navigation);
65
f9a0ea69 66
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;
b3e900ce 73 $showroles = 1;
f9a0ea69 74 include($CFG->dirroot.'/user/tabs.php');
75
9c9f7d77 76 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
600149be 77
7468bf01 78 switch ($mode) {
7e2d7c92 79 case "grade":
0a784551 80 if (empty($CFG->grade_profilereport) or !file_exists($CFG->dirroot.'/grade/report/'.$CFG->grade_profilereport.'/lib.php')) {
81 $CFG->grade_profilereport = 'user';
82 }
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';
4d25a59e 86
6bb08163 87 $course = $DB->get_record('course', array('id'=>required_param('id', PARAM_INT)));
0a784551 88 $functionname = 'grade_report_'.$CFG->grade_profilereport.'_profilereport';
89 if (function_exists($functionname)) {
90 $functionname($course, $user);
f9c471df 91 }
7e2d7c92 92 break;
0be6f678 93
a2ab3b05 94 case "todaylogs" :
f9a0ea69 95 echo '<div class="graph">';
adecd142 96 print_log_graph($course, $user->id, "userday.png");
f9a0ea69 97 echo '</div>';
0be6f678 98 print_log($course, $user->id, usergetmidnight(time()), "l.time DESC", $page, $perpage,
839f2456 99 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
2b8cef80 100 break;
101
102 case "alllogs" :
f9a0ea69 103 echo '<div class="graph">';
2b8cef80 104 print_log_graph($course, $user->id, "usercourse.png");
f9a0ea69 105 echo '</div>';
0be6f678 106 print_log($course, $user->id, 0, "l.time DESC", $page, $perpage,
839f2456 107 "user.php?id=$course->id&amp;user=$user->id&amp;mode=$mode");
7468bf01 108 break;
f3221af9 109 case 'stats':
110
111 if (empty($CFG->enablestats)) {
dbf91d9d 112 print_error('statsdisable', 'error');
f3221af9 113 }
114
115 require_once($CFG->dirroot.'/lib/statslib.php');
116
13d32e22 117 $statsstatus = stats_check_uptodate($course->id);
118 if ($statsstatus !== NULL) {
119 notify ($statsstatus);
120 }
140c139e 121
6bb08163 122 $earliestday = $DB->get_field_sql('SELECT timeend FROM {stats_user_daily} ORDER BY timeend');
8f15f8ec 123 $earliestweek = $DB->get_field_sql('SELECT timeend FROM {stats_user_weekly} ORDER BY timeend');
6bb08163 124 $earliestmonth = $DB->get_field_sql('SELECT timeend FROM {stats_user_monthly} ORDER BY timeend');
0be6f678 125
f3221af9 126 if (empty($earliestday)) $earliestday = time();
127 if (empty($earliestweek)) $earliestweek = time();
128 if (empty($earliestmonth)) $earliestmonth = time();
0be6f678 129
f3221af9 130 $now = stats_get_base_daily();
131 $lastweekend = stats_get_base_weekly();
132 $lastmonthend = stats_get_base_monthly();
133
134 $timeoptions = stats_get_time_options($now,$lastweekend,$lastmonthend,$earliestday,$earliestweek,$earliestmonth);
135
0be6f678 136 if (empty($timeoptions)) {
5a2a5331 137 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
f3221af9 138 }
139
140 // use the earliest.
3a317277 141 $time = array_pop(array_keys($timeoptions));
0be6f678 142
3eb9babc 143 $param = stats_get_parameters($time,STATS_REPORT_USER_VIEW,$course->id,STATS_MODE_DETAILED);
6bb08163 144 $params = $param->params;
f3221af9 145
146 $param->table = 'user_'.$param->table;
3eb9babc 147
6bb08163 148 $sql = 'SELECT timeend,'.$param->fields.' FROM {stats_'.$param->table.'} WHERE '
3a317277 149 .(($course->id == SITEID) ? '' : ' courseid = '.$course->id.' AND ')
6bb08163 150 .' userid = '.$user->id.' AND timeend >= '.$param->timeafter .$param->extras
f3221af9 151 .' ORDER BY timeend DESC';
6bb08163 152 $stats = $DB->get_records_sql($sql, $params); //TODO: improve these params!!
3a317277 153
154 if (empty($stats)) {
5a2a5331 155 print_error('nostatstodisplay', '', $CFG->wwwroot.'/course/user.php?id='.$course->id.'&user='.$user->id.'&mode=outline');
3a317277 156 }
0be6f678 157
3aafb7d9 158 // MDL-10818, do not display broken graph when user has no permission to view graph
a2e4bf7f 159 if (has_capability('coursereport/stats:view', get_context_instance(CONTEXT_COURSE, $id)) ||
a683deec 160 ($course->showreports and $USER->id == $user->id)) {
3aafb7d9 161 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>';
162 }
3eb9babc 163
e8e64fa3 164 // What the heck is this about? -- MD
3eb9babc 165 $stats = stats_fix_zeros($stats,$param->timeafter,$param->table,(!empty($param->line2)),(!empty($param->line3)));
e8e64fa3 166
f3221af9 167 $table = new object();
168 $table->align = array('left','center','center','center');
169 $param->table = str_replace('user_','',$param->table);
61460dd6 170 switch ($param->table) {
171 case 'daily' : $period = get_string('day'); break;
172 case 'weekly' : $period = get_string('week'); break;
173 case 'monthly': $period = get_string('month', 'form'); break;
174 default : $period = '';
175 }
176 $table->head = array(get_string('periodending','moodle',$period),$param->line1,$param->line2,$param->line3);
e8e64fa3 177 foreach ($stats as $stat) {
178 if (!empty($stat->zerofixed)) { // Don't know why this is necessary, see stats_fix_zeros above - MD
179 continue;
180 }
404fe839 181 $a = array(userdate($stat->timeend,get_string('strftimedate'),$CFG->timezone),$stat->line1);
f3221af9 182 $a[] = $stat->line2;
183 $a[] = $stat->line3;
184 $table->data[] = $a;
185 }
186 print_table($table);
187 break;
7468bf01 188 case "outline" :
189 case "complete" :
190 default:
191 $sections = get_all_sections($course->id);
600149be 192
7468bf01 193 for ($i=0; $i<=$course->numsections; $i++) {
194
195 if (isset($sections[$i])) { // should always be true
196
197 $section = $sections[$i];
15850dcb 198 $showsection = (has_capability('moodle/course:viewhiddensections', $coursecontext) or $section->visible or !$course->hiddensections);
7468bf01 199
1757627a 200 if ($showsection) { // prevent hidden sections in user activity. Thanks to Geoff Wilbert!
7468bf01 201
1757627a 202 if ($section->sequence) {
f9a0ea69 203 echo '<div class="section">';
204 echo '<h2>';
1757627a 205 switch ($course->format) {
206 case "weeks": print_string("week"); break;
207 case "topics": print_string("topic"); break;
208 default: print_string("section"); break;
7af6281f 209 }
1757627a 210 echo " $i</h2>";
0be6f678 211
f9a0ea69 212 echo '<div class="content">';
4f91674c 213
1757627a 214 if ($mode == "outline") {
215 echo "<table cellpadding=\"4\" cellspacing=\"0\">";
4f91674c 216 }
217
1757627a 218 $sectionmods = explode(",", $section->sequence);
219 foreach ($sectionmods as $sectionmod) {
220 if (empty($mods[$sectionmod])) {
221 continue;
222 }
223 $mod = $mods[$sectionmod];
0be6f678 224
1757627a 225 if (empty($mod->visible)) {
226 continue;
227 }
359b138b 228
6bb08163 229 $instance = $DB->get_record("$mod->modname", array("id"=>$mod->instance));
1757627a 230 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
231
232 if (file_exists($libfile)) {
233 require_once($libfile);
234
235 switch ($mode) {
236 case "outline":
237 $user_outline = $mod->modname."_user_outline";
238 if (function_exists($user_outline)) {
239 $output = $user_outline($course, $user, $mod, $instance);
240 print_outline_row($mod, $instance, $output);
241 }
242 break;
243 case "complete":
244 $user_complete = $mod->modname."_user_complete";
245 if (function_exists($user_complete)) {
246 $image = "<img src=\"../mod/$mod->modname/icon.gif\" ".
0d905d9f 247 "class=\"icon\" alt=\"$mod->modfullname\" />";
1757627a 248 echo "<h4>$image $mod->modfullname: ".
249 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
46043aff 250 format_string($instance->name,true)."</a></h4>";
0be6f678 251
d76be18f 252 ob_start();
253
1757627a 254 echo "<ul>";
255 $user_complete($course, $user, $mod, $instance);
256 echo "</ul>";
d76be18f 257
d76be18f 258 $output = ob_get_contents();
4b9842d2 259 ob_end_clean();
0be6f678 260
d76be18f 261 if (str_replace(' ', '', $output) != '<ul></ul>') {
262 echo $output;
263 }
1757627a 264 }
265 break;
359b138b 266 }
1757627a 267 }
7468bf01 268 }
0be6f678 269
1757627a 270 if ($mode == "outline") {
271 echo "</table>";
7468bf01 272 }
f9a0ea69 273 echo '</div>'; // content
274 echo '</div>'; // section
7468bf01 275 }
7468bf01 276 }
600149be 277 }
f9903ed0 278 }
7468bf01 279 break;
f9903ed0 280 }
281
7468bf01 282
f9903ed0 283 print_footer($course);
284
7468bf01 285
359b138b 286function print_outline_row($mod, $instance, $result) {
bad35380 287 global $CFG;
288
0d905d9f 289 $image = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\" class=\"icon\" alt=\"$mod->modfullname\" />";
24f2b65a 290
291 echo "<tr>";
292 echo "<td valign=\"top\">$image</td>";
fbe31d22 293 echo "<td valign=\"top\" style=\"width:300\">";
24f2b65a 294 echo " <a title=\"$mod->modfullname\"";
46043aff 295 echo " href=\"../mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
24f2b65a 296 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
1530c97c 297 echo "<td valign=\"top\">";
359b138b 298 if (isset($result->info)) {
299 echo "$result->info";
300 } else {
fbe31d22 301 echo "<p style=\"text-align:center\">-</p>";
359b138b 302 }
24f2b65a 303 echo "</td>";
304 echo "<td>&nbsp;&nbsp;&nbsp;</td>";
20affd15 305 if (!empty($result->time)) {
359b138b 306 $timeago = format_time(time() - $result->time);
fbe31d22 307 echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
359b138b 308 }
24f2b65a 309 echo "</tr>";
7468bf01 310}
311
f9903ed0 312?>