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