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