Added the 'get_recent_activity' function (see bug 1897).
[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
d7d145b1 604 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
605 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
6ac98433 606 $log->url = str_replace('&', '&amp;', $log->url); /// XHTML compatibility
d7d145b1 607
6ac98433 608 echo '<tr>';
720a43ce 609 if (! $course->category) {
6ac98433 610 echo '<td nowrap="nowrap"><font size="2"><a href="view.php?id='.$log->course.'">'.$courses[$log->course].'</a></font></td>';
720a43ce 611 }
6ac98433 612 echo '<td nowrap="nowrap" align="right"><font size="2">'.userdate($log->time, '%a').'</font></td>';
613 echo '<td nowrap="nowrap"><font size="2">'.userdate($log->time, $strftimedatetime).'</font></td>';
1c919752 614 echo '<td nowrap="nowrap"><font size="2">';
839f2456 615 link_to_popup_window("/lib/ipatlas/plot.php?address=$log->ip&amp;user=$log->userid", 'ipatlas',$log->ip, 400, 700);
6ac98433 616 echo '</font></td>';
2ac64806 617 $fullname = fullname($log, $isteacher);
6ac98433 618 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 619 echo '<td nowrap="nowrap"><font size="2">';
2eb68e6f 620 link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 400, 600);
6ac98433 621 echo '</font></td>';
622 echo '<td nowrap="nowrap"><font size="2">'.$log->info.'</font></td>';
2eb68e6f 623 echo '</tr>';
f9903ed0 624 }
2eb68e6f 625 echo '</table>';
519d369f 626
8f0cd6ef 627 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
f9903ed0 628}
629
630
c2cb4545 631function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
632 global $CFG;
633 if (empty($CFG->gdversion)) {
634 echo "(".get_string("gdneed").")";
d887b5a7 635 } else {
1c919752 636 echo '<img border="0" src="'.$CFG->wwwroot.'/course/loggraph.php?id='.$course->id.
839f2456 637 '&amp;user='.$userid.'&amp;type='.$type.'&amp;date='.$date.'" alt=\"\" />';
d887b5a7 638 }
639}
640
641
f9903ed0 642
600149be 643function print_recent_activity($course) {
644 // $course is an object
89adb174 645 // This function trawls through the logs looking for
600149be 646 // anything new since the user's last login
647
810393c8 648 global $CFG, $USER, $SESSION;
600149be 649
2ac64806 650 $isteacher = isteacher($course->id);
651
6f80940b 652 $timestart = time() - COURSE_MAX_RECENT_PERIOD;
0f87cb1d 653
9e51847a 654 if (!empty($USER->timeaccess[$course->id])) {
6f80940b 655 if ($USER->timeaccess[$course->id] > $timestart) {
9e51847a 656 $timestart = $USER->timeaccess[$course->id];
657 }
3d891989 658 }
0f87cb1d 659
de785682 660 echo '<div class="activitydate">';
0f87cb1d 661 echo get_string("activitysince", "", userdate($timestart));
de785682 662 echo '</div>';
663 echo '<div class="activityhead">';
0f87cb1d 664
de785682 665 echo '<a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a>';
0f87cb1d 666
de785682 667 echo '</div>';
0f87cb1d 668
600149be 669
670 // Firstly, have there been any new enrolments?
671
672 $heading = false;
673 $content = false;
1b5910c4 674
6c38b7e0 675 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 676
6c38b7e0 677 if ($users) {
e2b7251d 678 echo "<p>";
6c38b7e0 679 foreach ($users as $user) {
600149be 680 if (! $heading) {
4c654ee3 681 print_headline(get_string("newusers").":");
600149be 682 $heading = true;
683 $content = true;
684 }
2ac64806 685 $fullname = fullname($user, $isteacher);
839f2456 686 echo '<font size="1"><a href="'.$CFG->wwwroot."/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></font><br />";
600149be 687 }
e2b7251d 688 echo "</p>";
600149be 689 }
690
1b5910c4 691 // Next, have there been any modifications to the course structure?
692
89adb174 693 $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND
1b5910c4 694 module = 'course' AND action LIKE '% mod'", "time ASC");
695
696 if ($logs) {
697 foreach ($logs as $key => $log) {
698 $info = split(" ", $log->info);
c9f6251e 699
700 if ($info[0] == "label") { // Labels are special activities
701 continue;
702 }
703
1b5910c4 704 $modname = get_field($info[0], "name", "id", $info[1]);
705 //Create a temp valid module structure (course,id)
706 $tempmod->course = $log->course;
707 $tempmod->id = $info[1];
708 //Obtain the visible property from the instance
709 $modvisible = instance_is_visible($info[0],$tempmod);
89adb174 710
1b5910c4 711 //Only if the mod is visible
712 if ($modvisible) {
713 switch ($log->action) {
714 case "add mod":
715 $stradded = get_string("added", "moodle", get_string("modulename", $info[0]));
24f2b65a 716 $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:<br /><a href=\"$CFG->wwwroot/course/$log->url\">$modname</a>");
1b5910c4 717 break;
718 case "update mod":
719 $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0]));
720 if (empty($changelist["$log->info"])) {
24f2b65a 721 $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:<br /><a href=\"$CFG->wwwroot/course/$log->url\">$modname</a>");
1b5910c4 722 }
723 break;
724 case "delete mod":
89adb174 725 if (!empty($changelist["$log->info"]["operation"]) and
1b5910c4 726 $changelist["$log->info"]["operation"] == "add") {
727 $changelist["$log->info"] = NULL;
728 } else {
729 $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0]));
730 $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted);
731 }
732 break;
600149be 733 }
ef25340c 734 }
735 }
736 }
737
9c9f7d77 738 if (!empty($changelist)) {
ef25340c 739 foreach ($changelist as $changeinfo => $change) {
740 if ($change) {
741 $changes[$changeinfo] = $change;
742 }
743 }
8a59942e 744 if (isset($changes)){
745 if (count($changes) > 0) {
746 print_headline(get_string("courseupdates").":");
747 $content = true;
748 foreach ($changes as $changeinfo => $change) {
1c919752 749 echo '<p><font size="1">'.$change['text'].'</font></p>';
8a59942e 750 }
600149be 751 }
752 }
89adb174 753 }
bf40f9c1 754
755
756 // If this site uses Library module, then print recent items
757 if (!empty($CFG->librarypath)) {
758 if (file_exists("$CFG->dirroot/$CFG->librarypath/librarylib.php")) {
759 include_once("$CFG->dirroot/$CFG->librarypath/librarylib.php");
760 if (librarysummarize(5, '', date('YmdHis',$USER->lastlogin))) {
761 $content = true;
762 }
763 }
600149be 764 }
765
3869a2ac 766 // Now display new things from each module
600149be 767
0fd7da81 768 $mods = get_records("modules", "visible", "1", "name", "id, name");
769
1b5910c4 770 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
0fd7da81 771 include_once("$CFG->dirroot/mod/$mod->name/lib.php");
772 $print_recent_activity = $mod->name."_print_recent_activity";
1b5910c4 773 if (function_exists($print_recent_activity)) {
774 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
3869a2ac 775 if ($modcontent) {
776 $content = true;
600149be 777 }
600149be 778 }
779 }
780
781 if (! $content) {
1c919752 782 echo '<font size="2">'.get_string('nothingnew').'</font>';
600149be 783 }
600149be 784}
785
e1360728 786
d897cae4 787function get_array_of_activities($courseid) {
89adb174 788// For a given course, returns an array of course activity objects
d897cae4 789// Each item in the array contains he following properties:
790// cm - course module id
791// mod - name of the module (eg forum)
792// section - the number of the section (eg week or topic)
793// name - the name of the instance
5867bfb5 794// visible - is the instance visible or not
86aa7ccf 795// extra - contains extra string to include in any link
d897cae4 796
8dddba42 797 global $CFG;
798
d897cae4 799 $mod = array();
800
9fa49e22 801 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 802 return NULL;
803 }
804
805 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
806 foreach ($sections as $section) {
74666583 807 if (!empty($section->sequence)) {
d897cae4 808 $sequence = explode(",", $section->sequence);
809 foreach ($sequence as $seq) {
7af6281f 810 if (empty($rawmods[$seq])) {
811 continue;
812 }
d897cae4 813 $mod[$seq]->cm = $rawmods[$seq]->id;
814 $mod[$seq]->mod = $rawmods[$seq]->modname;
815 $mod[$seq]->section = $section->section;
816 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 817 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 818 $mod[$seq]->extra = "";
8dddba42 819
820 $modname = $mod[$seq]->mod;
821 $functionname = $modname."_get_coursemodule_info";
822
823 include_once("$CFG->dirroot/mod/$modname/lib.php");
824
825 if (function_exists($functionname)) {
9d361034 826 if ($info = $functionname($rawmods[$seq])) {
827 if (!empty($info->extra)) {
828 $mod[$seq]->extra = $info->extra;
829 }
830 if (!empty($info->icon)) {
831 $mod[$seq]->icon = $info->icon;
832 }
c9f6251e 833 }
834 }
d897cae4 835 }
836 }
837 }
838 }
839 return $mod;
840}
841
842
843
e1360728 844
90845098 845function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
846// Returns a number of useful structures for course displays
7468bf01 847
90845098 848 $mods = NULL; // course modules indexed by id
e0161bff 849 $modnames = NULL; // all course module names (except resource!)
94361e02 850 $modnamesplural= NULL; // all course module names (plural form)
90845098 851 $modnamesused = NULL; // course module names used
7468bf01 852
9fa49e22 853 if ($allmods = get_records("modules")) {
90845098 854 foreach ($allmods as $mod) {
5867bfb5 855 if ($mod->visible) {
856 $modnames[$mod->name] = get_string("modulename", "$mod->name");
857 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
858 }
90845098 859 }
860 asort($modnames);
861 } else {
862 error("No modules are installed!");
863 }
864
9fa49e22 865 if ($rawmods = get_course_mods($courseid)) {
7468bf01 866 foreach($rawmods as $mod) { // Index the mods
867 $mods[$mod->id] = $mod;
90845098 868 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
1acfbce5 869 if ($mod->visible or isteacher($courseid)) {
870 $modnamesused[$mod->modname] = $modnames[$mod->modname];
871 }
7468bf01 872 }
c7da6f7a 873 if ($modnamesused) {
874 asort($modnamesused);
875 }
7468bf01 876 }
e0161bff 877
878 unset($modnames['resource']);
879 unset($modnames['label']);
7468bf01 880}
881
9fa49e22 882
7468bf01 883function get_all_sections($courseid) {
89adb174 884
885 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 886 "section, id, course, summary, sequence, visible");
7468bf01 887}
888
b86fc0e2 889function course_set_display($courseid, $display=0) {
890 global $USER;
891
1066e0dc 892 if (empty($USER->id)) {
b86fc0e2 893 return false;
894 }
895
896 if ($display == "all" or empty($display)) {
897 $display = 0;
898 }
899
900 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
901 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
902 } else {
903 $record->userid = $USER->id;
904 $record->course = $courseid;
905 $record->display = $display;
906 if (!insert_record("course_display", $record)) {
907 notify("Could not save your course display!");
908 }
909 }
910
911 return $USER->display[$courseid] = $display; // Note: = not ==
912}
913
7d99d695 914function set_section_visible($courseid, $sectionnumber, $visibility) {
915/// For a given course section, markes it visible or hidden,
916/// and does the same for every activity in that section
917
918 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
919 set_field("course_sections", "visible", "$visibility", "id", $section->id);
920 if (!empty($section->sequence)) {
921 $modules = explode(",", $section->sequence);
922 foreach ($modules as $moduleid) {
48e535bc 923 set_coursemodule_visible($moduleid, $visibility);
7d99d695 924 }
925 }
5867bfb5 926 rebuild_course_cache($courseid);
7d99d695 927 }
928}
ba2e5d73 929
5e367a2d 930
d897cae4 931function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 932/// Prints a section full of activity modules
7977cffd 933 global $CFG, $USER;
934
3d575e6f 935 static $groupbuttons;
32d03b7b 936 static $groupbuttonslink;
52dcc2f9 937 static $isteacher;
938 static $isediting;
7977cffd 939 static $ismoving;
940 static $strmovehere;
941 static $strmovefull;
54669989 942 static $strunreadpostsone;
52dcc2f9 943
a22f8313 944 $labelformatoptions = New stdClass;
110a32e2 945
52dcc2f9 946 if (!isset($isteacher)) {
9fd9c29b 947 $groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
32d03b7b 948 $groupbuttonslink = (!$course->groupmodeforce);
52dcc2f9 949 $isteacher = isteacher($course->id);
52dcc2f9 950 $isediting = isediting($course->id);
ff0c7de0 951 $ismoving = $isediting && ismoving($course->id);
3d575e6f 952 if ($ismoving) {
953 $strmovehere = get_string("movehere");
954 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
955 }
54669989 956 $strunreadpostsone = get_string('unreadpostsone', 'forum');
7977cffd 957 }
60bd11cf 958 $labelformatoptions->noclean = true;
94361e02 959
c408b0c4 960 $modinfo = unserialize($course->modinfo);
94361e02 961
446390fb 962 echo '<table width="'.$width.'" class="section">';
74666583 963 if (!empty($section->sequence)) {
94361e02 964
965 $sectionmods = explode(",", $section->sequence);
966
967 foreach ($sectionmods as $modnumber) {
9ae687af 968 if (empty($mods[$modnumber])) {
969 continue;
970 }
94361e02 971 $mod = $mods[$modnumber];
c9f6251e 972
52dcc2f9 973 if ($mod->visible or $isteacher) {
446390fb 974 echo '<tr><td class="activity '.$mod->modname.'">';
7977cffd 975 if ($ismoving) {
976 if ($mod->id == $USER->activitycopy) {
977 continue;
978 }
1c919752 979 echo '<a title="'.$strmovefull.'"'.
8b92f5bb 980 ' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 981 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
982 ' alt="'.$strmovehere.'" /></a><br />
1c919752 983 ';
1acfbce5 984 }
7977cffd 985 $instancename = urldecode($modinfo[$modnumber]->name);
3c89f9a8 986 if (!empty($CFG->filterall)) {
b346b424 987 $instancename = filter_text("<nolink>$instancename</nolink>", $course->id);
3c89f9a8 988 }
c9f6251e 989
86aa7ccf 990 if (!empty($modinfo[$modnumber]->extra)) {
991 $extra = urldecode($modinfo[$modnumber]->extra);
992 } else {
993 $extra = "";
994 }
c9f6251e 995
9d361034 996 if (!empty($modinfo[$modnumber]->icon)) {
997 $icon = "$CFG->pixpath/".urldecode($modinfo[$modnumber]->icon);
998 } else {
999 $icon = "$CFG->modpixpath/$mod->modname/icon.gif";
1000 }
1001
aac94fd0 1002 if ($mod->indent) {
1003 print_spacer(12, 20 * $mod->indent, false);
1004 }
1005
c9f6251e 1006 if ($mod->modname == "label") {
aac94fd0 1007 if (!$mod->visible) {
1008 echo "<span class=\"dimmed_text\">";
1009 }
179c9a50 1010 echo format_text($extra, FORMAT_HTML, $labelformatoptions);
aac94fd0 1011 if (!$mod->visible) {
1012 echo "</span>";
1013 }
c9f6251e 1014
1015 } else { // Normal activity
1016 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
1c919752 1017 echo '<img src="'.$icon.'"'.
446390fb 1018 ' class="activityicon" alt="'.$mod->modfullname.'" />'.
1019 ' <a title="'.$mod->modfullname.'" '.$linkcss.' '.$extra.
1c919752 1020 ' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.
446390fb 1021 $instancename.'</a>';
c9f6251e 1022 }
f37da850 1023 if ($CFG->forum_trackreadposts && $mod->modname == 'forum') {
1024 $groupmode = groupmode($course, $mod);
1025 $groupid = ($groupmode == SEPARATEGROUPS && !isteacheredit($course->id)) ?
1026 get_current_group($course->id) : false;
89c0e63f 1027// $unread = forum_tp_count_forum_posts($mod->instance, $groupid) -
1028// forum_tp_count_forum_read_records($USER->id, $mod->instance, $groupid);
1029 $unread = forum_tp_count_forum_unread_posts($USER->id, $mod->instance, $groupid);
d0388ebe 1030 if ($unread) {
1031 echo '<span class="unread"> <a href="'.$CFG->wwwroot.'/mod/forum/view.php?id='.$mod->id.'">';
1032 if ($unread == 1) {
1033 echo $strunreadpostsone;
1034 } else {
1035 print_string('unreadpostsnumber', 'forum', $unread);
1036 }
1037 echo '</a> </span>';
54669989 1038 }
f37da850 1039 }
1040
c9f6251e 1041 if ($isediting) {
5f390342 1042 if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource') {
32d03b7b 1043 if (! $mod->groupmodelink = $groupbuttonslink) {
1044 $mod->groupmode = $course->groupmode;
1045 }
1046
1047 } else {
3d575e6f 1048 $mod->groupmode = false;
1049 }
ffc69cd3 1050 echo "&nbsp;&nbsp;";
3d575e6f 1051 echo make_editing_buttons($mod, $absolute, true, $mod->indent);
c9f6251e 1052 }
ffc69cd3 1053 echo "</td>";
c9f6251e 1054 echo "</tr>";
94361e02 1055 }
94361e02 1056 }
87d32352 1057 } else {
1058 echo "<tr><td></td></tr>"; // needed for XHTML compatibility
94361e02 1059 }
7977cffd 1060 if ($ismoving) {
1c919752 1061 echo '<tr><td><a title="'.$strmovefull.'"'.
8b92f5bb 1062 ' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 1063 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
1064 ' alt="'.$strmovehere.'" /></a></td></tr>
1c919752 1065 ';
7977cffd 1066 }
c9f6251e 1067 echo "</table>\n\n";
a7ad3ea6 1068}
1069
5867bfb5 1070
cb57e6f4 1071function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
e0161bff 1072// Prints the menus to add activities and resources
1073
8b92f5bb 1074 global $CFG, $USER;
6da4b261 1075 static $straddactivity, $stractivities, $straddresource, $resources;
e0161bff 1076
1077 if (!isset($straddactivity)) {
1078 $straddactivity = get_string('addactivity');
1079 $straddresource = get_string('addresource');
6da4b261 1080
1081 /// Standard resource types
1082 require_once("$CFG->dirroot/mod/resource/lib.php");
1083 $resourceraw = resource_get_resource_types();
1084
1085 foreach ($resourceraw as $type => $name) {
839f2456 1086 $resources["resource&amp;type=$type"] = $name;
e0161bff 1087 }
e0161bff 1088 $resources['label'] = get_string('resourcetypelabel', 'resource');
1089 }
1090
cb57e6f4 1091 $output = '';
1092
1093 $output .= '<div align="right"><table align="right"><tr><td>';
8b92f5bb 1094 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 1095 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
cb57e6f4 1096 $output .= '</td>';
1097
1098 if ($vertical) {
1099 $output .= '</tr><tr>';
1100 }
1101
1102 $output .= '<td>';
8b92f5bb 1103 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 1104 $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
cb57e6f4 1105 $output .= '</td></tr></table>';
1106 $output .= '</div>';
1107
1108 if ($return) {
1109 return $output;
1110 } else {
1111 echo $output;
1112 }
e0161bff 1113}
1114
5867bfb5 1115function rebuild_course_cache($courseid=0) {
1116// Rebuilds the cached list of course activities stored in the database
1117// If a courseid is not specified, then all are rebuilt
1118
1119 if ($courseid) {
1120 $select = "id = '$courseid'";
1121 } else {
1122 $select = "";
1123 }
1124
1a31c2b3 1125 if ($courses = get_records_select("course", $select,'','id,fullname')) {
5867bfb5 1126 foreach ($courses as $course) {
1127 $modinfo = serialize(get_array_of_activities($course->id));
1128 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
1129 notify("Could not cache module information for course '$course->fullname'!");
1130 }
1131 }
1132 }
1133}
1134
1135
446390fb 1136function print_heading_block($heading, $class = NULL) {
1137 $divclass = 'headingblock';
1138 if(!empty($class)) {
1139 $divclass .= ' '.$class;
1140 }
1141 echo '<div class="'.$divclass.'">'.stripslashes($heading).'</div>';
5e367a2d 1142}
1143
c2cb4545 1144
1145function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 1146/// Given an empty array, this function recursively travels the
c2cb4545 1147/// categories, building up a nice list for display. It also makes
1148/// an array that list all the parents for each category.
1149
1150 if ($category) {
1151 if ($path) {
e05bcf2f 1152 $path = $path.' / '.$category->name;
c2cb4545 1153 } else {
e05bcf2f 1154 $path = $category->name;
c2cb4545 1155 }
1156 $list[$category->id] = $path;
1157 } else {
1158 $category->id = 0;
1159 }
1160
e05bcf2f 1161 if ($categories = get_categories($category->id)) { // Print all the children recursively
c2cb4545 1162 foreach ($categories as $cat) {
1163 if (!empty($category->id)) {
3bd4de22 1164 if (isset($parents[$category->id])) {
2832badf 1165 $parents[$cat->id] = $parents[$category->id];
1166 }
c2cb4545 1167 $parents[$cat->id][] = $category->id;
1168 }
89adb174 1169 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1170 }
1171 }
1172}
1173
1174
c2cb4545 1175function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
89adb174 1176/// Recursive function to print out all the categories in a nice format
c2cb4545 1177/// with or without courses included
9ff5310a 1178 global $CFG;
e05bcf2f 1179
1180 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1181 return;
9ff5310a 1182 }
c2cb4545 1183
1184 if (!$displaylist) {
e92fe848 1185 make_categories_list($displaylist, $parentslist);
c2cb4545 1186 }
1187
1188 if ($category) {
3af6e1db 1189 if ($category->visible or iscreator()) {
d2b6ba70 1190 print_category_info($category, $depth);
c2cb4545 1191 } else {
1192 return; // Don't bother printing children of invisible categories
1193 }
89adb174 1194
c2cb4545 1195 } else {
c2cb4545 1196 $category->id = "0";
1197 }
1198
1199 if ($categories = get_categories($category->id)) { // Print all the children recursively
1200 $countcats = count($categories);
1201 $count = 0;
1202 $first = true;
1203 $last = false;
1204 foreach ($categories as $cat) {
1205 $count++;
1206 if ($count == $countcats) {
1207 $last = true;
1208 }
1209 $up = $first ? false : true;
1210 $down = $last ? false : true;
1211 $first = false;
1212
89adb174 1213 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
c2cb4545 1214 }
1215 }
c2cb4545 1216}
1217
1218
d2b6ba70 1219function print_category_info($category, $depth) {
1220/// Prints the category info in indented fashion
1221/// This function is only used by print_whole_category_list() above
c2cb4545 1222
1223 global $CFG;
b48f834c 1224 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1225
b48f834c 1226 if (empty($strsummary)) {
e05bcf2f 1227 $strallowguests = get_string('allowguests');
1228 $strrequireskey = get_string('requireskey');
1229 $strsummary = get_string('summary');
b48f834c 1230 }
ba2e5d73 1231
e05bcf2f 1232 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1233
8ef9cb56 1234 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
839f2456 1235 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" width="16" height="16" border="0" alt="" />';
b48f834c 1236 } else {
1237 $catimage = "&nbsp";
8ef9cb56 1238 }
b48f834c 1239
87d32352 1240 echo "\n\n".'<table border="0" cellpadding="3" cellspacing="0" width="100%">';
d2b6ba70 1241
c2cb4545 1242 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
a36a1759 1243 $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 1244
1245 echo "<tr>";
1246
1247 if ($depth) {
1248 $indent = $depth*30;
1249 $rows = count($courses) + 1;
1c919752 1250 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1251 print_spacer(10, $indent);
e05bcf2f 1252 echo '</td>';
b48f834c 1253 }
89adb174 1254
e05bcf2f 1255 echo '<td valign="top">'.$catimage.'</td>';
1256 echo '<td valign="top" width="100%" class="categoryname">';
1257 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1258 echo '</td>';
1259 echo '<td class="categoryname">&nbsp;</td>';
1260 echo '</tr>';
b48f834c 1261
9ff5310a 1262 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1263 foreach ($courses as $course) {
e05bcf2f 1264 $linkcss = $course->visible ? '' : ' class="dimmed" ';
1265 echo '<tr><td valign="top" width="30">&nbsp;';
1266 echo '</td><td valign="top" width="100%" class="coursename">';
1267 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>';
1268 echo '</td><td align="right" valign="top" nowrap="nowrap" class="coursename">';
c2cb4545 1269 if ($course->guest ) {
e05bcf2f 1270 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1271 echo '<img hspace="1" alt="'.$strallowguests.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1272 } else {
e05bcf2f 1273 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1274 }
c2cb4545 1275 if ($course->password) {
e05bcf2f 1276 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1277 echo '<img hspace="1" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1278 } else {
e05bcf2f 1279 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1280 }
1281 if ($course->summary) {
e05bcf2f 1282 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
1283 '<img hspace="1" alt="'.$strsummary.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1284 400, 500, $strsummary);
ebe8ddc1 1285 } else {
e05bcf2f 1286 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1287 }
e05bcf2f 1288 echo '</td></tr>';
0c656181 1289 }
ba2e5d73 1290 }
d2b6ba70 1291 } else {
b48f834c 1292
1293 if ($depth) {
1294 $indent = $depth*20;
e05bcf2f 1295 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1296 print_spacer(10, $indent);
e05bcf2f 1297 echo '</td>';
d2b6ba70 1298 }
89adb174 1299
e05bcf2f 1300 echo '<td valign="top" width="100%" class="categoryname">';
1301 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1302 echo '</td>';
1303 echo '<td valign="top" class="categorynumber">';
1304 if ($category->coursecount) {
1305 echo $category->coursecount;
1306 }
1307 echo '</td></tr>';
c2cb4545 1308 }
e05bcf2f 1309 echo '</table>';
c2cb4545 1310}
1311
c2cb4545 1312
c2cb4545 1313function print_courses($category, $width="100%") {
1314/// Category is 0 (for all courses) or an object
1315
810393c8 1316 global $CFG;
c2cb4545 1317
1318 if (empty($category)) {
90c2ca2e 1319 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1320 if (count($categories) == 1) {
1321 $category = array_shift($categories);
76ca0f31 1322 $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 1323 } else {
e05bcf2f 1324 $courses = get_courses('all', 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher');
90c2ca2e 1325 }
1326 unset($categories);
607809b3 1327 } else {
c2cb4545 1328 $categories = get_categories($category->id); // sub categories
76ca0f31 1329 $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 1330 }
1331
c2cb4545 1332 if ($courses) {
1333 foreach ($courses as $course) {
1334 print_course($course, $width);
1335 echo "<br />\n";
1336 }
1337 } else {
1338 print_heading(get_string("nocoursesyet"));
1339 }
1340
1341}
1342
1343
1344function print_course($course, $width="100%") {
1345
810393c8 1346 global $CFG;
c2cb4545 1347
146bbb8f 1348 static $enrol;
1349
1350 if (empty($enrol)) {
1351 require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
1352 $enrol = new enrolment_plugin;
1353 }
1354
810393c8 1355 print_simple_box_start("center", "$width", '', 5, "coursebox");
c2cb4545 1356
22288704 1357 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1358
c2cb4545 1359 echo "<table width=\"100%\">";
e5e81e78 1360 echo '<tr valign="top">';
1361 echo '<td valign="top" width="50%" class="courseboxinfo">';
34b5847a 1362 echo '<b><a title="'.get_string('entercourse').'"'.
e5e81e78 1363 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
34b5847a 1364 $course->fullname.'</a></b><br />';
c2cb4545 1365 if ($teachers = get_course_teachers($course->id)) {
34b5847a 1366 echo "<span class=\"courseboxteachers\">\n";
c2cb4545 1367 foreach ($teachers as $teacher) {
1368 if ($teacher->authority > 0) {
1369 if (!$teacher->role) {
1370 $teacher->role = $course->teacher;
1371 }
2ac64806 1372 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
8bd84b4d 1373 echo $teacher->role.': <a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id.
1374 '&amp;course='.SITEID.'">'.$fullname.'</a><br />';
c2cb4545 1375 }
1376 }
34b5847a 1377 echo "</span>\n";
c2cb4545 1378 }
c2cb4545 1379
146bbb8f 1380 echo $enrol->get_access_icons($course);
c2cb4545 1381
e5e81e78 1382 echo '</td><td valign="top" width="50%" class="courseboxsummary">';
9f39c190 1383 $options = NULL;
1384 $options->noclean = true;
34b5847a 1385 $options->para = false;
9f39c190 1386 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
c2cb4545 1387 echo "</td></tr>";
1388 echo "</table>";
1389
1390 print_simple_box_end();
1391}
1392
1393
1394function print_my_moodle() {
1395/// Prints custom user information on the home page.
1396/// Over time this can include all sorts of information
1397
1398 global $USER, $CFG;
1399
1400 if (!isset($USER->id)) {
1401 error("It shouldn't be possible to see My Moodle without being logged in.");
1402 }
1403
1404 if ($courses = get_my_courses($USER->id)) {
1405 foreach ($courses as $course) {
c81696e5 1406 if (!$course->category) {
1407 continue;
1408 }
c2cb4545 1409 print_course($course, "100%");
1410 echo "<br />\n";
1411 }
38a10939 1412
7f989948 1413 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1414 echo "<table width=\"100%\"><tr><td align=\"center\">";
1415 print_course_search("", false, "short");
1416 echo "</td><td align=\"center\">";
1417 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1418 echo "</td></tr></table>\n";
1419 }
26330001 1420 } else {
1421 if (count_records("course_categories") > 1) {
cb29b020 1422 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1423 print_whole_category_list();
1424 print_simple_box_end();
1425 } else {
1426 print_courses(0, "100%");
1427 }
607809b3 1428 }
2b8cef80 1429}
1430
11b0c469 1431
a8b56716 1432function print_course_search($value="", $return=false, $format="plain") {
38a10939 1433
1434 global $CFG;
1435
1436 $strsearchcourses= get_string("searchcourses");
1437
1c919752 1438 if ($format == 'plain') {
87d32352 1439 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1440 $output .= '<center><p align="center" class="coursesearchbox">';
1441 $output .= '<input type="text" size="30" name="search" alt="'.$strsearchcourses.'" value="'.$value.'" />';
1c919752 1442 $output .= '<input type="submit" value="'.$strsearchcourses.'" />';
87d32352 1443 $output .= '</p></center></form>';
1c919752 1444 } else if ($format == 'short') {
87d32352 1445 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1446 $output .= '<center><p align="center" class="coursesearchbox">';
1447 $output .= '<input type="text" size="12" name="search" alt="'.$strsearchcourses.'" value="'.$value.'" />';
1c919752 1448 $output .= '<input type="submit" value="'.$strsearchcourses.'" />';
87d32352 1449 $output .= '</p></center></form>';
1c919752 1450 } else if ($format == 'navbar') {
87d32352 1451 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1452 $output .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
1453 $output .= '<input type="text" size="20" name="search" alt="'.$strsearchcourses.'" value="'.$value.'" />';
1c919752 1454 $output .= '<input type="submit" value="'.$strsearchcourses.'" />';
1c919752 1455 $output .= '</td></tr></table>';
87d32352 1456 $output .= '</form>';
a8b56716 1457 }
1458
1459 if ($return) {
1460 return $output;
1461 }
1462 echo $output;
38a10939 1463}
11b0c469 1464
1465/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1466
1467function add_course_module($mod) {
11b0c469 1468
e5dfd0f3 1469 $mod->added = time();
53f4ad2c 1470 unset($mod->id);
11b0c469 1471
e5dfd0f3 1472 return insert_record("course_modules", $mod);
11b0c469 1473}
1474
7977cffd 1475function add_mod_to_section($mod, $beforemod=NULL) {
1476/// Given a full mod object with section and course already defined
1477/// If $before is specified, then this is an existing ID which we
1478/// will insert the new module before
1479///
1480/// Returns the course_sections ID where the mod is inserted
11b0c469 1481
9fa49e22 1482 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1483
1484 $section->sequence = trim($section->sequence);
1485
1486 if (empty($section->sequence)) {
11b0c469 1487 $newsequence = "$mod->coursemodule";
7977cffd 1488
1489 } else if ($beforemod) {
1490 $modarray = explode(",", $section->sequence);
1491
1492 if ($key = array_keys ($modarray, $beforemod->id)) {
1493 $insertarray = array($mod->id, $beforemod->id);
1494 array_splice($modarray, $key[0], 1, $insertarray);
1495 $newsequence = implode(",", $modarray);
1496
1497 } else { // Just tack it on the end anyway
1498 $newsequence = "$section->sequence,$mod->coursemodule";
1499 }
1500
1501 } else {
1502 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1503 }
89adb174 1504
e5dfd0f3 1505 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1506 return $section->id; // Return course_sections ID that was used.
11b0c469 1507 } else {
e5dfd0f3 1508 return 0;
11b0c469 1509 }
89adb174 1510
11b0c469 1511 } else { // Insert a new record
e5dfd0f3 1512 $section->course = $mod->course;
1513 $section->section = $mod->section;
1514 $section->summary = "";
1515 $section->sequence = $mod->coursemodule;
1516 return insert_record("course_sections", $section);
11b0c469 1517 }
1518}
1519
48e535bc 1520function set_coursemodule_groupmode($id, $groupmode) {
3d575e6f 1521 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1522}
1523
48e535bc 1524function set_coursemodule_visible($id, $visible) {
1525 $cm = get_record('course_modules', 'id', $id);
dcd338ff 1526 $modulename = get_field('modules', 'name', 'id', $cm->module);
1527 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1528 foreach($events as $event) {
48e535bc 1529 if ($visible) {
1530 show_event($event);
1531 } else {
1532 hide_event($event);
1533 }
dcd338ff 1534 }
1535 }
48e535bc 1536 return set_field("course_modules", "visible", $visible, "id", $id);
1acfbce5 1537}
1538
48e535bc 1539function delete_course_module($id) {
1540 $cm = get_record('course_modules', 'id', $id);
dcd338ff 1541 $modulename = get_field('modules', 'name', 'id', $cm->module);
1542 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1543 foreach($events as $event) {
48e535bc 1544 delete_event($event);
dcd338ff 1545 }
1546 }
48e535bc 1547 return set_field("course_modules", "visible", 0, "id", $id);
1548 return set_field("course_modules", "deleted", 1, "id", $id);
11b0c469 1549}
1550
1551function delete_mod_from_section($mod, $section) {
11b0c469 1552
e5dfd0f3 1553 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1554
e5dfd0f3 1555 $modarray = explode(",", $section->sequence);
11b0c469 1556
1557 if ($key = array_keys ($modarray, $mod)) {
1558 array_splice($modarray, $key[0], 1);
1559 $newsequence = implode(",", $modarray);
e5dfd0f3 1560 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1561 } else {
1562 return false;
1563 }
89adb174 1564
11b0c469 1565 }
7977cffd 1566 return false;
11b0c469 1567}
1568
12905134 1569function move_section($course, $section, $move) {
1570/// Moves a whole course section up and down within the course
798b70a1 1571 global $USER;
12905134 1572
1573 if (!$move) {
1574 return true;
1575 }
1576
1577 $sectiondest = $section + $move;
1578
1579 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1580 return false;
1581 }
1582
1583 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1584 return false;
1585 }
1586
1587 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1588 return false;
1589 }
1590
56e34ee4 1591 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1592 return false;
1593 }
56e34ee4 1594 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1595 return false;
1596 }
798b70a1 1597 // if the focus is on the section that is being moved, then move the focus along
1598 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
1599 course_set_display($course->id, $sectiondest);
1600 }
12905134 1601 return true;
1602}
1603
1604
7977cffd 1605function moveto_module($mod, $section, $beforemod=NULL) {
1606/// All parameters are objects
1607/// Move the module object $mod to the specified $section
1608/// If $beforemod exists then that is the module
1609/// before which $modid should be inserted
1610
1611/// Remove original module from original section
1612
1613 if (! delete_mod_from_section($mod->id, $mod->section)) {
1614 notify("Could not delete module from existing section");
1615 }
1616
1617/// Update module itself if necessary
1618
1619 if ($mod->section != $section->id) {
89adb174 1620 $mod->section = $section->id;
7977cffd 1621 if (!update_record("course_modules", $mod)) {
1622 return false;
1623 }
48e535bc 1624 // if moving to a hidden section then hide module
1625 if (!$section->visible) {
1626 set_coursemodule_visible($mod->id, 0);
1627 }
7977cffd 1628 }
1629
1630/// Add the module into the new section
1631
1632 $mod->course = $section->course;
1633 $mod->section = $section->section; // need relative reference
1634 $mod->coursemodule = $mod->id;
1635
1636 if (! add_mod_to_section($mod, $beforemod)) {
1637 return false;
1638 }
1639
1640 return true;
1641
1642}
1643
3d575e6f 1644function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1) {
810393c8 1645 global $CFG, $USER;
94361e02 1646
3d575e6f 1647 static $str;
1648
1649 if (!isset($str)) {
1650 $str->delete = get_string("delete");
1651 $str->move = get_string("move");
1652 $str->moveup = get_string("moveup");
1653 $str->movedown = get_string("movedown");
1654 $str->moveright = get_string("moveright");
1655 $str->moveleft = get_string("moveleft");
1656 $str->update = get_string("update");
d33a2a6f 1657 $str->duplicate = get_string("duplicate");
3d575e6f 1658 $str->hide = get_string("hide");
1659 $str->show = get_string("show");
1660 $str->clicktochange = get_string("clicktochange");
32d03b7b 1661 $str->forcedmode = get_string("forcedmode");
3d575e6f 1662 $str->groupsnone = get_string("groupsnone");
1663 $str->groupsseparate = get_string("groupsseparate");
1664 $str->groupsvisible = get_string("groupsvisible");
1acfbce5 1665 }
94361e02 1666
1667 if ($absolute) {
dc0dc7d5 1668 $path = "$CFG->wwwroot/course";
1669 } else {
1670 $path = ".";
1671 }
1672
3d575e6f 1673 if ($mod->visible) {
8b92f5bb 1674 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$mod->id&amp;sesskey=$USER->sesskey\"><img".
810393c8 1675 " src=\"$CFG->pixpath/t/hide.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"$str->hide\" /></a> ";
1acfbce5 1676 } else {
8b92f5bb 1677 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$mod->id&amp;sesskey=$USER->sesskey\"><img".
810393c8 1678 " src=\"$CFG->pixpath/t/show.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
839f2456 1679 "border=\"0\" alt=\"$str->show\" /></a> ";
7977cffd 1680 }
3d575e6f 1681 if ($mod->groupmode !== false) {
1682 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1683 $grouptitle = $str->groupsseparate;
810393c8 1684 $groupimage = "$CFG->pixpath/t/groups.gif";
8b92f5bb 1685 $grouplink = "$path/mod.php?id=$mod->id&amp;groupmode=0&amp;sesskey=$USER->sesskey";
3d575e6f 1686 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1687 $grouptitle = $str->groupsvisible;
810393c8 1688 $groupimage = "$CFG->pixpath/t/groupv.gif";
8b92f5bb 1689 $grouplink = "$path/mod.php?id=$mod->id&amp;groupmode=1&amp;sesskey=$USER->sesskey";
32d03b7b 1690 } else {
1691 $grouptitle = $str->groupsnone;
810393c8 1692 $groupimage = "$CFG->pixpath/t/groupn.gif";
8b92f5bb 1693 $grouplink = "$path/mod.php?id=$mod->id&amp;groupmode=2&amp;sesskey=$USER->sesskey";
32d03b7b 1694 }
1695 if ($mod->groupmodelink) {
1696 $groupmode = "<a title=\"$grouptitle ($str->clicktochange)\" href=\"$grouplink\">".
dbcd54b7 1697 "<img src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1698 "border=\"0\" alt=\"$grouptitle\" /></a>";
3d575e6f 1699 } else {
32d03b7b 1700 $groupmode = "<img title=\"$grouptitle ($str->forcedmode)\" ".
dbcd54b7 1701 " src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1702 "border=\"0\" alt=\"$grouptitle\" />";
3d575e6f 1703 }
1704 } else {
1705 $groupmode = "";
1706 }
7977cffd 1707
1708 if ($moveselect) {
8b92f5bb 1709 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$mod->id&amp;sesskey=$USER->sesskey\"><img".
810393c8 1710 " src=\"$CFG->pixpath/t/move.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1711 " border=\"0\" alt=\"$str->move\" /></a>";
493486c4 1712 } else {
8b92f5bb 1713 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$mod->id&amp;move=-1&amp;sesskey=$USER->sesskey\"><img".
810393c8 1714 " src=\"$CFG->pixpath/t/up.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1715 " border=\"0\" alt=\"$str->moveup\" /></a>".
8b92f5bb 1716 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$mod->id&amp;move=1&amp;sesskey=$USER->sesskey\"><img".
810393c8 1717 " src=\"$CFG->pixpath/t/down.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1718 " border=\"0\" alt=\"$str->movedown\" /></a>";
7977cffd 1719 }
1720
aac94fd0 1721 $leftright = "";
1722 if ($indent > 0) {
8b92f5bb 1723 $leftright .= "<a title=\"$str->moveleft\" href=\"$path/mod.php?id=$mod->id&amp;indent=-1&amp;sesskey=$USER->sesskey\"><img".
810393c8 1724 " src=\"$CFG->pixpath/t/left.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
839f2456 1725 " border=\"0\" alt=\"$str->moveleft\" /></a>";
aac94fd0 1726 }
1727 if ($indent >= 0) {
8b92f5bb 1728 $leftright .= "<a title=\"$str->moveright\" href=\"$path/mod.php?id=$mod->id&amp;indent=1&amp;sesskey=$USER->sesskey\"><img".
810393c8 1729 " src=\"$CFG->pixpath/t/right.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
839f2456 1730 " border=\"0\" alt=\"$str->moveright\" /></a>";
aac94fd0 1731 }
1732
bf1241ad 1733 return "$leftright$move".
8b92f5bb 1734 "<a title=\"$str->update\" href=\"$path/mod.php?update=$mod->id&amp;sesskey=$USER->sesskey\"><img".
810393c8 1735 " src=\"$CFG->pixpath/t/edit.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" ".
839f2456 1736 " alt=\"$str->update\" /></a>".
d4bfbe4d 1737 // Following line is commented out until this feature is more definite -- martin
8b92f5bb 1738 // "<a title=\"$str->duplicate\" href=\"$path/mod.php?duplicate=$mod->id&amp;sesskey=$USER->sesskey\"> 2 </a>".
1739 "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$mod->id&amp;sesskey=$USER->sesskey\"><img".
810393c8 1740 " src=\"$CFG->pixpath/t/delete.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" ".
839f2456 1741 " alt=\"$str->delete\" /></a>$hideshow$groupmode";
90845098 1742}
1743
b61efafb 1744/**
1745 * given a course object with shortname & fullname, this function will
1746 * truncate the the number of chars allowed and add ... if it was too long
1747 */
1748function course_format_name ($course,$max=100) {
1749
1750 $str = $course->shortname.': '.$course->fullname;
1751 if (strlen($str) <= $max) {
1752 return $str;
1753 }
1754 else {
1755 return substr($str,0,$max-3).'...';
1756 }
1757}
1758
1759/**
1760 * This function will return true if the given course is a child course at all
1761 */
1762function course_in_meta ($course) {
5f37b628 1763 return record_exists("course_meta","child_course",$course->id);
b61efafb 1764}
1765
48e535bc 1766
1767/**
1768 * Print standard form elements on module setup forms in mod/.../mod.html
1769 */
1770function print_standard_coursemodule_settings($form) {
da2224f8 1771 if (! $course = get_record('course', 'id', $form->course)) {
1772 error("This course doesn't exist");
1773 }
1774 print_groupmode_setting($form, $course);
1775 print_visible_setting($form, $course);
48e535bc 1776}
1777
1778/**
1779 * Print groupmode form element on module setup forms in mod/.../mod.html
1780 */
5ebb746b 1781function print_groupmode_setting($form, $course=NULL) {
48e535bc 1782
5ebb746b 1783 if (empty($course)) {
1784 if (! $course = get_record('course', 'id', $form->course)) {
1785 error("This course doesn't exist");
1786 }
1787 }
48e535bc 1788 if ($form->coursemodule) {
1789 if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
1790 error("This course module doesn't exist");
1791 }
1792 } else {
1793 $cm = null;
1794 }
1795 $groupmode = groupmode($course, $cm);
1796 if ($course->groupmode or (!$course->groupmodeforce)) {
1797 echo '<tr valign="top">';
1798 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
1799 echo '<td>';
1800 unset($choices);
1801 $choices[NOGROUPS] = get_string('groupsnone');
1802 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
1803 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
1804 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
1805 helpbutton('groupmode', get_string('groupmode'));
1806 echo '</td></tr>';
1807 }
1808}
1809
1810/**
1811 * Print visibility setting form element on module setup forms in mod/.../mod.html
1812 */
5ebb746b 1813function print_visible_setting($form, $course=NULL) {
1ee55c41 1814 if (empty($course)) {
1815 if (! $course = get_record('course', 'id', $form->course)) {
1816 error("This course doesn't exist");
1817 }
1818 }
48e535bc 1819 if ($form->coursemodule) {
1820 $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
1821 } else {
1822 $visible = true;
1823 }
1824
1825 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
1826 $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
1827 } else {
1828 $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
1829 }
1830 if ($hiddensection) {
1831 $visible = false;
1832 }
1833
1834 echo '<tr valign="top">';
1f15db9d 1835 echo '<td align="right"><b>'.get_string('visibletostudents','',moodle_strtolower($course->students)).':</b></td>';
48e535bc 1836 echo '<td>';
1837 unset($choices);
1f15db9d 1838 $choices[1] = get_string('show');
1839 $choices[0] = get_string('hide');
48e535bc 1840 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
1841 echo '</td></tr>';
1842}
1843
87d32352 1844?>