MDL-7861, fixing broken xhtml
[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
92890025 9define('COURSE_MAX_LOG_DISPLAY', 150); // days
10define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
11define('COURSE_LIVELOG_REFRESH', 60); // Seconds
12define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds
13define('COURSE_MAX_SUMMARIES_PER_PAGE', 10); // courses
950c35a9 14define('COURSE_MAX_COURSES_PER_DROPDOWN',1000); // max courses in log dropdown before switching to optional
92890025 15define('COURSE_MAX_USERS_PER_DROPDOWN',1000); // max users in log dropdown before switching to optional
16define('FRONTPAGENEWS', 0);
6f24e48e 17define('FRONTPAGECOURSELIST', 1);
18define('FRONTPAGECATEGORYNAMES', 2);
19define('FRONTPAGETOPICONLY', 3);
20define('FRONTPAGECATEGORYCOMBO', 4);
21define('FRONTPAGECOURSELIMIT', 200); // maximum number of courses displayed on the frontpage
22define('EXCELROWS', 65535);
23define('FIRSTUSEDEXCELROW', 3);
60fdc714 24
f9903ed0 25
587510be 26function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0, $selecteddate="lastlogin",
4581271a 27 $mod="", $modid="activity/All", $modaction="", $selectedgroup="", $selectedsort="default") {
cb83c3cb 28
29 global $USER, $CFG;
30
587510be 31 if ($advancedfilter) {
32
33 // Get all the possible users
34 $users = array();
89adb174 35
65ee9c16 36 if ($courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname')) {
587510be 37 foreach ($courseusers as $courseuser) {
1c45e42e 38 $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
587510be 39 }
cb83c3cb 40 }
587510be 41 if ($guest = get_guest()) {
42 $users[$guest->id] = fullname($guest);
43 }
89adb174 44
51792df0 45 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
587510be 46 if ($ccc = get_records("course", "", "", "fullname")) {
47 foreach ($ccc as $cc) {
48 if ($cc->category) {
49 $courses["$cc->id"] = "$cc->fullname";
50 } else {
51 $courses["$cc->id"] = " $cc->fullname (Site)";
89adb174 52 }
587510be 53 }
cb83c3cb 54 }
587510be 55 asort($courses);
56 }
4581271a 57
587510be 58 $activities = array();
cb83c3cb 59
587510be 60 $selectedactivity = $modid;
4581271a 61
fea43a7f 62 /// Casting $course->modinfo to string prevents one notice when the field is null
63 if ($modinfo = unserialize((string)$course->modinfo)) {
587510be 64 $section = 0;
65 if ($course->format == 'weeks') { // Body
66 $strsection = get_string("week");
67 } else {
68 $strsection = get_string("topic");
cb83c3cb 69 }
cb83c3cb 70
587510be 71 $activities["activity/All"] = "All activities";
72 $activities["activity/Assignments"] = "All assignments";
73 $activities["activity/Chats"] = "All chats";
74 $activities["activity/Forums"] = "All forums";
75 $activities["activity/Quizzes"] = "All quizzes";
76 $activities["activity/Workshops"] = "All workshops";
77
78 $activities["section/individual"] = "------------- Individual Activities --------------";
79
80 foreach ($modinfo as $mod) {
81 if ($mod->mod == "label") {
82 continue;
89adb174 83 }
3924b988 84 if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities',get_context_instance(CONTEXT_MODULE, $mod->cm))) {
9c08ad13 85 continue;
86 }
87
587510be 88 if ($mod->section > 0 and $section <> $mod->section) {
89 $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
90 }
91 $section = $mod->section;
235a4ee8 92 $mod->name = strip_tags(format_string(urldecode($mod->name),true));
587510be 93 if (strlen($mod->name) > 55) {
94 $mod->name = substr($mod->name, 0, 50)."...";
95 }
96 if (!$mod->visible) {
97 $mod->name = "(".$mod->name.")";
98 }
99 $activities["$mod->cm"] = $mod->name;
100
101 if ($mod->cm == $modid) {
102 $selectedactivity = "$mod->cm";
103 }
cb83c3cb 104 }
105 }
cb83c3cb 106
587510be 107 $strftimedate = get_string("strftimedate");
108 $strftimedaydate = get_string("strftimedaydate");
cb83c3cb 109
587510be 110 asort($users);
cb83c3cb 111
587510be 112 // Get all the possible dates
113 // Note that we are keeping track of real (GMT) time and user time
114 // User time is only used in displays - all calcs and passing is GMT
cb83c3cb 115
587510be 116 $timenow = time(); // GMT
cb83c3cb 117
587510be 118 // What day is it now for the user, and when is midnight that day (in GMT).
119 $timemidnight = $today = usergetmidnight($timenow);
cb83c3cb 120
587510be 121 $dates = array();
122 $dates["$USER->lastlogin"] = get_string("lastlogin").", ".userdate($USER->lastlogin, $strftimedate);
123 $dates["$timemidnight"] = get_string("today").", ".userdate($timenow, $strftimedate);
cb83c3cb 124
587510be 125 if (!$course->startdate or ($course->startdate > $timenow)) {
126 $course->startdate = $course->timecreated;
127 }
cb83c3cb 128
587510be 129 $numdates = 1;
130 while ($timemidnight > $course->startdate and $numdates < 365) {
131 $timemidnight = $timemidnight - 86400;
132 $timenow = $timenow - 86400;
133 $dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
134 $numdates++;
135 }
cb83c3cb 136
96dcfb56 137 if ($selecteddate === "lastlogin") {
587510be 138 $selecteddate = $USER->lastlogin;
139 }
140
141 echo '<form action="recent.php" method="get">';
1c919752 142 echo '<input type="hidden" name="chooserecent" value="1" />';
587510be 143 echo "<center>";
144 echo "<table>";
145
51792df0 146 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
587510be 147 echo "<tr><td><b>" . get_string("courses") . "</b></td><td>";
148 choose_from_menu ($courses, "id", $course->id, "");
149 echo "</td></tr>";
150 } else {
1c919752 151 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
587510be 152 }
cb83c3cb 153
f5ffb87d 154 $sortfields = array("default" => get_string("bycourseorder"),"dateasc" => get_string("datemostrecentlast"), "datedesc" => get_string("datemostrecentfirst"));
4581271a 155
587510be 156 echo "<tr><td><b>" . get_string("participants") . "</b></td><td>";
157 choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") );
158 echo "</td>";
159
1c919752 160 echo '<td align="right"><b>' . get_string("since") . '</b></td><td>';
587510be 161 choose_from_menu ($dates, "date", $selecteddate, get_string("alldays"));
4581271a 162 echo "</td></tr>";
4581271a 163
587510be 164 echo "<tr><td><b>" . get_string("activities") . "</b></td><td>";
165 choose_from_menu ($activities, "modid", $selectedactivity, "");
166 echo "</td>";
4581271a 167
1c919752 168 echo '<td align="right"><b>' . get_string("sortby") . "</b></td><td>";
587510be 169 choose_from_menu ($sortfields, "sortby", $selectedsort, "");
170 echo "</td></tr>";
4581271a 171
587510be 172 echo '<tr>';
4581271a 173
587510be 174 $groupmode = groupmode($course);
4581271a 175
3924b988 176 if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id)))) {
587510be 177 if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
4581271a 178 echo '<td><b>';
587510be 179 if ($groupmode == VISIBLEGROUPS) {
180 print_string('groupsvisible');
181 } else {
182 print_string('groupsseparate');
183 }
184 echo ':</b></td><td>';
185 choose_from_menu($groups, "selectedgroup", $selectedgroup, get_string("allgroups"), "", "");
186 echo '</td>';
187 }
188 }
189
190
1c919752 191 echo '<td colspan="2" align="right">';
192 echo '<input type="submit" value="'.get_string('showrecent').'" />';
587510be 193 echo "</td></tr>";
194
195 echo "</table>";
196
839f2456 197 $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&amp;advancedfilter=0\">" . get_string("normalfilter") . "</a>";
587510be 198 print_heading($advancedlink);
199 echo "</center>";
200 echo "</form>";
201
202 } else {
203
204 $day_list = array("1","7","14","21","30");
205 $strsince = get_string("since");
206 $strlastlogin = get_string("lastlogin");
207 $strday = get_string("day");
208 $strdays = get_string("days");
209
210 $heading = "";
211 foreach ($day_list as $count) {
212 if ($count == "1") {
213 $day = $strday;
4581271a 214 } else {
587510be 215 $day = $strdays;
4581271a 216 }
3819ed31 217 $tmpdate = time() - ($count * 3600 * 24);
587510be 218 $heading = $heading .
839f2456 219 "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&amp;date=$tmpdate\"> $count $day</a> | ";
4581271a 220 }
4581271a 221
587510be 222 $heading = $strsince . ": <a href=\"$CFG->wwwroot/course/recent.php?id=$course->id\">$strlastlogin</a>" . " | " . $heading;
223 print_heading($heading);
4581271a 224
839f2456 225 $advancedlink = "<a href=\"$CFG->wwwroot/course/recent.php?id=$course->id&amp;advancedfilter=1\">" . get_string("advancedfilter") . "</a>";
587510be 226 print_heading($advancedlink);
227
228 }
4581271a 229
cb83c3cb 230}
9ae687af 231
f9903ed0 232
600149be 233function make_log_url($module, $url) {
234 switch ($module) {
bd7be234 235 case 'user':
236 case 'course':
237 case 'file':
238 case 'login':
239 case 'lib':
240 case 'admin':
241 case 'message':
242 case 'calendar':
243 case 'blog':
600149be 244 return "/$module/$url";
245 break;
bd7be234 246 case 'upload':
247 return $url;
c80b7585 248 break;
bd7be234 249 case 'library':
250 case '':
251 return '/';
de2dfe68 252 break;
600149be 253 default:
254 return "/mod/$module/$url";
255 break;
256 }
257}
258
92890025 259
260function build_logs_array($course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
261 $modname="", $modid=0, $modaction="", $groupid=0) {
f24cffb9 262
e0161bff 263 // It is assumed that $date is the GMT time of midnight for that day,
264 // and so the next 86400 seconds worth of logs are printed.
f9903ed0 265
69c76405 266 /// Setup for group handling.
264867fd 267
69c76405 268 /// If the group mode is separate, and this user does not have editing privileges,
269 /// then only the user's group can be viewed.
3924b988 270 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
69c76405 271 $groupid = get_current_group($course->id);
272 }
273 /// If this course doesn't have groups, no groupid can be specified.
274 else if (!$course->groupmode) {
275 $groupid = 0;
276 }
277
e0161bff 278 $joins = array();
a2ab3b05 279
e15ef260 280 if ($course->id != SITEID || $modid != 0) {
8f0cd6ef 281 $joins[] = "l.course='$course->id'";
e15ef260 282 }
f9903ed0 283
c469a7ef 284 if ($modname) {
e0161bff 285 $joins[] = "l.module = '$modname'";
f24cffb9 286 }
287
e21922f0 288 if ('site_errors' === $modid) {
bf35eb15 289 $joins[] = "( l.action='error' OR l.action='infected' )";
e21922f0 290 } else if ($modid) {
291 $joins[] = "l.cmid = '$modid'";
69d79bc3 292 }
293
294 if ($modaction) {
ee35e0b8 295 $firstletter = substr($modaction, 0, 1);
296 if (ctype_alpha($firstletter)) {
297 $joins[] = "lower(l.action) LIKE '%" . strtolower($modaction) . "%'";
298 } else if ($firstletter == '-') {
299 $joins[] = "lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'";
300 }
f24cffb9 301 }
302
69c76405 303 /// Getting all members of a group.
304 if ($groupid and !$user) {
305 if ($gusers = get_records('groups_members', 'groupid', $groupid)) {
306 $first = true;
307 foreach($gusers as $guser) {
308 if ($first) {
309 $gselect = '(l.userid='.$guser->userid;
310 $first = false;
311 }
312 else {
313 $gselect .= ' OR l.userid='.$guser->userid;
314 }
315 }
316 if (!$first) $gselect .= ')';
317 $joins[] = $gselect;
318 }
319 }
320 else if ($user) {
e0161bff 321 $joins[] = "l.userid = '$user'";
f9903ed0 322 }
323
324 if ($date) {
325 $enddate = $date + 86400;
e0161bff 326 $joins[] = "l.time > '$date' AND l.time < '$enddate'";
f9903ed0 327 }
328
2828ff51 329 $selector = '';
e0161bff 330 for ($i = 0; $i < count($joins); $i++) {
331 $selector .= $joins[$i] . (($i == count($joins)-1) ? " " : " AND ");
332 }
333
d09f3c80 334 $totalcount = 0; // Initialise
264867fd 335
92890025 336 $result = array();
337 $result['logs'] = get_logs($selector, $order, $limitfrom, $limitnum, $totalcount);
338 $result['totalcount'] = $totalcount;
339 return $result;
340}
264867fd 341
342
92890025 343function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
344 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
264867fd 345
92890025 346 global $CFG;
264867fd 347
92890025 348 if (!$logs = build_logs_array($course, $user, $date, $order, $page*$perpage, $perpage,
349 $modname, $modid, $modaction, $groupid)) {
f9903ed0 350 notify("No logs found!");
351 print_footer($course);
352 exit;
353 }
264867fd 354
ea49a66c 355 $courses = array();
356
92890025 357 if ($course->id == SITEID) {
358 $courses[0] = '';
ea49a66c 359 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
92890025 360 foreach ($ccc as $cc) {
361 $courses[$cc->id] = $cc->shortname;
362 }
363 }
ea49a66c 364 } else {
365 $courses[$course->id] = $course->shortname;
92890025 366 }
264867fd 367
92890025 368 $totalcount = $logs['totalcount'];
f9903ed0 369 $count=0;
2eb68e6f 370 $ldcache = array();
f9903ed0 371 $tt = getdate(time());
372 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
1c0200e0 373
dcde9f02 374 $strftimedatetime = get_string("strftimedatetime");
375
21283ddc 376 echo "<p align=\"center\">\n";
519d369f 377 print_string("displayingrecords", "", $totalcount);
21283ddc 378 echo "</p>\n";
1c0200e0 379
8f0cd6ef 380 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
519d369f 381
21283ddc 382 echo "<table class=\"logtable\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\">\n";
383 echo "<tr>";
1548978d 384 if ($course->id == SITEID) {
54926e78 385 echo "<th class=\"c0 header\" scope=\"col\">".get_string('course')."</th>\n";
1548978d 386 }
54926e78 387 echo "<th class=\"c1 header\" scope=\"col\">".get_string('time')."</th>\n";
388 echo "<th class=\"c2 header\" scope=\"col\">".get_string('ip_address')."</th>\n";
389 echo "<th class=\"c3 header\" scope=\"col\">".get_string('fullname')."</th>\n";
390 echo "<th class=\"c4 header\" scope=\"col\">".get_string('action')."</th>\n";
391 echo "<th class=\"c5 header\" scope=\"col\">".get_string('info')."</th>\n";
21283ddc 392 echo "</tr>\n";
1548978d 393
2b2d182a 394 if (empty($logs['logs'])) {
395 echo "</table>\n";
396 return;
397 }
398
1548978d 399 $row = 1;
92890025 400 foreach ($logs['logs'] as $log) {
600149be 401
1548978d 402 $row = ($row + 1) % 2;
403
2eb68e6f 404 if (isset($ldcache[$log->module][$log->action])) {
405 $ld = $ldcache[$log->module][$log->action];
406 } else {
1548978d 407 $ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
2eb68e6f 408 $ldcache[$log->module][$log->action] = $ld;
409 }
76feee3f 410 if ($ld && !empty($log->info)) {
181b888e 411 // ugly hack to make sure fullname is shown correctly
4068bedb 412 if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
181b888e 413 $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
414 } else {
415 $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
416 }
600149be 417 }
418
264867fd 419 //Filter log->info
c8b0a50b 420 $log->info = format_string($log->info);
421
d7d145b1 422 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
423 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
6ac98433 424 $log->url = str_replace('&', '&amp;', $log->url); /// XHTML compatibility
d7d145b1 425
1548978d 426 echo '<tr class="r'.$row.'">';
427 if ($course->id == SITEID) {
21283ddc 428 echo "<td class=\"r$row c0\" nowrap=\"nowrap\">\n";
81e10e95 429 echo " <a href=\"{$CFG->wwwroot}/course/view.php?id={$log->course}\">".$courses[$log->course]."</a>\n";
21283ddc 430 echo "</td>\n";
720a43ce 431 }
21283ddc 432 echo "<td class=\"r$row c1\" nowrap=\"nowrap\" align=\"right\">".userdate($log->time, '%a').
433 ' '.userdate($log->time, $strftimedatetime)."</td>\n";
434 echo "<td class=\"r$row c2\" nowrap=\"nowrap\">\n";
7eca967c 435 link_to_popup_window("/iplookup/index.php?ip=$log->ip&amp;user=$log->userid", 'iplookup',$log->ip, 400, 700);
21283ddc 436 echo "</td>\n";
1c45e42e 437 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
21283ddc 438 echo "<td class=\"r$row c3\" nowrap=\"nowrap\">\n";
d3a75287 439 echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}&amp;course={$log->course}\">$fullname</a>\n";
21283ddc 440 echo "</td>\n";
441 echo "<td class=\"r$row c4\" nowrap=\"nowrap\">\n";
2eb68e6f 442 link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 400, 600);
21283ddc 443 echo "</td>\n";;
444 echo "<td class=\"r$row c5\" nowrap=\"nowrap\">{$log->info}</td>\n";
445 echo "</tr>\n";
f9903ed0 446 }
21283ddc 447 echo "</table>\n";
519d369f 448
8f0cd6ef 449 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
f9903ed0 450}
451
452
92890025 453function print_log_csv($course, $user, $date, $order='l.time DESC', $modname,
454 $modid, $modaction, $groupid) {
4068bedb 455
954fdb42 456 $text = get_string('course')."\t".get_string('time')."\t".get_string('ip_address')."\t".
457 get_string('fullname')."\t".get_string('action')."\t".get_string('info');
264867fd 458
954fdb42 459 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
92890025 460 $modname, $modid, $modaction, $groupid)) {
461 return false;
462 }
264867fd 463
ea49a66c 464 $courses = array();
465
92890025 466 if ($course->id == SITEID) {
467 $courses[0] = '';
468 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
469 foreach ($ccc as $cc) {
470 $courses[$cc->id] = $cc->shortname;
471 }
472 }
ea49a66c 473 } else {
474 $courses[$course->id] = $course->shortname;
92890025 475 }
264867fd 476
92890025 477 $count=0;
478 $ldcache = array();
479 $tt = getdate(time());
480 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
481
482 $strftimedatetime = get_string("strftimedatetime");
92890025 483
954fdb42 484 $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
485 $filename .= '.txt';
264867fd 486 header("Content-Type: application/download\n");
954fdb42 487 header("Content-Disposition: attachment; filename=$filename");
488 header("Expires: 0");
489 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
490 header("Pragma: public");
491
492 echo get_string('savedat').userdate(time(), $strftimedatetime)."\n";
493 echo $text;
494
2b2d182a 495 if (empty($logs['logs'])) {
496 return true;
497 }
498
954fdb42 499 foreach ($logs['logs'] as $log) {
500 if (isset($ldcache[$log->module][$log->action])) {
501 $ld = $ldcache[$log->module][$log->action];
502 } else {
503 $ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
504 $ldcache[$log->module][$log->action] = $ld;
505 }
506 if ($ld && !empty($log->info)) {
507 // ugly hack to make sure fullname is shown correctly
4068bedb 508 if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
954fdb42 509 $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
510 } else {
511 $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
512 }
513 }
514
264867fd 515 //Filter log->info
954fdb42 516 $log->info = format_string($log->info);
517
518 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
519 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
520 $log->url = str_replace('&', '&amp;', $log->url); // XHTML compatibility
521
522 $firstField = $courses[$log->course];
1c45e42e 523 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
954fdb42 524 $row = array($firstField, userdate($log->time, $strftimedatetime), $log->ip, $fullname, $log->module.' '.$log->action, $log->info);
525 $text = implode("\t", $row);
526 echo $text." \n";
527 }
528 return true;
92890025 529}
530
531
532function print_log_xls($course, $user, $date, $order='l.time DESC', $modname,
533 $modid, $modaction, $groupid) {
264867fd 534
92890025 535 global $CFG;
536
954fdb42 537 require_once("$CFG->libdir/excellib.class.php");
264867fd 538
954fdb42 539 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
92890025 540 $modname, $modid, $modaction, $groupid)) {
541 return false;
542 }
264867fd 543
ea49a66c 544 $courses = array();
545
92890025 546 if ($course->id == SITEID) {
547 $courses[0] = '';
548 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
549 foreach ($ccc as $cc) {
550 $courses[$cc->id] = $cc->shortname;
551 }
552 }
ea49a66c 553 } else {
554 $courses[$course->id] = $course->shortname;
92890025 555 }
264867fd 556
92890025 557 $count=0;
558 $ldcache = array();
559 $tt = getdate(time());
560 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
561
562 $strftimedatetime = get_string("strftimedatetime");
92890025 563
954fdb42 564 $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
565 $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
566 $filename .= '.xls';
264867fd 567
92890025 568 $workbook = new MoodleExcelWorkbook('-');
569 $workbook->send($filename);
264867fd 570
954fdb42 571 $worksheet = array();
572 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
573 get_string('fullname'), get_string('action'), get_string('info'));
264867fd 574
954fdb42 575 // Creating worksheets
576 for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
577 $sheettitle = get_string('excel_sheettitle', 'logs', $wsnumber).$nroPages;
578 $worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle);
579 $worksheet[$wsnumber]->set_column(1, 1, 30);
580 $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
581 userdate(time(), $strftimedatetime));
582 $col = 0;
583 foreach ($headers as $item) {
584 $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
585 $col++;
586 }
587 }
588
2b2d182a 589 if (empty($logs['logs'])) {
590 $workbook->close();
591 return true;
592 }
593
954fdb42 594 $formatDate =& $workbook->add_format();
595 $formatDate->set_num_format(get_string('log_excel_date_format'));
596
597 $row = FIRSTUSEDEXCELROW;
598 $wsnumber = 1;
599 $myxls =& $worksheet[$wsnumber];
600 foreach ($logs['logs'] as $log) {
601 if (isset($ldcache[$log->module][$log->action])) {
602 $ld = $ldcache[$log->module][$log->action];
603 } else {
604 $ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
605 $ldcache[$log->module][$log->action] = $ld;
606 }
607 if ($ld && !empty($log->info)) {
608 // ugly hack to make sure fullname is shown correctly
4068bedb 609 if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
954fdb42 610 $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
611 } else {
612 $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
613 }
614 }
615
616 // Filter log->info
617 $log->info = format_string($log->info);
618 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
619
620 if ($nroPages>1) {
621 if ($row > EXCELROWS) {
622 $wsnumber++;
623 $myxls =& $worksheet[$wsnumber];
624 $row = FIRSTUSEDEXCELROW;
625 }
626 }
264867fd 627
954fdb42 628 $myxls->write($row, 0, $courses[$log->course], '');
629 // Excel counts from 1/1/1900
630 $excelTime=25569+$log->time/(3600*24);
631 $myxls->write($row, 1, $excelTime, $formatDate);
632 $myxls->write($row, 2, $log->ip, '');
1c45e42e 633 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
954fdb42 634 $myxls->write($row, 3, $fullname, '');
635 $myxls->write($row, 4, $log->module.' '.$log->action, '');
636 $myxls->write($row, 5, $log->info, '');
264867fd 637
954fdb42 638 $row++;
639 }
640
641 $workbook->close();
ea49a66c 642 return true;
643}
644
645function print_log_ods($course, $user, $date, $order='l.time DESC', $modname,
646 $modid, $modaction, $groupid) {
647
648 global $CFG;
649
650 require_once("$CFG->libdir/odslib.class.php");
651
652 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
653 $modname, $modid, $modaction, $groupid)) {
654 return false;
655 }
656
657 $courses = array();
658
659 if ($course->id == SITEID) {
660 $courses[0] = '';
661 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
662 foreach ($ccc as $cc) {
663 $courses[$cc->id] = $cc->shortname;
664 }
665 }
666 } else {
667 $courses[$course->id] = $course->shortname;
668 }
669
670 $count=0;
671 $ldcache = array();
672 $tt = getdate(time());
673 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
674
675 $strftimedatetime = get_string("strftimedatetime");
676
677 $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
678 $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
679 $filename .= '.ods';
680
681 $workbook = new MoodleODSWorkbook('-');
682 $workbook->send($filename);
683
684 $worksheet = array();
685 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
686 get_string('fullname'), get_string('action'), get_string('info'));
687
688 // Creating worksheets
689 for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
690 $sheettitle = get_string('excel_sheettitle', 'logs', $wsnumber).$nroPages;
691 $worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle);
692 $worksheet[$wsnumber]->set_column(1, 1, 30);
693 $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
694 userdate(time(), $strftimedatetime));
695 $col = 0;
696 foreach ($headers as $item) {
697 $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
698 $col++;
699 }
700 }
701
702 if (empty($logs['logs'])) {
703 $workbook->close();
704 return true;
705 }
706
707 $formatDate =& $workbook->add_format();
708 $formatDate->set_num_format(get_string('log_excel_date_format'));
709
710 $row = FIRSTUSEDEXCELROW;
711 $wsnumber = 1;
712 $myxls =& $worksheet[$wsnumber];
713 foreach ($logs['logs'] as $log) {
714 if (isset($ldcache[$log->module][$log->action])) {
715 $ld = $ldcache[$log->module][$log->action];
716 } else {
717 $ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
718 $ldcache[$log->module][$log->action] = $ld;
719 }
720 if ($ld && !empty($log->info)) {
721 // ugly hack to make sure fullname is shown correctly
722 if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
723 $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
724 } else {
725 $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
726 }
727 }
728
729 // Filter log->info
730 $log->info = format_string($log->info);
731 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
732
733 if ($nroPages>1) {
734 if ($row > EXCELROWS) {
735 $wsnumber++;
736 $myxls =& $worksheet[$wsnumber];
737 $row = FIRSTUSEDEXCELROW;
738 }
739 }
740
d81b7ffb 741 $myxls->write_string($row, 0, $courses[$log->course]);
742 $myxls->write_date($row, 1, $log->time);
743 $myxls->write_string($row, 2, $log->ip);
ea49a66c 744 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
d81b7ffb 745 $myxls->write_string($row, 3, $fullname);
746 $myxls->write_string($row, 4, $log->module.' '.$log->action);
747 $myxls->write_string($row, 5, $log->info);
ea49a66c 748
749 $row++;
750 }
751
752 $workbook->close();
954fdb42 753 return true;
92890025 754}
755
756/*
757// Relies on $CFG->libdir.'/phpdocwriter/lib/include.php', which is not
758// included in the default Moodle distribution.
759
760function print_log_ooo($course, $user, $date, $order='l.time DESC', $modname,
0adf53f6 761 $modid, $modaction, $groupid) {
264867fd 762
0adf53f6 763 global $CFG;
954fdb42 764 require_once($CFG->libdir.'/phpdocwriter/lib/include.php');
264867fd 765
92890025 766 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
767 $modname, $modid, $modaction, $groupid)) {
768 return false;
769 }
264867fd 770
92890025 771 if ($course->id == SITEID) {
772 $courses[0] = '';
773 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
774 foreach ($ccc as $cc) {
775 $courses[$cc->id] = $cc->shortname;
776 }
777 }
778 }
264867fd 779
92890025 780 $count=0;
781 $ldcache = array();
782 $tt = getdate(time());
783 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
784
785 $strftimedatetime = get_string("strftimedatetime");
92890025 786
954fdb42 787 $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
788 $filename .= '.sxw';
789 import('phpdocwriter.pdw_document');
790 header("Content-Type: application/download\n");
791 header("Content-Disposition: attachment; filename=$filename");
792 header("Expires: 0");
793 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
794 header("Pragma: public");
795 header("Content-Transfer-Encoding: binary");
796
797 $sxw = new pdw_document;
798 $sxw->SetFileName($filename);
799 $sxw->SetAuthor('Moodle');
800 $sxw->SetTitle('logs');
801 $sxw->SetDescription('logs'.' - '.$filename);
802 $sxw->SetLanguage('es','ES');
803 $sxw->SetStdFont("Times New Roman",12);
804 $sxw->AddPageDef(array('name'=>'Standard', 'margins'=>'1,1,1,1', 'w'=>'29.7', 'h'=>'21'));
805 $sxw->Write(get_string('savedat').userdate(time(), $strftimedatetime));
92890025 806 $sxw->Ln(3);
264867fd 807
92890025 808 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
954fdb42 809 get_string('fullname'), get_string('action'), get_string('info'));
264867fd 810
954fdb42 811 foreach($headers as $key=>$header){
812 $headers[$key] = eregi_replace ("<br?>", " ",$header);
813 }
814
815 foreach ($logs['logs'] as $log) {
816 if (isset($ldcache[$log->module][$log->action])) {
817 $ld = $ldcache[$log->module][$log->action];
818 } else {
819 $ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
820 $ldcache[$log->module][$log->action] = $ld;
821 }
822 if ($ld && !empty($log->info)) {
823 // ugly hack to make sure fullname is shown correctly
4068bedb 824 if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
954fdb42 825 $log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
826 } else {
827 $log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
828 }
829 }
830
264867fd 831 // Filter log->info
954fdb42 832 $log->info = format_string($log->info);
833
834 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
835 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
836 $log->url = str_replace('&', '&amp;', $log->url); // XHTML compatibility
837
838 $firstField = $courses[$log->course];
1c45e42e 839 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
954fdb42 840 $row = array($firstField, userdate($log->time, $strftimedatetime), $log->ip, $fullname, $log->module.' '.$log->action, $log->info);
841
842 $data[] = $row;
843 }
844 $sxw->Table($headers,$data);
845 $sxw->Output();
264867fd 846
954fdb42 847 return true;
92890025 848}
849*/
850
c2cb4545 851function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
852 global $CFG;
853 if (empty($CFG->gdversion)) {
854 echo "(".get_string("gdneed").")";
d887b5a7 855 } else {
980a5b3a 856 echo '<img src="'.$CFG->wwwroot.'/course/report/log/graph.php?id='.$course->id.
29b59206 857 '&amp;user='.$userid.'&amp;type='.$type.'&amp;date='.$date.'" alt="" />';
d887b5a7 858 }
859}
860
861
185cfb09 862function print_overview($courses) {
0d6b9d4f 863
864 global $CFG, $USER;
865
185cfb09 866 $htmlarray = array();
f8716988 867 if ($modules = get_records('modules')) {
868 foreach ($modules as $mod) {
869 if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) {
870 require_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php');
871 $fname = $mod->name.'_print_overview';
0d6b9d4f 872 if (function_exists($fname)) {
185cfb09 873 $fname($courses,$htmlarray);
0d6b9d4f 874 }
875 }
876 }
877 }
185cfb09 878 foreach ($courses as $course) {
fe5a1e23 879 print_simple_box_start('center', '100%', '', 5, "coursebox");
185cfb09 880 $linkcss = '';
881 if (empty($course->visible)) {
882 $linkcss = 'class="dimmed"';
883 }
884 print_heading('<a title="'.$course->fullname.'" '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>');
885 if (array_key_exists($course->id,$htmlarray)) {
886 foreach ($htmlarray[$course->id] as $modname => $html) {
887 echo $html;
888 }
889 }
890 print_simple_box_end();
891 }
0d6b9d4f 892}
893
894
600149be 895function print_recent_activity($course) {
896 // $course is an object
89adb174 897 // This function trawls through the logs looking for
600149be 898 // anything new since the user's last login
899
810393c8 900 global $CFG, $USER, $SESSION;
600149be 901
e2a3a0e7 902 $context = get_context_instance(CONTEXT_COURSE, $course->id);
2ac64806 903
6f80940b 904 $timestart = time() - COURSE_MAX_RECENT_PERIOD;
0f87cb1d 905
e2a3a0e7 906 if (!has_capability('moodle/legacy:guest', $context, NULL, false)) {
907 if (!empty($USER->lastcourseaccess[$course->id])) {
908 if ($USER->lastcourseaccess[$course->id] > $timestart) {
909 $timestart = $USER->lastcourseaccess[$course->id];
910 }
9e51847a 911 }
3d891989 912 }
0f87cb1d 913
de785682 914 echo '<div class="activitydate">';
27bf9e20 915 echo get_string('activitysince', '', userdate($timestart));
de785682 916 echo '</div>';
917 echo '<div class="activityhead">';
0f87cb1d 918
de785682 919 echo '<a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a>';
0f87cb1d 920
5fc835a5 921 echo "</div>\n";
0f87cb1d 922
600149be 923
924 // Firstly, have there been any new enrolments?
925
926 $heading = false;
927 $content = false;
1b5910c4 928
6c38b7e0 929 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 930
5fc835a5 931 //Accessibility: new users now appear in an <OL> list.
6c38b7e0 932 if ($users) {
27bf9e20 933 echo '<div class="newusers">';
5fc835a5 934 if (! $heading) {
264867fd 935 print_headline(get_string("newusers").':', 3);
5fc835a5 936 $heading = true;
937 $content = true;
938 }
939 echo "<ol class=\"list\">\n";
6c38b7e0 940 foreach ($users as $user) {
264867fd 941
1c45e42e 942 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
5fc835a5 943 echo '<li class="name"><a href="'.$CFG->wwwroot."/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></li>\n";
600149be 944 }
5fc835a5 945 echo "</ol>\n</div>\n";
600149be 946 }
947
1b5910c4 948 // Next, have there been any modifications to the course structure?
949
27bf9e20 950 $logs = get_records_select('log', "time > '$timestart' AND course = '$course->id' AND
1b5910c4 951 module = 'course' AND action LIKE '% mod'", "time ASC");
952
953 if ($logs) {
954 foreach ($logs as $key => $log) {
27bf9e20 955 $info = split(' ', $log->info);
c9f6251e 956
27bf9e20 957 if ($info[0] == 'label') { // Labels are special activities
c9f6251e 958 continue;
959 }
960
27bf9e20 961 $modname = get_field($info[0], 'name', 'id', $info[1]);
1b5910c4 962 //Create a temp valid module structure (course,id)
963 $tempmod->course = $log->course;
964 $tempmod->id = $info[1];
965 //Obtain the visible property from the instance
966 $modvisible = instance_is_visible($info[0],$tempmod);
89adb174 967
1b5910c4 968 //Only if the mod is visible
969 if ($modvisible) {
970 switch ($log->action) {
27bf9e20 971 case 'add mod':
972 $stradded = get_string('added', 'moodle', get_string('modulename', $info[0]));
5847b267 973 $changelist[$log->info] = array ('operation' => 'add', 'text' => "$stradded:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>");
1b5910c4 974 break;
27bf9e20 975 case 'update mod':
976 $strupdated = get_string('updated', 'moodle', get_string('modulename', $info[0]));
977 if (empty($changelist[$log->info])) {
5847b267 978 $changelist[$log->info] = array ('operation' => 'update', 'text' => "$strupdated:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>");
1b5910c4 979 }
980 break;
27bf9e20 981 case 'delete mod':
982 if (!empty($changelist[$log->info]['operation']) and
983 $changelist[$log->info]['operation'] == 'add') {
984 $changelist[$log->info] = NULL;
1b5910c4 985 } else {
27bf9e20 986 $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $info[0]));
987 $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted);
1b5910c4 988 }
989 break;
600149be 990 }
ef25340c 991 }
992 }
993 }
994
9c9f7d77 995 if (!empty($changelist)) {
ef25340c 996 foreach ($changelist as $changeinfo => $change) {
997 if ($change) {
998 $changes[$changeinfo] = $change;
999 }
1000 }
8a59942e 1001 if (isset($changes)){
1002 if (count($changes) > 0) {
27bf9e20 1003 print_headline(get_string('courseupdates').':', 3);
8a59942e 1004 $content = true;
1005 foreach ($changes as $changeinfo => $change) {
27bf9e20 1006 echo '<p class="activity">'.$change['text'].'</p>';
8a59942e 1007 }
600149be 1008 }
1009 }
89adb174 1010 }
bf40f9c1 1011
3869a2ac 1012 // Now display new things from each module
600149be 1013
27bf9e20 1014 $mods = get_records('modules', 'visible', '1', 'name', 'id, name');
0fd7da81 1015
e2a3a0e7 1016 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
1017
1b5910c4 1018 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
27bf9e20 1019 include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php');
1020 $print_recent_activity = $mod->name.'_print_recent_activity';
1b5910c4 1021 if (function_exists($print_recent_activity)) {
66b0b104 1022 //
1023 // NOTE:
e2a3a0e7 1024 // $isteacher (second parameter below) is to be deprecated!
66b0b104 1025 //
1026 // TODO:
1172e5db 1027 // 1) Make sure that all _print_recent_activity functions are
1028 // not using the $isteacher value.
1029 // 2) Eventually, remove the $isteacher parameter from the
1030 // function calls.
66b0b104 1031 //
e2a3a0e7 1032 $modcontent = $print_recent_activity($course, $viewfullnames, $timestart);
3869a2ac 1033 if ($modcontent) {
1034 $content = true;
600149be 1035 }
600149be 1036 }
1037 }
1038
1039 if (! $content) {
27bf9e20 1040 echo '<p class="message">'.get_string('nothingnew').'</p>';
600149be 1041 }
600149be 1042}
1043
e1360728 1044
d897cae4 1045function get_array_of_activities($courseid) {
89adb174 1046// For a given course, returns an array of course activity objects
d897cae4 1047// Each item in the array contains he following properties:
1048// cm - course module id
1049// mod - name of the module (eg forum)
1050// section - the number of the section (eg week or topic)
1051// name - the name of the instance
5867bfb5 1052// visible - is the instance visible or not
86aa7ccf 1053// extra - contains extra string to include in any link
d897cae4 1054
8dddba42 1055 global $CFG;
1056
d897cae4 1057 $mod = array();
1058
9fa49e22 1059 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 1060 return NULL;
1061 }
1062
1063 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
1064 foreach ($sections as $section) {
74666583 1065 if (!empty($section->sequence)) {
d897cae4 1066 $sequence = explode(",", $section->sequence);
1067 foreach ($sequence as $seq) {
7af6281f 1068 if (empty($rawmods[$seq])) {
1069 continue;
1070 }
d897cae4 1071 $mod[$seq]->cm = $rawmods[$seq]->id;
1072 $mod[$seq]->mod = $rawmods[$seq]->modname;
1073 $mod[$seq]->section = $section->section;
1074 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 1075 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 1076 $mod[$seq]->extra = "";
8dddba42 1077
1078 $modname = $mod[$seq]->mod;
1079 $functionname = $modname."_get_coursemodule_info";
1080
1081 include_once("$CFG->dirroot/mod/$modname/lib.php");
1082
1083 if (function_exists($functionname)) {
9d361034 1084 if ($info = $functionname($rawmods[$seq])) {
1085 if (!empty($info->extra)) {
1086 $mod[$seq]->extra = $info->extra;
1087 }
1088 if (!empty($info->icon)) {
1089 $mod[$seq]->icon = $info->icon;
1090 }
c9f6251e 1091 }
1092 }
d897cae4 1093 }
1094 }
1095 }
1096 }
1097 return $mod;
1098}
1099
1100
1101
e1360728 1102
90845098 1103function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
1104// Returns a number of useful structures for course displays
7468bf01 1105
90845098 1106 $mods = NULL; // course modules indexed by id
e0161bff 1107 $modnames = NULL; // all course module names (except resource!)
94361e02 1108 $modnamesplural= NULL; // all course module names (plural form)
90845098 1109 $modnamesused = NULL; // course module names used
7468bf01 1110
9fa49e22 1111 if ($allmods = get_records("modules")) {
90845098 1112 foreach ($allmods as $mod) {
5867bfb5 1113 if ($mod->visible) {
1114 $modnames[$mod->name] = get_string("modulename", "$mod->name");
1115 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
1116 }
90845098 1117 }
1118 asort($modnames);
1119 } else {
1120 error("No modules are installed!");
1121 }
1122
9fa49e22 1123 if ($rawmods = get_course_mods($courseid)) {
7468bf01 1124 foreach($rawmods as $mod) { // Index the mods
959ae824 1125 if (empty($modnames[$mod->modname])) {
1126 continue;
1127 }
7468bf01 1128 $mods[$mod->id] = $mod;
959ae824 1129 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
3924b988 1130 if ($mod->visible or has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) {
959ae824 1131 $modnamesused[$mod->modname] = $modnames[$mod->modname];
1acfbce5 1132 }
7468bf01 1133 }
c7da6f7a 1134 if ($modnamesused) {
1135 asort($modnamesused);
1136 }
7468bf01 1137 }
e0161bff 1138
1139 unset($modnames['resource']);
1140 unset($modnames['label']);
7468bf01 1141}
1142
9fa49e22 1143
7468bf01 1144function get_all_sections($courseid) {
89adb174 1145
1146 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 1147 "section, id, course, summary, sequence, visible");
7468bf01 1148}
1149
b86fc0e2 1150function course_set_display($courseid, $display=0) {
1151 global $USER;
1152
b86fc0e2 1153 if ($display == "all" or empty($display)) {
1154 $display = 0;
1155 }
1156
7b678e0a 1157 if (empty($USER->id) or $USER->username == 'guest') {
1158 //do not store settings in db for guests
1159 } else if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
b86fc0e2 1160 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
1161 } else {
1162 $record->userid = $USER->id;
1163 $record->course = $courseid;
1164 $record->display = $display;
1165 if (!insert_record("course_display", $record)) {
1166 notify("Could not save your course display!");
1167 }
1168 }
1169
1170 return $USER->display[$courseid] = $display; // Note: = not ==
1171}
1172
7d99d695 1173function set_section_visible($courseid, $sectionnumber, $visibility) {
1174/// For a given course section, markes it visible or hidden,
1175/// and does the same for every activity in that section
1176
1177 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
1178 set_field("course_sections", "visible", "$visibility", "id", $section->id);
1179 if (!empty($section->sequence)) {
1180 $modules = explode(",", $section->sequence);
1181 foreach ($modules as $moduleid) {
02f66c42 1182 set_coursemodule_visible($moduleid, $visibility, true);
7d99d695 1183 }
1184 }
5867bfb5 1185 rebuild_course_cache($courseid);
7d99d695 1186 }
1187}
ba2e5d73 1188
5e367a2d 1189
d897cae4 1190function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 1191/// Prints a section full of activity modules
7977cffd 1192 global $CFG, $USER;
1193
3d575e6f 1194 static $groupbuttons;
32d03b7b 1195 static $groupbuttonslink;
52dcc2f9 1196 static $isteacher;
1197 static $isediting;
7977cffd 1198 static $ismoving;
1199 static $strmovehere;
1200 static $strmovefull;
54669989 1201 static $strunreadpostsone;
52dcc2f9 1202
4877707e 1203 static $untracked;
a2d71d8e 1204 static $usetracking;
4877707e 1205
a22f8313 1206 $labelformatoptions = New stdClass;
110a32e2 1207
52dcc2f9 1208 if (!isset($isteacher)) {
9fd9c29b 1209 $groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
32d03b7b 1210 $groupbuttonslink = (!$course->groupmodeforce);
52dcc2f9 1211 $isediting = isediting($course->id);
ff0c7de0 1212 $ismoving = $isediting && ismoving($course->id);
3d575e6f 1213 if ($ismoving) {
1214 $strmovehere = get_string("movehere");
1215 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
1216 }
94a6a70f 1217 include_once($CFG->dirroot.'/mod/forum/lib.php');
1218 if ($usetracking = forum_tp_can_track_forums()) {
a2d71d8e 1219 $strunreadpostsone = get_string('unreadpostsone', 'forum');
94a6a70f 1220 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
a2d71d8e 1221 }
7977cffd 1222 }
60bd11cf 1223 $labelformatoptions->noclean = true;
94361e02 1224
fea43a7f 1225/// Casting $course->modinfo to string prevents one notice when the field is null
1226 $modinfo = unserialize((string)$course->modinfo);
94361e02 1227
c6a55371 1228 //Acccessibility: replace table with list <ul>, but don't output empty list.
74666583 1229 if (!empty($section->sequence)) {
94361e02 1230
f2d660dc 1231 // Fix bug #5027, don't want style=\"width:$width\".
1232 echo "<ul class=\"section\">\n";
94361e02 1233 $sectionmods = explode(",", $section->sequence);
1234
1235 foreach ($sectionmods as $modnumber) {
9ae687af 1236 if (empty($mods[$modnumber])) {
1237 continue;
1238 }
94361e02 1239 $mod = $mods[$modnumber];
c9f6251e 1240
3924b988 1241 if ($mod->visible or has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $course->id))) {
954fdb42 1242 echo '<li class="activity '.$mod->modname.'" id="module-'.$modnumber.'">'; // Unique ID
7977cffd 1243 if ($ismoving) {
1244 if ($mod->id == $USER->activitycopy) {
1245 continue;
1246 }
1c919752 1247 echo '<a title="'.$strmovefull.'"'.
8b92f5bb 1248 ' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 1249 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
1250 ' alt="'.$strmovehere.'" /></a><br />
1c919752 1251 ';
1acfbce5 1252 }
7977cffd 1253 $instancename = urldecode($modinfo[$modnumber]->name);
954fdb42 1254 $instancename = format_string($instancename, true, $course->id);
c9f6251e 1255
86aa7ccf 1256 if (!empty($modinfo[$modnumber]->extra)) {
1257 $extra = urldecode($modinfo[$modnumber]->extra);
1258 } else {
1259 $extra = "";
1260 }
c9f6251e 1261
9d361034 1262 if (!empty($modinfo[$modnumber]->icon)) {
1263 $icon = "$CFG->pixpath/".urldecode($modinfo[$modnumber]->icon);
1264 } else {
1265 $icon = "$CFG->modpixpath/$mod->modname/icon.gif";
1266 }
1267
aac94fd0 1268 if ($mod->indent) {
1269 print_spacer(12, 20 * $mod->indent, false);
1270 }
1271
c9f6251e 1272 if ($mod->modname == "label") {
aac94fd0 1273 if (!$mod->visible) {
1274 echo "<span class=\"dimmed_text\">";
1275 }
179c9a50 1276 echo format_text($extra, FORMAT_HTML, $labelformatoptions);
aac94fd0 1277 if (!$mod->visible) {
1278 echo "</span>";
1279 }
c9f6251e 1280
1281 } else { // Normal activity
78e4f30a 1282
1283 if ($USER->screenreader) {
1284 $typestring = '('.get_string('modulename',$mod->modname).') ';
1285 } else {
1286 $typestring = '';
1287 }
1288
c9f6251e 1289 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
1c919752 1290 echo '<img src="'.$icon.'"'.
446390fb 1291 ' class="activityicon" alt="'.$mod->modfullname.'" />'.
1292 ' <a title="'.$mod->modfullname.'" '.$linkcss.' '.$extra.
1c919752 1293 ' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.
78e4f30a 1294 $typestring.$instancename.'</a>';
c9f6251e 1295 }
a2d71d8e 1296 if ($usetracking && $mod->modname == 'forum') {
f37da850 1297 $groupmode = groupmode($course, $mod);
3924b988 1298 $groupid = ($groupmode == SEPARATEGROUPS && !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) ?
a2d71d8e 1299 get_current_group($course->id) : false;
4877707e 1300
94a6a70f 1301 if (forum_tp_can_track_forums() && !isset($untracked[$mod->instance])) {
4877707e 1302 $unread = forum_tp_count_forum_unread_posts($USER->id, $mod->instance, $groupid);
1303 if ($unread) {
1304 echo '<span class="unread"> <a href="'.$CFG->wwwroot.'/mod/forum/view.php?id='.$mod->id.'">';
1305 if ($unread == 1) {
1306 echo $strunreadpostsone;
1307 } else {
1308 print_string('unreadpostsnumber', 'forum', $unread);
1309 }
1310 echo '</a> </span>';
d0388ebe 1311 }
54669989 1312 }
f37da850 1313 }
1314
c9f6251e 1315 if ($isediting) {
5f390342 1316 if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource') {
32d03b7b 1317 if (! $mod->groupmodelink = $groupbuttonslink) {
1318 $mod->groupmode = $course->groupmode;
1319 }
1320
1321 } else {
3d575e6f 1322 $mod->groupmode = false;
1323 }
37a88449 1324 echo '&nbsp;&nbsp;';
24e1eae4 1325 echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section);
c9f6251e 1326 }
c6a55371 1327 echo "</li>\n";
94361e02 1328 }
94361e02 1329 }
f2d660dc 1330 } elseif ($ismoving) {
1331 echo "<ul class=\"section\">\n";
264867fd 1332 }
7977cffd 1333 if ($ismoving) {
64fdc686 1334 echo '<li><a title="'.$strmovefull.'"'.
8b92f5bb 1335 ' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 1336 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
c6a55371 1337 ' alt="'.$strmovehere.'" /></a></li>
1c919752 1338 ';
7977cffd 1339 }
c6a55371 1340 if (!empty($section->sequence) || $ismoving) {
1341 echo "</ul><!--class='section'-->\n\n";
1342 }
a7ad3ea6 1343}
1344
5867bfb5 1345
cb57e6f4 1346function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
e0161bff 1347// Prints the menus to add activities and resources
1348
8b92f5bb 1349 global $CFG, $USER;
6da4b261 1350 static $straddactivity, $stractivities, $straddresource, $resources;
e0161bff 1351
1352 if (!isset($straddactivity)) {
1353 $straddactivity = get_string('addactivity');
1354 $straddresource = get_string('addresource');
6da4b261 1355
1356 /// Standard resource types
1357 require_once("$CFG->dirroot/mod/resource/lib.php");
1358 $resourceraw = resource_get_resource_types();
1359
1360 foreach ($resourceraw as $type => $name) {
839f2456 1361 $resources["resource&amp;type=$type"] = $name;
e0161bff 1362 }
0705ff84 1363 if (course_allowed_module($course,'label')) {
1364 $resources['label'] = get_string('resourcetypelabel', 'resource');
1365 }
e0161bff 1366 }
1367
d33d0cda 1368 $output = '<div style="text-align: right">';
0705ff84 1369 if (course_allowed_module($course,'resource')) {
81a5d9ca 1370 $resourceallowed = true;
0705ff84 1371 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
1372 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
1373 }
cb57e6f4 1374
1375 if ($vertical) {
d33d0cda 1376 $output .= '<div>';
cb57e6f4 1377 }
1378
0705ff84 1379 // we need to loop through the forms and check to see if we can add them.
cb77cf12 1380 foreach ($modnames as $key=>$value) {
0705ff84 1381 if (!course_allowed_module($course,$key))
cb77cf12 1382 unset($modnames[$key]);
0705ff84 1383 }
264867fd 1384
0705ff84 1385 // this is stupid but labels get put into resource, so if resource is hidden and label is not, we're in trouble.
1386 if (course_allowed_module($course,'label') && empty($resourceallowed)) {
1387 $modnames['label'] = get_string('modulename', 'label');
1388 }
1389
1c46eb03 1390 $output .= ' ';
8b92f5bb 1391 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 1392 $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
d33d0cda 1393
1394 if ($vertical) {
1395 $output .= '</div>';
1396 }
1397
cb57e6f4 1398 $output .= '</div>';
1399
1400 if ($return) {
1401 return $output;
1402 } else {
1403 echo $output;
1404 }
e0161bff 1405}
1406
5867bfb5 1407function rebuild_course_cache($courseid=0) {
1408// Rebuilds the cached list of course activities stored in the database
1409// If a courseid is not specified, then all are rebuilt
1410
1411 if ($courseid) {
1412 $select = "id = '$courseid'";
1413 } else {
1414 $select = "";
1415 }
1416
1a31c2b3 1417 if ($courses = get_records_select("course", $select,'','id,fullname')) {
5867bfb5 1418 foreach ($courses as $course) {
1419 $modinfo = serialize(get_array_of_activities($course->id));
1420 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
1421 notify("Could not cache module information for course '$course->fullname'!");
1422 }
1423 }
1424 }
1425}
1426
1427
c2cb4545 1428
1429function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 1430/// Given an empty array, this function recursively travels the
c2cb4545 1431/// categories, building up a nice list for display. It also makes
1432/// an array that list all the parents for each category.
1433
9d866ae0 1434 // initialize the arrays if needed
1435 if (!is_array($list)) {
264867fd 1436 $list = array();
9d866ae0 1437 }
1438 if (!is_array($parents)) {
264867fd 1439 $parents = array();
9d866ae0 1440 }
1441
c2cb4545 1442 if ($category) {
1443 if ($path) {
e05bcf2f 1444 $path = $path.' / '.$category->name;
c2cb4545 1445 } else {
e05bcf2f 1446 $path = $category->name;
c2cb4545 1447 }
1448 $list[$category->id] = $path;
1449 } else {
1450 $category->id = 0;
1451 }
1452
e05bcf2f 1453 if ($categories = get_categories($category->id)) { // Print all the children recursively
c2cb4545 1454 foreach ($categories as $cat) {
1455 if (!empty($category->id)) {
3bd4de22 1456 if (isset($parents[$category->id])) {
2832badf 1457 $parents[$cat->id] = $parents[$category->id];
1458 }
c2cb4545 1459 $parents[$cat->id][] = $category->id;
1460 }
89adb174 1461 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1462 }
1463 }
1464}
1465
1466
d157bd5b 1467function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $files = true) {
89adb174 1468/// Recursive function to print out all the categories in a nice format
c2cb4545 1469/// with or without courses included
9ff5310a 1470 global $CFG;
e05bcf2f 1471
1472 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1473 return;
9ff5310a 1474 }
c2cb4545 1475
1476 if (!$displaylist) {
e92fe848 1477 make_categories_list($displaylist, $parentslist);
c2cb4545 1478 }
1479
1480 if ($category) {
ec7a8b79 1481 if ($category->visible or has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
6f24e48e 1482 print_category_info($category, $depth, $files);
c2cb4545 1483 } else {
1484 return; // Don't bother printing children of invisible categories
1485 }
89adb174 1486
c2cb4545 1487 } else {
c2cb4545 1488 $category->id = "0";
1489 }
1490
1491 if ($categories = get_categories($category->id)) { // Print all the children recursively
1492 $countcats = count($categories);
1493 $count = 0;
1494 $first = true;
1495 $last = false;
1496 foreach ($categories as $cat) {
1497 $count++;
1498 if ($count == $countcats) {
1499 $last = true;
1500 }
1501 $up = $first ? false : true;
1502 $down = $last ? false : true;
1503 $first = false;
1504
6f24e48e 1505 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1, $files);
c2cb4545 1506 }
1507 }
c2cb4545 1508}
1509
0705ff84 1510// this function will return $options array for choose_from_menu, with whitespace to denote nesting.
1511
1512function make_categories_options() {
1513 make_categories_list($cats,$parents);
1514 foreach ($cats as $key => $value) {
1515 if (array_key_exists($key,$parents)) {
1516 if ($indent = count($parents[$key])) {
1517 for ($i = 0; $i < $indent; $i++) {
1518 $cats[$key] = '&nbsp;'.$cats[$key];
1519 }
1520 }
1521 }
1522 }
1523 return $cats;
1524}
c2cb4545 1525
6f24e48e 1526function print_category_info($category, $depth, $files = false) {
d2b6ba70 1527/// Prints the category info in indented fashion
1528/// This function is only used by print_whole_category_list() above
c2cb4545 1529
1530 global $CFG;
b48f834c 1531 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1532
b48f834c 1533 if (empty($strsummary)) {
e05bcf2f 1534 $strallowguests = get_string('allowguests');
1535 $strrequireskey = get_string('requireskey');
1536 $strsummary = get_string('summary');
b48f834c 1537 }
ba2e5d73 1538
e05bcf2f 1539 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1540
6f24e48e 1541 $coursecount = count_records('course') <= FRONTPAGECOURSELIMIT;
1542 if ($files and $coursecount) {
839f2456 1543 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" width="16" height="16" border="0" alt="" />';
b48f834c 1544 } else {
7b0b5c14 1545 $catimage = "&nbsp;";
8ef9cb56 1546 }
b48f834c 1547
87d32352 1548 echo "\n\n".'<table border="0" cellpadding="3" cellspacing="0" width="100%">';
d2b6ba70 1549
6f24e48e 1550 if ($files and $coursecount) {
19374e76 1551 $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.guest,c.cost,c.currency');
b48f834c 1552
978abb42 1553 echo '<tr>';
b48f834c 1554
1555 if ($depth) {
1556 $indent = $depth*30;
1557 $rows = count($courses) + 1;
1c919752 1558 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1559 print_spacer(10, $indent);
e05bcf2f 1560 echo '</td>';
b48f834c 1561 }
89adb174 1562
e05bcf2f 1563 echo '<td valign="top">'.$catimage.'</td>';
290130b3 1564 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1565 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1566 echo '</td>';
290130b3 1567 echo '<td class="category info">&nbsp;</td>';
e05bcf2f 1568 echo '</tr>';
b48f834c 1569
9ff5310a 1570 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1571 foreach ($courses as $course) {
e05bcf2f 1572 $linkcss = $course->visible ? '' : ' class="dimmed" ';
1573 echo '<tr><td valign="top" width="30">&nbsp;';
290130b3 1574 echo '</td><td valign="top" width="100%" class="course name">';
e05bcf2f 1575 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>';
290130b3 1576 echo '</td><td align="right" valign="top" nowrap="nowrap" class="course info">';
c2cb4545 1577 if ($course->guest ) {
e05bcf2f 1578 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1579 echo '<img hspace="1" alt="'.$strallowguests.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1580 } else {
e05bcf2f 1581 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1582 }
c2cb4545 1583 if ($course->password) {
e05bcf2f 1584 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1585 echo '<img hspace="1" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1586 } else {
e05bcf2f 1587 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1588 }
1589 if ($course->summary) {
e05bcf2f 1590 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
1591 '<img hspace="1" alt="'.$strsummary.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1592 400, 500, $strsummary);
ebe8ddc1 1593 } else {
e05bcf2f 1594 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1595 }
e05bcf2f 1596 echo '</td></tr>';
0c656181 1597 }
ba2e5d73 1598 }
d2b6ba70 1599 } else {
b48f834c 1600
e0140f24 1601 echo '<tr>';
1602
b48f834c 1603 if ($depth) {
1604 $indent = $depth*20;
e05bcf2f 1605 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1606 print_spacer(10, $indent);
e05bcf2f 1607 echo '</td>';
d2b6ba70 1608 }
89adb174 1609
290130b3 1610 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1611 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1612 echo '</td>';
290130b3 1613 echo '<td valign="top" class="category number">';
e05bcf2f 1614 if ($category->coursecount) {
1615 echo $category->coursecount;
1616 }
1617 echo '</td></tr>';
c2cb4545 1618 }
e05bcf2f 1619 echo '</table>';
c2cb4545 1620}
1621
c2cb4545 1622
8e227aa7 1623function print_courses($category, $width="100%", $hidesitecourse = false) {
c2cb4545 1624/// Category is 0 (for all courses) or an object
1625
810393c8 1626 global $CFG;
c2cb4545 1627
1628 if (empty($category)) {
90c2ca2e 1629 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1630 if (count($categories) == 1) {
1631 $category = array_shift($categories);
32b9a983 1632 $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol,c.guest');
90c2ca2e 1633 } else {
32b9a983 1634 $courses = get_courses('all', 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol,c.guest');
90c2ca2e 1635 }
1636 unset($categories);
607809b3 1637 } else {
c2cb4545 1638 $categories = get_categories($category->id); // sub categories
32b9a983 1639 $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.category,c.sortorder,c.visible,c.fullname,c.shortname,c.password,c.summary,c.teacher,c.cost,c.currency,c.enrol,c.guest');
c2cb4545 1640 }
1641
c2cb4545 1642 if ($courses) {
1643 foreach ($courses as $course) {
1936c10e 1644 if ($hidesitecourse and ($course->id == SITEID)) {
8e227aa7 1645 continue;
1646 }
c2cb4545 1647 print_course($course, $width);
c2cb4545 1648 }
1649 } else {
f9667a5a 1650 print_heading(get_string("nocoursesyet"));
954fdb42 1651 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
0468976c 1652 if (has_capability('moodle/course:create', $context)) {
255d1033 1653 $options = array();
1654 $options['category'] = $category->id;
1655 echo '<div class="addcoursebutton" align="center">';
1656 print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string("addnewcourse"));
1657 echo '</div>';
1658 }
c2cb4545 1659 }
c2cb4545 1660}
1661
1662
1663function print_course($course, $width="100%") {
1664
861efb19 1665 global $CFG, $USER;
c2cb4545 1666
88768091 1667 $context = get_context_instance(CONTEXT_COURSE, $course->id);
146bbb8f 1668
88768091 1669 print_simple_box_start('center', $width, '', 5, 'coursebox');
c2cb4545 1670
88768091 1671 $linkcss = $course->visible ? '' : ' class="dimmed" ';
22288704 1672
88768091 1673 echo '<table width="100%">';
e5e81e78 1674 echo '<tr valign="top">';
290130b3 1675 echo '<td valign="top" width="50%" class="info">';
34b5847a 1676 echo '<b><a title="'.get_string('entercourse').'"'.
e5e81e78 1677 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
b06334e8 1678 $course->fullname.'</a></b><br />';
1679
d42c64ba 1680 /// first find all roles that are supposed to be displayed
1681 if ($managerroles = get_config('', 'coursemanager')) {
1682 $coursemanagerroles = split(',', $managerroles->value);
1683 foreach ($coursemanagerroles as $roleid) {
1684 $role = get_record('role','id',$roleid);
1685 if ($users = get_role_users($roleid, $context, true, '', 'u.lastname ASC', true)) {
1686 foreach ($users as $teacher) {
1687 $fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context));
1688 $namesarray[] = format_string($role->name).': <a href="'.$CFG->wwwroot.'/user/view.php?id='.
b06334e8 1689 $teacher->id.'&amp;course='.SITEID.'">'.$fullname.'</a>';
d42c64ba 1690 }
b06334e8 1691 }
c2cb4545 1692 }
d42c64ba 1693
1694 if (!empty($namesarray)) {
88768091 1695 echo "<ul class=\"teachers\">\n<li>";
1696 echo implode('</li><li>', $namesarray);
1697 echo "</li></ul>";
1698 }
c2cb4545 1699 }
d42c64ba 1700
88768091 1701 require_once("$CFG->dirroot/enrol/enrol.class.php");
1702 $enrol = enrolment_factory::factory($course->enrol);
146bbb8f 1703 echo $enrol->get_access_icons($course);
c2cb4545 1704
290130b3 1705 echo '</td><td valign="top" width="50%" class="summary">';
9f39c190 1706 $options = NULL;
1707 $options->noclean = true;
34b5847a 1708 $options->para = false;
9f39c190 1709 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
c2cb4545 1710 echo "</td></tr>";
1711 echo "</table>";
1712
1713 print_simple_box_end();
1714}
1715
1716
1717function print_my_moodle() {
1718/// Prints custom user information on the home page.
1719/// Over time this can include all sorts of information
1720
1721 global $USER, $CFG;
1722
86a1ba04 1723 if (empty($USER->id)) {
c2cb4545 1724 error("It shouldn't be possible to see My Moodle without being logged in.");
1725 }
1726
1727 if ($courses = get_my_courses($USER->id)) {
1728 foreach ($courses as $course) {
1936c10e 1729 if ($course->id == SITEID) {
c81696e5 1730 continue;
1731 }
c2cb4545 1732 print_course($course, "100%");
c2cb4545 1733 }
38a10939 1734
7f989948 1735 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1736 echo "<table width=\"100%\"><tr><td align=\"center\">";
1737 print_course_search("", false, "short");
1738 echo "</td><td align=\"center\">";
1739 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1740 echo "</td></tr></table>\n";
1741 }
26330001 1742 } else {
1743 if (count_records("course_categories") > 1) {
cb29b020 1744 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1745 print_whole_category_list();
1746 print_simple_box_end();
1747 } else {
1748 print_courses(0, "100%");
1749 }
607809b3 1750 }
2b8cef80 1751}
1752
11b0c469 1753
a8b56716 1754function print_course_search($value="", $return=false, $format="plain") {
38a10939 1755
1756 global $CFG;
1757
1758 $strsearchcourses= get_string("searchcourses");
1759
1c919752 1760 if ($format == 'plain') {
87d32352 1761 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1762 $output .= '<center><p align="center" class="coursesearchbox">';
f8a5159a 1763 $output .= '<input type="text" size="30" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1764 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1765 $output .= '</p></center></form>';
1c919752 1766 } else if ($format == 'short') {
87d32352 1767 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1768 $output .= '<center><p align="center" class="coursesearchbox">';
f8a5159a 1769 $output .= '<input type="text" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1770 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1771 $output .= '</p></center></form>';
1c919752 1772 } else if ($format == 'navbar') {
87d32352 1773 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1774 $output .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
f8a5159a 1775 $output .= '<input type="text" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1776 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
1c919752 1777 $output .= '</td></tr></table>';
87d32352 1778 $output .= '</form>';
a8b56716 1779 }
1780
1781 if ($return) {
1782 return $output;
1783 }
1784 echo $output;
38a10939 1785}
11b0c469 1786
1787/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1788
1789function add_course_module($mod) {
11b0c469 1790
e5dfd0f3 1791 $mod->added = time();
53f4ad2c 1792 unset($mod->id);
11b0c469 1793
e5dfd0f3 1794 return insert_record("course_modules", $mod);
11b0c469 1795}
1796
7977cffd 1797function add_mod_to_section($mod, $beforemod=NULL) {
1798/// Given a full mod object with section and course already defined
1799/// If $before is specified, then this is an existing ID which we
1800/// will insert the new module before
1801///
1802/// Returns the course_sections ID where the mod is inserted
11b0c469 1803
9fa49e22 1804 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1805
1806 $section->sequence = trim($section->sequence);
1807
1808 if (empty($section->sequence)) {
11b0c469 1809 $newsequence = "$mod->coursemodule";
7977cffd 1810
1811 } else if ($beforemod) {
1812 $modarray = explode(",", $section->sequence);
1813
1814 if ($key = array_keys ($modarray, $beforemod->id)) {
1815 $insertarray = array($mod->id, $beforemod->id);
1816 array_splice($modarray, $key[0], 1, $insertarray);
1817 $newsequence = implode(",", $modarray);
1818
1819 } else { // Just tack it on the end anyway
1820 $newsequence = "$section->sequence,$mod->coursemodule";
1821 }
1822
1823 } else {
1824 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1825 }
89adb174 1826
e5dfd0f3 1827 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1828 return $section->id; // Return course_sections ID that was used.
11b0c469 1829 } else {
e5dfd0f3 1830 return 0;
11b0c469 1831 }
89adb174 1832
11b0c469 1833 } else { // Insert a new record
e5dfd0f3 1834 $section->course = $mod->course;
1835 $section->section = $mod->section;
1836 $section->summary = "";
1837 $section->sequence = $mod->coursemodule;
1838 return insert_record("course_sections", $section);
11b0c469 1839 }
1840}
1841
48e535bc 1842function set_coursemodule_groupmode($id, $groupmode) {
3d575e6f 1843 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1844}
1845
02f66c42 1846/**
1847* $prevstateoverrides = true will set the visibility of the course module
1848* to what is defined in visibleold. This enables us to remember the current
1849* visibility when making a whole section hidden, so that when we toggle
1850* that section back to visible, we are able to return the visibility of
1851* the course module back to what it was originally.
1852*/
1853function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
978abb42 1854 if (!$cm = get_record('course_modules', 'id', $id)) {
1855 return false;
1856 }
1857 if (!$modulename = get_field('modules', 'name', 'id', $cm->module)) {
1858 return false;
1859 }
dcd338ff 1860 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1861 foreach($events as $event) {
48e535bc 1862 if ($visible) {
1863 show_event($event);
1864 } else {
1865 hide_event($event);
1866 }
dcd338ff 1867 }
1868 }
02f66c42 1869 if ($prevstateoverrides) {
1870 if ($visible == '0') {
1871 // Remember the current visible state so we can toggle this back.
1872 set_field('course_modules', 'visibleold', $cm->visible, 'id', $id);
1873 } else {
1874 // Get the previous saved visible states.
1875 return set_field('course_modules', 'visible', $cm->visibleold, 'id', $id);
1876 }
1877 }
48e535bc 1878 return set_field("course_modules", "visible", $visible, "id", $id);
1acfbce5 1879}
1880
290130b3 1881/*
1882 * Delete a course module and any associated data at the course level (events)
264867fd 1883 * Until 1.5 this function simply marked a deleted flag ... now it
290130b3 1884 * deletes it completely.
1885 *
1886 */
48e535bc 1887function delete_course_module($id) {
290130b3 1888 if (!$cm = get_record('course_modules', 'id', $id)) {
1889 return true;
1890 }
dcd338ff 1891 $modulename = get_field('modules', 'name', 'id', $cm->module);
1892 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1893 foreach($events as $event) {
48e535bc 1894 delete_event($event);
dcd338ff 1895 }
1896 }
290130b3 1897 return delete_records('course_modules', 'id', $cm->id);
11b0c469 1898}
1899
1900function delete_mod_from_section($mod, $section) {
11b0c469 1901
e5dfd0f3 1902 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1903
e5dfd0f3 1904 $modarray = explode(",", $section->sequence);
11b0c469 1905
1906 if ($key = array_keys ($modarray, $mod)) {
1907 array_splice($modarray, $key[0], 1);
1908 $newsequence = implode(",", $modarray);
e5dfd0f3 1909 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1910 } else {
1911 return false;
1912 }
89adb174 1913
11b0c469 1914 }
7977cffd 1915 return false;
11b0c469 1916}
1917
12905134 1918function move_section($course, $section, $move) {
1919/// Moves a whole course section up and down within the course
798b70a1 1920 global $USER;
12905134 1921
1922 if (!$move) {
1923 return true;
1924 }
1925
1926 $sectiondest = $section + $move;
1927
1928 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1929 return false;
1930 }
1931
1932 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1933 return false;
1934 }
1935
1936 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1937 return false;
1938 }
1939
56e34ee4 1940 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1941 return false;
1942 }
56e34ee4 1943 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1944 return false;
1945 }
798b70a1 1946 // if the focus is on the section that is being moved, then move the focus along
1947 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
1948 course_set_display($course->id, $sectiondest);
1949 }
5390cbb7 1950
a987106d 1951 // Check for duplicates and fix order if needed.
5390cbb7 1952 // There is a very rare case that some sections in the same course have the same section id.
a987106d 1953 $sections = get_records_select('course_sections', "course = $course->id", 'section ASC');
1954 $n = 0;
1955 foreach ($sections as $section) {
1956 if ($section->section != $n) {
5390cbb7 1957 if (!set_field('course_sections', 'section', $n, 'id', $section->id)) {
1958 return false;
1959 }
5390cbb7 1960 }
a987106d 1961 $n++;
5390cbb7 1962 }
12905134 1963 return true;
1964}
1965
1966
7977cffd 1967function moveto_module($mod, $section, $beforemod=NULL) {
1968/// All parameters are objects
1969/// Move the module object $mod to the specified $section
1970/// If $beforemod exists then that is the module
1971/// before which $modid should be inserted
1972
1973/// Remove original module from original section
1974
1975 if (! delete_mod_from_section($mod->id, $mod->section)) {
1976 notify("Could not delete module from existing section");
1977 }
1978
1979/// Update module itself if necessary
1980
1981 if ($mod->section != $section->id) {
89adb174 1982 $mod->section = $section->id;
7977cffd 1983 if (!update_record("course_modules", $mod)) {
1984 return false;
1985 }
48e535bc 1986 // if moving to a hidden section then hide module
1987 if (!$section->visible) {
1988 set_coursemodule_visible($mod->id, 0);
1989 }
7977cffd 1990 }
1991
1992/// Add the module into the new section
1993
1994 $mod->course = $section->course;
1995 $mod->section = $section->section; // need relative reference
1996 $mod->coursemodule = $mod->id;
1997
1998 if (! add_mod_to_section($mod, $beforemod)) {
1999 return false;
2000 }
2001
2002 return true;
2003
2004}
2005
24e1eae4 2006function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
810393c8 2007 global $CFG, $USER;
94361e02 2008
3d575e6f 2009 static $str;
37a88449 2010 static $sesskey;
3d575e6f 2011
2012 if (!isset($str)) {
90ebdf65 2013 $str->delete = get_string("delete");
2014 $str->move = get_string("move");
2015 $str->moveup = get_string("moveup");
2016 $str->movedown = get_string("movedown");
2017 $str->moveright = get_string("moveright");
2018 $str->moveleft = get_string("moveleft");
2019 $str->update = get_string("update");
2020 $str->duplicate = get_string("duplicate");
2021 $str->hide = get_string("hide");
2022 $str->show = get_string("show");
3d575e6f 2023 $str->clicktochange = get_string("clicktochange");
32d03b7b 2024 $str->forcedmode = get_string("forcedmode");
3d575e6f 2025 $str->groupsnone = get_string("groupsnone");
2026 $str->groupsseparate = get_string("groupsseparate");
2027 $str->groupsvisible = get_string("groupsvisible");
37a88449 2028 $sesskey = sesskey();
1acfbce5 2029 }
94361e02 2030
24e1eae4 2031 if ($section >= 0) {
75f087b6 2032 $section = '&amp;sr='.$section; // Section return
24e1eae4 2033 } else {
2034 $section = '';
2035 }
2036
94361e02 2037 if ($absolute) {
37a88449 2038 $path = $CFG->wwwroot.'/course';
dc0dc7d5 2039 } else {
37a88449 2040 $path = '.';
dc0dc7d5 2041 }
2042
3d575e6f 2043 if ($mod->visible) {
90ebdf65 2044 $hideshow = '<a class="editing_hide" title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id.
37a88449 2045 '&amp;sesskey='.$sesskey.$section.'"><img'.
2046 ' src="'.$CFG->pixpath.'/t/hide.gif" hspace="2" height="11" width="11" '.
90ebdf65 2047 ' border="0" alt="'.$str->hide.'" /></a>'."\n";
1acfbce5 2048 } else {
90ebdf65 2049 $hideshow = '<a class="editing_show" title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id.
37a88449 2050 '&amp;sesskey='.$sesskey.$section.'"><img'.
2051 ' src="'.$CFG->pixpath.'/t/show.gif" hspace="2" height="11" width="11" '.
90ebdf65 2052 ' border="0" alt="'.$str->show.'" /></a>'."\n";
7977cffd 2053 }
3d575e6f 2054 if ($mod->groupmode !== false) {
2055 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 2056 $grouptitle = $str->groupsseparate;
90ebdf65 2057 $groupclass = 'editing_groupseparate';
37a88449 2058 $groupimage = $CFG->pixpath.'/t/groups.gif';
2059 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=0&amp;sesskey='.$sesskey;
3d575e6f 2060 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 2061 $grouptitle = $str->groupsvisible;
90ebdf65 2062 $groupclass = 'editing_groupvisible';
37a88449 2063 $groupimage = $CFG->pixpath.'/t/groupv.gif';
2064 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=1&amp;sesskey='.$sesskey;
32d03b7b 2065 } else {
2066 $grouptitle = $str->groupsnone;
90ebdf65 2067 $groupclass = 'editing_groupsnone';
37a88449 2068 $groupimage = $CFG->pixpath.'/t/groupn.gif';
2069 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=2&amp;sesskey='.$sesskey;
32d03b7b 2070 }
2071 if ($mod->groupmodelink) {
90ebdf65 2072 $groupmode = '<a class="'.$groupclass.'" title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'.
37a88449 2073 '<img src="'.$groupimage.'" hspace="2" height="11" width="11" '.
2074 'border="0" alt="'.$grouptitle.'" /></a>';
3d575e6f 2075 } else {
37a88449 2076 $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '.
2077 ' src="'.$groupimage.'" hspace="2" height="11" width="11" '.
2078 'border="0" alt="'.$grouptitle.'" />';
3d575e6f 2079 }
2080 } else {
2081 $groupmode = "";
2082 }
7977cffd 2083
2084 if ($moveselect) {
90ebdf65 2085 $move = '<a class="editing_move" title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id.
37a88449 2086 '&amp;sesskey='.$sesskey.$section.'"><img'.
2087 ' src="'.$CFG->pixpath.'/t/move.gif" hspace="2" height="11" width="11" '.
90ebdf65 2088 ' border="0" alt="'.$str->move.'" /></a>'."\n";
493486c4 2089 } else {
90ebdf65 2090 $move = '<a class="editing_moveup" title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2091 '&amp;move=-1&amp;sesskey='.$sesskey.$section.'"><img'.
2092 ' src="'.$CFG->pixpath.'/t/up.gif" hspace="2" height="11" width="11" '.
90ebdf65 2093 ' border="0" alt="'.$str->moveup.'" /></a>'."\n".
2094 '<a class="editing_movedown" title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2095 '&amp;move=1&amp;sesskey='.$sesskey.$section.'"><img'.
2096 ' src="'.$CFG->pixpath.'/t/down.gif" hspace="2" height="11" width="11" '.
90ebdf65 2097 ' border="0" alt="'.$str->movedown.'" /></a>'."\n";
7977cffd 2098 }
2099
aac94fd0 2100 $leftright = "";
2101 if ($indent > 0) {
90ebdf65 2102 $leftright .= '<a class="editing_moveleft" title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2103 '&amp;indent=-1&amp;sesskey='.$sesskey.$section.'"><img'.
2104 ' src="'.$CFG->pixpath.'/t/left.gif" hspace="2" height="11" width="11" '.
90ebdf65 2105 ' border="0" alt="'.$str->moveleft.'" /></a>'."\n";
aac94fd0 2106 }
2107 if ($indent >= 0) {
90ebdf65 2108 $leftright .= '<a class="editing_moveright" title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2109 '&amp;indent=1&amp;sesskey='.$sesskey.$section.'"><img'.
2110 ' src="'.$CFG->pixpath.'/t/right.gif" hspace="2" height="11" width="11" '.
90ebdf65 2111 ' border="0" alt="'.$str->moveright.'" /></a>'."\n";
37a88449 2112 }
2113
90ebdf65 2114 return '<span class="commands">'."\n".$leftright.$move.
2115 '<a class="editing_update" title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id.
37a88449 2116 '&amp;sesskey='.$sesskey.$section.'"><img'.
2117 ' src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" '.
90ebdf65 2118 ' alt="'.$str->update.'" /></a>'."\n".
2119 '<a class="editing_delete" title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id.
37a88449 2120 '&amp;sesskey='.$sesskey.$section.'"><img'.
2121 ' src="'.$CFG->pixpath.'/t/delete.gif" hspace="2" height="11" width="11" border="0" '.
90ebdf65 2122 ' alt="'.$str->delete.'" /></a>'."\n".$hideshow.$groupmode."\n".'</span>';
90845098 2123}
2124
b61efafb 2125/**
264867fd 2126 * given a course object with shortname & fullname, this function will
b61efafb 2127 * truncate the the number of chars allowed and add ... if it was too long
2128 */
2129function course_format_name ($course,$max=100) {
264867fd 2130
b61efafb 2131 $str = $course->shortname.': '.$course->fullname;
2132 if (strlen($str) <= $max) {
2133 return $str;
2134 }
2135 else {
2136 return substr($str,0,$max-3).'...';
2137 }
2138}
2139
2140/**
2141 * This function will return true if the given course is a child course at all
2142 */
2143function course_in_meta ($course) {
5f37b628 2144 return record_exists("course_meta","child_course",$course->id);
b61efafb 2145}
2146
48e535bc 2147
2148/**
2149 * Print standard form elements on module setup forms in mod/.../mod.html
2150 */
2151function print_standard_coursemodule_settings($form) {
da2224f8 2152 if (! $course = get_record('course', 'id', $form->course)) {
2153 error("This course doesn't exist");
2154 }
2155 print_groupmode_setting($form, $course);
2156 print_visible_setting($form, $course);
48e535bc 2157}
2158
2159/**
2160 * Print groupmode form element on module setup forms in mod/.../mod.html
2161 */
5ebb746b 2162function print_groupmode_setting($form, $course=NULL) {
48e535bc 2163
5ebb746b 2164 if (empty($course)) {
2165 if (! $course = get_record('course', 'id', $form->course)) {
2166 error("This course doesn't exist");
2167 }
2168 }
48e535bc 2169 if ($form->coursemodule) {
2170 if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
2171 error("This course module doesn't exist");
2172 }
2173 } else {
2174 $cm = null;
2175 }
2176 $groupmode = groupmode($course, $cm);
2177 if ($course->groupmode or (!$course->groupmodeforce)) {
2178 echo '<tr valign="top">';
2179 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
7bbe08a2 2180 echo '<td align="left">';
48e535bc 2181 unset($choices);
2182 $choices[NOGROUPS] = get_string('groupsnone');
2183 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
2184 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
2185 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
2186 helpbutton('groupmode', get_string('groupmode'));
2187 echo '</td></tr>';
2188 }
2189}
2190
2191/**
2192 * Print visibility setting form element on module setup forms in mod/.../mod.html
2193 */
5ebb746b 2194function print_visible_setting($form, $course=NULL) {
1ee55c41 2195 if (empty($course)) {
2196 if (! $course = get_record('course', 'id', $form->course)) {
2197 error("This course doesn't exist");
2198 }
2199 }
48e535bc 2200 if ($form->coursemodule) {
2201 $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
2202 } else {
2203 $visible = true;
2204 }
2205
2206 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
2207 $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
2208 } else {
2209 $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
2210 }
2211 if ($hiddensection) {
2212 $visible = false;
2213 }
264867fd 2214
48e535bc 2215 echo '<tr valign="top">';
182311e4 2216 echo '<td align="right"><b>'.get_string('visible', '').':</b></td>';
7bbe08a2 2217 echo '<td align="left">';
48e535bc 2218 unset($choices);
1f15db9d 2219 $choices[1] = get_string('show');
2220 $choices[0] = get_string('hide');
48e535bc 2221 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
2222 echo '</td></tr>';
264867fd 2223}
48e535bc 2224
0705ff84 2225function update_restricted_mods($course,$mods) {
2226 delete_records("course_allowed_modules","course",$course->id);
2227 if (empty($course->restrictmodules)) {
2228 return;
2229 }
2230 else {
2231 foreach ($mods as $mod) {
2232 if ($mod == 0)
2233 continue; // this is the 'allow none' option
2234 $am->course = $course->id;
2235 $am->module = $mod;
2236 insert_record("course_allowed_modules",$am);
2237 }
2238 }
2239}
2240
2241/**
2242 * This function will take an int (module id) or a string (module name)
2243 * and return true or false, whether it's allowed in the given course (object)
264867fd 2244 * $mod is not allowed to be an object, as the field for the module id is inconsistent
0705ff84 2245 * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
2246 */
2247
2248function course_allowed_module($course,$mod) {
2249 if (empty($course->restrictmodules)) {
2250 return true;
2251 }
264867fd 2252
51792df0 2253 // i am not sure this capability is correct
2254 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
0705ff84 2255 return true;
2256 }
2257 if (is_numeric($mod)) {
2258 $modid = $mod;
2259 } else if (is_string($mod)) {
cb77cf12 2260 if ($mod = get_field("modules","id","name",$mod))
0705ff84 2261 $modid = $mod;
2262 }
2263 if (empty($modid)) {
2264 return false;
2265 }
2266 return (record_exists("course_allowed_modules","course",$course->id,"module",$modid));
2267}
2268
861efb19 2269/***
2270 *** Efficiently moves many courses around while maintaining
2271 *** sortorder in order.
264867fd 2272 ***
861efb19 2273 *** $courseids is an array of course ids
2274 ***
2275 **/
2276
2277function move_courses ($courseids, $categoryid) {
2278
2279 global $CFG;
2280
2281 if (!empty($courseids)) {
264867fd 2282
2283 $courseids = array_reverse($courseids);
861efb19 2284
2285 foreach ($courseids as $courseid) {
264867fd 2286
861efb19 2287 if (! $course = get_record("course", "id", $courseid)) {
2288 notify("Error finding course $courseid");
2289 } else {
2290 // figure out a sortorder that we can use in the destination category
2291 $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
2292 FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid);
2293 if ($sortorder === false) {
2294 // the category is empty
2295 // rather than let the db default to 0
264867fd 2296 // set it to > 100 and avoid extra work in fix_coursesortorder()
861efb19 2297 $sortorder = 200;
2298 } else if ($sortorder < 10) {
2299 fix_course_sortorder($categoryid);
2300 }
2301
2302 $course->category = $categoryid;
2303 $course->sortorder = $sortorder;
2304 $course->fullname = addslashes($course->fullname);
2305 $course->shortname = addslashes($course->shortname);
2306 $course->summary = addslashes($course->summary);
2307 $course->password = addslashes($course->password);
2308 $course->teacher = addslashes($course->teacher);
2309 $course->teachers = addslashes($course->teachers);
2310 $course->student = addslashes($course->student);
2311 $course->students = addslashes($course->students);
264867fd 2312
861efb19 2313 if (!update_record('course', $course)) {
2314 notify("An error occurred - course not moved!");
2315 }
2316 }
2317 }
2318 fix_course_sortorder();
264867fd 2319 }
861efb19 2320 return true;
2321}
2322
ae628043 2323/**
2324 * @param string $format Course format ID e.g. 'weeks'
2325 * @return Name that the course format prefers for sections
2326 */
2327function get_section_name($format) {
2328 $sectionname = get_string("name$format","format_$format");
2329 if($sectionname == "[[name$format]]") {
2330 $sectionname = get_string("name$format");
2331 }
2332 return $sectionname;
2333}
2334
b63ec9db 2335?>