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