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