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