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