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