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