fix for 5385
[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
950c35a9 19define('COURSE_MAX_COURSES_PER_DROPDOWN',1000); // max courses in log dropdown before switching to optional
60fdc714 20
950c35a9 21define('COURSE_MAX_USERS_PER_DROPDOWN',1000); // max users in log dropdown before switching to optional
60fdc714 22
c2cb4545 23define("FRONTPAGENEWS", 0);
24define("FRONTPAGECOURSELIST", 1);
25define("FRONTPAGECATEGORYNAMES", 2);
5eafd948 26define("FRONTPAGETOPICONLY", 3);
f9903ed0 27
587510be 28function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0, $selecteddate="lastlogin",
4581271a 29 $mod="", $modid="activity/All", $modaction="", $selectedgroup="", $selectedsort="default") {
cb83c3cb 30
31 global $USER, $CFG;
32
cb83c3cb 33 $isteacher = isteacher($course->id);
587510be 34 if ($advancedfilter) {
35
36 // Get all the possible users
37 $users = array();
89adb174 38
65ee9c16 39 if ($courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname')) {
587510be 40 foreach ($courseusers as $courseuser) {
41 $users[$courseuser->id] = fullname($courseuser, $isteacher);
42 }
cb83c3cb 43 }
587510be 44 if ($guest = get_guest()) {
45 $users[$guest->id] = fullname($guest);
46 }
89adb174 47
587510be 48 if (isadmin()) {
49 if ($ccc = get_records("course", "", "", "fullname")) {
50 foreach ($ccc as $cc) {
51 if ($cc->category) {
52 $courses["$cc->id"] = "$cc->fullname";
53 } else {
54 $courses["$cc->id"] = " $cc->fullname (Site)";
89adb174 55 }
587510be 56 }
cb83c3cb 57 }
587510be 58 asort($courses);
59 }
4581271a 60
587510be 61 $activities = array();
cb83c3cb 62
587510be 63 $selectedactivity = $modid;
4581271a 64
587510be 65 if ($modinfo = unserialize($course->modinfo)) {
66 $section = 0;
67 if ($course->format == 'weeks') { // Body
68 $strsection = get_string("week");
69 } else {
70 $strsection = get_string("topic");
cb83c3cb 71 }
cb83c3cb 72
587510be 73 $activities["activity/All"] = "All activities";
74 $activities["activity/Assignments"] = "All assignments";
75 $activities["activity/Chats"] = "All chats";
76 $activities["activity/Forums"] = "All forums";
77 $activities["activity/Quizzes"] = "All quizzes";
78 $activities["activity/Workshops"] = "All workshops";
79
80 $activities["section/individual"] = "------------- Individual Activities --------------";
81
82 foreach ($modinfo as $mod) {
83 if ($mod->mod == "label") {
84 continue;
89adb174 85 }
9c08ad13 86 if (!$mod->visible and !$isteacher) {
87 continue;
88 }
89
587510be 90 if ($mod->section > 0 and $section <> $mod->section) {
91 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
92 }
93 $section = $mod->section;
235a4ee8 94 $mod->name = strip_tags(format_string(urldecode($mod->name),true));
587510be 95 if (strlen($mod->name) > 55) {
96 $mod->name = substr($mod->name, 0, 50)."...";
97 }
98 if (!$mod->visible) {
99 $mod->name = "(".$mod->name.")";
100 }
101 $activities["$mod->cm"] = $mod->name;
102
103 if ($mod->cm == $modid) {
104 $selectedactivity = "$mod->cm";
105 }
cb83c3cb 106 }
107 }
cb83c3cb 108
587510be 109 $strftimedate = get_string("strftimedate");
110 $strftimedaydate = get_string("strftimedaydate");
cb83c3cb 111
587510be 112 asort($users);
cb83c3cb 113
587510be 114 // Get all the possible dates
115 // Note that we are keeping track of real (GMT) time and user time
116 // User time is only used in displays - all calcs and passing is GMT
cb83c3cb 117
587510be 118 $timenow = time(); // GMT
cb83c3cb 119
587510be 120 // What day is it now for the user, and when is midnight that day (in GMT).
121 $timemidnight = $today = usergetmidnight($timenow);
cb83c3cb 122
587510be 123 $dates = array();
124 $dates["$USER->lastlogin"] = get_string("lastlogin").", ".userdate($USER->lastlogin, $strftimedate);
125 $dates["$timemidnight"] = get_string("today").", ".userdate($timenow, $strftimedate);
cb83c3cb 126
587510be 127 if (!$course->startdate or ($course->startdate > $timenow)) {
128 $course->startdate = $course->timecreated;
129 }
cb83c3cb 130
587510be 131 $numdates = 1;
132 while ($timemidnight > $course->startdate and $numdates < 365) {
133 $timemidnight = $timemidnight - 86400;
134 $timenow = $timenow - 86400;
135 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
136 $numdates++;
137 }
cb83c3cb 138
96dcfb56 139 if ($selecteddate === "lastlogin") {
587510be 140 $selecteddate = $USER->lastlogin;
141 }
142
143 echo '<form action="recent.php" method="get">';
1c919752 144 echo '<input type="hidden" name="chooserecent" value="1" />';
587510be 145 echo "<center>";
146 echo "<table>";
147
148 if (isadmin()) {
149 echo "<tr><td><b>" . get_string("courses") . "</b></td><td>";
150 choose_from_menu ($courses, "id", $course->id, "");
151 echo "</td></tr>";
152 } else {
1c919752 153 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
587510be 154 }
cb83c3cb 155
f5ffb87d 156 $sortfields = array("default" => get_string("bycourseorder"),"dateasc" => get_string("datemostrecentlast"), "datedesc" => get_string("datemostrecentfirst"));
4581271a 157
587510be 158 echo "<tr><td><b>" . get_string("participants") . "</b></td><td>";
159 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
160 echo "</td>";
161
1c919752 162 echo '<td align="right"><b>' . get_string("since") . '</b></td><td>';
587510be 163 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
4581271a 164 echo "</td></tr>";
4581271a 165
587510be 166 echo "<tr><td><b>" . get_string("activities") . "</b></td><td>";
167 choose_from_menu ($activities, "modid", $selectedactivity, "");
168 echo "</td>";
4581271a 169
1c919752 170 echo '<td align="right"><b>' . get_string("sortby") . "</b></td><td>";
587510be 171 choose_from_menu ($sortfields, "sortby", $selectedsort, "");
172 echo "</td></tr>";
4581271a 173
587510be 174 echo '<tr>';
4581271a 175
587510be 176 $groupmode = groupmode($course);
4581271a 177
587510be 178 if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
179 if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
4581271a 180 echo '<td><b>';
587510be 181 if ($groupmode == VISIBLEGROUPS) {
182 print_string('groupsvisible');
183 } else {
184 print_string('groupsseparate');
185 }
186 echo ':</b></td><td>';
187 choose_from_menu($groups, "selectedgroup", $selectedgroup, get_string("allgroups"), "", "");
188 echo '</td>';
189 }
190 }
191
192
1c919752 193 echo '<td colspan="2" align="right">';
194 echo '<input type="submit" value="'.get_string('showrecent').'" />';
587510be 195 echo "</td></tr>";
196
197 echo "</table>";
198
839f2456 199 $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&amp;advancedfilter=0\">" . get_string("normalfilter") . "</a>";
587510be 200 print_heading($advancedlink);
201 echo "</center>";
202 echo "</form>";
203
204 } else {
205
206 $day_list = array("1","7","14","21","30");
207 $strsince = get_string("since");
208 $strlastlogin = get_string("lastlogin");
209 $strday = get_string("day");
210 $strdays = get_string("days");
211
212 $heading = "";
213 foreach ($day_list as $count) {
214 if ($count == "1") {
215 $day = $strday;
4581271a 216 } else {
587510be 217 $day = $strdays;
4581271a 218 }
3819ed31 219 $tmpdate = time() - ($count * 3600 * 24);
587510be 220 $heading = $heading .
839f2456 221 "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&amp;date=$tmpdate\"> $count $day</a> | ";
4581271a 222 }
4581271a 223
587510be 224 $heading = $strsince . ": <a href=\"$CFG->wwwroot/course/recent.php?id=$course->id\">$strlastlogin</a>" . " | " . $heading;
225 print_heading($heading);
4581271a 226
839f2456 227 $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&amp;advancedfilter=1\">" . get_string("advancedfilter") . "</a>";
587510be 228 print_heading($advancedlink);
229
230 }
4581271a 231
cb83c3cb 232}
9ae687af 233
f9903ed0 234
600149be 235function make_log_url($module, $url) {
236 switch ($module) {
de2dfe68 237 case "user":
600149be 238 case "course":
600149be 239 case "file":
240 case "login":
241 case "lib":
242 case "admin":
c3dd5410 243 case "message":
244 case "calendar":
600149be 245 return "/$module/$url";
246 break;
c80b7585 247 case "upload":
248 return "$url";
249 break;
de2dfe68 250 case "library":
251 case "":
252 return "/";
253 break;
600149be 254 default:
255 return "/mod/$module/$url";
256 break;
257 }
258}
259
8f0cd6ef 260function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
69c76405 261 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
f24cffb9 262
e0161bff 263 // It is assumed that $date is the GMT time of midnight for that day,
264 // and so the next 86400 seconds worth of logs are printed.
f9903ed0 265
f24cffb9 266 global $CFG, $db;
47f1da80 267
69c76405 268 /// Setup for group handling.
269 $isteacher = isteacher($course->id);
270 $isteacheredit = isteacheredit($course->id);
271
272 /// If the group mode is separate, and this user does not have editing privileges,
273 /// then only the user's group can be viewed.
274 if ($course->groupmode == SEPARATEGROUPS and !$isteacheredit) {
275 $groupid = get_current_group($course->id);
276 }
277 /// If this course doesn't have groups, no groupid can be specified.
278 else if (!$course->groupmode) {
279 $groupid = 0;
280 }
281
e0161bff 282 $joins = array();
a2ab3b05 283
e15ef260 284 if ($course->id != SITEID || $modid != 0) {
8f0cd6ef 285 $joins[] = "l.course='$course->id'";
e15ef260 286 }
287 if ($course->id == SITEID) {
2eb68e6f 288 $courses[0] = '';
e15ef260 289 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
720a43ce 290 foreach ($ccc as $cc) {
e15ef260 291 $courses[$cc->id] = $cc->shortname;
720a43ce 292 }
293 }
294 }
f9903ed0 295
c469a7ef 296 if ($modname) {
e0161bff 297 $joins[] = "l.module = '$modname'";
f24cffb9 298 }
299
e21922f0 300 if ('site_errors' === $modid) {
bf35eb15 301 $joins[] = "( l.action='error' OR l.action='infected' )";
e21922f0 302 } else if ($modid) {
303 $joins[] = "l.cmid = '$modid'";
69d79bc3 304 }
305
306 if ($modaction) {
ee35e0b8 307 $firstletter = substr($modaction, 0, 1);
308 if (ctype_alpha($firstletter)) {
309 $joins[] = "lower(l.action) LIKE '%" . strtolower($modaction) . "%'";
310 } else if ($firstletter == '-') {
311 $joins[] = "lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'";
312 }
f24cffb9 313 }
314
69c76405 315 /// Getting all members of a group.
316 if ($groupid and !$user) {
317 if ($gusers = get_records('groups_members', 'groupid', $groupid)) {
318 $first = true;
319 foreach($gusers as $guser) {
320 if ($first) {
321 $gselect = '(l.userid='.$guser->userid;
322 $first = false;
323 }
324 else {
325 $gselect .= ' OR l.userid='.$guser->userid;
326 }
327 }
328 if (!$first) $gselect .= ')';
329 $joins[] = $gselect;
330 }
331 }
332 else if ($user) {
e0161bff 333 $joins[] = "l.userid = '$user'";
f9903ed0 334 }
335
336 if ($date) {
337 $enddate = $date + 86400;
e0161bff 338 $joins[] = "l.time > '$date' AND l.time < '$enddate'";
f9903ed0 339 }
340
2828ff51 341 $selector = '';
e0161bff 342 for ($i = 0; $i < count($joins); $i++) {
343 $selector .= $joins[$i] . (($i == count($joins)-1) ? " " : " AND ");
344 }
345
346
d09f3c80 347 $totalcount = 0; // Initialise
348
519d369f 349 if (!$logs = get_logs($selector, $order, $page*$perpage, $perpage, $totalcount)) {
f9903ed0 350 notify("No logs found!");
351 print_footer($course);
352 exit;
353 }
354
355 $count=0;
2eb68e6f 356 $ldcache = array();
f9903ed0 357 $tt = getdate(time());
358 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
1c0200e0 359
dcde9f02 360 $strftimedatetime = get_string("strftimedatetime");
2ac64806 361 $isteacher = isteacher($course->id);
dcde9f02 362
21283ddc 363 echo "<p align=\"center\">\n";
519d369f 364 print_string("displayingrecords", "", $totalcount);
21283ddc 365 echo "</p>\n";
1c0200e0 366
1548978d 367
8f0cd6ef 368 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
519d369f 369
21283ddc 370 echo "<table class=\"logtable\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\">\n";
371 echo "<tr>";
1548978d 372 if ($course->id == SITEID) {
21283ddc 373 echo "<th class=\"c0 header\">".get_string('course')."</th>\n";
1548978d 374 }
21283ddc 375 echo "<th class=\"c1 header\">".get_string('time')."</th>\n";
376 echo "<th class=\"c2 header\">".get_string('ip_address')."</th>\n";
377 echo "<th class=\"c3 header\">".get_string('fullname')."</th>\n";
378 echo "<th class=\"c4 header\">".get_string('action')."</th>\n";
379 echo "<th class=\"c5 header\">".get_string('info')."</th>\n";
380 echo "</tr>\n";
1548978d 381
382 $row = 1;
f9903ed0 383 foreach ($logs as $log) {
600149be 384
1548978d 385 $row = ($row + 1) % 2;
386
2eb68e6f 387 if (isset($ldcache[$log->module][$log->action])) {
388 $ld = $ldcache[$log->module][$log->action];
389 } else {
1548978d 390 $ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
2eb68e6f 391 $ldcache[$log->module][$log->action] = $ld;
392 }
76feee3f 393 if ($ld && !empty($log->info)) {
181b888e 394 // ugly hack to make sure fullname is shown correctly
395 if (($ld->mtable == 'user') and ($ld->field == 'CONCAT(firstname," ",lastname)')) {
396 $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
397 } else {
398 $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
399 }
600149be 400 }
401
c8b0a50b 402 //Filter log->info
403 $log->info = format_string($log->info);
404
d7d145b1 405 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
406 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
6ac98433 407 $log->url = str_replace('&', '&amp;', $log->url); /// XHTML compatibility
d7d145b1 408
1548978d 409 echo '<tr class="r'.$row.'">';
410 if ($course->id == SITEID) {
21283ddc 411 echo "<td class=\"r$row c0\" nowrap=\"nowrap\">\n";
412 echo " <a href=\"view.php?id={$log->course}\">".$courses[$log->course]."</a>\n";
413 echo "</td>\n";
720a43ce 414 }
21283ddc 415 echo "<td class=\"r$row c1\" nowrap=\"nowrap\" align=\"right\">".userdate($log->time, '%a').
416 ' '.userdate($log->time, $strftimedatetime)."</td>\n";
417 echo "<td class=\"r$row c2\" nowrap=\"nowrap\">\n";
7eca967c 418 link_to_popup_window("/iplookup/index.php?ip=$log->ip&amp;user=$log->userid", 'iplookup',$log->ip, 400, 700);
21283ddc 419 echo "</td>\n";
2ac64806 420 $fullname = fullname($log, $isteacher);
21283ddc 421 echo "<td class=\"r$row c3\" nowrap=\"nowrap\">\n";
d3a75287 422 echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}&amp;course={$log->course}\">$fullname</a>\n";
21283ddc 423 echo "</td>\n";
424 echo "<td class=\"r$row c4\" nowrap=\"nowrap\">\n";
2eb68e6f 425 link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 400, 600);
21283ddc 426 echo "</td>\n";;
427 echo "<td class=\"r$row c5\" nowrap=\"nowrap\">{$log->info}</td>\n";
428 echo "</tr>\n";
f9903ed0 429 }
21283ddc 430 echo "</table>\n";
519d369f 431
8f0cd6ef 432 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
f9903ed0 433}
434
435
c2cb4545 436function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
437 global $CFG;
438 if (empty($CFG->gdversion)) {
439 echo "(".get_string("gdneed").")";
d887b5a7 440 } else {
980a5b3a 441 echo '<img src="'.$CFG->wwwroot.'/course/report/log/graph.php?id='.$course->id.
29b59206 442 '&amp;user='.$userid.'&amp;type='.$type.'&amp;date='.$date.'" alt="" />';
d887b5a7 443 }
444}
445
446
185cfb09 447function print_overview($courses) {
0d6b9d4f 448
449 global $CFG, $USER;
450
0d6b9d4f 451
185cfb09 452 $htmlarray = array();
f8716988 453 if ($modules = get_records('modules')) {
454 foreach ($modules as $mod) {
455 if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) {
456 require_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php');
457 $fname = $mod->name.'_print_overview';
0d6b9d4f 458 if (function_exists($fname)) {
185cfb09 459 $fname($courses,$htmlarray);
0d6b9d4f 460 }
461 }
462 }
463 }
185cfb09 464
465 foreach ($courses as $course) {
fe5a1e23 466 print_simple_box_start('center', '100%', '', 5, "coursebox");
185cfb09 467 $linkcss = '';
468 if (empty($course->visible)) {
469 $linkcss = 'class="dimmed"';
470 }
471 print_heading('<a title="'.$course->fullname.'" '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>');
472 if (array_key_exists($course->id,$htmlarray)) {
473 foreach ($htmlarray[$course->id] as $modname => $html) {
474 echo $html;
475 }
476 }
477 print_simple_box_end();
478 }
0d6b9d4f 479}
480
481
f9903ed0 482
600149be 483function print_recent_activity($course) {
484 // $course is an object
89adb174 485 // This function trawls through the logs looking for
600149be 486 // anything new since the user's last login
487
810393c8 488 global $CFG, $USER, $SESSION;
600149be 489
2ac64806 490 $isteacher = isteacher($course->id);
491
6f80940b 492 $timestart = time() - COURSE_MAX_RECENT_PERIOD;
0f87cb1d 493
9e51847a 494 if (!empty($USER->timeaccess[$course->id])) {
6f80940b 495 if ($USER->timeaccess[$course->id] > $timestart) {
9e51847a 496 $timestart = $USER->timeaccess[$course->id];
497 }
3d891989 498 }
0f87cb1d 499
de785682 500 echo '<div class="activitydate">';
27bf9e20 501 echo get_string('activitysince', '', userdate($timestart));
de785682 502 echo '</div>';
503 echo '<div class="activityhead">';
0f87cb1d 504
de785682 505 echo '<a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a>';
0f87cb1d 506
de785682 507 echo '</div>';
0f87cb1d 508
600149be 509
510 // Firstly, have there been any new enrolments?
511
512 $heading = false;
513 $content = false;
1b5910c4 514
6c38b7e0 515 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 516
6c38b7e0 517 if ($users) {
27bf9e20 518 echo '<div class="newusers">';
6c38b7e0 519 foreach ($users as $user) {
600149be 520 if (! $heading) {
27bf9e20 521 print_headline(get_string("newusers").':', 3);
600149be 522 $heading = true;
523 $content = true;
524 }
2ac64806 525 $fullname = fullname($user, $isteacher);
27bf9e20 526 echo '<span class="name"><a href="'.$CFG->wwwroot."/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></span><br />";
600149be 527 }
27bf9e20 528 echo '</div>';
600149be 529 }
530
1b5910c4 531 // Next, have there been any modifications to the course structure?
532
27bf9e20 533 $logs = get_records_select('log', "time > '$timestart' AND course = '$course->id' AND
1b5910c4 534 module = 'course' AND action LIKE '% mod'", "time ASC");
535
536 if ($logs) {
537 foreach ($logs as $key => $log) {
27bf9e20 538 $info = split(' ', $log->info);
c9f6251e 539
27bf9e20 540 if ($info[0] == 'label') { // Labels are special activities
c9f6251e 541 continue;
542 }
543
27bf9e20 544 $modname = get_field($info[0], 'name', 'id', $info[1]);
1b5910c4 545 //Create a temp valid module structure (course,id)
546 $tempmod->course = $log->course;
547 $tempmod->id = $info[1];
548 //Obtain the visible property from the instance
549 $modvisible = instance_is_visible($info[0],$tempmod);
89adb174 550
1b5910c4 551 //Only if the mod is visible
552 if ($modvisible) {
553 switch ($log->action) {
27bf9e20 554 case 'add mod':
555 $stradded = get_string('added', 'moodle', get_string('modulename', $info[0]));
5847b267 556 $changelist[$log->info] = array ('operation' => 'add', 'text' => "$stradded:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>");
1b5910c4 557 break;
27bf9e20 558 case 'update mod':
559 $strupdated = get_string('updated', 'moodle', get_string('modulename', $info[0]));
560 if (empty($changelist[$log->info])) {
5847b267 561 $changelist[$log->info] = array ('operation' => 'update', 'text' => "$strupdated:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>");
1b5910c4 562 }
563 break;
27bf9e20 564 case 'delete mod':
565 if (!empty($changelist[$log->info]['operation']) and
566 $changelist[$log->info]['operation'] == 'add') {
567 $changelist[$log->info] = NULL;
1b5910c4 568 } else {
27bf9e20 569 $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $info[0]));
570 $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted);
1b5910c4 571 }
572 break;
600149be 573 }
ef25340c 574 }
575 }
576 }
577
9c9f7d77 578 if (!empty($changelist)) {
ef25340c 579 foreach ($changelist as $changeinfo => $change) {
580 if ($change) {
581 $changes[$changeinfo] = $change;
582 }
583 }
8a59942e 584 if (isset($changes)){
585 if (count($changes) > 0) {
27bf9e20 586 print_headline(get_string('courseupdates').':', 3);
8a59942e 587 $content = true;
588 foreach ($changes as $changeinfo => $change) {
27bf9e20 589 echo '<p class="activity">'.$change['text'].'</p>';
8a59942e 590 }
600149be 591 }
592 }
89adb174 593 }
bf40f9c1 594
3869a2ac 595 // Now display new things from each module
600149be 596
27bf9e20 597 $mods = get_records('modules', 'visible', '1', 'name', 'id, name');
0fd7da81 598
1b5910c4 599 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
27bf9e20 600 include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php');
601 $print_recent_activity = $mod->name.'_print_recent_activity';
1b5910c4 602 if (function_exists($print_recent_activity)) {
603 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
3869a2ac 604 if ($modcontent) {
605 $content = true;
600149be 606 }
600149be 607 }
608 }
609
610 if (! $content) {
27bf9e20 611 echo '<p class="message">'.get_string('nothingnew').'</p>';
600149be 612 }
600149be 613}
614
e1360728 615
d897cae4 616function get_array_of_activities($courseid) {
89adb174 617// For a given course, returns an array of course activity objects
d897cae4 618// Each item in the array contains he following properties:
619// cm - course module id
620// mod - name of the module (eg forum)
621// section - the number of the section (eg week or topic)
622// name - the name of the instance
5867bfb5 623// visible - is the instance visible or not
86aa7ccf 624// extra - contains extra string to include in any link
d897cae4 625
8dddba42 626 global $CFG;
627
d897cae4 628 $mod = array();
629
9fa49e22 630 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 631 return NULL;
632 }
633
634 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
635 foreach ($sections as $section) {
74666583 636 if (!empty($section->sequence)) {
d897cae4 637 $sequence = explode(",", $section->sequence);
638 foreach ($sequence as $seq) {
7af6281f 639 if (empty($rawmods[$seq])) {
640 continue;
641 }
d897cae4 642 $mod[$seq]->cm = $rawmods[$seq]->id;
643 $mod[$seq]->mod = $rawmods[$seq]->modname;
644 $mod[$seq]->section = $section->section;
645 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 646 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 647 $mod[$seq]->extra = "";
8dddba42 648
649 $modname = $mod[$seq]->mod;
650 $functionname = $modname."_get_coursemodule_info";
651
652 include_once("$CFG->dirroot/mod/$modname/lib.php");
653
654 if (function_exists($functionname)) {
9d361034 655 if ($info = $functionname($rawmods[$seq])) {
656 if (!empty($info->extra)) {
657 $mod[$seq]->extra = $info->extra;
658 }
659 if (!empty($info->icon)) {
660 $mod[$seq]->icon = $info->icon;
661 }
c9f6251e 662 }
663 }
d897cae4 664 }
665 }
666 }
667 }
668 return $mod;
669}
670
671
672
e1360728 673
90845098 674function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
675// Returns a number of useful structures for course displays
7468bf01 676
90845098 677 $mods = NULL; // course modules indexed by id
e0161bff 678 $modnames = NULL; // all course module names (except resource!)
94361e02 679 $modnamesplural= NULL; // all course module names (plural form)
90845098 680 $modnamesused = NULL; // course module names used
7468bf01 681
9fa49e22 682 if ($allmods = get_records("modules")) {
90845098 683 foreach ($allmods as $mod) {
5867bfb5 684 if ($mod->visible) {
685 $modnames[$mod->name] = get_string("modulename", "$mod->name");
686 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
687 }
90845098 688 }
689 asort($modnames);
690 } else {
691 error("No modules are installed!");
692 }
693
9fa49e22 694 if ($rawmods = get_course_mods($courseid)) {
7468bf01 695 foreach($rawmods as $mod) { // Index the mods
959ae824 696 if (empty($modnames[$mod->modname])) {
697 continue;
698 }
7468bf01 699 $mods[$mod->id] = $mod;
959ae824 700 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
701 if ($mod->visible or isteacher($courseid)) {
702 $modnamesused[$mod->modname] = $modnames[$mod->modname];
1acfbce5 703 }
7468bf01 704 }
c7da6f7a 705 if ($modnamesused) {
706 asort($modnamesused);
707 }
7468bf01 708 }
e0161bff 709
710 unset($modnames['resource']);
711 unset($modnames['label']);
7468bf01 712}
713
9fa49e22 714
7468bf01 715function get_all_sections($courseid) {
89adb174 716
717 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 718 "section, id, course, summary, sequence, visible");
7468bf01 719}
720
b86fc0e2 721function course_set_display($courseid, $display=0) {
722 global $USER;
723
1066e0dc 724 if (empty($USER->id)) {
b86fc0e2 725 return false;
726 }
727
728 if ($display == "all" or empty($display)) {
729 $display = 0;
730 }
731
732 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
733 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
734 } else {
735 $record->userid = $USER->id;
736 $record->course = $courseid;
737 $record->display = $display;
738 if (!insert_record("course_display", $record)) {
739 notify("Could not save your course display!");
740 }
741 }
742
743 return $USER->display[$courseid] = $display; // Note: = not ==
744}
745
7d99d695 746function set_section_visible($courseid, $sectionnumber, $visibility) {
747/// For a given course section, markes it visible or hidden,
748/// and does the same for every activity in that section
749
750 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
751 set_field("course_sections", "visible", "$visibility", "id", $section->id);
752 if (!empty($section->sequence)) {
753 $modules = explode(",", $section->sequence);
754 foreach ($modules as $moduleid) {
02f66c42 755 set_coursemodule_visible($moduleid, $visibility, true);
7d99d695 756 }
757 }
5867bfb5 758 rebuild_course_cache($courseid);
7d99d695 759 }
760}
ba2e5d73 761
5e367a2d 762
d897cae4 763function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 764/// Prints a section full of activity modules
7977cffd 765 global $CFG, $USER;
766
3d575e6f 767 static $groupbuttons;
32d03b7b 768 static $groupbuttonslink;
52dcc2f9 769 static $isteacher;
770 static $isediting;
7977cffd 771 static $ismoving;
772 static $strmovehere;
773 static $strmovefull;
54669989 774 static $strunreadpostsone;
52dcc2f9 775
4877707e 776 static $untracked;
a2d71d8e 777 static $usetracking;
4877707e 778
a22f8313 779 $labelformatoptions = New stdClass;
110a32e2 780
52dcc2f9 781 if (!isset($isteacher)) {
9fd9c29b 782 $groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
32d03b7b 783 $groupbuttonslink = (!$course->groupmodeforce);
52dcc2f9 784 $isteacher = isteacher($course->id);
52dcc2f9 785 $isediting = isediting($course->id);
ff0c7de0 786 $ismoving = $isediting && ismoving($course->id);
3d575e6f 787 if ($ismoving) {
788 $strmovehere = get_string("movehere");
789 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
790 }
94a6a70f 791 include_once($CFG->dirroot.'/mod/forum/lib.php');
792 if ($usetracking = forum_tp_can_track_forums()) {
a2d71d8e 793 $strunreadpostsone = get_string('unreadpostsone', 'forum');
94a6a70f 794 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
a2d71d8e 795 }
7977cffd 796 }
60bd11cf 797 $labelformatoptions->noclean = true;
94361e02 798
c408b0c4 799 $modinfo = unserialize($course->modinfo);
94361e02 800
c6a55371 801 //Acccessibility: replace table with list <ul>, but don't output empty list.
74666583 802 if (!empty($section->sequence)) {
94361e02 803
f2d660dc 804 // Fix bug #5027, don't want style=\"width:$width\".
805 echo "<ul class=\"section\">\n";
94361e02 806 $sectionmods = explode(",", $section->sequence);
807
808 foreach ($sectionmods as $modnumber) {
9ae687af 809 if (empty($mods[$modnumber])) {
810 continue;
811 }
94361e02 812 $mod = $mods[$modnumber];
c9f6251e 813
52dcc2f9 814 if ($mod->visible or $isteacher) {
64fdc686 815 echo '<li class="activity '.$mod->modname.'">';
7977cffd 816 if ($ismoving) {
817 if ($mod->id == $USER->activitycopy) {
818 continue;
819 }
1c919752 820 echo '<a title="'.$strmovefull.'"'.
8b92f5bb 821 ' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 822 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
823 ' alt="'.$strmovehere.'" /></a><br />
1c919752 824 ';
1acfbce5 825 }
7977cffd 826 $instancename = urldecode($modinfo[$modnumber]->name);
a92d95b1 827 $instancename = format_string($instancename, true, $course->id);
c9f6251e 828
86aa7ccf 829 if (!empty($modinfo[$modnumber]->extra)) {
830 $extra = urldecode($modinfo[$modnumber]->extra);
831 } else {
832 $extra = "";
833 }
c9f6251e 834
9d361034 835 if (!empty($modinfo[$modnumber]->icon)) {
836 $icon = "$CFG->pixpath/".urldecode($modinfo[$modnumber]->icon);
837 } else {
838 $icon = "$CFG->modpixpath/$mod->modname/icon.gif";
839 }
840
aac94fd0 841 if ($mod->indent) {
842 print_spacer(12, 20 * $mod->indent, false);
843 }
844
c9f6251e 845 if ($mod->modname == "label") {
aac94fd0 846 if (!$mod->visible) {
847 echo "<span class=\"dimmed_text\">";
848 }
179c9a50 849 echo format_text($extra, FORMAT_HTML, $labelformatoptions);
aac94fd0 850 if (!$mod->visible) {
851 echo "</span>";
852 }
c9f6251e 853
854 } else { // Normal activity
855 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
1c919752 856 echo '<img src="'.$icon.'"'.
446390fb 857 ' class="activityicon" alt="'.$mod->modfullname.'" />'.
858 ' <a title="'.$mod->modfullname.'" '.$linkcss.' '.$extra.
1c919752 859 ' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.
446390fb 860 $instancename.'</a>';
c9f6251e 861 }
a2d71d8e 862 if ($usetracking && $mod->modname == 'forum') {
f37da850 863 $groupmode = groupmode($course, $mod);
864 $groupid = ($groupmode == SEPARATEGROUPS && !isteacheredit($course->id)) ?
a2d71d8e 865 get_current_group($course->id) : false;
4877707e 866
94a6a70f 867 if (forum_tp_can_track_forums() && !isset($untracked[$mod->instance])) {
4877707e 868 $unread = forum_tp_count_forum_unread_posts($USER->id, $mod->instance, $groupid);
869 if ($unread) {
870 echo '<span class="unread"> <a href="'.$CFG->wwwroot.'/mod/forum/view.php?id='.$mod->id.'">';
871 if ($unread == 1) {
872 echo $strunreadpostsone;
873 } else {
874 print_string('unreadpostsnumber', 'forum', $unread);
875 }
876 echo '</a> </span>';
d0388ebe 877 }
54669989 878 }
f37da850 879 }
880
c9f6251e 881 if ($isediting) {
5f390342 882 if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource') {
32d03b7b 883 if (! $mod->groupmodelink = $groupbuttonslink) {
884 $mod->groupmode = $course->groupmode;
885 }
886
887 } else {
3d575e6f 888 $mod->groupmode = false;
889 }
37a88449 890 echo '&nbsp;&nbsp;';
24e1eae4 891 echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section);
c9f6251e 892 }
c6a55371 893 echo "</li>\n";
94361e02 894 }
94361e02 895 }
f2d660dc 896 } elseif ($ismoving) {
897 echo "<ul class=\"section\">\n";
898 }
7977cffd 899 if ($ismoving) {
64fdc686 900 echo '<li><a title="'.$strmovefull.'"'.
8b92f5bb 901 ' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 902 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
c6a55371 903 ' alt="'.$strmovehere.'" /></a></li>
1c919752 904 ';
7977cffd 905 }
c6a55371 906 if (!empty($section->sequence) || $ismoving) {
907 echo "</ul><!--class='section'-->\n\n";
908 }
a7ad3ea6 909}
910
5867bfb5 911
cb57e6f4 912function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
e0161bff 913// Prints the menus to add activities and resources
914
8b92f5bb 915 global $CFG, $USER;
6da4b261 916 static $straddactivity, $stractivities, $straddresource, $resources;
e0161bff 917
918 if (!isset($straddactivity)) {
919 $straddactivity = get_string('addactivity');
920 $straddresource = get_string('addresource');
6da4b261 921
922 /// Standard resource types
923 require_once("$CFG->dirroot/mod/resource/lib.php");
924 $resourceraw = resource_get_resource_types();
925
926 foreach ($resourceraw as $type => $name) {
839f2456 927 $resources["resource&amp;type=$type"] = $name;
e0161bff 928 }
0705ff84 929 if (course_allowed_module($course,'label')) {
930 $resources['label'] = get_string('resourcetypelabel', 'resource');
931 }
e0161bff 932 }
933
d33d0cda 934 $output = '<div style="text-align: right">';
0705ff84 935 if (course_allowed_module($course,'resource')) {
81a5d9ca 936 $resourceallowed = true;
0705ff84 937 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
938 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
939 }
cb57e6f4 940
941 if ($vertical) {
d33d0cda 942 $output .= '<div>';
cb57e6f4 943 }
944
0705ff84 945 // we need to loop through the forms and check to see if we can add them.
946 foreach ($modnames as $key) {
947 if (!course_allowed_module($course,$key))
948 unset($modnames[strtolower($key)]);
949 }
950
951 // this is stupid but labels get put into resource, so if resource is hidden and label is not, we're in trouble.
952 if (course_allowed_module($course,'label') && empty($resourceallowed)) {
953 $modnames['label'] = get_string('modulename', 'label');
954 }
955
1c46eb03 956 $output .= ' ';
8b92f5bb 957 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 958 $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
d33d0cda 959
960 if ($vertical) {
961 $output .= '</div>';
962 }
963
cb57e6f4 964 $output .= '</div>';
965
966 if ($return) {
967 return $output;
968 } else {
969 echo $output;
970 }
e0161bff 971}
972
5867bfb5 973function rebuild_course_cache($courseid=0) {
974// Rebuilds the cached list of course activities stored in the database
975// If a courseid is not specified, then all are rebuilt
976
977 if ($courseid) {
978 $select = "id = '$courseid'";
979 } else {
980 $select = "";
981 }
982
1a31c2b3 983 if ($courses = get_records_select("course", $select,'','id,fullname')) {
5867bfb5 984 foreach ($courses as $course) {
985 $modinfo = serialize(get_array_of_activities($course->id));
986 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
987 notify("Could not cache module information for course '$course->fullname'!");
988 }
989 }
990 }
991}
992
993
c2cb4545 994
995function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 996/// Given an empty array, this function recursively travels the
c2cb4545 997/// categories, building up a nice list for display. It also makes
998/// an array that list all the parents for each category.
999
9d866ae0 1000 // initialize the arrays if needed
1001 if (!is_array($list)) {
1002 $list = array();
1003 }
1004 if (!is_array($parents)) {
1005 $parents = array();
1006 }
1007
c2cb4545 1008 if ($category) {
1009 if ($path) {
e05bcf2f 1010 $path = $path.' / '.$category->name;
c2cb4545 1011 } else {
e05bcf2f 1012 $path = $category->name;
c2cb4545 1013 }
1014 $list[$category->id] = $path;
1015 } else {
1016 $category->id = 0;
1017 }
1018
e05bcf2f 1019 if ($categories = get_categories($category->id)) { // Print all the children recursively
c2cb4545 1020 foreach ($categories as $cat) {
1021 if (!empty($category->id)) {
3bd4de22 1022 if (isset($parents[$category->id])) {
2832badf 1023 $parents[$cat->id] = $parents[$category->id];
1024 }
c2cb4545 1025 $parents[$cat->id][] = $category->id;
1026 }
89adb174 1027 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1028 }
1029 }
1030}
1031
1032
c2cb4545 1033function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
89adb174 1034/// Recursive function to print out all the categories in a nice format
c2cb4545 1035/// with or without courses included
9ff5310a 1036 global $CFG;
e05bcf2f 1037
1038 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1039 return;
9ff5310a 1040 }
c2cb4545 1041
1042 if (!$displaylist) {
e92fe848 1043 make_categories_list($displaylist, $parentslist);
c2cb4545 1044 }
1045
1046 if ($category) {
3af6e1db 1047 if ($category->visible or iscreator()) {
d2b6ba70 1048 print_category_info($category, $depth);
c2cb4545 1049 } else {
1050 return; // Don't bother printing children of invisible categories
1051 }
89adb174 1052
c2cb4545 1053 } else {
c2cb4545 1054 $category->id = "0";
1055 }
1056
1057 if ($categories = get_categories($category->id)) { // Print all the children recursively
1058 $countcats = count($categories);
1059 $count = 0;
1060 $first = true;
1061 $last = false;
1062 foreach ($categories as $cat) {
1063 $count++;
1064 if ($count == $countcats) {
1065 $last = true;
1066 }
1067 $up = $first ? false : true;
1068 $down = $last ? false : true;
1069 $first = false;
1070
814f88c0 1071 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
c2cb4545 1072 }
1073 }
c2cb4545 1074}
1075
0705ff84 1076// this function will return $options array for choose_from_menu, with whitespace to denote nesting.
1077
1078function make_categories_options() {
1079 make_categories_list($cats,$parents);
1080 foreach ($cats as $key => $value) {
1081 if (array_key_exists($key,$parents)) {
1082 if ($indent = count($parents[$key])) {
1083 for ($i = 0; $i < $indent; $i++) {
1084 $cats[$key] = '&nbsp;'.$cats[$key];
1085 }
1086 }
1087 }
1088 }
1089 return $cats;
1090}
c2cb4545 1091
d2b6ba70 1092function print_category_info($category, $depth) {
1093/// Prints the category info in indented fashion
1094/// This function is only used by print_whole_category_list() above
c2cb4545 1095
1096 global $CFG;
b48f834c 1097 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1098
b48f834c 1099 if (empty($strsummary)) {
e05bcf2f 1100 $strallowguests = get_string('allowguests');
1101 $strrequireskey = get_string('requireskey');
1102 $strsummary = get_string('summary');
b48f834c 1103 }
ba2e5d73 1104
e05bcf2f 1105 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1106
e1638d61 1107 $frontpage = explode(',', $CFG->frontpage);
1108 $frontpage = $frontpage?array_flip($frontpage):array();
48643b49 1109 $coursecount = count_records('course') <= 200;
1110 if (isset($frontpage[FRONTPAGECATEGORYNAMES]) && !isset($frontpage[FRONTPAGECOURSELIST]) && $coursecount) {
839f2456 1111 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" width="16" height="16" border="0" alt="" />';
b48f834c 1112 } else {
7b0b5c14 1113 $catimage = "&nbsp;";
8ef9cb56 1114 }
b48f834c 1115
87d32352 1116 echo "\n\n".'<table border="0" cellpadding="3" cellspacing="0" width="100%">';
d2b6ba70 1117
48643b49 1118 if (isset($frontpage[FRONTPAGECATEGORYNAMES]) && !isset($frontpage[FRONTPAGECOURSELIST]) && $coursecount) {
19374e76 1119 $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.guest,c.cost,c.currency');
b48f834c 1120
1121 echo "<tr>";
1122
1123 if ($depth) {
1124 $indent = $depth*30;
1125 $rows = count($courses) + 1;
1c919752 1126 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1127 print_spacer(10, $indent);
e05bcf2f 1128 echo '</td>';
b48f834c 1129 }
89adb174 1130
e05bcf2f 1131 echo '<td valign="top">'.$catimage.'</td>';
290130b3 1132 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1133 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1134 echo '</td>';
290130b3 1135 echo '<td class="category info">&nbsp;</td>';
e05bcf2f 1136 echo '</tr>';
b48f834c 1137
9ff5310a 1138 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1139 foreach ($courses as $course) {
e05bcf2f 1140 $linkcss = $course->visible ? '' : ' class="dimmed" ';
1141 echo '<tr><td valign="top" width="30">&nbsp;';
290130b3 1142 echo '</td><td valign="top" width="100%" class="course name">';
e05bcf2f 1143 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>';
290130b3 1144 echo '</td><td align="right" valign="top" nowrap="nowrap" class="course info">';
c2cb4545 1145 if ($course->guest ) {
e05bcf2f 1146 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1147 echo '<img hspace="1" alt="'.$strallowguests.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1148 } else {
e05bcf2f 1149 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1150 }
c2cb4545 1151 if ($course->password) {
e05bcf2f 1152 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1153 echo '<img hspace="1" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1154 } else {
e05bcf2f 1155 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1156 }
1157 if ($course->summary) {
e05bcf2f 1158 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
1159 '<img hspace="1" alt="'.$strsummary.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1160 400, 500, $strsummary);
ebe8ddc1 1161 } else {
e05bcf2f 1162 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1163 }
e05bcf2f 1164 echo '</td></tr>';
0c656181 1165 }
ba2e5d73 1166 }
d2b6ba70 1167 } else {
b48f834c 1168
e0140f24 1169 echo '<tr>';
1170
b48f834c 1171 if ($depth) {
1172 $indent = $depth*20;
e05bcf2f 1173 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1174 print_spacer(10, $indent);
e05bcf2f 1175 echo '</td>';
d2b6ba70 1176 }
89adb174 1177
290130b3 1178 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1179 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1180 echo '</td>';
290130b3 1181 echo '<td valign="top" class="category number">';
e05bcf2f 1182 if ($category->coursecount) {
1183 echo $category->coursecount;
1184 }
1185 echo '</td></tr>';
c2cb4545 1186 }
e05bcf2f 1187 echo '</table>';
c2cb4545 1188}
1189
c2cb4545 1190
8e227aa7 1191function print_courses($category, $width="100%", $hidesitecourse = false) {
c2cb4545 1192/// Category is 0 (for all courses) or an object
1193
810393c8 1194 global $CFG;
c2cb4545 1195
1196 if (empty($category)) {
90c2ca2e 1197 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1198 if (count($categories) == 1) {
1199 $category = array_shift($categories);
0cb9ec10 1200 $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol');
90c2ca2e 1201 } else {
0cb9ec10 1202 $courses = get_courses('all', 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol');
90c2ca2e 1203 }
1204 unset($categories);
607809b3 1205 } else {
c2cb4545 1206 $categories = get_categories($category->id); // sub categories
0cb9ec10 1207 $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol');
c2cb4545 1208 }
1209
c2cb4545 1210 if ($courses) {
1211 foreach ($courses as $course) {
8e227aa7 1212 if ($hidesitecourse && !$course->category) {
1213 continue;
1214 }
c2cb4545 1215 print_course($course, $width);
c2cb4545 1216 }
1217 } else {
f9667a5a 1218 print_heading(get_string("nocoursesyet"));
255d1033 1219 if (iscreator()) { // Make it obvious for newbies on new sites how to add a course
1220 $options = array();
1221 $options['category'] = $category->id;
1222 echo '<div class="addcoursebutton" align="center">';
1223 print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string("addnewcourse"));
1224 echo '</div>';
1225 }
c2cb4545 1226 }
1227
1228}
1229
1230
1231function print_course($course, $width="100%") {
1232
861efb19 1233 global $CFG, $USER;
c2cb4545 1234
f9667a5a 1235 require_once("$CFG->dirroot/enrol/enrol.class.php");
146bbb8f 1236
f9667a5a 1237 $enrol = enrolment_factory::factory($course->enrol);
146bbb8f 1238
810393c8 1239 print_simple_box_start("center", "$width", '', 5, "coursebox");
c2cb4545 1240
22288704 1241 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1242
c2cb4545 1243 echo "<table width=\"100%\">";
e5e81e78 1244 echo '<tr valign="top">';
290130b3 1245 echo '<td valign="top" width="50%" class="info">';
34b5847a 1246 echo '<b><a title="'.get_string('entercourse').'"'.
e5e81e78 1247 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
34b5847a 1248 $course->fullname.'</a></b><br />';
c2cb4545 1249 if ($teachers = get_course_teachers($course->id)) {
290130b3 1250 echo "<span class=\"teachers\">\n";
c2cb4545 1251 foreach ($teachers as $teacher) {
1252 if ($teacher->authority > 0) {
1253 if (!$teacher->role) {
1254 $teacher->role = $course->teacher;
1255 }
2ac64806 1256 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
8bd84b4d 1257 echo $teacher->role.': <a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id.
1258 '&amp;course='.SITEID.'">'.$fullname.'</a><br />';
c2cb4545 1259 }
1260 }
34b5847a 1261 echo "</span>\n";
c2cb4545 1262 }
c2cb4545 1263
146bbb8f 1264 echo $enrol->get_access_icons($course);
c2cb4545 1265
290130b3 1266 echo '</td><td valign="top" width="50%" class="summary">';
9f39c190 1267 $options = NULL;
1268 $options->noclean = true;
34b5847a 1269 $options->para = false;
9f39c190 1270 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
c2cb4545 1271 echo "</td></tr>";
1272 echo "</table>";
1273
1274 print_simple_box_end();
1275}
1276
1277
1278function print_my_moodle() {
1279/// Prints custom user information on the home page.
1280/// Over time this can include all sorts of information
1281
1282 global $USER, $CFG;
1283
1284 if (!isset($USER->id)) {
1285 error("It shouldn't be possible to see My Moodle without being logged in.");
1286 }
1287
1288 if ($courses = get_my_courses($USER->id)) {
1289 foreach ($courses as $course) {
c81696e5 1290 if (!$course->category) {
1291 continue;
1292 }
c2cb4545 1293 print_course($course, "100%");
c2cb4545 1294 }
38a10939 1295
7f989948 1296 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1297 echo "<table width=\"100%\"><tr><td align=\"center\">";
1298 print_course_search("", false, "short");
1299 echo "</td><td align=\"center\">";
1300 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1301 echo "</td></tr></table>\n";
1302 }
26330001 1303 } else {
1304 if (count_records("course_categories") > 1) {
cb29b020 1305 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1306 print_whole_category_list();
1307 print_simple_box_end();
1308 } else {
1309 print_courses(0, "100%");
1310 }
607809b3 1311 }
2b8cef80 1312}
1313
11b0c469 1314
a8b56716 1315function print_course_search($value="", $return=false, $format="plain") {
38a10939 1316
1317 global $CFG;
1318
1319 $strsearchcourses= get_string("searchcourses");
1320
1c919752 1321 if ($format == 'plain') {
87d32352 1322 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1323 $output .= '<center><p align="center" class="coursesearchbox">';
f8a5159a 1324 $output .= '<input type="text" size="30" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1325 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1326 $output .= '</p></center></form>';
1c919752 1327 } else if ($format == 'short') {
87d32352 1328 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1329 $output .= '<center><p align="center" class="coursesearchbox">';
f8a5159a 1330 $output .= '<input type="text" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1331 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1332 $output .= '</p></center></form>';
1c919752 1333 } else if ($format == 'navbar') {
87d32352 1334 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1335 $output .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
f8a5159a 1336 $output .= '<input type="text" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1337 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
1c919752 1338 $output .= '</td></tr></table>';
87d32352 1339 $output .= '</form>';
a8b56716 1340 }
1341
1342 if ($return) {
1343 return $output;
1344 }
1345 echo $output;
38a10939 1346}
11b0c469 1347
1348/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1349
1350function add_course_module($mod) {
11b0c469 1351
e5dfd0f3 1352 $mod->added = time();
53f4ad2c 1353 unset($mod->id);
11b0c469 1354
e5dfd0f3 1355 return insert_record("course_modules", $mod);
11b0c469 1356}
1357
7977cffd 1358function add_mod_to_section($mod, $beforemod=NULL) {
1359/// Given a full mod object with section and course already defined
1360/// If $before is specified, then this is an existing ID which we
1361/// will insert the new module before
1362///
1363/// Returns the course_sections ID where the mod is inserted
11b0c469 1364
9fa49e22 1365 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1366
1367 $section->sequence = trim($section->sequence);
1368
1369 if (empty($section->sequence)) {
11b0c469 1370 $newsequence = "$mod->coursemodule";
7977cffd 1371
1372 } else if ($beforemod) {
1373 $modarray = explode(",", $section->sequence);
1374
1375 if ($key = array_keys ($modarray, $beforemod->id)) {
1376 $insertarray = array($mod->id, $beforemod->id);
1377 array_splice($modarray, $key[0], 1, $insertarray);
1378 $newsequence = implode(",", $modarray);
1379
1380 } else { // Just tack it on the end anyway
1381 $newsequence = "$section->sequence,$mod->coursemodule";
1382 }
1383
1384 } else {
1385 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1386 }
89adb174 1387
e5dfd0f3 1388 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1389 return $section->id; // Return course_sections ID that was used.
11b0c469 1390 } else {
e5dfd0f3 1391 return 0;
11b0c469 1392 }
89adb174 1393
11b0c469 1394 } else { // Insert a new record
e5dfd0f3 1395 $section->course = $mod->course;
1396 $section->section = $mod->section;
1397 $section->summary = "";
1398 $section->sequence = $mod->coursemodule;
1399 return insert_record("course_sections", $section);
11b0c469 1400 }
1401}
1402
48e535bc 1403function set_coursemodule_groupmode($id, $groupmode) {
3d575e6f 1404 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1405}
1406
02f66c42 1407/**
1408* $prevstateoverrides = true will set the visibility of the course module
1409* to what is defined in visibleold. This enables us to remember the current
1410* visibility when making a whole section hidden, so that when we toggle
1411* that section back to visible, we are able to return the visibility of
1412* the course module back to what it was originally.
1413*/
1414function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
48e535bc 1415 $cm = get_record('course_modules', 'id', $id);
dcd338ff 1416 $modulename = get_field('modules', 'name', 'id', $cm->module);
1417 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1418 foreach($events as $event) {
48e535bc 1419 if ($visible) {
1420 show_event($event);
1421 } else {
1422 hide_event($event);
1423 }
dcd338ff 1424 }
1425 }
02f66c42 1426 if ($prevstateoverrides) {
1427 if ($visible == '0') {
1428 // Remember the current visible state so we can toggle this back.
1429 set_field('course_modules', 'visibleold', $cm->visible, 'id', $id);
1430 } else {
1431 // Get the previous saved visible states.
1432 return set_field('course_modules', 'visible', $cm->visibleold, 'id', $id);
1433 }
1434 }
48e535bc 1435 return set_field("course_modules", "visible", $visible, "id", $id);
1acfbce5 1436}
1437
290130b3 1438/*
1439 * Delete a course module and any associated data at the course level (events)
1440 * Until 1.5 this function simply marked a deleted flag ... now it
1441 * deletes it completely.
1442 *
1443 */
48e535bc 1444function delete_course_module($id) {
290130b3 1445 if (!$cm = get_record('course_modules', 'id', $id)) {
1446 return true;
1447 }
dcd338ff 1448 $modulename = get_field('modules', 'name', 'id', $cm->module);
1449 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1450 foreach($events as $event) {
48e535bc 1451 delete_event($event);
dcd338ff 1452 }
1453 }
290130b3 1454 return delete_records('course_modules', 'id', $cm->id);
11b0c469 1455}
1456
1457function delete_mod_from_section($mod, $section) {
11b0c469 1458
e5dfd0f3 1459 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1460
e5dfd0f3 1461 $modarray = explode(",", $section->sequence);
11b0c469 1462
1463 if ($key = array_keys ($modarray, $mod)) {
1464 array_splice($modarray, $key[0], 1);
1465 $newsequence = implode(",", $modarray);
e5dfd0f3 1466 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1467 } else {
1468 return false;
1469 }
89adb174 1470
11b0c469 1471 }
7977cffd 1472 return false;
11b0c469 1473}
1474
12905134 1475function move_section($course, $section, $move) {
1476/// Moves a whole course section up and down within the course
798b70a1 1477 global $USER;
12905134 1478
1479 if (!$move) {
1480 return true;
1481 }
1482
1483 $sectiondest = $section + $move;
1484
1485 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1486 return false;
1487 }
1488
1489 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1490 return false;
1491 }
1492
1493 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1494 return false;
1495 }
1496
56e34ee4 1497 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1498 return false;
1499 }
56e34ee4 1500 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1501 return false;
1502 }
798b70a1 1503 // if the focus is on the section that is being moved, then move the focus along
1504 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
1505 course_set_display($course->id, $sectiondest);
1506 }
5390cbb7 1507
1508 // Check for duplicates.
1509 // There is a very rare case that some sections in the same course have the same section id.
1510 if (($count_section = count_records('course_sections', 'course', $course->id) - 1) != $course->numsections) {
1511 $sections = get_records_select('course_sections', "course = $course->id AND section > 0", 'section ASC');
1512 $n = 1;
1513 foreach ($sections as $section) {
1514 if (!set_field('course_sections', 'section', $n, 'id', $section->id)) {
1515 return false;
1516 }
1517 $n++;
1518 }
1519 if (!set_field('course', 'numsections', $count_section, 'id', $course->id)) {
1520 return false;
1521 }
1522 }
12905134 1523 return true;
1524}
1525
1526
7977cffd 1527function moveto_module($mod, $section, $beforemod=NULL) {
1528/// All parameters are objects
1529/// Move the module object $mod to the specified $section
1530/// If $beforemod exists then that is the module
1531/// before which $modid should be inserted
1532
1533/// Remove original module from original section
1534
1535 if (! delete_mod_from_section($mod->id, $mod->section)) {
1536 notify("Could not delete module from existing section");
1537 }
1538
1539/// Update module itself if necessary
1540
1541 if ($mod->section != $section->id) {
89adb174 1542 $mod->section = $section->id;
7977cffd 1543 if (!update_record("course_modules", $mod)) {
1544 return false;
1545 }
48e535bc 1546 // if moving to a hidden section then hide module
1547 if (!$section->visible) {
1548 set_coursemodule_visible($mod->id, 0);
1549 }
7977cffd 1550 }
1551
1552/// Add the module into the new section
1553
1554 $mod->course = $section->course;
1555 $mod->section = $section->section; // need relative reference
1556 $mod->coursemodule = $mod->id;
1557
1558 if (! add_mod_to_section($mod, $beforemod)) {
1559 return false;
1560 }
1561
1562 return true;
1563
1564}
1565
24e1eae4 1566function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
810393c8 1567 global $CFG, $USER;
94361e02 1568
3d575e6f 1569 static $str;
37a88449 1570 static $sesskey;
3d575e6f 1571
1572 if (!isset($str)) {
1573 $str->delete = get_string("delete");
1574 $str->move = get_string("move");
1575 $str->moveup = get_string("moveup");
1576 $str->movedown = get_string("movedown");
1577 $str->moveright = get_string("moveright");
1578 $str->moveleft = get_string("moveleft");
1579 $str->update = get_string("update");
d33a2a6f 1580 $str->duplicate = get_string("duplicate");
3d575e6f 1581 $str->hide = get_string("hide");
1582 $str->show = get_string("show");
1583 $str->clicktochange = get_string("clicktochange");
32d03b7b 1584 $str->forcedmode = get_string("forcedmode");
3d575e6f 1585 $str->groupsnone = get_string("groupsnone");
1586 $str->groupsseparate = get_string("groupsseparate");
1587 $str->groupsvisible = get_string("groupsvisible");
37a88449 1588 $sesskey = sesskey();
1acfbce5 1589 }
94361e02 1590
24e1eae4 1591 if ($section >= 0) {
75f087b6 1592 $section = '&amp;sr='.$section; // Section return
24e1eae4 1593 } else {
1594 $section = '';
1595 }
1596
94361e02 1597 if ($absolute) {
37a88449 1598 $path = $CFG->wwwroot.'/course';
dc0dc7d5 1599 } else {
37a88449 1600 $path = '.';
dc0dc7d5 1601 }
1602
3d575e6f 1603 if ($mod->visible) {
37a88449 1604 $hideshow = '<a title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id.
1605 '&amp;sesskey='.$sesskey.$section.'"><img'.
1606 ' src="'.$CFG->pixpath.'/t/hide.gif" hspace="2" height="11" width="11" '.
1607 ' border="0" alt="'.$str->hide.'" /></a> ';
1acfbce5 1608 } else {
37a88449 1609 $hideshow = '<a title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id.
1610 '&amp;sesskey='.$sesskey.$section.'"><img'.
1611 ' src="'.$CFG->pixpath.'/t/show.gif" hspace="2" height="11" width="11" '.
1612 ' border="0" alt="'.$str->show.'" /></a> ';
7977cffd 1613 }
3d575e6f 1614 if ($mod->groupmode !== false) {
1615 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1616 $grouptitle = $str->groupsseparate;
37a88449 1617 $groupimage = $CFG->pixpath.'/t/groups.gif';
1618 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=0&amp;sesskey='.$sesskey;
3d575e6f 1619 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1620 $grouptitle = $str->groupsvisible;
37a88449 1621 $groupimage = $CFG->pixpath.'/t/groupv.gif';
1622 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=1&amp;sesskey='.$sesskey;
32d03b7b 1623 } else {
1624 $grouptitle = $str->groupsnone;
37a88449 1625 $groupimage = $CFG->pixpath.'/t/groupn.gif';
1626 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=2&amp;sesskey='.$sesskey;
32d03b7b 1627 }
1628 if ($mod->groupmodelink) {
37a88449 1629 $groupmode = '<a title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'.
1630 '<img src="'.$groupimage.'" hspace="2" height="11" width="11" '.
1631 'border="0" alt="'.$grouptitle.'" /></a>';
3d575e6f 1632 } else {
37a88449 1633 $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '.
1634 ' src="'.$groupimage.'" hspace="2" height="11" width="11" '.
1635 'border="0" alt="'.$grouptitle.'" />';
3d575e6f 1636 }
1637 } else {
1638 $groupmode = "";
1639 }
7977cffd 1640
1641 if ($moveselect) {
37a88449 1642 $move = '<a title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id.
1643 '&amp;sesskey='.$sesskey.$section.'"><img'.
1644 ' src="'.$CFG->pixpath.'/t/move.gif" hspace="2" height="11" width="11" '.
1645 ' border="0" alt="'.$str->move.'" /></a>';
493486c4 1646 } else {
37a88449 1647 $move = '<a title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id.
1648 '&amp;move=-1&amp;sesskey='.$sesskey.$section.'"><img'.
1649 ' src="'.$CFG->pixpath.'/t/up.gif" hspace="2" height="11" width="11" '.
1650 ' border="0" alt="'.$str->moveup.'" /></a>'.
1651 '<a title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id.
1652 '&amp;move=1&amp;sesskey='.$sesskey.$section.'"><img'.
1653 ' src="'.$CFG->pixpath.'/t/down.gif" hspace="2" height="11" width="11" '.
1654 ' border="0" alt="'.$str->movedown.'" /></a>';
7977cffd 1655 }
1656
aac94fd0 1657 $leftright = "";
1658 if ($indent > 0) {
37a88449 1659 $leftright .= '<a title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id.
1660 '&amp;indent=-1&amp;sesskey='.$sesskey.$section.'"><img'.
1661 ' src="'.$CFG->pixpath.'/t/left.gif" hspace="2" height="11" width="11" '.
1662 ' border="0" alt="'.$str->moveleft.'" /></a>';
aac94fd0 1663 }
1664 if ($indent >= 0) {
37a88449 1665 $leftright .= '<a title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id.
1666 '&amp;indent=1&amp;sesskey='.$sesskey.$section.'"><img'.
1667 ' src="'.$CFG->pixpath.'/t/right.gif" hspace="2" height="11" width="11" '.
1668 ' border="0" alt="'.$str->moveright.'" /></a>';
1669 }
1670
1671 return '<span class="commands">'.$leftright.$move.
1672 '<a title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id.
1673 '&amp;sesskey='.$sesskey.$section.'"><img'.
1674 ' src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" '.
1675 ' alt="'.$str->update.'" /></a>'.
1676 '<a title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id.
1677 '&amp;sesskey='.$sesskey.$section.'"><img'.
1678 ' src="'.$CFG->pixpath.'/t/delete.gif" hspace="2" height="11" width="11" border="0" '.
1679 ' alt="'.$str->delete.'" /></a>'.$hideshow.$groupmode.'</span>';
90845098 1680}
1681
b61efafb 1682/**
1683 * given a course object with shortname & fullname, this function will
1684 * truncate the the number of chars allowed and add ... if it was too long
1685 */
1686function course_format_name ($course,$max=100) {
1687
1688 $str = $course->shortname.': '.$course->fullname;
1689 if (strlen($str) <= $max) {
1690 return $str;
1691 }
1692 else {
1693 return substr($str,0,$max-3).'...';
1694 }
1695}
1696
1697/**
1698 * This function will return true if the given course is a child course at all
1699 */
1700function course_in_meta ($course) {
5f37b628 1701 return record_exists("course_meta","child_course",$course->id);
b61efafb 1702}
1703
48e535bc 1704
1705/**
1706 * Print standard form elements on module setup forms in mod/.../mod.html
1707 */
1708function print_standard_coursemodule_settings($form) {
da2224f8 1709 if (! $course = get_record('course', 'id', $form->course)) {
1710 error("This course doesn't exist");
1711 }
1712 print_groupmode_setting($form, $course);
1713 print_visible_setting($form, $course);
48e535bc 1714}
1715
1716/**
1717 * Print groupmode form element on module setup forms in mod/.../mod.html
1718 */
5ebb746b 1719function print_groupmode_setting($form, $course=NULL) {
48e535bc 1720
5ebb746b 1721 if (empty($course)) {
1722 if (! $course = get_record('course', 'id', $form->course)) {
1723 error("This course doesn't exist");
1724 }
1725 }
48e535bc 1726 if ($form->coursemodule) {
1727 if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
1728 error("This course module doesn't exist");
1729 }
1730 } else {
1731 $cm = null;
1732 }
1733 $groupmode = groupmode($course, $cm);
1734 if ($course->groupmode or (!$course->groupmodeforce)) {
1735 echo '<tr valign="top">';
1736 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
7bbe08a2 1737 echo '<td align="left">';
48e535bc 1738 unset($choices);
1739 $choices[NOGROUPS] = get_string('groupsnone');
1740 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
1741 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
1742 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
1743 helpbutton('groupmode', get_string('groupmode'));
1744 echo '</td></tr>';
1745 }
1746}
1747
1748/**
1749 * Print visibility setting form element on module setup forms in mod/.../mod.html
1750 */
5ebb746b 1751function print_visible_setting($form, $course=NULL) {
1ee55c41 1752 if (empty($course)) {
1753 if (! $course = get_record('course', 'id', $form->course)) {
1754 error("This course doesn't exist");
1755 }
1756 }
48e535bc 1757 if ($form->coursemodule) {
1758 $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
1759 } else {
1760 $visible = true;
1761 }
1762
1763 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
1764 $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
1765 } else {
1766 $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
1767 }
1768 if ($hiddensection) {
1769 $visible = false;
1770 }
1771
1772 echo '<tr valign="top">';
1f15db9d 1773 echo '<td align="right"><b>'.get_string('visibletostudents','',moodle_strtolower($course->students)).':</b></td>';
7bbe08a2 1774 echo '<td align="left">';
48e535bc 1775 unset($choices);
1f15db9d 1776 $choices[1] = get_string('show');
1777 $choices[0] = get_string('hide');
48e535bc 1778 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
1779 echo '</td></tr>';
1780}
1781
0705ff84 1782function update_restricted_mods($course,$mods) {
1783 delete_records("course_allowed_modules","course",$course->id);
1784 if (empty($course->restrictmodules)) {
1785 return;
1786 }
1787 else {
1788 foreach ($mods as $mod) {
1789 if ($mod == 0)
1790 continue; // this is the 'allow none' option
1791 $am->course = $course->id;
1792 $am->module = $mod;
1793 insert_record("course_allowed_modules",$am);
1794 }
1795 }
1796}
1797
1798/**
1799 * This function will take an int (module id) or a string (module name)
1800 * and return true or false, whether it's allowed in the given course (object)
1801 * $mod is not allowed to be an object, as the field for the module id is inconsistent
1802 * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
1803 */
1804
1805function course_allowed_module($course,$mod) {
1806 if (empty($course->restrictmodules)) {
1807 return true;
1808 }
1809 if (isadmin()) {
1810 return true;
1811 }
1812 if (is_numeric($mod)) {
1813 $modid = $mod;
1814 } else if (is_string($mod)) {
1815 if ($mod = get_field("modules","id","name",strtolower($mod)))
1816 $modid = $mod;
1817 }
1818 if (empty($modid)) {
1819 return false;
1820 }
1821 return (record_exists("course_allowed_modules","course",$course->id,"module",$modid));
1822}
1823
861efb19 1824/***
1825 *** Efficiently moves many courses around while maintaining
1826 *** sortorder in order.
1827 ***
1828 *** $courseids is an array of course ids
1829 ***
1830 **/
1831
1832function move_courses ($courseids, $categoryid) {
1833
1834 global $CFG;
1835
1836 if (!empty($courseids)) {
1837
1838 $courseids = array_reverse($courseids);
1839
1840 foreach ($courseids as $courseid) {
1841
1842 if (! $course = get_record("course", "id", $courseid)) {
1843 notify("Error finding course $courseid");
1844 } else {
1845 // figure out a sortorder that we can use in the destination category
1846 $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
1847 FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid);
1848 if ($sortorder === false) {
1849 // the category is empty
1850 // rather than let the db default to 0
1851 // set it to > 100 and avoid extra work in fix_coursesortorder()
1852 $sortorder = 200;
1853 } else if ($sortorder < 10) {
1854 fix_course_sortorder($categoryid);
1855 }
1856
1857 $course->category = $categoryid;
1858 $course->sortorder = $sortorder;
1859 $course->fullname = addslashes($course->fullname);
1860 $course->shortname = addslashes($course->shortname);
1861 $course->summary = addslashes($course->summary);
1862 $course->password = addslashes($course->password);
1863 $course->teacher = addslashes($course->teacher);
1864 $course->teachers = addslashes($course->teachers);
1865 $course->student = addslashes($course->student);
1866 $course->students = addslashes($course->students);
1867
1868 if (!update_record('course', $course)) {
1869 notify("An error occurred - course not moved!");
1870 }
1871 }
1872 }
1873 fix_course_sortorder();
1874 }
1875 return true;
1876}
1877
87d32352 1878?>