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