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