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