merged fix for MDL-8987, guests should not see blogs flagged as site blogs
[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 }
6ba65fa0 991 print_heading('<a title="'. format_string($course->fullname).'" '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'. format_string($course->fullname).'</a>');
185cfb09 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
217a8ee9 1455 // check to see if user can add menus
1456 if (!has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))) {
e2cd3ed0 1457 return false;
217a8ee9 1458 }
1459
89bfeee0 1460 static $resources = false;
1461 static $activities = false;
e0161bff 1462
89bfeee0 1463 if ($resources === false) {
1464 $resources = array();
1465 $activities = array();
6da4b261 1466
89bfeee0 1467 foreach($modnames as $modname=>$modnamestr) {
1468 if (!course_allowed_module($course, $modname)) {
1469 continue;
1470 }
6da4b261 1471
89bfeee0 1472 require_once("$CFG->dirroot/mod/$modname/lib.php");
1473 $gettypesfunc = $modname.'_get_types';
1474 if (function_exists($gettypesfunc)) {
1475 $types = $gettypesfunc();
1476 foreach($types as $type) {
1477 if ($type->modclass == MOD_CLASS_RESOURCE) {
1478 $resources[$type->type] = $type->typestr;
1479 } else {
1480 $activities[$type->type] = $type->typestr;
1481 }
1482 }
1483 } else {
1484 // all mods without type are considered activity
1485 $activities[$modname] = $modnamestr;
1486 }
0705ff84 1487 }
e0161bff 1488 }
1489
89bfeee0 1490 $straddactivity = get_string('addactivity');
1491 $straddresource = get_string('addresource');
1492
4f24b3e3 1493 $output = '<div class="section_add_menus">';
1494
1495 if (!$vertical) {
1496 $output .= '<div class="horizontal">';
1497 }
89bfeee0 1498
1499 if (!empty($resources)) {
1500 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=".sesskey()."&amp;add=",
0705ff84 1501 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
1502 }
cb57e6f4 1503
89bfeee0 1504 if (!empty($activities)) {
1505 $output .= ' ';
1506 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=".sesskey()."&amp;add=",
1507 $activities, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
0705ff84 1508 }
1509
4f24b3e3 1510 if (!$vertical) {
d33d0cda 1511 $output .= '</div>';
1512 }
1513
cb57e6f4 1514 $output .= '</div>';
1515
1516 if ($return) {
1517 return $output;
1518 } else {
1519 echo $output;
1520 }
e0161bff 1521}
1522
5867bfb5 1523function rebuild_course_cache($courseid=0) {
1524// Rebuilds the cached list of course activities stored in the database
1525// If a courseid is not specified, then all are rebuilt
1526
1527 if ($courseid) {
1528 $select = "id = '$courseid'";
1529 } else {
1530 $select = "";
1531 }
1532
1a31c2b3 1533 if ($courses = get_records_select("course", $select,'','id,fullname')) {
5867bfb5 1534 foreach ($courses as $course) {
1535 $modinfo = serialize(get_array_of_activities($course->id));
1536 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
6ba65fa0 1537 notify("Could not cache module information for course '" . format_string($course->fullname) . "'!");
5867bfb5 1538 }
1539 }
1540 }
1541}
1542
1543
c2cb4545 1544
1545function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
89adb174 1546/// Given an empty array, this function recursively travels the
c2cb4545 1547/// categories, building up a nice list for display. It also makes
1548/// an array that list all the parents for each category.
1549
9d866ae0 1550 // initialize the arrays if needed
1551 if (!is_array($list)) {
264867fd 1552 $list = array();
9d866ae0 1553 }
1554 if (!is_array($parents)) {
264867fd 1555 $parents = array();
9d866ae0 1556 }
1557
c2cb4545 1558 if ($category) {
1559 if ($path) {
6ba65fa0 1560 $path = $path.' / '.format_string($category->name);
c2cb4545 1561 } else {
6ba65fa0 1562 $path = format_string($category->name);
c2cb4545 1563 }
1564 $list[$category->id] = $path;
1565 } else {
1566 $category->id = 0;
1567 }
1568
e05bcf2f 1569 if ($categories = get_categories($category->id)) { // Print all the children recursively
c2cb4545 1570 foreach ($categories as $cat) {
1571 if (!empty($category->id)) {
3bd4de22 1572 if (isset($parents[$category->id])) {
2832badf 1573 $parents[$cat->id] = $parents[$category->id];
1574 }
c2cb4545 1575 $parents[$cat->id][] = $category->id;
1576 }
89adb174 1577 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1578 }
1579 }
1580}
1581
1582
d157bd5b 1583function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $files = true) {
89adb174 1584/// Recursive function to print out all the categories in a nice format
c2cb4545 1585/// with or without courses included
9ff5310a 1586 global $CFG;
e05bcf2f 1587
1588 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1589 return;
9ff5310a 1590 }
c2cb4545 1591
1592 if (!$displaylist) {
e92fe848 1593 make_categories_list($displaylist, $parentslist);
c2cb4545 1594 }
1595
1596 if ($category) {
ec7a8b79 1597 if ($category->visible or has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
6f24e48e 1598 print_category_info($category, $depth, $files);
c2cb4545 1599 } else {
1600 return; // Don't bother printing children of invisible categories
1601 }
89adb174 1602
c2cb4545 1603 } else {
c2cb4545 1604 $category->id = "0";
1605 }
1606
1607 if ($categories = get_categories($category->id)) { // Print all the children recursively
1608 $countcats = count($categories);
1609 $count = 0;
1610 $first = true;
1611 $last = false;
1612 foreach ($categories as $cat) {
1613 $count++;
1614 if ($count == $countcats) {
1615 $last = true;
1616 }
1617 $up = $first ? false : true;
1618 $down = $last ? false : true;
1619 $first = false;
1620
6f24e48e 1621 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1, $files);
c2cb4545 1622 }
1623 }
c2cb4545 1624}
1625
0705ff84 1626// this function will return $options array for choose_from_menu, with whitespace to denote nesting.
1627
1628function make_categories_options() {
1629 make_categories_list($cats,$parents);
1630 foreach ($cats as $key => $value) {
1631 if (array_key_exists($key,$parents)) {
1632 if ($indent = count($parents[$key])) {
1633 for ($i = 0; $i < $indent; $i++) {
1634 $cats[$key] = '&nbsp;'.$cats[$key];
1635 }
1636 }
1637 }
1638 }
1639 return $cats;
1640}
c2cb4545 1641
6f24e48e 1642function print_category_info($category, $depth, $files = false) {
d2b6ba70 1643/// Prints the category info in indented fashion
1644/// This function is only used by print_whole_category_list() above
c2cb4545 1645
1646 global $CFG;
b48f834c 1647 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1648
b48f834c 1649 if (empty($strsummary)) {
e05bcf2f 1650 $strallowguests = get_string('allowguests');
1651 $strrequireskey = get_string('requireskey');
1652 $strsummary = get_string('summary');
b48f834c 1653 }
ba2e5d73 1654
e05bcf2f 1655 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1656
6f24e48e 1657 $coursecount = count_records('course') <= FRONTPAGECOURSELIMIT;
1658 if ($files and $coursecount) {
fcf9577a 1659 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" alt="" />';
b48f834c 1660 } else {
7b0b5c14 1661 $catimage = "&nbsp;";
8ef9cb56 1662 }
b48f834c 1663
fcf9577a 1664 echo "\n\n".'<table class="categorylist">';
d2b6ba70 1665
6f24e48e 1666 if ($files and $coursecount) {
19374e76 1667 $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 1668
978abb42 1669 echo '<tr>';
b48f834c 1670
1671 if ($depth) {
1672 $indent = $depth*30;
1673 $rows = count($courses) + 1;
1c919752 1674 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1675 print_spacer(10, $indent);
e05bcf2f 1676 echo '</td>';
b48f834c 1677 }
89adb174 1678
9deaeaa1 1679 echo '<td valign="top" class="category image">'.$catimage.'</td>';
fcf9577a 1680 echo '<td valign="top" class="category name">';
6ba65fa0 1681 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'. format_string($category->name).'</a>';
e05bcf2f 1682 echo '</td>';
290130b3 1683 echo '<td class="category info">&nbsp;</td>';
e05bcf2f 1684 echo '</tr>';
b48f834c 1685
9ff5310a 1686 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1687 foreach ($courses as $course) {
e05bcf2f 1688 $linkcss = $course->visible ? '' : ' class="dimmed" ';
fcf9577a 1689 echo '<tr><td valign="top">&nbsp;';
1690 echo '</td><td valign="top" class="course name">';
6ba65fa0 1691 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'. format_string($course->fullname).'</a>';
fcf9577a 1692 echo '</td><td align="right" valign="top" class="course info">';
c2cb4545 1693 if ($course->guest ) {
e05bcf2f 1694 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
fcf9577a 1695 echo '<img alt="'.$strallowguests.'" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1696 } else {
fcf9577a 1697 echo '<img alt="" style="width:18px;height:16px;" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1698 }
c2cb4545 1699 if ($course->password) {
e05bcf2f 1700 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
fcf9577a 1701 echo '<img alt="'.$strrequireskey.'" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1702 } else {
fcf9577a 1703 echo '<img alt="" style="width:18px;height:16px;" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1704 }
1705 if ($course->summary) {
e05bcf2f 1706 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
fcf9577a 1707 '<img alt="'.$strsummary.'" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1708 400, 500, $strsummary);
ebe8ddc1 1709 } else {
fcf9577a 1710 echo '<img alt="" style="width:18px;height:16px;" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1711 }
e05bcf2f 1712 echo '</td></tr>';
0c656181 1713 }
ba2e5d73 1714 }
d2b6ba70 1715 } else {
b48f834c 1716
e0140f24 1717 echo '<tr>';
1718
b48f834c 1719 if ($depth) {
1720 $indent = $depth*20;
e05bcf2f 1721 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1722 print_spacer(10, $indent);
e05bcf2f 1723 echo '</td>';
d2b6ba70 1724 }
89adb174 1725
fcf9577a 1726 echo '<td valign="top" class="category name">';
6ba65fa0 1727 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'. format_string($category->name).'</a>';
e05bcf2f 1728 echo '</td>';
290130b3 1729 echo '<td valign="top" class="category number">';
e05bcf2f 1730 if ($category->coursecount) {
1731 echo $category->coursecount;
1732 }
1733 echo '</td></tr>';
c2cb4545 1734 }
e05bcf2f 1735 echo '</table>';
c2cb4545 1736}
1737
c2cb4545 1738
35d0244a 1739function print_courses($category, $hidesitecourse = false) {
c2cb4545 1740/// Category is 0 (for all courses) or an object
1741
810393c8 1742 global $CFG;
c2cb4545 1743
1744 if (empty($category)) {
90c2ca2e 1745 $categories = get_categories(0); // Parent = 0 ie top-level categories only
650408a3 1746 if ($categories != null and count($categories) == 1) {
90c2ca2e 1747 $category = array_shift($categories);
32b9a983 1748 $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 1749 } else {
32b9a983 1750 $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 1751 }
1752 unset($categories);
607809b3 1753 } else {
c2cb4545 1754 $categories = get_categories($category->id); // sub categories
32b9a983 1755 $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 1756 }
1757
c2cb4545 1758 if ($courses) {
1759 foreach ($courses as $course) {
1936c10e 1760 if ($hidesitecourse and ($course->id == SITEID)) {
8e227aa7 1761 continue;
1762 }
35d0244a 1763 print_course($course);
c2cb4545 1764 }
1765 } else {
f9667a5a 1766 print_heading(get_string("nocoursesyet"));
954fdb42 1767 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
0468976c 1768 if (has_capability('moodle/course:create', $context)) {
255d1033 1769 $options = array();
1770 $options['category'] = $category->id;
6b7425d2 1771 echo '<div class="addcoursebutton">';
255d1033 1772 print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string("addnewcourse"));
1773 echo '</div>';
1774 }
c2cb4545 1775 }
86dd62a7 1776
1777
1778
c2cb4545 1779}
1780
1781
35d0244a 1782function print_course($course) {
c2cb4545 1783
861efb19 1784 global $CFG, $USER;
c2cb4545 1785
88768091 1786 $context = get_context_instance(CONTEXT_COURSE, $course->id);
146bbb8f 1787
88768091 1788 $linkcss = $course->visible ? '' : ' class="dimmed" ';
22288704 1789
afba7be1 1790 echo '<div class="coursebox">';
1791 echo '<div class="info">';
1792 echo '<div class="name"><a title="'.get_string('entercourse').'"'.
e5e81e78 1793 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
6ba65fa0 1794 format_string($course->fullname).'</a></div>';
b06334e8 1795
d42c64ba 1796 /// first find all roles that are supposed to be displayed
1797 if ($managerroles = get_config('', 'coursemanager')) {
6480b0da 1798 $coursemanagerroles = split(',', $managerroles);
d42c64ba 1799 foreach ($coursemanagerroles as $roleid) {
1800 $role = get_record('role','id',$roleid);
1801 if ($users = get_role_users($roleid, $context, true, '', 'u.lastname ASC', true)) {
1802 foreach ($users as $teacher) {
1803 $fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context));
0150c561 1804 $namesarray[] = role_get_name($role, $context).': <a href="'.$CFG->wwwroot.'/user/view.php?id='.
b06334e8 1805 $teacher->id.'&amp;course='.SITEID.'">'.$fullname.'</a>';
d42c64ba 1806 }
b06334e8 1807 }
c2cb4545 1808 }
d42c64ba 1809
1810 if (!empty($namesarray)) {
88768091 1811 echo "<ul class=\"teachers\">\n<li>";
1812 echo implode('</li><li>', $namesarray);
1813 echo "</li></ul>";
1814 }
c2cb4545 1815 }
d42c64ba 1816
88768091 1817 require_once("$CFG->dirroot/enrol/enrol.class.php");
1818 $enrol = enrolment_factory::factory($course->enrol);
146bbb8f 1819 echo $enrol->get_access_icons($course);
c2cb4545 1820
afba7be1 1821 echo '</div><div class="summary">';
9f39c190 1822 $options = NULL;
1823 $options->noclean = true;
34b5847a 1824 $options->para = false;
9f39c190 1825 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
afba7be1 1826 echo '</div>';
1827 echo '</div>';
1828 echo '<div class="clearer"></div>';
c2cb4545 1829}
1830
1831
1832function print_my_moodle() {
1833/// Prints custom user information on the home page.
1834/// Over time this can include all sorts of information
1835
1836 global $USER, $CFG;
1837
86a1ba04 1838 if (empty($USER->id)) {
c2cb4545 1839 error("It shouldn't be possible to see My Moodle without being logged in.");
1840 }
1841
86dd62a7 1842 $courses = get_my_courses($USER->id);
1843 $rhosts = array();
1844 $rcourses = array();
36e6379e 1845 if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
86dd62a7 1846 $rcourses = get_my_remotecourses($USER->id);
643b67b8 1847 $rhosts = get_my_remotehosts();
86dd62a7 1848 }
1849
1850 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
1851
1852 if (!empty($courses)) {
1853 foreach ($courses as $course) {
1854 if ($course->id == SITEID) {
1855 continue;
1856 }
1857 print_course($course, "100%");
1858 }
1859 }
1860
1861 // MNET
1862 if (!empty($rcourses)) {
1863 // at the IDP, we know of all the remote courses
1864 foreach ($rcourses as $course) {
1865 print_remote_course($course, "100%");
1866 }
1867 } elseif (!empty($rhosts)) {
1868 // non-IDP, we know of all the remote servers, but not courses
1869 foreach ($rhosts as $host) {
643b67b8 1870 print_remote_host($host, "100%");
c81696e5 1871 }
c2cb4545 1872 }
86dd62a7 1873 unset($course);
1874 unset($host);
38a10939 1875
7f989948 1876 if (count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
1877 echo "<table width=\"100%\"><tr><td align=\"center\">";
1878 print_course_search("", false, "short");
1879 echo "</td><td align=\"center\">";
1880 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
1881 echo "</td></tr></table>\n";
1882 }
86dd62a7 1883
26330001 1884 } else {
1885 if (count_records("course_categories") > 1) {
cb29b020 1886 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 1887 print_whole_category_list();
1888 print_simple_box_end();
1889 } else {
35d0244a 1890 print_courses(0);
26330001 1891 }
607809b3 1892 }
2b8cef80 1893}
1894
11b0c469 1895
a8b56716 1896function print_course_search($value="", $return=false, $format="plain") {
38a10939 1897
1898 global $CFG;
1e0fb105 1899 static $count = 0;
1900
1901 $count++;
1902
1903 $id = 'coursesearch';
1904
1905 if ($count > 1) {
1906 $id .= $count;
1907 }
38a10939 1908
1909 $strsearchcourses= get_string("searchcourses");
1910
1c919752 1911 if ($format == 'plain') {
1e0fb105 1912 $output = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
fcf9577a 1913 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
f8a5159a 1914 $output .= '<input type="text" size="30" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1915 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
fcf9577a 1916 $output .= '</fieldset></form>';
1c919752 1917 } else if ($format == 'short') {
1e0fb105 1918 $output = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
fcf9577a 1919 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
f8a5159a 1920 $output .= '<input type="text" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1921 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
fcf9577a 1922 $output .= '</fieldset></form>';
1c919752 1923 } else if ($format == 'navbar') {
fcf9577a 1924 $output = '<form id="coursesearchnavbar" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
1925 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
f8a5159a 1926 $output .= '<input type="text" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value, true).'" />';
9cc78ee1 1927 $output .= '<input type="submit" value="'.s($strsearchcourses).'" />';
fcf9577a 1928 $output .= '</fieldset></form>';
a8b56716 1929 }
1930
1931 if ($return) {
1932 return $output;
1933 }
1934 echo $output;
38a10939 1935}
11b0c469 1936
86dd62a7 1937function print_remote_course($course, $width="100%") {
1938
1939 global $CFG, $USER;
1940
1941 $linkcss = '';
1942
1943 $url = "{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}";
1944
643b67b8 1945 echo '<div class="coursebox remotecoursebox">';
86dd62a7 1946 echo '<div class="info">';
1947 echo '<div class="name"><a title="'.get_string('entercourse').'"'.
1948 $linkcss.' href="'.$url.'">'
6ba65fa0 1949 . format_string($course->fullname) .'</a><br />'
1950 . format_string($course->hostname) . ' : '
1951 . format_string($course->cat_name) . ' : '
1952 . format_string($course->shortname). '</div>';
86dd62a7 1953 echo '</div><div class="summary">';
1954 $options = NULL;
1955 $options->noclean = true;
1956 $options->para = false;
1957 echo format_text($course->summary, FORMAT_MOODLE, $options);
1958 echo '</div>';
1959 echo '</div>';
1960 echo '<div class="clearer"></div>';
1961}
1962
643b67b8 1963function print_remote_host($host, $width="100%") {
1964
1965 global $CFG, $USER;
1966
1967 $linkcss = '';
1968
1969 echo '<div class="coursebox">';
1970 echo '<div class="info">';
1971 echo '<div class="name">';
1972 echo '<img src="'.$CFG->pixpath.'/i/mnethost.gif" class="icon" alt="'.get_string('course').'" />';
1973 echo '<a title="'.s($host['name']).'" href="'.s($host['url']).'">'
1974 . s($host['name']).'</a> - ';
1fd80ad3 1975 echo $host['count'] . ' ' . get_string('courses');
643b67b8 1976 echo '</div>';
1977 echo '</div>';
caa90d56 1978 echo '</div>';
643b67b8 1979 echo '<div class="clearer"></div>';
1980}
1981
86dd62a7 1982
11b0c469 1983/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1984
1985function add_course_module($mod) {
11b0c469 1986
e5dfd0f3 1987 $mod->added = time();
53f4ad2c 1988 unset($mod->id);
11b0c469 1989
e5dfd0f3 1990 return insert_record("course_modules", $mod);
11b0c469 1991}
1992
7977cffd 1993function add_mod_to_section($mod, $beforemod=NULL) {
1994/// Given a full mod object with section and course already defined
1995/// If $before is specified, then this is an existing ID which we
1996/// will insert the new module before
1997///
1998/// Returns the course_sections ID where the mod is inserted
11b0c469 1999
9fa49e22 2000 if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
7977cffd 2001
2002 $section->sequence = trim($section->sequence);
2003
2004 if (empty($section->sequence)) {
11b0c469 2005 $newsequence = "$mod->coursemodule";
7977cffd 2006
2007 } else if ($beforemod) {
2008 $modarray = explode(",", $section->sequence);
2009
2010 if ($key = array_keys ($modarray, $beforemod->id)) {
2011 $insertarray = array($mod->id, $beforemod->id);
2012 array_splice($modarray, $key[0], 1, $insertarray);
2013 $newsequence = implode(",", $modarray);
2014
2015 } else { // Just tack it on the end anyway
2016 $newsequence = "$section->sequence,$mod->coursemodule";
2017 }
2018
2019 } else {
2020 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 2021 }
89adb174 2022
e5dfd0f3 2023 if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
2024 return $section->id; // Return course_sections ID that was used.
11b0c469 2025 } else {
e5dfd0f3 2026 return 0;
11b0c469 2027 }
89adb174 2028
11b0c469 2029 } else { // Insert a new record
e5dfd0f3 2030 $section->course = $mod->course;
2031 $section->section = $mod->section;
2032 $section->summary = "";
2033 $section->sequence = $mod->coursemodule;
2034 return insert_record("course_sections", $section);
11b0c469 2035 }
2036}
2037
48e535bc 2038function set_coursemodule_groupmode($id, $groupmode) {
3d575e6f 2039 return set_field("course_modules", "groupmode", $groupmode, "id", $id);
2040}
2041
02f66c42 2042/**
2043* $prevstateoverrides = true will set the visibility of the course module
2044* to what is defined in visibleold. This enables us to remember the current
2045* visibility when making a whole section hidden, so that when we toggle
2046* that section back to visible, we are able to return the visibility of
2047* the course module back to what it was originally.
2048*/
2049function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
978abb42 2050 if (!$cm = get_record('course_modules', 'id', $id)) {
2051 return false;
2052 }
2053 if (!$modulename = get_field('modules', 'name', 'id', $cm->module)) {
2054 return false;
2055 }
dcd338ff 2056 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
2057 foreach($events as $event) {
48e535bc 2058 if ($visible) {
2059 show_event($event);
2060 } else {
2061 hide_event($event);
2062 }
dcd338ff 2063 }
2064 }
02f66c42 2065 if ($prevstateoverrides) {
2066 if ($visible == '0') {
2067 // Remember the current visible state so we can toggle this back.
2068 set_field('course_modules', 'visibleold', $cm->visible, 'id', $id);
2069 } else {
2070 // Get the previous saved visible states.
2071 return set_field('course_modules', 'visible', $cm->visibleold, 'id', $id);
2072 }
2073 }
48e535bc 2074 return set_field("course_modules", "visible", $visible, "id", $id);
1acfbce5 2075}
2076
290130b3 2077/*
2078 * Delete a course module and any associated data at the course level (events)
264867fd 2079 * Until 1.5 this function simply marked a deleted flag ... now it
290130b3 2080 * deletes it completely.
2081 *
2082 */
48e535bc 2083function delete_course_module($id) {
290130b3 2084 if (!$cm = get_record('course_modules', 'id', $id)) {
2085 return true;
2086 }
dcd338ff 2087 $modulename = get_field('modules', 'name', 'id', $cm->module);
2088 if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
2089 foreach($events as $event) {
48e535bc 2090 delete_event($event);
dcd338ff 2091 }
2092 }
290130b3 2093 return delete_records('course_modules', 'id', $cm->id);
11b0c469 2094}
2095
2096function delete_mod_from_section($mod, $section) {
11b0c469 2097
e5dfd0f3 2098 if ($section = get_record("course_sections", "id", "$section") ) {
11b0c469 2099
e5dfd0f3 2100 $modarray = explode(",", $section->sequence);
11b0c469 2101
2102 if ($key = array_keys ($modarray, $mod)) {
2103 array_splice($modarray, $key[0], 1);
2104 $newsequence = implode(",", $modarray);
e5dfd0f3 2105 return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
11b0c469 2106 } else {
2107 return false;
2108 }
89adb174 2109
11b0c469 2110 }
7977cffd 2111 return false;
11b0c469 2112}
2113
12905134 2114function move_section($course, $section, $move) {
2115/// Moves a whole course section up and down within the course
798b70a1 2116 global $USER;
12905134 2117
2118 if (!$move) {
2119 return true;
2120 }
2121
2122 $sectiondest = $section + $move;
2123
2124 if ($sectiondest > $course->numsections or $sectiondest < 1) {
2125 return false;
2126 }
2127
2128 if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
2129 return false;
2130 }
2131
2132 if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
2133 return false;
2134 }
2135
56e34ee4 2136 if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
12905134 2137 return false;
2138 }
56e34ee4 2139 if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
12905134 2140 return false;
2141 }
798b70a1 2142 // if the focus is on the section that is being moved, then move the focus along
2143 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
2144 course_set_display($course->id, $sectiondest);
2145 }
5390cbb7 2146
a987106d 2147 // Check for duplicates and fix order if needed.
5390cbb7 2148 // There is a very rare case that some sections in the same course have the same section id.
a987106d 2149 $sections = get_records_select('course_sections', "course = $course->id", 'section ASC');
2150 $n = 0;
2151 foreach ($sections as $section) {
2152 if ($section->section != $n) {
5390cbb7 2153 if (!set_field('course_sections', 'section', $n, 'id', $section->id)) {
2154 return false;
2155 }
5390cbb7 2156 }
a987106d 2157 $n++;
5390cbb7 2158 }
12905134 2159 return true;
2160}
2161
2162
7977cffd 2163function moveto_module($mod, $section, $beforemod=NULL) {
2164/// All parameters are objects
2165/// Move the module object $mod to the specified $section
2166/// If $beforemod exists then that is the module
2167/// before which $modid should be inserted
2168
2169/// Remove original module from original section
2170
2171 if (! delete_mod_from_section($mod->id, $mod->section)) {
2172 notify("Could not delete module from existing section");
2173 }
2174
2175/// Update module itself if necessary
2176
2177 if ($mod->section != $section->id) {
89adb174 2178 $mod->section = $section->id;
7977cffd 2179 if (!update_record("course_modules", $mod)) {
2180 return false;
2181 }
48e535bc 2182 // if moving to a hidden section then hide module
2183 if (!$section->visible) {
2184 set_coursemodule_visible($mod->id, 0);
2185 }
7977cffd 2186 }
2187
2188/// Add the module into the new section
2189
2190 $mod->course = $section->course;
2191 $mod->section = $section->section; // need relative reference
2192 $mod->coursemodule = $mod->id;
2193
2194 if (! add_mod_to_section($mod, $beforemod)) {
2195 return false;
2196 }
2197
2198 return true;
2199
2200}
2201
24e1eae4 2202function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
810393c8 2203 global $CFG, $USER;
94361e02 2204
3d575e6f 2205 static $str;
37a88449 2206 static $sesskey;
3d575e6f 2207
217a8ee9 2208 $modcontext = get_context_instance(CONTEXT_MODULE, $mod->id);
2209 // no permission to edit
2210 if (!has_capability('moodle/course:manageactivities', $modcontext)) {
e2cd3ed0 2211 return false;
217a8ee9 2212 }
2213
3d575e6f 2214 if (!isset($str)) {
90ebdf65 2215 $str->delete = get_string("delete");
2216 $str->move = get_string("move");
2217 $str->moveup = get_string("moveup");
2218 $str->movedown = get_string("movedown");
2219 $str->moveright = get_string("moveright");
2220 $str->moveleft = get_string("moveleft");
2221 $str->update = get_string("update");
2222 $str->duplicate = get_string("duplicate");
2223 $str->hide = get_string("hide");
2224 $str->show = get_string("show");
3d575e6f 2225 $str->clicktochange = get_string("clicktochange");
32d03b7b 2226 $str->forcedmode = get_string("forcedmode");
3d575e6f 2227 $str->groupsnone = get_string("groupsnone");
2228 $str->groupsseparate = get_string("groupsseparate");
2229 $str->groupsvisible = get_string("groupsvisible");
37a88449 2230 $sesskey = sesskey();
1acfbce5 2231 }
94361e02 2232
24e1eae4 2233 if ($section >= 0) {
75f087b6 2234 $section = '&amp;sr='.$section; // Section return
24e1eae4 2235 } else {
2236 $section = '';
2237 }
2238
94361e02 2239 if ($absolute) {
37a88449 2240 $path = $CFG->wwwroot.'/course';
dc0dc7d5 2241 } else {
37a88449 2242 $path = '.';
dc0dc7d5 2243 }
2244
217a8ee9 2245 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
2246 if ($mod->visible) {
2247 $hideshow = '<a class="editing_hide" title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id.
2248 '&amp;sesskey='.$sesskey.$section.'"><img'.
2249 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" '.
2250 ' alt="'.$str->hide.'" /></a>'."\n";
2251 } else {
2252 $hideshow = '<a class="editing_show" title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id.
2253 '&amp;sesskey='.$sesskey.$section.'"><img'.
2254 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" '.
2255 ' alt="'.$str->show.'" /></a>'."\n";
2256 }
7977cffd 2257 }
3d575e6f 2258 if ($mod->groupmode !== false) {
2259 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 2260 $grouptitle = $str->groupsseparate;
cddbd5d5 2261 $groupclass = 'editing_groupsseparate';
37a88449 2262 $groupimage = $CFG->pixpath.'/t/groups.gif';
2263 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=0&amp;sesskey='.$sesskey;
3d575e6f 2264 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 2265 $grouptitle = $str->groupsvisible;
cddbd5d5 2266 $groupclass = 'editing_groupsvisible';
37a88449 2267 $groupimage = $CFG->pixpath.'/t/groupv.gif';
2268 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=1&amp;sesskey='.$sesskey;
32d03b7b 2269 } else {
2270 $grouptitle = $str->groupsnone;
90ebdf65 2271 $groupclass = 'editing_groupsnone';
37a88449 2272 $groupimage = $CFG->pixpath.'/t/groupn.gif';
2273 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=2&amp;sesskey='.$sesskey;
32d03b7b 2274 }
2275 if ($mod->groupmodelink) {
90ebdf65 2276 $groupmode = '<a class="'.$groupclass.'" title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'.
0d905d9f 2277 '<img src="'.$groupimage.'" class="iconsmall" '.
2278 'alt="'.$grouptitle.'" /></a>';
3d575e6f 2279 } else {
37a88449 2280 $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '.
0d905d9f 2281 ' src="'.$groupimage.'" class="iconsmall" '.
2282 'alt="'.$grouptitle.'" />';
3d575e6f 2283 }
2284 } else {
2285 $groupmode = "";
2286 }
e2cd3ed0 2287
217a8ee9 2288 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $mod->course))) {
2289 if ($moveselect) {
2290 $move = '<a class="editing_move" title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id.
2291 '&amp;sesskey='.$sesskey.$section.'"><img'.
2292 ' src="'.$CFG->pixpath.'/t/move.gif" class="iconsmall" '.
2293 ' alt="'.$str->move.'" /></a>'."\n";
2294 } else {
2295 $move = '<a class="editing_moveup" title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id.
2296 '&amp;move=-1&amp;sesskey='.$sesskey.$section.'"><img'.
2297 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" '.
2298 ' alt="'.$str->moveup.'" /></a>'."\n".
2299 '<a class="editing_movedown" title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id.
2300 '&amp;move=1&amp;sesskey='.$sesskey.$section.'"><img'.
2301 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" '.
2302 ' alt="'.$str->movedown.'" /></a>'."\n";
2303 }
7977cffd 2304 }
2305
aac94fd0 2306 $leftright = "";
217a8ee9 2307 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $mod->course))) {
2308 if ($indent > 0) {
2309 $leftright .= '<a class="editing_moveleft" title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id.
2310 '&amp;indent=-1&amp;sesskey='.$sesskey.$section.'"><img'.
2311 ' src="'.$CFG->pixpath.'/t/left.gif" class="iconsmall" '.
2312 ' alt="'.$str->moveleft.'" /></a>'."\n";
2313 }
2314 if ($indent >= 0) {
2315 $leftright .= '<a class="editing_moveright" title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id.
2316 '&amp;indent=1&amp;sesskey='.$sesskey.$section.'"><img'.
2317 ' src="'.$CFG->pixpath.'/t/right.gif" class="iconsmall" '.
2318 ' alt="'.$str->moveright.'" /></a>'."\n";
2319 }
37a88449 2320 }
2321
90ebdf65 2322 return '<span class="commands">'."\n".$leftright.$move.
2323 '<a class="editing_update" title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id.
37a88449 2324 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2325 ' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" '.
90ebdf65 2326 ' alt="'.$str->update.'" /></a>'."\n".
2327 '<a class="editing_delete" title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id.
37a88449 2328 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2329 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" '.
90ebdf65 2330 ' alt="'.$str->delete.'" /></a>'."\n".$hideshow.$groupmode."\n".'</span>';
90845098 2331}
2332
b61efafb 2333/**
264867fd 2334 * given a course object with shortname & fullname, this function will
b61efafb 2335 * truncate the the number of chars allowed and add ... if it was too long
2336 */
2337function course_format_name ($course,$max=100) {
264867fd 2338
6ba65fa0 2339 $str = $course->shortname.': '. $course->fullname;
b61efafb 2340 if (strlen($str) <= $max) {
2341 return $str;
2342 }
2343 else {
2344 return substr($str,0,$max-3).'...';
2345 }
2346}
2347
2348/**
2349 * This function will return true if the given course is a child course at all
2350 */
2351function course_in_meta ($course) {
5f37b628 2352 return record_exists("course_meta","child_course",$course->id);
b61efafb 2353}
2354
48e535bc 2355
2356/**
2357 * Print standard form elements on module setup forms in mod/.../mod.html
2358 */
2359function print_standard_coursemodule_settings($form) {
da2224f8 2360 if (! $course = get_record('course', 'id', $form->course)) {
2361 error("This course doesn't exist");
2362 }
2363 print_groupmode_setting($form, $course);
2364 print_visible_setting($form, $course);
48e535bc 2365}
2366
2367/**
2368 * Print groupmode form element on module setup forms in mod/.../mod.html
2369 */
5ebb746b 2370function print_groupmode_setting($form, $course=NULL) {
48e535bc 2371
5ebb746b 2372 if (empty($course)) {
2373 if (! $course = get_record('course', 'id', $form->course)) {
2374 error("This course doesn't exist");
2375 }
2376 }
48e535bc 2377 if ($form->coursemodule) {
2378 if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
2379 error("This course module doesn't exist");
2380 }
2381 } else {
2382 $cm = null;
2383 }
2384 $groupmode = groupmode($course, $cm);
2385 if ($course->groupmode or (!$course->groupmodeforce)) {
2386 echo '<tr valign="top">';
2387 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
7bbe08a2 2388 echo '<td align="left">';
48e535bc 2389 unset($choices);
2390 $choices[NOGROUPS] = get_string('groupsnone');
2391 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
2392 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
2393 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
2394 helpbutton('groupmode', get_string('groupmode'));
2395 echo '</td></tr>';
2396 }
2397}
2398
2399/**
2400 * Print visibility setting form element on module setup forms in mod/.../mod.html
2401 */
5ebb746b 2402function print_visible_setting($form, $course=NULL) {
1ee55c41 2403 if (empty($course)) {
2404 if (! $course = get_record('course', 'id', $form->course)) {
2405 error("This course doesn't exist");
2406 }
2407 }
48e535bc 2408 if ($form->coursemodule) {
2409 $visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
2410 } else {
2411 $visible = true;
2412 }
2413
2414 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
2415 $hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
2416 } else {
2417 $hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
2418 }
2419 if ($hiddensection) {
2420 $visible = false;
2421 }
264867fd 2422
48e535bc 2423 echo '<tr valign="top">';
182311e4 2424 echo '<td align="right"><b>'.get_string('visible', '').':</b></td>';
7bbe08a2 2425 echo '<td align="left">';
48e535bc 2426 unset($choices);
1f15db9d 2427 $choices[1] = get_string('show');
2428 $choices[0] = get_string('hide');
48e535bc 2429 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
2430 echo '</td></tr>';
264867fd 2431}
48e535bc 2432
0705ff84 2433function update_restricted_mods($course,$mods) {
2434 delete_records("course_allowed_modules","course",$course->id);
2435 if (empty($course->restrictmodules)) {
2436 return;
2437 }
2438 else {
2439 foreach ($mods as $mod) {
2440 if ($mod == 0)
2441 continue; // this is the 'allow none' option
2442 $am->course = $course->id;
2443 $am->module = $mod;
2444 insert_record("course_allowed_modules",$am);
2445 }
2446 }
2447}
2448
2449/**
2450 * This function will take an int (module id) or a string (module name)
2451 * and return true or false, whether it's allowed in the given course (object)
264867fd 2452 * $mod is not allowed to be an object, as the field for the module id is inconsistent
0705ff84 2453 * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
2454 */
2455
2456function course_allowed_module($course,$mod) {
2457 if (empty($course->restrictmodules)) {
2458 return true;
2459 }
264867fd 2460
51792df0 2461 // i am not sure this capability is correct
2462 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
0705ff84 2463 return true;
2464 }
2465 if (is_numeric($mod)) {
2466 $modid = $mod;
2467 } else if (is_string($mod)) {
cb77cf12 2468 if ($mod = get_field("modules","id","name",$mod))
0705ff84 2469 $modid = $mod;
2470 }
2471 if (empty($modid)) {
2472 return false;
2473 }
2474 return (record_exists("course_allowed_modules","course",$course->id,"module",$modid));
2475}
2476
861efb19 2477/***
2478 *** Efficiently moves many courses around while maintaining
2479 *** sortorder in order.
264867fd 2480 ***
861efb19 2481 *** $courseids is an array of course ids
2482 ***
2483 **/
2484
2485function move_courses ($courseids, $categoryid) {
2486
2487 global $CFG;
2488
2489 if (!empty($courseids)) {
264867fd 2490
2491 $courseids = array_reverse($courseids);
861efb19 2492
2493 foreach ($courseids as $courseid) {
264867fd 2494
861efb19 2495 if (! $course = get_record("course", "id", $courseid)) {
2496 notify("Error finding course $courseid");
2497 } else {
2498 // figure out a sortorder that we can use in the destination category
2499 $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
2500 FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid);
2501 if ($sortorder === false) {
2502 // the category is empty
2503 // rather than let the db default to 0
264867fd 2504 // set it to > 100 and avoid extra work in fix_coursesortorder()
861efb19 2505 $sortorder = 200;
2506 } else if ($sortorder < 10) {
2507 fix_course_sortorder($categoryid);
2508 }
2509
2510 $course->category = $categoryid;
2511 $course->sortorder = $sortorder;
2512 $course->fullname = addslashes($course->fullname);
2513 $course->shortname = addslashes($course->shortname);
2514 $course->summary = addslashes($course->summary);
2515 $course->password = addslashes($course->password);
2516 $course->teacher = addslashes($course->teacher);
2517 $course->teachers = addslashes($course->teachers);
2518 $course->student = addslashes($course->student);
2519 $course->students = addslashes($course->students);
264867fd 2520
861efb19 2521 if (!update_record('course', $course)) {
2522 notify("An error occurred - course not moved!");
2523 }
118c0f6d 2524 // parents changed (course category), do not delete child context relations
2525 insert_context_rel(get_context_instance(CONTEXT_COURSE, $course->id), false);
861efb19 2526 }
2527 }
2528 fix_course_sortorder();
264867fd 2529 }
861efb19 2530 return true;
2531}
2532
ae628043 2533/**
2534 * @param string $format Course format ID e.g. 'weeks'
2535 * @return Name that the course format prefers for sections
2536 */
2537function get_section_name($format) {
2538 $sectionname = get_string("name$format","format_$format");
2539 if($sectionname == "[[name$format]]") {
2540 $sectionname = get_string("name$format");
2541 }
2542 return $sectionname;
2543}
2544
2585a68d 2545/**
2546 * Can the current user delete this course?
2547 * @param int $courseid
2548 * @return boolean
2549 *
2550 * Exception here to fix MDL-7796.
2551 *
2552 * FIXME
2553 * Course creators who can manage activities in the course
2554 * shoule be allowed to delete the course. We do it this
2555 * way because we need a quick fix to bring the functionality
2556 * in line with what we had pre-roles. We can't give the
2557 * default course creator role moodle/course:delete at
2558 * CONTEXT_SYSTEM level because this will allow them to
2559 * delete any course in the site. So we hard code this here
2560 * for now.
2561 *
2562 * @author vyshane AT gmail.com
2563 */
2564function can_delete_course($courseid) {
2565
2566 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2567
2568 return ( has_capability('moodle/course:delete', $context)
2569 || (has_capability('moodle/legacy:coursecreator', $context)
2570 && has_capability('moodle/course:manageactivities', $context)) );
2571}
2572
2573
2574
2575
9deaeaa1 2576?>