Fixed link of user name in footer of course page
[moodle.git] / course / lib.php
CommitLineData
e4027ac9 1<?php // $Id$
97c270e9 2 // Library of useful functions
f9903ed0 3
f9903ed0 4
3d891989 5if (defined('COURSE_MAX_LOG_DISPLAY')) { // Being included again - should never happen!!
9ae687af 6 return;
7}
8
3d891989 9define('COURSE_MAX_LOG_DISPLAY', 150); // days
d9d1c35d 10
3d891989 11define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
1c0200e0 12
3d891989 13define('COURSE_LIVELOG_REFRESH', 60); // Seconds
87ffed0d 14
9e51847a 15define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds
cb29b020 16
17define('COURSE_MAX_SUMMARIES_PER_PAGE', 10); // courses
ef58b822 18
c2cb4545 19define("FRONTPAGENEWS", 0);
20define("FRONTPAGECOURSELIST", 1);
21define("FRONTPAGECATEGORYNAMES", 2);
f9903ed0 22
587510be 23function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0, $selecteddate="lastlogin",
4581271a 24 $mod="", $modid="activity/All", $modaction="", $selectedgroup="", $selectedsort="default") {
cb83c3cb 25
26 global $USER, $CFG;
27
cb83c3cb 28 $isteacher = isteacher($course->id);
587510be 29
30 if ($advancedfilter) {
31
32 // Get all the possible users
33 $users = array();
89adb174 34
587510be 35 if ($course->category) {
36 $courseusers = get_course_users($course->id);
37 } else {
38 $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname");
89adb174 39 }
40
587510be 41 if ($courseusers) {
42 foreach ($courseusers as $courseuser) {
43 $users[$courseuser->id] = fullname($courseuser, $isteacher);
44 }
cb83c3cb 45 }
587510be 46 if ($guest = get_guest()) {
47 $users[$guest->id] = fullname($guest);
48 }
89adb174 49
587510be 50 if (isadmin()) {
51 if ($ccc = get_records("course", "", "", "fullname")) {
52 foreach ($ccc as $cc) {
53 if ($cc->category) {
54 $courses["$cc->id"] = "$cc->fullname";
55 } else {
56 $courses["$cc->id"] = " $cc->fullname (Site)";
89adb174 57 }
587510be 58 }
cb83c3cb 59 }
587510be 60 asort($courses);
61 }
4581271a 62
587510be 63 $activities = array();
cb83c3cb 64
587510be 65 $selectedactivity = $modid;
4581271a 66
587510be 67 if ($modinfo = unserialize($course->modinfo)) {
68 $section = 0;
69 if ($course->format == 'weeks') { // Body
70 $strsection = get_string("week");
71 } else {
72 $strsection = get_string("topic");
cb83c3cb 73 }
cb83c3cb 74
587510be 75 $activities["activity/All"] = "All activities";
76 $activities["activity/Assignments"] = "All assignments";
77 $activities["activity/Chats"] = "All chats";
78 $activities["activity/Forums"] = "All forums";
79 $activities["activity/Quizzes"] = "All quizzes";
80 $activities["activity/Workshops"] = "All workshops";
81
82 $activities["section/individual"] = "------------- Individual Activities --------------";
83
84 foreach ($modinfo as $mod) {
85 if ($mod->mod == "label") {
86 continue;
89adb174 87 }
9c08ad13 88 if (!$mod->visible and !$isteacher) {
89 continue;
90 }
91
587510be 92 if ($mod->section > 0 and $section <> $mod->section) {
93 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
94 }
95 $section = $mod->section;
70fb7712 96 $mod->name = strip_tags(urldecode($mod->name));
587510be 97 if (strlen($mod->name) > 55) {
98 $mod->name = substr($mod->name, 0, 50)."...";
99 }
100 if (!$mod->visible) {
101 $mod->name = "(".$mod->name.")";
102 }
103 $activities["$mod->cm"] = $mod->name;
104
105 if ($mod->cm == $modid) {
106 $selectedactivity = "$mod->cm";
107 }
cb83c3cb 108 }
109 }
cb83c3cb 110
587510be 111 $strftimedate = get_string("strftimedate");
112 $strftimedaydate = get_string("strftimedaydate");
cb83c3cb 113
587510be 114 asort($users);
cb83c3cb 115
587510be 116 // Get all the possible dates
117 // Note that we are keeping track of real (GMT) time and user time
118 // User time is only used in displays - all calcs and passing is GMT
cb83c3cb 119
587510be 120 $timenow = time(); // GMT
cb83c3cb 121
587510be 122 // What day is it now for the user, and when is midnight that day (in GMT).
123 $timemidnight = $today = usergetmidnight($timenow);
cb83c3cb 124
587510be 125 $dates = array();
126 $dates["$USER->lastlogin"] = get_string("lastlogin").", ".userdate($USER->lastlogin, $strftimedate);
127 $dates["$timemidnight"] = get_string("today").", ".userdate($timenow, $strftimedate);
cb83c3cb 128
587510be 129 if (!$course->startdate or ($course->startdate > $timenow)) {
130 $course->startdate = $course->timecreated;
131 }
cb83c3cb 132
587510be 133 $numdates = 1;
134 while ($timemidnight > $course->startdate and $numdates < 365) {
135 $timemidnight = $timemidnight - 86400;
136 $timenow = $timenow - 86400;
137 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
138 $numdates++;
139 }
cb83c3cb 140
587510be 141 if ($selecteddate == "lastlogin") {
142 $selecteddate = $USER->lastlogin;
143 }
144
145 echo '<form action="recent.php" method="get">';
146 echo '<input type=hidden name=chooserecent value="1">';
147 echo "<center>";
148 echo "<table>";
149
150 if (isadmin()) {
151 echo "<tr><td><b>" . get_string("courses") . "</b></td><td>";
152 choose_from_menu ($courses, "id", $course->id, "");
153 echo "</td></tr>";
154 } else {
155 echo "<input type=hidden name=id value=\"$course->id\">";
156 }
cb83c3cb 157
f5ffb87d 158 $sortfields = array("default" => get_string("bycourseorder"),"dateasc" => get_string("datemostrecentlast"), "datedesc" => get_string("datemostrecentfirst"));
4581271a 159
587510be 160 echo "<tr><td><b>" . get_string("participants") . "</b></td><td>";
161 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
162 echo "</td>";
163
164 echo "<td align='right'><b>" . get_string("since") . "</b></td><td>";
165 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
4581271a 166 echo "</td></tr>";
4581271a 167
587510be 168 echo "<tr><td><b>" . get_string("activities") . "</b></td><td>";
169 choose_from_menu ($activities, "modid", $selectedactivity, "");
170 echo "</td>";
4581271a 171
587510be 172 echo "<td align='right'><b>" . get_string("sortby") . "</b></td><td>";
173 choose_from_menu ($sortfields, "sortby", $selectedsort, "");
174 echo "</td></tr>";
4581271a 175
587510be 176 echo '<tr>';
4581271a 177
587510be 178 $groupmode = groupmode($course);
4581271a 179
587510be 180 if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
181 if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
4581271a 182 echo '<td><b>';
587510be 183 if ($groupmode == VISIBLEGROUPS) {
184 print_string('groupsvisible');
185 } else {
186 print_string('groupsseparate');
187 }
188 echo ':</b></td><td>';
189 choose_from_menu($groups, "selectedgroup", $selectedgroup, get_string("allgroups"), "", "");
190 echo '</td>';
191 }
192 }
193
194
195 echo "<td colspan='2' align='right'>";
196 echo "<input type=submit value=\"".get_string("showrecent")."\">";
197 echo "</td></tr>";
198
199 echo "</table>";
200
201 $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&advancedfilter=0\">" . get_string("normalfilter") . "</a>";
202 print_heading($advancedlink);
203 echo "</center>";
204 echo "</form>";
205
206 } else {
207
208 $day_list = array("1","7","14","21","30");
209 $strsince = get_string("since");
210 $strlastlogin = get_string("lastlogin");
211 $strday = get_string("day");
212 $strdays = get_string("days");
213
214 $heading = "";
215 foreach ($day_list as $count) {
216 if ($count == "1") {
217 $day = $strday;
4581271a 218 } else {
587510be 219 $day = $strdays;
4581271a 220 }
3819ed31 221 $tmpdate = time() - ($count * 3600 * 24);
587510be 222 $heading = $heading .
3819ed31 223 "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&date=$tmpdate\"> $count $day</a> | ";
4581271a 224 }
4581271a 225
587510be 226 $heading = $strsince . ": <a href=\"$CFG->wwwroot/course/recent.php?id=$course->id\">$strlastlogin</a>" . " | " . $heading;
227 print_heading($heading);
4581271a 228
587510be 229 $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&advancedfilter=1\">" . get_string("advancedfilter") . "</a>";
230 print_heading($advancedlink);
231
232 }
4581271a 233
cb83c3cb 234}
9ae687af 235
f24cffb9 236function print_log_selector_form($course, $selecteduser=0, $selecteddate="today",
69c76405 237 $modname="", $modid=0, $modaction="", $selectedgroup=-1) {
f9903ed0 238
9481285b 239 global $USER, $CFG;
240
69c76405 241 /// Setup for group handling.
2ac64806 242 $isteacher = isteacher($course->id);
69c76405 243 $isteacheredit = isteacheredit($course->id);
244 if ($course->groupmode == SEPARATEGROUPS and !$isteacheredit) {
245 $selectedgroup = get_current_group($course->id);
246 $showgroups = false;
247 }
248 else if ($course->groupmode) {
249 $selectedgroup = ($selectedgroup == -1) ? get_current_group($course->id) : $selectedgroup;
250 $showgroups = true;
251 }
252 else {
253 $selectedgroup = 0;
254 $showgroups = false;
255 }
256
f9903ed0 257 // Get all the possible users
258 $users = array();
720a43ce 259
260 if ($course->category) {
69c76405 261 /// If using a group, only get users in that group.
262 if ($selectedgroup) {
263 $sql = 'SELECT u.id as id, u.firstname, u.lastname, u.lastaccess '.
264 'FROM '.$CFG->prefix.'user u,'.$CFG->prefix.'user_students us,'.$CFG->prefix.'groups_members gm, '.
265 $CFG->prefix.'user_teachers ut '.
266 'WHERE us.course='.$course->id.' AND gm.groupid='.$selectedgroup.
267 ' AND (gm.userid=us.userid OR gm.userid=ut.userid) AND gm.userid=u.id';
268 $courseusers = get_records_sql($sql);
269 }
270 else {
271 $courseusers = get_course_users($course->id);
272 }
1a5ab449 273 } else {
274 $courseusers = get_site_users("u.lastaccess DESC", "u.id, u.firstname, u.lastname");
275 }
276
277 if ($courseusers) {
278 foreach ($courseusers as $courseuser) {
2ac64806 279 $users[$courseuser->id] = fullname($courseuser, $isteacher);
122cffc9 280 }
f9903ed0 281 }
1a5ab449 282 if ($guest = get_guest()) {
f5104a2b 283 $users[$guest->id] = fullname($guest);
1a5ab449 284 }
720a43ce 285
286 if (isadmin()) {
9fa49e22 287 if ($ccc = get_records("course", "", "", "fullname")) {
720a43ce 288 foreach ($ccc as $cc) {
cfa5d3f2 289 if ($cc->category) {
290 $courses["$cc->id"] = "$cc->fullname";
291 } else {
292 $courses["$cc->id"] = " $cc->fullname (Site)";
293 }
720a43ce 294 }
f9903ed0 295 }
cfa5d3f2 296 asort($courses);
f9903ed0 297 }
298
f24cffb9 299 $activities = array();
300 $selectedactivity = "";
301
302 if ($modinfo = unserialize($course->modinfo)) {
303 $section = 0;
304 if ($course->format == 'weeks') { // Bodgy
305 $strsection = get_string("week");
306 } else {
307 $strsection = get_string("topic");
308 }
309 foreach ($modinfo as $mod) {
310 if ($mod->mod == "label") {
311 continue;
312 }
313 if ($mod->section > 0 and $section <> $mod->section) {
314 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
315 }
316 $section = $mod->section;
69c76405 317 $mod->name = urldecode($mod->name);
f24cffb9 318 if (strlen($mod->name) > 55) {
319 $mod->name = substr($mod->name, 0, 50)."...";
320 }
321 if (!$mod->visible) {
322 $mod->name = "(".$mod->name.")";
323 }
69d79bc3 324 $activities["$mod->cm"] = $mod->name;
f24cffb9 325
326 if ($mod->cm == $modid) {
69d79bc3 327 $selectedactivity = "$mod->cm";
f24cffb9 328 }
329 }
69c76405 330
331 if (isadmin() && !$course->category) {
332 $activities["site_errors"] = get_string("siteerrors");
333 if ($modid === "site_errors") {
334 $selectedactivity = "site_errors";
335 }
336 }
f24cffb9 337 }
69c76405 338
dcde9f02 339
340 $strftimedate = get_string("strftimedate");
341 $strftimedaydate = get_string("strftimedaydate");
342
f9903ed0 343 asort($users);
344
345 // Get all the possible dates
9481285b 346 // Note that we are keeping track of real (GMT) time and user time
347 // User time is only used in displays - all calcs and passing is GMT
348
349 $timenow = time(); // GMT
350
351 // What day is it now for the user, and when is midnight that day (in GMT).
9604ccb1 352 $timemidnight = $today = usergetmidnight($timenow);
9481285b 353
354 // Put today up the top of the list
dcde9f02 355 $dates = array("$timemidnight" => get_string("today").", ".userdate($timenow, $strftimedate) );
9481285b 356
3695f5b4 357 if (!$course->startdate or ($course->startdate > $timenow)) {
9481285b 358 $course->startdate = $course->timecreated;
359 }
f9903ed0 360
9481285b 361 $numdates = 1;
362 while ($timemidnight > $course->startdate and $numdates < 365) {
f9903ed0 363 $timemidnight = $timemidnight - 86400;
9481285b 364 $timenow = $timenow - 86400;
dcde9f02 365 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
9481285b 366 $numdates++;
f9903ed0 367 }
368
369 if ($selecteddate == "today") {
370 $selecteddate = $today;
371 }
372
f24cffb9 373 echo '<center>';
374 echo '<form action="log.php" method="get">';
375 echo '<input type=hidden name=chooselog value="1">';
720a43ce 376 if (isadmin()) {
849bc02a 377 choose_from_menu ($courses, "id", $course->id, "");
720a43ce 378 } else {
f24cffb9 379 echo "<input type=hidden name=id value=\"$course->id\">";
720a43ce 380 }
69c76405 381
382 if ($showgroups) {
383 $cgroups = get_groups($course->id);
384 foreach ($cgroups as $cgroup) {
385 $groups[$cgroup->id] = $cgroup->name;
386 }
387 choose_from_menu ($groups, "group", $selectedgroup, get_string("allgroups") );
388 }
389
1a5ab449 390 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
97c270e9 391 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
69d79bc3 392 choose_from_menu ($activities, "modid", $selectedactivity, get_string("allactivities"), "", "");
f24cffb9 393 echo "<input type=submit value=\"".get_string("showtheselogs")."\">";
394 echo "</form>";
395 echo "</center>";
f9903ed0 396}
397
600149be 398function make_log_url($module, $url) {
399 switch ($module) {
400 case "course":
401 case "user":
402 case "file":
403 case "login":
404 case "lib":
405 case "admin":
406 return "/$module/$url";
407 break;
408 default:
409 return "/mod/$module/$url";
410 break;
411 }
412}
413
69c76405 414function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
415 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
f24cffb9 416
e0161bff 417 // It is assumed that $date is the GMT time of midnight for that day,
418 // and so the next 86400 seconds worth of logs are printed.
f9903ed0 419
f24cffb9 420 global $CFG, $db;
47f1da80 421
69c76405 422 /// Setup for group handling.
423 $isteacher = isteacher($course->id);
424 $isteacheredit = isteacheredit($course->id);
425
426 /// If the group mode is separate, and this user does not have editing privileges,
427 /// then only the user's group can be viewed.
428 if ($course->groupmode == SEPARATEGROUPS and !$isteacheredit) {
429 $groupid = get_current_group($course->id);
430 }
431 /// If this course doesn't have groups, no groupid can be specified.
432 else if (!$course->groupmode) {
433 $groupid = 0;
434 }
435
e0161bff 436 $joins = array();
a2ab3b05 437
e0161bff 438 if ($course->category) {
439 $joins[] = "l.course='$course->id'";
720a43ce 440 } else {
2eb68e6f 441 $courses[0] = '';
1a5ab449 442 if ($ccc = get_courses("all", "c.id ASC", "c.id,c.shortname")) {
720a43ce 443 foreach ($ccc as $cc) {
444 $courses[$cc->id] = "$cc->shortname";
445 }
446 }
447 }
f9903ed0 448
c469a7ef 449 if ($modname) {
e0161bff 450 $joins[] = "l.module = '$modname'";
f24cffb9 451 }
452
e0161bff 453 if ($modid && is_int($modid)) {
454 $joins[] = "l.cmid = '$modid'";
455 } else if ($modid == "site_errors") {
456 $joins[] = "l.action='error'";
69d79bc3 457 }
458
459 if ($modaction) {
e0161bff 460 $joins[] = "l.action = '$modaction'";
f24cffb9 461 }
462
69c76405 463 /// Getting all members of a group.
464 if ($groupid and !$user) {
465 if ($gusers = get_records('groups_members', 'groupid', $groupid)) {
466 $first = true;
467 foreach($gusers as $guser) {
468 if ($first) {
469 $gselect = '(l.userid='.$guser->userid;
470 $first = false;
471 }
472 else {
473 $gselect .= ' OR l.userid='.$guser->userid;
474 }
475 }
476 if (!$first) $gselect .= ')';
477 $joins[] = $gselect;
478 }
479 }
480 else if ($user) {
e0161bff 481 $joins[] = "l.userid = '$user'";
f9903ed0 482 }
483
484 if ($date) {
485 $enddate = $date + 86400;
e0161bff 486 $joins[] = "l.time > '$date' AND l.time < '$enddate'";
f9903ed0 487 }
488
2828ff51 489 $selector = '';
e0161bff 490 for ($i = 0; $i < count($joins); $i++) {
491 $selector .= $joins[$i] . (($i == count($joins)-1) ? " " : " AND ");
492 }
493
494
d09f3c80 495 $totalcount = 0; // Initialise
496
519d369f 497 if (!$logs = get_logs($selector, $order, $page*$perpage, $perpage, $totalcount)) {
f9903ed0 498 notify("No logs found!");
499 print_footer($course);
500 exit;
501 }
502
503 $count=0;
2eb68e6f 504 $ldcache = array();
f9903ed0 505 $tt = getdate(time());
506 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
1c0200e0 507
dcde9f02 508 $strftimedatetime = get_string("strftimedatetime");
2ac64806 509 $isteacher = isteacher($course->id);
dcde9f02 510
f9d5fd3b 511 echo "<p align=center>";
519d369f 512 print_string("displayingrecords", "", $totalcount);
f9d5fd3b 513 echo "</p>";
1c0200e0 514
519d369f 515 print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
516
2eb68e6f 517 echo '<table border=0 align=center cellpadding=3 cellspacing=3>';
f9903ed0 518 foreach ($logs as $log) {
600149be 519
2eb68e6f 520 if (isset($ldcache[$log->module][$log->action])) {
521 $ld = $ldcache[$log->module][$log->action];
522 } else {
523 $ld = get_record('log_display', 'module', $log->module, "action", $log->action);
524 $ldcache[$log->module][$log->action] = $ld;
525 }
526 if ($ld) {
527 $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
600149be 528 }
529
2eb68e6f 530 echo '<tr nowrap="nowrap">';
720a43ce 531 if (! $course->category) {
2eb68e6f 532 echo '<td nowrap="nowrap"><font size=2><a href="view.php?id='.$log->course.'">'.$courses[$log->course].'</a></td>';
720a43ce 533 }
2eb68e6f 534 echo '<td nowrap align=right><font size=2>'.userdate($log->time, '%a').'</td>';
535 echo '<td nowrap><font size=2>'.userdate($log->time, $strftimedatetime).'</td>';
536 echo '<td nowrap><font size=2>';
537 link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&user=$log->userid", 'ipatlas',$log->ip, 400, 700);
538 echo '</td>';
2ac64806 539 $fullname = fullname($log, $isteacher);
2eb68e6f 540 echo '<td nowrap><font size=2><a href="../user/view.php?id='."$log->userid&course=$log->course".'"><b>'.$fullname.'</b></td>';
541 echo '<td nowrap><font size=2>';
542 link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 400, 600);
543 echo '</td>';
544 echo '<td nowrap><font size=2>'.$log->info.'</td>';
545 echo '</tr>';
f9903ed0 546 }
2eb68e6f 547 echo '</table>';
519d369f 548
549 print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
f9903ed0 550}
551
552
c2cb4545 553function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
554 global $CFG;
555 if (empty($CFG->gdversion)) {
556 echo "(".get_string("gdneed").")";
d887b5a7 557 } else {
c2cb4545 558 echo "<IMG BORDER=0 SRC=\"$CFG->wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">";
d887b5a7 559 }
560}
561
562
f9903ed0 563
600149be 564function print_recent_activity($course) {
565 // $course is an object
89adb174 566 // This function trawls through the logs looking for
600149be 567 // anything new since the user's last login
568
bf40f9c1 569 global $CFG, $USER, $THEME, $SESSION;
600149be 570
2ac64806 571 $isteacher = isteacher($course->id);
572
6f80940b 573 $timestart = time() - COURSE_MAX_RECENT_PERIOD;
0f87cb1d 574
9e51847a 575 if (!empty($USER->timeaccess[$course->id])) {
6f80940b 576 if ($USER->timeaccess[$course->id] > $timestart) {
9e51847a 577 $timestart = $USER->timeaccess[$course->id];
578 }
3d891989 579 }
0f87cb1d 580
0f87cb1d 581 echo '<center><font size="1">';
582 echo get_string("activitysince", "", userdate($timestart));
583
761b1f84 584 echo '<p><a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a></p>';
0f87cb1d 585
586 echo '</font></center>';
587
600149be 588
589 // Firstly, have there been any new enrolments?
590
591 $heading = false;
592 $content = false;
1b5910c4 593
6c38b7e0 594 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 595
6c38b7e0 596 if ($users) {
e2b7251d 597 echo "<p>";
6c38b7e0 598 foreach ($users as $user) {
600149be 599 if (! $heading) {
4c654ee3 600 print_headline(get_string("newusers").":");
600149be 601 $heading = true;
602 $content = true;
603 }
2ac64806 604 $fullname = fullname($user, $isteacher);
761b1f84 605 echo '<font size=1><a href="'.$CFG->wwwroot."/user/view.php?id=$user->id&course=$course->id\">$fullname</a></font><br />";
600149be 606 }
e2b7251d 607 echo "</p>";
600149be 608 }
609
1b5910c4 610 // Next, have there been any modifications to the course structure?
611
89adb174 612 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
1b5910c4 613 module = 'course' AND action LIKE '% mod'", "time ASC");
614
615 if ($logs) {
616 foreach ($logs as $key => $log) {
617 $info = split(" ", $log->info);
c9f6251e 618
619 if ($info[0] == "label") { // Labels are special activities
620 continue;
621 }
622
1b5910c4 623 $modname = get_field($info[0], "name", "id", $info[1]);
624 //Create a temp valid module structure (course,id)
625 $tempmod->course = $log->course;
626 $tempmod->id = $info[1];
627 //Obtain the visible property from the instance
628 $modvisible = instance_is_visible($info[0],$tempmod);
89adb174 629
1b5910c4 630 //Only if the mod is visible
631 if ($modvisible) {
632 switch ($log->action) {
633 case "add mod":
634 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
635 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
636 break;
637 case "update mod":
638 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
639 if (empty($changelist["$log->info"])) {
640 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
641 }
642 break;
643 case "delete mod":
89adb174 644 if (!empty($changelist["$log->info"]["operation"]) and
1b5910c4 645 $changelist["$log->info"]["operation"] == "add") {
646 $changelist["$log->info"] = NULL;
647 } else {
648 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
649 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
650 }
651 break;
600149be 652 }
ef25340c 653 }
654 }
655 }
656
9c9f7d77 657 if (!empty($changelist)) {
ef25340c 658 foreach ($changelist as $changeinfo => $change) {
659 if ($change) {
660 $changes[$changeinfo] = $change;
661 }
662 }
8a59942e 663 if (isset($changes)){
664 if (count($changes) > 0) {
665 print_headline(get_string("courseupdates").":");
666 $content = true;
667 foreach ($changes as $changeinfo => $change) {
668 echo "<p><font size=1>".$change["text"]."</font></p>";
669 }
600149be 670 }
671 }
89adb174 672 }
bf40f9c1 673
674
675 // If this site uses Library module, then print recent items
676 if (!empty($CFG->librarypath)) {
677 if (file_exists("$CFG->dirroot/$CFG->librarypath/librarylib.php")) {
678 include_once("$CFG->dirroot/$CFG->librarypath/librarylib.php");
679 if (librarysummarize(5, '', date('YmdHis',$USER->lastlogin))) {
680 $content = true;
681 }
682 }
600149be 683 }
684
3869a2ac 685 // Now display new things from each module
600149be 686
0fd7da81 687 $mods = get_records("modules", "visible", "1", "name", "id, name");
688
1b5910c4 689 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
0fd7da81 690 include_once("$CFG->dirroot/mod/$mod->name/lib.php");
691 $print_recent_activity = $mod->name."_print_recent_activity";
1b5910c4 692 if (function_exists($print_recent_activity)) {
693 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
3869a2ac 694 if ($modcontent) {
695 $content = true;
600149be 696 }
600149be 697 }
698 }
699
700 if (! $content) {
3d891989 701 echo "<font size=2>".get_string("nothingnew")."</font>";
600149be 702 }
600149be 703}
704
e1360728 705
d897cae4 706function get_array_of_activities($courseid) {
89adb174 707// For a given course, returns an array of course activity objects
d897cae4 708// Each item in the array contains he following properties:
709// cm - course module id
710// mod - name of the module (eg forum)
711// section - the number of the section (eg week or topic)
712// name - the name of the instance
5867bfb5 713// visible - is the instance visible or not
86aa7ccf 714// extra - contains extra string to include in any link
d897cae4 715
8dddba42 716 global $CFG;
717
d897cae4 718 $mod = array();
719
9fa49e22 720 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 721 return NULL;
722 }
723
724 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
725 foreach ($sections as $section) {
74666583 726 if (!empty($section->sequence)) {
d897cae4 727 $sequence = explode(",", $section->sequence);
728 foreach ($sequence as $seq) {
7af6281f 729 if (empty($rawmods[$seq])) {
730 continue;
731 }
d897cae4 732 $mod[$seq]->cm = $rawmods[$seq]->id;
733 $mod[$seq]->mod = $rawmods[$seq]->modname;
734 $mod[$seq]->section = $section->section;
735 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 736 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 737 $mod[$seq]->extra = "";
8dddba42 738
739 $modname = $mod[$seq]->mod;
740 $functionname = $modname."_get_coursemodule_info";
741
742 include_once("$CFG->dirroot/mod/$modname/lib.php");
743
744 if (function_exists($functionname)) {
9d361034 745 if ($info = $functionname($rawmods[$seq])) {
746 if (!empty($info->extra)) {
747 $mod[$seq]->extra = $info->extra;
748 }
749 if (!empty($info->icon)) {
750 $mod[$seq]->icon = $info->icon;
751 }
c9f6251e 752 }
753 }
d897cae4 754 }
755 }
756 }
757 }
758 return $mod;
759}
760
761
762
e1360728 763
90845098 764function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
765// Returns a number of useful structures for course displays
7468bf01 766
90845098 767 $mods = NULL; // course modules indexed by id
e0161bff 768 $modnames = NULL; // all course module names (except resource!)
94361e02 769 $modnamesplural= NULL; // all course module names (plural form)
90845098 770 $modnamesused = NULL; // course module names used
7468bf01 771
9fa49e22 772 if ($allmods = get_records("modules")) {
90845098 773 foreach ($allmods as $mod) {
5867bfb5 774 if ($mod->visible) {
775 $modnames[$mod->name] = get_string("modulename", "$mod->name");
776 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
777 }
90845098 778 }
779 asort($modnames);
780 } else {
781 error("No modules are installed!");
782 }
783
9fa49e22 784 if ($rawmods = get_course_mods($courseid)) {
7468bf01 785 foreach($rawmods as $mod) { // Index the mods
786 $mods[$mod->id] = $mod;
90845098 787 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
1acfbce5 788 if ($mod->visible or isteacher($courseid)) {
789 $modnamesused[$mod->modname] = $modnames[$mod->modname];
790 }
7468bf01 791 }
c7da6f7a 792 if ($modnamesused) {
793 asort($modnamesused);
794 }
7468bf01 795 }
e0161bff 796
797 unset($modnames['resource']);
798 unset($modnames['label']);
7468bf01 799}
800
9fa49e22 801
7468bf01 802function get_all_sections($courseid) {
89adb174 803
804 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 805 "section, id, course, summary, sequence, visible");
7468bf01 806}
807
b86fc0e2 808function course_set_display($courseid, $display=0) {
809 global $USER;
810
811 if (empty($USER)) {
812 return false;
813 }
814
815 if ($display == "all" or empty($display)) {
816 $display = 0;
817 }
818
819 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
820 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
821 } else {
822 $record->userid = $USER->id;
823 $record->course = $courseid;
824 $record->display = $display;
825 if (!insert_record("course_display", $record)) {
826 notify("Could not save your course display!");
827 }
828 }
829
830 return $USER->display[$courseid] = $display; // Note: = not ==
831}
832
7d99d695 833function set_section_visible($courseid, $sectionnumber, $visibility) {
834/// For a given course section, markes it visible or hidden,
835/// and does the same for every activity in that section
836
837 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
838 set_field("course_sections", "visible", "$visibility", "id", $section->id);
839 if (!empty($section->sequence)) {
840 $modules = explode(",", $section->sequence);
841 foreach ($modules as $moduleid) {
dcd338ff 842 if ($visibility) {
843 show_course_module($moduleid);
844 } else {
845 hide_course_module($moduleid);
846 }
7d99d695 847 }
848 }
5867bfb5 849 rebuild_course_cache($courseid);
7d99d695 850 }
851}
ba2e5d73 852
5e367a2d 853
d897cae4 854function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 855/// Prints a section full of activity modules
7977cffd 856 global $CFG, $USER;
857
3d575e6f 858 static $groupbuttons;
32d03b7b 859 static $groupbuttonslink;
52dcc2f9 860 static $isteacher;
861 static $isediting;
7977cffd 862 static $ismoving;
863 static $strmovehere;
864 static $strmovefull;
52dcc2f9 865
110a32e2 866
52dcc2f9 867 if (!isset($isteacher)) {
32d03b7b 868 $groupbuttons = $course->groupmode;
869 $groupbuttonslink = (!$course->groupmodeforce);
52dcc2f9 870 $isteacher = isteacher($course->id);
52dcc2f9 871 $isediting = isediting($course->id);
7977cffd 872 $ismoving = ismoving($course->id);
3d575e6f 873 if ($ismoving) {
874 $strmovehere = get_string("movehere");
875 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
876 }
7977cffd 877 }
94361e02 878
c408b0c4 879 $modinfo = unserialize($course->modinfo);
94361e02 880
89adb174 881 echo '<table width="'.$width.'" cellpadding="1" cellspacing="0">';
74666583 882 if (!empty($section->sequence)) {
94361e02 883
884 $sectionmods = explode(",", $section->sequence);
885
886 foreach ($sectionmods as $modnumber) {
9ae687af 887 if (empty($mods[$modnumber])) {
888 continue;
889 }
94361e02 890 $mod = $mods[$modnumber];
c9f6251e 891
52dcc2f9 892 if ($mod->visible or $isteacher) {
c9f6251e 893 echo "<tr><td class=\"activity$mod->modname\">";
7977cffd 894 if ($ismoving) {
895 if ($mod->id == $USER->activitycopy) {
896 continue;
897 }
6951a894 898 echo "<a title=\"$strmovefull\"".
5fc2f2fd 899 " href=\"$CFG->wwwroot/course/mod.php?moveto=$mod->id\">".
6951a894 900 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
901 " alt=\"$strmovehere\" border=\"0\"></a><br />\n";
1acfbce5 902 }
7977cffd 903 $instancename = urldecode($modinfo[$modnumber]->name);
3c89f9a8 904 if (!empty($CFG->filterall)) {
b346b424 905 $instancename = filter_text("<nolink>$instancename</nolink>", $course->id);
3c89f9a8 906 }
c9f6251e 907
86aa7ccf 908 if (!empty($modinfo[$modnumber]->extra)) {
909 $extra = urldecode($modinfo[$modnumber]->extra);
910 } else {
911 $extra = "";
912 }
c9f6251e 913
9d361034 914 if (!empty($modinfo[$modnumber]->icon)) {
915 $icon = "$CFG->pixpath/".urldecode($modinfo[$modnumber]->icon);
916 } else {
917 $icon = "$CFG->modpixpath/$mod->modname/icon.gif";
918 }
919
aac94fd0 920 if ($mod->indent) {
921 print_spacer(12, 20 * $mod->indent, false);
922 }
923
c9f6251e 924 if ($mod->modname == "label") {
aac94fd0 925 if (!$mod->visible) {
926 echo "<span class=\"dimmed_text\">";
927 }
c9f6251e 928 echo format_text($extra, FORMAT_HTML);
aac94fd0 929 if (!$mod->visible) {
930 echo "</span>";
931 }
c9f6251e 932
933 } else { // Normal activity
934 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
9d361034 935 echo "<img src=\"$icon\"".
c9f6251e 936 " height=16 width=16 alt=\"$mod->modfullname\">".
937 " <font size=2><a title=\"$mod->modfullname\" $linkcss $extra".
938 " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a></font>";
939 }
c9f6251e 940 if ($isediting) {
32d03b7b 941 if ($groupbuttons) {
942 if (! $mod->groupmodelink = $groupbuttonslink) {
943 $mod->groupmode = $course->groupmode;
944 }
945
946 } else {
3d575e6f 947 $mod->groupmode = false;
948 }
ffc69cd3 949 echo "&nbsp;&nbsp;";
3d575e6f 950 echo make_editing_buttons($mod, $absolute, true, $mod->indent);
c9f6251e 951 }
ffc69cd3 952 echo "</td>";
c9f6251e 953 echo "</tr>";
94361e02 954 }
94361e02 955 }
956 }
7977cffd 957 if ($ismoving) {
6951a894 958 echo "<tr><td><a title=\"$strmovefull\"".
959 " href=\"mod.php?movetosection=$section->id\">".
960 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
961 " alt=\"$strmovehere\" border=\"0\"></a></td></tr>\n";
7977cffd 962 }
c9f6251e 963 echo "</table>\n\n";
a7ad3ea6 964}
965
5867bfb5 966
cb57e6f4 967function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
e0161bff 968// Prints the menus to add activities and resources
969
2e2a6351 970 global $CFG;
e0161bff 971 static $straddactivity, $straddresource, $resources;
972
973 if (!isset($straddactivity)) {
974 $straddactivity = get_string('addactivity');
975 $straddresource = get_string('addresource');
976 $resourcetypes = get_list_of_plugins('mod/resource/type');
977 foreach ($resourcetypes as $resourcetype) {
978 $resources["resource&type=$resourcetype"] = get_string("resourcetype$resourcetype", 'resource');
979 }
980 asort($resources);
981 $resources['label'] = get_string('resourcetypelabel', 'resource');
982 }
983
cb57e6f4 984 $output = '';
985
986 $output .= '<div align="right"><table align="right"><tr><td>';
987 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&add=",
988 $resources, "ressection$section", "", $straddresource, '', '', true);
989 $output .= '</td>';
990
991 if ($vertical) {
992 $output .= '</tr><tr>';
993 }
994
995 $output .= '<td>';
996 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&add=",
997 $modnames, "section$section", "", $straddactivity, '', '', true);
998 $output .= '</td></tr></table>';
999 $output .= '</div>';
1000
1001 if ($return) {
1002 return $output;
1003 } else {
1004 echo $output;
1005 }
e0161bff 1006}
1007
5867bfb5 1008function rebuild_course_cache($courseid=0) {
1009// Rebuilds the cached list of course activities stored in the database
1010// If a courseid is not specified, then all are rebuilt
1011
1012 if ($courseid) {
1013 $select = "id = '$courseid'";
1014 } else {
1015 $select = "";
1016 }
1017
1018 if ($courses = get_records_select("course", $select)) {
1019 foreach ($courses as $course) {
1020 $modinfo = serialize(get_array_of_activities($course->id));
1021 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
1022 notify("Could not cache module information for course '$course->fullname'!");
1023 }
1024 }
1025 }
1026}
1027
1028
7541bc3e 1029function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 1030 global $THEME;
1031
7541bc3e 1032 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
1033 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 1034 echo stripslashes($heading);
1035 echo "</td></tr></table>";
5e367a2d 1036}
1037
c2cb4545 1038
1039function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 1040/// Given an empty array, this function recursively travels the
c2cb4545 1041/// categories, building up a nice list for display. It also makes
1042/// an array that list all the parents for each category.
1043
1044 if ($category) {
1045 if ($path) {
1046 $path = "$path / $category->name";
1047 } else {
1048 $path = "$category->name";
1049 }
1050 $list[$category->id] = $path;
1051 } else {
1052 $category->id = 0;
1053 }
1054
1055 if ($categories = get_categories("$category->id")) { // Print all the children recursively
1056 foreach ($categories as $cat) {
1057 if (!empty($category->id)) {
3bd4de22 1058 if (isset($parents[$category->id])) {
2832badf 1059 $parents[$cat->id] = $parents[$category->id];
1060 }
c2cb4545 1061 $parents[$cat->id][] = $category->id;
1062 }
89adb174 1063 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1064 }
1065 }
1066}
1067
1068
c2cb4545 1069function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
89adb174 1070/// Recursive function to print out all the categories in a nice format
c2cb4545 1071/// with or without courses included
9ff5310a 1072 global $CFG;
1073 if (isset($CFG->max_category_depth)&&($depth >= $CFG->max_category_depth)) {
1074 return;
1075 }
c2cb4545 1076
1077 if (!$displaylist) {
e92fe848 1078 make_categories_list($displaylist, $parentslist);
c2cb4545 1079 }
1080
1081 if ($category) {
3af6e1db 1082 if ($category->visible or iscreator()) {
d2b6ba70 1083 print_category_info($category, $depth);
c2cb4545 1084 } else {
1085 return; // Don't bother printing children of invisible categories
1086 }
89adb174 1087
c2cb4545 1088 } else {
c2cb4545 1089 $category->id = "0";
1090 }
1091
1092 if ($categories = get_categories($category->id)) { // Print all the children recursively
1093 $countcats = count($categories);
1094 $count = 0;
1095 $first = true;
1096 $last = false;
1097 foreach ($categories as $cat) {
1098 $count++;
1099 if ($count == $countcats) {
1100 $last = true;
1101 }
1102 $up = $first ? false : true;
1103 $down = $last ? false : true;
1104 $first = false;
1105
89adb174 1106 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
c2cb4545 1107 }
1108 }
c2cb4545 1109}
1110
1111
d2b6ba70 1112function print_category_info($category, $depth) {
1113/// Prints the category info in indented fashion
1114/// This function is only used by print_whole_category_list() above
c2cb4545 1115
1116 global $CFG;
b48f834c 1117 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1118
b48f834c 1119 if (empty($strsummary)) {
1120 $strallowguests = get_string("allowguests");
1121 $strrequireskey = get_string("requireskey");
1122 $strsummary = get_string("summary");
1123 }
ba2e5d73 1124
d5f26b07 1125 $catlinkcss = $category->visible ? "" : " class=\"dimmed\" ";
1126
8ef9cb56 1127 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
c9f6251e 1128 $catimage = "<img src=\"$CFG->pixpath/i/course.gif\" width=16 height=16 border=0>";
b48f834c 1129 } else {
1130 $catimage = "&nbsp";
8ef9cb56 1131 }
b48f834c 1132
1133 echo "\n\n<table border=0 cellpadding=3 cellspacing=0 width=\"100%\"><tr>";
d2b6ba70 1134
c2cb4545 1135 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
b48f834c 1136 $courses = get_courses($category->id);
1137
1138 echo "<tr>";
1139
1140 if ($depth) {
1141 $indent = $depth*30;
1142 $rows = count($courses) + 1;
1143 echo "<td rowspan=\"$rows\" valign=\"top\" width=\"$indent\">";
1144 print_spacer(10, $indent);
1145 echo "</td>";
1146 }
89adb174 1147
b48f834c 1148 echo "<td valign=\"top\">$catimage</td>";
cb29b020 1149 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1150 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1151 echo "</td>";
1152 echo "<td class=\"categoryname\">&nbsp;</td>";
b48f834c 1153 echo "</tr>\n";
1154
9ff5310a 1155 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1156 foreach ($courses as $course) {
1157 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
b48f834c 1158 echo "<tr><td valign=\"top\" width=\"30\">&nbsp;";
cb29b020 1159 echo "</td>\n<td valign=\"top\" width=\"100%\" class=\"coursename\">";
a6870f0c 1160 echo "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
cb29b020 1161 echo "</td>\n<td align=\"right\" valign=\"top\" nowrap class=\"coursename\">";
c2cb4545 1162 if ($course->guest ) {
1163 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
9928b09e 1164 echo "<img hspace=1 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>";
ebe8ddc1 1165 } else {
c9f6251e 1166 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1167 }
c2cb4545 1168 if ($course->password) {
1169 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
9928b09e 1170 echo "<img hspace=1 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
ebe8ddc1 1171 } else {
c9f6251e 1172 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
b48f834c 1173 }
1174 if ($course->summary) {
89adb174 1175 link_to_popup_window ("/course/info.php?id=$course->id", "courseinfo",
1176 "<img hspace=1 alt=\"$strsummary\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/info.gif\">",
b48f834c 1177 400, 500, $strsummary);
ebe8ddc1 1178 } else {
c9f6251e 1179 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1180 }
b48f834c 1181 echo "</td></tr>\n";
0c656181 1182 }
ba2e5d73 1183 }
d2b6ba70 1184 } else {
b48f834c 1185
1186 if ($depth) {
1187 $indent = $depth*20;
1188 echo "<td valign=\"top\" width=\"$indent\">";
1189 print_spacer(10, $indent);
1190 echo "</td>";
d2b6ba70 1191 }
89adb174 1192
cb29b020 1193 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1194 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1195 echo "</td>";
a0456978 1196 echo "<td valign=\"top\" class=\"categorynumber\">$category->coursecount</td></tr>";
c2cb4545 1197 }
b48f834c 1198 echo "\n</table>\n";
c2cb4545 1199}
1200
c2cb4545 1201
c2cb4545 1202function print_courses($category, $width="100%") {
1203/// Category is 0 (for all courses) or an object
1204
1205 global $CFG, $THEME;
1206
1207 if (empty($category)) {
90c2ca2e 1208 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1209 if (count($categories) == 1) {
1210 $category = array_shift($categories);
d2b6ba70 1211 $courses = get_courses($category->id);
90c2ca2e 1212 } else {
d2b6ba70 1213 $courses = get_courses("all");
90c2ca2e 1214 }
1215 unset($categories);
607809b3 1216 } else {
c2cb4545 1217 $categories = get_categories($category->id); // sub categories
d2b6ba70 1218 $courses = get_courses($category->id);
c2cb4545 1219 }
1220
c2cb4545 1221 if ($courses) {
1222 foreach ($courses as $course) {
1223 print_course($course, $width);
1224 echo "<br />\n";
1225 }
1226 } else {
1227 print_heading(get_string("nocoursesyet"));
1228 }
1229
1230}
1231
1232
1233function print_course($course, $width="100%") {
1234
1235 global $CFG, $THEME;
1236
146bbb8f 1237 static $enrol;
1238
1239 if (empty($enrol)) {
1240 require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
1241 $enrol = new enrolment_plugin;
1242 }
1243
c2cb4545 1244 if (! $site = get_site()) {
1245 error("Could not find a site!");
1246 }
1247
146bbb8f 1248
1b6a4b1d 1249 print_simple_box_start("center", "$width", $THEME->cellcontent, 5, "coursebox");
c2cb4545 1250
22288704 1251 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1252
c2cb4545 1253 echo "<table width=\"100%\">";
1254 echo "<tr valign=top>";
1b6a4b1d 1255 echo "<td valign=top width=\"50%\" class=\"courseboxinfo\">";
89adb174 1256 echo "<p><font size=3><b><a title=\"".get_string("entercourse")."\"
22288704 1257 $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a></b></font></p>";
c2cb4545 1258 if ($teachers = get_course_teachers($course->id)) {
1259 echo "<p><font size=\"1\">\n";
1260 foreach ($teachers as $teacher) {
1261 if ($teacher->authority > 0) {
1262 if (!$teacher->role) {
1263 $teacher->role = $course->teacher;
1264 }
2ac64806 1265 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
1266 echo "$teacher->role: <a href=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$fullname</a><br />";
c2cb4545 1267 }
1268 }
1269 echo "</font></p>";
1270 }
c2cb4545 1271
146bbb8f 1272 echo $enrol->get_access_icons($course);
c2cb4545 1273
1b6a4b1d 1274 echo "</td><td valign=top width=\"50%\" class=\"courseboxsummary\">";
0d0e550f 1275 echo "<p><font size=2>".filter_text(text_to_html($course->summary), $course->id)."</font></p>";
c2cb4545 1276 echo "</td></tr>";
1277 echo "</table>";
1278
1279 print_simple_box_end();
1280}
1281
1282
1283function print_my_moodle() {
1284/// Prints custom user information on the home page.
1285/// Over time this can include all sorts of information
1286
1287 global $USER, $CFG;
1288
1289 if (!isset($USER->id)) {
1290 error("It shouldn't be possible to see My Moodle without being logged in.");
1291 }
1292
1293 if ($courses = get_my_courses($USER->id)) {
1294 foreach ($courses as $course) {
c81696e5 1295 if (!$course->category) {
1296 continue;
1297 }
c2cb4545 1298 print_course($course, "100%");
1299 echo "<br />\n";
1300 }
38a10939 1301
7f989948 1302 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1303 echo "<table width=\"100%\"><tr><td align=\"center\">";
1304 print_course_search("", false, "short");
1305 echo "</td><td align=\"center\">";
1306 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1307 echo "</td></tr></table>\n";
1308 }
26330001 1309 } else {
1310 if (count_records("course_categories") > 1) {
cb29b020 1311 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1312 print_whole_category_list();
1313 print_simple_box_end();
1314 } else {
1315 print_courses(0, "100%");
1316 }
607809b3 1317 }
2b8cef80 1318}
1319
11b0c469 1320
a8b56716 1321function print_course_search($value="", $return=false, $format="plain") {
38a10939 1322
1323 global $CFG;
1324
1325 $strsearchcourses= get_string("searchcourses");
1326
a8b56716 1327 if ($format == "plain") {
1328 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1329 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1330 $output .= "<input type=\"text\" size=30 name=\"search\" value=\"$value\">";
1331 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1332 $output .= "</form></p></center>";
e4027ac9 1333 } else if ($format == "short") {
1334 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1335 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1336 $output .= "<input type=\"text\" size=12 name=\"search\" value=\"$value\">";
1337 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1338 $output .= "</form></p></center>";
a8b56716 1339 } else if ($format == "navbar") {
1340 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1341 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1342 $output .= "<input type=\"text\" size=20 name=\"search\" value=\"$value\">";
1343 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
a8b56716 1344 $output .= "</form>";
1345 $output .= "</td></tr></table>";
1346 }
1347
1348 if ($return) {
1349 return $output;
1350 }
1351 echo $output;
38a10939 1352}
11b0c469 1353
1354/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1355
1356function add_course_module($mod) {
11b0c469 1357
e5dfd0f3 1358 $mod->added = time();
11b0c469 1359
e5dfd0f3 1360 return insert_record("course_modules", $mod);
11b0c469 1361}
1362
7977cffd 1363function add_mod_to_section($mod, $beforemod=NULL) {
1364/// Given a full mod object with section and course already defined
1365/// If $before is specified, then this is an existing ID which we
1366/// will insert the new module before
1367///
1368/// Returns the course_sections ID where the mod is inserted
11b0c469 1369
9fa49e22 1370 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1371
1372 $section->sequence = trim($section->sequence);
1373
1374 if (empty($section->sequence)) {
11b0c469 1375 $newsequence = "$mod->coursemodule";
7977cffd 1376
1377 } else if ($beforemod) {
1378 $modarray = explode(",", $section->sequence);
1379
1380 if ($key = array_keys ($modarray, $beforemod->id)) {
1381 $insertarray = array($mod->id, $beforemod->id);
1382 array_splice($modarray, $key[0], 1, $insertarray);
1383 $newsequence = implode(",", $modarray);
1384
1385 } else { // Just tack it on the end anyway
1386 $newsequence = "$section->sequence,$mod->coursemodule";
1387 }
1388
1389 } else {
1390 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1391 }
89adb174 1392
e5dfd0f3 1393 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1394 return $section->id; // Return course_sections ID that was used.
11b0c469 1395 } else {
e5dfd0f3 1396 return 0;
11b0c469 1397 }
89adb174 1398
11b0c469 1399 } else { // Insert a new record
e5dfd0f3 1400 $section->course = $mod->course;
1401 $section->section = $mod->section;
1402 $section->summary = "";
1403 $section->sequence = $mod->coursemodule;
1404 return insert_record("course_sections", $section);
11b0c469 1405 }
1406}
1407
3d575e6f 1408function set_groupmode_for_module($id, $groupmode) {
1409 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1410}
1411
1acfbce5 1412function hide_course_module($mod) {
dcd338ff 1413 $cm = get_record('course_modules', 'id', $mod);
1414 $modulename = get_field('modules', 'name', 'id', $cm->module);
1415 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1416 foreach($events as $event) {
1417 hide_event($event);
1418 }
1419 }
1acfbce5 1420 return set_field("course_modules", "visible", 0, "id", $mod);
1421}
1422
1423function show_course_module($mod) {
dcd338ff 1424 $cm = get_record('course_modules', 'id', $mod);
1425 $modulename = get_field('modules', 'name', 'id', $cm->module);
1426 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1427 foreach($events as $event) {
1428 show_event($event);
1429 }
1430 }
1acfbce5 1431 return set_field("course_modules", "visible", 1, "id", $mod);
1432}
1433
11b0c469 1434function delete_course_module($mod) {
1435 return set_field("course_modules", "deleted", 1, "id", $mod);
1436}
1437
1438function delete_mod_from_section($mod, $section) {
11b0c469 1439
e5dfd0f3 1440 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1441
e5dfd0f3 1442 $modarray = explode(",", $section->sequence);
11b0c469 1443
1444 if ($key = array_keys ($modarray, $mod)) {
1445 array_splice($modarray, $key[0], 1);
1446 $newsequence = implode(",", $modarray);
e5dfd0f3 1447 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1448 } else {
1449 return false;
1450 }
89adb174 1451
11b0c469 1452 }
7977cffd 1453 return false;
11b0c469 1454}
1455
12905134 1456function move_section($course, $section, $move) {
1457/// Moves a whole course section up and down within the course
1458
1459 if (!$move) {
1460 return true;
1461 }
1462
1463 $sectiondest = $section + $move;
1464
1465 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1466 return false;
1467 }
1468
1469 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1470 return false;
1471 }
1472
1473 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1474 return false;
1475 }
1476
56e34ee4 1477 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1478 return false;
1479 }
56e34ee4 1480 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1481 return false;
1482 }
1483 return true;
1484}
1485
1486
7977cffd 1487function moveto_module($mod, $section, $beforemod=NULL) {
1488/// All parameters are objects
1489/// Move the module object $mod to the specified $section
1490/// If $beforemod exists then that is the module
1491/// before which $modid should be inserted
1492
1493/// Remove original module from original section
1494
1495 if (! delete_mod_from_section($mod->id, $mod->section)) {
1496 notify("Could not delete module from existing section");
1497 }
1498
1499/// Update module itself if necessary
1500
1501 if ($mod->section != $section->id) {
89adb174 1502 $mod->section = $section->id;
7977cffd 1503
1504 if (!update_record("course_modules", $mod)) {
1505 return false;
1506 }
1507 }
1508
1509/// Add the module into the new section
1510
1511 $mod->course = $section->course;
1512 $mod->section = $section->section; // need relative reference
1513 $mod->coursemodule = $mod->id;
1514
1515 if (! add_mod_to_section($mod, $beforemod)) {
1516 return false;
1517 }
1518
1519 return true;
1520
1521}
1522
1523
11b0c469 1524
7c0f2984 1525function move_module($cm, $move) {
12905134 1526/// Moves an activity module up and down within the course
11b0c469 1527
1528 if (!$move) {
1529 return true;
1530 }
1531
11b0c469 1532 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1533 error("This course section doesn't exist");
1534 }
1535
1536 $mods = explode(",", $thissection->sequence);
1537
1538 $len = count($mods);
1539 $pos = array_keys($mods, $cm->id);
1540 $thepos = $pos[0];
1541
1542 if ($len == 0 || count($pos) == 0 ) {
1543 error("Very strange. Could not find the required module in this section.");
1544 }
1545
1546 if ($len == 1) {
1547 $first = true;
1548 $last = true;
1549 } else {
1550 $first = ($thepos == 0);
1551 $last = ($thepos == $len - 1);
1552 }
1553
1554 if ($move < 0) { // Moving the module up
1555
1556 if ($first) {
ad41694c 1557 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1558 return true;
ad41694c 1559
11b0c469 1560 } else { // Push onto end of previous section
1561 $prevsectionnumber = $thissection->section - 1;
89adb174 1562 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
9fa49e22 1563 "section", "$prevsectionnumber")) {
11b0c469 1564 error("Previous section ($prevsection->id) doesn't exist");
1565 }
1566
74666583 1567 if (!empty($prevsection->sequence)) {
11b0c469 1568 $newsequence = "$prevsection->sequence,$cm->id";
1569 } else {
1570 $newsequence = "$cm->id";
1571 }
1572
1573 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1574 error("Previous section could not be updated");
1575 }
1576
1577 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1578 error("Module could not be updated");
1579 }
1580
1581 array_splice($mods, 0, 1);
1582 $newsequence = implode(",", $mods);
1583 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1584 error("Module could not be updated");
1585 }
1586
1587 return true;
1588
1589 }
1590 } else { // move up within this section
1591 $swap = $mods[$thepos-1];
1592 $mods[$thepos-1] = $mods[$thepos];
1593 $mods[$thepos] = $swap;
89adb174 1594
11b0c469 1595 $newsequence = implode(",", $mods);
1596 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1597 error("This section could not be updated");
1598 }
1599 return true;
1600 }
1601
1602 } else { // Moving the module down
1603
1604 if ($last) {
1605 $nextsectionnumber = $thissection->section + 1;
89adb174 1606 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
9fa49e22 1607 "section", "$nextsectionnumber")) {
11b0c469 1608
74666583 1609 if (!empty($nextsection->sequence)) {
11b0c469 1610 $newsequence = "$cm->id,$nextsection->sequence";
1611 } else {
1612 $newsequence = "$cm->id";
1613 }
1614
1615 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1616 error("Next section could not be updated");
1617 }
1618
1619 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1620 error("Module could not be updated");
1621 }
1622
1623 array_splice($mods, $thepos, 1);
1624 $newsequence = implode(",", $mods);
1625 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1626 error("This section could not be updated");
1627 }
1628 return true;
1629
1630 } else { // There is no next section, so just return
1631 return true;
1632
1633 }
1634 } else { // move down within this section
1635 $swap = $mods[$thepos+1];
1636 $mods[$thepos+1] = $mods[$thepos];
1637 $mods[$thepos] = $swap;
89adb174 1638
11b0c469 1639 $newsequence = implode(",", $mods);
1640 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1641 error("This section could not be updated");
1642 }
1643 return true;
1644 }
1645 }
1646}
1647
3d575e6f 1648function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1) {
dc0dc7d5 1649 global $CFG, $THEME;
94361e02 1650
3d575e6f 1651 static $str;
1652
1653 if (!isset($str)) {
1654 $str->delete = get_string("delete");
1655 $str->move = get_string("move");
1656 $str->moveup = get_string("moveup");
1657 $str->movedown = get_string("movedown");
1658 $str->moveright = get_string("moveright");
1659 $str->moveleft = get_string("moveleft");
1660 $str->update = get_string("update");
d33a2a6f 1661 $str->duplicate = get_string("duplicate");
3d575e6f 1662 $str->hide = get_string("hide");
1663 $str->show = get_string("show");
1664 $str->clicktochange = get_string("clicktochange");
32d03b7b 1665 $str->forcedmode = get_string("forcedmode");
3d575e6f 1666 $str->groupsnone = get_string("groupsnone");
1667 $str->groupsseparate = get_string("groupsseparate");
1668 $str->groupsvisible = get_string("groupsvisible");
1acfbce5 1669 }
94361e02 1670
1671 if ($absolute) {
dc0dc7d5 1672 $path = "$CFG->wwwroot/course";
1673 } else {
1674 $path = ".";
1675 }
1676
1677 if (empty($THEME->custompix)) {
1678 $pixpath = "$path/../pix";
94361e02 1679 } else {
dc0dc7d5 1680 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1681 }
1acfbce5 1682
3d575e6f 1683 if ($mod->visible) {
1684 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$mod->id\"><img".
dbcd54b7 1685 " src=\"$pixpath/t/hide.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"$str->hide\"></a> ";
1acfbce5 1686 } else {
3d575e6f 1687 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$mod->id\"><img".
dbcd54b7 1688 " src=\"$pixpath/t/show.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
1689 "border=\"0\" alt=\"$str->show\"></a> ";
7977cffd 1690 }
3d575e6f 1691 if ($mod->groupmode !== false) {
1692 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1693 $grouptitle = $str->groupsseparate;
1694 $groupimage = "$pixpath/t/groups.gif";
1695 $grouplink = "$path/mod.php?id=$mod->id&groupmode=0";
3d575e6f 1696 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1697 $grouptitle = $str->groupsvisible;
1698 $groupimage = "$pixpath/t/groupv.gif";
1699 $grouplink = "$path/mod.php?id=$mod->id&groupmode=1";
1700 } else {
1701 $grouptitle = $str->groupsnone;
1702 $groupimage = "$pixpath/t/groupn.gif";
1703 $grouplink = "$path/mod.php?id=$mod->id&groupmode=2";
1704 }
1705 if ($mod->groupmodelink) {
1706 $groupmode = "<a title=\"$grouptitle ($str->clicktochange)\" href=\"$grouplink\">".
dbcd54b7 1707 "<img src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" ".
1708 "border=\"0\" alt=\"$grouptitle\"></a>";
3d575e6f 1709 } else {
32d03b7b 1710 $groupmode = "<img title=\"$grouptitle ($str->forcedmode)\" ".
dbcd54b7 1711 " src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" ".
1712 "border=\"0\" alt=\"$grouptitle\">";
3d575e6f 1713 }
1714 } else {
1715 $groupmode = "";
1716 }
7977cffd 1717
1718 if ($moveselect) {
3d575e6f 1719 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$mod->id\"><img".
dbcd54b7 1720 " src=\"$pixpath/t/move.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
1721 " border=\"0\" alt=\"$str->move\"></a>";
493486c4 1722 } else {
3d575e6f 1723 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$mod->id&move=-1\"><img".
dbcd54b7 1724 " src=\"$pixpath/t/up.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
1725 " border=\"0\" alt=\"$str->moveup\"></a>".
3d575e6f 1726 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$mod->id&move=1\"><img".
dbcd54b7 1727 " src=\"$pixpath/t/down.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
1728 " border=\"0\" alt=\"$str->movedown\"></a>";
7977cffd 1729 }
1730
aac94fd0 1731 $leftright = "";
1732 if ($indent > 0) {
3d575e6f 1733 $leftright .= "<a title=\"$str->moveleft\" href=\"$path/mod.php?id=$mod->id&indent=-1\"><img".
dbcd54b7 1734 " src=\"$pixpath/t/left.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
1735 " border=\"0\" alt=\"$str->moveleft\"></a>";
aac94fd0 1736 }
1737 if ($indent >= 0) {
3d575e6f 1738 $leftright .= "<a title=\"$str->moveright\" href=\"$path/mod.php?id=$mod->id&indent=1\"><img".
dbcd54b7 1739 " src=\"$pixpath/t/right.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
1740 " border=\"0\" alt=\"$str->moveright\"></a>";
aac94fd0 1741 }
1742
bf1241ad 1743 return "$leftright$move".
3d575e6f 1744 "<a title=\"$str->update\" href=\"$path/mod.php?update=$mod->id\"><img".
dbcd54b7 1745 " src=\"$pixpath/t/edit.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" ".
1746 " alt=\"$str->update\"></a>".
d4bfbe4d 1747 // Following line is commented out until this feature is more definite -- martin
1748 // "<a title=\"$str->duplicate\" href=\"$path/mod.php?duplicate=$mod->id\"> 2 </a>".
3d575e6f 1749 "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$mod->id\"><img".
dbcd54b7 1750 " src=\"$pixpath/t/delete.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" ".
1751 " alt=\"$str->delete\"></a>$hideshow$groupmode";
90845098 1752}
1753
f9903ed0 1754?>