FIxes bug 1669 and some defunct resource types
[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
e0161bff 893function print_section_add_menus($course, $section, $modnames) {
894// Prints the menus to add activities and resources
895
896 static $straddactivity, $straddresource, $resources;
897
898 if (!isset($straddactivity)) {
899 $straddactivity = get_string('addactivity');
900 $straddresource = get_string('addresource');
901 $resourcetypes = get_list_of_plugins('mod/resource/type');
902 foreach ($resourcetypes as $resourcetype) {
903 $resources["resource&type=$resourcetype"] = get_string("resourcetype$resourcetype", 'resource');
904 }
905 asort($resources);
906 $resources['label'] = get_string('resourcetypelabel', 'resource');
907 }
908
909 echo '<div align="right"><table align="right"><tr><td>';
910 popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&add=",
911 $resources, "ressection$section", "", $straddresource);
912 echo '</td><td>';
913 popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&add=",
914 $modnames, "section$section", "", $straddactivity);
915 echo '</td></tr></table>';
916 echo '</div>';
917}
918
5867bfb5 919function rebuild_course_cache($courseid=0) {
920// Rebuilds the cached list of course activities stored in the database
921// If a courseid is not specified, then all are rebuilt
922
923 if ($courseid) {
924 $select = "id = '$courseid'";
925 } else {
926 $select = "";
927 }
928
929 if ($courses = get_records_select("course", $select)) {
930 foreach ($courses as $course) {
931 $modinfo = serialize(get_array_of_activities($course->id));
932 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
933 notify("Could not cache module information for course '$course->fullname'!");
934 }
935 }
936 }
937}
938
939
7541bc3e 940function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 941 global $THEME;
942
7541bc3e 943 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
944 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 945 echo stripslashes($heading);
946 echo "</td></tr></table>";
5e367a2d 947}
948
c2cb4545 949
950function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 951/// Given an empty array, this function recursively travels the
c2cb4545 952/// categories, building up a nice list for display. It also makes
953/// an array that list all the parents for each category.
954
955 if ($category) {
956 if ($path) {
957 $path = "$path / $category->name";
958 } else {
959 $path = "$category->name";
960 }
961 $list[$category->id] = $path;
962 } else {
963 $category->id = 0;
964 }
965
966 if ($categories = get_categories("$category->id")) { // Print all the children recursively
967 foreach ($categories as $cat) {
968 if (!empty($category->id)) {
3bd4de22 969 if (isset($parents[$category->id])) {
2832badf 970 $parents[$cat->id] = $parents[$category->id];
971 }
c2cb4545 972 $parents[$cat->id][] = $category->id;
973 }
89adb174 974 make_categories_list($list, $parents, $cat, $path);
c2cb4545 975 }
976 }
977}
978
979
c2cb4545 980function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
89adb174 981/// Recursive function to print out all the categories in a nice format
c2cb4545 982/// with or without courses included
9ff5310a 983 global $CFG;
984 if (isset($CFG->max_category_depth)&&($depth >= $CFG->max_category_depth)) {
985 return;
986 }
c2cb4545 987
988 if (!$displaylist) {
e92fe848 989 make_categories_list($displaylist, $parentslist);
c2cb4545 990 }
991
992 if ($category) {
3af6e1db 993 if ($category->visible or iscreator()) {
d2b6ba70 994 print_category_info($category, $depth);
c2cb4545 995 } else {
996 return; // Don't bother printing children of invisible categories
997 }
89adb174 998
c2cb4545 999 } else {
c2cb4545 1000 $category->id = "0";
1001 }
1002
1003 if ($categories = get_categories($category->id)) { // Print all the children recursively
1004 $countcats = count($categories);
1005 $count = 0;
1006 $first = true;
1007 $last = false;
1008 foreach ($categories as $cat) {
1009 $count++;
1010 if ($count == $countcats) {
1011 $last = true;
1012 }
1013 $up = $first ? false : true;
1014 $down = $last ? false : true;
1015 $first = false;
1016
89adb174 1017 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
c2cb4545 1018 }
1019 }
c2cb4545 1020}
1021
1022
d2b6ba70 1023function print_category_info($category, $depth) {
1024/// Prints the category info in indented fashion
1025/// This function is only used by print_whole_category_list() above
c2cb4545 1026
1027 global $CFG;
b48f834c 1028 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1029
b48f834c 1030 if (empty($strsummary)) {
1031 $strallowguests = get_string("allowguests");
1032 $strrequireskey = get_string("requireskey");
1033 $strsummary = get_string("summary");
1034 }
ba2e5d73 1035
d5f26b07 1036 $catlinkcss = $category->visible ? "" : " class=\"dimmed\" ";
1037
8ef9cb56 1038 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
c9f6251e 1039 $catimage = "<img src=\"$CFG->pixpath/i/course.gif\" width=16 height=16 border=0>";
b48f834c 1040 } else {
1041 $catimage = "&nbsp";
8ef9cb56 1042 }
b48f834c 1043
1044 echo "\n\n<table border=0 cellpadding=3 cellspacing=0 width=\"100%\"><tr>";
d2b6ba70 1045
c2cb4545 1046 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
b48f834c 1047 $courses = get_courses($category->id);
1048
1049 echo "<tr>";
1050
1051 if ($depth) {
1052 $indent = $depth*30;
1053 $rows = count($courses) + 1;
1054 echo "<td rowspan=\"$rows\" valign=\"top\" width=\"$indent\">";
1055 print_spacer(10, $indent);
1056 echo "</td>";
1057 }
89adb174 1058
b48f834c 1059 echo "<td valign=\"top\">$catimage</td>";
cb29b020 1060 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1061 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1062 echo "</td>";
1063 echo "<td class=\"categoryname\">&nbsp;</td>";
b48f834c 1064 echo "</tr>\n";
1065
9ff5310a 1066 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1067 foreach ($courses as $course) {
1068 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
b48f834c 1069 echo "<tr><td valign=\"top\" width=\"30\">&nbsp;";
cb29b020 1070 echo "</td>\n<td valign=\"top\" width=\"100%\" class=\"coursename\">";
a6870f0c 1071 echo "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
cb29b020 1072 echo "</td>\n<td align=\"right\" valign=\"top\" nowrap class=\"coursename\">";
c2cb4545 1073 if ($course->guest ) {
1074 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
9928b09e 1075 echo "<img hspace=1 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>";
ebe8ddc1 1076 } else {
c9f6251e 1077 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1078 }
c2cb4545 1079 if ($course->password) {
1080 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
9928b09e 1081 echo "<img hspace=1 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
ebe8ddc1 1082 } else {
c9f6251e 1083 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
b48f834c 1084 }
1085 if ($course->summary) {
89adb174 1086 link_to_popup_window ("/course/info.php?id=$course->id", "courseinfo",
1087 "<img hspace=1 alt=\"$strsummary\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/info.gif\">",
b48f834c 1088 400, 500, $strsummary);
ebe8ddc1 1089 } else {
c9f6251e 1090 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1091 }
b48f834c 1092 echo "</td></tr>\n";
0c656181 1093 }
ba2e5d73 1094 }
d2b6ba70 1095 } else {
b48f834c 1096
1097 if ($depth) {
1098 $indent = $depth*20;
1099 echo "<td valign=\"top\" width=\"$indent\">";
1100 print_spacer(10, $indent);
1101 echo "</td>";
d2b6ba70 1102 }
89adb174 1103
cb29b020 1104 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1105 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1106 echo "</td>";
a0456978 1107 echo "<td valign=\"top\" class=\"categorynumber\">$category->coursecount</td></tr>";
c2cb4545 1108 }
b48f834c 1109 echo "\n</table>\n";
c2cb4545 1110}
1111
c2cb4545 1112
c2cb4545 1113function print_courses($category, $width="100%") {
1114/// Category is 0 (for all courses) or an object
1115
1116 global $CFG, $THEME;
1117
1118 if (empty($category)) {
90c2ca2e 1119 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1120 if (count($categories) == 1) {
1121 $category = array_shift($categories);
d2b6ba70 1122 $courses = get_courses($category->id);
90c2ca2e 1123 } else {
d2b6ba70 1124 $courses = get_courses("all");
90c2ca2e 1125 }
1126 unset($categories);
607809b3 1127 } else {
c2cb4545 1128 $categories = get_categories($category->id); // sub categories
d2b6ba70 1129 $courses = get_courses($category->id);
c2cb4545 1130 }
1131
c2cb4545 1132 if ($courses) {
1133 foreach ($courses as $course) {
1134 print_course($course, $width);
1135 echo "<br />\n";
1136 }
1137 } else {
1138 print_heading(get_string("nocoursesyet"));
1139 }
1140
1141}
1142
1143
1144function print_course($course, $width="100%") {
1145
1146 global $CFG, $THEME;
1147
146bbb8f 1148 static $enrol;
1149
1150 if (empty($enrol)) {
1151 require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
1152 $enrol = new enrolment_plugin;
1153 }
1154
c2cb4545 1155 if (! $site = get_site()) {
1156 error("Could not find a site!");
1157 }
1158
146bbb8f 1159
1b6a4b1d 1160 print_simple_box_start("center", "$width", $THEME->cellcontent, 5, "coursebox");
c2cb4545 1161
22288704 1162 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1163
c2cb4545 1164 echo "<table width=\"100%\">";
1165 echo "<tr valign=top>";
1b6a4b1d 1166 echo "<td valign=top width=\"50%\" class=\"courseboxinfo\">";
89adb174 1167 echo "<p><font size=3><b><a title=\"".get_string("entercourse")."\"
22288704 1168 $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a></b></font></p>";
c2cb4545 1169 if ($teachers = get_course_teachers($course->id)) {
1170 echo "<p><font size=\"1\">\n";
1171 foreach ($teachers as $teacher) {
1172 if ($teacher->authority > 0) {
1173 if (!$teacher->role) {
1174 $teacher->role = $course->teacher;
1175 }
2ac64806 1176 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
1177 echo "$teacher->role: <a href=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$fullname</a><br />";
c2cb4545 1178 }
1179 }
1180 echo "</font></p>";
1181 }
c2cb4545 1182
146bbb8f 1183 echo $enrol->get_access_icons($course);
c2cb4545 1184
1b6a4b1d 1185 echo "</td><td valign=top width=\"50%\" class=\"courseboxsummary\">";
0d0e550f 1186 echo "<p><font size=2>".filter_text(text_to_html($course->summary), $course->id)."</font></p>";
c2cb4545 1187 echo "</td></tr>";
1188 echo "</table>";
1189
1190 print_simple_box_end();
1191}
1192
1193
1194function print_my_moodle() {
1195/// Prints custom user information on the home page.
1196/// Over time this can include all sorts of information
1197
1198 global $USER, $CFG;
1199
1200 if (!isset($USER->id)) {
1201 error("It shouldn't be possible to see My Moodle without being logged in.");
1202 }
1203
1204 if ($courses = get_my_courses($USER->id)) {
1205 foreach ($courses as $course) {
c81696e5 1206 if (!$course->category) {
1207 continue;
1208 }
c2cb4545 1209 print_course($course, "100%");
1210 echo "<br />\n";
1211 }
38a10939 1212
7f989948 1213 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1214 echo "<table width=\"100%\"><tr><td align=\"center\">";
1215 print_course_search("", false, "short");
1216 echo "</td><td align=\"center\">";
1217 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1218 echo "</td></tr></table>\n";
1219 }
26330001 1220 } else {
1221 if (count_records("course_categories") > 1) {
cb29b020 1222 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1223 print_whole_category_list();
1224 print_simple_box_end();
1225 } else {
1226 print_courses(0, "100%");
1227 }
607809b3 1228 }
2b8cef80 1229}
1230
11b0c469 1231
a8b56716 1232function print_course_search($value="", $return=false, $format="plain") {
38a10939 1233
1234 global $CFG;
1235
1236 $strsearchcourses= get_string("searchcourses");
1237
a8b56716 1238 if ($format == "plain") {
1239 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1240 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1241 $output .= "<input type=\"text\" size=30 name=\"search\" value=\"$value\">";
1242 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1243 $output .= "</form></p></center>";
e4027ac9 1244 } else if ($format == "short") {
1245 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1246 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1247 $output .= "<input type=\"text\" size=12 name=\"search\" value=\"$value\">";
1248 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1249 $output .= "</form></p></center>";
a8b56716 1250 } else if ($format == "navbar") {
1251 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1252 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1253 $output .= "<input type=\"text\" size=20 name=\"search\" value=\"$value\">";
1254 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
a8b56716 1255 $output .= "</form>";
1256 $output .= "</td></tr></table>";
1257 }
1258
1259 if ($return) {
1260 return $output;
1261 }
1262 echo $output;
38a10939 1263}
11b0c469 1264
1265/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1266
1267function add_course_module($mod) {
11b0c469 1268
e5dfd0f3 1269 $mod->added = time();
11b0c469 1270
e5dfd0f3 1271 return insert_record("course_modules", $mod);
11b0c469 1272}
1273
7977cffd 1274function add_mod_to_section($mod, $beforemod=NULL) {
1275/// Given a full mod object with section and course already defined
1276/// If $before is specified, then this is an existing ID which we
1277/// will insert the new module before
1278///
1279/// Returns the course_sections ID where the mod is inserted
11b0c469 1280
9fa49e22 1281 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1282
1283 $section->sequence = trim($section->sequence);
1284
1285 if (empty($section->sequence)) {
11b0c469 1286 $newsequence = "$mod->coursemodule";
7977cffd 1287
1288 } else if ($beforemod) {
1289 $modarray = explode(",", $section->sequence);
1290
1291 if ($key = array_keys ($modarray, $beforemod->id)) {
1292 $insertarray = array($mod->id, $beforemod->id);
1293 array_splice($modarray, $key[0], 1, $insertarray);
1294 $newsequence = implode(",", $modarray);
1295
1296 } else { // Just tack it on the end anyway
1297 $newsequence = "$section->sequence,$mod->coursemodule";
1298 }
1299
1300 } else {
1301 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1302 }
89adb174 1303
e5dfd0f3 1304 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1305 return $section->id; // Return course_sections ID that was used.
11b0c469 1306 } else {
e5dfd0f3 1307 return 0;
11b0c469 1308 }
89adb174 1309
11b0c469 1310 } else { // Insert a new record
e5dfd0f3 1311 $section->course = $mod->course;
1312 $section->section = $mod->section;
1313 $section->summary = "";
1314 $section->sequence = $mod->coursemodule;
1315 return insert_record("course_sections", $section);
11b0c469 1316 }
1317}
1318
3d575e6f 1319function set_groupmode_for_module($id, $groupmode) {
1320 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1321}
1322
1acfbce5 1323function hide_course_module($mod) {
dcd338ff 1324 $cm = get_record('course_modules', 'id', $mod);
1325 $modulename = get_field('modules', 'name', 'id', $cm->module);
1326 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1327 foreach($events as $event) {
1328 hide_event($event);
1329 }
1330 }
1acfbce5 1331 return set_field("course_modules", "visible", 0, "id", $mod);
1332}
1333
1334function show_course_module($mod) {
dcd338ff 1335 $cm = get_record('course_modules', 'id', $mod);
1336 $modulename = get_field('modules', 'name', 'id', $cm->module);
1337 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1338 foreach($events as $event) {
1339 show_event($event);
1340 }
1341 }
1acfbce5 1342 return set_field("course_modules", "visible", 1, "id", $mod);
1343}
1344
11b0c469 1345function delete_course_module($mod) {
1346 return set_field("course_modules", "deleted", 1, "id", $mod);
1347}
1348
1349function delete_mod_from_section($mod, $section) {
11b0c469 1350
e5dfd0f3 1351 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1352
e5dfd0f3 1353 $modarray = explode(",", $section->sequence);
11b0c469 1354
1355 if ($key = array_keys ($modarray, $mod)) {
1356 array_splice($modarray, $key[0], 1);
1357 $newsequence = implode(",", $modarray);
e5dfd0f3 1358 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1359 } else {
1360 return false;
1361 }
89adb174 1362
11b0c469 1363 }
7977cffd 1364 return false;
11b0c469 1365}
1366
12905134 1367function move_section($course, $section, $move) {
1368/// Moves a whole course section up and down within the course
1369
1370 if (!$move) {
1371 return true;
1372 }
1373
1374 $sectiondest = $section + $move;
1375
1376 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1377 return false;
1378 }
1379
1380 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1381 return false;
1382 }
1383
1384 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1385 return false;
1386 }
1387
56e34ee4 1388 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1389 return false;
1390 }
56e34ee4 1391 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1392 return false;
1393 }
1394 return true;
1395}
1396
1397
7977cffd 1398function moveto_module($mod, $section, $beforemod=NULL) {
1399/// All parameters are objects
1400/// Move the module object $mod to the specified $section
1401/// If $beforemod exists then that is the module
1402/// before which $modid should be inserted
1403
1404/// Remove original module from original section
1405
1406 if (! delete_mod_from_section($mod->id, $mod->section)) {
1407 notify("Could not delete module from existing section");
1408 }
1409
1410/// Update module itself if necessary
1411
1412 if ($mod->section != $section->id) {
89adb174 1413 $mod->section = $section->id;
7977cffd 1414
1415 if (!update_record("course_modules", $mod)) {
1416 return false;
1417 }
1418 }
1419
1420/// Add the module into the new section
1421
1422 $mod->course = $section->course;
1423 $mod->section = $section->section; // need relative reference
1424 $mod->coursemodule = $mod->id;
1425
1426 if (! add_mod_to_section($mod, $beforemod)) {
1427 return false;
1428 }
1429
1430 return true;
1431
1432}
1433
1434
11b0c469 1435
7c0f2984 1436function move_module($cm, $move) {
12905134 1437/// Moves an activity module up and down within the course
11b0c469 1438
1439 if (!$move) {
1440 return true;
1441 }
1442
11b0c469 1443 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1444 error("This course section doesn't exist");
1445 }
1446
1447 $mods = explode(",", $thissection->sequence);
1448
1449 $len = count($mods);
1450 $pos = array_keys($mods, $cm->id);
1451 $thepos = $pos[0];
1452
1453 if ($len == 0 || count($pos) == 0 ) {
1454 error("Very strange. Could not find the required module in this section.");
1455 }
1456
1457 if ($len == 1) {
1458 $first = true;
1459 $last = true;
1460 } else {
1461 $first = ($thepos == 0);
1462 $last = ($thepos == $len - 1);
1463 }
1464
1465 if ($move < 0) { // Moving the module up
1466
1467 if ($first) {
ad41694c 1468 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1469 return true;
ad41694c 1470
11b0c469 1471 } else { // Push onto end of previous section
1472 $prevsectionnumber = $thissection->section - 1;
89adb174 1473 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
9fa49e22 1474 "section", "$prevsectionnumber")) {
11b0c469 1475 error("Previous section ($prevsection->id) doesn't exist");
1476 }
1477
74666583 1478 if (!empty($prevsection->sequence)) {
11b0c469 1479 $newsequence = "$prevsection->sequence,$cm->id";
1480 } else {
1481 $newsequence = "$cm->id";
1482 }
1483
1484 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1485 error("Previous section could not be updated");
1486 }
1487
1488 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1489 error("Module could not be updated");
1490 }
1491
1492 array_splice($mods, 0, 1);
1493 $newsequence = implode(",", $mods);
1494 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1495 error("Module could not be updated");
1496 }
1497
1498 return true;
1499
1500 }
1501 } else { // move up within this section
1502 $swap = $mods[$thepos-1];
1503 $mods[$thepos-1] = $mods[$thepos];
1504 $mods[$thepos] = $swap;
89adb174 1505
11b0c469 1506 $newsequence = implode(",", $mods);
1507 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1508 error("This section could not be updated");
1509 }
1510 return true;
1511 }
1512
1513 } else { // Moving the module down
1514
1515 if ($last) {
1516 $nextsectionnumber = $thissection->section + 1;
89adb174 1517 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
9fa49e22 1518 "section", "$nextsectionnumber")) {
11b0c469 1519
74666583 1520 if (!empty($nextsection->sequence)) {
11b0c469 1521 $newsequence = "$cm->id,$nextsection->sequence";
1522 } else {
1523 $newsequence = "$cm->id";
1524 }
1525
1526 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1527 error("Next section could not be updated");
1528 }
1529
1530 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1531 error("Module could not be updated");
1532 }
1533
1534 array_splice($mods, $thepos, 1);
1535 $newsequence = implode(",", $mods);
1536 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1537 error("This section could not be updated");
1538 }
1539 return true;
1540
1541 } else { // There is no next section, so just return
1542 return true;
1543
1544 }
1545 } else { // move down within this section
1546 $swap = $mods[$thepos+1];
1547 $mods[$thepos+1] = $mods[$thepos];
1548 $mods[$thepos] = $swap;
89adb174 1549
11b0c469 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 }
1557}
1558
3d575e6f 1559function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1) {
dc0dc7d5 1560 global $CFG, $THEME;
94361e02 1561
3d575e6f 1562 static $str;
1563
1564 if (!isset($str)) {
1565 $str->delete = get_string("delete");
1566 $str->move = get_string("move");
1567 $str->moveup = get_string("moveup");
1568 $str->movedown = get_string("movedown");
1569 $str->moveright = get_string("moveright");
1570 $str->moveleft = get_string("moveleft");
1571 $str->update = get_string("update");
d33a2a6f 1572 $str->duplicate = get_string("duplicate");
3d575e6f 1573 $str->hide = get_string("hide");
1574 $str->show = get_string("show");
1575 $str->clicktochange = get_string("clicktochange");
32d03b7b 1576 $str->forcedmode = get_string("forcedmode");
3d575e6f 1577 $str->groupsnone = get_string("groupsnone");
1578 $str->groupsseparate = get_string("groupsseparate");
1579 $str->groupsvisible = get_string("groupsvisible");
1acfbce5 1580 }
94361e02 1581
1582 if ($absolute) {
dc0dc7d5 1583 $path = "$CFG->wwwroot/course";
1584 } else {
1585 $path = ".";
1586 }
1587
1588 if (empty($THEME->custompix)) {
1589 $pixpath = "$path/../pix";
94361e02 1590 } else {
dc0dc7d5 1591 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1592 }
1acfbce5 1593
3d575e6f 1594 if ($mod->visible) {
1595 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$mod->id\"><img".
7977cffd 1596 " src=\"$pixpath/t/hide.gif\" hspace=2 height=11 width=11 border=0></a> ";
1acfbce5 1597 } else {
3d575e6f 1598 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$mod->id\"><img".
7977cffd 1599 " src=\"$pixpath/t/show.gif\" hspace=2 height=11 width=11 border=0></a> ";
1600 }
3d575e6f 1601 if ($mod->groupmode !== false) {
1602 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1603 $grouptitle = $str->groupsseparate;
1604 $groupimage = "$pixpath/t/groups.gif";
1605 $grouplink = "$path/mod.php?id=$mod->id&groupmode=0";
3d575e6f 1606 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1607 $grouptitle = $str->groupsvisible;
1608 $groupimage = "$pixpath/t/groupv.gif";
1609 $grouplink = "$path/mod.php?id=$mod->id&groupmode=1";
1610 } else {
1611 $grouptitle = $str->groupsnone;
1612 $groupimage = "$pixpath/t/groupn.gif";
1613 $grouplink = "$path/mod.php?id=$mod->id&groupmode=2";
1614 }
1615 if ($mod->groupmodelink) {
1616 $groupmode = "<a title=\"$grouptitle ($str->clicktochange)\" href=\"$grouplink\">".
1617 "<img src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\"></a>";
3d575e6f 1618 } else {
32d03b7b 1619 $groupmode = "<img title=\"$grouptitle ($str->forcedmode)\" ".
1620 " src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\">";
3d575e6f 1621 }
1622 } else {
1623 $groupmode = "";
1624 }
7977cffd 1625
1626 if ($moveselect) {
3d575e6f 1627 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$mod->id\"><img".
bf1241ad 1628 " src=\"$pixpath/t/move.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\"></a>";
493486c4 1629 } else {
3d575e6f 1630 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$mod->id&move=-1\"><img".
bf1241ad 1631 " src=\"$pixpath/t/up.gif\" hspace=\"2\" height=11 width=11 border=0></a>".
3d575e6f 1632 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$mod->id&move=1\"><img".
bf1241ad 1633 " src=\"$pixpath/t/down.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
7977cffd 1634 }
1635
aac94fd0 1636 $leftright = "";
1637 if ($indent > 0) {
3d575e6f 1638 $leftright .= "<a title=\"$str->moveleft\" href=\"$path/mod.php?id=$mod->id&indent=-1\"><img".
bf1241ad 1639 " src=\"$pixpath/t/left.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
aac94fd0 1640 }
1641 if ($indent >= 0) {
3d575e6f 1642 $leftright .= "<a title=\"$str->moveright\" href=\"$path/mod.php?id=$mod->id&indent=1\"><img".
bf1241ad 1643 " src=\"$pixpath/t/right.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
aac94fd0 1644 }
1645
bf1241ad 1646 return "$leftright$move".
3d575e6f 1647 "<a title=\"$str->update\" href=\"$path/mod.php?update=$mod->id\"><img".
bf1241ad 1648 " src=\"$pixpath/t/edit.gif\" hspace=\"2\" height=11 width=11 border=0></a>".
d4bfbe4d 1649 // Following line is commented out until this feature is more definite -- martin
1650 // "<a title=\"$str->duplicate\" href=\"$path/mod.php?duplicate=$mod->id\"> 2 </a>".
3d575e6f 1651 "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$mod->id\"><img".
1652 " src=\"$pixpath/t/delete.gif\" hspace=\"2\" height=11 width=11 border=0></a>$hideshow$groupmode";
90845098 1653}
1654
f9903ed0 1655?>