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