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