use clean_filename for filename of text file download
[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
950c35a9 19define('COURSE_MAX_COURSES_PER_DROPDOWN',1000); // max courses in log dropdown before switching to optional
60fdc714 20
950c35a9 21define('COURSE_MAX_USERS_PER_DROPDOWN',1000); // max users in log dropdown before switching to optional
60fdc714 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')) {
81a5d9ca 1120 $resourceallowed = true;
0705ff84 1121 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
1122 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
1123 }
cb57e6f4 1124
1125 if ($vertical) {
d33d0cda 1126 $output .= '<div>';
cb57e6f4 1127 }
1128
0705ff84 1129 // we need to loop through the forms and check to see if we can add them.
1130 foreach ($modnames as $key) {
1131 if (!course_allowed_module($course,$key))
1132 unset($modnames[strtolower($key)]);
1133 }
1134
1135 // this is stupid but labels get put into resource, so if resource is hidden and label is not, we're in trouble.
1136 if (course_allowed_module($course,'label') && empty($resourceallowed)) {
1137 $modnames['label'] = get_string('modulename', 'label');
1138 }
1139
1c46eb03 1140 $output .= ' ';
8b92f5bb 1141 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 1142 $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
d33d0cda 1143
1144 if ($vertical) {
1145 $output .= '</div>';
1146 }
1147
cb57e6f4 1148 $output .= '</div>';
1149
1150 if ($return) {
1151 return $output;
1152 } else {
1153 echo $output;
1154 }
e0161bff 1155}
1156
5867bfb5 1157function rebuild_course_cache($courseid=0) {
1158// Rebuilds the cached list of course activities stored in the database
1159// If a courseid is not specified, then all are rebuilt
1160
1161 if ($courseid) {
1162 $select = "id = '$courseid'";
1163 } else {
1164 $select = "";
1165 }
1166
1a31c2b3 1167 if ($courses = get_records_select("course", $select,'','id,fullname')) {
5867bfb5 1168 foreach ($courses as $course) {
1169 $modinfo = serialize(get_array_of_activities($course->id));
1170 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
1171 notify("Could not cache module information for course '$course->fullname'!");
1172 }
1173 }
1174 }
1175}
1176
1177
c2cb4545 1178
1179function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 1180/// Given an empty array, this function recursively travels the
c2cb4545 1181/// categories, building up a nice list for display. It also makes
1182/// an array that list all the parents for each category.
1183
9d866ae0 1184 // initialize the arrays if needed
1185 if (!is_array($list)) {
1186 $list = array();
1187 }
1188 if (!is_array($parents)) {
1189 $parents = array();
1190 }
1191
c2cb4545 1192 if ($category) {
1193 if ($path) {
e05bcf2f 1194 $path = $path.' / '.$category->name;
c2cb4545 1195 } else {
e05bcf2f 1196 $path = $category->name;
c2cb4545 1197 }
1198 $list[$category->id] = $path;
1199 } else {
1200 $category->id = 0;
1201 }
1202
e05bcf2f 1203 if ($categories = get_categories($category->id)) { // Print all the children recursively
c2cb4545 1204 foreach ($categories as $cat) {
1205 if (!empty($category->id)) {
3bd4de22 1206 if (isset($parents[$category->id])) {
2832badf 1207 $parents[$cat->id] = $parents[$category->id];
1208 }
c2cb4545 1209 $parents[$cat->id][] = $category->id;
1210 }
89adb174 1211 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1212 }
1213 }
1214}
1215
1216
c2cb4545 1217function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
89adb174 1218/// Recursive function to print out all the categories in a nice format
c2cb4545 1219/// with or without courses included
9ff5310a 1220 global $CFG;
e05bcf2f 1221
1222 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1223 return;
9ff5310a 1224 }
c2cb4545 1225
1226 if (!$displaylist) {
e92fe848 1227 make_categories_list($displaylist, $parentslist);
c2cb4545 1228 }
1229
1230 if ($category) {
3af6e1db 1231 if ($category->visible or iscreator()) {
d2b6ba70 1232 print_category_info($category, $depth);
c2cb4545 1233 } else {
1234 return; // Don't bother printing children of invisible categories
1235 }
89adb174 1236
c2cb4545 1237 } else {
c2cb4545 1238 $category->id = "0";
1239 }
1240
1241 if ($categories = get_categories($category->id)) { // Print all the children recursively
1242 $countcats = count($categories);
1243 $count = 0;
1244 $first = true;
1245 $last = false;
1246 foreach ($categories as $cat) {
1247 $count++;
1248 if ($count == $countcats) {
1249 $last = true;
1250 }
1251 $up = $first ? false : true;
1252 $down = $last ? false : true;
1253 $first = false;
1254
814f88c0 1255 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
c2cb4545 1256 }
1257 }
c2cb4545 1258}
1259
0705ff84 1260// this function will return $options array for choose_from_menu, with whitespace to denote nesting.
1261
1262function make_categories_options() {
1263 make_categories_list($cats,$parents);
1264 foreach ($cats as $key => $value) {
1265 if (array_key_exists($key,$parents)) {
1266 if ($indent = count($parents[$key])) {
1267 for ($i = 0; $i < $indent; $i++) {
1268 $cats[$key] = '&nbsp;'.$cats[$key];
1269 }
1270 }
1271 }
1272 }
1273 return $cats;
1274}
c2cb4545 1275
d2b6ba70 1276function print_category_info($category, $depth) {
1277/// Prints the category info in indented fashion
1278/// This function is only used by print_whole_category_list() above
c2cb4545 1279
1280 global $CFG;
b48f834c 1281 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1282
b48f834c 1283 if (empty($strsummary)) {
e05bcf2f 1284 $strallowguests = get_string('allowguests');
1285 $strrequireskey = get_string('requireskey');
1286 $strsummary = get_string('summary');
b48f834c 1287 }
ba2e5d73 1288
e05bcf2f 1289 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1290
e1638d61 1291 $frontpage = explode(',', $CFG->frontpage);
1292 $frontpage = $frontpage?array_flip($frontpage):array();
1293 if (isset($frontpage[FRONTPAGECOURSELIST])) {
839f2456 1294 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" width="16" height="16" border="0" alt="" />';
b48f834c 1295 } else {
7b0b5c14 1296 $catimage = "&nbsp;";
8ef9cb56 1297 }
b48f834c 1298
87d32352 1299 echo "\n\n".'<table border="0" cellpadding="3" cellspacing="0" width="100%">';
d2b6ba70 1300
e1638d61 1301 if (isset($frontpage[FRONTPAGECOURSELIST])) {
19374e76 1302 $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 1303
1304 echo "<tr>";
1305
1306 if ($depth) {
1307 $indent = $depth*30;
1308 $rows = count($courses) + 1;
1c919752 1309 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1310 print_spacer(10, $indent);
e05bcf2f 1311 echo '</td>';
b48f834c 1312 }
89adb174 1313
e05bcf2f 1314 echo '<td valign="top">'.$catimage.'</td>';
290130b3 1315 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1316 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1317 echo '</td>';
290130b3 1318 echo '<td class="category info">&nbsp;</td>';
e05bcf2f 1319 echo '</tr>';
b48f834c 1320
9ff5310a 1321 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1322 foreach ($courses as $course) {
e05bcf2f 1323 $linkcss = $course->visible ? '' : ' class="dimmed" ';
1324 echo '<tr><td valign="top" width="30">&nbsp;';
290130b3 1325 echo '</td><td valign="top" width="100%" class="course name">';
e05bcf2f 1326 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>';
290130b3 1327 echo '</td><td align="right" valign="top" nowrap="nowrap" class="course info">';
c2cb4545 1328 if ($course->guest ) {
e05bcf2f 1329 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1330 echo '<img hspace="1" alt="'.$strallowguests.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1331 } else {
e05bcf2f 1332 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1333 }
c2cb4545 1334 if ($course->password) {
e05bcf2f 1335 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1336 echo '<img hspace="1" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1337 } else {
e05bcf2f 1338 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1339 }
1340 if ($course->summary) {
e05bcf2f 1341 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
1342 '<img hspace="1" alt="'.$strsummary.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1343 400, 500, $strsummary);
ebe8ddc1 1344 } else {
e05bcf2f 1345 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1346 }
e05bcf2f 1347 echo '</td></tr>';
0c656181 1348 }
ba2e5d73 1349 }
d2b6ba70 1350 } else {
b48f834c 1351
1352 if ($depth) {
1353 $indent = $depth*20;
e05bcf2f 1354 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1355 print_spacer(10, $indent);
e05bcf2f 1356 echo '</td>';
d2b6ba70 1357 }
89adb174 1358
290130b3 1359 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1360 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1361 echo '</td>';
290130b3 1362 echo '<td valign="top" class="category number">';
e05bcf2f 1363 if ($category->coursecount) {
1364 echo $category->coursecount;
1365 }
1366 echo '</td></tr>';
c2cb4545 1367 }
e05bcf2f 1368 echo '</table>';
c2cb4545 1369}
1370
c2cb4545 1371
c2cb4545 1372function print_courses($category, $width="100%") {
1373/// Category is 0 (for all courses) or an object
1374
810393c8 1375 global $CFG;
c2cb4545 1376
1377 if (empty($category)) {
90c2ca2e 1378 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1379 if (count($categories) == 1) {
1380 $category = array_shift($categories);
19374e76 1381 $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 1382 } else {
19374e76 1383 $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 1384 }
1385 unset($categories);
607809b3 1386 } else {
c2cb4545 1387 $categories = get_categories($category->id); // sub categories
19374e76 1388 $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 1389 }
1390
c2cb4545 1391 if ($courses) {
1392 foreach ($courses as $course) {
1393 print_course($course, $width);
c2cb4545 1394 }
1395 } else {
4a7948ce 1396 print_heading(get_string('nocoursesyet'));
1397 if (iscreator()) {
1398 $options = array();
1399 $options['category'] = $category->id;
1400 echo '<div class="addcoursebutton" align="center">';
1401 print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string('addnewcourse'));
1402 echo '</div>';
1403 }
c2cb4545 1404 }
1405
1406}
1407
1408
1409function print_course($course, $width="100%") {
1410
861efb19 1411 global $CFG, $USER;
c2cb4545 1412
146bbb8f 1413 static $enrol;
1414
1415 if (empty($enrol)) {
1416 require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
1417 $enrol = new enrolment_plugin;
1418 }
1419
810393c8 1420 print_simple_box_start("center", "$width", '', 5, "coursebox");
c2cb4545 1421
22288704 1422 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1423
c2cb4545 1424 echo "<table width=\"100%\">";
e5e81e78 1425 echo '<tr valign="top">';
290130b3 1426 echo '<td valign="top" width="50%" class="info">';
34b5847a 1427 echo '<b><a title="'.get_string('entercourse').'"'.
e5e81e78 1428 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
34b5847a 1429 $course->fullname.'</a></b><br />';
c2cb4545 1430 if ($teachers = get_course_teachers($course->id)) {
290130b3 1431 echo "<span class=\"teachers\">\n";
c2cb4545 1432 foreach ($teachers as $teacher) {
1433 if ($teacher->authority > 0) {
1434 if (!$teacher->role) {
1435 $teacher->role = $course->teacher;
1436 }
2ac64806 1437 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
8bd84b4d 1438 echo $teacher->role.': <a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id.
1439 '&amp;course='.SITEID.'">'.$fullname.'</a><br />';
c2cb4545 1440 }
1441 }
34b5847a 1442 echo "</span>\n";
c2cb4545 1443 }
c2cb4545 1444
146bbb8f 1445 echo $enrol->get_access_icons($course);
c2cb4545 1446
290130b3 1447 echo '</td><td valign="top" width="50%" class="summary">';
9f39c190 1448 $options = NULL;
1449 $options->noclean = true;
34b5847a 1450 $options->para = false;
9f39c190 1451 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
c2cb4545 1452 echo "</td></tr>";
1453 echo "</table>";
1454
1455 print_simple_box_end();
1456}
1457
1458
1459function print_my_moodle() {
1460/// Prints custom user information on the home page.
1461/// Over time this can include all sorts of information
1462
1463 global $USER, $CFG;
1464
1465 if (!isset($USER->id)) {
1466 error("It shouldn't be possible to see My Moodle without being logged in.");
1467 }
1468
1469 if ($courses = get_my_courses($USER->id)) {
1470 foreach ($courses as $course) {
c81696e5 1471 if (!$course->category) {
1472 continue;
1473 }
c2cb4545 1474 print_course($course, "100%");
c2cb4545 1475 }
38a10939 1476
7f989948 1477 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1478 echo "<table width=\"100%\"><tr><td align=\"center\">";
1479 print_course_search("", false, "short");
1480 echo "</td><td align=\"center\">";
1481 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1482 echo "</td></tr></table>\n";
1483 }
26330001 1484 } else {
1485 if (count_records("course_categories") > 1) {
cb29b020 1486 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1487 print_whole_category_list();
1488 print_simple_box_end();
1489 } else {
1490 print_courses(0, "100%");
1491 }
607809b3 1492 }
2b8cef80 1493}
1494
11b0c469 1495
a8b56716 1496function print_course_search($value="", $return=false, $format="plain") {
38a10939 1497
1498 global $CFG;
1499
1500 $strsearchcourses= get_string("searchcourses");
1501
1c919752 1502 if ($format == 'plain') {
87d32352 1503 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1504 $output .= '<center><p align="center" class="coursesearchbox">';
9cc78ee1 1505 $output .= '<input type="text" size="30" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
1506 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1507 $output .= '</p></center></form>';
1c919752 1508 } else if ($format == 'short') {
87d32352 1509 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1510 $output .= '<center><p align="center" class="coursesearchbox">';
9cc78ee1 1511 $output .= '<input type="text" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
1512 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1513 $output .= '</p></center></form>';
1c919752 1514 } else if ($format == 'navbar') {
87d32352 1515 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1516 $output .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
9cc78ee1 1517 $output .= '<input type="text" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
1518 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
1c919752 1519 $output .= '</td></tr></table>';
87d32352 1520 $output .= '</form>';
a8b56716 1521 }
1522
1523 if ($return) {
1524 return $output;
1525 }
1526 echo $output;
38a10939 1527}
11b0c469 1528
1529/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1530
1531function add_course_module($mod) {
11b0c469 1532
e5dfd0f3 1533 $mod->added = time();
53f4ad2c 1534 unset($mod->id);
11b0c469 1535
e5dfd0f3 1536 return insert_record("course_modules", $mod);
11b0c469 1537}
1538
7977cffd 1539function add_mod_to_section($mod, $beforemod=NULL) {
1540/// Given a full mod object with section and course already defined
1541/// If $before is specified, then this is an existing ID which we
1542/// will insert the new module before
1543///
1544/// Returns the course_sections ID where the mod is inserted
11b0c469 1545
9fa49e22 1546 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1547
1548 $section->sequence = trim($section->sequence);
1549
1550 if (empty($section->sequence)) {
11b0c469 1551 $newsequence = "$mod->coursemodule";
7977cffd 1552
1553 } else if ($beforemod) {
1554 $modarray = explode(",", $section->sequence);
1555
1556 if ($key = array_keys ($modarray, $beforemod->id)) {
1557 $insertarray = array($mod->id, $beforemod->id);
1558 array_splice($modarray, $key[0], 1, $insertarray);
1559 $newsequence = implode(",", $modarray);
1560
1561 } else { // Just tack it on the end anyway
1562 $newsequence = "$section->sequence,$mod->coursemodule";
1563 }
1564
1565 } else {
1566 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1567 }
89adb174 1568
e5dfd0f3 1569 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1570 return $section->id; // Return course_sections ID that was used.
11b0c469 1571 } else {
e5dfd0f3 1572 return 0;
11b0c469 1573 }
89adb174 1574
11b0c469 1575 } else { // Insert a new record
e5dfd0f3 1576 $section->course = $mod->course;
1577 $section->section = $mod->section;
1578 $section->summary = "";
1579 $section->sequence = $mod->coursemodule;
1580 return insert_record("course_sections", $section);
11b0c469 1581 }
1582}
1583
48e535bc 1584function set_coursemodule_groupmode($id, $groupmode) {
3d575e6f 1585 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1586}
1587
48e535bc 1588function set_coursemodule_visible($id, $visible) {
1589 $cm = get_record('course_modules', 'id', $id);
dcd338ff 1590 $modulename = get_field('modules', 'name', 'id', $cm->module);
1591 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1592 foreach($events as $event) {
48e535bc 1593 if ($visible) {
1594 show_event($event);
1595 } else {
1596 hide_event($event);
1597 }
dcd338ff 1598 }
1599 }
48e535bc 1600 return set_field("course_modules", "visible", $visible, "id", $id);
1acfbce5 1601}
1602
290130b3 1603/*
1604 * Delete a course module and any associated data at the course level (events)
1605 * Until 1.5 this function simply marked a deleted flag ... now it
1606 * deletes it completely.
1607 *
1608 */
48e535bc 1609function delete_course_module($id) {
290130b3 1610 if (!$cm = get_record('course_modules', 'id', $id)) {
1611 return true;
1612 }
dcd338ff 1613 $modulename = get_field('modules', 'name', 'id', $cm->module);
1614 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1615 foreach($events as $event) {
48e535bc 1616 delete_event($event);
dcd338ff 1617 }
1618 }
290130b3 1619 return delete_records('course_modules', 'id', $cm->id);
11b0c469 1620}
1621
1622function delete_mod_from_section($mod, $section) {
11b0c469 1623
e5dfd0f3 1624 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1625
e5dfd0f3 1626 $modarray = explode(",", $section->sequence);
11b0c469 1627
1628 if ($key = array_keys ($modarray, $mod)) {
1629 array_splice($modarray, $key[0], 1);
1630 $newsequence = implode(",", $modarray);
e5dfd0f3 1631 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1632 } else {
1633 return false;
1634 }
89adb174 1635
11b0c469 1636 }
7977cffd 1637 return false;
11b0c469 1638}
1639
12905134 1640function move_section($course, $section, $move) {
1641/// Moves a whole course section up and down within the course
798b70a1 1642 global $USER;
12905134 1643
1644 if (!$move) {
1645 return true;
1646 }
1647
1648 $sectiondest = $section + $move;
1649
1650 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1651 return false;
1652 }
1653
1654 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1655 return false;
1656 }
1657
1658 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1659 return false;
1660 }
1661
56e34ee4 1662 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1663 return false;
1664 }
56e34ee4 1665 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1666 return false;
1667 }
798b70a1 1668 // if the focus is on the section that is being moved, then move the focus along
1669 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
1670 course_set_display($course->id, $sectiondest);
1671 }
5390cbb7 1672
1673 // Check for duplicates.
1674 // There is a very rare case that some sections in the same course have the same section id.
1675 if (($count_section = count_records('course_sections', 'course', $course->id) - 1) != $course->numsections) {
1676 $sections = get_records_select('course_sections', "course = $course->id AND section > 0", 'section ASC');
1677 $n = 1;
1678 foreach ($sections as $section) {
1679 if (!set_field('course_sections', 'section', $n, 'id', $section->id)) {
1680 return false;
1681 }
1682 $n++;
1683 }
1684 if (!set_field('course', 'numsections', $count_section, 'id', $course->id)) {
1685 return false;
1686 }
1687 }
12905134 1688 return true;
1689}
1690
1691
7977cffd 1692function moveto_module($mod, $section, $beforemod=NULL) {
1693/// All parameters are objects
1694/// Move the module object $mod to the specified $section
1695/// If $beforemod exists then that is the module
1696/// before which $modid should be inserted
1697
1698/// Remove original module from original section
1699
1700 if (! delete_mod_from_section($mod->id, $mod->section)) {
1701 notify("Could not delete module from existing section");
1702 }
1703
1704/// Update module itself if necessary
1705
1706 if ($mod->section != $section->id) {
89adb174 1707 $mod->section = $section->id;
7977cffd 1708 if (!update_record("course_modules", $mod)) {
1709 return false;
1710 }
48e535bc 1711 // if moving to a hidden section then hide module
1712 if (!$section->visible) {
1713 set_coursemodule_visible($mod->id, 0);
1714 }
7977cffd 1715 }
1716
1717/// Add the module into the new section
1718
1719 $mod->course = $section->course;
1720 $mod->section = $section->section; // need relative reference
1721 $mod->coursemodule = $mod->id;
1722
1723 if (! add_mod_to_section($mod, $beforemod)) {
1724 return false;
1725 }
1726
1727 return true;
1728
1729}
1730
24e1eae4 1731function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
810393c8 1732 global $CFG, $USER;
94361e02 1733
3d575e6f 1734 static $str;
37a88449 1735 static $sesskey;
3d575e6f 1736
1737 if (!isset($str)) {
1738 $str->delete = get_string("delete");
1739 $str->move = get_string("move");
1740 $str->moveup = get_string("moveup");
1741 $str->movedown = get_string("movedown");
1742 $str->moveright = get_string("moveright");
1743 $str->moveleft = get_string("moveleft");
1744 $str->update = get_string("update");
d33a2a6f 1745 $str->duplicate = get_string("duplicate");
3d575e6f 1746 $str->hide = get_string("hide");
1747 $str->show = get_string("show");
1748 $str->clicktochange = get_string("clicktochange");
32d03b7b 1749 $str->forcedmode = get_string("forcedmode");
3d575e6f 1750 $str->groupsnone = get_string("groupsnone");
1751 $str->groupsseparate = get_string("groupsseparate");
1752 $str->groupsvisible = get_string("groupsvisible");
37a88449 1753 $sesskey = sesskey();
1acfbce5 1754 }
94361e02 1755
24e1eae4 1756 if ($section >= 0) {
75f087b6 1757 $section = '&amp;sr='.$section; // Section return
24e1eae4 1758 } else {
1759 $section = '';
1760 }
1761
94361e02 1762 if ($absolute) {
37a88449 1763 $path = $CFG->wwwroot.'/course';
dc0dc7d5 1764 } else {
37a88449 1765 $path = '.';
dc0dc7d5 1766 }
1767
3d575e6f 1768 if ($mod->visible) {
37a88449 1769 $hideshow = '<a title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id.
1770 '&amp;sesskey='.$sesskey.$section.'"><img'.
1771 ' src="'.$CFG->pixpath.'/t/hide.gif" hspace="2" height="11" width="11" '.
1772 ' border="0" alt="'.$str->hide.'" /></a> ';
1acfbce5 1773 } else {
37a88449 1774 $hideshow = '<a title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id.
1775 '&amp;sesskey='.$sesskey.$section.'"><img'.
1776 ' src="'.$CFG->pixpath.'/t/show.gif" hspace="2" height="11" width="11" '.
1777 ' border="0" alt="'.$str->show.'" /></a> ';
7977cffd 1778 }
3d575e6f 1779 if ($mod->groupmode !== false) {
1780 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1781 $grouptitle = $str->groupsseparate;
37a88449 1782 $groupimage = $CFG->pixpath.'/t/groups.gif';
1783 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=0&amp;sesskey='.$sesskey;
3d575e6f 1784 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1785 $grouptitle = $str->groupsvisible;
37a88449 1786 $groupimage = $CFG->pixpath.'/t/groupv.gif';
1787 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=1&amp;sesskey='.$sesskey;
32d03b7b 1788 } else {
1789 $grouptitle = $str->groupsnone;
37a88449 1790 $groupimage = $CFG->pixpath.'/t/groupn.gif';
1791 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=2&amp;sesskey='.$sesskey;
32d03b7b 1792 }
1793 if ($mod->groupmodelink) {
37a88449 1794 $groupmode = '<a title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'.
1795 '<img src="'.$groupimage.'" hspace="2" height="11" width="11" '.
1796 'border="0" alt="'.$grouptitle.'" /></a>';
3d575e6f 1797 } else {
37a88449 1798 $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '.
1799 ' src="'.$groupimage.'" hspace="2" height="11" width="11" '.
1800 'border="0" alt="'.$grouptitle.'" />';
3d575e6f 1801 }
1802 } else {
1803 $groupmode = "";
1804 }
7977cffd 1805
1806 if ($moveselect) {
37a88449 1807 $move = '<a title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id.
1808 '&amp;sesskey='.$sesskey.$section.'"><img'.
1809 ' src="'.$CFG->pixpath.'/t/move.gif" hspace="2" height="11" width="11" '.
1810 ' border="0" alt="'.$str->move.'" /></a>';
493486c4 1811 } else {
37a88449 1812 $move = '<a title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id.
1813 '&amp;move=-1&amp;sesskey='.$sesskey.$section.'"><img'.
1814 ' src="'.$CFG->pixpath.'/t/up.gif" hspace="2" height="11" width="11" '.
1815 ' border="0" alt="'.$str->moveup.'" /></a>'.
1816 '<a title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id.
1817 '&amp;move=1&amp;sesskey='.$sesskey.$section.'"><img'.
1818 ' src="'.$CFG->pixpath.'/t/down.gif" hspace="2" height="11" width="11" '.
1819 ' border="0" alt="'.$str->movedown.'" /></a>';
7977cffd 1820 }
1821
aac94fd0 1822 $leftright = "";
1823 if ($indent > 0) {
37a88449 1824 $leftright .= '<a title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id.
1825 '&amp;indent=-1&amp;sesskey='.$sesskey.$section.'"><img'.
1826 ' src="'.$CFG->pixpath.'/t/left.gif" hspace="2" height="11" width="11" '.
1827 ' border="0" alt="'.$str->moveleft.'" /></a>';
aac94fd0 1828 }
1829 if ($indent >= 0) {
37a88449 1830 $leftright .= '<a title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id.
1831 '&amp;indent=1&amp;sesskey='.$sesskey.$section.'"><img'.
1832 ' src="'.$CFG->pixpath.'/t/right.gif" hspace="2" height="11" width="11" '.
1833 ' border="0" alt="'.$str->moveright.'" /></a>';
1834 }
1835
1836 return '<span class="commands">'.$leftright.$move.
1837 '<a title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id.
1838 '&amp;sesskey='.$sesskey.$section.'"><img'.
1839 ' src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" '.
1840 ' alt="'.$str->update.'" /></a>'.
1841 '<a title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id.
1842 '&amp;sesskey='.$sesskey.$section.'"><img'.
1843 ' src="'.$CFG->pixpath.'/t/delete.gif" hspace="2" height="11" width="11" border="0" '.
1844 ' alt="'.$str->delete.'" /></a>'.$hideshow.$groupmode.'</span>';
90845098 1845}
1846
b61efafb 1847/**
1848 * given a course object with shortname & fullname, this function will
1849 * truncate the the number of chars allowed and add ... if it was too long
1850 */
1851function course_format_name ($course,$max=100) {
1852
1853 $str = $course->shortname.': '.$course->fullname;
1854 if (strlen($str) <= $max) {
1855 return $str;
1856 }
1857 else {
1858 return substr($str,0,$max-3).'...';
1859 }
1860}
1861
1862/**
1863 * This function will return true if the given course is a child course at all
1864 */
1865function course_in_meta ($course) {
5f37b628 1866 return record_exists("course_meta","child_course",$course->id);
b61efafb 1867}
1868
48e535bc 1869
1870/**
1871 * Print standard form elements on module setup forms in mod/.../mod.html
1872 */
1873function print_standard_coursemodule_settings($form) {
da2224f8 1874 if (! $course = get_record('course', 'id', $form->course)) {
1875 error("This course doesn't exist");
1876 }
1877 print_groupmode_setting($form, $course);
1878 print_visible_setting($form, $course);
48e535bc 1879}
1880
1881/**
1882 * Print groupmode form element on module setup forms in mod/.../mod.html
1883 */
5ebb746b 1884function print_groupmode_setting($form, $course=NULL) {
48e535bc 1885
5ebb746b 1886 if (empty($course)) {
1887 if (! $course = get_record('course', 'id', $form->course)) {
1888 error("This course doesn't exist");
1889 }
1890 }
48e535bc 1891 if ($form->coursemodule) {
1892 if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
1893 error("This course module doesn't exist");
1894 }
1895 } else {
1896 $cm = null;
1897 }
1898 $groupmode = groupmode($course, $cm);
1899 if ($course->groupmode or (!$course->groupmodeforce)) {
1900 echo '<tr valign="top">';
1901 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
7bbe08a2 1902 echo '<td align="left">';
48e535bc 1903 unset($choices);
1904 $choices[NOGROUPS] = get_string('groupsnone');
1905 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
1906 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
1907 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
1908 helpbutton('groupmode', get_string('groupmode'));
1909 echo '</td></tr>';
1910 }
1911}
1912
1913/**
1914 * Print visibility setting form element on module setup forms in mod/.../mod.html
1915 */
5ebb746b 1916function print_visible_setting($form, $course=NULL) {
1ee55c41 1917 if (empty($course)) {
1918 if (! $course = get_record('course', 'id', $form->course)) {
1919 error("This course doesn't exist");
1920 }
1921 }
48e535bc 1922 if ($form->coursemodule) {
1923 $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
1924 } else {
1925 $visible = true;
1926 }
1927
1928 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
1929 $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
1930 } else {
1931 $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
1932 }
1933 if ($hiddensection) {
1934 $visible = false;
1935 }
1936
1937 echo '<tr valign="top">';
1f15db9d 1938 echo '<td align="right"><b>'.get_string('visibletostudents','',moodle_strtolower($course->students)).':</b></td>';
7bbe08a2 1939 echo '<td align="left">';
48e535bc 1940 unset($choices);
1f15db9d 1941 $choices[1] = get_string('show');
1942 $choices[0] = get_string('hide');
48e535bc 1943 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
1944 echo '</td></tr>';
1945}
1946
0705ff84 1947function update_restricted_mods($course,$mods) {
1948 delete_records("course_allowed_modules","course",$course->id);
1949 if (empty($course->restrictmodules)) {
1950 return;
1951 }
1952 else {
1953 foreach ($mods as $mod) {
1954 if ($mod == 0)
1955 continue; // this is the 'allow none' option
1956 $am->course = $course->id;
1957 $am->module = $mod;
1958 insert_record("course_allowed_modules",$am);
1959 }
1960 }
1961}
1962
1963/**
1964 * This function will take an int (module id) or a string (module name)
1965 * and return true or false, whether it's allowed in the given course (object)
1966 * $mod is not allowed to be an object, as the field for the module id is inconsistent
1967 * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
1968 */
1969
1970function course_allowed_module($course,$mod) {
1971 if (empty($course->restrictmodules)) {
1972 return true;
1973 }
1974 if (isadmin()) {
1975 return true;
1976 }
1977 if (is_numeric($mod)) {
1978 $modid = $mod;
1979 } else if (is_string($mod)) {
1980 if ($mod = get_field("modules","id","name",strtolower($mod)))
1981 $modid = $mod;
1982 }
1983 if (empty($modid)) {
1984 return false;
1985 }
1986 return (record_exists("course_allowed_modules","course",$course->id,"module",$modid));
1987}
1988
861efb19 1989/***
1990 *** Efficiently moves many courses around while maintaining
1991 *** sortorder in order.
1992 ***
1993 *** $courseids is an array of course ids
1994 ***
1995 **/
1996
1997function move_courses ($courseids, $categoryid) {
1998
1999 global $CFG;
2000
2001 if (!empty($courseids)) {
2002
2003 $courseids = array_reverse($courseids);
2004
2005 foreach ($courseids as $courseid) {
2006
2007 if (! $course = get_record("course", "id", $courseid)) {
2008 notify("Error finding course $courseid");
2009 } else {
2010 // figure out a sortorder that we can use in the destination category
2011 $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
2012 FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid);
2013 if ($sortorder === false) {
2014 // the category is empty
2015 // rather than let the db default to 0
2016 // set it to > 100 and avoid extra work in fix_coursesortorder()
2017 $sortorder = 200;
2018 } else if ($sortorder < 10) {
2019 fix_course_sortorder($categoryid);
2020 }
2021
2022 $course->category = $categoryid;
2023 $course->sortorder = $sortorder;
2024 $course->fullname = addslashes($course->fullname);
2025 $course->shortname = addslashes($course->shortname);
2026 $course->summary = addslashes($course->summary);
2027 $course->password = addslashes($course->password);
2028 $course->teacher = addslashes($course->teacher);
2029 $course->teachers = addslashes($course->teachers);
2030 $course->student = addslashes($course->student);
2031 $course->students = addslashes($course->students);
2032
2033 if (!update_record('course', $course)) {
2034 notify("An error occurred - course not moved!");
2035 }
2036 }
2037 }
2038 fix_course_sortorder();
2039 }
2040 return true;
2041}
2042
87d32352 2043?>