Commented out debugging code
[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
0f87cb1d 15define('COURSE_MAX_RECENT_PERIOD', 86400); // A day, 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();
4581271a 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");
39 }
40
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 }
49
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)";
57 }
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;
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
f24cffb9 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
9481285b 376// It is assumed that $date is the GMT time of midnight for that day,
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
478 // This function trawls through the logs looking for
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
0f87cb1d 485
486 if (empty($USER->lastlogin)) {
487 echo '<p align="center"><font size="1">';
4b1371a7 488 print_string("welcometocourse", "", $course->shortname);
0f87cb1d 489 echo '</font></p>';
600149be 490 return;
3d891989 491 }
0f87cb1d 492
3d891989 493 $timestart = $USER->lastlogin;
b1992e65 494 $timemaxrecent = time() - COURSE_MAX_RECENT_PERIOD;
495 if ($timestart < $timemaxrecent) {
496 $timestart = $timemaxrecent;
600149be 497 }
498
0f87cb1d 499 echo '<center><font size="1">';
500 echo get_string("activitysince", "", userdate($timestart));
501
502 echo '<p><a href="recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a></p>';
503
504 echo '</font></center>';
505
600149be 506
507 // Firstly, have there been any new enrolments?
508
509 $heading = false;
510 $content = false;
1b5910c4 511
6c38b7e0 512 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 513
6c38b7e0 514 if ($users) {
e2b7251d 515 echo "<p>";
6c38b7e0 516 foreach ($users as $user) {
600149be 517 if (! $heading) {
4c654ee3 518 print_headline(get_string("newusers").":");
600149be 519 $heading = true;
520 $content = true;
521 }
2ac64806 522 $fullname = fullname($user, $isteacher);
523 echo "<font size=1><a href=\"../user/view.php?id=$user->id&course=$course->id\">$fullname</a></font><br />";
600149be 524 }
e2b7251d 525 echo "</p>";
600149be 526 }
527
1b5910c4 528 // Next, have there been any modifications to the course structure?
529
530 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
531 module = 'course' AND action LIKE '% mod'", "time ASC");
532
533 if ($logs) {
534 foreach ($logs as $key => $log) {
535 $info = split(" ", $log->info);
c9f6251e 536
537 if ($info[0] == "label") { // Labels are special activities
538 continue;
539 }
540
1b5910c4 541 $modname = get_field($info[0], "name", "id", $info[1]);
542 //Create a temp valid module structure (course,id)
543 $tempmod->course = $log->course;
544 $tempmod->id = $info[1];
545 //Obtain the visible property from the instance
546 $modvisible = instance_is_visible($info[0],$tempmod);
547
548 //Only if the mod is visible
549 if ($modvisible) {
550 switch ($log->action) {
551 case "add mod":
552 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
553 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
554 break;
555 case "update mod":
556 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
557 if (empty($changelist["$log->info"])) {
558 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<BR><A HREF=\"$CFG->wwwroot/course/$log->url\">$modname</A>");
559 }
560 break;
561 case "delete mod":
562 if (!empty($changelist["$log->info"]["operation"]) and
563 $changelist["$log->info"]["operation"] == "add") {
564 $changelist["$log->info"] = NULL;
565 } else {
566 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
567 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
568 }
569 break;
600149be 570 }
ef25340c 571 }
572 }
573 }
574
9c9f7d77 575 if (!empty($changelist)) {
ef25340c 576 foreach ($changelist as $changeinfo => $change) {
577 if ($change) {
578 $changes[$changeinfo] = $change;
579 }
580 }
8a59942e 581 if (isset($changes)){
582 if (count($changes) > 0) {
583 print_headline(get_string("courseupdates").":");
584 $content = true;
585 foreach ($changes as $changeinfo => $change) {
586 echo "<p><font size=1>".$change["text"]."</font></p>";
587 }
600149be 588 }
589 }
bf40f9c1 590 }
591
592
593 // If this site uses Library module, then print recent items
594 if (!empty($CFG->librarypath)) {
595 if (file_exists("$CFG->dirroot/$CFG->librarypath/librarylib.php")) {
596 include_once("$CFG->dirroot/$CFG->librarypath/librarylib.php");
597 if (librarysummarize(5, '', date('YmdHis',$USER->lastlogin))) {
598 $content = true;
599 }
600 }
600149be 601 }
602
3869a2ac 603 // Now display new things from each module
600149be 604
0fd7da81 605 $mods = get_records("modules", "visible", "1", "name", "id, name");
606
1b5910c4 607 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
0fd7da81 608 include_once("$CFG->dirroot/mod/$mod->name/lib.php");
609 $print_recent_activity = $mod->name."_print_recent_activity";
1b5910c4 610 if (function_exists($print_recent_activity)) {
611 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
3869a2ac 612 if ($modcontent) {
613 $content = true;
600149be 614 }
600149be 615 }
616 }
617
618 if (! $content) {
3d891989 619 echo "<font size=2>".get_string("nothingnew")."</font>";
600149be 620 }
600149be 621}
622
e1360728 623
d897cae4 624function get_array_of_activities($courseid) {
625// For a given course, returns an array of course activity objects
626// Each item in the array contains he following properties:
627// cm - course module id
628// mod - name of the module (eg forum)
629// section - the number of the section (eg week or topic)
630// name - the name of the instance
5867bfb5 631// visible - is the instance visible or not
86aa7ccf 632// extra - contains extra string to include in any link
d897cae4 633
8dddba42 634 global $CFG;
635
d897cae4 636 $mod = array();
637
9fa49e22 638 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 639 return NULL;
640 }
641
642 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
643 foreach ($sections as $section) {
74666583 644 if (!empty($section->sequence)) {
d897cae4 645 $sequence = explode(",", $section->sequence);
646 foreach ($sequence as $seq) {
7af6281f 647 if (empty($rawmods[$seq])) {
648 continue;
649 }
d897cae4 650 $mod[$seq]->cm = $rawmods[$seq]->id;
651 $mod[$seq]->mod = $rawmods[$seq]->modname;
652 $mod[$seq]->section = $section->section;
653 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 654 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 655 $mod[$seq]->extra = "";
8dddba42 656
657 $modname = $mod[$seq]->mod;
658 $functionname = $modname."_get_coursemodule_info";
659
660 include_once("$CFG->dirroot/mod/$modname/lib.php");
661
662 if (function_exists($functionname)) {
663 if ($extra = $functionname($rawmods[$seq])) {
664 $mod[$seq]->extra = $extra;
c9f6251e 665 }
666 }
d897cae4 667 }
668 }
669 }
670 }
671 return $mod;
672}
673
674
675
e1360728 676
90845098 677function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
678// Returns a number of useful structures for course displays
7468bf01 679
90845098 680 $mods = NULL; // course modules indexed by id
681 $modnames = NULL; // all course module names
94361e02 682 $modnamesplural= NULL; // all course module names (plural form)
90845098 683 $modnamesused = NULL; // course module names used
7468bf01 684
9fa49e22 685 if ($allmods = get_records("modules")) {
90845098 686 foreach ($allmods as $mod) {
5867bfb5 687 if ($mod->visible) {
688 $modnames[$mod->name] = get_string("modulename", "$mod->name");
689 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
690 }
90845098 691 }
692 asort($modnames);
693 } else {
694 error("No modules are installed!");
695 }
696
9fa49e22 697 if ($rawmods = get_course_mods($courseid)) {
7468bf01 698 foreach($rawmods as $mod) { // Index the mods
699 $mods[$mod->id] = $mod;
90845098 700 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
1acfbce5 701 if ($mod->visible or isteacher($courseid)) {
702 $modnamesused[$mod->modname] = $modnames[$mod->modname];
703 }
7468bf01 704 }
c7da6f7a 705 if ($modnamesused) {
706 asort($modnamesused);
707 }
7468bf01 708 }
7468bf01 709}
710
9fa49e22 711
7468bf01 712function get_all_sections($courseid) {
713
d26d7ed0 714 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 715 "section, id, course, summary, sequence, visible");
7468bf01 716}
717
b86fc0e2 718function course_set_display($courseid, $display=0) {
719 global $USER;
720
721 if (empty($USER)) {
722 return false;
723 }
724
725 if ($display == "all" or empty($display)) {
726 $display = 0;
727 }
728
729 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
730 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
731 } else {
732 $record->userid = $USER->id;
733 $record->course = $courseid;
734 $record->display = $display;
735 if (!insert_record("course_display", $record)) {
736 notify("Could not save your course display!");
737 }
738 }
739
740 return $USER->display[$courseid] = $display; // Note: = not ==
741}
742
7d99d695 743function set_section_visible($courseid, $sectionnumber, $visibility) {
744/// For a given course section, markes it visible or hidden,
745/// and does the same for every activity in that section
746
747 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
748 set_field("course_sections", "visible", "$visibility", "id", $section->id);
749 if (!empty($section->sequence)) {
750 $modules = explode(",", $section->sequence);
751 foreach ($modules as $moduleid) {
752 set_field("course_modules", "visible", "$visibility", "id", $moduleid);
753 }
754 }
5867bfb5 755 rebuild_course_cache($courseid);
7d99d695 756 }
757}
ba2e5d73 758
5e367a2d 759function print_section_block($heading, $course, $section, $mods, $modnames, $modnamesused,
52dcc2f9 760 $absolute=true, $width="100%") {
5e367a2d 761
2c8cb30c 762 global $CFG, $USER, $THEME;
3d575e6f 763 static $groupbuttons;
52dcc2f9 764 static $isteacher;
765 static $isediting;
2c8cb30c 766 static $ismoving;
767 static $strmovehere;
768 static $strmovefull;
3d575e6f 769 static $strcancel;
770 static $stractivityclipboard;
52dcc2f9 771
772 if (!isset($isteacher)) {
3d575e6f 773 $groupbuttons = $course->groupmode and !$course->groupmodeforce;
52dcc2f9 774 $isteacher = isteacher($course->id);
52dcc2f9 775 $isediting = isediting($course->id);
2c8cb30c 776 $ismoving = ismoving($course->id);
3d575e6f 777 if ($ismoving) {
778 $strmovehere = get_string("movehere");
779 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
780 $strcancel= get_string("cancel");
781 $stractivityclipboard = $USER->activitycopyname;
782 }
2c8cb30c 783 }
5e367a2d 784
785 $modinfo = unserialize($course->modinfo);
786 $moddata = array();
787 $modicon = array();
788 $editbuttons = "";
789
3d575e6f 790 if ($ismoving) {
791 $modicon[] = "&nbsp;<img align=bottom src=\"$CFG->pixpath/t/move.gif\" height=\"11\" width=\"11\">";
792 $moddata[] = "$USER->activitycopyname&nbsp;(<a href=\"$CFG->wwwroot/course/mod.php?cancelcopy=true\">$strcancel</a>)";
793 }
794
74666583 795 if (!empty($section->sequence)) {
5e367a2d 796
797 $sectionmods = explode(",", $section->sequence);
798
2c8cb30c 799
5e367a2d 800 foreach ($sectionmods as $modnumber) {
52dcc2f9 801 if (empty($mods[$modnumber])) {
802 continue;
803 }
5e367a2d 804 $mod = $mods[$modnumber];
2c8cb30c 805 if ($isediting and !$ismoving) {
3d575e6f 806 if (!$groupbuttons) {
807 $mod->groupmode = false;
808 }
809 $editbuttons = "<br />".make_editing_buttons($mod, $absolute, true);
2c8cb30c 810 } else {
811 $editbuttons = "";
1acfbce5 812 }
52dcc2f9 813 if ($mod->visible or $isteacher) {
2c8cb30c 814 if ($ismoving) {
815 if ($mod->id == $USER->activitycopy) {
816 continue;
817 }
818 $modicon[] = "";
6951a894 819 $moddata[] = "<a title=\"$strmovefull\"".
820 " href=\"$CFG->wwwroot/course/mod.php?moveto=$mod->id\">".
821 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
822 " alt=\"$strmovehere\" border=\"0\"></a>";
2c8cb30c 823 }
1acfbce5 824 $instancename = urldecode($modinfo[$modnumber]->name);
3c89f9a8 825 if (!empty($CFG->filterall)) {
b346b424 826 $instancename = filter_text("<nolink>$instancename</nolink>", $course->id);
3c89f9a8 827 }
c2cb4545 828 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
3ee3e01a 829 if (!empty($modinfo[$modnumber]->extra)) {
830 $extra = urldecode($modinfo[$modnumber]->extra);
831 } else {
832 $extra = "";
833 }
7977cffd 834
c9f6251e 835 if ($mod->modname == "label") {
836 $modicon[] = "";
837 $moddata[] = format_text($extra, FORMAT_HTML).$editbuttons;
838 } else {
e845dee2 839 $modicon[] = "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\"".
c9f6251e 840 " height=\"16\" width=\"16\" alt=\"$mod->modfullname\">";
841 $moddata[] = "<a title=\"$mod->modfullname\" $linkcss $extra".
842 "href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a>".
843 "$editbuttons";
844 }
5e367a2d 845 }
5e367a2d 846 }
847 }
2c8cb30c 848 if ($ismoving) {
849 $modicon[] = "";
6951a894 850 $moddata[] = "<a title=\"$strmovefull\"".
851 " href=\"$CFG->wwwroot/course/mod.php?movetosection=$section->id\">".
852 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
853 " alt=\"$strmovehere\" border=\"0\"></a>";
2c8cb30c 854 }
a44d18e7 855 if ($isediting) {
15ac9065 856 $editmenu = popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section->section&add=",
5e367a2d 857 $modnames, "section0", "", get_string("add")."...", "mods", get_string("activities"), true);
dfec7b01 858 $editmenu = "<div align=right>$editmenu</div>";
47f1da80 859 } else {
860 $editmenu = "";
5e367a2d 861 }
862
863 print_side_block($heading, "", $moddata, $modicon, $editmenu, $width);
864}
865
866
d897cae4 867function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 868/// Prints a section full of activity modules
7977cffd 869 global $CFG, $USER;
870
3d575e6f 871 static $groupbuttons;
32d03b7b 872 static $groupbuttonslink;
52dcc2f9 873 static $isteacher;
874 static $isediting;
7977cffd 875 static $ismoving;
876 static $strmovehere;
877 static $strmovefull;
52dcc2f9 878
110a32e2 879
52dcc2f9 880 if (!isset($isteacher)) {
32d03b7b 881 $groupbuttons = $course->groupmode;
882 $groupbuttonslink = (!$course->groupmodeforce);
52dcc2f9 883 $isteacher = isteacher($course->id);
52dcc2f9 884 $isediting = isediting($course->id);
7977cffd 885 $ismoving = ismoving($course->id);
3d575e6f 886 if ($ismoving) {
887 $strmovehere = get_string("movehere");
888 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
889 }
7977cffd 890 }
94361e02 891
c408b0c4 892 $modinfo = unserialize($course->modinfo);
94361e02 893
567df742 894 echo "<table width=\"$width\" cellpadding=1 cellspacing=0>\n";
74666583 895 if (!empty($section->sequence)) {
94361e02 896
897 $sectionmods = explode(",", $section->sequence);
898
899 foreach ($sectionmods as $modnumber) {
9ae687af 900 if (empty($mods[$modnumber])) {
901 continue;
902 }
94361e02 903 $mod = $mods[$modnumber];
c9f6251e 904
52dcc2f9 905 if ($mod->visible or $isteacher) {
c9f6251e 906 echo "<tr><td class=\"activity$mod->modname\">";
7977cffd 907 if ($ismoving) {
908 if ($mod->id == $USER->activitycopy) {
909 continue;
910 }
6951a894 911 echo "<a title=\"$strmovefull\"".
912 " href=\"mod.php?moveto=$mod->id\">".
913 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
914 " alt=\"$strmovehere\" border=\"0\"></a><br />\n";
1acfbce5 915 }
7977cffd 916 $instancename = urldecode($modinfo[$modnumber]->name);
3c89f9a8 917 if (!empty($CFG->filterall)) {
b346b424 918 $instancename = filter_text("<nolink>$instancename</nolink>", $course->id);
3c89f9a8 919 }
c9f6251e 920
86aa7ccf 921 if (!empty($modinfo[$modnumber]->extra)) {
922 $extra = urldecode($modinfo[$modnumber]->extra);
923 } else {
924 $extra = "";
925 }
c9f6251e 926
aac94fd0 927 if ($mod->indent) {
928 print_spacer(12, 20 * $mod->indent, false);
929 }
930
c9f6251e 931 if ($mod->modname == "label") {
aac94fd0 932 if (!$mod->visible) {
933 echo "<span class=\"dimmed_text\">";
934 }
c9f6251e 935 echo format_text($extra, FORMAT_HTML);
aac94fd0 936 if (!$mod->visible) {
937 echo "</span>";
938 }
c9f6251e 939
940 } else { // Normal activity
941 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
e845dee2 942 echo "<img src=\"$CFG->modpixpath/$mod->modname/icon.gif\"".
c9f6251e 943 " height=16 width=16 alt=\"$mod->modfullname\">".
944 " <font size=2><a title=\"$mod->modfullname\" $linkcss $extra".
945 " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a></font>";
946 }
c9f6251e 947 if ($isediting) {
32d03b7b 948 if ($groupbuttons) {
949 if (! $mod->groupmodelink = $groupbuttonslink) {
950 $mod->groupmode = $course->groupmode;
951 }
952
953 } else {
3d575e6f 954 $mod->groupmode = false;
955 }
ffc69cd3 956 echo "&nbsp;&nbsp;";
3d575e6f 957 echo make_editing_buttons($mod, $absolute, true, $mod->indent);
c9f6251e 958 }
ffc69cd3 959 echo "</td>";
c9f6251e 960 echo "</tr>";
94361e02 961 }
94361e02 962 }
963 }
7977cffd 964 if ($ismoving) {
6951a894 965 echo "<tr><td><a title=\"$strmovefull\"".
966 " href=\"mod.php?movetosection=$section->id\">".
967 "<img height=\"16\" width=\"80\" src=\"$CFG->pixpath/movehere.gif\" ".
968 " alt=\"$strmovehere\" border=\"0\"></a></td></tr>\n";
7977cffd 969 }
c9f6251e 970 echo "</table>\n\n";
a7ad3ea6 971}
972
5867bfb5 973
974function rebuild_course_cache($courseid=0) {
975// Rebuilds the cached list of course activities stored in the database
976// If a courseid is not specified, then all are rebuilt
977
978 if ($courseid) {
979 $select = "id = '$courseid'";
980 } else {
981 $select = "";
982 }
983
984 if ($courses = get_records_select("course", $select)) {
985 foreach ($courses as $course) {
986 $modinfo = serialize(get_array_of_activities($course->id));
987 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
988 notify("Could not cache module information for course '$course->fullname'!");
989 }
990 }
991 }
992}
993
994
7541bc3e 995function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 996 global $THEME;
997
7541bc3e 998 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
999 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 1000 echo stripslashes($heading);
1001 echo "</td></tr></table>";
5e367a2d 1002}
1003
1004function print_side_block($heading="", $content="", $list=NULL, $icons=NULL, $footer="", $width=180) {
1005// Prints a nice side block with an optional header. The content can either
1006// be a block of HTML or a list of text with optional icons.
a7ad3ea6 1007
5e367a2d 1008 global $THEME;
1009
7541bc3e 1010 print_side_block_start($heading, $width);
1011
5e367a2d 1012 if ($content) {
7541bc3e 1013 echo "$content";
5e367a2d 1014 } else {
5e367a2d 1015 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
5fef34d7 1016 if ($list) {
1017 foreach ($list as $key => $string) {
1018 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
1019 if ($icons) {
1020 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">".$icons[$key]."</td>";
1021 }
1022 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"*\"><font size=\"-1\">$string</font></td>";
1023 echo "</tr>";
a7ad3ea6 1024 }
a7ad3ea6 1025 }
5e367a2d 1026 if ($footer) {
7541bc3e 1027 echo "<tr bgcolor=\"$THEME->cellcontent2\">";
5e367a2d 1028 if ($icons) {
7541bc3e 1029 echo "<td class=\"sideblocklinks\" valign=\"top\" width=\"16\">&nbsp;</td>";
5e367a2d 1030 }
7541bc3e 1031 echo "<td class=\"sideblocklinks\"><font size=\"-1\">$footer</td>";
5e367a2d 1032 echo "</tr>";
1033 }
1034 echo "</table>";
a7ad3ea6 1035 }
5e367a2d 1036
7541bc3e 1037 print_side_block_end();
1038}
1039
1040function print_side_block_start($heading="", $width=180, $class="sideblockmain") {
1041// Starts a nice side block with an optional header.
1042
1043 global $THEME;
1044
1045 echo "<table class=\"sideblock\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
1046 if ($heading) {
1047 echo "<tr>";
1048 echo "<td class=\"sideblockheading\" bgcolor=\"$THEME->cellheading\">$heading</td>";
1049 echo "</tr>";
1050 }
1051 echo "<tr>";
1052 echo "<td class=\"$class\" bgcolor=\"$THEME->cellcontent2\">";
1053}
1054
1055function print_side_block_end() {
1056 echo "</td></tr>";
7d99d695 1057 echo "</table><br />";
94361e02 1058}
1059
5e367a2d 1060
670fddf1 1061function print_admin_links ($siteid, $width=180) {
dc0dc7d5 1062 global $CFG, $THEME;
2b25f2a0 1063
440eb0d7 1064 if (isadmin()) {
0d0e550f 1065 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/configure.php\">".get_string("configuration")."</a>...";
1066 $modicon[]="<img src=\"$CFG->pixpath/i/admin.gif\" height=16 width=16 alt=\"\" />";
2f4d324b 1067
0d0e550f 1068 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">".get_string("users")."</a>...";
1069 $modicon[]="<img src=\"$CFG->pixpath/i/users.gif\" height=16 width=16 alt=\"\" />";
440eb0d7 1070 }
2f4d324b 1071
55e4b5f9 1072 if (iscreator()) {
0d0e550f 1073 $moddata[]="<a href=\"$CFG->wwwroot/course/index.php?edit=on\">".get_string("courses")."</a>";
1074 $modicon[]="<img src=\"$CFG->pixpath/i/course.gif\" height=16 width=16 alt=\"\" />";
55e4b5f9 1075 $fulladmin = "";
1076 }
2f4d324b 1077
1924074c 1078 if (isadmin()) {
0d0e550f 1079 $moddata[]="<a href=\"$CFG->wwwroot/course/log.php?id=$siteid\">".get_string("logs")."</a>";
1080 $modicon[]="<img src=\"$CFG->pixpath/i/log.gif\" height=16 width=16 alt=\"\" />";
2f4d324b 1081
0d0e550f 1082 $moddata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$siteid\">".get_string("sitefiles")."</a>";
1083 $modicon[]="<img src=\"$CFG->pixpath/i/files.gif\" height=16 width=16 alt=\"\" />";
2f4d324b 1084
0d0e550f 1085 if (file_exists("$CFG->dirroot/$CFG->admin/$CFG->dbtype")) {
2f4d324b 1086 $moddata[]="<a href=\"$CFG->wwwroot/$CFG->admin/$CFG->dbtype/frame.php\">".
1087 get_string("managedatabase")."</a>";
0d0e550f 1088 $modicon[]="<img src=\"$CFG->pixpath/i/db.gif\" height=16 width=16 alt=\"\" />";
1089 }
5867bfb5 1090 $fulladmin = "<p><a href=\"$CFG->wwwroot/$CFG->admin/\">".get_string("admin")."</a>...";
1924074c 1091 }
5e367a2d 1092
1093 print_side_block(get_string("administration"), "", $moddata, $modicon, $fulladmin, $width);
1094
dc0dc7d5 1095 echo "<img src=\"$CFG->wwwroot/pix/spacer.gif\" width=\"$width\" height=1><br>";
2b25f2a0 1096}
1097
b4d7002e 1098function print_course_admin_links($course, $width=180) {
dc0dc7d5 1099 global $USER, $CFG, $THEME;
44dad735 1100
fce1ce13 1101 if (isguest()) {
1102 return true;
1103 }
eef16119 1104
13469b82 1105 if (isteacher($course->id)) {
eef16119 1106
1107 $isteacheredit = isteacheredit($course->id);
1108
1109 if ($isteacheredit) {
c9f6251e 1110 $adminicon[]="<img src=\"$CFG->pixpath/i/edit.gif\" height=16 width=16 alt=\"\">";
02ebf404 1111 if (isediting($course->id)) {
1112 $admindata[]="<a href=\"view.php?id=$course->id&edit=off\">".get_string("turneditingoff")."</a>";
1113 } else {
1114 $admindata[]="<a href=\"view.php?id=$course->id&edit=on\">".get_string("turneditingon")."</a>";
1115 }
1116 $admindata[]="<a href=\"edit.php?id=$course->id\">".get_string("settings")."...</a>";
c9f6251e 1117 $adminicon[]="<img src=\"$CFG->pixpath/i/settings.gif\" height=16 width=16 alt=\"\">";
3b1bb969 1118
1119 if (iscreator() or !empty($CFG->teacherassignteachers)) {
1120 if (!$course->teachers) {
1121 $course->teachers = get_string("defaultcourseteachers");
1122 }
1123 $admindata[]="<a href=\"teacher.php?id=$course->id\">$course->teachers...</a>";
c9f6251e 1124 $adminicon[]="<img src=\"$CFG->pixpath/i/users.gif\" height=16 width=16 alt=\"\">";
02ebf404 1125 }
02ebf404 1126
dd050b33 1127 if (!$course->students) {
1128 $course->students = get_string("defaultcoursestudents");
1129 }
1130 $admindata[]="<a href=\"student.php?id=$course->id\">$course->students...</a>";
1131 $adminicon[]="<img src=\"$CFG->pixpath/i/users.gif\" height=16 width=16 alt=\"\">";
7d569d28 1132
02ebf404 1133 $admindata[]="<a href=\"$CFG->wwwroot/backup/backup.php?id=$course->id\">".get_string("backup")."...</a>";
c9f6251e 1134 $adminicon[]="<img src=\"$CFG->pixpath/i/backup.gif\" height=16 width=16 alt=\"\">";
02ebf404 1135
07bc2435 1136 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id&wdir=/backupdata\">".get_string("restore")."...</a>";
c9f6251e 1137 $adminicon[]="<img src=\"$CFG->pixpath/i/restore.gif\" height=16 width=16 alt=\"\">";
02ebf404 1138 $admindata[]="<a href=\"scales.php?id=$course->id\">".get_string("scales")."...</a>";
c9f6251e 1139 $adminicon[]="<img src=\"$CFG->pixpath/i/scales.gif\" height=16 width=16 alt=\"\">";
13469b82 1140 }
13469b82 1141
dc0dc7d5 1142 $admindata[]="<a href=\"grades.php?id=$course->id\">".get_string("grades")."...</a>";
c9f6251e 1143 $adminicon[]="<img src=\"$CFG->pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
13469b82 1144
dc0dc7d5 1145 $admindata[]="<a href=\"log.php?id=$course->id\">".get_string("logs")."...</a>";
c9f6251e 1146 $adminicon[]="<img src=\"$CFG->pixpath/i/log.gif\" height=16 width=16 alt=\"\">";
dc0dc7d5 1147
eef16119 1148 if ($isteacheredit) {
1149 $admindata[]="<a href=\"$CFG->wwwroot/files/index.php?id=$course->id\">".get_string("files")."...</a>";
1150 $adminicon[]="<img src=\"$CFG->pixpath/i/files.gif\" height=16 width=16 alt=\"\">";
1151 }
f354106d 1152
dc0dc7d5 1153 $admindata[]="<a href=\"$CFG->wwwroot/doc/view.php?id=$course->id&file=teacher.html\">".get_string("help")."...</a>";
c9f6251e 1154 $adminicon[]="<img src=\"$CFG->modpixpath/resource/icon.gif\" height=16 width=16 alt=\"\">";
b4d7002e 1155
13469b82 1156 if ($teacherforum = forum_get_course_forum($course->id, "teacher")) {
450c3d39 1157 $admindata[]="<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$teacherforum->id\">".get_string("nameteacher","forum")."</a>";
c9f6251e 1158 $adminicon[]="<img src=\"$CFG->modpixpath/forum/icon.gif\" height=16 width=16 alt=\"\">";
13469b82 1159 }
f354106d 1160
e550e2d6 1161 } else if (!isguest()) { // Students menu
5b8540e0 1162 if ($course->showgrades) {
fff79722 1163 $admindata[]="<a href=\"grade.php?id=$course->id\">".get_string("grades")."...</a>";
c9f6251e 1164 $adminicon[]="<img src=\"$CFG->pixpath/i/grades.gif\" height=16 width=16 alt=\"\">";
fff79722 1165 }
3f125001 1166 if ($course->showreports) {
1167 $admindata[]="<a href=\"user.php?id=$course->id&user=$USER->id\">".get_string("activityreport")."...</a>";
1168 $adminicon[]="<img src=\"$CFG->pixpath/i/report.gif\" height=16 width=16 alt=\"\">";
1169 }
4fc2eb7a 1170 if (is_internal_auth()) {
e550e2d6 1171 $admindata[]="<a href=\"$CFG->wwwroot/login/change_password.php?id=$course->id\">".
1172 get_string("changepassword")."...</a>";
c9f6251e 1173 $adminicon[]="<img src=\"$CFG->pixpath/i/user.gif\" height=16 width=16 alt=\"\">";
e550e2d6 1174 } else if ($CFG->changepassword) {
1175 $admindata[]="<a href=\"$CFG->changepassword\">".get_string("changepassword")."...</a>";
c9f6251e 1176 $adminicon[]="<img src=\"$CFG->pixpath/i/user.gif\" height=16 width=16 alt=\"\">";
e550e2d6 1177 }
5204d831 1178 if ($CFG->allowunenroll) {
1179 $admindata[]="<a href=\"unenrol.php?id=$course->id\">".get_string("unenrolme", "", $course->shortname)."...</a>";
3f125001 1180 $adminicon[]="<img src=\"$CFG->pixpath/i/user.gif\" height=16 width=16 alt=\"\">";
5204d831 1181 }
13469b82 1182 }
44dad735 1183
f354106d 1184 if (!empty($admindata)) {
1185 print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width);
1186 }
44dad735 1187}
2b25f2a0 1188
c2cb4545 1189
1190function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
1191/// Given an empty array, this function recursively travels the
1192/// categories, building up a nice list for display. It also makes
1193/// an array that list all the parents for each category.
1194
1195 if ($category) {
1196 if ($path) {
1197 $path = "$path / $category->name";
1198 } else {
1199 $path = "$category->name";
1200 }
1201 $list[$category->id] = $path;
1202 } else {
1203 $category->id = 0;
1204 }
1205
1206 if ($categories = get_categories("$category->id")) { // Print all the children recursively
1207 foreach ($categories as $cat) {
1208 if (!empty($category->id)) {
3bd4de22 1209 if (isset($parents[$category->id])) {
2832badf 1210 $parents[$cat->id] = $parents[$category->id];
1211 }
c2cb4545 1212 $parents[$cat->id][] = $category->id;
1213 }
e92fe848 1214 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1215 }
1216 }
1217}
1218
1219
c2cb4545 1220function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
1221/// Recursive function to print out all the categories in a nice format
1222/// with or without courses included
9ff5310a 1223 global $CFG;
1224 if (isset($CFG->max_category_depth)&&($depth >= $CFG->max_category_depth)) {
1225 return;
1226 }
c2cb4545 1227
1228 if (!$displaylist) {
e92fe848 1229 make_categories_list($displaylist, $parentslist);
c2cb4545 1230 }
1231
1232 if ($category) {
3af6e1db 1233 if ($category->visible or iscreator()) {
d2b6ba70 1234 print_category_info($category, $depth);
c2cb4545 1235 } else {
1236 return; // Don't bother printing children of invisible categories
1237 }
1238
1239 } else {
c2cb4545 1240 $category->id = "0";
1241 }
1242
1243 if ($categories = get_categories($category->id)) { // Print all the children recursively
1244 $countcats = count($categories);
1245 $count = 0;
1246 $first = true;
1247 $last = false;
1248 foreach ($categories as $cat) {
1249 $count++;
1250 if ($count == $countcats) {
1251 $last = true;
1252 }
1253 $up = $first ? false : true;
1254 $down = $last ? false : true;
1255 $first = false;
1256
1257 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
1258 }
1259 }
c2cb4545 1260}
1261
1262
d2b6ba70 1263function print_category_info($category, $depth) {
1264/// Prints the category info in indented fashion
1265/// This function is only used by print_whole_category_list() above
c2cb4545 1266
1267 global $CFG;
b48f834c 1268 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1269
b48f834c 1270 if (empty($strsummary)) {
1271 $strallowguests = get_string("allowguests");
1272 $strrequireskey = get_string("requireskey");
1273 $strsummary = get_string("summary");
1274 }
ba2e5d73 1275
d5f26b07 1276 $catlinkcss = $category->visible ? "" : " class=\"dimmed\" ";
1277
8ef9cb56 1278 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
c9f6251e 1279 $catimage = "<img src=\"$CFG->pixpath/i/course.gif\" width=16 height=16 border=0>";
b48f834c 1280 } else {
1281 $catimage = "&nbsp";
8ef9cb56 1282 }
b48f834c 1283
1284 echo "\n\n<table border=0 cellpadding=3 cellspacing=0 width=\"100%\"><tr>";
d2b6ba70 1285
c2cb4545 1286 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
b48f834c 1287 $courses = get_courses($category->id);
1288
1289 echo "<tr>";
1290
1291 if ($depth) {
1292 $indent = $depth*30;
1293 $rows = count($courses) + 1;
1294 echo "<td rowspan=\"$rows\" valign=\"top\" width=\"$indent\">";
1295 print_spacer(10, $indent);
1296 echo "</td>";
1297 }
1298
1299 echo "<td valign=\"top\">$catimage</td>";
cb29b020 1300 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1301 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1302 echo "</td>";
1303 echo "<td class=\"categoryname\">&nbsp;</td>";
b48f834c 1304 echo "</tr>\n";
1305
9ff5310a 1306 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1307 foreach ($courses as $course) {
1308 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
b48f834c 1309 echo "<tr><td valign=\"top\" width=\"30\">&nbsp;";
cb29b020 1310 echo "</td>\n<td valign=\"top\" width=\"100%\" class=\"coursename\">";
a6870f0c 1311 echo "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
cb29b020 1312 echo "</td>\n<td align=\"right\" valign=\"top\" nowrap class=\"coursename\">";
c2cb4545 1313 if ($course->guest ) {
1314 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
9928b09e 1315 echo "<img hspace=1 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>";
ebe8ddc1 1316 } else {
c9f6251e 1317 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1318 }
c2cb4545 1319 if ($course->password) {
1320 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
9928b09e 1321 echo "<img hspace=1 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
ebe8ddc1 1322 } else {
c9f6251e 1323 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
b48f834c 1324 }
1325 if ($course->summary) {
1326 link_to_popup_window ("/course/info.php?id=$course->id", "courseinfo",
9928b09e 1327 "<img hspace=1 alt=\"$strsummary\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/info.gif\">",
b48f834c 1328 400, 500, $strsummary);
ebe8ddc1 1329 } else {
c9f6251e 1330 echo "<img alt=\"\" height=16 width=18 border=0 src=\"$CFG->pixpath/spacer.gif\">";
0c656181 1331 }
b48f834c 1332 echo "</td></tr>\n";
0c656181 1333 }
ba2e5d73 1334 }
d2b6ba70 1335 } else {
b48f834c 1336
1337 if ($depth) {
1338 $indent = $depth*20;
1339 echo "<td valign=\"top\" width=\"$indent\">";
1340 print_spacer(10, $indent);
1341 echo "</td>";
d2b6ba70 1342 }
b48f834c 1343
cb29b020 1344 echo "<td valign=\"top\" width=\"100%\" class=\"categoryname\">";
b48f834c 1345 echo "<a $catlinkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
cb29b020 1346 echo "</td>";
a0456978 1347 echo "<td valign=\"top\" class=\"categorynumber\">$category->coursecount</td></tr>";
c2cb4545 1348 }
b48f834c 1349 echo "\n</table>\n";
c2cb4545 1350}
1351
1352function print_courses_sideblock($category=0, $width="100%") {
f3120024 1353 global $CFG, $THEME, $USER;
c2cb4545 1354
1355 if (empty($THEME->custompix)) {
1356 $icon = "<img src=\"$CFG->wwwroot/pix/i/course.gif\"".
1357 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
1358 } else {
1359 $icon = "<img src=\"$CFG->wwwroot/theme/$CFG->theme/pix/i/course.gif\"".
1360 " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">";
1361 }
1362
f3120024 1363 if (isset($USER->id) and !isadmin()) { // Just print My Courses
1364 if ($courses = get_my_courses($USER->id)) {
1365 foreach ($courses as $course) {
c81696e5 1366 if (!$course->category) {
1367 continue;
1368 }
d1d74247 1369 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
f3120024 1370 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
1371 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
1372 $modicon[]=$icon;
1373 }
5dc3d80e 1374 $fulllist = "<p><a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
f3120024 1375 print_side_block( get_string("mycourses"), "", $moddata, $modicon, $fulllist, $width);
1376 return;
1377 }
1378 }
1379
c571f3fc 1380 $categories = get_categories("0"); // Parent = 0 ie top-level categories only
c2cb4545 1381 if (count($categories) > 1) { // Just print top level category links
1382 foreach ($categories as $category) {
1383 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
d2b6ba70 1384 $moddata[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a>";
c2cb4545 1385 $modicon[]=$icon;
0c656181 1386 }
b5333476 1387 $fulllist = "<p><a href=\"$CFG->wwwroot/course/\">".get_string("searchcourses")."</a>...";
352d94cf 1388 $blocktitle = get_string("categories");
c2cb4545 1389 } else { // Just print course names of single category
90c2ca2e 1390 $category = array_shift($categories);
d2b6ba70 1391 $courses = get_courses($category->id);
1392
c2cb4545 1393 if ($courses) {
1394 foreach ($courses as $course) {
1395 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1396 $moddata[]="<a $linkcss title=\"$course->shortname\" ".
1397 "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a>";
1398 $modicon[]=$icon;
1399 }
5dc3d80e 1400 $fulllist = "<p><a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a>...";
c2cb4545 1401 } else {
1402 $moddata = array();
1403 $modicon = array();
1404 $fulllist = get_string("nocoursesyet");
0c656181 1405 }
352d94cf 1406 $blocktitle = get_string("courses");
ba2e5d73 1407 }
c2cb4545 1408
352d94cf 1409 print_side_block($blocktitle, "", $moddata, $modicon, $fulllist, $width);
ba2e5d73 1410}
94361e02 1411
c2cb4545 1412
1413function print_courses($category, $width="100%") {
1414/// Category is 0 (for all courses) or an object
1415
1416 global $CFG, $THEME;
1417
1418 if (empty($category)) {
90c2ca2e 1419 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1420 if (count($categories) == 1) {
1421 $category = array_shift($categories);
d2b6ba70 1422 $courses = get_courses($category->id);
90c2ca2e 1423 } else {
d2b6ba70 1424 $courses = get_courses("all");
90c2ca2e 1425 }
1426 unset($categories);
607809b3 1427 } else {
c2cb4545 1428 $categories = get_categories($category->id); // sub categories
d2b6ba70 1429 $courses = get_courses($category->id);
c2cb4545 1430 }
1431
c2cb4545 1432 if ($courses) {
1433 foreach ($courses as $course) {
1434 print_course($course, $width);
1435 echo "<br />\n";
1436 }
1437 } else {
1438 print_heading(get_string("nocoursesyet"));
1439 }
1440
1441}
1442
1443
1444function print_course($course, $width="100%") {
1445
1446 global $CFG, $THEME;
1447
1448 if (! $site = get_site()) {
1449 error("Could not find a site!");
1450 }
1451
1b6a4b1d 1452 print_simple_box_start("center", "$width", $THEME->cellcontent, 5, "coursebox");
c2cb4545 1453
22288704 1454 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1455
c2cb4545 1456 echo "<table width=\"100%\">";
1457 echo "<tr valign=top>";
1b6a4b1d 1458 echo "<td valign=top width=\"50%\" class=\"courseboxinfo\">";
c2cb4545 1459 echo "<p><font size=3><b><a title=\"".get_string("entercourse")."\"
22288704 1460 $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname</a></b></font></p>";
c2cb4545 1461 if ($teachers = get_course_teachers($course->id)) {
1462 echo "<p><font size=\"1\">\n";
1463 foreach ($teachers as $teacher) {
1464 if ($teacher->authority > 0) {
1465 if (!$teacher->role) {
1466 $teacher->role = $course->teacher;
1467 }
2ac64806 1468 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
1469 echo "$teacher->role: <a href=\"$CFG->wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$fullname</a><br />";
c2cb4545 1470 }
1471 }
1472 echo "</font></p>";
1473 }
1474 if ($course->guest) {
1475 $strallowguests = get_string("allowguests");
1476 echo "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
a9a070e0 1477 echo "<img vspace=4 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>&nbsp;&nbsp;";
c2cb4545 1478 }
1479 if ($course->password) {
1480 $strrequireskey = get_string("requireskey");
1481 echo "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
c9f6251e 1482 echo "<img vspace=4 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
c2cb4545 1483 }
1484
1485
1b6a4b1d 1486 echo "</td><td valign=top width=\"50%\" class=\"courseboxsummary\">";
0d0e550f 1487 echo "<p><font size=2>".filter_text(text_to_html($course->summary), $course->id)."</font></p>";
c2cb4545 1488 echo "</td></tr>";
1489 echo "</table>";
1490
1491 print_simple_box_end();
1492}
1493
1494
1495function print_my_moodle() {
1496/// Prints custom user information on the home page.
1497/// Over time this can include all sorts of information
1498
1499 global $USER, $CFG;
1500
1501 if (!isset($USER->id)) {
1502 error("It shouldn't be possible to see My Moodle without being logged in.");
1503 }
1504
1505 if ($courses = get_my_courses($USER->id)) {
1506 foreach ($courses as $course) {
c81696e5 1507 if (!$course->category) {
1508 continue;
1509 }
c2cb4545 1510 print_course($course, "100%");
1511 echo "<br />\n";
1512 }
38a10939 1513
7f989948 1514 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1515 echo "<table width=\"100%\"><tr><td align=\"center\">";
1516 print_course_search("", false, "short");
1517 echo "</td><td align=\"center\">";
1518 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1519 echo "</td></tr></table>\n";
1520 }
26330001 1521 } else {
1522 if (count_records("course_categories") > 1) {
cb29b020 1523 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1524 print_whole_category_list();
1525 print_simple_box_end();
1526 } else {
1527 print_courses(0, "100%");
1528 }
607809b3 1529 }
2b8cef80 1530}
1531
11b0c469 1532
a8b56716 1533function print_course_search($value="", $return=false, $format="plain") {
38a10939 1534
1535 global $CFG;
1536
1537 $strsearchcourses= get_string("searchcourses");
1538
a8b56716 1539 if ($format == "plain") {
1540 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1541 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1542 $output .= "<input type=\"text\" size=30 name=\"search\" value=\"$value\">";
1543 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1544 $output .= "</form></p></center>";
e4027ac9 1545 } else if ($format == "short") {
1546 $output = "<center><p align=\"center\" class=\"coursesearchbox\">";
1547 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1548 $output .= "<input type=\"text\" size=12 name=\"search\" value=\"$value\">";
1549 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
1550 $output .= "</form></p></center>";
a8b56716 1551 } else if ($format == "navbar") {
1552 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1553 $output .= "<form name=\"coursesearch\" action=\"$CFG->wwwroot/course/search.php\" method=\"get\">";
1554 $output .= "<input type=\"text\" size=20 name=\"search\" value=\"$value\">";
1555 $output .= "<input type=\"submit\" value=\"$strsearchcourses\">";
a8b56716 1556 $output .= "</form>";
1557 $output .= "</td></tr></table>";
1558 }
1559
1560 if ($return) {
1561 return $output;
1562 }
1563 echo $output;
38a10939 1564}
11b0c469 1565
1566/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1567
1568function add_course_module($mod) {
11b0c469 1569
e5dfd0f3 1570 $mod->added = time();
11b0c469 1571
e5dfd0f3 1572 return insert_record("course_modules", $mod);
11b0c469 1573}
1574
7977cffd 1575function add_mod_to_section($mod, $beforemod=NULL) {
1576/// Given a full mod object with section and course already defined
1577/// If $before is specified, then this is an existing ID which we
1578/// will insert the new module before
1579///
1580/// Returns the course_sections ID where the mod is inserted
11b0c469 1581
9fa49e22 1582 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1583
1584 $section->sequence = trim($section->sequence);
1585
1586 if (empty($section->sequence)) {
11b0c469 1587 $newsequence = "$mod->coursemodule";
7977cffd 1588
1589 } else if ($beforemod) {
1590 $modarray = explode(",", $section->sequence);
1591
1592 if ($key = array_keys ($modarray, $beforemod->id)) {
1593 $insertarray = array($mod->id, $beforemod->id);
1594 array_splice($modarray, $key[0], 1, $insertarray);
1595 $newsequence = implode(",", $modarray);
1596
1597 } else { // Just tack it on the end anyway
1598 $newsequence = "$section->sequence,$mod->coursemodule";
1599 }
1600
1601 } else {
1602 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1603 }
7977cffd 1604
e5dfd0f3 1605 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1606 return $section->id; // Return course_sections ID that was used.
11b0c469 1607 } else {
e5dfd0f3 1608 return 0;
11b0c469 1609 }
1610
1611 } else { // Insert a new record
e5dfd0f3 1612 $section->course = $mod->course;
1613 $section->section = $mod->section;
1614 $section->summary = "";
1615 $section->sequence = $mod->coursemodule;
1616 return insert_record("course_sections", $section);
11b0c469 1617 }
1618}
1619
3d575e6f 1620function set_groupmode_for_module($id, $groupmode) {
1621 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1622}
1623
1acfbce5 1624function hide_course_module($mod) {
1625 return set_field("course_modules", "visible", 0, "id", $mod);
1626}
1627
1628function show_course_module($mod) {
1629 return set_field("course_modules", "visible", 1, "id", $mod);
1630}
1631
11b0c469 1632function delete_course_module($mod) {
1633 return set_field("course_modules", "deleted", 1, "id", $mod);
1634}
1635
1636function delete_mod_from_section($mod, $section) {
11b0c469 1637
e5dfd0f3 1638 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1639
e5dfd0f3 1640 $modarray = explode(",", $section->sequence);
11b0c469 1641
1642 if ($key = array_keys ($modarray, $mod)) {
1643 array_splice($modarray, $key[0], 1);
1644 $newsequence = implode(",", $modarray);
e5dfd0f3 1645 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1646 } else {
1647 return false;
1648 }
1649
11b0c469 1650 }
7977cffd 1651 return false;
11b0c469 1652}
1653
12905134 1654function move_section($course, $section, $move) {
1655/// Moves a whole course section up and down within the course
1656
1657 if (!$move) {
1658 return true;
1659 }
1660
1661 $sectiondest = $section + $move;
1662
1663 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1664 return false;
1665 }
1666
1667 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1668 return false;
1669 }
1670
1671 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1672 return false;
1673 }
1674
56e34ee4 1675 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1676 return false;
1677 }
56e34ee4 1678 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1679 return false;
1680 }
1681 return true;
1682}
1683
1684
7977cffd 1685function moveto_module($mod, $section, $beforemod=NULL) {
1686/// All parameters are objects
1687/// Move the module object $mod to the specified $section
1688/// If $beforemod exists then that is the module
1689/// before which $modid should be inserted
1690
1691/// Remove original module from original section
1692
1693 if (! delete_mod_from_section($mod->id, $mod->section)) {
1694 notify("Could not delete module from existing section");
1695 }
1696
1697/// Update module itself if necessary
1698
1699 if ($mod->section != $section->id) {
1700 $mod->section = $section->id;
1701
1702 if (!update_record("course_modules", $mod)) {
1703 return false;
1704 }
1705 }
1706
1707/// Add the module into the new section
1708
1709 $mod->course = $section->course;
1710 $mod->section = $section->section; // need relative reference
1711 $mod->coursemodule = $mod->id;
1712
1713 if (! add_mod_to_section($mod, $beforemod)) {
1714 return false;
1715 }
1716
1717 return true;
1718
1719}
1720
1721
11b0c469 1722
7c0f2984 1723function move_module($cm, $move) {
12905134 1724/// Moves an activity module up and down within the course
11b0c469 1725
1726 if (!$move) {
1727 return true;
1728 }
1729
11b0c469 1730 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1731 error("This course section doesn't exist");
1732 }
1733
1734 $mods = explode(",", $thissection->sequence);
1735
1736 $len = count($mods);
1737 $pos = array_keys($mods, $cm->id);
1738 $thepos = $pos[0];
1739
1740 if ($len == 0 || count($pos) == 0 ) {
1741 error("Very strange. Could not find the required module in this section.");
1742 }
1743
1744 if ($len == 1) {
1745 $first = true;
1746 $last = true;
1747 } else {
1748 $first = ($thepos == 0);
1749 $last = ($thepos == $len - 1);
1750 }
1751
1752 if ($move < 0) { // Moving the module up
1753
1754 if ($first) {
ad41694c 1755 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1756 return true;
ad41694c 1757
11b0c469 1758 } else { // Push onto end of previous section
1759 $prevsectionnumber = $thissection->section - 1;
9fa49e22 1760 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
1761 "section", "$prevsectionnumber")) {
11b0c469 1762 error("Previous section ($prevsection->id) doesn't exist");
1763 }
1764
74666583 1765 if (!empty($prevsection->sequence)) {
11b0c469 1766 $newsequence = "$prevsection->sequence,$cm->id";
1767 } else {
1768 $newsequence = "$cm->id";
1769 }
1770
1771 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1772 error("Previous section could not be updated");
1773 }
1774
1775 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1776 error("Module could not be updated");
1777 }
1778
1779 array_splice($mods, 0, 1);
1780 $newsequence = implode(",", $mods);
1781 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1782 error("Module could not be updated");
1783 }
1784
1785 return true;
1786
1787 }
1788 } else { // move up within this section
1789 $swap = $mods[$thepos-1];
1790 $mods[$thepos-1] = $mods[$thepos];
1791 $mods[$thepos] = $swap;
1792
1793 $newsequence = implode(",", $mods);
1794 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1795 error("This section could not be updated");
1796 }
1797 return true;
1798 }
1799
1800 } else { // Moving the module down
1801
1802 if ($last) {
1803 $nextsectionnumber = $thissection->section + 1;
9fa49e22 1804 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
1805 "section", "$nextsectionnumber")) {
11b0c469 1806
74666583 1807 if (!empty($nextsection->sequence)) {
11b0c469 1808 $newsequence = "$cm->id,$nextsection->sequence";
1809 } else {
1810 $newsequence = "$cm->id";
1811 }
1812
1813 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1814 error("Next section could not be updated");
1815 }
1816
1817 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1818 error("Module could not be updated");
1819 }
1820
1821 array_splice($mods, $thepos, 1);
1822 $newsequence = implode(",", $mods);
1823 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1824 error("This section could not be updated");
1825 }
1826 return true;
1827
1828 } else { // There is no next section, so just return
1829 return true;
1830
1831 }
1832 } else { // move down within this section
1833 $swap = $mods[$thepos+1];
1834 $mods[$thepos+1] = $mods[$thepos];
1835 $mods[$thepos] = $swap;
1836
1837 $newsequence = implode(",", $mods);
1838 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1839 error("This section could not be updated");
1840 }
1841 return true;
1842 }
1843 }
1844}
1845
3d575e6f 1846function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1) {
dc0dc7d5 1847 global $CFG, $THEME;
94361e02 1848
3d575e6f 1849 static $str;
1850
1851 if (!isset($str)) {
1852 $str->delete = get_string("delete");
1853 $str->move = get_string("move");
1854 $str->moveup = get_string("moveup");
1855 $str->movedown = get_string("movedown");
1856 $str->moveright = get_string("moveright");
1857 $str->moveleft = get_string("moveleft");
1858 $str->update = get_string("update");
1859 $str->hide = get_string("hide");
1860 $str->show = get_string("show");
1861 $str->clicktochange = get_string("clicktochange");
32d03b7b 1862 $str->forcedmode = get_string("forcedmode");
3d575e6f 1863 $str->groupsnone = get_string("groupsnone");
1864 $str->groupsseparate = get_string("groupsseparate");
1865 $str->groupsvisible = get_string("groupsvisible");
1acfbce5 1866 }
94361e02 1867
1868 if ($absolute) {
dc0dc7d5 1869 $path = "$CFG->wwwroot/course";
1870 } else {
1871 $path = ".";
1872 }
1873
1874 if (empty($THEME->custompix)) {
1875 $pixpath = "$path/../pix";
94361e02 1876 } else {
dc0dc7d5 1877 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1878 }
1acfbce5 1879
3d575e6f 1880 if ($mod->visible) {
1881 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$mod->id\"><img".
7977cffd 1882 " src=\"$pixpath/t/hide.gif\" hspace=2 height=11 width=11 border=0></a> ";
1acfbce5 1883 } else {
3d575e6f 1884 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$mod->id\"><img".
7977cffd 1885 " src=\"$pixpath/t/show.gif\" hspace=2 height=11 width=11 border=0></a> ";
1886 }
3d575e6f 1887 if ($mod->groupmode !== false) {
1888 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1889 $grouptitle = $str->groupsseparate;
1890 $groupimage = "$pixpath/t/groups.gif";
1891 $grouplink = "$path/mod.php?id=$mod->id&groupmode=0";
3d575e6f 1892 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1893 $grouptitle = $str->groupsvisible;
1894 $groupimage = "$pixpath/t/groupv.gif";
1895 $grouplink = "$path/mod.php?id=$mod->id&groupmode=1";
1896 } else {
1897 $grouptitle = $str->groupsnone;
1898 $groupimage = "$pixpath/t/groupn.gif";
1899 $grouplink = "$path/mod.php?id=$mod->id&groupmode=2";
1900 }
1901 if ($mod->groupmodelink) {
1902 $groupmode = "<a title=\"$grouptitle ($str->clicktochange)\" href=\"$grouplink\">".
1903 "<img src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\"></a>";
3d575e6f 1904 } else {
32d03b7b 1905 $groupmode = "<img title=\"$grouptitle ($str->forcedmode)\" ".
1906 " src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\">";
3d575e6f 1907 }
1908 } else {
1909 $groupmode = "";
1910 }
7977cffd 1911
1912 if ($moveselect) {
3d575e6f 1913 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$mod->id\"><img".
bf1241ad 1914 " src=\"$pixpath/t/move.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\"></a>";
493486c4 1915 } else {
3d575e6f 1916 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$mod->id&move=-1\"><img".
bf1241ad 1917 " src=\"$pixpath/t/up.gif\" hspace=\"2\" height=11 width=11 border=0></a>".
3d575e6f 1918 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$mod->id&move=1\"><img".
bf1241ad 1919 " src=\"$pixpath/t/down.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
7977cffd 1920 }
1921
aac94fd0 1922 $leftright = "";
1923 if ($indent > 0) {
3d575e6f 1924 $leftright .= "<a title=\"$str->moveleft\" href=\"$path/mod.php?id=$mod->id&indent=-1\"><img".
bf1241ad 1925 " src=\"$pixpath/t/left.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
aac94fd0 1926 }
1927 if ($indent >= 0) {
3d575e6f 1928 $leftright .= "<a title=\"$str->moveright\" href=\"$path/mod.php?id=$mod->id&indent=1\"><img".
bf1241ad 1929 " src=\"$pixpath/t/right.gif\" hspace=\"2\" height=11 width=11 border=0></a>";
aac94fd0 1930 }
1931
bf1241ad 1932 return "$leftright$move".
3d575e6f 1933 "<a title=\"$str->update\" href=\"$path/mod.php?update=$mod->id\"><img".
bf1241ad 1934 " src=\"$pixpath/t/edit.gif\" hspace=\"2\" height=11 width=11 border=0></a>".
3d575e6f 1935 "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$mod->id\"><img".
1936 " src=\"$pixpath/t/delete.gif\" hspace=\"2\" height=11 width=11 border=0></a>$hideshow$groupmode";
90845098 1937}
1938
f9903ed0 1939?>