fixed bug in paging bar on participants list
[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
bf40f9c1 662 global $CFG, $USER, $THEME, $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) {
dcd338ff 937 if ($visibility) {
938 show_course_module($moduleid);
939 } else {
940 hide_course_module($moduleid);
941 }
7d99d695 942 }
943 }
5867bfb5 944 rebuild_course_cache($courseid);
7d99d695 945 }
946}
ba2e5d73 947
5e367a2d 948
d897cae4 949function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 950/// Prints a section full of activity modules
7977cffd 951 global $CFG, $USER;
952
3d575e6f 953 static $groupbuttons;
32d03b7b 954 static $groupbuttonslink;
52dcc2f9 955 static $isteacher;
956 static $isediting;
7977cffd 957 static $ismoving;
958 static $strmovehere;
959 static $strmovefull;
52dcc2f9 960
a22f8313 961 $labelformatoptions = New stdClass;
110a32e2 962
52dcc2f9 963 if (!isset($isteacher)) {
9fd9c29b 964 $groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
32d03b7b 965 $groupbuttonslink = (!$course->groupmodeforce);
52dcc2f9 966 $isteacher = isteacher($course->id);
52dcc2f9 967 $isediting = isediting($course->id);
7977cffd 968 $ismoving = ismoving($course->id);
3d575e6f 969 if ($ismoving) {
970 $strmovehere = get_string("movehere");
971 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
972 }
7977cffd 973 }
60bd11cf 974 $labelformatoptions->noclean = true;
94361e02 975
c408b0c4 976 $modinfo = unserialize($course->modinfo);
94361e02 977
89adb174 978 echo '<table width="'.$width.'" cellpadding="1" cellspacing="0">';
74666583 979 if (!empty($section->sequence)) {
94361e02 980
981 $sectionmods = explode(",", $section->sequence);
982
983 foreach ($sectionmods as $modnumber) {
9ae687af 984 if (empty($mods[$modnumber])) {
985 continue;
986 }
94361e02 987 $mod = $mods[$modnumber];
c9f6251e 988
52dcc2f9 989 if ($mod->visible or $isteacher) {
c9f6251e 990 echo "<tr><td class=\"activity$mod->modname\">";
7977cffd 991 if ($ismoving) {
992 if ($mod->id == $USER->activitycopy) {
993 continue;
994 }
1c919752 995 echo '<a title="'.$strmovefull.'"'.
8b92f5bb 996 ' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.$USER->sesskey.'">'.
1c919752 997 '<img height="16" width="80" src="'.$CFG->pixpath.'/movehere.gif" '.
998 ' alt="'.$strmovehere.'" border="0" /></a><br />
999 ';
1acfbce5 1000 }
7977cffd 1001 $instancename = urldecode($modinfo[$modnumber]->name);
3c89f9a8 1002 if (!empty($CFG->filterall)) {
b346b424 1003 $instancename = filter_text("<nolink>$instancename</nolink>", $course->id);
3c89f9a8 1004 }
c9f6251e 1005
86aa7ccf 1006 if (!empty($modinfo[$modnumber]->extra)) {
1007 $extra = urldecode($modinfo[$modnumber]->extra);
1008 } else {
1009 $extra = "";
1010 }
c9f6251e 1011
9d361034 1012 if (!empty($modinfo[$modnumber]->icon)) {
1013 $icon = "$CFG->pixpath/".urldecode($modinfo[$modnumber]->icon);
1014 } else {
1015 $icon = "$CFG->modpixpath/$mod->modname/icon.gif";
1016 }
1017
aac94fd0 1018 if ($mod->indent) {
1019 print_spacer(12, 20 * $mod->indent, false);
1020 }
1021
c9f6251e 1022 if ($mod->modname == "label") {
aac94fd0 1023 if (!$mod->visible) {
1024 echo "<span class=\"dimmed_text\">";
1025 }
179c9a50 1026 echo format_text($extra, FORMAT_HTML, $labelformatoptions);
aac94fd0 1027 if (!$mod->visible) {
1028 echo "</span>";
1029 }
c9f6251e 1030
1031 } else { // Normal activity
1032 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
1c919752 1033 echo '<img src="'.$icon.'"'.
1034 ' height="16" width="16" alt="'.$mod->modfullname.'" />'.
1035 ' <font size="2"><a title="'.$mod->modfullname.'" '.$linkcss.' '.$extra.
1036 ' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.
1037 $instancename.'</a></font>';
c9f6251e 1038 }
c9f6251e 1039 if ($isediting) {
32d03b7b 1040 if ($groupbuttons) {
1041 if (! $mod->groupmodelink = $groupbuttonslink) {
1042 $mod->groupmode = $course->groupmode;
1043 }
1044
1045 } else {
3d575e6f 1046 $mod->groupmode = false;
1047 }
ffc69cd3 1048 echo "&nbsp;&nbsp;";
3d575e6f 1049 echo make_editing_buttons($mod, $absolute, true, $mod->indent);
c9f6251e 1050 }
ffc69cd3 1051 echo "</td>";
c9f6251e 1052 echo "</tr>";
94361e02 1053 }
94361e02 1054 }
87d32352 1055 } else {
1056 echo "<tr><td></td></tr>"; // needed for XHTML compatibility
94361e02 1057 }
7977cffd 1058 if ($ismoving) {
1c919752 1059 echo '<tr><td><a title="'.$strmovefull.'"'.
8b92f5bb 1060 ' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.$USER->sesskey.'">'.
1c919752 1061 '<img height="16" width="80" src="'.$CFG->pixpath.'/movehere.gif" '.
1062 ' alt="'.$strmovehere.'" border="0" /></a></td></tr>
1063 ';
7977cffd 1064 }
c9f6251e 1065 echo "</table>\n\n";
a7ad3ea6 1066}
1067
5867bfb5 1068
cb57e6f4 1069function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
e0161bff 1070// Prints the menus to add activities and resources
1071
8b92f5bb 1072 global $CFG, $USER;
6da4b261 1073 static $straddactivity, $stractivities, $straddresource, $resources;
e0161bff 1074
1075 if (!isset($straddactivity)) {
1076 $straddactivity = get_string('addactivity');
1077 $straddresource = get_string('addresource');
6da4b261 1078
1079 /// Standard resource types
1080 require_once("$CFG->dirroot/mod/resource/lib.php");
1081 $resourceraw = resource_get_resource_types();
1082
1083 foreach ($resourceraw as $type => $name) {
839f2456 1084 $resources["resource&amp;type=$type"] = $name;
e0161bff 1085 }
e0161bff 1086 $resources['label'] = get_string('resourcetypelabel', 'resource');
1087 }
1088
cb57e6f4 1089 $output = '';
1090
1091 $output .= '<div align="right"><table align="right"><tr><td>';
8b92f5bb 1092 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 1093 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
cb57e6f4 1094 $output .= '</td>';
1095
1096 if ($vertical) {
1097 $output .= '</tr><tr>';
1098 }
1099
1100 $output .= '<td>';
8b92f5bb 1101 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 1102 $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
cb57e6f4 1103 $output .= '</td></tr></table>';
1104 $output .= '</div>';
1105
1106 if ($return) {
1107 return $output;
1108 } else {
1109 echo $output;
1110 }
e0161bff 1111}
1112
5867bfb5 1113function rebuild_course_cache($courseid=0) {
1114// Rebuilds the cached list of course activities stored in the database
1115// If a courseid is not specified, then all are rebuilt
1116
1117 if ($courseid) {
1118 $select = "id = '$courseid'";
1119 } else {
1120 $select = "";
1121 }
1122
1a31c2b3 1123 if ($courses = get_records_select("course", $select,'','id,fullname')) {
5867bfb5 1124 foreach ($courses as $course) {
1125 $modinfo = serialize(get_array_of_activities($course->id));
1126 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
1127 notify("Could not cache module information for course '$course->fullname'!");
1128 }
1129 }
1130 }
1131}
1132
1133
7541bc3e 1134function print_heading_block($heading, $width="100%", $class="headingblock") {
5e367a2d 1135 global $THEME;
1136
7541bc3e 1137 echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
1138 echo "<tr><td bgcolor=\"$THEME->cellheading\" class=\"$class\">";
5e367a2d 1139 echo stripslashes($heading);
1140 echo "</td></tr></table>";
5e367a2d 1141}
1142
c2cb4545 1143
1144function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 1145/// Given an empty array, this function recursively travels the
c2cb4545 1146/// categories, building up a nice list for display. It also makes
1147/// an array that list all the parents for each category.
1148
1149 if ($category) {
1150 if ($path) {
e05bcf2f 1151 $path = $path.' / '.$category->name;
c2cb4545 1152 } else {
e05bcf2f 1153 $path = $category->name;
c2cb4545 1154 }
1155 $list[$category->id] = $path;
1156 } else {
1157 $category->id = 0;
1158 }
1159
e05bcf2f 1160 if ($categories = get_categories($category->id)) { // Print all the children recursively
c2cb4545 1161 foreach ($categories as $cat) {
1162 if (!empty($category->id)) {
3bd4de22 1163 if (isset($parents[$category->id])) {
2832badf 1164 $parents[$cat->id] = $parents[$category->id];
1165 }
c2cb4545 1166 $parents[$cat->id][] = $category->id;
1167 }
89adb174 1168 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1169 }
1170 }
1171}
1172
1173
c2cb4545 1174function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) {
89adb174 1175/// Recursive function to print out all the categories in a nice format
c2cb4545 1176/// with or without courses included
9ff5310a 1177 global $CFG;
e05bcf2f 1178
1179 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1180 return;
9ff5310a 1181 }
c2cb4545 1182
1183 if (!$displaylist) {
e92fe848 1184 make_categories_list($displaylist, $parentslist);
c2cb4545 1185 }
1186
1187 if ($category) {
3af6e1db 1188 if ($category->visible or iscreator()) {
d2b6ba70 1189 print_category_info($category, $depth);
c2cb4545 1190 } else {
1191 return; // Don't bother printing children of invisible categories
1192 }
89adb174 1193
c2cb4545 1194 } else {
c2cb4545 1195 $category->id = "0";
1196 }
1197
1198 if ($categories = get_categories($category->id)) { // Print all the children recursively
1199 $countcats = count($categories);
1200 $count = 0;
1201 $first = true;
1202 $last = false;
1203 foreach ($categories as $cat) {
1204 $count++;
1205 if ($count == $countcats) {
1206 $last = true;
1207 }
1208 $up = $first ? false : true;
1209 $down = $last ? false : true;
1210 $first = false;
1211
89adb174 1212 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1);
c2cb4545 1213 }
1214 }
c2cb4545 1215}
1216
1217
d2b6ba70 1218function print_category_info($category, $depth) {
1219/// Prints the category info in indented fashion
1220/// This function is only used by print_whole_category_list() above
c2cb4545 1221
1222 global $CFG;
b48f834c 1223 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1224
b48f834c 1225 if (empty($strsummary)) {
e05bcf2f 1226 $strallowguests = get_string('allowguests');
1227 $strrequireskey = get_string('requireskey');
1228 $strsummary = get_string('summary');
b48f834c 1229 }
ba2e5d73 1230
e05bcf2f 1231 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1232
8ef9cb56 1233 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
839f2456 1234 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" width="16" height="16" border="0" alt="" />';
b48f834c 1235 } else {
1236 $catimage = "&nbsp";
8ef9cb56 1237 }
b48f834c 1238
87d32352 1239 echo "\n\n".'<table border="0" cellpadding="3" cellspacing="0" width="100%">';
d2b6ba70 1240
c2cb4545 1241 if ($CFG->frontpage == FRONTPAGECOURSELIST) {
a36a1759 1242 $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 1243
1244 echo "<tr>";
1245
1246 if ($depth) {
1247 $indent = $depth*30;
1248 $rows = count($courses) + 1;
1c919752 1249 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1250 print_spacer(10, $indent);
e05bcf2f 1251 echo '</td>';
b48f834c 1252 }
89adb174 1253
e05bcf2f 1254 echo '<td valign="top">'.$catimage.'</td>';
1255 echo '<td valign="top" width="100%" class="categoryname">';
1256 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1257 echo '</td>';
1258 echo '<td class="categoryname">&nbsp;</td>';
1259 echo '</tr>';
b48f834c 1260
9ff5310a 1261 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1262 foreach ($courses as $course) {
e05bcf2f 1263 $linkcss = $course->visible ? '' : ' class="dimmed" ';
1264 echo '<tr><td valign="top" width="30">&nbsp;';
1265 echo '</td><td valign="top" width="100%" class="coursename">';
1266 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>';
1267 echo '</td><td align="right" valign="top" nowrap="nowrap" class="coursename">';
c2cb4545 1268 if ($course->guest ) {
e05bcf2f 1269 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1270 echo '<img hspace="1" alt="'.$strallowguests.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1271 } else {
e05bcf2f 1272 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1273 }
c2cb4545 1274 if ($course->password) {
e05bcf2f 1275 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1276 echo '<img hspace="1" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1277 } else {
e05bcf2f 1278 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1279 }
1280 if ($course->summary) {
e05bcf2f 1281 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
1282 '<img hspace="1" alt="'.$strsummary.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1283 400, 500, $strsummary);
ebe8ddc1 1284 } else {
e05bcf2f 1285 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1286 }
e05bcf2f 1287 echo '</td></tr>';
0c656181 1288 }
ba2e5d73 1289 }
d2b6ba70 1290 } else {
b48f834c 1291
1292 if ($depth) {
1293 $indent = $depth*20;
e05bcf2f 1294 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1295 print_spacer(10, $indent);
e05bcf2f 1296 echo '</td>';
d2b6ba70 1297 }
89adb174 1298
e05bcf2f 1299 echo '<td valign="top" width="100%" class="categoryname">';
1300 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1301 echo '</td>';
1302 echo '<td valign="top" class="categorynumber">';
1303 if ($category->coursecount) {
1304 echo $category->coursecount;
1305 }
1306 echo '</td></tr>';
c2cb4545 1307 }
e05bcf2f 1308 echo '</table>';
c2cb4545 1309}
1310
c2cb4545 1311
c2cb4545 1312function print_courses($category, $width="100%") {
1313/// Category is 0 (for all courses) or an object
1314
1315 global $CFG, $THEME;
1316
1317 if (empty($category)) {
90c2ca2e 1318 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1319 if (count($categories) == 1) {
1320 $category = array_shift($categories);
76ca0f31 1321 $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 1322 } else {
e05bcf2f 1323 $courses = get_courses('all', 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher');
90c2ca2e 1324 }
1325 unset($categories);
607809b3 1326 } else {
c2cb4545 1327 $categories = get_categories($category->id); // sub categories
76ca0f31 1328 $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 1329 }
1330
c2cb4545 1331 if ($courses) {
1332 foreach ($courses as $course) {
1333 print_course($course, $width);
1334 echo "<br />\n";
1335 }
1336 } else {
1337 print_heading(get_string("nocoursesyet"));
1338 }
1339
1340}
1341
1342
1343function print_course($course, $width="100%") {
1344
1345 global $CFG, $THEME;
1346
146bbb8f 1347 static $enrol;
1348
1349 if (empty($enrol)) {
1350 require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
1351 $enrol = new enrolment_plugin;
1352 }
1353
1b6a4b1d 1354 print_simple_box_start("center", "$width", $THEME->cellcontent, 5, "coursebox");
c2cb4545 1355
22288704 1356 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
1357
c2cb4545 1358 echo "<table width=\"100%\">";
e5e81e78 1359 echo '<tr valign="top">';
1360 echo '<td valign="top" width="50%" class="courseboxinfo">';
34b5847a 1361 echo '<b><a title="'.get_string('entercourse').'"'.
e5e81e78 1362 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
34b5847a 1363 $course->fullname.'</a></b><br />';
c2cb4545 1364 if ($teachers = get_course_teachers($course->id)) {
34b5847a 1365 echo "<span class=\"courseboxteachers\">\n";
c2cb4545 1366 foreach ($teachers as $teacher) {
1367 if ($teacher->authority > 0) {
1368 if (!$teacher->role) {
1369 $teacher->role = $course->teacher;
1370 }
2ac64806 1371 $fullname = fullname($teacher, isteacher($course->id)); // is the USER a teacher of that course
8bd84b4d 1372 echo $teacher->role.': <a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id.
1373 '&amp;course='.SITEID.'">'.$fullname.'</a><br />';
c2cb4545 1374 }
1375 }
34b5847a 1376 echo "</span>\n";
c2cb4545 1377 }
c2cb4545 1378
146bbb8f 1379 echo $enrol->get_access_icons($course);
c2cb4545 1380
e5e81e78 1381 echo '</td><td valign="top" width="50%" class="courseboxsummary">';
9f39c190 1382 $options = NULL;
1383 $options->noclean = true;
34b5847a 1384 $options->para = false;
9f39c190 1385 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
c2cb4545 1386 echo "</td></tr>";
1387 echo "</table>";
1388
1389 print_simple_box_end();
1390}
1391
1392
1393function print_my_moodle() {
1394/// Prints custom user information on the home page.
1395/// Over time this can include all sorts of information
1396
1397 global $USER, $CFG;
1398
1399 if (!isset($USER->id)) {
1400 error("It shouldn't be possible to see My Moodle without being logged in.");
1401 }
1402
1403 if ($courses = get_my_courses($USER->id)) {
1404 foreach ($courses as $course) {
c81696e5 1405 if (!$course->category) {
1406 continue;
1407 }
c2cb4545 1408 print_course($course, "100%");
1409 echo "<br />\n";
1410 }
38a10939 1411
7f989948 1412 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1413 echo "<table width=\"100%\"><tr><td align=\"center\">";
1414 print_course_search("", false, "short");
1415 echo "</td><td align=\"center\">";
1416 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1417 echo "</td></tr></table>\n";
1418 }
26330001 1419 } else {
1420 if (count_records("course_categories") > 1) {
cb29b020 1421 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1422 print_whole_category_list();
1423 print_simple_box_end();
1424 } else {
1425 print_courses(0, "100%");
1426 }
607809b3 1427 }
2b8cef80 1428}
1429
11b0c469 1430
a8b56716 1431function print_course_search($value="", $return=false, $format="plain") {
38a10939 1432
1433 global $CFG;
1434
1435 $strsearchcourses= get_string("searchcourses");
1436
1c919752 1437 if ($format == 'plain') {
87d32352 1438 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1439 $output .= '<center><p align="center" class="coursesearchbox">';
1440 $output .= '<input type="text" size="30" name="search" alt="'.$strsearchcourses.'" value="'.$value.'" />';
1c919752 1441 $output .= '<input type="submit" value="'.$strsearchcourses.'" />';
87d32352 1442 $output .= '</p></center></form>';
1c919752 1443 } else if ($format == 'short') {
87d32352 1444 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1445 $output .= '<center><p align="center" class="coursesearchbox">';
1446 $output .= '<input type="text" size="12" name="search" alt="'.$strsearchcourses.'" value="'.$value.'" />';
1c919752 1447 $output .= '<input type="submit" value="'.$strsearchcourses.'" />';
87d32352 1448 $output .= '</p></center></form>';
1c919752 1449 } else if ($format == 'navbar') {
87d32352 1450 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1451 $output .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
1452 $output .= '<input type="text" size="20" name="search" alt="'.$strsearchcourses.'" value="'.$value.'" />';
1c919752 1453 $output .= '<input type="submit" value="'.$strsearchcourses.'" />';
1c919752 1454 $output .= '</td></tr></table>';
87d32352 1455 $output .= '</form>';
a8b56716 1456 }
1457
1458 if ($return) {
1459 return $output;
1460 }
1461 echo $output;
38a10939 1462}
11b0c469 1463
1464/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1465
1466function add_course_module($mod) {
11b0c469 1467
e5dfd0f3 1468 $mod->added = time();
53f4ad2c 1469 unset($mod->id);
11b0c469 1470
e5dfd0f3 1471 return insert_record("course_modules", $mod);
11b0c469 1472}
1473
7977cffd 1474function add_mod_to_section($mod, $beforemod=NULL) {
1475/// Given a full mod object with section and course already defined
1476/// If $before is specified, then this is an existing ID which we
1477/// will insert the new module before
1478///
1479/// Returns the course_sections ID where the mod is inserted
11b0c469 1480
9fa49e22 1481 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1482
1483 $section->sequence = trim($section->sequence);
1484
1485 if (empty($section->sequence)) {
11b0c469 1486 $newsequence = "$mod->coursemodule";
7977cffd 1487
1488 } else if ($beforemod) {
1489 $modarray = explode(",", $section->sequence);
1490
1491 if ($key = array_keys ($modarray, $beforemod->id)) {
1492 $insertarray = array($mod->id, $beforemod->id);
1493 array_splice($modarray, $key[0], 1, $insertarray);
1494 $newsequence = implode(",", $modarray);
1495
1496 } else { // Just tack it on the end anyway
1497 $newsequence = "$section->sequence,$mod->coursemodule";
1498 }
1499
1500 } else {
1501 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1502 }
89adb174 1503
e5dfd0f3 1504 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1505 return $section->id; // Return course_sections ID that was used.
11b0c469 1506 } else {
e5dfd0f3 1507 return 0;
11b0c469 1508 }
89adb174 1509
11b0c469 1510 } else { // Insert a new record
e5dfd0f3 1511 $section->course = $mod->course;
1512 $section->section = $mod->section;
1513 $section->summary = "";
1514 $section->sequence = $mod->coursemodule;
1515 return insert_record("course_sections", $section);
11b0c469 1516 }
1517}
1518
3d575e6f 1519function set_groupmode_for_module($id, $groupmode) {
1520 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1521}
1522
1acfbce5 1523function hide_course_module($mod) {
dcd338ff 1524 $cm = get_record('course_modules', 'id', $mod);
1525 $modulename = get_field('modules', 'name', 'id', $cm->module);
1526 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1527 foreach($events as $event) {
1528 hide_event($event);
1529 }
1530 }
1acfbce5 1531 return set_field("course_modules", "visible", 0, "id", $mod);
1532}
1533
1534function show_course_module($mod) {
dcd338ff 1535 $cm = get_record('course_modules', 'id', $mod);
1536 $modulename = get_field('modules', 'name', 'id', $cm->module);
1537 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1538 foreach($events as $event) {
1539 show_event($event);
1540 }
1541 }
1acfbce5 1542 return set_field("course_modules", "visible", 1, "id", $mod);
1543}
1544
11b0c469 1545function delete_course_module($mod) {
1546 return set_field("course_modules", "deleted", 1, "id", $mod);
1547}
1548
1549function delete_mod_from_section($mod, $section) {
11b0c469 1550
e5dfd0f3 1551 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1552
e5dfd0f3 1553 $modarray = explode(",", $section->sequence);
11b0c469 1554
1555 if ($key = array_keys ($modarray, $mod)) {
1556 array_splice($modarray, $key[0], 1);
1557 $newsequence = implode(",", $modarray);
e5dfd0f3 1558 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1559 } else {
1560 return false;
1561 }
89adb174 1562
11b0c469 1563 }
7977cffd 1564 return false;
11b0c469 1565}
1566
12905134 1567function move_section($course, $section, $move) {
1568/// Moves a whole course section up and down within the course
798b70a1 1569 global $USER;
12905134 1570
1571 if (!$move) {
1572 return true;
1573 }
1574
1575 $sectiondest = $section + $move;
1576
1577 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1578 return false;
1579 }
1580
1581 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1582 return false;
1583 }
1584
1585 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1586 return false;
1587 }
1588
56e34ee4 1589 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1590 return false;
1591 }
56e34ee4 1592 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1593 return false;
1594 }
798b70a1 1595 // if the focus is on the section that is being moved, then move the focus along
1596 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
1597 course_set_display($course->id, $sectiondest);
1598 }
12905134 1599 return true;
1600}
1601
1602
7977cffd 1603function moveto_module($mod, $section, $beforemod=NULL) {
1604/// All parameters are objects
1605/// Move the module object $mod to the specified $section
1606/// If $beforemod exists then that is the module
1607/// before which $modid should be inserted
1608
1609/// Remove original module from original section
1610
1611 if (! delete_mod_from_section($mod->id, $mod->section)) {
1612 notify("Could not delete module from existing section");
1613 }
1614
1615/// Update module itself if necessary
1616
1617 if ($mod->section != $section->id) {
89adb174 1618 $mod->section = $section->id;
7977cffd 1619
1620 if (!update_record("course_modules", $mod)) {
1621 return false;
1622 }
1623 }
1624
1625/// Add the module into the new section
1626
1627 $mod->course = $section->course;
1628 $mod->section = $section->section; // need relative reference
1629 $mod->coursemodule = $mod->id;
1630
1631 if (! add_mod_to_section($mod, $beforemod)) {
1632 return false;
1633 }
1634
1635 return true;
1636
1637}
1638
1639
11b0c469 1640
7c0f2984 1641function move_module($cm, $move) {
12905134 1642/// Moves an activity module up and down within the course
11b0c469 1643
1644 if (!$move) {
1645 return true;
1646 }
1647
11b0c469 1648 if (! $thissection = get_record("course_sections", "id", $cm->section)) {
1649 error("This course section doesn't exist");
1650 }
1651
1652 $mods = explode(",", $thissection->sequence);
1653
1654 $len = count($mods);
1655 $pos = array_keys($mods, $cm->id);
1656 $thepos = $pos[0];
1657
1658 if ($len == 0 || count($pos) == 0 ) {
1659 error("Very strange. Could not find the required module in this section.");
1660 }
1661
1662 if ($len == 1) {
1663 $first = true;
1664 $last = true;
1665 } else {
1666 $first = ($thepos == 0);
1667 $last = ($thepos == $len - 1);
1668 }
1669
1670 if ($move < 0) { // Moving the module up
1671
1672 if ($first) {
ad41694c 1673 if ($thissection->section == 0) { // First section, do nothing
11b0c469 1674 return true;
ad41694c 1675
11b0c469 1676 } else { // Push onto end of previous section
1677 $prevsectionnumber = $thissection->section - 1;
89adb174 1678 if (! $prevsection = get_record("course_sections", "course", "$thissection->course",
9fa49e22 1679 "section", "$prevsectionnumber")) {
11b0c469 1680 error("Previous section ($prevsection->id) doesn't exist");
1681 }
1682
74666583 1683 if (!empty($prevsection->sequence)) {
11b0c469 1684 $newsequence = "$prevsection->sequence,$cm->id";
1685 } else {
1686 $newsequence = "$cm->id";
1687 }
1688
1689 if (! set_field("course_sections", "sequence", $newsequence, "id", $prevsection->id)) {
1690 error("Previous section could not be updated");
1691 }
1692
1693 if (! set_field("course_modules", "section", $prevsection->id, "id", $cm->id)) {
1694 error("Module could not be updated");
1695 }
1696
1697 array_splice($mods, 0, 1);
1698 $newsequence = implode(",", $mods);
1699 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1700 error("Module could not be updated");
1701 }
1702
1703 return true;
1704
1705 }
1706 } else { // move up within this section
1707 $swap = $mods[$thepos-1];
1708 $mods[$thepos-1] = $mods[$thepos];
1709 $mods[$thepos] = $swap;
89adb174 1710
11b0c469 1711 $newsequence = implode(",", $mods);
1712 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1713 error("This section could not be updated");
1714 }
1715 return true;
1716 }
1717
1718 } else { // Moving the module down
1719
1720 if ($last) {
1721 $nextsectionnumber = $thissection->section + 1;
89adb174 1722 if ($nextsection = get_record("course_sections", "course", "$thissection->course",
9fa49e22 1723 "section", "$nextsectionnumber")) {
11b0c469 1724
74666583 1725 if (!empty($nextsection->sequence)) {
11b0c469 1726 $newsequence = "$cm->id,$nextsection->sequence";
1727 } else {
1728 $newsequence = "$cm->id";
1729 }
1730
1731 if (! set_field("course_sections", "sequence", $newsequence, "id", $nextsection->id)) {
1732 error("Next section could not be updated");
1733 }
1734
1735 if (! set_field("course_modules", "section", $nextsection->id, "id", $cm->id)) {
1736 error("Module could not be updated");
1737 }
1738
1739 array_splice($mods, $thepos, 1);
1740 $newsequence = implode(",", $mods);
1741 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1742 error("This section could not be updated");
1743 }
1744 return true;
1745
1746 } else { // There is no next section, so just return
1747 return true;
1748
1749 }
1750 } else { // move down within this section
1751 $swap = $mods[$thepos+1];
1752 $mods[$thepos+1] = $mods[$thepos];
1753 $mods[$thepos] = $swap;
89adb174 1754
11b0c469 1755 $newsequence = implode(",", $mods);
1756 if (! set_field("course_sections", "sequence", $newsequence, "id", $thissection->id)) {
1757 error("This section could not be updated");
1758 }
1759 return true;
1760 }
1761 }
1762}
1763
3d575e6f 1764function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1) {
8b92f5bb 1765 global $CFG, $THEME, $USER;
94361e02 1766
3d575e6f 1767 static $str;
1768
1769 if (!isset($str)) {
1770 $str->delete = get_string("delete");
1771 $str->move = get_string("move");
1772 $str->moveup = get_string("moveup");
1773 $str->movedown = get_string("movedown");
1774 $str->moveright = get_string("moveright");
1775 $str->moveleft = get_string("moveleft");
1776 $str->update = get_string("update");
d33a2a6f 1777 $str->duplicate = get_string("duplicate");
3d575e6f 1778 $str->hide = get_string("hide");
1779 $str->show = get_string("show");
1780 $str->clicktochange = get_string("clicktochange");
32d03b7b 1781 $str->forcedmode = get_string("forcedmode");
3d575e6f 1782 $str->groupsnone = get_string("groupsnone");
1783 $str->groupsseparate = get_string("groupsseparate");
1784 $str->groupsvisible = get_string("groupsvisible");
1acfbce5 1785 }
94361e02 1786
1787 if ($absolute) {
dc0dc7d5 1788 $path = "$CFG->wwwroot/course";
1789 } else {
1790 $path = ".";
1791 }
1792
1793 if (empty($THEME->custompix)) {
1794 $pixpath = "$path/../pix";
94361e02 1795 } else {
dc0dc7d5 1796 $pixpath = "$path/../theme/$CFG->theme/pix";
94361e02 1797 }
1acfbce5 1798
3d575e6f 1799 if ($mod->visible) {
8b92f5bb 1800 $hideshow = "<a title=\"$str->hide\" href=\"$path/mod.php?hide=$mod->id&amp;sesskey=$USER->sesskey\"><img".
839f2456 1801 " src=\"$pixpath/t/hide.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" alt=\"$str->hide\" /></a> ";
1acfbce5 1802 } else {
8b92f5bb 1803 $hideshow = "<a title=\"$str->show\" href=\"$path/mod.php?show=$mod->id&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1804 " src=\"$pixpath/t/show.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
839f2456 1805 "border=\"0\" alt=\"$str->show\" /></a> ";
7977cffd 1806 }
3d575e6f 1807 if ($mod->groupmode !== false) {
1808 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1809 $grouptitle = $str->groupsseparate;
1810 $groupimage = "$pixpath/t/groups.gif";
8b92f5bb 1811 $grouplink = "$path/mod.php?id=$mod->id&amp;groupmode=0&amp;sesskey=$USER->sesskey";
3d575e6f 1812 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1813 $grouptitle = $str->groupsvisible;
1814 $groupimage = "$pixpath/t/groupv.gif";
8b92f5bb 1815 $grouplink = "$path/mod.php?id=$mod->id&amp;groupmode=1&amp;sesskey=$USER->sesskey";
32d03b7b 1816 } else {
1817 $grouptitle = $str->groupsnone;
1818 $groupimage = "$pixpath/t/groupn.gif";
8b92f5bb 1819 $grouplink = "$path/mod.php?id=$mod->id&amp;groupmode=2&amp;sesskey=$USER->sesskey";
32d03b7b 1820 }
1821 if ($mod->groupmodelink) {
1822 $groupmode = "<a title=\"$grouptitle ($str->clicktochange)\" href=\"$grouplink\">".
dbcd54b7 1823 "<img src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1824 "border=\"0\" alt=\"$grouptitle\" /></a>";
3d575e6f 1825 } else {
32d03b7b 1826 $groupmode = "<img title=\"$grouptitle ($str->forcedmode)\" ".
dbcd54b7 1827 " src=\"$groupimage\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1828 "border=\"0\" alt=\"$grouptitle\" />";
3d575e6f 1829 }
1830 } else {
1831 $groupmode = "";
1832 }
7977cffd 1833
1834 if ($moveselect) {
8b92f5bb 1835 $move = "<a title=\"$str->move\" href=\"$path/mod.php?copy=$mod->id&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1836 " src=\"$pixpath/t/move.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1837 " border=\"0\" alt=\"$str->move\" /></a>";
493486c4 1838 } else {
8b92f5bb 1839 $move = "<a title=\"$str->moveup\" href=\"$path/mod.php?id=$mod->id&amp;move=-1&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1840 " src=\"$pixpath/t/up.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1841 " border=\"0\" alt=\"$str->moveup\" /></a>".
8b92f5bb 1842 "<a title=\"$str->movedown\" href=\"$path/mod.php?id=$mod->id&amp;move=1&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1843 " src=\"$pixpath/t/down.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
26686b7a 1844 " border=\"0\" alt=\"$str->movedown\" /></a>";
7977cffd 1845 }
1846
aac94fd0 1847 $leftright = "";
1848 if ($indent > 0) {
8b92f5bb 1849 $leftright .= "<a title=\"$str->moveleft\" href=\"$path/mod.php?id=$mod->id&amp;indent=-1&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1850 " src=\"$pixpath/t/left.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
839f2456 1851 " border=\"0\" alt=\"$str->moveleft\" /></a>";
aac94fd0 1852 }
1853 if ($indent >= 0) {
8b92f5bb 1854 $leftright .= "<a title=\"$str->moveright\" href=\"$path/mod.php?id=$mod->id&amp;indent=1&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1855 " src=\"$pixpath/t/right.gif\" hspace=\"2\" height=\"11\" width=\"11\" ".
839f2456 1856 " border=\"0\" alt=\"$str->moveright\" /></a>";
aac94fd0 1857 }
1858
bf1241ad 1859 return "$leftright$move".
8b92f5bb 1860 "<a title=\"$str->update\" href=\"$path/mod.php?update=$mod->id&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1861 " src=\"$pixpath/t/edit.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" ".
839f2456 1862 " alt=\"$str->update\" /></a>".
d4bfbe4d 1863 // Following line is commented out until this feature is more definite -- martin
8b92f5bb 1864 // "<a title=\"$str->duplicate\" href=\"$path/mod.php?duplicate=$mod->id&amp;sesskey=$USER->sesskey\"> 2 </a>".
1865 "<a title=\"$str->delete\" href=\"$path/mod.php?delete=$mod->id&amp;sesskey=$USER->sesskey\"><img".
dbcd54b7 1866 " src=\"$pixpath/t/delete.gif\" hspace=\"2\" height=\"11\" width=\"11\" border=\"0\" ".
839f2456 1867 " alt=\"$str->delete\" /></a>$hideshow$groupmode";
90845098 1868}
1869
87d32352 1870?>