mnet: introducing print_remote_course() and use it in print_my_moodle()
[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
5577ceb3 469 echo "<div class=\"info\">\n";
519d369f 470 print_string("displayingrecords", "", $totalcount);
5577ceb3 471 echo "</div>\n";
1c0200e0 472
8f0cd6ef 473 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
519d369f 474
5577ceb3 475 echo "<table class=\"logtable\" cellpadding=\"3\" cellspacing=\"0\" summary=\"\">\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) {
5577ceb3 521 echo "<td class=\"r$row c0\">\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 }
5577ceb3 525 echo "<td class=\"r$row c1\" align=\"right\">".userdate($log->time, '%a').
21283ddc 526 ' '.userdate($log->time, $strftimedatetime)."</td>\n";
5577ceb3 527 echo "<td class=\"r$row c2\">\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)));
5577ceb3 531 echo "<td class=\"r$row c3\">\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";
5577ceb3 534 echo "<td class=\"r$row c4\">\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";;
5577ceb3 537 echo "<td class=\"r$row c5\">{$log->info}</td>\n";
21283ddc 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
5577ceb3 575 echo "<div class=\"info\">\n";
c215b32b 576 print_string("displayingrecords", "", $totalcount);
5577ceb3 577 echo "</div>\n";
c215b32b 578
579 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
580
5577ceb3 581 echo "<table class=\"logtable\" 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) {
5577ceb3 628 echo "<td class=\"r$row c0\" >\n";
c215b32b 629 echo " <a href=\"{$CFG->wwwroot}/course/view.php?id={$log->course}\">".$courses[$log->course]."</a>\n";
630 echo "</td>\n";
631 }
5577ceb3 632 echo "<td class=\"r$row c1\" align=\"right\">".userdate($log->time, '%a').
c215b32b 633 ' '.userdate($log->time, $strftimedatetime)."</td>\n";
5577ceb3 634 echo "<td class=\"r$row c2\" >\n";
c215b32b 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)));
5577ceb3 638 echo "<td class=\"r$row c3\" >\n";
c215b32b 639 echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}\">$fullname</a>\n";
640 echo "</td>\n";
5577ceb3 641 echo "<td class=\"r$row c4\">\n";
c215b32b 642 echo $log->action .': '.$log->module;
643 echo "</td>\n";;
5577ceb3 644 echo "<td class=\"r$row c5\">{$log->info}</td>\n";
c215b32b 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)) {
86dd62a7 1739 error_log(1);
90c2ca2e 1740 $categories = get_categories(0); // Parent = 0 ie top-level categories only
1741 if (count($categories) == 1) {
1742 $category = array_shift($categories);
32b9a983 1743 $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 1744 } else {
32b9a983 1745 $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 1746 }
1747 unset($categories);
607809b3 1748 } else {
86dd62a7 1749error_log(2);
c2cb4545 1750 $categories = get_categories($category->id); // sub categories
32b9a983 1751 $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 1752 }
1753
c2cb4545 1754 if ($courses) {
1755 foreach ($courses as $course) {
1936c10e 1756 if ($hidesitecourse and ($course->id == SITEID)) {
8e227aa7 1757 continue;
1758 }
c2cb4545 1759 print_course($course, $width);
c2cb4545 1760 }
1761 } else {
f9667a5a 1762 print_heading(get_string("nocoursesyet"));
954fdb42 1763 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
0468976c 1764 if (has_capability('moodle/course:create', $context)) {
255d1033 1765 $options = array();
1766 $options['category'] = $category->id;
6b7425d2 1767 echo '<div class="addcoursebutton">';
255d1033 1768 print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string("addnewcourse"));
1769 echo '</div>';
1770 }
c2cb4545 1771 }
86dd62a7 1772
1773
1774
c2cb4545 1775}
1776
1777
1778function print_course($course, $width="100%") {
1779
861efb19 1780 global $CFG, $USER;
c2cb4545 1781
88768091 1782 $context = get_context_instance(CONTEXT_COURSE, $course->id);
146bbb8f 1783
88768091 1784 $linkcss = $course->visible ? '' : ' class="dimmed" ';
22288704 1785
afba7be1 1786 echo '<div class="coursebox">';
1787 echo '<div class="info">';
1788 echo '<div class="name"><a title="'.get_string('entercourse').'"'.
e5e81e78 1789 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
afba7be1 1790 $course->fullname.'</a></div>';
b06334e8 1791
d42c64ba 1792 /// first find all roles that are supposed to be displayed
1793 if ($managerroles = get_config('', 'coursemanager')) {
6480b0da 1794 $coursemanagerroles = split(',', $managerroles);
d42c64ba 1795 foreach ($coursemanagerroles as $roleid) {
1796 $role = get_record('role','id',$roleid);
1797 if ($users = get_role_users($roleid, $context, true, '', 'u.lastname ASC', true)) {
1798 foreach ($users as $teacher) {
1799 $fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context));
1800 $namesarray[] = format_string($role->name).': <a href="'.$CFG->wwwroot.'/user/view.php?id='.
b06334e8 1801 $teacher->id.'&amp;course='.SITEID.'">'.$fullname.'</a>';
d42c64ba 1802 }
b06334e8 1803 }
c2cb4545 1804 }
d42c64ba 1805
1806 if (!empty($namesarray)) {
88768091 1807 echo "<ul class=\"teachers\">\n<li>";
1808 echo implode('</li><li>', $namesarray);
1809 echo "</li></ul>";
1810 }
c2cb4545 1811 }
d42c64ba 1812
88768091 1813 require_once("$CFG->dirroot/enrol/enrol.class.php");
1814 $enrol = enrolment_factory::factory($course->enrol);
146bbb8f 1815 echo $enrol->get_access_icons($course);
c2cb4545 1816
afba7be1 1817 echo '</div><div class="summary">';
9f39c190 1818 $options = NULL;
1819 $options->noclean = true;
34b5847a 1820 $options->para = false;
9f39c190 1821 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
afba7be1 1822 echo '</div>';
1823 echo '</div>';
1824 echo '<div class="clearer"></div>';
c2cb4545 1825}
1826
1827
1828function print_my_moodle() {
1829/// Prints custom user information on the home page.
1830/// Over time this can include all sorts of information
1831
1832 global $USER, $CFG;
1833
86a1ba04 1834 if (empty($USER->id)) {
c2cb4545 1835 error("It shouldn't be possible to see My Moodle without being logged in.");
1836 }
1837
86dd62a7 1838 $courses = get_my_courses($USER->id);
1839 $rhosts = array();
1840 $rcourses = array();
1841 if ($CFG->mnet_dispatcher_mode === 'strict') {
1842 $rcourses = get_my_remotecourses($USER->id);
1843 $rhosts = get_my_remotehosts($USER->id);
1844 }
1845
1846 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
1847
1848 if (!empty($courses)) {
1849 foreach ($courses as $course) {
1850 if ($course->id == SITEID) {
1851 continue;
1852 }
1853 print_course($course, "100%");
1854 }
1855 }
1856
1857 // MNET
1858 if (!empty($rcourses)) {
1859 // at the IDP, we know of all the remote courses
1860 foreach ($rcourses as $course) {
1861 print_remote_course($course, "100%");
1862 }
1863 } elseif (!empty($rhosts)) {
1864 // non-IDP, we know of all the remote servers, but not courses
1865 foreach ($rhosts as $host) {
1866 // print_remote_host($host, "100%");
c81696e5 1867 }
c2cb4545 1868 }
86dd62a7 1869 unset($course);
1870 unset($host);
38a10939 1871
7f989948 1872 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1873 echo "<table width=\"100%\"><tr><td align=\"center\">";
1874 print_course_search("", false, "short");
1875 echo "</td><td align=\"center\">";
1876 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1877 echo "</td></tr></table>\n";
1878 }
86dd62a7 1879
26330001 1880 } else {
1881 if (count_records("course_categories") > 1) {
cb29b020 1882 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1883 print_whole_category_list();
1884 print_simple_box_end();
1885 } else {
1886 print_courses(0, "100%");
1887 }
607809b3 1888 }
2b8cef80 1889}
1890
11b0c469 1891
a8b56716 1892function print_course_search($value="", $return=false, $format="plain") {
38a10939 1893
1894 global $CFG;
1895
1896 $strsearchcourses= get_string("searchcourses");
1897
1c919752 1898 if ($format == 'plain') {
b7dc2256 1899 $output = '<form id="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
fcf9577a 1900 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
f8a5159a 1901 $output .= '<input type="text" size="30" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1902 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
fcf9577a 1903 $output .= '</fieldset></form>';
1c919752 1904 } else if ($format == 'short') {
b7dc2256 1905 $output = '<form id="coursesearch" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
fcf9577a 1906 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
f8a5159a 1907 $output .= '<input type="text" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1908 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
fcf9577a 1909 $output .= '</fieldset></form>';
1c919752 1910 } else if ($format == 'navbar') {
fcf9577a 1911 $output = '<form id="coursesearchnavbar" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1912 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
f8a5159a 1913 $output .= '<input type="text" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1914 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
fcf9577a 1915 $output .= '</fieldset></form>';
a8b56716 1916 }
1917
1918 if ($return) {
1919 return $output;
1920 }
1921 echo $output;
38a10939 1922}
11b0c469 1923
86dd62a7 1924function print_remote_course($course, $width="100%") {
1925
1926 global $CFG, $USER;
1927
1928 $linkcss = '';
1929
1930 $url = "{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}";
1931
1932 echo '<div class="coursebox">';
1933 echo '<div class="info">';
1934 echo '<div class="name"><a title="'.get_string('entercourse').'"'.
1935 $linkcss.' href="'.$url.'">'
1936 . s($course->fullname) .'</a><br />'
1937 . s($course->hostname) . ' : '
1938 . s($course->cat_name) . ' : '
1939 . s($course->shortname). '</div>';
1940 echo '</div><div class="summary">';
1941 $options = NULL;
1942 $options->noclean = true;
1943 $options->para = false;
1944 echo format_text($course->summary, FORMAT_MOODLE, $options);
1945 echo '</div>';
1946 echo '</div>';
1947 echo '<div class="clearer"></div>';
1948}
1949
1950
11b0c469 1951/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1952
1953function add_course_module($mod) {
11b0c469 1954
e5dfd0f3 1955 $mod->added = time();
53f4ad2c 1956 unset($mod->id);
11b0c469 1957
e5dfd0f3 1958 return insert_record("course_modules", $mod);
11b0c469 1959}
1960
7977cffd 1961function add_mod_to_section($mod, $beforemod=NULL) {
1962/// Given a full mod object with section and course already defined
1963/// If $before is specified, then this is an existing ID which we
1964/// will insert the new module before
1965///
1966/// Returns the course_sections ID where the mod is inserted
11b0c469 1967
9fa49e22 1968 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 1969
1970 $section->sequence = trim($section->sequence);
1971
1972 if (empty($section->sequence)) {
11b0c469 1973 $newsequence = "$mod->coursemodule";
7977cffd 1974
1975 } else if ($beforemod) {
1976 $modarray = explode(",", $section->sequence);
1977
1978 if ($key = array_keys ($modarray, $beforemod->id)) {
1979 $insertarray = array($mod->id, $beforemod->id);
1980 array_splice($modarray, $key[0], 1, $insertarray);
1981 $newsequence = implode(",", $modarray);
1982
1983 } else { // Just tack it on the end anyway
1984 $newsequence = "$section->sequence,$mod->coursemodule";
1985 }
1986
1987 } else {
1988 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 1989 }
89adb174 1990
e5dfd0f3 1991 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
1992 return $section->id; // Return course_sections ID that was used.
11b0c469 1993 } else {
e5dfd0f3 1994 return 0;
11b0c469 1995 }
89adb174 1996
11b0c469 1997 } else { // Insert a new record
e5dfd0f3 1998 $section->course = $mod->course;
1999 $section->section = $mod->section;
2000 $section->summary = "";
2001 $section->sequence = $mod->coursemodule;
2002 return insert_record("course_sections", $section);
11b0c469 2003 }
2004}
2005
48e535bc 2006function set_coursemodule_groupmode($id, $groupmode) {
3d575e6f 2007 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
2008}
2009
02f66c42 2010/**
2011* $prevstateoverrides = true will set the visibility of the course module
2012* to what is defined in visibleold. This enables us to remember the current
2013* visibility when making a whole section hidden, so that when we toggle
2014* that section back to visible, we are able to return the visibility of
2015* the course module back to what it was originally.
2016*/
2017function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
978abb42 2018 if (!$cm = get_record('course_modules', 'id', $id)) {
2019 return false;
2020 }
2021 if (!$modulename = get_field('modules', 'name', 'id', $cm->module)) {
2022 return false;
2023 }
dcd338ff 2024 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
2025 foreach($events as $event) {
48e535bc 2026 if ($visible) {
2027 show_event($event);
2028 } else {
2029 hide_event($event);
2030 }
dcd338ff 2031 }
2032 }
02f66c42 2033 if ($prevstateoverrides) {
2034 if ($visible == '0') {
2035 // Remember the current visible state so we can toggle this back.
2036 set_field('course_modules', 'visibleold', $cm->visible, 'id', $id);
2037 } else {
2038 // Get the previous saved visible states.
2039 return set_field('course_modules', 'visible', $cm->visibleold, 'id', $id);
2040 }
2041 }
48e535bc 2042 return set_field("course_modules", "visible", $visible, "id", $id);
1acfbce5 2043}
2044
290130b3 2045/*
2046 * Delete a course module and any associated data at the course level (events)
264867fd 2047 * Until 1.5 this function simply marked a deleted flag ... now it
290130b3 2048 * deletes it completely.
2049 *
2050 */
48e535bc 2051function delete_course_module($id) {
290130b3 2052 if (!$cm = get_record('course_modules', 'id', $id)) {
2053 return true;
2054 }
dcd338ff 2055 $modulename = get_field('modules', 'name', 'id', $cm->module);
2056 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
2057 foreach($events as $event) {
48e535bc 2058 delete_event($event);
dcd338ff 2059 }
2060 }
290130b3 2061 return delete_records('course_modules', 'id', $cm->id);
11b0c469 2062}
2063
2064function delete_mod_from_section($mod, $section) {
11b0c469 2065
e5dfd0f3 2066 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 2067
e5dfd0f3 2068 $modarray = explode(",", $section->sequence);
11b0c469 2069
2070 if ($key = array_keys ($modarray, $mod)) {
2071 array_splice($modarray, $key[0], 1);
2072 $newsequence = implode(",", $modarray);
e5dfd0f3 2073 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 2074 } else {
2075 return false;
2076 }
89adb174 2077
11b0c469 2078 }
7977cffd 2079 return false;
11b0c469 2080}
2081
12905134 2082function move_section($course, $section, $move) {
2083/// Moves a whole course section up and down within the course
798b70a1 2084 global $USER;
12905134 2085
2086 if (!$move) {
2087 return true;
2088 }
2089
2090 $sectiondest = $section + $move;
2091
2092 if ($sectiondest > $course->numsections or $sectiondest < 1) {
2093 return false;
2094 }
2095
2096 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
2097 return false;
2098 }
2099
2100 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
2101 return false;
2102 }
2103
56e34ee4 2104 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 2105 return false;
2106 }
56e34ee4 2107 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 2108 return false;
2109 }
798b70a1 2110 // if the focus is on the section that is being moved, then move the focus along
2111 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
2112 course_set_display($course->id, $sectiondest);
2113 }
5390cbb7 2114
a987106d 2115 // Check for duplicates and fix order if needed.
5390cbb7 2116 // There is a very rare case that some sections in the same course have the same section id.
a987106d 2117 $sections = get_records_select('course_sections', "course = $course->id", 'section ASC');
2118 $n = 0;
2119 foreach ($sections as $section) {
2120 if ($section->section != $n) {
5390cbb7 2121 if (!set_field('course_sections', 'section', $n, 'id', $section->id)) {
2122 return false;
2123 }
5390cbb7 2124 }
a987106d 2125 $n++;
5390cbb7 2126 }
12905134 2127 return true;
2128}
2129
2130
7977cffd 2131function moveto_module($mod, $section, $beforemod=NULL) {
2132/// All parameters are objects
2133/// Move the module object $mod to the specified $section
2134/// If $beforemod exists then that is the module
2135/// before which $modid should be inserted
2136
2137/// Remove original module from original section
2138
2139 if (! delete_mod_from_section($mod->id, $mod->section)) {
2140 notify("Could not delete module from existing section");
2141 }
2142
2143/// Update module itself if necessary
2144
2145 if ($mod->section != $section->id) {
89adb174 2146 $mod->section = $section->id;
7977cffd 2147 if (!update_record("course_modules", $mod)) {
2148 return false;
2149 }
48e535bc 2150 // if moving to a hidden section then hide module
2151 if (!$section->visible) {
2152 set_coursemodule_visible($mod->id, 0);
2153 }
7977cffd 2154 }
2155
2156/// Add the module into the new section
2157
2158 $mod->course = $section->course;
2159 $mod->section = $section->section; // need relative reference
2160 $mod->coursemodule = $mod->id;
2161
2162 if (! add_mod_to_section($mod, $beforemod)) {
2163 return false;
2164 }
2165
2166 return true;
2167
2168}
2169
24e1eae4 2170function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
810393c8 2171 global $CFG, $USER;
94361e02 2172
3d575e6f 2173 static $str;
37a88449 2174 static $sesskey;
3d575e6f 2175
2176 if (!isset($str)) {
90ebdf65 2177 $str->delete = get_string("delete");
2178 $str->move = get_string("move");
2179 $str->moveup = get_string("moveup");
2180 $str->movedown = get_string("movedown");
2181 $str->moveright = get_string("moveright");
2182 $str->moveleft = get_string("moveleft");
2183 $str->update = get_string("update");
2184 $str->duplicate = get_string("duplicate");
2185 $str->hide = get_string("hide");
2186 $str->show = get_string("show");
3d575e6f 2187 $str->clicktochange = get_string("clicktochange");
32d03b7b 2188 $str->forcedmode = get_string("forcedmode");
3d575e6f 2189 $str->groupsnone = get_string("groupsnone");
2190 $str->groupsseparate = get_string("groupsseparate");
2191 $str->groupsvisible = get_string("groupsvisible");
37a88449 2192 $sesskey = sesskey();
1acfbce5 2193 }
94361e02 2194
24e1eae4 2195 if ($section >= 0) {
75f087b6 2196 $section = '&amp;sr='.$section; // Section return
24e1eae4 2197 } else {
2198 $section = '';
2199 }
2200
94361e02 2201 if ($absolute) {
37a88449 2202 $path = $CFG->wwwroot.'/course';
dc0dc7d5 2203 } else {
37a88449 2204 $path = '.';
dc0dc7d5 2205 }
2206
3d575e6f 2207 if ($mod->visible) {
90ebdf65 2208 $hideshow = '<a class="editing_hide" title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id.
37a88449 2209 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2210 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" '.
2211 ' alt="'.$str->hide.'" /></a>'."\n";
1acfbce5 2212 } else {
90ebdf65 2213 $hideshow = '<a class="editing_show" title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id.
37a88449 2214 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2215 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" '.
2216 ' alt="'.$str->show.'" /></a>'."\n";
7977cffd 2217 }
3d575e6f 2218 if ($mod->groupmode !== false) {
2219 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 2220 $grouptitle = $str->groupsseparate;
90ebdf65 2221 $groupclass = 'editing_groupseparate';
37a88449 2222 $groupimage = $CFG->pixpath.'/t/groups.gif';
2223 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=0&amp;sesskey='.$sesskey;
3d575e6f 2224 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 2225 $grouptitle = $str->groupsvisible;
90ebdf65 2226 $groupclass = 'editing_groupvisible';
37a88449 2227 $groupimage = $CFG->pixpath.'/t/groupv.gif';
2228 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=1&amp;sesskey='.$sesskey;
32d03b7b 2229 } else {
2230 $grouptitle = $str->groupsnone;
90ebdf65 2231 $groupclass = 'editing_groupsnone';
37a88449 2232 $groupimage = $CFG->pixpath.'/t/groupn.gif';
2233 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=2&amp;sesskey='.$sesskey;
32d03b7b 2234 }
2235 if ($mod->groupmodelink) {
90ebdf65 2236 $groupmode = '<a class="'.$groupclass.'" title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'.
0d905d9f 2237 '<img src="'.$groupimage.'" class="iconsmall" '.
2238 'alt="'.$grouptitle.'" /></a>';
3d575e6f 2239 } else {
37a88449 2240 $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '.
0d905d9f 2241 ' src="'.$groupimage.'" class="iconsmall" '.
2242 'alt="'.$grouptitle.'" />';
3d575e6f 2243 }
2244 } else {
2245 $groupmode = "";
2246 }
7977cffd 2247
2248 if ($moveselect) {
90ebdf65 2249 $move = '<a class="editing_move" title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id.
37a88449 2250 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2251 ' src="'.$CFG->pixpath.'/t/move.gif" class="iconsmall" '.
2252 ' alt="'.$str->move.'" /></a>'."\n";
493486c4 2253 } else {
90ebdf65 2254 $move = '<a class="editing_moveup" title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2255 '&amp;move=-1&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2256 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" '.
2257 ' alt="'.$str->moveup.'" /></a>'."\n".
90ebdf65 2258 '<a class="editing_movedown" title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2259 '&amp;move=1&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2260 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" '.
2261 ' alt="'.$str->movedown.'" /></a>'."\n";
7977cffd 2262 }
2263
aac94fd0 2264 $leftright = "";
2265 if ($indent > 0) {
90ebdf65 2266 $leftright .= '<a class="editing_moveleft" title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2267 '&amp;indent=-1&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2268 ' src="'.$CFG->pixpath.'/t/left.gif" class="iconsmall" '.
2269 ' alt="'.$str->moveleft.'" /></a>'."\n";
aac94fd0 2270 }
2271 if ($indent >= 0) {
90ebdf65 2272 $leftright .= '<a class="editing_moveright" title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id.
37a88449 2273 '&amp;indent=1&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2274 ' src="'.$CFG->pixpath.'/t/right.gif" class="iconsmall" '.
2275 ' alt="'.$str->moveright.'" /></a>'."\n";
37a88449 2276 }
2277
90ebdf65 2278 return '<span class="commands">'."\n".$leftright.$move.
2279 '<a class="editing_update" title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id.
37a88449 2280 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2281 ' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" '.
90ebdf65 2282 ' alt="'.$str->update.'" /></a>'."\n".
2283 '<a class="editing_delete" title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id.
37a88449 2284 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2285 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" '.
90ebdf65 2286 ' alt="'.$str->delete.'" /></a>'."\n".$hideshow.$groupmode."\n".'</span>';
90845098 2287}
2288
b61efafb 2289/**
264867fd 2290 * given a course object with shortname & fullname, this function will
b61efafb 2291 * truncate the the number of chars allowed and add ... if it was too long
2292 */
2293function course_format_name ($course,$max=100) {
264867fd 2294
b61efafb 2295 $str = $course->shortname.': '.$course->fullname;
2296 if (strlen($str) <= $max) {
2297 return $str;
2298 }
2299 else {
2300 return substr($str,0,$max-3).'...';
2301 }
2302}
2303
2304/**
2305 * This function will return true if the given course is a child course at all
2306 */
2307function course_in_meta ($course) {
5f37b628 2308 return record_exists("course_meta","child_course",$course->id);
b61efafb 2309}
2310
48e535bc 2311
2312/**
2313 * Print standard form elements on module setup forms in mod/.../mod.html
2314 */
2315function print_standard_coursemodule_settings($form) {
da2224f8 2316 if (! $course = get_record('course', 'id', $form->course)) {
2317 error("This course doesn't exist");
2318 }
2319 print_groupmode_setting($form, $course);
2320 print_visible_setting($form, $course);
48e535bc 2321}
2322
2323/**
2324 * Print groupmode form element on module setup forms in mod/.../mod.html
2325 */
5ebb746b 2326function print_groupmode_setting($form, $course=NULL) {
48e535bc 2327
5ebb746b 2328 if (empty($course)) {
2329 if (! $course = get_record('course', 'id', $form->course)) {
2330 error("This course doesn't exist");
2331 }
2332 }
48e535bc 2333 if ($form->coursemodule) {
2334 if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
2335 error("This course module doesn't exist");
2336 }
2337 } else {
2338 $cm = null;
2339 }
2340 $groupmode = groupmode($course, $cm);
2341 if ($course->groupmode or (!$course->groupmodeforce)) {
2342 echo '<tr valign="top">';
2343 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
7bbe08a2 2344 echo '<td align="left">';
48e535bc 2345 unset($choices);
2346 $choices[NOGROUPS] = get_string('groupsnone');
2347 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
2348 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
2349 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
2350 helpbutton('groupmode', get_string('groupmode'));
2351 echo '</td></tr>';
2352 }
2353}
2354
2355/**
2356 * Print visibility setting form element on module setup forms in mod/.../mod.html
2357 */
5ebb746b 2358function print_visible_setting($form, $course=NULL) {
1ee55c41 2359 if (empty($course)) {
2360 if (! $course = get_record('course', 'id', $form->course)) {
2361 error("This course doesn't exist");
2362 }
2363 }
48e535bc 2364 if ($form->coursemodule) {
2365 $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
2366 } else {
2367 $visible = true;
2368 }
2369
2370 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
2371 $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
2372 } else {
2373 $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
2374 }
2375 if ($hiddensection) {
2376 $visible = false;
2377 }
264867fd 2378
48e535bc 2379 echo '<tr valign="top">';
182311e4 2380 echo '<td align="right"><b>'.get_string('visible', '').':</b></td>';
7bbe08a2 2381 echo '<td align="left">';
48e535bc 2382 unset($choices);
1f15db9d 2383 $choices[1] = get_string('show');
2384 $choices[0] = get_string('hide');
48e535bc 2385 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
2386 echo '</td></tr>';
264867fd 2387}
48e535bc 2388
0705ff84 2389function update_restricted_mods($course,$mods) {
2390 delete_records("course_allowed_modules","course",$course->id);
2391 if (empty($course->restrictmodules)) {
2392 return;
2393 }
2394 else {
2395 foreach ($mods as $mod) {
2396 if ($mod == 0)
2397 continue; // this is the 'allow none' option
2398 $am->course = $course->id;
2399 $am->module = $mod;
2400 insert_record("course_allowed_modules",$am);
2401 }
2402 }
2403}
2404
2405/**
2406 * This function will take an int (module id) or a string (module name)
2407 * and return true or false, whether it's allowed in the given course (object)
264867fd 2408 * $mod is not allowed to be an object, as the field for the module id is inconsistent
0705ff84 2409 * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
2410 */
2411
2412function course_allowed_module($course,$mod) {
2413 if (empty($course->restrictmodules)) {
2414 return true;
2415 }
264867fd 2416
51792df0 2417 // i am not sure this capability is correct
2418 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
0705ff84 2419 return true;
2420 }
2421 if (is_numeric($mod)) {
2422 $modid = $mod;
2423 } else if (is_string($mod)) {
cb77cf12 2424 if ($mod = get_field("modules","id","name",$mod))
0705ff84 2425 $modid = $mod;
2426 }
2427 if (empty($modid)) {
2428 return false;
2429 }
2430 return (record_exists("course_allowed_modules","course",$course->id,"module",$modid));
2431}
2432
861efb19 2433/***
2434 *** Efficiently moves many courses around while maintaining
2435 *** sortorder in order.
264867fd 2436 ***
861efb19 2437 *** $courseids is an array of course ids
2438 ***
2439 **/
2440
2441function move_courses ($courseids, $categoryid) {
2442
2443 global $CFG;
2444
2445 if (!empty($courseids)) {
264867fd 2446
2447 $courseids = array_reverse($courseids);
861efb19 2448
2449 foreach ($courseids as $courseid) {
264867fd 2450
861efb19 2451 if (! $course = get_record("course", "id", $courseid)) {
2452 notify("Error finding course $courseid");
2453 } else {
2454 // figure out a sortorder that we can use in the destination category
2455 $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
2456 FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid);
2457 if ($sortorder === false) {
2458 // the category is empty
2459 // rather than let the db default to 0
264867fd 2460 // set it to > 100 and avoid extra work in fix_coursesortorder()
861efb19 2461 $sortorder = 200;
2462 } else if ($sortorder < 10) {
2463 fix_course_sortorder($categoryid);
2464 }
2465
2466 $course->category = $categoryid;
2467 $course->sortorder = $sortorder;
2468 $course->fullname = addslashes($course->fullname);
2469 $course->shortname = addslashes($course->shortname);
2470 $course->summary = addslashes($course->summary);
2471 $course->password = addslashes($course->password);
2472 $course->teacher = addslashes($course->teacher);
2473 $course->teachers = addslashes($course->teachers);
2474 $course->student = addslashes($course->student);
2475 $course->students = addslashes($course->students);
264867fd 2476
861efb19 2477 if (!update_record('course', $course)) {
2478 notify("An error occurred - course not moved!");
2479 }
2480 }
2481 }
2482 fix_course_sortorder();
264867fd 2483 }
861efb19 2484 return true;
2485}
2486
ae628043 2487/**
2488 * @param string $format Course format ID e.g. 'weeks'
2489 * @return Name that the course format prefers for sections
2490 */
2491function get_section_name($format) {
2492 $sectionname = get_string("name$format","format_$format");
2493 if($sectionname == "[[name$format]]") {
2494 $sectionname = get_string("name$format");
2495 }
2496 return $sectionname;
2497}
2498
b63ec9db 2499?>