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