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