migrated to formslib
[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.
1c45e42e 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
92890025 335
336 $result = array();
337 $result['logs'] = get_logs($selector, $order, $limitfrom, $limitnum, $totalcount);
338 $result['totalcount'] = $totalcount;
339 return $result;
340}
341
342
343function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
344 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
345
346 global $CFG;
347
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 }
92890025 354
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 }
363
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) {
21283ddc 381 echo "<th class=\"c0 header\">".get_string('course')."</th>\n";
1548978d 382 }
21283ddc 383 echo "<th class=\"c1 header\">".get_string('time')."</th>\n";
384 echo "<th class=\"c2 header\">".get_string('ip_address')."</th>\n";
385 echo "<th class=\"c3 header\">".get_string('fullname')."</th>\n";
386 echo "<th class=\"c4 header\">".get_string('action')."</th>\n";
387 echo "<th class=\"c5 header\">".get_string('info')."</th>\n";
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
c8b0a50b 415 //Filter log->info
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');
454
455 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
92890025 456 $modname, $modid, $modaction, $groupid)) {
457 return false;
458 }
459
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 }
954fdb42 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';
478 header("Content-Type: application/download\n");
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
507 //Filter log->info
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) {
526
527 global $CFG;
528
954fdb42 529 require_once("$CFG->libdir/excellib.class.php");
530
531 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
92890025 532 $modname, $modid, $modaction, $groupid)) {
533 return false;
534 }
535
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 }
954fdb42 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';
92890025 555
556 $workbook = new MoodleExcelWorkbook('-');
557 $workbook->send($filename);
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'));
562
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 }
615
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, '');
92890025 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) {
639
640 global $CFG;
954fdb42 641 require_once($CFG->libdir.'/phpdocwriter/lib/include.php');
92890025 642
643 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
644 $modname, $modid, $modaction, $groupid)) {
645 return false;
646 }
647
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 }
656
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);
684
685 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
954fdb42 686 get_string('fullname'), get_string('action'), get_string('info'));
687
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
708 // Filter log->info
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();
92890025 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
1c45e42e 779 $isteacher = has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id));
2ac64806 780
6f80940b 781 $timestart = time() - COURSE_MAX_RECENT_PERIOD;
0f87cb1d 782
721d14cb 783 if (!empty($USER->lastcourseaccess[$course->id])) {
784 if ($USER->lastcourseaccess[$course->id] > $timestart) {
785 $timestart = $USER->lastcourseaccess[$course->id];
9e51847a 786 }
3d891989 787 }
0f87cb1d 788
de785682 789 echo '<div class="activitydate">';
27bf9e20 790 echo get_string('activitysince', '', userdate($timestart));
de785682 791 echo '</div>';
792 echo '<div class="activityhead">';
0f87cb1d 793
de785682 794 echo '<a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a>';
0f87cb1d 795
5fc835a5 796 echo "</div>\n";
0f87cb1d 797
600149be 798
799 // Firstly, have there been any new enrolments?
800
801 $heading = false;
802 $content = false;
1b5910c4 803
6c38b7e0 804 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 805
5fc835a5 806 //Accessibility: new users now appear in an <OL> list.
6c38b7e0 807 if ($users) {
27bf9e20 808 echo '<div class="newusers">';
5fc835a5 809 if (! $heading) {
810 print_headline(get_string("newusers").':', 3);
811 $heading = true;
812 $content = true;
813 }
814 echo "<ol class=\"list\">\n";
6c38b7e0 815 foreach ($users as $user) {
5fc835a5 816
1c45e42e 817 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
5fc835a5 818 echo '<li class="name"><a href="'.$CFG->wwwroot."/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></li>\n";
600149be 819 }
5fc835a5 820 echo "</ol>\n</div>\n";
600149be 821 }
822
1b5910c4 823 // Next, have there been any modifications to the course structure?
824
27bf9e20 825 $logs = get_records_select('log', "time > '$timestart' AND course = '$course->id' AND
1b5910c4 826 module = 'course' AND action LIKE '% mod'", "time ASC");
827
828 if ($logs) {
829 foreach ($logs as $key => $log) {
27bf9e20 830 $info = split(' ', $log->info);
c9f6251e 831
27bf9e20 832 if ($info[0] == 'label') { // Labels are special activities
c9f6251e 833 continue;
834 }
835
27bf9e20 836 $modname = get_field($info[0], 'name', 'id', $info[1]);
1b5910c4 837 //Create a temp valid module structure (course,id)
838 $tempmod->course = $log->course;
839 $tempmod->id = $info[1];
840 //Obtain the visible property from the instance
841 $modvisible = instance_is_visible($info[0],$tempmod);
89adb174 842
1b5910c4 843 //Only if the mod is visible
844 if ($modvisible) {
845 switch ($log->action) {
27bf9e20 846 case 'add mod':
847 $stradded = get_string('added', 'moodle', get_string('modulename', $info[0]));
5847b267 848 $changelist[$log->info] = array ('operation' => 'add', 'text' => "$stradded:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>");
1b5910c4 849 break;
27bf9e20 850 case 'update mod':
851 $strupdated = get_string('updated', 'moodle', get_string('modulename', $info[0]));
852 if (empty($changelist[$log->info])) {
5847b267 853 $changelist[$log->info] = array ('operation' => 'update', 'text' => "$strupdated:<br /><a href=\"$CFG->wwwroot/course/$log->url\">".format_string($modname,true)."</a>");
1b5910c4 854 }
855 break;
27bf9e20 856 case 'delete mod':
857 if (!empty($changelist[$log->info]['operation']) and
858 $changelist[$log->info]['operation'] == 'add') {
859 $changelist[$log->info] = NULL;
1b5910c4 860 } else {
27bf9e20 861 $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $info[0]));
862 $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted);
1b5910c4 863 }
864 break;
600149be 865 }
ef25340c 866 }
867 }
868 }
869
9c9f7d77 870 if (!empty($changelist)) {
ef25340c 871 foreach ($changelist as $changeinfo => $change) {
872 if ($change) {
873 $changes[$changeinfo] = $change;
874 }
875 }
8a59942e 876 if (isset($changes)){
877 if (count($changes) > 0) {
27bf9e20 878 print_headline(get_string('courseupdates').':', 3);
8a59942e 879 $content = true;
880 foreach ($changes as $changeinfo => $change) {
27bf9e20 881 echo '<p class="activity">'.$change['text'].'</p>';
8a59942e 882 }
600149be 883 }
884 }
89adb174 885 }
bf40f9c1 886
3869a2ac 887 // Now display new things from each module
600149be 888
27bf9e20 889 $mods = get_records('modules', 'visible', '1', 'name', 'id, name');
0fd7da81 890
1b5910c4 891 foreach ($mods as $mod) { // Each module gets it's own logs and prints them
27bf9e20 892 include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php');
893 $print_recent_activity = $mod->name.'_print_recent_activity';
1b5910c4 894 if (function_exists($print_recent_activity)) {
66b0b104 895 //
896 // NOTE:
897 // $isteacher is to be deprecated!
898 //
899 // TODO:
1172e5db 900 // 1) Make sure that all _print_recent_activity functions are
901 // not using the $isteacher value.
902 // 2) Eventually, remove the $isteacher parameter from the
903 // function calls.
66b0b104 904 //
1b5910c4 905 $modcontent = $print_recent_activity($course, $isteacher, $timestart);
3869a2ac 906 if ($modcontent) {
907 $content = true;
600149be 908 }
600149be 909 }
910 }
911
912 if (! $content) {
27bf9e20 913 echo '<p class="message">'.get_string('nothingnew').'</p>';
600149be 914 }
600149be 915}
916
e1360728 917
d897cae4 918function get_array_of_activities($courseid) {
89adb174 919// For a given course, returns an array of course activity objects
d897cae4 920// Each item in the array contains he following properties:
921// cm - course module id
922// mod - name of the module (eg forum)
923// section - the number of the section (eg week or topic)
924// name - the name of the instance
5867bfb5 925// visible - is the instance visible or not
86aa7ccf 926// extra - contains extra string to include in any link
d897cae4 927
8dddba42 928 global $CFG;
929
d897cae4 930 $mod = array();
931
9fa49e22 932 if (!$rawmods = get_course_mods($courseid)) {
d897cae4 933 return NULL;
934 }
935
936 if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
937 foreach ($sections as $section) {
74666583 938 if (!empty($section->sequence)) {
d897cae4 939 $sequence = explode(",", $section->sequence);
940 foreach ($sequence as $seq) {
7af6281f 941 if (empty($rawmods[$seq])) {
942 continue;
943 }
d897cae4 944 $mod[$seq]->cm = $rawmods[$seq]->id;
945 $mod[$seq]->mod = $rawmods[$seq]->modname;
946 $mod[$seq]->section = $section->section;
947 $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
fec5a6a6 948 $mod[$seq]->visible = $rawmods[$seq]->visible;
86aa7ccf 949 $mod[$seq]->extra = "";
8dddba42 950
951 $modname = $mod[$seq]->mod;
952 $functionname = $modname."_get_coursemodule_info";
953
954 include_once("$CFG->dirroot/mod/$modname/lib.php");
955
956 if (function_exists($functionname)) {
9d361034 957 if ($info = $functionname($rawmods[$seq])) {
958 if (!empty($info->extra)) {
959 $mod[$seq]->extra = $info->extra;
960 }
961 if (!empty($info->icon)) {
962 $mod[$seq]->icon = $info->icon;
963 }
c9f6251e 964 }
965 }
d897cae4 966 }
967 }
968 }
969 }
970 return $mod;
971}
972
973
974
e1360728 975
90845098 976function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
977// Returns a number of useful structures for course displays
7468bf01 978
90845098 979 $mods = NULL; // course modules indexed by id
e0161bff 980 $modnames = NULL; // all course module names (except resource!)
94361e02 981 $modnamesplural= NULL; // all course module names (plural form)
90845098 982 $modnamesused = NULL; // course module names used
7468bf01 983
9fa49e22 984 if ($allmods = get_records("modules")) {
90845098 985 foreach ($allmods as $mod) {
5867bfb5 986 if ($mod->visible) {
987 $modnames[$mod->name] = get_string("modulename", "$mod->name");
988 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
989 }
90845098 990 }
991 asort($modnames);
992 } else {
993 error("No modules are installed!");
994 }
995
9fa49e22 996 if ($rawmods = get_course_mods($courseid)) {
7468bf01 997 foreach($rawmods as $mod) { // Index the mods
959ae824 998 if (empty($modnames[$mod->modname])) {
999 continue;
1000 }
7468bf01 1001 $mods[$mod->id] = $mod;
959ae824 1002 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
3924b988 1003 if ($mod->visible or has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) {
959ae824 1004 $modnamesused[$mod->modname] = $modnames[$mod->modname];
1acfbce5 1005 }
7468bf01 1006 }
c7da6f7a 1007 if ($modnamesused) {
1008 asort($modnamesused);
1009 }
7468bf01 1010 }
e0161bff 1011
1012 unset($modnames['resource']);
1013 unset($modnames['label']);
7468bf01 1014}
1015
9fa49e22 1016
7468bf01 1017function get_all_sections($courseid) {
89adb174 1018
1019 return get_records("course_sections", "course", "$courseid", "section",
7d99d695 1020 "section, id, course, summary, sequence, visible");
7468bf01 1021}
1022
b86fc0e2 1023function course_set_display($courseid, $display=0) {
1024 global $USER;
1025
1066e0dc 1026 if (empty($USER->id)) {
b86fc0e2 1027 return false;
1028 }
1029
1030 if ($display == "all" or empty($display)) {
1031 $display = 0;
1032 }
1033
1034 if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
1035 set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
1036 } else {
1037 $record->userid = $USER->id;
1038 $record->course = $courseid;
1039 $record->display = $display;
1040 if (!insert_record("course_display", $record)) {
1041 notify("Could not save your course display!");
1042 }
1043 }
1044
1045 return $USER->display[$courseid] = $display; // Note: = not ==
1046}
1047
7d99d695 1048function set_section_visible($courseid, $sectionnumber, $visibility) {
1049/// For a given course section, markes it visible or hidden,
1050/// and does the same for every activity in that section
1051
1052 if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
1053 set_field("course_sections", "visible", "$visibility", "id", $section->id);
1054 if (!empty($section->sequence)) {
1055 $modules = explode(",", $section->sequence);
1056 foreach ($modules as $moduleid) {
02f66c42 1057 set_coursemodule_visible($moduleid, $visibility, true);
7d99d695 1058 }
1059 }
5867bfb5 1060 rebuild_course_cache($courseid);
7d99d695 1061 }
1062}
ba2e5d73 1063
5e367a2d 1064
d897cae4 1065function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
52dcc2f9 1066/// Prints a section full of activity modules
7977cffd 1067 global $CFG, $USER;
1068
3d575e6f 1069 static $groupbuttons;
32d03b7b 1070 static $groupbuttonslink;
52dcc2f9 1071 static $isteacher;
1072 static $isediting;
7977cffd 1073 static $ismoving;
1074 static $strmovehere;
1075 static $strmovefull;
54669989 1076 static $strunreadpostsone;
52dcc2f9 1077
4877707e 1078 static $untracked;
a2d71d8e 1079 static $usetracking;
4877707e 1080
a22f8313 1081 $labelformatoptions = New stdClass;
110a32e2 1082
52dcc2f9 1083 if (!isset($isteacher)) {
9fd9c29b 1084 $groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
32d03b7b 1085 $groupbuttonslink = (!$course->groupmodeforce);
52dcc2f9 1086 $isediting = isediting($course->id);
ff0c7de0 1087 $ismoving = $isediting && ismoving($course->id);
3d575e6f 1088 if ($ismoving) {
1089 $strmovehere = get_string("movehere");
1090 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
1091 }
94a6a70f 1092 include_once($CFG->dirroot.'/mod/forum/lib.php');
1093 if ($usetracking = forum_tp_can_track_forums()) {
a2d71d8e 1094 $strunreadpostsone = get_string('unreadpostsone', 'forum');
94a6a70f 1095 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
a2d71d8e 1096 }
7977cffd 1097 }
60bd11cf 1098 $labelformatoptions->noclean = true;
94361e02 1099
fea43a7f 1100/// Casting $course->modinfo to string prevents one notice when the field is null
1101 $modinfo = unserialize((string)$course->modinfo);
94361e02 1102
c6a55371 1103 //Acccessibility: replace table with list <ul>, but don't output empty list.
74666583 1104 if (!empty($section->sequence)) {
94361e02 1105
f2d660dc 1106 // Fix bug #5027, don't want style=\"width:$width\".
1107 echo "<ul class=\"section\">\n";
94361e02 1108 $sectionmods = explode(",", $section->sequence);
1109
1110 foreach ($sectionmods as $modnumber) {
9ae687af 1111 if (empty($mods[$modnumber])) {
1112 continue;
1113 }
94361e02 1114 $mod = $mods[$modnumber];
c9f6251e 1115
3924b988 1116 if ($mod->visible or has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $course->id))) {
954fdb42 1117 echo '<li class="activity '.$mod->modname.'" id="module-'.$modnumber.'">'; // Unique ID
7977cffd 1118 if ($ismoving) {
1119 if ($mod->id == $USER->activitycopy) {
1120 continue;
1121 }
1c919752 1122 echo '<a title="'.$strmovefull.'"'.
8b92f5bb 1123 ' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 1124 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
1125 ' alt="'.$strmovehere.'" /></a><br />
1c919752 1126 ';
1acfbce5 1127 }
7977cffd 1128 $instancename = urldecode($modinfo[$modnumber]->name);
954fdb42 1129 $instancename = format_string($instancename, true, $course->id);
c9f6251e 1130
86aa7ccf 1131 if (!empty($modinfo[$modnumber]->extra)) {
1132 $extra = urldecode($modinfo[$modnumber]->extra);
1133 } else {
1134 $extra = "";
1135 }
c9f6251e 1136
9d361034 1137 if (!empty($modinfo[$modnumber]->icon)) {
1138 $icon = "$CFG->pixpath/".urldecode($modinfo[$modnumber]->icon);
1139 } else {
1140 $icon = "$CFG->modpixpath/$mod->modname/icon.gif";
1141 }
1142
aac94fd0 1143 if ($mod->indent) {
1144 print_spacer(12, 20 * $mod->indent, false);
1145 }
1146
c9f6251e 1147 if ($mod->modname == "label") {
aac94fd0 1148 if (!$mod->visible) {
1149 echo "<span class=\"dimmed_text\">";
1150 }
179c9a50 1151 echo format_text($extra, FORMAT_HTML, $labelformatoptions);
aac94fd0 1152 if (!$mod->visible) {
1153 echo "</span>";
1154 }
c9f6251e 1155
1156 } else { // Normal activity
1157 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
1c919752 1158 echo '<img src="'.$icon.'"'.
446390fb 1159 ' class="activityicon" alt="'.$mod->modfullname.'" />'.
1160 ' <a title="'.$mod->modfullname.'" '.$linkcss.' '.$extra.
1c919752 1161 ' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.
446390fb 1162 $instancename.'</a>';
c9f6251e 1163 }
a2d71d8e 1164 if ($usetracking && $mod->modname == 'forum') {
f37da850 1165 $groupmode = groupmode($course, $mod);
3924b988 1166 $groupid = ($groupmode == SEPARATEGROUPS && !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) ?
a2d71d8e 1167 get_current_group($course->id) : false;
4877707e 1168
94a6a70f 1169 if (forum_tp_can_track_forums() && !isset($untracked[$mod->instance])) {
4877707e 1170 $unread = forum_tp_count_forum_unread_posts($USER->id, $mod->instance, $groupid);
1171 if ($unread) {
1172 echo '<span class="unread"> <a href="'.$CFG->wwwroot.'/mod/forum/view.php?id='.$mod->id.'">';
1173 if ($unread == 1) {
1174 echo $strunreadpostsone;
1175 } else {
1176 print_string('unreadpostsnumber', 'forum', $unread);
1177 }
1178 echo '</a> </span>';
d0388ebe 1179 }
54669989 1180 }
f37da850 1181 }
1182
c9f6251e 1183 if ($isediting) {
5f390342 1184 if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource') {
32d03b7b 1185 if (! $mod->groupmodelink = $groupbuttonslink) {
1186 $mod->groupmode = $course->groupmode;
1187 }
1188
1189 } else {
3d575e6f 1190 $mod->groupmode = false;
1191 }
37a88449 1192 echo '&nbsp;&nbsp;';
24e1eae4 1193 echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section);
c9f6251e 1194 }
c6a55371 1195 echo "</li>\n";
94361e02 1196 }
94361e02 1197 }
f2d660dc 1198 } elseif ($ismoving) {
1199 echo "<ul class=\"section\">\n";
1200 }
7977cffd 1201 if ($ismoving) {
64fdc686 1202 echo '<li><a title="'.$strmovefull.'"'.
8b92f5bb 1203 ' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 1204 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
c6a55371 1205 ' alt="'.$strmovehere.'" /></a></li>
1c919752 1206 ';
7977cffd 1207 }
c6a55371 1208 if (!empty($section->sequence) || $ismoving) {
1209 echo "</ul><!--class='section'-->\n\n";
1210 }
a7ad3ea6 1211}
1212
5867bfb5 1213
cb57e6f4 1214function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
e0161bff 1215// Prints the menus to add activities and resources
1216
8b92f5bb 1217 global $CFG, $USER;
6da4b261 1218 static $straddactivity, $stractivities, $straddresource, $resources;
e0161bff 1219
1220 if (!isset($straddactivity)) {
1221 $straddactivity = get_string('addactivity');
1222 $straddresource = get_string('addresource');
6da4b261 1223
1224 /// Standard resource types
1225 require_once("$CFG->dirroot/mod/resource/lib.php");
1226 $resourceraw = resource_get_resource_types();
1227
1228 foreach ($resourceraw as $type => $name) {
839f2456 1229 $resources["resource&amp;type=$type"] = $name;
e0161bff 1230 }
0705ff84 1231 if (course_allowed_module($course,'label')) {
1232 $resources['label'] = get_string('resourcetypelabel', 'resource');
1233 }
e0161bff 1234 }
1235
d33d0cda 1236 $output = '<div style="text-align: right">';
0705ff84 1237 if (course_allowed_module($course,'resource')) {
81a5d9ca 1238 $resourceallowed = true;
0705ff84 1239 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
1240 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
1241 }
cb57e6f4 1242
1243 if ($vertical) {
d33d0cda 1244 $output .= '<div>';
cb57e6f4 1245 }
1246
0705ff84 1247 // we need to loop through the forms and check to see if we can add them.
cb77cf12 1248 foreach ($modnames as $key=>$value) {
0705ff84 1249 if (!course_allowed_module($course,$key))
cb77cf12 1250 unset($modnames[$key]);
0705ff84 1251 }
1252
1253 // this is stupid but labels get put into resource, so if resource is hidden and label is not, we're in trouble.
1254 if (course_allowed_module($course,'label') && empty($resourceallowed)) {
1255 $modnames['label'] = get_string('modulename', 'label');
1256 }
1257
1c46eb03 1258 $output .= ' ';
8b92f5bb 1259 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=$USER->sesskey&amp;add=",
6da4b261 1260 $modnames, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
d33d0cda 1261
1262 if ($vertical) {
1263 $output .= '</div>';
1264 }
1265
cb57e6f4 1266 $output .= '</div>';
1267
1268 if ($return) {
1269 return $output;
1270 } else {
1271 echo $output;
1272 }
e0161bff 1273}
1274
5867bfb5 1275function rebuild_course_cache($courseid=0) {
1276// Rebuilds the cached list of course activities stored in the database
1277// If a courseid is not specified, then all are rebuilt
1278
1279 if ($courseid) {
1280 $select = "id = '$courseid'";
1281 } else {
1282 $select = "";
1283 }
1284
1a31c2b3 1285 if ($courses = get_records_select("course", $select,'','id,fullname')) {
5867bfb5 1286 foreach ($courses as $course) {
1287 $modinfo = serialize(get_array_of_activities($course->id));
1288 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
1289 notify("Could not cache module information for course '$course->fullname'!");
1290 }
1291 }
1292 }
1293}
1294
1295
c2cb4545 1296
1297function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 1298/// Given an empty array, this function recursively travels the
c2cb4545 1299/// categories, building up a nice list for display. It also makes
1300/// an array that list all the parents for each category.
1301
9d866ae0 1302 // initialize the arrays if needed
1303 if (!is_array($list)) {
1304 $list = array();
1305 }
1306 if (!is_array($parents)) {
1307 $parents = array();
1308 }
1309
c2cb4545 1310 if ($category) {
1311 if ($path) {
e05bcf2f 1312 $path = $path.' / '.$category->name;
c2cb4545 1313 } else {
e05bcf2f 1314 $path = $category->name;
c2cb4545 1315 }
1316 $list[$category->id] = $path;
1317 } else {
1318 $category->id = 0;
1319 }
1320
e05bcf2f 1321 if ($categories = get_categories($category->id)) { // Print all the children recursively
c2cb4545 1322 foreach ($categories as $cat) {
1323 if (!empty($category->id)) {
3bd4de22 1324 if (isset($parents[$category->id])) {
2832badf 1325 $parents[$cat->id] = $parents[$category->id];
1326 }
c2cb4545 1327 $parents[$cat->id][] = $category->id;
1328 }
89adb174 1329 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1330 }
1331 }
1332}
1333
1334
d157bd5b 1335function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $files = true) {
89adb174 1336/// Recursive function to print out all the categories in a nice format
c2cb4545 1337/// with or without courses included
9ff5310a 1338 global $CFG;
e05bcf2f 1339
1340 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1341 return;
9ff5310a 1342 }
c2cb4545 1343
1344 if (!$displaylist) {
e92fe848 1345 make_categories_list($displaylist, $parentslist);
c2cb4545 1346 }
1347
1348 if ($category) {
ec7a8b79 1349 if ($category->visible or has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
6f24e48e 1350 print_category_info($category, $depth, $files);
c2cb4545 1351 } else {
1352 return; // Don't bother printing children of invisible categories
1353 }
89adb174 1354
c2cb4545 1355 } else {
c2cb4545 1356 $category->id = "0";
1357 }
1358
1359 if ($categories = get_categories($category->id)) { // Print all the children recursively
1360 $countcats = count($categories);
1361 $count = 0;
1362 $first = true;
1363 $last = false;
1364 foreach ($categories as $cat) {
1365 $count++;
1366 if ($count == $countcats) {
1367 $last = true;
1368 }
1369 $up = $first ? false : true;
1370 $down = $last ? false : true;
1371 $first = false;
1372
6f24e48e 1373 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1, $files);
c2cb4545 1374 }
1375 }
c2cb4545 1376}
1377
0705ff84 1378// this function will return $options array for choose_from_menu, with whitespace to denote nesting.
1379
1380function make_categories_options() {
1381 make_categories_list($cats,$parents);
1382 foreach ($cats as $key => $value) {
1383 if (array_key_exists($key,$parents)) {
1384 if ($indent = count($parents[$key])) {
1385 for ($i = 0; $i < $indent; $i++) {
1386 $cats[$key] = '&nbsp;'.$cats[$key];
1387 }
1388 }
1389 }
1390 }
1391 return $cats;
1392}
c2cb4545 1393
6f24e48e 1394function print_category_info($category, $depth, $files = false) {
d2b6ba70 1395/// Prints the category info in indented fashion
1396/// This function is only used by print_whole_category_list() above
c2cb4545 1397
1398 global $CFG;
b48f834c 1399 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1400
b48f834c 1401 if (empty($strsummary)) {
e05bcf2f 1402 $strallowguests = get_string('allowguests');
1403 $strrequireskey = get_string('requireskey');
1404 $strsummary = get_string('summary');
b48f834c 1405 }
ba2e5d73 1406
e05bcf2f 1407 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1408
6f24e48e 1409 $coursecount = count_records('course') <= FRONTPAGECOURSELIMIT;
1410 if ($files and $coursecount) {
839f2456 1411 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" width="16" height="16" border="0" alt="" />';
b48f834c 1412 } else {
7b0b5c14 1413 $catimage = "&nbsp;";
8ef9cb56 1414 }
b48f834c 1415
87d32352 1416 echo "\n\n".'<table border="0" cellpadding="3" cellspacing="0" width="100%">';
d2b6ba70 1417
6f24e48e 1418 if ($files and $coursecount) {
19374e76 1419 $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 1420
978abb42 1421 echo '<tr>';
b48f834c 1422
1423 if ($depth) {
1424 $indent = $depth*30;
1425 $rows = count($courses) + 1;
1c919752 1426 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1427 print_spacer(10, $indent);
e05bcf2f 1428 echo '</td>';
b48f834c 1429 }
89adb174 1430
e05bcf2f 1431 echo '<td valign="top">'.$catimage.'</td>';
290130b3 1432 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1433 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1434 echo '</td>';
290130b3 1435 echo '<td class="category info">&nbsp;</td>';
e05bcf2f 1436 echo '</tr>';
b48f834c 1437
9ff5310a 1438 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1439 foreach ($courses as $course) {
e05bcf2f 1440 $linkcss = $course->visible ? '' : ' class="dimmed" ';
1441 echo '<tr><td valign="top" width="30">&nbsp;';
290130b3 1442 echo '</td><td valign="top" width="100%" class="course name">';
e05bcf2f 1443 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->fullname.'</a>';
290130b3 1444 echo '</td><td align="right" valign="top" nowrap="nowrap" class="course info">';
c2cb4545 1445 if ($course->guest ) {
e05bcf2f 1446 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1447 echo '<img hspace="1" alt="'.$strallowguests.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1448 } else {
e05bcf2f 1449 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1450 }
c2cb4545 1451 if ($course->password) {
e05bcf2f 1452 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
1453 echo '<img hspace="1" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1454 } else {
e05bcf2f 1455 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1456 }
1457 if ($course->summary) {
e05bcf2f 1458 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
1459 '<img hspace="1" alt="'.$strsummary.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1460 400, 500, $strsummary);
ebe8ddc1 1461 } else {
e05bcf2f 1462 echo '<img alt="" height="16" width="18" border="0" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1463 }
e05bcf2f 1464 echo '</td></tr>';
0c656181 1465 }
ba2e5d73 1466 }
d2b6ba70 1467 } else {
b48f834c 1468
e0140f24 1469 echo '<tr>';
1470
b48f834c 1471 if ($depth) {
1472 $indent = $depth*20;
e05bcf2f 1473 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1474 print_spacer(10, $indent);
e05bcf2f 1475 echo '</td>';
d2b6ba70 1476 }
89adb174 1477
290130b3 1478 echo '<td valign="top" width="100%" class="category name">';
e05bcf2f 1479 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'.$category->name.'</a>';
1480 echo '</td>';
290130b3 1481 echo '<td valign="top" class="category number">';
e05bcf2f 1482 if ($category->coursecount) {
1483 echo $category->coursecount;
1484 }
1485 echo '</td></tr>';
c2cb4545 1486 }
e05bcf2f 1487 echo '</table>';
c2cb4545 1488}
1489
c2cb4545 1490
8e227aa7 1491function print_courses($category, $width="100%", $hidesitecourse = false) {
c2cb4545 1492/// Category is 0 (for all courses) or an object
1493
810393c8 1494 global $CFG;
c2cb4545 1495
1496 if (empty($category)) {
90c2ca2e 1497 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1498 if (count($categories) == 1) {
1499 $category = array_shift($categories);
32b9a983 1500 $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 1501 } else {
32b9a983 1502 $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 1503 }
1504 unset($categories);
607809b3 1505 } else {
c2cb4545 1506 $categories = get_categories($category->id); // sub categories
32b9a983 1507 $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 1508 }
1509
c2cb4545 1510 if ($courses) {
1511 foreach ($courses as $course) {
8e227aa7 1512 if ($hidesitecourse && !$course->category) {
1513 continue;
1514 }
c2cb4545 1515 print_course($course, $width);
c2cb4545 1516 }
1517 } else {
f9667a5a 1518 print_heading(get_string("nocoursesyet"));
954fdb42 1519 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
0468976c 1520 if (has_capability('moodle/course:create', $context)) {
255d1033 1521 $options = array();
1522 $options['category'] = $category->id;
1523 echo '<div class="addcoursebutton" align="center">';
1524 print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string("addnewcourse"));
1525 echo '</div>';
1526 }
c2cb4545 1527 }
c2cb4545 1528}
1529
1530
1531function print_course($course, $width="100%") {
1532
861efb19 1533 global $CFG, $USER;
c2cb4545 1534
88768091 1535 $context = get_context_instance(CONTEXT_COURSE, $course->id);
146bbb8f 1536
88768091 1537 print_simple_box_start('center', $width, '', 5, 'coursebox');
c2cb4545 1538
88768091 1539 $linkcss = $course->visible ? '' : ' class="dimmed" ';
22288704 1540
88768091 1541 echo '<table width="100%">';
e5e81e78 1542 echo '<tr valign="top">';
290130b3 1543 echo '<td valign="top" width="50%" class="info">';
34b5847a 1544 echo '<b><a title="'.get_string('entercourse').'"'.
e5e81e78 1545 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
34b5847a 1546 $course->fullname.'</a></b><br />';
88768091 1547
1d546bb1 1548 if ($teachers = get_users_by_capability($context, 'moodle/course:update',
1549 'u.*, ul.timeaccess as lastaccess, ra.hidden',
0a3e9703 1550 'r.sortorder ASC', '','','','', false)) {
88768091 1551 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
1552 $namesarray = array();
c2cb4545 1553 foreach ($teachers as $teacher) {
88768091 1554 if (!$teacher->hidden || $canseehidden) {
1555 $roles = get_user_roles($context, $teacher->id);
1556 $role = array_shift($roles); // First one
1557 $fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context));
1558 $namesarray[] = format_string($role->name).': <a href="'.$CFG->wwwroot.'/user/view.php?id='.
1559 $teacher->id.'&amp;course='.SITEID.'">'.$fullname.'</a>';
c2cb4545 1560 }
1561 }
88768091 1562 if ($namesarray) {
1563 echo "<ul class=\"teachers\">\n<li>";
1564 echo implode('</li><li>', $namesarray);
1565 echo "</li></ul>";
1566 }
c2cb4545 1567 }
c2cb4545 1568
88768091 1569 require_once("$CFG->dirroot/enrol/enrol.class.php");
1570 $enrol = enrolment_factory::factory($course->enrol);
146bbb8f 1571 echo $enrol->get_access_icons($course);
c2cb4545 1572
290130b3 1573 echo '</td><td valign="top" width="50%" class="summary">';
9f39c190 1574 $options = NULL;
1575 $options->noclean = true;
34b5847a 1576 $options->para = false;
9f39c190 1577 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
c2cb4545 1578 echo "</td></tr>";
1579 echo "</table>";
1580
1581 print_simple_box_end();
1582}
1583
1584
1585function print_my_moodle() {
1586/// Prints custom user information on the home page.
1587/// Over time this can include all sorts of information
1588
1589 global $USER, $CFG;
1590
86a1ba04 1591 if (empty($USER->id)) {
c2cb4545 1592 error("It shouldn't be possible to see My Moodle without being logged in.");
1593 }
1594
1595 if ($courses = get_my_courses($USER->id)) {
1596 foreach ($courses as $course) {
c81696e5 1597 if (!$course->category) {
1598 continue;
1599 }
c2cb4545 1600 print_course($course, "100%");
c2cb4545 1601 }
38a10939 1602
7f989948 1603 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1604 echo "<table width=\"100%\"><tr><td align=\"center\">";
1605 print_course_search("", false, "short");
1606 echo "</td><td align=\"center\">";
1607 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1608 echo "</td></tr></table>\n";
1609 }
26330001 1610 } else {
1611 if (count_records("course_categories") > 1) {
cb29b020 1612 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1613 print_whole_category_list();
1614 print_simple_box_end();
1615 } else {
1616 print_courses(0, "100%");
1617 }
607809b3 1618 }
2b8cef80 1619}
1620
11b0c469 1621
a8b56716 1622function print_course_search($value="", $return=false, $format="plain") {
38a10939 1623
1624 global $CFG;
1625
1626 $strsearchcourses= get_string("searchcourses");
1627
1c919752 1628 if ($format == 'plain') {
87d32352 1629 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1630 $output .= '<center><p align="center" class="coursesearchbox">';
f8a5159a 1631 $output .= '<input type="text" size="30" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1632 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1633 $output .= '</p></center></form>';
1c919752 1634 } else if ($format == 'short') {
87d32352 1635 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1636 $output .= '<center><p align="center" class="coursesearchbox">';
f8a5159a 1637 $output .= '<input type="text" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1638 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
87d32352 1639 $output .= '</p></center></form>';
1c919752 1640 } else if ($format == 'navbar') {
87d32352 1641 $output = '<form name="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1642 $output .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
f8a5159a 1643 $output .= '<input type="text" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1644 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
1c919752 1645 $output .= '</td></tr></table>';
87d32352 1646 $output .= '</form>';
a8b56716 1647 }
1648
1649 if ($return) {
1650 return $output;
1651 }
1652 echo $output;
38a10939 1653}
11b0c469 1654
1655/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1656
1657function add_course_module($mod) {
11b0c469 1658
e5dfd0f3 1659 $mod->added = time();
53f4ad2c 1660 unset($mod->id);
11b0c469 1661
e5dfd0f3 1662 return insert_record("course_modules", $mod);
11b0c469 1663}
1664
7977cffd 1665function add_mod_to_section($mod, $beforemod=NULL) {
1666/// Given a full mod object with section and course already defined
1667/// If $before is specified, then this is an existing ID which we
1668/// will insert the new module before
1669///
1670/// Returns the course_sections ID where the mod is inserted
11b0c469 1671
9fa49e22 1672 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1673
1674 $section->sequence = trim($section->sequence);
1675
1676 if (empty($section->sequence)) {
11b0c469 1677 $newsequence = "$mod->coursemodule";
7977cffd 1678
1679 } else if ($beforemod) {
1680 $modarray = explode(",", $section->sequence);
1681
1682 if ($key = array_keys ($modarray, $beforemod->id)) {
1683 $insertarray = array($mod->id, $beforemod->id);
1684 array_splice($modarray, $key[0], 1, $insertarray);
1685 $newsequence = implode(",", $modarray);
1686
1687 } else { // Just tack it on the end anyway
1688 $newsequence = "$section->sequence,$mod->coursemodule";
1689 }
1690
1691 } else {
1692 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1693 }
89adb174 1694
e5dfd0f3 1695 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1696 return $section->id; // Return course_sections ID that was used.
11b0c469 1697 } else {
e5dfd0f3 1698 return 0;
11b0c469 1699 }
89adb174 1700
11b0c469 1701 } else { // Insert a new record
e5dfd0f3 1702 $section->course = $mod->course;
1703 $section->section = $mod->section;
1704 $section->summary = "";
1705 $section->sequence = $mod->coursemodule;
1706 return insert_record("course_sections", $section);
11b0c469 1707 }
1708}
1709
48e535bc 1710function set_coursemodule_groupmode($id, $groupmode) {
3d575e6f 1711 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
1712}
1713
02f66c42 1714/**
1715* $prevstateoverrides = true will set the visibility of the course module
1716* to what is defined in visibleold. This enables us to remember the current
1717* visibility when making a whole section hidden, so that when we toggle
1718* that section back to visible, we are able to return the visibility of
1719* the course module back to what it was originally.
1720*/
1721function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
978abb42 1722 if (!$cm = get_record('course_modules', 'id', $id)) {
1723 return false;
1724 }
1725 if (!$modulename = get_field('modules', 'name', 'id', $cm->module)) {
1726 return false;
1727 }
dcd338ff 1728 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1729 foreach($events as $event) {
48e535bc 1730 if ($visible) {
1731 show_event($event);
1732 } else {
1733 hide_event($event);
1734 }
dcd338ff 1735 }
1736 }
02f66c42 1737 if ($prevstateoverrides) {
1738 if ($visible == '0') {
1739 // Remember the current visible state so we can toggle this back.
1740 set_field('course_modules', 'visibleold', $cm->visible, 'id', $id);
1741 } else {
1742 // Get the previous saved visible states.
1743 return set_field('course_modules', 'visible', $cm->visibleold, 'id', $id);
1744 }
1745 }
48e535bc 1746 return set_field("course_modules", "visible", $visible, "id", $id);
1acfbce5 1747}
1748
290130b3 1749/*
1750 * Delete a course module and any associated data at the course level (events)
1751 * Until 1.5 this function simply marked a deleted flag ... now it
1752 * deletes it completely.
1753 *
1754 */
48e535bc 1755function delete_course_module($id) {
290130b3 1756 if (!$cm = get_record('course_modules', 'id', $id)) {
1757 return true;
1758 }
dcd338ff 1759 $modulename = get_field('modules', 'name', 'id', $cm->module);
1760 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
1761 foreach($events as $event) {
48e535bc 1762 delete_event($event);
dcd338ff 1763 }
1764 }
290130b3 1765 return delete_records('course_modules', 'id', $cm->id);
11b0c469 1766}
1767
1768function delete_mod_from_section($mod, $section) {
11b0c469 1769
e5dfd0f3 1770 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 1771
e5dfd0f3 1772 $modarray = explode(",", $section->sequence);
11b0c469 1773
1774 if ($key = array_keys ($modarray, $mod)) {
1775 array_splice($modarray, $key[0], 1);
1776 $newsequence = implode(",", $modarray);
e5dfd0f3 1777 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 1778 } else {
1779 return false;
1780 }
89adb174 1781
11b0c469 1782 }
7977cffd 1783 return false;
11b0c469 1784}
1785
12905134 1786function move_section($course, $section, $move) {
1787/// Moves a whole course section up and down within the course
798b70a1 1788 global $USER;
12905134 1789
1790 if (!$move) {
1791 return true;
1792 }
1793
1794 $sectiondest = $section + $move;
1795
1796 if ($sectiondest > $course->numsections or $sectiondest < 1) {
1797 return false;
1798 }
1799
1800 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
1801 return false;
1802 }
1803
1804 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
1805 return false;
1806 }
1807
56e34ee4 1808 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 1809 return false;
1810 }
56e34ee4 1811 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 1812 return false;
1813 }
798b70a1 1814 // if the focus is on the section that is being moved, then move the focus along
1815 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
1816 course_set_display($course->id, $sectiondest);
1817 }
5390cbb7 1818
a987106d 1819 // Check for duplicates and fix order if needed.
5390cbb7 1820 // There is a very rare case that some sections in the same course have the same section id.
a987106d 1821 $sections = get_records_select('course_sections', "course = $course->id", 'section ASC');
1822 $n = 0;
1823 foreach ($sections as $section) {
1824 if ($section->section != $n) {
5390cbb7 1825 if (!set_field('course_sections', 'section', $n, 'id', $section->id)) {
1826 return false;
1827 }
5390cbb7 1828 }
a987106d 1829 $n++;
5390cbb7 1830 }
12905134 1831 return true;
1832}
1833
1834
7977cffd 1835function moveto_module($mod, $section, $beforemod=NULL) {
1836/// All parameters are objects
1837/// Move the module object $mod to the specified $section
1838/// If $beforemod exists then that is the module
1839/// before which $modid should be inserted
1840
1841/// Remove original module from original section
1842
1843 if (! delete_mod_from_section($mod->id, $mod->section)) {
1844 notify("Could not delete module from existing section");
1845 }
1846
1847/// Update module itself if necessary
1848
1849 if ($mod->section != $section->id) {
89adb174 1850 $mod->section = $section->id;
7977cffd 1851 if (!update_record("course_modules", $mod)) {
1852 return false;
1853 }
48e535bc 1854 // if moving to a hidden section then hide module
1855 if (!$section->visible) {
1856 set_coursemodule_visible($mod->id, 0);
1857 }
7977cffd 1858 }
1859
1860/// Add the module into the new section
1861
1862 $mod->course = $section->course;
1863 $mod->section = $section->section; // need relative reference
1864 $mod->coursemodule = $mod->id;
1865
1866 if (! add_mod_to_section($mod, $beforemod)) {
1867 return false;
1868 }
1869
1870 return true;
1871
1872}
1873
24e1eae4 1874function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
810393c8 1875 global $CFG, $USER;
94361e02 1876
3d575e6f 1877 static $str;
37a88449 1878 static $sesskey;
3d575e6f 1879
1880 if (!isset($str)) {
1881 $str->delete = get_string("delete");
1882 $str->move = get_string("move");
1883 $str->moveup = get_string("moveup");
1884 $str->movedown = get_string("movedown");
1885 $str->moveright = get_string("moveright");
1886 $str->moveleft = get_string("moveleft");
1887 $str->update = get_string("update");
d33a2a6f 1888 $str->duplicate = get_string("duplicate");
3d575e6f 1889 $str->hide = get_string("hide");
1890 $str->show = get_string("show");
1891 $str->clicktochange = get_string("clicktochange");
32d03b7b 1892 $str->forcedmode = get_string("forcedmode");
3d575e6f 1893 $str->groupsnone = get_string("groupsnone");
1894 $str->groupsseparate = get_string("groupsseparate");
1895 $str->groupsvisible = get_string("groupsvisible");
37a88449 1896 $sesskey = sesskey();
1acfbce5 1897 }
94361e02 1898
24e1eae4 1899 if ($section >= 0) {
75f087b6 1900 $section = '&amp;sr='.$section; // Section return
24e1eae4 1901 } else {
1902 $section = '';
1903 }
1904
94361e02 1905 if ($absolute) {
37a88449 1906 $path = $CFG->wwwroot.'/course';
dc0dc7d5 1907 } else {
37a88449 1908 $path = '.';
dc0dc7d5 1909 }
1910
3d575e6f 1911 if ($mod->visible) {
37a88449 1912 $hideshow = '<a title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id.
1913 '&amp;sesskey='.$sesskey.$section.'"><img'.
1914 ' src="'.$CFG->pixpath.'/t/hide.gif" hspace="2" height="11" width="11" '.
1915 ' border="0" alt="'.$str->hide.'" /></a> ';
1acfbce5 1916 } else {
37a88449 1917 $hideshow = '<a title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id.
1918 '&amp;sesskey='.$sesskey.$section.'"><img'.
1919 ' src="'.$CFG->pixpath.'/t/show.gif" hspace="2" height="11" width="11" '.
1920 ' border="0" alt="'.$str->show.'" /></a> ';
7977cffd 1921 }
3d575e6f 1922 if ($mod->groupmode !== false) {
1923 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 1924 $grouptitle = $str->groupsseparate;
37a88449 1925 $groupimage = $CFG->pixpath.'/t/groups.gif';
1926 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=0&amp;sesskey='.$sesskey;
3d575e6f 1927 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 1928 $grouptitle = $str->groupsvisible;
37a88449 1929 $groupimage = $CFG->pixpath.'/t/groupv.gif';
1930 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=1&amp;sesskey='.$sesskey;
32d03b7b 1931 } else {
1932 $grouptitle = $str->groupsnone;
37a88449 1933 $groupimage = $CFG->pixpath.'/t/groupn.gif';
1934 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=2&amp;sesskey='.$sesskey;
32d03b7b 1935 }
1936 if ($mod->groupmodelink) {
37a88449 1937 $groupmode = '<a title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'.
1938 '<img src="'.$groupimage.'" hspace="2" height="11" width="11" '.
1939 'border="0" alt="'.$grouptitle.'" /></a>';
3d575e6f 1940 } else {
37a88449 1941 $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '.
1942 ' src="'.$groupimage.'" hspace="2" height="11" width="11" '.
1943 'border="0" alt="'.$grouptitle.'" />';
3d575e6f 1944 }
1945 } else {
1946 $groupmode = "";
1947 }
7977cffd 1948
1949 if ($moveselect) {
37a88449 1950 $move = '<a title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id.
1951 '&amp;sesskey='.$sesskey.$section.'"><img'.
1952 ' src="'.$CFG->pixpath.'/t/move.gif" hspace="2" height="11" width="11" '.
1953 ' border="0" alt="'.$str->move.'" /></a>';
493486c4 1954 } else {
37a88449 1955 $move = '<a title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id.
1956 '&amp;move=-1&amp;sesskey='.$sesskey.$section.'"><img'.
1957 ' src="'.$CFG->pixpath.'/t/up.gif" hspace="2" height="11" width="11" '.
1958 ' border="0" alt="'.$str->moveup.'" /></a>'.
1959 '<a title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id.
1960 '&amp;move=1&amp;sesskey='.$sesskey.$section.'"><img'.
1961 ' src="'.$CFG->pixpath.'/t/down.gif" hspace="2" height="11" width="11" '.
1962 ' border="0" alt="'.$str->movedown.'" /></a>';
7977cffd 1963 }
1964
aac94fd0 1965 $leftright = "";
1966 if ($indent > 0) {
37a88449 1967 $leftright .= '<a title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id.
1968 '&amp;indent=-1&amp;sesskey='.$sesskey.$section.'"><img'.
1969 ' src="'.$CFG->pixpath.'/t/left.gif" hspace="2" height="11" width="11" '.
1970 ' border="0" alt="'.$str->moveleft.'" /></a>';
aac94fd0 1971 }
1972 if ($indent >= 0) {
37a88449 1973 $leftright .= '<a title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id.
1974 '&amp;indent=1&amp;sesskey='.$sesskey.$section.'"><img'.
1975 ' src="'.$CFG->pixpath.'/t/right.gif" hspace="2" height="11" width="11" '.
1976 ' border="0" alt="'.$str->moveright.'" /></a>';
1977 }
1978
1979 return '<span class="commands">'.$leftright.$move.
1980 '<a title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id.
1981 '&amp;sesskey='.$sesskey.$section.'"><img'.
1982 ' src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" '.
1983 ' alt="'.$str->update.'" /></a>'.
1984 '<a title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id.
1985 '&amp;sesskey='.$sesskey.$section.'"><img'.
1986 ' src="'.$CFG->pixpath.'/t/delete.gif" hspace="2" height="11" width="11" border="0" '.
1987 ' alt="'.$str->delete.'" /></a>'.$hideshow.$groupmode.'</span>';
90845098 1988}
1989
b61efafb 1990/**
1991 * given a course object with shortname & fullname, this function will
1992 * truncate the the number of chars allowed and add ... if it was too long
1993 */
1994function course_format_name ($course,$max=100) {
1995
1996 $str = $course->shortname.': '.$course->fullname;
1997 if (strlen($str) <= $max) {
1998 return $str;
1999 }
2000 else {
2001 return substr($str,0,$max-3).'...';
2002 }
2003}
2004
2005/**
2006 * This function will return true if the given course is a child course at all
2007 */
2008function course_in_meta ($course) {
5f37b628 2009 return record_exists("course_meta","child_course",$course->id);
b61efafb 2010}
2011
48e535bc 2012
2013/**
2014 * Print standard form elements on module setup forms in mod/.../mod.html
2015 */
2016function print_standard_coursemodule_settings($form) {
da2224f8 2017 if (! $course = get_record('course', 'id', $form->course)) {
2018 error("This course doesn't exist");
2019 }
2020 print_groupmode_setting($form, $course);
2021 print_visible_setting($form, $course);
48e535bc 2022}
2023
2024/**
2025 * Print groupmode form element on module setup forms in mod/.../mod.html
2026 */
5ebb746b 2027function print_groupmode_setting($form, $course=NULL) {
48e535bc 2028
5ebb746b 2029 if (empty($course)) {
2030 if (! $course = get_record('course', 'id', $form->course)) {
2031 error("This course doesn't exist");
2032 }
2033 }
48e535bc 2034 if ($form->coursemodule) {
2035 if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
2036 error("This course module doesn't exist");
2037 }
2038 } else {
2039 $cm = null;
2040 }
2041 $groupmode = groupmode($course, $cm);
2042 if ($course->groupmode or (!$course->groupmodeforce)) {
2043 echo '<tr valign="top">';
2044 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
7bbe08a2 2045 echo '<td align="left">';
48e535bc 2046 unset($choices);
2047 $choices[NOGROUPS] = get_string('groupsnone');
2048 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
2049 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
2050 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
2051 helpbutton('groupmode', get_string('groupmode'));
2052 echo '</td></tr>';
2053 }
2054}
2055
2056/**
2057 * Print visibility setting form element on module setup forms in mod/.../mod.html
2058 */
5ebb746b 2059function print_visible_setting($form, $course=NULL) {
1ee55c41 2060 if (empty($course)) {
2061 if (! $course = get_record('course', 'id', $form->course)) {
2062 error("This course doesn't exist");
2063 }
2064 }
48e535bc 2065 if ($form->coursemodule) {
2066 $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
2067 } else {
2068 $visible = true;
2069 }
2070
2071 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
2072 $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
2073 } else {
2074 $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
2075 }
2076 if ($hiddensection) {
2077 $visible = false;
2078 }
2079
2080 echo '<tr valign="top">';
182311e4 2081 echo '<td align="right"><b>'.get_string('visible', '').':</b></td>';
7bbe08a2 2082 echo '<td align="left">';
48e535bc 2083 unset($choices);
1f15db9d 2084 $choices[1] = get_string('show');
2085 $choices[0] = get_string('hide');
48e535bc 2086 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
2087 echo '</td></tr>';
2088}
2089
0705ff84 2090function update_restricted_mods($course,$mods) {
2091 delete_records("course_allowed_modules","course",$course->id);
2092 if (empty($course->restrictmodules)) {
2093 return;
2094 }
2095 else {
2096 foreach ($mods as $mod) {
2097 if ($mod == 0)
2098 continue; // this is the 'allow none' option
2099 $am->course = $course->id;
2100 $am->module = $mod;
2101 insert_record("course_allowed_modules",$am);
2102 }
2103 }
2104}
2105
2106/**
2107 * This function will take an int (module id) or a string (module name)
2108 * and return true or false, whether it's allowed in the given course (object)
2109 * $mod is not allowed to be an object, as the field for the module id is inconsistent
2110 * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
2111 */
2112
2113function course_allowed_module($course,$mod) {
2114 if (empty($course->restrictmodules)) {
2115 return true;
2116 }
51792df0 2117
2118 // i am not sure this capability is correct
2119 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
0705ff84 2120 return true;
2121 }
2122 if (is_numeric($mod)) {
2123 $modid = $mod;
2124 } else if (is_string($mod)) {
cb77cf12 2125 if ($mod = get_field("modules","id","name",$mod))
0705ff84 2126 $modid = $mod;
2127 }
2128 if (empty($modid)) {
2129 return false;
2130 }
2131 return (record_exists("course_allowed_modules","course",$course->id,"module",$modid));
2132}
2133
861efb19 2134/***
2135 *** Efficiently moves many courses around while maintaining
2136 *** sortorder in order.
2137 ***
2138 *** $courseids is an array of course ids
2139 ***
2140 **/
2141
2142function move_courses ($courseids, $categoryid) {
2143
2144 global $CFG;
2145
2146 if (!empty($courseids)) {
2147
2148 $courseids = array_reverse($courseids);
2149
2150 foreach ($courseids as $courseid) {
2151
2152 if (! $course = get_record("course", "id", $courseid)) {
2153 notify("Error finding course $courseid");
2154 } else {
2155 // figure out a sortorder that we can use in the destination category
2156 $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
2157 FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid);
2158 if ($sortorder === false) {
2159 // the category is empty
2160 // rather than let the db default to 0
2161 // set it to > 100 and avoid extra work in fix_coursesortorder()
2162 $sortorder = 200;
2163 } else if ($sortorder < 10) {
2164 fix_course_sortorder($categoryid);
2165 }
2166
2167 $course->category = $categoryid;
2168 $course->sortorder = $sortorder;
2169 $course->fullname = addslashes($course->fullname);
2170 $course->shortname = addslashes($course->shortname);
2171 $course->summary = addslashes($course->summary);
2172 $course->password = addslashes($course->password);
2173 $course->teacher = addslashes($course->teacher);
2174 $course->teachers = addslashes($course->teachers);
2175 $course->student = addslashes($course->student);
2176 $course->students = addslashes($course->students);
2177
2178 if (!update_record('course', $course)) {
2179 notify("An error occurred - course not moved!");
2180 }
2181 }
2182 }
2183 fix_course_sortorder();
2184 }
2185 return true;
2186}
2187
b63ec9db 2188?>