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