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