removed shorttag
[moodle.git] / course / lib.php
CommitLineData
e4027ac9 1<?php // $Id$
97c270e9 2 // Library of useful functions
f9903ed0 3
f9903ed0 4
92890025 5define('COURSE_MAX_LOG_DISPLAY', 150); // days
6define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
7define('COURSE_LIVELOG_REFRESH', 60); // Seconds
8define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds
9define('COURSE_MAX_SUMMARIES_PER_PAGE', 10); // courses
950c35a9 10define('COURSE_MAX_COURSES_PER_DROPDOWN',1000); // max courses in log dropdown before switching to optional
92890025 11define('COURSE_MAX_USERS_PER_DROPDOWN',1000); // max users in log dropdown before switching to optional
220a90c5 12define('FRONTPAGENEWS', '0');
13define('FRONTPAGECOURSELIST', '1');
14define('FRONTPAGECATEGORYNAMES', '2');
15define('FRONTPAGETOPICONLY', '3');
16define('FRONTPAGECATEGORYCOMBO', '4');
6f24e48e 17define('FRONTPAGECOURSELIMIT', 200); // maximum number of courses displayed on the frontpage
18define('EXCELROWS', 65535);
19define('FIRSTUSEDEXCELROW', 3);
60fdc714 20
89bfeee0 21define('MOD_CLASS_ACTIVITY', 0);
22define('MOD_CLASS_RESOURCE', 1);
23
f9903ed0 24
600149be 25function make_log_url($module, $url) {
26 switch ($module) {
bd7be234 27 case 'course':
28 case 'file':
29 case 'login':
30 case 'lib':
31 case 'admin':
bd7be234 32 case 'calendar':
bd5d0ce5 33 case 'mnet course':
34 return "/course/$url";
35 break;
01d5d399 36 case 'user':
bd7be234 37 case 'blog':
600149be 38 return "/$module/$url";
39 break;
bd7be234 40 case 'upload':
41 return $url;
c80b7585 42 break;
38fb8190 43 case 'coursetags':
44 return '/'.$url;
45 break;
bd7be234 46 case 'library':
47 case '':
48 return '/';
de2dfe68 49 break;
4597d533 50 case 'message':
51 return "/message/$url";
52 break;
600149be 53 default:
54 return "/mod/$module/$url";
55 break;
56 }
57}
58
92890025 59
c215b32b 60function build_mnet_logs_array($hostid, $course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
61 $modname="", $modid=0, $modaction="", $groupid=0) {
cb6fec1f 62 global $CFG, $DB;
c215b32b 63
64 // It is assumed that $date is the GMT time of midnight for that day,
65 // and so the next 86400 seconds worth of logs are printed.
66
67 /// Setup for group handling.
238c0dd9 68
69 // TODO: I don't understand group/context/etc. enough to be able to do
c215b32b 70 // something interesting with it here
71 // What is the context of a remote course?
238c0dd9 72
c215b32b 73 /// If the group mode is separate, and this user does not have editing privileges,
74 /// then only the user's group can be viewed.
75 //if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
76 // $groupid = get_current_group($course->id);
77 //}
78 /// If this course doesn't have groups, no groupid can be specified.
79 //else if (!$course->groupmode) {
80 // $groupid = 0;
81 //}
cb6fec1f 82
83 $ILIKE = $DB->sql_ilike();
84
c215b32b 85 $groupid = 0;
86
87 $joins = array();
88
cb6fec1f 89 $qry = "SELECT l.*, u.firstname, u.lastname, u.picture
90 FROM {mnet_log} l
91 LEFT JOIN {user} u ON l.userid = u.id
92 WHERE ";
93 $params = array();
94
95 $where .= "l.hostid = :hostid";
96 $params['hostid'] = $hostid;
c215b32b 97
98 // TODO: Is 1 really a magic number referring to the sitename?
cb6fec1f 99 if ($course != SITEID || $modid != 0) {
100 $where .= " AND l.course=:courseid";
101 $params['courseid'] = $course;
c215b32b 102 }
103
104 if ($modname) {
cb6fec1f 105 $where .= " AND l.module = :modname";
106 $params['modname'] = $modname;
c215b32b 107 }
108
109 if ('site_errors' === $modid) {
cb6fec1f 110 $where .= " AND ( l.action='error' OR l.action='infected' )";
c215b32b 111 } else if ($modid) {
238c0dd9 112 //TODO: This assumes that modids are the same across sites... probably
c215b32b 113 //not true
cb6fec1f 114 $where .= " AND l.cmid = :modid";
115 $params['modid'] = $modid;
c215b32b 116 }
117
118 if ($modaction) {
119 $firstletter = substr($modaction, 0, 1);
c659559f 120 if (preg_match('/[[:alpha:]]/', $firstletter)) {
cb6fec1f 121 $where .= " AND lower(l.action) $ILIKE :modaction";
122 $params['modaction'] = "%$modaction%";
c215b32b 123 } else if ($firstletter == '-') {
cb6fec1f 124 $where .= " AND lower(l.action) NOT $ILIKE :modaction";
125 $params['modaction'] = "%$modaction%";
c215b32b 126 }
127 }
128
129 if ($user) {
cb6fec1f 130 $where .= " AND l.userid = :user";
131 $params['user'] = $user;
c215b32b 132 }
133
134 if ($date) {
135 $enddate = $date + 86400;
cb6fec1f 136 $where .= " AND l.time > :date AND l.time < :enddate";
137 $params['date'] = $date;
138 $params['enddate'] = $enddate;
c215b32b 139 }
140
141 $result = array();
cb6fec1f 142 $result['totalcount'] = $DB->count_records_sql("SELECT COUNT('x') FROM {mnet_log} l WHERE $where", $params);
c215b32b 143 if(!empty($result['totalcount'])) {
cb6fec1f 144 $where .= " ORDER BY $order";
145 $result['logs'] = $DB->get_records_sql("$qry $where", $params, $limitfrom, $limitnum);
c215b32b 146 } else {
147 $result['logs'] = array();
148 }
149 return $result;
150}
151
92890025 152function build_logs_array($course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
153 $modname="", $modid=0, $modaction="", $groupid=0) {
c3df0901 154 global $DB;
e0161bff 155 // It is assumed that $date is the GMT time of midnight for that day,
156 // and so the next 86400 seconds worth of logs are printed.
f9903ed0 157
69c76405 158 /// Setup for group handling.
264867fd 159
69c76405 160 /// If the group mode is separate, and this user does not have editing privileges,
161 /// then only the user's group can be viewed.
3924b988 162 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
69c76405 163 $groupid = get_current_group($course->id);
164 }
165 /// If this course doesn't have groups, no groupid can be specified.
166 else if (!$course->groupmode) {
167 $groupid = 0;
168 }
169
e0161bff 170 $joins = array();
c3df0901 171 $oarams = array();
a2ab3b05 172
e15ef260 173 if ($course->id != SITEID || $modid != 0) {
c3df0901 174 $joins[] = "l.course = :courseid";
175 $params['courseid'] = $course->id;
e15ef260 176 }
f9903ed0 177
c469a7ef 178 if ($modname) {
c3df0901 179 $joins[] = "l.module = :modname";
238c0dd9 180 $params['modname'] = $modname;
f24cffb9 181 }
182
e21922f0 183 if ('site_errors' === $modid) {
bf35eb15 184 $joins[] = "( l.action='error' OR l.action='infected' )";
e21922f0 185 } else if ($modid) {
c3df0901 186 $joins[] = "l.cmid = :modid";
187 $params['modid'] = $modid;
69d79bc3 188 }
189
190 if ($modaction) {
c3df0901 191 $ILIKE = $DB->sql_ilike();
ee35e0b8 192 $firstletter = substr($modaction, 0, 1);
c659559f 193 if (preg_match('/[[:alpha:]]/', $firstletter)) {
c3df0901 194 $joins[] = "l.action $ILIKE :modaction";
195 $params['modaction'] = '%'.$modaction.'%';
ee35e0b8 196 } else if ($firstletter == '-') {
c3df0901 197 $joins[] = "l.action NOT $ILIKE :modaction";
198 $params['modaction'] = '%'.substr($modaction, 1).'%';
ee35e0b8 199 }
f24cffb9 200 }
201
238c0dd9 202
69c76405 203 /// Getting all members of a group.
204 if ($groupid and !$user) {
62d63838 205 if ($gusers = groups_get_members($groupid)) {
206 $gusers = array_keys($gusers);
1e95d7b7 207 $joins[] = 'l.userid IN (' . implode(',', $gusers) . ')';
208 } else {
05a33439 209 $joins[] = 'l.userid = 0'; // No users in groups, so we want something that will always be false.
69c76405 210 }
211 }
212 else if ($user) {
c3df0901 213 $joins[] = "l.userid = :userid";
214 $params['userid'] = $user;
f9903ed0 215 }
216
217 if ($date) {
218 $enddate = $date + 86400;
c3df0901 219 $joins[] = "l.time > :date AND l.time < :enddate";
220 $params['date'] = $date;
221 $params['enddate'] = $enddate;
f9903ed0 222 }
223
1e95d7b7 224 $selector = implode(' AND ', $joins);
e0161bff 225
d09f3c80 226 $totalcount = 0; // Initialise
92890025 227 $result = array();
c3df0901 228 $result['logs'] = get_logs($selector, $params, $order, $limitfrom, $limitnum, $totalcount);
92890025 229 $result['totalcount'] = $totalcount;
230 return $result;
231}
264867fd 232
233
92890025 234function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
235 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
264867fd 236
cb6fec1f 237 global $CFG, $DB;
264867fd 238
92890025 239 if (!$logs = build_logs_array($course, $user, $date, $order, $page*$perpage, $perpage,
240 $modname, $modid, $modaction, $groupid)) {
f9903ed0 241 notify("No logs found!");
242 print_footer($course);
243 exit;
244 }
264867fd 245
ea49a66c 246 $courses = array();
247
92890025 248 if ($course->id == SITEID) {
249 $courses[0] = '';
bf221aca 250 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
92890025 251 foreach ($ccc as $cc) {
bf221aca 252 $courses[$cc->id] = $cc->shortname;
92890025 253 }
254 }
ea49a66c 255 } else {
bf221aca 256 $courses[$course->id] = $course->shortname;
92890025 257 }
264867fd 258
92890025 259 $totalcount = $logs['totalcount'];
f9903ed0 260 $count=0;
2eb68e6f 261 $ldcache = array();
f9903ed0 262 $tt = getdate(time());
263 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
1c0200e0 264
dcde9f02 265 $strftimedatetime = get_string("strftimedatetime");
266
5577ceb3 267 echo "<div class=\"info\">\n";
519d369f 268 print_string("displayingrecords", "", $totalcount);
5577ceb3 269 echo "</div>\n";
1c0200e0 270
8f0cd6ef 271 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
519d369f 272
56595370 273 echo '<table class="logtable genearlbox boxaligncenter" summary="">'."\n";
274 // echo "<table class=\"logtable\" cellpadding=\"3\" cellspacing=\"0\" summary=\"\">\n";
21283ddc 275 echo "<tr>";
1548978d 276 if ($course->id == SITEID) {
54926e78 277 echo "<th class=\"c0 header\" scope=\"col\">".get_string('course')."</th>\n";
1548978d 278 }
54926e78 279 echo "<th class=\"c1 header\" scope=\"col\">".get_string('time')."</th>\n";
280 echo "<th class=\"c2 header\" scope=\"col\">".get_string('ip_address')."</th>\n";
d6cc2341 281 echo "<th class=\"c3 header\" scope=\"col\">".get_string('fullnamecourse')."</th>\n";
54926e78 282 echo "<th class=\"c4 header\" scope=\"col\">".get_string('action')."</th>\n";
283 echo "<th class=\"c5 header\" scope=\"col\">".get_string('info')."</th>\n";
21283ddc 284 echo "</tr>\n";
1548978d 285
1e95d7b7 286 // Make sure that the logs array is an array, even it is empty, to avoid warnings from the foreach.
2b2d182a 287 if (empty($logs['logs'])) {
1e95d7b7 288 $logs['logs'] = array();
2b2d182a 289 }
238c0dd9 290
1548978d 291 $row = 1;
92890025 292 foreach ($logs['logs'] as $log) {
600149be 293
1548978d 294 $row = ($row + 1) % 2;
295
2eb68e6f 296 if (isset($ldcache[$log->module][$log->action])) {
297 $ld = $ldcache[$log->module][$log->action];
298 } else {
cb6fec1f 299 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
2eb68e6f 300 $ldcache[$log->module][$log->action] = $ld;
301 }
edf3ef00 302 if ($ld && is_numeric($log->info)) {
181b888e 303 // ugly hack to make sure fullname is shown correctly
cb6fec1f 304 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
305 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
181b888e 306 } else {
cb6fec1f 307 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
181b888e 308 }
600149be 309 }
310
264867fd 311 //Filter log->info
c8b0a50b 312 $log->info = format_string($log->info);
313
6c5a2108 314 // If $log->url has been trimmed short by the db size restriction
315 // code in add_to_log, keep a note so we don't add a link to a broken url
316 $tl=textlib_get_instance();
317 $brokenurl=($tl->strlen($log->url)==100 && $tl->substr($log->url,97)=='...');
318
d7d145b1 319 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
320 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
024ef528 321 $log->url = s($log->url); /// XSS protection and XHTML compatibility - should be in link_to_popup_window() instead!!
d7d145b1 322
1548978d 323 echo '<tr class="r'.$row.'">';
324 if ($course->id == SITEID) {
56595370 325 echo "<td class=\"cell c0\">\n";
bd5d0ce5 326 if (empty($log->course)) {
05a33439 327 echo get_string('site') . "\n";
bd5d0ce5 328 } else {
bf221aca 329 echo " <a href=\"{$CFG->wwwroot}/course/view.php?id={$log->course}\">". format_string($courses[$log->course])."</a>\n";
bd5d0ce5 330 }
21283ddc 331 echo "</td>\n";
720a43ce 332 }
56595370 333 echo "<td class=\"cell c1\" align=\"right\">".userdate($log->time, '%a').
21283ddc 334 ' '.userdate($log->time, $strftimedatetime)."</td>\n";
56595370 335 echo "<td class=\"cell c2\">\n";
7c09710c 336 link_to_popup_window("/iplookup/index.php?ip=$log->ip&amp;user=$log->userid", 'iplookup',$log->ip, 440, 700);
21283ddc 337 echo "</td>\n";
bf221aca 338 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
56595370 339 echo "<td class=\"cell c3\">\n";
bf221aca 340 echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}&amp;course={$log->course}\">$fullname</a>\n";
21283ddc 341 echo "</td>\n";
56595370 342 echo "<td class=\"cell c4\">\n";
6c5a2108 343 $displayaction="$log->module $log->action";
344 if($brokenurl) {
345 echo $displayaction;
346 } else {
347 link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',$displayaction, 440, 700);
348 }
21283ddc 349 echo "</td>\n";;
56595370 350 echo "<td class=\"cell c5\">{$log->info}</td>\n";
21283ddc 351 echo "</tr>\n";
f9903ed0 352 }
21283ddc 353 echo "</table>\n";
519d369f 354
8f0cd6ef 355 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
f9903ed0 356}
357
358
c215b32b 359function print_mnet_log($hostid, $course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
360 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
238c0dd9 361
cb6fec1f 362 global $CFG, $DB;
238c0dd9 363
c215b32b 364 if (!$logs = build_mnet_logs_array($hostid, $course, $user, $date, $order, $page*$perpage, $perpage,
365 $modname, $modid, $modaction, $groupid)) {
366 notify("No logs found!");
367 print_footer($course);
368 exit;
369 }
238c0dd9 370
c215b32b 371 if ($course->id == SITEID) {
372 $courses[0] = '';
373 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname,c.visible')) {
374 foreach ($ccc as $cc) {
375 $courses[$cc->id] = $cc->shortname;
376 }
377 }
378 }
238c0dd9 379
c215b32b 380 $totalcount = $logs['totalcount'];
381 $count=0;
382 $ldcache = array();
383 $tt = getdate(time());
384 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
385
386 $strftimedatetime = get_string("strftimedatetime");
387
5577ceb3 388 echo "<div class=\"info\">\n";
c215b32b 389 print_string("displayingrecords", "", $totalcount);
5577ceb3 390 echo "</div>\n";
c215b32b 391
392 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
393
5577ceb3 394 echo "<table class=\"logtable\" cellpadding=\"3\" cellspacing=\"0\">\n";
c215b32b 395 echo "<tr>";
396 if ($course->id == SITEID) {
397 echo "<th class=\"c0 header\">".get_string('course')."</th>\n";
398 }
399 echo "<th class=\"c1 header\">".get_string('time')."</th>\n";
400 echo "<th class=\"c2 header\">".get_string('ip_address')."</th>\n";
d6cc2341 401 echo "<th class=\"c3 header\">".get_string('fullnamecourse')."</th>\n";
c215b32b 402 echo "<th class=\"c4 header\">".get_string('action')."</th>\n";
403 echo "<th class=\"c5 header\">".get_string('info')."</th>\n";
404 echo "</tr>\n";
405
406 if (empty($logs['logs'])) {
407 echo "</table>\n";
408 return;
409 }
410
411 $row = 1;
412 foreach ($logs['logs'] as $log) {
238c0dd9 413
c215b32b 414 $log->info = $log->coursename;
415 $row = ($row + 1) % 2;
416
417 if (isset($ldcache[$log->module][$log->action])) {
418 $ld = $ldcache[$log->module][$log->action];
419 } else {
6bb08163 420 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
c215b32b 421 $ldcache[$log->module][$log->action] = $ld;
422 }
423 if (0 && $ld && !empty($log->info)) {
424 // ugly hack to make sure fullname is shown correctly
cb6fec1f 425 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
426 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
c215b32b 427 } else {
cb6fec1f 428 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
c215b32b 429 }
430 }
431
238c0dd9 432 //Filter log->info
c215b32b 433 $log->info = format_string($log->info);
434
435 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
436 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
437 $log->url = str_replace('&', '&amp;', $log->url); /// XHTML compatibility
438
439 echo '<tr class="r'.$row.'">';
440 if ($course->id == SITEID) {
5577ceb3 441 echo "<td class=\"r$row c0\" >\n";
c215b32b 442 echo " <a href=\"{$CFG->wwwroot}/course/view.php?id={$log->course}\">".$courses[$log->course]."</a>\n";
443 echo "</td>\n";
444 }
5577ceb3 445 echo "<td class=\"r$row c1\" align=\"right\">".userdate($log->time, '%a').
c215b32b 446 ' '.userdate($log->time, $strftimedatetime)."</td>\n";
5577ceb3 447 echo "<td class=\"r$row c2\" >\n";
c215b32b 448 link_to_popup_window("/iplookup/index.php?ip=$log->ip&amp;user=$log->userid", 'iplookup',$log->ip, 400, 700);
449 echo "</td>\n";
450 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
5577ceb3 451 echo "<td class=\"r$row c3\" >\n";
c215b32b 452 echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}\">$fullname</a>\n";
453 echo "</td>\n";
5577ceb3 454 echo "<td class=\"r$row c4\">\n";
c215b32b 455 echo $log->action .': '.$log->module;
456 echo "</td>\n";;
5577ceb3 457 echo "<td class=\"r$row c5\">{$log->info}</td>\n";
c215b32b 458 echo "</tr>\n";
459 }
460 echo "</table>\n";
461
462 print_paging_bar($totalcount, $page, $perpage, "$url&amp;perpage=$perpage&amp;");
463}
464
465
92890025 466function print_log_csv($course, $user, $date, $order='l.time DESC', $modname,
467 $modid, $modaction, $groupid) {
cb6fec1f 468 global $DB;
4068bedb 469
954fdb42 470 $text = get_string('course')."\t".get_string('time')."\t".get_string('ip_address')."\t".
d6cc2341 471 get_string('fullnamecourse')."\t".get_string('action')."\t".get_string('info');
264867fd 472
954fdb42 473 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
92890025 474 $modname, $modid, $modaction, $groupid)) {
475 return false;
476 }
264867fd 477
ea49a66c 478 $courses = array();
479
92890025 480 if ($course->id == SITEID) {
481 $courses[0] = '';
482 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
483 foreach ($ccc as $cc) {
484 $courses[$cc->id] = $cc->shortname;
485 }
486 }
ea49a66c 487 } else {
488 $courses[$course->id] = $course->shortname;
92890025 489 }
264867fd 490
92890025 491 $count=0;
492 $ldcache = array();
493 $tt = getdate(time());
494 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
495
496 $strftimedatetime = get_string("strftimedatetime");
92890025 497
954fdb42 498 $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
499 $filename .= '.txt';
264867fd 500 header("Content-Type: application/download\n");
954fdb42 501 header("Content-Disposition: attachment; filename=$filename");
502 header("Expires: 0");
503 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
504 header("Pragma: public");
505
506 echo get_string('savedat').userdate(time(), $strftimedatetime)."\n";
507 echo $text;
508
2b2d182a 509 if (empty($logs['logs'])) {
510 return true;
511 }
512
954fdb42 513 foreach ($logs['logs'] as $log) {
514 if (isset($ldcache[$log->module][$log->action])) {
515 $ld = $ldcache[$log->module][$log->action];
516 } else {
6bb08163 517 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
954fdb42 518 $ldcache[$log->module][$log->action] = $ld;
519 }
520 if ($ld && !empty($log->info)) {
521 // ugly hack to make sure fullname is shown correctly
cb6fec1f 522 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
523 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
954fdb42 524 } else {
cb6fec1f 525 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
954fdb42 526 }
527 }
528
264867fd 529 //Filter log->info
954fdb42 530 $log->info = format_string($log->info);
531
532 $log->url = strip_tags(urldecode($log->url)); // Some XSS protection
533 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
534 $log->url = str_replace('&', '&amp;', $log->url); // XHTML compatibility
535
536 $firstField = $courses[$log->course];
1c45e42e 537 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
954fdb42 538 $row = array($firstField, userdate($log->time, $strftimedatetime), $log->ip, $fullname, $log->module.' '.$log->action, $log->info);
539 $text = implode("\t", $row);
540 echo $text." \n";
541 }
542 return true;
92890025 543}
544
545
546function print_log_xls($course, $user, $date, $order='l.time DESC', $modname,
547 $modid, $modaction, $groupid) {
264867fd 548
cb6fec1f 549 global $CFG, $DB;
92890025 550
954fdb42 551 require_once("$CFG->libdir/excellib.class.php");
264867fd 552
954fdb42 553 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
92890025 554 $modname, $modid, $modaction, $groupid)) {
555 return false;
556 }
264867fd 557
ea49a66c 558 $courses = array();
559
92890025 560 if ($course->id == SITEID) {
561 $courses[0] = '';
562 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
563 foreach ($ccc as $cc) {
564 $courses[$cc->id] = $cc->shortname;
565 }
566 }
ea49a66c 567 } else {
568 $courses[$course->id] = $course->shortname;
92890025 569 }
264867fd 570
92890025 571 $count=0;
572 $ldcache = array();
573 $tt = getdate(time());
574 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
575
576 $strftimedatetime = get_string("strftimedatetime");
92890025 577
954fdb42 578 $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
579 $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
580 $filename .= '.xls';
264867fd 581
92890025 582 $workbook = new MoodleExcelWorkbook('-');
583 $workbook->send($filename);
264867fd 584
954fdb42 585 $worksheet = array();
586 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
d6cc2341 587 get_string('fullnamecourse'), get_string('action'), get_string('info'));
264867fd 588
954fdb42 589 // Creating worksheets
590 for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
0a013367 591 $sheettitle = get_string('logs').' '.$wsnumber.'-'.$nroPages;
954fdb42 592 $worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle);
593 $worksheet[$wsnumber]->set_column(1, 1, 30);
594 $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
595 userdate(time(), $strftimedatetime));
596 $col = 0;
597 foreach ($headers as $item) {
598 $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
599 $col++;
600 }
601 }
602
2b2d182a 603 if (empty($logs['logs'])) {
604 $workbook->close();
605 return true;
606 }
607
954fdb42 608 $formatDate =& $workbook->add_format();
609 $formatDate->set_num_format(get_string('log_excel_date_format'));
610
611 $row = FIRSTUSEDEXCELROW;
612 $wsnumber = 1;
613 $myxls =& $worksheet[$wsnumber];
614 foreach ($logs['logs'] as $log) {
615 if (isset($ldcache[$log->module][$log->action])) {
616 $ld = $ldcache[$log->module][$log->action];
617 } else {
cb6fec1f 618 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
954fdb42 619 $ldcache[$log->module][$log->action] = $ld;
620 }
621 if ($ld && !empty($log->info)) {
622 // ugly hack to make sure fullname is shown correctly
cb6fec1f 623 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
624 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
954fdb42 625 } else {
cb6fec1f 626 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
954fdb42 627 }
628 }
629
630 // Filter log->info
631 $log->info = format_string($log->info);
632 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
633
634 if ($nroPages>1) {
635 if ($row > EXCELROWS) {
636 $wsnumber++;
637 $myxls =& $worksheet[$wsnumber];
638 $row = FIRSTUSEDEXCELROW;
639 }
640 }
264867fd 641
954fdb42 642 $myxls->write($row, 0, $courses[$log->course], '');
643 // Excel counts from 1/1/1900
644 $excelTime=25569+$log->time/(3600*24);
645 $myxls->write($row, 1, $excelTime, $formatDate);
646 $myxls->write($row, 2, $log->ip, '');
1c45e42e 647 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
954fdb42 648 $myxls->write($row, 3, $fullname, '');
649 $myxls->write($row, 4, $log->module.' '.$log->action, '');
650 $myxls->write($row, 5, $log->info, '');
264867fd 651
954fdb42 652 $row++;
653 }
654
655 $workbook->close();
ea49a66c 656 return true;
657}
658
659function print_log_ods($course, $user, $date, $order='l.time DESC', $modname,
660 $modid, $modaction, $groupid) {
661
cb6fec1f 662 global $CFG, $DB;
ea49a66c 663
664 require_once("$CFG->libdir/odslib.class.php");
665
666 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
667 $modname, $modid, $modaction, $groupid)) {
668 return false;
669 }
670
671 $courses = array();
672
673 if ($course->id == SITEID) {
674 $courses[0] = '';
675 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
676 foreach ($ccc as $cc) {
677 $courses[$cc->id] = $cc->shortname;
678 }
679 }
680 } else {
681 $courses[$course->id] = $course->shortname;
682 }
683
684 $count=0;
685 $ldcache = array();
686 $tt = getdate(time());
687 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
688
689 $strftimedatetime = get_string("strftimedatetime");
690
691 $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
692 $filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
693 $filename .= '.ods';
694
695 $workbook = new MoodleODSWorkbook('-');
696 $workbook->send($filename);
697
698 $worksheet = array();
699 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
d6cc2341 700 get_string('fullnamecourse'), get_string('action'), get_string('info'));
ea49a66c 701
702 // Creating worksheets
703 for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
0a013367 704 $sheettitle = get_string('logs').' '.$wsnumber.'-'.$nroPages;
ea49a66c 705 $worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle);
706 $worksheet[$wsnumber]->set_column(1, 1, 30);
707 $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
708 userdate(time(), $strftimedatetime));
709 $col = 0;
710 foreach ($headers as $item) {
711 $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
712 $col++;
713 }
714 }
715
716 if (empty($logs['logs'])) {
717 $workbook->close();
718 return true;
719 }
720
721 $formatDate =& $workbook->add_format();
722 $formatDate->set_num_format(get_string('log_excel_date_format'));
723
724 $row = FIRSTUSEDEXCELROW;
725 $wsnumber = 1;
726 $myxls =& $worksheet[$wsnumber];
727 foreach ($logs['logs'] as $log) {
728 if (isset($ldcache[$log->module][$log->action])) {
729 $ld = $ldcache[$log->module][$log->action];
730 } else {
cb6fec1f 731 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
ea49a66c 732 $ldcache[$log->module][$log->action] = $ld;
733 }
734 if ($ld && !empty($log->info)) {
735 // ugly hack to make sure fullname is shown correctly
7e60297f 736 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
cb6fec1f 737 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
ea49a66c 738 } else {
cb6fec1f 739 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
ea49a66c 740 }
741 }
742
743 // Filter log->info
744 $log->info = format_string($log->info);
745 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
746
747 if ($nroPages>1) {
748 if ($row > EXCELROWS) {
749 $wsnumber++;
750 $myxls =& $worksheet[$wsnumber];
751 $row = FIRSTUSEDEXCELROW;
752 }
753 }
754
d81b7ffb 755 $myxls->write_string($row, 0, $courses[$log->course]);
756 $myxls->write_date($row, 1, $log->time);
757 $myxls->write_string($row, 2, $log->ip);
ea49a66c 758 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
d81b7ffb 759 $myxls->write_string($row, 3, $fullname);
760 $myxls->write_string($row, 4, $log->module.' '.$log->action);
761 $myxls->write_string($row, 5, $log->info);
ea49a66c 762
763 $row++;
764 }
765
766 $workbook->close();
954fdb42 767 return true;
92890025 768}
769
92890025 770
c2cb4545 771function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
a683deec 772 global $CFG, $USER;
c2cb4545 773 if (empty($CFG->gdversion)) {
774 echo "(".get_string("gdneed").")";
d887b5a7 775 } else {
a683deec 776 // MDL-10818, do not display broken graph when user has no permission to view graph
777 if (has_capability('moodle/site:viewreports', get_context_instance(CONTEXT_COURSE, $course->id)) ||
778 ($course->showreports and $USER->id == $userid)) {
779 echo '<img src="'.$CFG->wwwroot.'/course/report/log/graph.php?id='.$course->id.
780 '&amp;user='.$userid.'&amp;type='.$type.'&amp;date='.$date.'" alt="" />';
781 }
d887b5a7 782 }
783}
784
785
185cfb09 786function print_overview($courses) {
6bb08163 787 global $CFG, $USER, $DB;
0d6b9d4f 788
185cfb09 789 $htmlarray = array();
6bb08163 790 if ($modules = $DB->get_records('modules')) {
f8716988 791 foreach ($modules as $mod) {
792 if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) {
f461e8ec 793 include_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php');
f8716988 794 $fname = $mod->name.'_print_overview';
0d6b9d4f 795 if (function_exists($fname)) {
185cfb09 796 $fname($courses,$htmlarray);
0d6b9d4f 797 }
798 }
799 }
800 }
185cfb09 801 foreach ($courses as $course) {
fe5a1e23 802 print_simple_box_start('center', '100%', '', 5, "coursebox");
185cfb09 803 $linkcss = '';
804 if (empty($course->visible)) {
805 $linkcss = 'class="dimmed"';
806 }
6ba65fa0 807 print_heading('<a title="'. format_string($course->fullname).'" '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'. format_string($course->fullname).'</a>');
185cfb09 808 if (array_key_exists($course->id,$htmlarray)) {
809 foreach ($htmlarray[$course->id] as $modname => $html) {
810 echo $html;
811 }
812 }
813 print_simple_box_end();
814 }
0d6b9d4f 815}
816
817
cb6fec1f 818/**
819 * This function trawls through the logs looking for
820 * anything new since the user's last login
821 */
600149be 822function print_recent_activity($course) {
823 // $course is an object
cb6fec1f 824 global $CFG, $USER, $SESSION, $DB;
600149be 825
e2a3a0e7 826 $context = get_context_instance(CONTEXT_COURSE, $course->id);
2ac64806 827
dd97c328 828 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
829
830 $timestart = round(time() - COURSE_MAX_RECENT_PERIOD, -2); // better db caching for guests - 100 seconds
0f87cb1d 831
e2a3a0e7 832 if (!has_capability('moodle/legacy:guest', $context, NULL, false)) {
833 if (!empty($USER->lastcourseaccess[$course->id])) {
834 if ($USER->lastcourseaccess[$course->id] > $timestart) {
835 $timestart = $USER->lastcourseaccess[$course->id];
836 }
9e51847a 837 }
3d891989 838 }
0f87cb1d 839
de785682 840 echo '<div class="activitydate">';
27bf9e20 841 echo get_string('activitysince', '', userdate($timestart));
de785682 842 echo '</div>';
843 echo '<div class="activityhead">';
0f87cb1d 844
de785682 845 echo '<a href="'.$CFG->wwwroot.'/course/recent.php?id='.$course->id.'">'.get_string('recentactivityreport').'</a>';
0f87cb1d 846
5fc835a5 847 echo "</div>\n";
0f87cb1d 848
600149be 849 $content = false;
1b5910c4 850
dd97c328 851/// Firstly, have there been any new enrolments?
852
6c38b7e0 853 $users = get_recent_enrolments($course->id, $timestart);
1b5910c4 854
5fc835a5 855 //Accessibility: new users now appear in an <OL> list.
6c38b7e0 856 if ($users) {
27bf9e20 857 echo '<div class="newusers">';
dd97c328 858 print_headline(get_string("newusers").':', 3);
859 $content = true;
5fc835a5 860 echo "<ol class=\"list\">\n";
6c38b7e0 861 foreach ($users as $user) {
dd97c328 862 $fullname = fullname($user, $viewfullnames);
863 echo '<li class="name"><a href="'."$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></li>\n";
600149be 864 }
5fc835a5 865 echo "</ol>\n</div>\n";
600149be 866 }
867
dd97c328 868/// Next, have there been any modifications to the course structure?
869
870 $modinfo =& get_fast_modinfo($course);
871
872 $changelist = array();
1b5910c4 873
cb6fec1f 874 $logs = $DB->get_records_select('log', "time > ? AND course = ? AND
875 module = 'course' AND
876 (action = 'add mod' OR action = 'update mod' OR action = 'delete mod')",
877 array($timestart, $course->id), "id ASC");
1b5910c4 878
879 if ($logs) {
dd97c328 880 $actions = array('add mod', 'update mod', 'delete mod');
881 $newgones = array(); // added and later deleted items
1b5910c4 882 foreach ($logs as $key => $log) {
dd97c328 883 if (!in_array($log->action, $actions)) {
884 continue;
885 }
27bf9e20 886 $info = split(' ', $log->info);
c9f6251e 887
dd97c328 888 if ($info[0] == 'label') { // Labels are ignored in recent activity
c9f6251e 889 continue;
890 }
891
dd97c328 892 if (count($info) != 2) {
893 debugging("Incorrect log entry info: id = ".$log->id, DEBUG_DEVELOPER);
894 continue;
895 }
896
897 $modname = $info[0];
898 $instanceid = $info[1];
899
900 if ($log->action == 'delete mod') {
901 // unfortunately we do not know if the mod was visible
902 if (!array_key_exists($log->info, $newgones)) {
903 $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $modname));
904 $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted);
905 }
906 } else {
907 if (!isset($modinfo->instances[$modname][$instanceid])) {
908 if ($log->action == 'add mod') {
909 // do not display added and later deleted activities
910 $newgones[$log->info] = true;
911 }
912 continue;
913 }
914 $cm = $modinfo->instances[$modname][$instanceid];
915 if (!$cm->uservisible) {
ff96219d 916 continue;
dd97c328 917 }
918
919 if ($log->action == 'add mod') {
920 $stradded = get_string('added', 'moodle', get_string('modulename', $modname));
921 $changelist[$log->info] = array('operation' => 'add', 'text' => "$stradded:<br /><a href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true)."</a>");
922
923 } else if ($log->action == 'update mod' and empty($changelist[$log->info])) {
924 $strupdated = get_string('updated', 'moodle', get_string('modulename', $modname));
925 $changelist[$log->info] = array('operation' => 'update', 'text' => "$strupdated:<br /><a href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true)."</a>");
600149be 926 }
ef25340c 927 }
928 }
929 }
930
9c9f7d77 931 if (!empty($changelist)) {
dd97c328 932 print_headline(get_string('courseupdates').':', 3);
933 $content = true;
ef25340c 934 foreach ($changelist as $changeinfo => $change) {
dd97c328 935 echo '<p class="activity">'.$change['text'].'</p>';
600149be 936 }
89adb174 937 }
bf40f9c1 938
dd97c328 939/// Now display new things from each module
0fd7da81 940
dd97c328 941 $usedmodules = array();
942 foreach($modinfo->cms as $cm) {
943 if (isset($usedmodules[$cm->modname])) {
944 continue;
945 }
946 if (!$cm->uservisible) {
947 continue;
948 }
949 $usedmodules[$cm->modname] = $cm->modname;
950 }
e2a3a0e7 951
dd97c328 952 foreach ($usedmodules as $modname) { // Each module gets it's own logs and prints them
953 if (file_exists($CFG->dirroot.'/mod/'.$modname.'/lib.php')) {
954 include_once($CFG->dirroot.'/mod/'.$modname.'/lib.php');
955 $print_recent_activity = $modname.'_print_recent_activity';
296c6ac2 956 if (function_exists($print_recent_activity)) {
dd97c328 957 // NOTE: original $isteacher (second parameter below) was replaced with $viewfullnames!
958 $content = $print_recent_activity($course, $viewfullnames, $timestart) || $content;
600149be 959 }
296c6ac2 960 } else {
238c0dd9 961 debugging("Missing lib.php in lib/{$modname} - please reinstall files or uninstall the module");
600149be 962 }
963 }
964
965 if (! $content) {
27bf9e20 966 echo '<p class="message">'.get_string('nothingnew').'</p>';
600149be 967 }
600149be 968}
969
cb6fec1f 970/**
971 * For a given course, returns an array of course activity objects
972 * Each item in the array contains he following properties:
973 */
d897cae4 974function get_array_of_activities($courseid) {
d897cae4 975// cm - course module id
976// mod - name of the module (eg forum)
977// section - the number of the section (eg week or topic)
978// name - the name of the instance
5867bfb5 979// visible - is the instance visible or not
13534ef7
ML
980// groupingid - grouping id
981// groupmembersonly - is this instance visible to group members only
86aa7ccf 982// extra - contains extra string to include in any link
cb6fec1f 983 global $CFG, $DB;
8dddba42 984
d897cae4 985 $mod = array();
986
9fa49e22 987 if (!$rawmods = get_course_mods($courseid)) {
dd97c328 988 return $mod; // always return array
d897cae4 989 }
990
cb6fec1f 991 if ($sections = $DB->get_records("course_sections", array("course"=>$courseid), "section ASC")) {
d897cae4 992 foreach ($sections as $section) {
74666583 993 if (!empty($section->sequence)) {
d897cae4 994 $sequence = explode(",", $section->sequence);
995 foreach ($sequence as $seq) {
7af6281f 996 if (empty($rawmods[$seq])) {
997 continue;
998 }
dd97c328 999 $mod[$seq]->id = $rawmods[$seq]->instance;
1000 $mod[$seq]->cm = $rawmods[$seq]->id;
1001 $mod[$seq]->mod = $rawmods[$seq]->modname;
1002 $mod[$seq]->section = $section->section;
dd97c328 1003 $mod[$seq]->visible = $rawmods[$seq]->visible;
1004 $mod[$seq]->groupmode = $rawmods[$seq]->groupmode;
1005 $mod[$seq]->groupingid = $rawmods[$seq]->groupingid;
13534ef7 1006 $mod[$seq]->groupmembersonly = $rawmods[$seq]->groupmembersonly;
dd97c328 1007 $mod[$seq]->extra = "";
8dddba42 1008
1009 $modname = $mod[$seq]->mod;
1010 $functionname = $modname."_get_coursemodule_info";
1011
3a37b3f8 1012 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
1013 continue;
1014 }
1015
8dddba42 1016 include_once("$CFG->dirroot/mod/$modname/lib.php");
1017
1018 if (function_exists($functionname)) {
9d361034 1019 if ($info = $functionname($rawmods[$seq])) {
1020 if (!empty($info->extra)) {
1021 $mod[$seq]->extra = $info->extra;
1022 }
1023 if (!empty($info->icon)) {
1024 $mod[$seq]->icon = $info->icon;
1025 }
1ea543df 1026 if (!empty($info->name)) {
1027 $mod[$seq]->name = urlencode($info->name);
1028 }
c9f6251e 1029 }
1030 }
1ea543df 1031 if (!isset($mod[$seq]->name)) {
a5d424df 1032 $mod[$seq]->name = urlencode($DB->get_field($rawmods[$seq]->modname, "name", array("id"=>$rawmods[$seq]->instance)));
1ea543df 1033 }
d897cae4 1034 }
1035 }
1036 }
1037 }
1038 return $mod;
1039}
1040
1041
dd97c328 1042/**
1043 * Returns reference to full info about modules in course (including visibility).
1044 * Cached and as fast as possible (0 or 1 db query).
65a00c97 1045 * @param $course object or 'reset' string to reset caches, modinfo may be updated in db
dd97c328 1046 * @return mixed courseinfo object or nothing if resetting
1047 */
65a00c97 1048function &get_fast_modinfo(&$course, $userid=0) {
cb6fec1f 1049 global $CFG, $USER, $DB;
dd97c328 1050
1051 static $cache = array();
1052
1053 if ($course === 'reset') {
1054 $cache = array();
1055 $nothing = null;
1056 return $nothing; // we must return some reference
1057 }
1058
1059 if (empty($userid)) {
1060 $userid = $USER->id;
1061 }
1062
1063 if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) {
1064 return $cache[$course->id];
1065 }
1066
1067 if (empty($course->modinfo)) {
1068 // no modinfo yet - load it
1069 rebuild_course_cache($course->id);
cb6fec1f 1070 $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
dd97c328 1071 }
1072
1073 $modinfo = new object();
1074 $modinfo->courseid = $course->id;
1075 $modinfo->userid = $userid;
1076 $modinfo->sections = array();
1077 $modinfo->cms = array();
1078 $modinfo->instances = array();
1079 $modinfo->groups = null; // loaded only when really needed - the only one db query
1080
1081 $info = unserialize($course->modinfo);
1082 if (!is_array($info)) {
1083 // hmm, something is wrong - lets try to fix it
1084 rebuild_course_cache($course->id);
cb6fec1f 1085 $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
dd97c328 1086 $info = unserialize($course->modinfo);
1087 if (!is_array($info)) {
1088 return $modinfo;
1089 }
1090 }
1091
1092 if ($info) {
1093 // detect if upgrade required
1094 $first = reset($info);
1095 if (!isset($first->id)) {
1096 rebuild_course_cache($course->id);
cb6fec1f 1097 $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
dd97c328 1098 $info = unserialize($course->modinfo);
1099 if (!is_array($info)) {
1100 return $modinfo;
1101 }
1102 }
1103 }
1104
1105 $modlurals = array();
1106
65bcf17b 1107 $cmids = array();
1108 $contexts = null;
1109 foreach ($info as $mod) {
1110 $cmids[$mod->cm] = $mod->cm;
1111 }
1112 if ($cmids) {
1113 // preload all module contexts with one query
1114 $contexts = get_context_instance(CONTEXT_MODULE, $cmids);
1115 }
1116
dd97c328 1117 foreach ($info as $mod) {
7c3b032e 1118 if (empty($mod->name)) {
1119 // something is wrong here
1120 continue;
1121 }
dd97c328 1122 // reconstruct minimalistic $cm
1123 $cm = new object();
1124 $cm->id = $mod->cm;
1125 $cm->instance = $mod->id;
1126 $cm->course = $course->id;
1127 $cm->modname = $mod->mod;
1128 $cm->name = urldecode($mod->name);
1129 $cm->visible = $mod->visible;
76cbde41 1130 $cm->sectionnum = $mod->section;
dd97c328 1131 $cm->groupmode = $mod->groupmode;
1132 $cm->groupingid = $mod->groupingid;
1133 $cm->groupmembersonly = $mod->groupmembersonly;
1134 $cm->extra = isset($mod->extra) ? urldecode($mod->extra) : '';
1135 $cm->icon = isset($mod->icon) ? $mod->icon : '';
1136 $cm->uservisible = true;
1137
7c3b032e 1138 // preload long names plurals and also check module is installed properly
dd97c328 1139 if (!isset($modlurals[$cm->modname])) {
7c3b032e 1140 if (!file_exists("$CFG->dirroot/mod/$cm->modname/lib.php")) {
1141 continue;
1142 }
dd97c328 1143 $modlurals[$cm->modname] = get_string('modulenameplural', $cm->modname);
1144 }
1145 $cm->modplural = $modlurals[$cm->modname];
1146
65bcf17b 1147 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $contexts[$cm->id], $userid)) {
dd97c328 1148 $cm->uservisible = false;
1149
1150 } else if (!empty($CFG->enablegroupings) and !empty($cm->groupmembersonly)
65bcf17b 1151 and !has_capability('moodle/site:accessallgroups', $contexts[$cm->id], $userid)) {
dd97c328 1152 if (is_null($modinfo->groups)) {
1153 $modinfo->groups = groups_get_user_groups($course->id, $userid);
1154 }
1155 if (empty($modinfo->groups[$cm->groupingid])) {
1156 $cm->uservisible = false;
1157 }
1158 }
1159
1160 if (!isset($modinfo->instances[$cm->modname])) {
1161 $modinfo->instances[$cm->modname] = array();
1162 }
1163 $modinfo->instances[$cm->modname][$cm->instance] =& $cm;
1164 $modinfo->cms[$cm->id] =& $cm;
1165
1166 // reconstruct sections
76cbde41 1167 if (!isset($modinfo->sections[$cm->sectionnum])) {
1168 $modinfo->sections[$cm->sectionnum] = array();
dd97c328 1169 }
76cbde41 1170 $modinfo->sections[$cm->sectionnum][] = $cm->id;
dd97c328 1171
1172 unset($cm);
1173 }
1174
76cbde41 1175 unset($cache[$course->id]); // prevent potential reference problems when switching users
dd97c328 1176 $cache[$course->id] = $modinfo;
1177
1178 return $cache[$course->id];
1179}
d897cae4 1180
cb6fec1f 1181/**
1182 * Returns a number of useful structures for course displays
1183 */
90845098 1184function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
238c0dd9 1185 global $DB;
7468bf01 1186
dd97c328 1187 $mods = array(); // course modules indexed by id
1188 $modnames = array(); // all course module names (except resource!)
1189 $modnamesplural= array(); // all course module names (plural form)
1190 $modnamesused = array(); // course module names used
7468bf01 1191
cb6fec1f 1192 if ($allmods = $DB->get_records("modules")) {
90845098 1193 foreach ($allmods as $mod) {
5867bfb5 1194 if ($mod->visible) {
1195 $modnames[$mod->name] = get_string("modulename", "$mod->name");
1196 $modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
1197 }
90845098 1198 }
91374f3e 1199 asort($modnames, SORT_LOCALE_STRING);
90845098 1200 } else {
ba6018a9 1201 print_error("nomodules", 'debug');
90845098 1202 }
1203
9fa49e22 1204 if ($rawmods = get_course_mods($courseid)) {
7468bf01 1205 foreach($rawmods as $mod) { // Index the mods
959ae824 1206 if (empty($modnames[$mod->modname])) {
1207 continue;
1208 }
dd97c328 1209 $mods[$mod->id] = $mod;
1210 $mods[$mod->id]->modfullname = $modnames[$mod->modname];
1211 if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) {
1212 continue;
1213 }
13534ef7
ML
1214 // Check groupings
1215 if (!groups_course_module_visible($mod)) {
1216 continue;
1217 }
dd97c328 1218 $modnamesused[$mod->modname] = $modnames[$mod->modname];
7468bf01 1219 }
c7da6f7a 1220 if ($modnamesused) {
dba21d4a 1221 asort($modnamesused, SORT_LOCALE_STRING);
c7da6f7a 1222 }
7468bf01 1223 }
7468bf01 1224}
1225
9fa49e22 1226
7468bf01 1227function get_all_sections($courseid) {
cb6fec1f 1228 global $DB;
1229 return $DB->get_records("course_sections", array("course"=>"$courseid"), "section",
7d99d695 1230 "section, id, course, summary, sequence, visible");
7468bf01 1231}
1232
b86fc0e2 1233function course_set_display($courseid, $display=0) {
cb6fec1f 1234 global $USER, $DB;
b86fc0e2 1235
b86fc0e2 1236 if ($display == "all" or empty($display)) {
1237 $display = 0;
1238 }
1239
7b678e0a 1240 if (empty($USER->id) or $USER->username == 'guest') {
1241 //do not store settings in db for guests
238c0dd9 1242 } else if ($DB->record_exists("course_display", array("userid" => $USER->id, "course"=>$courseid))) {
cb6fec1f 1243 $DB->set_field("course_display", "display", $display, array("userid"=>$USER->id, "course"=>$courseid));
b86fc0e2 1244 } else {
dd97c328 1245 $record = new object();
b86fc0e2 1246 $record->userid = $USER->id;
1247 $record->course = $courseid;
1248 $record->display = $display;
cb6fec1f 1249 if (!$DB->insert_record("course_display", $record)) {
b86fc0e2 1250 notify("Could not save your course display!");
1251 }
1252 }
1253
1254 return $USER->display[$courseid] = $display; // Note: = not ==
1255}
1256
cb6fec1f 1257/**
1258 * For a given course section, markes it visible or hidden,
1259 * and does the same for every activity in that section
1260 */
7d99d695 1261function set_section_visible($courseid, $sectionnumber, $visibility) {
cb6fec1f 1262 global $DB;
7d99d695 1263
cb6fec1f 1264 if ($section = $DB->get_record("course_sections", array("course"=>$courseid, "section"=>$sectionnumber))) {
1265 $DB->set_field("course_sections", "visible", "$visibility", array("id"=>$section->id));
7d99d695 1266 if (!empty($section->sequence)) {
1267 $modules = explode(",", $section->sequence);
1268 foreach ($modules as $moduleid) {
02f66c42 1269 set_coursemodule_visible($moduleid, $visibility, true);
7d99d695 1270 }
1271 }
5867bfb5 1272 rebuild_course_cache($courseid);
7d99d695 1273 }
1274}
ba2e5d73 1275
cb6fec1f 1276/**
1277 * Prints a section full of activity modules
1278 */
d897cae4 1279function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
cb6fec1f 1280 global $CFG, $USER, $DB;
7977cffd 1281
dd97c328 1282 static $initialised;
1283
3d575e6f 1284 static $groupbuttons;
32d03b7b 1285 static $groupbuttonslink;
52dcc2f9 1286 static $isediting;
7977cffd 1287 static $ismoving;
1288 static $strmovehere;
1289 static $strmovefull;
54669989 1290 static $strunreadpostsone;
a2d71d8e 1291 static $usetracking;
dd97c328 1292 static $groupings;
4877707e 1293
110a32e2 1294
dd97c328 1295 if (!isset($initialised)) {
9fd9c29b 1296 $groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
32d03b7b 1297 $groupbuttonslink = (!$course->groupmodeforce);
dd97c328 1298 $isediting = isediting($course->id);
1299 $ismoving = $isediting && ismoving($course->id);
3d575e6f 1300 if ($ismoving) {
dd97c328 1301 $strmovehere = get_string("movehere");
1302 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
3d575e6f 1303 }
94a6a70f 1304 include_once($CFG->dirroot.'/mod/forum/lib.php');
1305 if ($usetracking = forum_tp_can_track_forums()) {
dd97c328 1306 $strunreadpostsone = get_string('unreadpostsone', 'forum');
a2d71d8e 1307 }
dd97c328 1308 $initialised = true;
7977cffd 1309 }
dd97c328 1310
1311 $labelformatoptions = new object();
60bd11cf 1312 $labelformatoptions->noclean = true;
94361e02 1313
fea43a7f 1314/// Casting $course->modinfo to string prevents one notice when the field is null
dd97c328 1315 $modinfo = get_fast_modinfo($course);
238c0dd9 1316
94361e02 1317
c6a55371 1318 //Acccessibility: replace table with list <ul>, but don't output empty list.
74666583 1319 if (!empty($section->sequence)) {
94361e02 1320
f2d660dc 1321 // Fix bug #5027, don't want style=\"width:$width\".
6285f8a8 1322 echo "<ul class=\"section img-text\">\n";
94361e02 1323 $sectionmods = explode(",", $section->sequence);
1324
1325 foreach ($sectionmods as $modnumber) {
9ae687af 1326 if (empty($mods[$modnumber])) {
1327 continue;
1328 }
dd97c328 1329
94361e02 1330 $mod = $mods[$modnumber];
c9f6251e 1331
dd97c328 1332 if ($ismoving and $mod->id == $USER->activitycopy) {
1333 // do not display moving mod
1334 continue;
1335 }
c9f6251e 1336
dd97c328 1337 if (isset($modinfo->cms[$modnumber])) {
1338 if (!$modinfo->cms[$modnumber]->uservisible) {
1339 // visibility shortcut
1340 continue;
86aa7ccf 1341 }
dd97c328 1342 } else {
0f56c9da 1343 if (!file_exists("$CFG->dirroot/mod/$mod->modname/lib.php")) {
1344 // module not installed
1345 continue;
1346 }
dd97c328 1347 if (!coursemodule_visible_for_user($mod)) {
1348 // full visibility check
1349 continue;
9d361034 1350 }
dd97c328 1351 }
1352
1353 echo '<li class="activity '.$mod->modname.'" id="module-'.$modnumber.'">'; // Unique ID
1354 if ($ismoving) {
1355 echo '<a title="'.$strmovefull.'"'.
1356 ' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.$USER->sesskey.'">'.
1357 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
1358 ' alt="'.$strmovehere.'" /></a><br />
1359 ';
1360 }
9d361034 1361
dd97c328 1362 if ($mod->indent) {
1363 print_spacer(12, 20 * $mod->indent, false);
1364 }
1365
554606c7 1366 $extra = '';
1367 if (!empty($modinfo->cms[$modnumber]->extra)) {
1368 $extra = $modinfo->cms[$modnumber]->extra;
1369 }
dd97c328 1370
1371 if ($mod->modname == "label") {
1372 if (!$mod->visible) {
9009a990 1373 echo "<div class=\"dimmed_text\">";
dd97c328 1374 }
1375 echo format_text($extra, FORMAT_HTML, $labelformatoptions);
1376 if (!$mod->visible) {
9009a990 1377 echo "</div>";
aac94fd0 1378 }
c4d989a1 1379 if (!empty($CFG->enablegroupings) && !empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
1380 if (!isset($groupings)) {
1381 $groupings = groups_get_all_groupings($course->id);
1382 }
1470825e 1383 echo " <span class=\"groupinglabel\">(".format_string($groupings[$mod->groupingid]->name).')</span>';
c4d989a1 1384 }
aac94fd0 1385
dd97c328 1386 } else { // Normal activity
1387 $instancename = format_string($modinfo->cms[$modnumber]->name, true, $course->id);
c9f6251e 1388
dd97c328 1389 if (!empty($modinfo->cms[$modnumber]->icon)) {
1390 $icon = "$CFG->pixpath/".$modinfo->cms[$modnumber]->icon;
1391 } else {
1392 $icon = "$CFG->modpixpath/$mod->modname/icon.gif";
1393 }
e0b033d5 1394
dd97c328 1395 //Accessibility: for files get description via icon.
1396 $altname = '';
1397 if ('resource'==$mod->modname) {
1398 if (!empty($modinfo->cms[$modnumber]->icon)) {
1399 $possaltname = $modinfo->cms[$modnumber]->icon;
6285f8a8 1400
dd97c328 1401 $mimetype = mimeinfo_from_icon('type', $possaltname);
1402 $altname = get_mimetype_description($mimetype);
6285f8a8 1403 } else {
1404 $altname = $mod->modfullname;
1405 }
dd97c328 1406 } else {
1407 $altname = $mod->modfullname;
1408 }
1409 // Avoid unnecessary duplication.
1410 if (false!==stripos($instancename, $altname)) {
1411 $altname = '';
1412 }
1413 // File type after name, for alphabetic lists (screen reader).
1414 if ($altname) {
1415 $altname = get_accesshide(' '.$altname);
1416 }
6285f8a8 1417
dd97c328 1418 $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
1419 echo '<a '.$linkcss.' '.$extra. // Title unnecessary!
1420 ' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.
1421 '<img src="'.$icon.'" class="activityicon" alt="" /> <span>'.
1422 $instancename.$altname.'</span></a>';
806ebc15 1423
dd97c328 1424 if (!empty($CFG->enablegroupings) && !empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
1425 if (!isset($groupings)) {
1426 $groupings = groups_get_all_groupings($course->id);
acf000b0 1427 }
1470825e 1428 echo " <span class=\"groupinglabel\">(".format_string($groupings[$mod->groupingid]->name).')</span>';
c9f6251e 1429 }
dd97c328 1430 }
1431 if ($usetracking && $mod->modname == 'forum') {
90f4745c 1432 if ($unread = forum_tp_count_forum_unread_posts($mod, $course)) {
1433 echo '<span class="unread"> <a href="'.$CFG->wwwroot.'/mod/forum/view.php?id='.$mod->id.'">';
1434 if ($unread == 1) {
1435 echo $strunreadpostsone;
1436 } else {
1437 print_string('unreadpostsnumber', 'forum', $unread);
54669989 1438 }
90f4745c 1439 echo '</a></span>';
f37da850 1440 }
dd97c328 1441 }
f37da850 1442
dd97c328 1443 if ($isediting) {
1444 // TODO: we must define this as mod property!
1445 if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource' and $mod->modname != 'glossary') {
1446 if (! $mod->groupmodelink = $groupbuttonslink) {
1447 $mod->groupmode = $course->groupmode;
3d575e6f 1448 }
dd97c328 1449
1450 } else {
1451 $mod->groupmode = false;
c9f6251e 1452 }
dd97c328 1453 echo '&nbsp;&nbsp;';
1454 echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section);
94361e02 1455 }
dd97c328 1456 echo "</li>\n";
94361e02 1457 }
dd97c328 1458
f2d660dc 1459 } elseif ($ismoving) {
1460 echo "<ul class=\"section\">\n";
264867fd 1461 }
dd97c328 1462
7977cffd 1463 if ($ismoving) {
64fdc686 1464 echo '<li><a title="'.$strmovefull.'"'.
8b92f5bb 1465 ' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.$USER->sesskey.'">'.
446390fb 1466 '<img class="movetarget" src="'.$CFG->pixpath.'/movehere.gif" '.
c6a55371 1467 ' alt="'.$strmovehere.'" /></a></li>
1c919752 1468 ';
7977cffd 1469 }
c6a55371 1470 if (!empty($section->sequence) || $ismoving) {
1471 echo "</ul><!--class='section'-->\n\n";
1472 }
a7ad3ea6 1473}
1474
89bfeee0 1475/**
1476 * Prints the menus to add activities and resources.
1477 */
cb57e6f4 1478function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false) {
89bfeee0 1479 global $CFG;
e0161bff 1480
217a8ee9 1481 // check to see if user can add menus
1482 if (!has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))) {
e2cd3ed0 1483 return false;
217a8ee9 1484 }
1485
89bfeee0 1486 static $resources = false;
1487 static $activities = false;
e0161bff 1488
238c0dd9 1489 if ($resources === false) {
89bfeee0 1490 $resources = array();
1491 $activities = array();
6da4b261 1492
89bfeee0 1493 foreach($modnames as $modname=>$modnamestr) {
1494 if (!course_allowed_module($course, $modname)) {
1495 continue;
1496 }
6da4b261 1497
ffd58dd6 1498 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
1499 if (!file_exists($libfile)) {
1500 continue;
1501 }
1502 include_once($libfile);
89bfeee0 1503 $gettypesfunc = $modname.'_get_types';
1504 if (function_exists($gettypesfunc)) {
1505 $types = $gettypesfunc();
1506 foreach($types as $type) {
65bcf17b 1507 if (!isset($type->modclass) or !isset($type->typestr)) {
ddb0a19f 1508 debugging('Incorrect activity type in '.$modname);
65bcf17b 1509 continue;
1510 }
89bfeee0 1511 if ($type->modclass == MOD_CLASS_RESOURCE) {
1512 $resources[$type->type] = $type->typestr;
1513 } else {
1514 $activities[$type->type] = $type->typestr;
1515 }
1516 }
1517 } else {
1518 // all mods without type are considered activity
1519 $activities[$modname] = $modnamestr;
1520 }
0705ff84 1521 }
e0161bff 1522 }
1523
89bfeee0 1524 $straddactivity = get_string('addactivity');
1525 $straddresource = get_string('addresource');
1526
4f24b3e3 1527 $output = '<div class="section_add_menus">';
1528
1529 if (!$vertical) {
1530 $output .= '<div class="horizontal">';
1531 }
89bfeee0 1532
1533 if (!empty($resources)) {
1534 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=".sesskey()."&amp;add=",
0705ff84 1535 $resources, "ressection$section", "", $straddresource, 'resource/types', $straddresource, true);
1536 }
cb57e6f4 1537
89bfeee0 1538 if (!empty($activities)) {
1539 $output .= ' ';
1540 $output .= popup_form("$CFG->wwwroot/course/mod.php?id=$course->id&amp;section=$section&amp;sesskey=".sesskey()."&amp;add=",
1541 $activities, "section$section", "", $straddactivity, 'mods', $straddactivity, true);
0705ff84 1542 }
1543
4f24b3e3 1544 if (!$vertical) {
d33d0cda 1545 $output .= '</div>';
1546 }
1547
cb57e6f4 1548 $output .= '</div>';
1549
1550 if ($return) {
1551 return $output;
1552 } else {
1553 echo $output;
1554 }
e0161bff 1555}
1556
f36cbf1d 1557/**
1558 * Rebuilds the cached list of course activities stored in the database
1559 * @param int $courseid - id of course to rebuil, empty means all
1560 * @param boolean $clearonly - only clear the modinfo fields, gets rebuild automatically on the fly
1561 */
1562function rebuild_course_cache($courseid=0, $clearonly=false) {
f33e1ed4 1563 global $COURSE, $DB;
f36cbf1d 1564
1565 if ($clearonly) {
1c69b885 1566 if (empty($courseid)) {
1567 $courseselect = array();
1568 } else {
1569 $courseselect = array('id'=>$courseid);
1570 }
1571 $DB->set_field('course', 'modinfo', null, $courseselect);
f36cbf1d 1572 // update cached global COURSE too ;-)
1573 if ($courseid == $COURSE->id) {
238c0dd9 1574 $COURSE->modinfo = null;
f36cbf1d 1575 }
1576 // reset the fast modinfo cache
1577 $reset = 'reset';
1578 get_fast_modinfo($reset);
1579 return;
1580 }
5867bfb5 1581
1582 if ($courseid) {
cb6fec1f 1583 $select = array('id'=>$courseid);
5867bfb5 1584 } else {
cb6fec1f 1585 $select = array();
6cf890e3 1586 @set_time_limit(0); // this could take a while! MDL-10954
5867bfb5 1587 }
1588
cb6fec1f 1589 if ($rs = $DB->get_recordset("course", $select,'','id,fullname')) {
1590 foreach ($rs as $course) {
5867bfb5 1591 $modinfo = serialize(get_array_of_activities($course->id));
cb6fec1f 1592 if (!$DB->set_field("course", "modinfo", $modinfo, array("id"=>$course->id))) {
6ba65fa0 1593 notify("Could not cache module information for course '" . format_string($course->fullname) . "'!");
5867bfb5 1594 }
dd97c328 1595 // update cached global COURSE too ;-)
1596 if ($course->id == $COURSE->id) {
238c0dd9 1597 $COURSE->modinfo = $modinfo;
dd97c328 1598 }
5867bfb5 1599 }
cb6fec1f 1600 $rs->close();
5867bfb5 1601 }
dd97c328 1602 // reset the fast modinfo cache
65a00c97 1603 $reset = 'reset';
1604 get_fast_modinfo($reset);
5867bfb5 1605}
1606
cb6fec1f 1607/**
1608 * Returns an array of the children categories for the given category
1609 * ID by caching all of the categories in a static hash
1610 */
9bb19e58 1611function get_child_categories($parent) {
9bb19e58 1612 static $allcategories = null;
1613
1614 // only fill in this variable the first time
1615 if (null == $allcategories) {
1616 $allcategories = array();
1617
1618 $categories = get_categories();
1619 foreach ($categories as $category) {
1620 if (empty($allcategories[$category->parent])) {
1621 $allcategories[$category->parent] = array();
1622 }
1623 $allcategories[$category->parent][] = $category;
1624 }
1625 }
1626
1627 if (empty($allcategories[$parent])) {
1628 return array();
1629 } else {
1630 return $allcategories[$parent];
1631 }
1632}
1633
cb6fec1f 1634/**
1635 * Given an empty array, this function recursively travels the
1636 * categories, building up a nice list for display. It also makes
1637 * an array that list all the parents for each category.
1638 */
c2cb4545 1639function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
9d866ae0 1640 // initialize the arrays if needed
1641 if (!is_array($list)) {
264867fd 1642 $list = array();
9d866ae0 1643 }
1644 if (!is_array($parents)) {
264867fd 1645 $parents = array();
9d866ae0 1646 }
1647
c2cb4545 1648 if ($category) {
1649 if ($path) {
6ba65fa0 1650 $path = $path.' / '.format_string($category->name);
c2cb4545 1651 } else {
6ba65fa0 1652 $path = format_string($category->name);
c2cb4545 1653 }
1654 $list[$category->id] = $path;
1655 } else {
1656 $category->id = 0;
1657 }
1658
9bb19e58 1659 if ($categories = get_child_categories($category->id)) { // Print all the children recursively
c2cb4545 1660 foreach ($categories as $cat) {
1661 if (!empty($category->id)) {
3bd4de22 1662 if (isset($parents[$category->id])) {
2832badf 1663 $parents[$cat->id] = $parents[$category->id];
1664 }
c2cb4545 1665 $parents[$cat->id][] = $category->id;
1666 }
89adb174 1667 make_categories_list($list, $parents, $cat, $path);
c2cb4545 1668 }
1669 }
1670}
1671
1672
cb6fec1f 1673/**
1674 * Recursive function to print out all the categories in a nice format
1675 * with or without courses included
1676 */
d157bd5b 1677function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $files = true) {
9ff5310a 1678 global $CFG;
e05bcf2f 1679
1680 if (isset($CFG->max_category_depth) && ($depth >= $CFG->max_category_depth)) {
1681 return;
9ff5310a 1682 }
c2cb4545 1683
1684 if (!$displaylist) {
e92fe848 1685 make_categories_list($displaylist, $parentslist);
c2cb4545 1686 }
1687
1688 if ($category) {
8e480396 1689 if ($category->visible or has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM))) {
6f24e48e 1690 print_category_info($category, $depth, $files);
c2cb4545 1691 } else {
1692 return; // Don't bother printing children of invisible categories
1693 }
89adb174 1694
c2cb4545 1695 } else {
c2cb4545 1696 $category->id = "0";
1697 }
1698
9bb19e58 1699 if ($categories = get_child_categories($category->id)) { // Print all the children recursively
c2cb4545 1700 $countcats = count($categories);
1701 $count = 0;
1702 $first = true;
1703 $last = false;
1704 foreach ($categories as $cat) {
1705 $count++;
1706 if ($count == $countcats) {
1707 $last = true;
1708 }
1709 $up = $first ? false : true;
1710 $down = $last ? false : true;
1711 $first = false;
1712
6f24e48e 1713 print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1, $files);
c2cb4545 1714 }
1715 }
c2cb4545 1716}
1717
cb6fec1f 1718/**
1719 * This function will return $options array for choose_from_menu, with whitespace to denote nesting.
1720 */
0705ff84 1721function make_categories_options() {
1722 make_categories_list($cats,$parents);
1723 foreach ($cats as $key => $value) {
1724 if (array_key_exists($key,$parents)) {
1725 if ($indent = count($parents[$key])) {
1726 for ($i = 0; $i < $indent; $i++) {
1727 $cats[$key] = '&nbsp;'.$cats[$key];
1728 }
1729 }
1730 }
1731 }
1732 return $cats;
1733}
c2cb4545 1734
cb6fec1f 1735/**
1736 * Prints the category info in indented fashion
1737 * This function is only used by print_whole_category_list() above
1738 */
6f24e48e 1739function print_category_info($category, $depth, $files = false) {
cb6fec1f 1740 global $CFG, $DB;
b48f834c 1741 static $strallowguests, $strrequireskey, $strsummary;
c2cb4545 1742
b48f834c 1743 if (empty($strsummary)) {
e05bcf2f 1744 $strallowguests = get_string('allowguests');
1745 $strrequireskey = get_string('requireskey');
1746 $strsummary = get_string('summary');
b48f834c 1747 }
ba2e5d73 1748
e05bcf2f 1749 $catlinkcss = $category->visible ? '' : ' class="dimmed" ';
d5f26b07 1750
cb6fec1f 1751 $coursecount = $DB->count_records('course') <= FRONTPAGECOURSELIMIT;
6f24e48e 1752 if ($files and $coursecount) {
fcf9577a 1753 $catimage = '<img src="'.$CFG->pixpath.'/i/course.gif" alt="" />';
b48f834c 1754 } else {
7b0b5c14 1755 $catimage = "&nbsp;";
8ef9cb56 1756 }
b48f834c 1757
fcf9577a 1758 echo "\n\n".'<table class="categorylist">';
d2b6ba70 1759
7ffcbfe1 1760 $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');
6f24e48e 1761 if ($files and $coursecount) {
b48f834c 1762
978abb42 1763 echo '<tr>';
b48f834c 1764
1765 if ($depth) {
1766 $indent = $depth*30;
1767 $rows = count($courses) + 1;
1c919752 1768 echo '<td rowspan="'.$rows.'" valign="top" width="'.$indent.'">';
b48f834c 1769 print_spacer(10, $indent);
e05bcf2f 1770 echo '</td>';
b48f834c 1771 }
89adb174 1772
9deaeaa1 1773 echo '<td valign="top" class="category image">'.$catimage.'</td>';
fcf9577a 1774 echo '<td valign="top" class="category name">';
6ba65fa0 1775 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'. format_string($category->name).'</a>';
e05bcf2f 1776 echo '</td>';
290130b3 1777 echo '<td class="category info">&nbsp;</td>';
e05bcf2f 1778 echo '</tr>';
b48f834c 1779
9ff5310a 1780 if ($courses && !(isset($CFG->max_category_depth)&&($depth>=$CFG->max_category_depth-1))) {
c2cb4545 1781 foreach ($courses as $course) {
e05bcf2f 1782 $linkcss = $course->visible ? '' : ' class="dimmed" ';
fcf9577a 1783 echo '<tr><td valign="top">&nbsp;';
1784 echo '</td><td valign="top" class="course name">';
6ba65fa0 1785 echo '<a '.$linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'. format_string($course->fullname).'</a>';
fcf9577a 1786 echo '</td><td align="right" valign="top" class="course info">';
c2cb4545 1787 if ($course->guest ) {
e05bcf2f 1788 echo '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
fcf9577a 1789 echo '<img alt="'.$strallowguests.'" src="'.$CFG->pixpath.'/i/guest.gif" /></a>';
ebe8ddc1 1790 } else {
fcf9577a 1791 echo '<img alt="" style="width:18px;height:16px;" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1792 }
c2cb4545 1793 if ($course->password) {
e05bcf2f 1794 echo '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
fcf9577a 1795 echo '<img alt="'.$strrequireskey.'" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
ebe8ddc1 1796 } else {
fcf9577a 1797 echo '<img alt="" style="width:18px;height:16px;" src="'.$CFG->pixpath.'/spacer.gif" />';
b48f834c 1798 }
1799 if ($course->summary) {
e05bcf2f 1800 link_to_popup_window ('/course/info.php?id='.$course->id, 'courseinfo',
fcf9577a 1801 '<img alt="'.$strsummary.'" src="'.$CFG->pixpath.'/i/info.gif" />',
b48f834c 1802 400, 500, $strsummary);
ebe8ddc1 1803 } else {
fcf9577a 1804 echo '<img alt="" style="width:18px;height:16px;" src="'.$CFG->pixpath.'/spacer.gif" />';
0c656181 1805 }
e05bcf2f 1806 echo '</td></tr>';
0c656181 1807 }
ba2e5d73 1808 }
d2b6ba70 1809 } else {
b48f834c 1810
e0140f24 1811 echo '<tr>';
1812
b48f834c 1813 if ($depth) {
1814 $indent = $depth*20;
e05bcf2f 1815 echo '<td valign="top" width="'.$indent.'">';
b48f834c 1816 print_spacer(10, $indent);
e05bcf2f 1817 echo '</td>';
d2b6ba70 1818 }
89adb174 1819
fcf9577a 1820 echo '<td valign="top" class="category name">';
6ba65fa0 1821 echo '<a '.$catlinkcss.' href="'.$CFG->wwwroot.'/course/category.php?id='.$category->id.'">'. format_string($category->name).'</a>';
e05bcf2f 1822 echo '</td>';
290130b3 1823 echo '<td valign="top" class="category number">';
7ffcbfe1 1824 if (count($courses)) {
1825 echo count($courses);
e05bcf2f 1826 }
1827 echo '</td></tr>';
c2cb4545 1828 }
e05bcf2f 1829 echo '</table>';
c2cb4545 1830}
1831
c2cb4545 1832
e0b033d5 1833
cb6fec1f 1834/**
1835 * Category is 0 (for all courses) or an object
1836 */
6c54240a 1837function print_courses($category) {
810393c8 1838 global $CFG;
c2cb4545 1839
4dde1463 1840 if (!is_object($category) && $category==0) {
9bb19e58 1841 $categories = get_child_categories(0); // Parent = 0 ie top-level categories only
4dde1463 1842 if (is_array($categories) && count($categories) == 1) {
90c2ca2e 1843 $category = array_shift($categories);
238c0dd9 1844 $courses = get_courses_wmanagers($category->id,
1845 'c.sortorder ASC',
4dde1463 1846 array('password','summary','currency'));
90c2ca2e 1847 } else {
238c0dd9 1848 $courses = get_courses_wmanagers('all',
1849 'c.sortorder ASC',
4dde1463 1850 array('password','summary','currency'));
90c2ca2e 1851 }
1852 unset($categories);
607809b3 1853 } else {
238c0dd9 1854 $courses = get_courses_wmanagers($category->id,
1855 'c.sortorder ASC',
4dde1463 1856 array('password','summary','currency'));
c2cb4545 1857 }
1858
49cd4d79 1859 if ($courses) {
8fee6c60 1860 echo '<ul class="unlist">';
c2cb4545 1861 foreach ($courses as $course) {
4dde1463 1862 if ($course->visible == 1
003bbcc8 1863 || has_capability('moodle/course:viewhiddencourses',$course->context)) {
8fee6c60 1864 echo '<li>';
4dde1463 1865 print_course($course);
8fee6c60 1866 echo "</li>\n";
4dde1463 1867 }
c2cb4545 1868 }
8fee6c60 1869 echo "</ul>\n";
c2cb4545 1870 } else {
f9667a5a 1871 print_heading(get_string("nocoursesyet"));
8e480396 1872 $context = get_context_instance(CONTEXT_SYSTEM);
0468976c 1873 if (has_capability('moodle/course:create', $context)) {
255d1033 1874 $options = array();
1875 $options['category'] = $category->id;
6b7425d2 1876 echo '<div class="addcoursebutton">';
255d1033 1877 print_single_button($CFG->wwwroot.'/course/edit.php', $options, get_string("addnewcourse"));
1878 echo '</div>';
1879 }
c2cb4545 1880 }
c2cb4545 1881}
1882
1883
35d0244a 1884function print_course($course) {
cb6fec1f 1885 global $CFG, $USER, $DB;
c2cb4545 1886
4dde1463 1887 if (isset($course->context)) {
1888 $context = $course->context;
1889 } else {
1890 $context = get_context_instance(CONTEXT_COURSE, $course->id);
1891 }
146bbb8f 1892
88768091 1893 $linkcss = $course->visible ? '' : ' class="dimmed" ';
22288704 1894
7cd266e9 1895 echo '<div class="coursebox clearfix">';
afba7be1 1896 echo '<div class="info">';
7f9c4fb9 1897 echo '<div class="name"><a title="'.get_string('entercourse').'"'.
e5e81e78 1898 $linkcss.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.
238c0dd9 1899 format_string($course->fullname).'</a></div>';
1900
d42c64ba 1901 /// first find all roles that are supposed to be displayed
238c0dd9 1902
6b4d8c4d 1903 if (!empty($CFG->coursemanager)) {
1904 $managerroles = split(',', $CFG->coursemanager);
3bf13d05 1905 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
4dde1463 1906 $namesarray = array();
1907 if (isset($course->managers)) {
1908 if (count($course->managers)) {
1909 $rusers = $course->managers;
1910 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
238c0dd9 1911
b682cee9 1912 /// Rename some of the role names if needed
1913 if (isset($context)) {
cb6fec1f 1914 $aliasnames = $DB->get_records('role_names', array('contextid'=>$context->id), '', 'roleid,contextid,name');
b682cee9 1915 }
1916
9d5a4b23 1917 // keep a note of users displayed to eliminate duplicates
1918 $usersshown = array();
4dde1463 1919 foreach ($rusers as $ra) {
9d5a4b23 1920
1921 // if we've already displayed user don't again
1922 if (in_array($ra->user->id,$usersshown)) {
1923 continue;
1924 }
1925 $usersshown[] = $ra->user->id;
1926
4dde1463 1927 if ($ra->hidden == 0 || $canseehidden) {
238c0dd9 1928 $fullname = fullname($ra->user, $canviewfullnames);
e6924a01 1929 if ($ra->hidden == 1) {
b9837ddf 1930 $status = " <img src=\"{$CFG->pixpath}/t/show.gif\" title=\"".get_string('userhashiddenassignments', 'role')."\" alt=\"".get_string('hiddenassign')."\" class=\"hide-show-image\"/>";
e6924a01 1931 } else {
1932 $status = '';
1780d87b 1933 }
b682cee9 1934
1935 if (isset($aliasnames[$ra->roleid])) {
87486420 1936 $ra->rolename = $aliasnames[$ra->roleid]->name;
b682cee9 1937 }
1938
238c0dd9 1939 $namesarray[] = format_string($ra->rolename)
4dde1463 1940 . ': <a href="'.$CFG->wwwroot.'/user/view.php?id='.$ra->user->id.'&amp;course='.SITEID.'">'
238c0dd9 1941 . $fullname . '</a>' . $status;
4dde1463 1942 }
1943 }
1944 }
1945 } else {
238c0dd9 1946 $rusers = get_role_users($managerroles, $context,
4dde1463 1947 true, '', 'r.sortorder ASC, u.lastname ASC', $canseehidden);
1948 if (is_array($rusers) && count($rusers)) {
1949 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
1950 foreach ($rusers as $teacher) {
238c0dd9 1951 $fullname = fullname($teacher, $canviewfullnames);
1952 $namesarray[] = format_string($teacher->rolename)
4dde1463 1953 . ': <a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id.'&amp;course='.SITEID.'">'
238c0dd9 1954 . $fullname . '</a>';
4dde1463 1955 }
431cad0d 1956 }
c2cb4545 1957 }
431cad0d 1958
d42c64ba 1959 if (!empty($namesarray)) {
88768091 1960 echo "<ul class=\"teachers\">\n<li>";
1961 echo implode('</li><li>', $namesarray);
1962 echo "</li></ul>";
1963 }
c2cb4545 1964 }
238c0dd9 1965
88768091 1966 require_once("$CFG->dirroot/enrol/enrol.class.php");
1967 $enrol = enrolment_factory::factory($course->enrol);
146bbb8f 1968 echo $enrol->get_access_icons($course);
c2cb4545 1969
afba7be1 1970 echo '</div><div class="summary">';
9f39c190 1971 $options = NULL;
1972 $options->noclean = true;
34b5847a 1973 $options->para = false;
9f39c190 1974 echo format_text($course->summary, FORMAT_MOODLE, $options, $course->id);
afba7be1 1975 echo '</div>';
1976 echo '</div>';
c2cb4545 1977}
1978
cb6fec1f 1979/**
1980 * Prints custom user information on the home page.
1981 * Over time this can include all sorts of information
1982 */
c2cb4545 1983function print_my_moodle() {
cb6fec1f 1984 global $USER, $CFG, $DB;
c2cb4545 1985
86a1ba04 1986 if (empty($USER->id)) {
ba6018a9 1987 print_error('nopermissions', '', '', 'See My Moodle');
c2cb4545 1988 }
1989
5b9e50ca 1990 $courses = get_my_courses($USER->id, 'visible DESC,sortorder ASC', array('summary'));
86dd62a7 1991 $rhosts = array();
1992 $rcourses = array();
36e6379e 1993 if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
86dd62a7 1994 $rcourses = get_my_remotecourses($USER->id);
643b67b8 1995 $rhosts = get_my_remotehosts();
86dd62a7 1996 }
1997
1998 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
1999
2000 if (!empty($courses)) {
8fee6c60 2001 echo '<ul class="unlist">';
86dd62a7 2002 foreach ($courses as $course) {
2003 if ($course->id == SITEID) {
2004 continue;
2005 }
8fee6c60 2006 echo '<li>';
86dd62a7 2007 print_course($course, "100%");
8fee6c60 2008 echo "</li>\n";
86dd62a7 2009 }
8fee6c60 2010 echo "</ul>\n";
86dd62a7 2011 }
2012
2013 // MNET
238c0dd9 2014 if (!empty($rcourses)) {
86dd62a7 2015 // at the IDP, we know of all the remote courses
2016 foreach ($rcourses as $course) {
2017 print_remote_course($course, "100%");
2018 }
2019 } elseif (!empty($rhosts)) {
2020 // non-IDP, we know of all the remote servers, but not courses
2021 foreach ($rhosts as $host) {
643b67b8 2022 print_remote_host($host, "100%");
c81696e5 2023 }
c2cb4545 2024 }
86dd62a7 2025 unset($course);
2026 unset($host);
38a10939 2027
cb6fec1f 2028 if ($DB->count_records("course") > (count($courses) + 1) ) { // Some courses not being displayed
7f989948 2029 echo "<table width=\"100%\"><tr><td align=\"center\">";
2030 print_course_search("", false, "short");
2031 echo "</td><td align=\"center\">";
2032 print_single_button("$CFG->wwwroot/course/index.php", NULL, get_string("fulllistofcourses"), "get");
2033 echo "</td></tr></table>\n";
2034 }
86dd62a7 2035
26330001 2036 } else {
cb6fec1f 2037 if ($DB->count_records("course_categories") > 1) {
cb29b020 2038 print_simple_box_start("center", "100%", "#FFFFFF", 5, "categorybox");
26330001 2039 print_whole_category_list();
2040 print_simple_box_end();
2041 } else {
35d0244a 2042 print_courses(0);
26330001 2043 }
607809b3 2044 }
2b8cef80 2045}
2046
11b0c469 2047
a8b56716 2048function print_course_search($value="", $return=false, $format="plain") {
38a10939 2049 global $CFG;
1e0fb105 2050 static $count = 0;
2051
2052 $count++;
2053
2054 $id = 'coursesearch';
2055
2056 if ($count > 1) {
2057 $id .= $count;
2058 }
38a10939 2059
2060 $strsearchcourses= get_string("searchcourses");
2061
1c919752 2062 if ($format == 'plain') {
1e0fb105 2063 $output = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
fcf9577a 2064 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
e42f4d92 2065 $output .= '<label for="coursesearchbox">'.$strsearchcourses.': </label>';
cb6fec1f 2066 $output .= '<input type="text" id="coursesearchbox" size="30" name="search" value="'.s($value).'" />';
e42f4d92 2067 $output .= '<input type="submit" value="'.get_string('go').'" />';
fcf9577a 2068 $output .= '</fieldset></form>';
1c919752 2069 } else if ($format == 'short') {
1e0fb105 2070 $output = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
fcf9577a 2071 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
b1f97418 2072 $output .= '<label for="shortsearchbox">'.$strsearchcourses.': </label>';
cb6fec1f 2073 $output .= '<input type="text" id="shortsearchbox" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
e42f4d92 2074 $output .= '<input type="submit" value="'.get_string('go').'" />';
fcf9577a 2075 $output .= '</fieldset></form>';
1c919752 2076 } else if ($format == 'navbar') {
fcf9577a 2077 $output = '<form id="coursesearchnavbar" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
2078 $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
b1f97418 2079 $output .= '<label for="navsearchbox">'.$strsearchcourses.': </label>';
cb6fec1f 2080 $output .= '<input type="text" id="navsearchbox" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
e42f4d92 2081 $output .= '<input type="submit" value="'.get_string('go').'" />';
fcf9577a 2082 $output .= '</fieldset></form>';
a8b56716 2083 }
2084
2085 if ($return) {
2086 return $output;
2087 }
2088 echo $output;
38a10939 2089}
11b0c469 2090
86dd62a7 2091function print_remote_course($course, $width="100%") {
86dd62a7 2092 global $CFG, $USER;
2093
2094 $linkcss = '';
2095
2096 $url = "{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}";
2097
7cd266e9 2098 echo '<div class="coursebox remotecoursebox clearfix">';
86dd62a7 2099 echo '<div class="info">';
2100 echo '<div class="name"><a title="'.get_string('entercourse').'"'.
2101 $linkcss.' href="'.$url.'">'
6ba65fa0 2102 . format_string($course->fullname) .'</a><br />'
2103 . format_string($course->hostname) . ' : '
238c0dd9 2104 . format_string($course->cat_name) . ' : '
2105 . format_string($course->shortname). '</div>';
86dd62a7 2106 echo '</div><div class="summary">';
2107 $options = NULL;
2108 $options->noclean = true;
2109 $options->para = false;
2110 echo format_text($course->summary, FORMAT_MOODLE, $options);
2111 echo '</div>';
2112 echo '</div>';
86dd62a7 2113}
2114
643b67b8 2115function print_remote_host($host, $width="100%") {
643b67b8 2116 global $CFG, $USER;
2117
2118 $linkcss = '';
2119
7cd266e9 2120 echo '<div class="coursebox clearfix">';
643b67b8 2121 echo '<div class="info">';
2122 echo '<div class="name">';
2123 echo '<img src="'.$CFG->pixpath.'/i/mnethost.gif" class="icon" alt="'.get_string('course').'" />';
2124 echo '<a title="'.s($host['name']).'" href="'.s($host['url']).'">'
2125 . s($host['name']).'</a> - ';
1fd80ad3 2126 echo $host['count'] . ' ' . get_string('courses');
643b67b8 2127 echo '</div>';
2128 echo '</div>';
caa90d56 2129 echo '</div>';
643b67b8 2130}
2131
86dd62a7 2132
11b0c469 2133/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
2134
2135function add_course_module($mod) {
cb6fec1f 2136 global $DB;
11b0c469 2137
e5dfd0f3 2138 $mod->added = time();
53f4ad2c 2139 unset($mod->id);
11b0c469 2140
cb6fec1f 2141 return $DB->insert_record("course_modules", $mod);
11b0c469 2142}
2143
97928ddf 2144/**
2145 * Returns course section - creates new if does not exist yet.
2146 * @param int $relative section number
2147 * @param int $courseid
2148 * @return object $course_section object
2149 */
2150function get_course_section($section, $courseid) {
cb6fec1f 2151 global $DB;
2152
2153 if ($cw = $DB->get_record("course_sections", array("section"=>$section, "course"=>$courseid))) {
97928ddf 2154 return $cw;
2155 }
2156 $cw = new object();
cb6fec1f 2157 $cw->course = $courseid;
2158 $cw->section = $section;
2159 $cw->summary = "";
97928ddf 2160 $cw->sequence = "";
cb6fec1f 2161 $id = $DB->insert_record("course_sections", $cw);
dc5af91a 2162 return $DB->get_record("course_sections", array("id"=>$id));
97928ddf 2163}
ece966f0 2164/**
2165 * Given a full mod object with section and course already defined, adds this module to that section.
2166 *
2167 * @param object $mod
2168 * @param int $beforemod An existing ID which we will insert the new module before
2169 * @return int The course_sections ID where the mod is inserted
2170 */
7977cffd 2171function add_mod_to_section($mod, $beforemod=NULL) {
cb6fec1f 2172 global $DB;
11b0c469 2173
cb6fec1f 2174 if ($section = $DB->get_record("course_sections", array("course"=>$mod->course, "section"=>$mod->section))) {
7977cffd 2175
2176 $section->sequence = trim($section->sequence);
2177
2178 if (empty($section->sequence)) {
11b0c469 2179 $newsequence = "$mod->coursemodule";
7977cffd 2180
2181 } else if ($beforemod) {
2182 $modarray = explode(",", $section->sequence);
2183
2184 if ($key = array_keys ($modarray, $beforemod->id)) {
2185 $insertarray = array($mod->id, $beforemod->id);
2186 array_splice($modarray, $key[0], 1, $insertarray);
2187 $newsequence = implode(",", $modarray);
2188
2189 } else { // Just tack it on the end anyway
2190 $newsequence = "$section->sequence,$mod->coursemodule";
2191 }
2192
2193 } else {
2194 $newsequence = "$section->sequence,$mod->coursemodule";
11b0c469 2195 }
89adb174 2196
cb6fec1f 2197 if ($DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id))) {
e5dfd0f3 2198 return $section->id; // Return course_sections ID that was used.
11b0c469 2199 } else {
e5dfd0f3 2200 return 0;
11b0c469 2201 }
89adb174 2202
11b0c469 2203 } else { // Insert a new record
cb6fec1f 2204 $section->course = $mod->course;
2205 $section->section = $mod->section;
2206 $section->summary = "";
e5dfd0f3 2207 $section->sequence = $mod->coursemodule;
cb6fec1f 2208 return $DB->insert_record("course_sections", $section);
11b0c469 2209 }
2210}
2211
48e535bc 2212function set_coursemodule_groupmode($id, $groupmode) {
cb6fec1f 2213 global $DB;
a5d424df 2214 return $DB->set_field("course_modules", "groupmode", $groupmode, array("id"=>$id));
3d575e6f 2215}
2216
24f41672 2217function set_coursemodule_groupingid($id, $groupingid) {
cb6fec1f 2218 global $DB;
a5d424df 2219 return $DB->set_field("course_modules", "groupingid", $groupingid, array("id"=>$id));
24f41672 2220}
2221
2222function set_coursemodule_groupmembersonly($id, $groupmembersonly) {
cb6fec1f 2223 global $DB;
a5d424df 2224 return $DB->set_field("course_modules", "groupmembersonly", $groupmembersonly, array("id"=>$id));
24f41672 2225}
2226
177d4abf 2227function set_coursemodule_idnumber($id, $idnumber) {
cb6fec1f 2228 global $DB;
238c0dd9 2229 return $DB->set_field("course_modules", "idnumber", $idnumber, array("id"=>$id));
177d4abf 2230}
02f66c42 2231/**
2232* $prevstateoverrides = true will set the visibility of the course module
2233* to what is defined in visibleold. This enables us to remember the current
2234* visibility when making a whole section hidden, so that when we toggle
2235* that section back to visible, we are able to return the visibility of
2236* the course module back to what it was originally.
2237*/
2238function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
cb6fec1f 2239 global $DB;
2240 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
978abb42 2241 return false;
2242 }
cb6fec1f 2243 if (!$modulename = $DB->get_field('modules', 'name', array('id'=>$cm->module))) {
978abb42 2244 return false;
2245 }
cb6fec1f 2246 if ($events = $DB->get_records('event', array('instance'=>$cm->instance, 'modulename'=>$modulename))) {
dcd338ff 2247 foreach($events as $event) {
48e535bc 2248 if ($visible) {
2249 show_event($event);
2250 } else {
2251 hide_event($event);
2252 }
dcd338ff 2253 }
2254 }
02f66c42 2255 if ($prevstateoverrides) {
2256 if ($visible == '0') {
2257 // Remember the current visible state so we can toggle this back.
cb6fec1f 2258 $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id'=>$id));
02f66c42 2259 } else {
2260 // Get the previous saved visible states.
cb6fec1f 2261 return $DB->set_field('course_modules', 'visible', $cm->visibleold, array('id'=>$id));
02f66c42 2262 }
2263 }
cb6fec1f 2264 return $DB->set_field("course_modules", "visible", $visible, array("id"=>$id));
1acfbce5 2265}
2266
cb6fec1f 2267/**
290130b3 2268 * Delete a course module and any associated data at the course level (events)
264867fd 2269 * Until 1.5 this function simply marked a deleted flag ... now it
290130b3 2270 * deletes it completely.
2271 *
2272 */
48e535bc 2273function delete_course_module($id) {
cb6fec1f 2274 global $CFG, $DB;
f615fbab 2275 require_once($CFG->libdir.'/gradelib.php');
2276
cb6fec1f 2277 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
290130b3 2278 return true;
2279 }
cb6fec1f 2280 $modulename = $DB->get_field('modules', 'name', array('id'=>$cm->module));
f615fbab 2281 //delete events from calendar
cb6fec1f 2282 if ($events = $DB->get_records('event', array('instance'=>$cm->instance, 'modulename'=>$modulename))) {
dcd338ff 2283 foreach($events as $event) {
0ea03696 2284 delete_event($event->id);
dcd338ff 2285 }
2286 }
f615fbab 2287 //delete grade items, outcome items and grades attached to modules
2288 if ($grade_items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
2289 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course))) {
2290 foreach ($grade_items as $grade_item) {
2291 $grade_item->delete('moddelete');
2292 }
238c0dd9 2293
f615fbab 2294 }
cb6fec1f 2295 return $DB->delete_records('course_modules', array('id'=>$cm->id));
11b0c469 2296}
2297
2298function delete_mod_from_section($mod, $section) {
cb6fec1f 2299 global $DB;
11b0c469 2300
cb6fec1f 2301 if ($section = $DB->get_record("course_sections", array("id"=>$section)) ) {
11b0c469 2302
e5dfd0f3 2303 $modarray = explode(",", $section->sequence);
11b0c469 2304
2305 if ($key = array_keys ($modarray, $mod)) {
2306 array_splice($modarray, $key[0], 1);
2307 $newsequence = implode(",", $modarray);
cb6fec1f 2308 return $DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));
11b0c469 2309 } else {
2310 return false;
2311 }
89adb174 2312
11b0c469 2313 }
7977cffd 2314 return false;
11b0c469 2315}
2316
12905134 2317function move_section($course, $section, $move) {
2318/// Moves a whole course section up and down within the course
cb6fec1f 2319 global $USER, $DB;
12905134 2320
2321 if (!$move) {
2322 return true;
2323 }
2324
2325 $sectiondest = $section + $move;
2326
2327 if ($sectiondest > $course->numsections or $sectiondest < 1) {
2328 return false;
2329 }
2330
cb6fec1f 2331 if (!$sectionrecord = $DB->get_record("course_sections", array("course"=>$course->id, "section"=>$section))) {
12905134 2332 return false;
2333 }
2334
cb6fec1f 2335 if (!$sectiondestrecord = $DB->get_record("course_sections", array("course"=>$course->id, "section"=>$sectiondest))) {
12905134 2336 return false;
2337 }
2338
cb6fec1f 2339 if (!$DB->set_field("course_sections", "section", $sectiondest, array("id"=>$sectionrecord->id))) {
12905134 2340 return false;
2341 }
cb6fec1f 2342 if (!$DB->set_field("course_sections", "section", $section, array("id"=>$sectiondestrecord->id))) {
12905134 2343 return false;
2344 }
798b70a1 2345 // if the focus is on the section that is being moved, then move the focus along
2346 if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
2347 course_set_display($course->id, $sectiondest);
2348 }
5390cbb7 2349
a987106d 2350 // Check for duplicates and fix order if needed.
5390cbb7 2351 // There is a very rare case that some sections in the same course have the same section id.
cb6fec1f 2352 $sections = $DB->get_records('course_sections', array('course'=>$course->id), 'section ASC');
a987106d 2353 $n = 0;
2354 foreach ($sections as $section) {
2355 if ($section->section != $n) {
cb6fec1f 2356 if (!$DB->set_field('course_sections', 'section', $n, array('id'=>$section->id))) {
5390cbb7 2357 return false;
2358 }
5390cbb7 2359 }
a987106d 2360 $n++;
5390cbb7 2361 }
12905134 2362 return true;
2363}
2364
cb6fec1f 2365/**
2366 * Move the module object $mod to the specified $section
2367 * If $beforemod exists then that is the module
2368 * before which $modid should be inserted
2369 * All parameters are objects
2370 */
7977cffd 2371function moveto_module($mod, $section, $beforemod=NULL) {
cb6fec1f 2372 global $DB;
7977cffd 2373
2374/// Remove original module from original section
7977cffd 2375 if (! delete_mod_from_section($mod->id, $mod->section)) {
2376 notify("Could not delete module from existing section");
2377 }
2378
2379/// Update module itself if necessary
2380
2381 if ($mod->section != $section->id) {
89adb174 2382 $mod->section = $section->id;
cb6fec1f 2383 if (!$DB->update_record("course_modules", $mod)) {
7977cffd 2384 return false;
2385 }
48e535bc 2386 // if moving to a hidden section then hide module
2387 if (!$section->visible) {
2388 set_coursemodule_visible($mod->id, 0);
2389 }
7977cffd 2390 }
2391
2392/// Add the module into the new section
2393
2394 $mod->course = $section->course;
2395 $mod->section = $section->section; // need relative reference
2396 $mod->coursemodule = $mod->id;
2397
2398 if (! add_mod_to_section($mod, $beforemod)) {
2399 return false;
2400 }
2401
2402 return true;
7977cffd 2403}
2404
24e1eae4 2405function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
cb6fec1f 2406 global $CFG, $USER, $DB;
94361e02 2407
3d575e6f 2408 static $str;
37a88449 2409 static $sesskey;
3d575e6f 2410
217a8ee9 2411 $modcontext = get_context_instance(CONTEXT_MODULE, $mod->id);
2412 // no permission to edit
2413 if (!has_capability('moodle/course:manageactivities', $modcontext)) {
e2cd3ed0 2414 return false;
217a8ee9 2415 }
2416
3d575e6f 2417 if (!isset($str)) {
9534a8cb 2418 $str->assign = get_string("assignroles", 'role');
90ebdf65 2419 $str->delete = get_string("delete");
2420 $str->move = get_string("move");
2421 $str->moveup = get_string("moveup");
2422 $str->movedown = get_string("movedown");
2423 $str->moveright = get_string("moveright");
2424 $str->moveleft = get_string("moveleft");
2425 $str->update = get_string("update");
2426 $str->duplicate = get_string("duplicate");
2427 $str->hide = get_string("hide");
2428 $str->show = get_string("show");
3d575e6f 2429 $str->clicktochange = get_string("clicktochange");
32d03b7b 2430 $str->forcedmode = get_string("forcedmode");
3d575e6f 2431 $str->groupsnone = get_string("groupsnone");
2432 $str->groupsseparate = get_string("groupsseparate");
2433 $str->groupsvisible = get_string("groupsvisible");
37a88449 2434 $sesskey = sesskey();
1acfbce5 2435 }
94361e02 2436
24e1eae4 2437 if ($section >= 0) {
75f087b6 2438 $section = '&amp;sr='.$section; // Section return
24e1eae4 2439 } else {
2440 $section = '';
2441 }
2442
94361e02 2443 if ($absolute) {
37a88449 2444 $path = $CFG->wwwroot.'/course';
dc0dc7d5 2445 } else {
37a88449 2446 $path = '.';
dc0dc7d5 2447 }
217a8ee9 2448 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
2449 if ($mod->visible) {
2450 $hideshow = '<a class="editing_hide" title="'.$str->hide.'" href="'.$path.'/mod.php?hide='.$mod->id.
2451 '&amp;sesskey='.$sesskey.$section.'"><img'.
2452 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" '.
2453 ' alt="'.$str->hide.'" /></a>'."\n";
2454 } else {
2455 $hideshow = '<a class="editing_show" title="'.$str->show.'" href="'.$path.'/mod.php?show='.$mod->id.
2456 '&amp;sesskey='.$sesskey.$section.'"><img'.
2457 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" '.
2458 ' alt="'.$str->show.'" /></a>'."\n";
2459 }
530db4cd 2460 } else {
2461 $hideshow = '';
7977cffd 2462 }
530db4cd 2463
3d575e6f 2464 if ($mod->groupmode !== false) {
2465 if ($mod->groupmode == SEPARATEGROUPS) {
32d03b7b 2466 $grouptitle = $str->groupsseparate;
cddbd5d5 2467 $groupclass = 'editing_groupsseparate';
37a88449 2468 $groupimage = $CFG->pixpath.'/t/groups.gif';
2469 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=0&amp;sesskey='.$sesskey;
3d575e6f 2470 } else if ($mod->groupmode == VISIBLEGROUPS) {
32d03b7b 2471 $grouptitle = $str->groupsvisible;
cddbd5d5 2472 $groupclass = 'editing_groupsvisible';
37a88449 2473 $groupimage = $CFG->pixpath.'/t/groupv.gif';
2474 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=1&amp;sesskey='.$sesskey;
32d03b7b 2475 } else {
2476 $grouptitle = $str->groupsnone;
90ebdf65 2477 $groupclass = 'editing_groupsnone';
37a88449 2478 $groupimage = $CFG->pixpath.'/t/groupn.gif';
2479 $grouplink = $path.'/mod.php?id='.$mod->id.'&amp;groupmode=2&amp;sesskey='.$sesskey;
32d03b7b 2480 }
2481 if ($mod->groupmodelink) {
90ebdf65 2482 $groupmode = '<a class="'.$groupclass.'" title="'.$grouptitle.' ('.$str->clicktochange.')" href="'.$grouplink.'">'.
0d905d9f 2483 '<img src="'.$groupimage.'" class="iconsmall" '.
2484 'alt="'.$grouptitle.'" /></a>';
3d575e6f 2485 } else {
37a88449 2486 $groupmode = '<img title="'.$grouptitle.' ('.$str->forcedmode.')" '.
0d905d9f 2487 ' src="'.$groupimage.'" class="iconsmall" '.
2488 'alt="'.$grouptitle.'" />';
3d575e6f 2489 }
2490 } else {
2491 $groupmode = "";
2492 }
e2cd3ed0 2493
217a8ee9 2494 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $mod->course))) {
2495 if ($moveselect) {
2496 $move = '<a class="editing_move" title="'.$str->move.'" href="'.$path.'/mod.php?copy='.$mod->id.
2497 '&amp;sesskey='.$sesskey.$section.'"><img'.
2498 ' src="'.$CFG->pixpath.'/t/move.gif" class="iconsmall" '.
2499 ' alt="'.$str->move.'" /></a>'."\n";
2500 } else {
2501 $move = '<a class="editing_moveup" title="'.$str->moveup.'" href="'.$path.'/mod.php?id='.$mod->id.
2502 '&amp;move=-1&amp;sesskey='.$sesskey.$section.'"><img'.
2503 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" '.
2504 ' alt="'.$str->moveup.'" /></a>'."\n".
2505 '<a class="editing_movedown" title="'.$str->movedown.'" href="'.$path.'/mod.php?id='.$mod->id.
2506 '&amp;move=1&amp;sesskey='.$sesskey.$section.'"><img'.
2507 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" '.
2508 ' alt="'.$str->movedown.'" /></a>'."\n";
2509 }
7b44777e 2510 } else {
238c0dd9 2511 $move = '';
7977cffd 2512 }
2513
932be046 2514 $leftright = '';
217a8ee9 2515 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $mod->course))) {
932be046 2516
8047ab1d 2517 if (right_to_left()) { // Exchange arrows on RTL
932be046 2518 $rightarrow = 'left.gif';
2519 $leftarrow = 'right.gif';
2520 } else {
2521 $rightarrow = 'right.gif';
2522 $leftarrow = 'left.gif';
2523 }
238c0dd9 2524
217a8ee9 2525 if ($indent > 0) {
2526 $leftright .= '<a class="editing_moveleft" title="'.$str->moveleft.'" href="'.$path.'/mod.php?id='.$mod->id.
2527 '&amp;indent=-1&amp;sesskey='.$sesskey.$section.'"><img'.
932be046 2528 ' src="'.$CFG->pixpath.'/t/'.$leftarrow.'" class="iconsmall" '.
217a8ee9 2529 ' alt="'.$str->moveleft.'" /></a>'."\n";
2530 }
2531 if ($indent >= 0) {
2532 $leftright .= '<a class="editing_moveright" title="'.$str->moveright.'" href="'.$path.'/mod.php?id='.$mod->id.
2533 '&amp;indent=1&amp;sesskey='.$sesskey.$section.'"><img'.
932be046 2534 ' src="'.$CFG->pixpath.'/t/'.$rightarrow.'" class="iconsmall" '.
217a8ee9 2535 ' alt="'.$str->moveright.'" /></a>'."\n";
2536 }
37a88449 2537 }
9534a8cb 2538 if (has_capability('moodle/course:managegroups', $modcontext)){
2539 $context = get_context_instance(CONTEXT_MODULE, $mod->id);
2540 $assign = '<a class="editing_assign" title="" href="'.$CFG->wwwroot.'/admin/roles/assign.php?contextid='.
2541 $context->id.'"><img src="'.$CFG->pixpath.'/i/roles.gif" alt="'.$str->assign.'" class="iconsmall"/></a>';
8634e001 2542 } else {
2543 $assign = '';
9534a8cb 2544 }
8634e001 2545
90ebdf65 2546 return '<span class="commands">'."\n".$leftright.$move.
2547 '<a class="editing_update" title="'.$str->update.'" href="'.$path.'/mod.php?update='.$mod->id.
37a88449 2548 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2549 ' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" '.
90ebdf65 2550 ' alt="'.$str->update.'" /></a>'."\n".
2551 '<a class="editing_delete" title="'.$str->delete.'" href="'.$path.'/mod.php?delete='.$mod->id.
37a88449 2552 '&amp;sesskey='.$sesskey.$section.'"><img'.
0d905d9f 2553 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" '.
9534a8cb 2554 ' alt="'.$str->delete.'" /></a>'."\n".$hideshow.$groupmode."\n".$assign.'</span>';
90845098 2555}
2556
b61efafb 2557/**
264867fd 2558 * given a course object with shortname & fullname, this function will
b61efafb 2559 * truncate the the number of chars allowed and add ... if it was too long
2560 */
2561function course_format_name ($course,$max=100) {
264867fd 2562
6ba65fa0 2563 $str = $course->shortname.': '. $course->fullname;
b61efafb 2564 if (strlen($str) <= $max) {
2565 return $str;
2566 }
2567 else {
2568 return substr($str,0,$max-3).'...';
2569 }
2570}
2571
2572/**
2573 * This function will return true if the given course is a child course at all
2574 */
2575function course_in_meta ($course) {
cb6fec1f 2576 global $DB;
2577 return $DB->record_exists("course_meta", array("child_course"=>$course->id));
b61efafb 2578}
2579
48e535bc 2580
2581/**
7cac0c4b 2582 * Print standard form elements on module setup forms in mod/.../mod_form.php
48e535bc 2583 */
263017bb 2584function print_standard_coursemodule_settings($form, $features=null) {
cb6fec1f 2585 global $DB;
2586 if (!$course = $DB->get_record('course', array('id'=>$form->course))) {
ba6018a9 2587 print_error("invalidcourseid");
da2224f8 2588 }
2589 print_groupmode_setting($form, $course);
263017bb 2590 if (!empty($features->groupings)) {
2591 print_grouping_settings($form, $course);
2592 }
da2224f8 2593 print_visible_setting($form, $course);
48e535bc 2594}
2595
2596/**
7cac0c4b 2597 * Print groupmode form element on module setup forms in mod/.../mod_form.php
48e535bc 2598 */
5ebb746b 2599function print_groupmode_setting($form, $course=NULL) {
cb6fec1f 2600 global $DB;
48e535bc 2601
5ebb746b 2602 if (empty($course)) {
cb6fec1f 2603 if (!$course = $DB->get_record('course', array('id'=>$form->course))) {
ba6018a9 2604 print_error("invalidcourseid");
5ebb746b 2605 }
2606 }
48e535bc 2607 if ($form->coursemodule) {
cb6fec1f 2608 if (!$cm = $DB->get_record('course_modules', array('id'=>$form->coursemodule))) {
ba6018a9 2609 print_error("cmunknown");
48e535bc 2610 }
ffc536af 2611 $groupmode = groups_get_activity_groupmode($cm);
48e535bc 2612 } else {
2613 $cm = null;
ffc536af 2614 $groupmode = groups_get_course_groupmode($course);
48e535bc 2615 }
48e535bc 2616 if ($course->groupmode or (!$course->groupmodeforce)) {
2617 echo '<tr valign="top">';
2618 echo '<td align="right"><b>'.get_string('groupmode').':</b></td>';
7bbe08a2 2619 echo '<td align="left">';
ffc536af 2620 $choices = array();
48e535bc 2621 $choices[NOGROUPS] = get_string('groupsnone');
2622 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
2623 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
2624 choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
2625 helpbutton('groupmode', get_string('groupmode'));
2626 echo '</td></tr>';
2627 }
2628}
2629
263017bb 2630/**
7cac0c4b 2631 * Print groupmode form element on module setup forms in mod/.../mod_form.php
263017bb 2632 */
2633function print_grouping_settings($form, $course=NULL) {
f33e1ed4 2634 global $DB;
263017bb 2635
2636 if (empty($course)) {
cb6fec1f 2637 if (! $course = $DB->get_record('course', array('id'=>$form->course))) {
ba6018a9 2638 print_error("invalidcourseid");
263017bb 2639 }
2640 }
2641 if ($form->coursemodule) {
cb6fec1f 2642 if (!$cm = $DB->get_record('course_modules', array('id'=>$form->coursemodule))) {
ba6018a9 2643 print_error("cmunknown");
263017bb 2644 }
2645 } else {
2646 $cm = null;
2647 }
2648
f33e1ed4 2649 $groupings = $DB->get_records_menu('groupings', array('courseid'=>$course->id), 'name', 'id, name');
263017bb 2650 if (!empty($groupings)) {
2651 echo '<tr valign="top">';
2652 echo '<td align="right"><b>'.get_string('grouping', 'group').':</b></td>';
2653 echo '<td align="left">';
238c0dd9 2654
263017bb 2655 $groupingid = isset($cm->groupingid) ? $cm->groupingid : 0;
238c0dd9 2656
263017bb 2657 choose_from_menu($groupings, 'groupingid', $groupingid, get_string('none'), '', 0, false);
2658 echo '</td></tr>';
238c0dd9 2659
263017bb 2660 $checked = empty($cm->groupmembersonly) ? '':'checked="checked"';
2661 echo '<tr valign="top">';
2662 echo '<td align="right"><b>'.get_string('groupmembersonly', 'group').':</b></td>';
2663 echo '<td align="left">';
2664 echo "<input type=\"checkbox\" name=\"groupmembersonly\" value=\"1\" $checked />";
2665 echo '</td></tr>';
2666
2667 }
2668}
2669
48e535bc 2670/**
7cac0c4b 2671 * Print visibility setting form element on module setup forms in mod/.../mod_form.php
48e535bc 2672 */
5ebb746b 2673function print_visible_setting($form, $course=NULL) {
cb6fec1f 2674 global $DB;
1ee55c41 2675 if (empty($course)) {
cb6fec1f 2676 if (!$course = $DB->get_record('course', array('id'=>$form->course))) {
ba6018a9 2677 print_error("invalidcourseid");
1ee55c41 2678 }
2679 }
48e535bc 2680 if ($form->coursemodule) {
cb6fec1f 2681 $visible = $DB->get_field('course_modules', 'visible', array('id'=>$form->coursemodule));
48e535bc 2682 } else {
2683 $visible = true;
2684 }
2685
2686 if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
cb6fec1f 2687 $hiddensection = !$DB->get_field('course_sections', 'visible', array('section'=>$form->section, 'course'=>$form->course));
48e535bc 2688 } else {
cb6fec1f 2689 $hiddensection = !$DB->get_field('course_sections', 'visible', array('id'=>$form->section));
48e535bc 2690 }
2691 if ($hiddensection) {
2692 $visible = false;
2693 }
264867fd 2694
48e535bc 2695 echo '<tr valign="top">';
182311e4 2696 echo '<td align="right"><b>'.get_string('visible', '').':</b></td>';
7bbe08a2 2697 echo '<td align="left">';
dd97c328 2698 $choices = array(1 => get_string('show'), 0 => get_string('hide'));
48e535bc 2699 choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
2700 echo '</td></tr>';
264867fd 2701}
48e535bc 2702
cb6fec1f 2703function update_restricted_mods($course, $mods) {
2704 global $DB;
ddb0a19f 2705
2706/// Delete all the current restricted list
cb6fec1f 2707 $DB->delete_records('course_allowed_modules', array('course'=>$course->id));
ddb0a19f 2708
0705ff84 2709 if (empty($course->restrictmodules)) {
ddb0a19f 2710 return; // We're done
0705ff84 2711 }
ddb0a19f 2712
2713/// Insert the new list of restricted mods
2714 foreach ($mods as $mod) {
2715 if ($mod == 0) {
2716 continue; // this is the 'allow none' option
0705ff84 2717 }
ddb0a19f 2718 $am = new object();
2719 $am->course = $course->id;
2720 $am->module = $mod;
cb6fec1f 2721 $DB->insert_record('course_allowed_modules',$am);
0705ff84 2722 }
2723}
2724
2725/**
2726 * This function will take an int (module id) or a string (module name)
2727 * and return true or false, whether it's allowed in the given course (object)
264867fd 2728 * $mod is not allowed to be an object, as the field for the module id is inconsistent
0705ff84 2729 * depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
2730 */
2731
2732function course_allowed_module($course,$mod) {
cb6fec1f 2733 global $DB;
238c0dd9 2734
0705ff84 2735 if (empty($course->restrictmodules)) {
2736 return true;
2737 }
264867fd 2738
ddb0a19f 2739 // Admins and admin-like people who can edit everything can also add anything.
2740 // This is a bit wierd, really. I debated taking it out but it's enshrined in help for the setting.
8e480396 2741 if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM))) {
0705ff84 2742 return true;
2743 }
ddb0a19f 2744
0705ff84 2745 if (is_numeric($mod)) {
2746 $modid = $mod;
2747 } else if (is_string($mod)) {
cb6fec1f 2748 $modid = $DB->get_field('modules', 'id', array('name'=>$mod));
0705ff84 2749 }
2750 if (empty($modid)) {
2751 return false;
2752 }
238c0dd9 2753
cb6fec1f 2754 return $DB->record_exists('course_allowed_modules', array('course'=>$course->id, 'module'=>$modid));
0705ff84 2755}
2756
e2b347e9 2757/**
2758 * Recursively delete category including all subcategories and courses.
2759 * @param object $ccategory
2760 * @return bool status
2761 */
2762function category_delete_full($category, $showfeedback=true) {
cb6fec1f 2763 global $CFG, $DB;
e2b347e9 2764 require_once($CFG->libdir.'/gradelib.php');
2765 require_once($CFG->libdir.'/questionlib.php');
2766
cb6fec1f 2767 if ($children = $DB->get_records('course_categories', array('parent'=>$category->id), 'sortorder ASC')) {
e2b347e9 2768 foreach ($children as $childcat) {
2769 if (!category_delete_full($childcat, $showfeedback)) {
238c0dd9 2770 notify("Error deleting category $childcat->name");
e2b347e9 2771 return false;
2772 }
2773 }
2774 }
2775
cb6fec1f 2776 if ($courses = $DB->get_records('course', array('category'=>$category->id), 'sortorder ASC')) {
e2b347e9 2777 foreach ($courses as $course) {
2942a5cd 2778 if (!delete_course($course, false)) {
238c0dd9 2779 notify("Error deleting course $course->shortname");
e2b347e9 2780 return false;
2781 }
238c0dd9 2782 notify(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
e2b347e9 2783 }
2784 }
2785
2786 // now delete anything that may depend on course category context
2787 grade_course_category_delete($category->id, 0, $showfeedback);
2788 if (!question_delete_course_category($category, 0, $showfeedback)) {
238c0dd9 2789 notify(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess');
e2b347e9 2790 return false;
2791 }
2792
2793 // finally delete the category and it's context
cb6fec1f 2794 $DB->delete_records('course_categories', array('id'=>$category->id));
e2b347e9 2795 delete_context(CONTEXT_COURSECAT, $category->id);
2796
2942a5cd 2797 events_trigger('course_category_deleted', $category);
e2b347e9 2798
238c0dd9 2799 notify(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess');
e2b347e9 2800
2801 return true;
2802}
2803
2804/**
2805 * Delete category, but move contents to another category.
2806 * @param object $ccategory
2807 * @param int $newparentid category id
2808 * @return bool status
2809 */
2810function category_delete_move($category, $newparentid, $showfeedback=true) {
cb6fec1f 2811 global $CFG, $DB;
e2b347e9 2812 require_once($CFG->libdir.'/gradelib.php');
2813 require_once($CFG->libdir.'/questionlib.php');
2814
cb6fec1f 2815 if (!$newparentcat = $DB->get_record('course_categories', array('id'=>$newparentid))) {
e2b347e9 2816 return false;
2817 }
2818
cb6fec1f 2819 if ($children = $DB->get_records('course_categories', array('parent'=>$category->id), 'sortorder ASC')) {
e2b347e9 2820 foreach ($children as $childcat) {
2821 if (!move_category($childcat, $newparentcat)) {
238c0dd9 2822 notify("Error moving category $childcat->name");
e2b347e9 2823 return false;
2824 }
2825 }
2826 }
2827
cb6fec1f 2828 if ($courses = $DB->get_records('course', array('category'=>$category->id), 'sortorder ASC', 'id')) {
e2b347e9 2829 if (!move_courses(array_keys($courses), $newparentid)) {
238c0dd9 2830 notify("Error moving courses");
e2b347e9 2831 return false;
2832 }
238c0dd9 2833 notify(get_string('coursesmovedout', '', format_string($category->name)), 'notifysuccess');
e2b347e9 2834 }
2835
2836 // now delete anything that may depend on course category context
2837 grade_course_category_delete($category->id, $newparentid, $showfeedback);
2838 if (!question_delete_course_category($category, $newparentcat, $showfeedback)) {
238c0dd9 2839 notify(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess');
e2b347e9 2840 return false;
2841 }
2842
2843 // finally delete the category and it's context
cb6fec1f 2844 $DB->delete_records('course_categories', array('id'=>$category->id));
e2b347e9 2845 delete_context(CONTEXT_COURSECAT, $category->id);
2846
2942a5cd 2847 events_trigger('course_category_deleted', $category);
e2b347e9 2848
238c0dd9 2849 notify(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess');
e2b347e9 2850
2851 return true;
2852}
2853
cb6fec1f 2854/**
2855 * Efficiently moves many courses around while maintaining
2856 * sortorder in order.
2857 *
2858 * @param $courseids is an array of course ids
2859 */
2860function move_courses($courseids, $categoryid) {
2861 global $CFG, $DB;
861efb19 2862
0cbe8111 2863 if (!empty($courseids) and $category = $DB->get_record('course_categories', array('id'=>$categoryid))) {
2864 $courseids = array_reverse($courseids);
2865 $i = 1;
264867fd 2866
0cbe8111 2867 foreach ($courseids as $courseid) {
2868 if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
2869 notify("Error finding course $courseid");
2870 } else {
2871 $course->category = $categoryid;
2872 $course->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - $i++;
19f601d1 2873
0cbe8111 2874 if (!$DB->update_record('course', $course)) {
2875 notify("An error occurred - course not moved!");
861efb19 2876 }
0cbe8111 2877
2878 $context = get_context_instance(CONTEXT_COURSE, $course->id);
2879 $newparent = get_context_instance(CONTEXT_COURSECAT, $course->category);
2880 context_moved($context, $newparent);
861efb19 2881 }
264867fd 2882 }
0cbe8111 2883 fix_course_sortorder();
2884 }
861efb19 2885 return true;
2886}
2887
cb6fec1f 2888/**
2889 * Efficiently moves a category - NOTE that this can have
2890 * a huge impact access-control-wise...
2891 */
19f601d1 2892function move_category ($category, $newparentcat) {
cb6fec1f 2893 global $CFG, $DB;
19f601d1 2894
1f41940f 2895 $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
2896
2897 if (empty($newparentcat->id)) {
cb6fec1f 2898 if (!$DB->set_field('course_categories', 'parent', 0, array('id'=>$category->id))) {
1f41940f 2899 return false;
2900 }
0cbe8111 2901
1f41940f 2902 $newparent = get_context_instance(CONTEXT_SYSTEM);
0cbe8111 2903
1f41940f 2904 } else {
cb6fec1f 2905 if (!$DB->set_field('course_categories', 'parent', $newparentcat->id, array('id'=>$category->id))) {
1f41940f 2906 return false;
2907 }
2908 $newparent = get_context_instance(CONTEXT_COURSECAT, $newparentcat->id);
19f601d1 2909 }
2910
19f601d1 2911 context_moved($context, $newparent);
2912
0cbe8111 2913 // now make it last in new category
2914 $DB->set_field('course_categories', 'sortorder', MAX_COURSES_IN_CATEGORY*MAX_COURSE_CATEGORIES, array('id'=>$category->id));
2915
2916 // and fix the sortorders
19f601d1 2917 fix_course_sortorder();
238c0dd9 2918
19f601d1 2919 return true;
2920}
2921
ae628043 2922/**
2923 * @param string $format Course format ID e.g. 'weeks'
2924 * @return Name that the course format prefers for sections
2925 */
2926function get_section_name($format) {
2927 $sectionname = get_string("name$format","format_$format");
2928 if($sectionname == "[[name$format]]") {
2929 $sectionname = get_string("name$format");
2930 }
a044c05d 2931 return $sectionname;
ae628043 2932}
2933
2585a68d 2934/**
2935 * Can the current user delete this course?
2936 * @param int $courseid
2937 * @return boolean
2938 *
2939 * Exception here to fix MDL-7796.
2940 *
2941 * FIXME
2942 * Course creators who can manage activities in the course
2943 * shoule be allowed to delete the course. We do it this
2944 * way because we need a quick fix to bring the functionality
2945 * in line with what we had pre-roles. We can't give the
2946 * default course creator role moodle/course:delete at
2947 * CONTEXT_SYSTEM level because this will allow them to
2948 * delete any course in the site. So we hard code this here
2949 * for now.
2950 *
2951 * @author vyshane AT gmail.com
2952 */
2953function can_delete_course($courseid) {
2954
2955 $context = get_context_instance(CONTEXT_COURSE, $courseid);
2956
2957 return ( has_capability('moodle/course:delete', $context)
2958 || (has_capability('moodle/legacy:coursecreator', $context)
2959 && has_capability('moodle/course:manageactivities', $context)) );
2960}
2961
2962
c3df0901 2963/**
bfefa87e 2964 * Create a course and either return a $course object or false
2965 *
2966 * @param object $data - all the data needed for an entry in the 'course' table
2967 */
2968function create_course($data) {
c3df0901 2969 global $CFG, $USER, $DB;
bfefa87e 2970
2971 // preprocess allowed mods
2972 $allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
2973 unset($data->allowedmods);
ddb0a19f 2974 if ($CFG->restrictmodulesfor == 'all') {
2975 $data->restrictmodules = 1;
2976 } else {
2977 $data->restrictmodules = 0;
bfefa87e 2978 }
2585a68d 2979
bfefa87e 2980 $data->timecreated = time();
2981
0cbe8111 2982 // place at beginning of any category
2983 $data->sortorder = 0;
bfefa87e 2984
c3df0901 2985 if ($newcourseid = $DB->insert_record('course', $data)) { // Set up new course
bfefa87e 2986
c3df0901 2987 $course = $DB->get_record('course', array('id'=>$newcourseid));
bfefa87e 2988
2989 // Setup the blocks
2990 $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
2991 blocks_repopulate_page($page); // Return value not checked because you can always edit later
2992
ddb0a19f 2993 update_restricted_mods($course, $allowedmods);
bfefa87e 2994
2995 $section = new object();
cb6fec1f 2996 $section->course = $course->id; // Create a default section.
bfefa87e 2997 $section->section = 0;
c3df0901 2998 $section->id = $DB->insert_record('course_sections', $section);
bfefa87e 2999
3000 fix_course_sortorder();
3001
3002 add_to_log(SITEID, 'course', 'new', 'view.php?id='.$course->id, $data->fullname.' (ID '.$course->id.')');
3003
2942a5cd 3004 //trigger events
3005 events_trigger('course_created', $course);
3006
bfefa87e 3007 return $course;
238c0dd9 3008 }
bfefa87e 3009
3010 return false; // error
3011}
3012
3013
c3df0901 3014/**
bfefa87e 3015 * Update a course and return true or false
3016 *
3017 * @param object $data - all the data needed for an entry in the 'course' table
3018 */
3019function update_course($data) {
c3df0901 3020 global $USER, $CFG, $DB;
bfefa87e 3021
ddb0a19f 3022 // Preprocess allowed mods
bfefa87e 3023 $allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
3024 unset($data->allowedmods);
ddb0a19f 3025
3026 // Normal teachers can't change setting
bfefa87e 3027 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
3028 unset($data->restrictmodules);
3029 }
3030
a372aab5 3031 $movecat = false;
c3df0901 3032 $oldcourse = $DB->get_record('course', array('id'=>$data->id)); // should not fail, already tested above
bfefa87e 3033 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $oldcourse->category))
3034 or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $data->category))) {
3035 // can not move to new category, keep the old one
3036 unset($data->category);
c3df0901 3037
a372aab5 3038 } elseif ($oldcourse->category != $data->category) {
3039 $movecat = true;
bfefa87e 3040 }
3041
3042 // Update with the new data
c3df0901 3043 if ($DB->update_record('course', $data)) {
bfefa87e 3044
c3df0901 3045 $course = $DB->get_record('course', array('id'=>$data->id));
bfefa87e 3046
0be70104 3047 add_to_log($course->id, "course", "update", "edit.php?id=$course->id", $course->id);
bfefa87e 3048
ddb0a19f 3049 // "Admins" can change allowed mods for a course
bfefa87e 3050 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
3051 update_restricted_mods($course, $allowedmods);
3052 }
3053
a372aab5 3054 if ($movecat) {
3055 $context = get_context_instance(CONTEXT_COURSE, $course->id);
3056 $newparent = get_context_instance(CONTEXT_COURSECAT, $course->category);
3057 context_moved($context, $newparent);
3058 }
3059
bfefa87e 3060 fix_course_sortorder();
3061
3062 // Test for and remove blocks which aren't appropriate anymore
3063 $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
3064 blocks_remove_inappropriate($page);
3065
3066 // put custom role names into db
3067 $context = get_context_instance(CONTEXT_COURSE, $course->id);
238c0dd9 3068
bfefa87e 3069 foreach ($data as $dname => $dvalue) {
238c0dd9 3070
bfefa87e 3071 // is this the right param?
3072 $dvalue = clean_param($dvalue, PARAM_NOTAGS);
3073
3074 if (!strstr($dname, 'role_')) {
3075 continue;
238c0dd9 3076 }
3077
bfefa87e 3078 $dt = explode('_', $dname);
3079 $roleid = $dt[1];
3080 // make up our mind whether we want to delete, update or insert
238c0dd9 3081
bfefa87e 3082 if (empty($dvalue)) {
238c0dd9 3083
c3df0901 3084 $DB->delete_records('role_names', array('contextid'=>$context->id, 'roleid'=>$roleid));
238c0dd9 3085
c3df0901 3086 } else if ($t = $DB->get_record('role_names', array('contextid'=>$context->id, 'roleid'=>$roleid))) {
238c0dd9 3087
87486420 3088 $t->name = $dvalue;
238c0dd9 3089 $DB->update_record('role_names', $t);
3090
bfefa87e 3091 } else {
238c0dd9 3092
bfefa87e 3093 $t->contextid = $context->id;
3094 $t->roleid = $roleid;
87486420 3095 $t->name = $dvalue;
238c0dd9 3096 $DB->insert_record('role_names', $t);
bfefa87e 3097 }
238c0dd9 3098
bfefa87e 3099 }
2942a5cd 3100 //trigger events
3101 events_trigger('course_updated', $course);
bfefa87e 3102
3103 return true;
3104
3105 }
3106
3107 return false;
3108}
2585a68d 3109
7f9c4fb9 3110?>