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