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