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