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