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