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