Merge branch 'MDL-48199_M27' of git://github.com/lazydaisy/moodle into MOODLE_27_STABLE
[moodle.git] / course / lib.php
blob3dc5219f98f7473cb7c5641a4a8f81ad20e7cd15
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Library of useful functions
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core_course
25 defined('MOODLE_INTERNAL') || die;
27 require_once($CFG->libdir.'/completionlib.php');
28 require_once($CFG->libdir.'/filelib.php');
29 require_once($CFG->dirroot.'/course/format/lib.php');
31 define('COURSE_MAX_LOGS_PER_PAGE', 1000); // Records.
32 define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds.
34 /**
35 * Number of courses to display when summaries are included.
36 * @var int
37 * @deprecated since 2.4, use $CFG->courseswithsummarieslimit instead.
39 define('COURSE_MAX_SUMMARIES_PER_PAGE', 10);
41 // Max courses in log dropdown before switching to optional.
42 define('COURSE_MAX_COURSES_PER_DROPDOWN', 1000);
43 // Max users in log dropdown before switching to optional.
44 define('COURSE_MAX_USERS_PER_DROPDOWN', 1000);
45 define('FRONTPAGENEWS', '0');
46 define('FRONTPAGECATEGORYNAMES', '2');
47 define('FRONTPAGECATEGORYCOMBO', '4');
48 define('FRONTPAGEENROLLEDCOURSELIST', '5');
49 define('FRONTPAGEALLCOURSELIST', '6');
50 define('FRONTPAGECOURSESEARCH', '7');
51 // Important! Replaced with $CFG->frontpagecourselimit - maximum number of courses displayed on the frontpage.
52 define('EXCELROWS', 65535);
53 define('FIRSTUSEDEXCELROW', 3);
55 define('MOD_CLASS_ACTIVITY', 0);
56 define('MOD_CLASS_RESOURCE', 1);
58 function make_log_url($module, $url) {
59 switch ($module) {
60 case 'course':
61 if (strpos($url, 'report/') === 0) {
62 // there is only one report type, course reports are deprecated
63 $url = "/$url";
64 break;
66 case 'file':
67 case 'login':
68 case 'lib':
69 case 'admin':
70 case 'category':
71 case 'mnet course':
72 if (strpos($url, '../') === 0) {
73 $url = ltrim($url, '.');
74 } else {
75 $url = "/course/$url";
77 break;
78 case 'calendar':
79 $url = "/calendar/$url";
80 break;
81 case 'user':
82 case 'blog':
83 $url = "/$module/$url";
84 break;
85 case 'upload':
86 $url = $url;
87 break;
88 case 'coursetags':
89 $url = '/'.$url;
90 break;
91 case 'library':
92 case '':
93 $url = '/';
94 break;
95 case 'message':
96 $url = "/message/$url";
97 break;
98 case 'notes':
99 $url = "/notes/$url";
100 break;
101 case 'tag':
102 $url = "/tag/$url";
103 break;
104 case 'role':
105 $url = '/'.$url;
106 break;
107 case 'grade':
108 $url = "/grade/$url";
109 break;
110 default:
111 $url = "/mod/$module/$url";
112 break;
115 //now let's sanitise urls - there might be some ugly nasties:-(
116 $parts = explode('?', $url);
117 $script = array_shift($parts);
118 if (strpos($script, 'http') === 0) {
119 $script = clean_param($script, PARAM_URL);
120 } else {
121 $script = clean_param($script, PARAM_PATH);
124 $query = '';
125 if ($parts) {
126 $query = implode('', $parts);
127 $query = str_replace('&amp;', '&', $query); // both & and &amp; are stored in db :-|
128 $parts = explode('&', $query);
129 $eq = urlencode('=');
130 foreach ($parts as $key=>$part) {
131 $part = urlencode(urldecode($part));
132 $part = str_replace($eq, '=', $part);
133 $parts[$key] = $part;
135 $query = '?'.implode('&amp;', $parts);
138 return $script.$query;
142 function build_mnet_logs_array($hostid, $course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
143 $modname="", $modid=0, $modaction="", $groupid=0) {
144 global $CFG, $DB;
146 // It is assumed that $date is the GMT time of midnight for that day,
147 // and so the next 86400 seconds worth of logs are printed.
149 /// Setup for group handling.
151 // TODO: I don't understand group/context/etc. enough to be able to do
152 // something interesting with it here
153 // What is the context of a remote course?
155 /// If the group mode is separate, and this user does not have editing privileges,
156 /// then only the user's group can be viewed.
157 //if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', context_course::instance($course->id))) {
158 // $groupid = get_current_group($course->id);
160 /// If this course doesn't have groups, no groupid can be specified.
161 //else if (!$course->groupmode) {
162 // $groupid = 0;
165 $groupid = 0;
167 $joins = array();
168 $where = '';
170 $qry = "SELECT l.*, u.firstname, u.lastname, u.picture
171 FROM {mnet_log} l
172 LEFT JOIN {user} u ON l.userid = u.id
173 WHERE ";
174 $params = array();
176 $where .= "l.hostid = :hostid";
177 $params['hostid'] = $hostid;
179 // TODO: Is 1 really a magic number referring to the sitename?
180 if ($course != SITEID || $modid != 0) {
181 $where .= " AND l.course=:courseid";
182 $params['courseid'] = $course;
185 if ($modname) {
186 $where .= " AND l.module = :modname";
187 $params['modname'] = $modname;
190 if ('site_errors' === $modid) {
191 $where .= " AND ( l.action='error' OR l.action='infected' )";
192 } else if ($modid) {
193 //TODO: This assumes that modids are the same across sites... probably
194 //not true
195 $where .= " AND l.cmid = :modid";
196 $params['modid'] = $modid;
199 if ($modaction) {
200 $firstletter = substr($modaction, 0, 1);
201 if ($firstletter == '-') {
202 $where .= " AND ".$DB->sql_like('l.action', ':modaction', false, true, true);
203 $params['modaction'] = '%'.substr($modaction, 1).'%';
204 } else {
205 $where .= " AND ".$DB->sql_like('l.action', ':modaction', false);
206 $params['modaction'] = '%'.$modaction.'%';
210 if ($user) {
211 $where .= " AND l.userid = :user";
212 $params['user'] = $user;
215 if ($date) {
216 $enddate = $date + 86400;
217 $where .= " AND l.time > :date AND l.time < :enddate";
218 $params['date'] = $date;
219 $params['enddate'] = $enddate;
222 $result = array();
223 $result['totalcount'] = $DB->count_records_sql("SELECT COUNT('x') FROM {mnet_log} l WHERE $where", $params);
224 if(!empty($result['totalcount'])) {
225 $where .= " ORDER BY $order";
226 $result['logs'] = $DB->get_records_sql("$qry $where", $params, $limitfrom, $limitnum);
227 } else {
228 $result['logs'] = array();
230 return $result;
233 function build_logs_array($course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
234 $modname="", $modid=0, $modaction="", $groupid=0) {
235 global $DB, $SESSION, $USER;
236 // It is assumed that $date is the GMT time of midnight for that day,
237 // and so the next 86400 seconds worth of logs are printed.
239 /// Setup for group handling.
241 /// If the group mode is separate, and this user does not have editing privileges,
242 /// then only the user's group can be viewed.
243 if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', context_course::instance($course->id))) {
244 if (isset($SESSION->currentgroup[$course->id])) {
245 $groupid = $SESSION->currentgroup[$course->id];
246 } else {
247 $groupid = groups_get_all_groups($course->id, $USER->id);
248 if (is_array($groupid)) {
249 $groupid = array_shift(array_keys($groupid));
250 $SESSION->currentgroup[$course->id] = $groupid;
251 } else {
252 $groupid = 0;
256 /// If this course doesn't have groups, no groupid can be specified.
257 else if (!$course->groupmode) {
258 $groupid = 0;
261 $joins = array();
262 $params = array();
264 if ($course->id != SITEID || $modid != 0) {
265 $joins[] = "l.course = :courseid";
266 $params['courseid'] = $course->id;
269 if ($modname) {
270 $joins[] = "l.module = :modname";
271 $params['modname'] = $modname;
274 if ('site_errors' === $modid) {
275 $joins[] = "( l.action='error' OR l.action='infected' )";
276 } else if ($modid) {
277 $joins[] = "l.cmid = :modid";
278 $params['modid'] = $modid;
281 if ($modaction) {
282 $firstletter = substr($modaction, 0, 1);
283 if ($firstletter == '-') {
284 $joins[] = $DB->sql_like('l.action', ':modaction', false, true, true);
285 $params['modaction'] = '%'.substr($modaction, 1).'%';
286 } else {
287 $joins[] = $DB->sql_like('l.action', ':modaction', false);
288 $params['modaction'] = '%'.$modaction.'%';
293 /// Getting all members of a group.
294 if ($groupid and !$user) {
295 if ($gusers = groups_get_members($groupid)) {
296 $gusers = array_keys($gusers);
297 $joins[] = 'l.userid IN (' . implode(',', $gusers) . ')';
298 } else {
299 $joins[] = 'l.userid = 0'; // No users in groups, so we want something that will always be false.
302 else if ($user) {
303 $joins[] = "l.userid = :userid";
304 $params['userid'] = $user;
307 if ($date) {
308 $enddate = $date + 86400;
309 $joins[] = "l.time > :date AND l.time < :enddate";
310 $params['date'] = $date;
311 $params['enddate'] = $enddate;
314 $selector = implode(' AND ', $joins);
316 $totalcount = 0; // Initialise
317 $result = array();
318 $result['logs'] = get_logs($selector, $params, $order, $limitfrom, $limitnum, $totalcount);
319 $result['totalcount'] = $totalcount;
320 return $result;
324 function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
325 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
327 global $CFG, $DB, $OUTPUT;
329 if (!$logs = build_logs_array($course, $user, $date, $order, $page*$perpage, $perpage,
330 $modname, $modid, $modaction, $groupid)) {
331 echo $OUTPUT->notification("No logs found!");
332 echo $OUTPUT->footer();
333 exit;
336 $courses = array();
338 if ($course->id == SITEID) {
339 $courses[0] = '';
340 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
341 foreach ($ccc as $cc) {
342 $courses[$cc->id] = $cc->shortname;
345 } else {
346 $courses[$course->id] = $course->shortname;
349 $totalcount = $logs['totalcount'];
350 $count=0;
351 $ldcache = array();
352 $tt = getdate(time());
353 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
355 $strftimedatetime = get_string("strftimedatetime");
357 echo "<div class=\"info\">\n";
358 print_string("displayingrecords", "", $totalcount);
359 echo "</div>\n";
361 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage");
363 $table = new html_table();
364 $table->classes = array('logtable','generaltable');
365 $table->align = array('right', 'left', 'left');
366 $table->head = array(
367 get_string('time'),
368 get_string('ip_address'),
369 get_string('fullnameuser'),
370 get_string('action'),
371 get_string('info')
373 $table->data = array();
375 if ($course->id == SITEID) {
376 array_unshift($table->align, 'left');
377 array_unshift($table->head, get_string('course'));
380 // Make sure that the logs array is an array, even it is empty, to avoid warnings from the foreach.
381 if (empty($logs['logs'])) {
382 $logs['logs'] = array();
385 foreach ($logs['logs'] as $log) {
387 if (isset($ldcache[$log->module][$log->action])) {
388 $ld = $ldcache[$log->module][$log->action];
389 } else {
390 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
391 $ldcache[$log->module][$log->action] = $ld;
393 if ($ld && is_numeric($log->info)) {
394 // ugly hack to make sure fullname is shown correctly
395 if ($ld->mtable == 'user' && $ld->field == $DB->sql_concat('firstname', "' '" , 'lastname')) {
396 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
397 } else {
398 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
402 //Filter log->info
403 $log->info = format_string($log->info);
405 // If $log->url has been trimmed short by the db size restriction
406 // code in add_to_log, keep a note so we don't add a link to a broken url
407 $brokenurl=(core_text::strlen($log->url)==100 && core_text::substr($log->url,97)=='...');
409 $row = array();
410 if ($course->id == SITEID) {
411 if (empty($log->course)) {
412 $row[] = get_string('site');
413 } else {
414 $row[] = "<a href=\"{$CFG->wwwroot}/course/view.php?id={$log->course}\">". format_string($courses[$log->course])."</a>";
418 $row[] = userdate($log->time, '%a').' '.userdate($log->time, $strftimedatetime);
420 $link = new moodle_url("/iplookup/index.php?ip=$log->ip&user=$log->userid");
421 $row[] = $OUTPUT->action_link($link, $log->ip, new popup_action('click', $link, 'iplookup', array('height' => 440, 'width' => 700)));
423 $row[] = html_writer::link(new moodle_url("/user/view.php?id={$log->userid}&course={$log->course}"), fullname($log, has_capability('moodle/site:viewfullnames', context_course::instance($course->id))));
425 $displayaction="$log->module $log->action";
426 if ($brokenurl) {
427 $row[] = $displayaction;
428 } else {
429 $link = make_log_url($log->module,$log->url);
430 $row[] = $OUTPUT->action_link($link, $displayaction, new popup_action('click', $link, 'fromloglive'), array('height' => 440, 'width' => 700));
432 $row[] = $log->info;
433 $table->data[] = $row;
436 echo html_writer::table($table);
437 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage");
441 function print_mnet_log($hostid, $course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
442 $url="", $modname="", $modid=0, $modaction="", $groupid=0) {
444 global $CFG, $DB, $OUTPUT;
446 if (!$logs = build_mnet_logs_array($hostid, $course, $user, $date, $order, $page*$perpage, $perpage,
447 $modname, $modid, $modaction, $groupid)) {
448 echo $OUTPUT->notification("No logs found!");
449 echo $OUTPUT->footer();
450 exit;
453 if ($course->id == SITEID) {
454 $courses[0] = '';
455 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname,c.visible')) {
456 foreach ($ccc as $cc) {
457 $courses[$cc->id] = $cc->shortname;
462 $totalcount = $logs['totalcount'];
463 $count=0;
464 $ldcache = array();
465 $tt = getdate(time());
466 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
468 $strftimedatetime = get_string("strftimedatetime");
470 echo "<div class=\"info\">\n";
471 print_string("displayingrecords", "", $totalcount);
472 echo "</div>\n";
474 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage");
476 echo "<table class=\"logtable\" cellpadding=\"3\" cellspacing=\"0\">\n";
477 echo "<tr>";
478 if ($course->id == SITEID) {
479 echo "<th class=\"c0 header\">".get_string('course')."</th>\n";
481 echo "<th class=\"c1 header\">".get_string('time')."</th>\n";
482 echo "<th class=\"c2 header\">".get_string('ip_address')."</th>\n";
483 echo "<th class=\"c3 header\">".get_string('fullnameuser')."</th>\n";
484 echo "<th class=\"c4 header\">".get_string('action')."</th>\n";
485 echo "<th class=\"c5 header\">".get_string('info')."</th>\n";
486 echo "</tr>\n";
488 if (empty($logs['logs'])) {
489 echo "</table>\n";
490 return;
493 $row = 1;
494 foreach ($logs['logs'] as $log) {
496 $log->info = $log->coursename;
497 $row = ($row + 1) % 2;
499 if (isset($ldcache[$log->module][$log->action])) {
500 $ld = $ldcache[$log->module][$log->action];
501 } else {
502 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
503 $ldcache[$log->module][$log->action] = $ld;
505 if (0 && $ld && !empty($log->info)) {
506 // ugly hack to make sure fullname is shown correctly
507 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
508 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
509 } else {
510 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
514 //Filter log->info
515 $log->info = format_string($log->info);
517 echo '<tr class="r'.$row.'">';
518 if ($course->id == SITEID) {
519 $courseshortname = format_string($courses[$log->course], true, array('context' => context_course::instance(SITEID)));
520 echo "<td class=\"r$row c0\" >\n";
521 echo " <a href=\"{$CFG->wwwroot}/course/view.php?id={$log->course}\">".$courseshortname."</a>\n";
522 echo "</td>\n";
524 echo "<td class=\"r$row c1\" align=\"right\">".userdate($log->time, '%a').
525 ' '.userdate($log->time, $strftimedatetime)."</td>\n";
526 echo "<td class=\"r$row c2\" >\n";
527 $link = new moodle_url("/iplookup/index.php?ip=$log->ip&user=$log->userid");
528 echo $OUTPUT->action_link($link, $log->ip, new popup_action('click', $link, 'iplookup', array('height' => 400, 'width' => 700)));
529 echo "</td>\n";
530 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
531 echo "<td class=\"r$row c3\" >\n";
532 echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}\">$fullname</a>\n";
533 echo "</td>\n";
534 echo "<td class=\"r$row c4\">\n";
535 echo $log->action .': '.$log->module;
536 echo "</td>\n";
537 echo "<td class=\"r$row c5\">{$log->info}</td>\n";
538 echo "</tr>\n";
540 echo "</table>\n";
542 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage");
546 function print_log_csv($course, $user, $date, $order='l.time DESC', $modname,
547 $modid, $modaction, $groupid) {
548 global $DB, $CFG;
550 require_once($CFG->libdir . '/csvlib.class.php');
552 $csvexporter = new csv_export_writer('tab');
554 $header = array();
555 $header[] = get_string('course');
556 $header[] = get_string('time');
557 $header[] = get_string('ip_address');
558 $header[] = get_string('fullnameuser');
559 $header[] = get_string('action');
560 $header[] = get_string('info');
562 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
563 $modname, $modid, $modaction, $groupid)) {
564 return false;
567 $courses = array();
569 if ($course->id == SITEID) {
570 $courses[0] = '';
571 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
572 foreach ($ccc as $cc) {
573 $courses[$cc->id] = $cc->shortname;
576 } else {
577 $courses[$course->id] = $course->shortname;
580 $count=0;
581 $ldcache = array();
582 $tt = getdate(time());
583 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
585 $strftimedatetime = get_string("strftimedatetime");
587 $csvexporter->set_filename('logs', '.txt');
588 $title = array(get_string('savedat').userdate(time(), $strftimedatetime));
589 $csvexporter->add_data($title);
590 $csvexporter->add_data($header);
592 if (empty($logs['logs'])) {
593 return true;
596 foreach ($logs['logs'] as $log) {
597 if (isset($ldcache[$log->module][$log->action])) {
598 $ld = $ldcache[$log->module][$log->action];
599 } else {
600 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
601 $ldcache[$log->module][$log->action] = $ld;
603 if ($ld && is_numeric($log->info)) {
604 // ugly hack to make sure fullname is shown correctly
605 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
606 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
607 } else {
608 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
612 //Filter log->info
613 $log->info = format_string($log->info);
614 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
616 $coursecontext = context_course::instance($course->id);
617 $firstField = format_string($courses[$log->course], true, array('context' => $coursecontext));
618 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', $coursecontext));
619 $actionurl = $CFG->wwwroot. make_log_url($log->module,$log->url);
620 $row = array($firstField, userdate($log->time, $strftimedatetime), $log->ip, $fullname, $log->module.' '.$log->action.' ('.$actionurl.')', $log->info);
621 $csvexporter->add_data($row);
623 $csvexporter->download_file();
624 return true;
628 function print_log_xls($course, $user, $date, $order='l.time DESC', $modname,
629 $modid, $modaction, $groupid) {
631 global $CFG, $DB;
633 require_once("$CFG->libdir/excellib.class.php");
635 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
636 $modname, $modid, $modaction, $groupid)) {
637 return false;
640 $courses = array();
642 if ($course->id == SITEID) {
643 $courses[0] = '';
644 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
645 foreach ($ccc as $cc) {
646 $courses[$cc->id] = $cc->shortname;
649 } else {
650 $courses[$course->id] = $course->shortname;
653 $count=0;
654 $ldcache = array();
655 $tt = getdate(time());
656 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
658 $strftimedatetime = get_string("strftimedatetime");
660 $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
661 $filename = 'logs_'.userdate(time(),get_string('backupnameformat', 'langconfig'),99,false);
662 $filename .= '.xls';
664 $workbook = new MoodleExcelWorkbook('-');
665 $workbook->send($filename);
667 $worksheet = array();
668 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
669 get_string('fullnameuser'), get_string('action'), get_string('info'));
671 // Creating worksheets
672 for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
673 $sheettitle = get_string('logs').' '.$wsnumber.'-'.$nroPages;
674 $worksheet[$wsnumber] = $workbook->add_worksheet($sheettitle);
675 $worksheet[$wsnumber]->set_column(1, 1, 30);
676 $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
677 userdate(time(), $strftimedatetime));
678 $col = 0;
679 foreach ($headers as $item) {
680 $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
681 $col++;
685 if (empty($logs['logs'])) {
686 $workbook->close();
687 return true;
690 $formatDate =& $workbook->add_format();
691 $formatDate->set_num_format(get_string('log_excel_date_format'));
693 $row = FIRSTUSEDEXCELROW;
694 $wsnumber = 1;
695 $myxls =& $worksheet[$wsnumber];
696 foreach ($logs['logs'] as $log) {
697 if (isset($ldcache[$log->module][$log->action])) {
698 $ld = $ldcache[$log->module][$log->action];
699 } else {
700 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
701 $ldcache[$log->module][$log->action] = $ld;
703 if ($ld && is_numeric($log->info)) {
704 // ugly hack to make sure fullname is shown correctly
705 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
706 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
707 } else {
708 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
712 // Filter log->info
713 $log->info = format_string($log->info);
714 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
716 if ($nroPages>1) {
717 if ($row > EXCELROWS) {
718 $wsnumber++;
719 $myxls =& $worksheet[$wsnumber];
720 $row = FIRSTUSEDEXCELROW;
724 $coursecontext = context_course::instance($course->id);
726 $myxls->write($row, 0, format_string($courses[$log->course], true, array('context' => $coursecontext)), '');
727 $myxls->write_date($row, 1, $log->time, $formatDate); // write_date() does conversion/timezone support. MDL-14934
728 $myxls->write($row, 2, $log->ip, '');
729 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', $coursecontext));
730 $myxls->write($row, 3, $fullname, '');
731 $actionurl = $CFG->wwwroot. make_log_url($log->module,$log->url);
732 $myxls->write($row, 4, $log->module.' '.$log->action.' ('.$actionurl.')', '');
733 $myxls->write($row, 5, $log->info, '');
735 $row++;
738 $workbook->close();
739 return true;
742 function print_log_ods($course, $user, $date, $order='l.time DESC', $modname,
743 $modid, $modaction, $groupid) {
745 global $CFG, $DB;
747 require_once("$CFG->libdir/odslib.class.php");
749 if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
750 $modname, $modid, $modaction, $groupid)) {
751 return false;
754 $courses = array();
756 if ($course->id == SITEID) {
757 $courses[0] = '';
758 if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
759 foreach ($ccc as $cc) {
760 $courses[$cc->id] = $cc->shortname;
763 } else {
764 $courses[$course->id] = $course->shortname;
767 $count=0;
768 $ldcache = array();
769 $tt = getdate(time());
770 $today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
772 $strftimedatetime = get_string("strftimedatetime");
774 $nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
775 $filename = 'logs_'.userdate(time(),get_string('backupnameformat', 'langconfig'),99,false);
776 $filename .= '.ods';
778 $workbook = new MoodleODSWorkbook('-');
779 $workbook->send($filename);
781 $worksheet = array();
782 $headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
783 get_string('fullnameuser'), get_string('action'), get_string('info'));
785 // Creating worksheets
786 for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
787 $sheettitle = get_string('logs').' '.$wsnumber.'-'.$nroPages;
788 $worksheet[$wsnumber] = $workbook->add_worksheet($sheettitle);
789 $worksheet[$wsnumber]->set_column(1, 1, 30);
790 $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
791 userdate(time(), $strftimedatetime));
792 $col = 0;
793 foreach ($headers as $item) {
794 $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
795 $col++;
799 if (empty($logs['logs'])) {
800 $workbook->close();
801 return true;
804 $formatDate =& $workbook->add_format();
805 $formatDate->set_num_format(get_string('log_excel_date_format'));
807 $row = FIRSTUSEDEXCELROW;
808 $wsnumber = 1;
809 $myxls =& $worksheet[$wsnumber];
810 foreach ($logs['logs'] as $log) {
811 if (isset($ldcache[$log->module][$log->action])) {
812 $ld = $ldcache[$log->module][$log->action];
813 } else {
814 $ld = $DB->get_record('log_display', array('module'=>$log->module, 'action'=>$log->action));
815 $ldcache[$log->module][$log->action] = $ld;
817 if ($ld && is_numeric($log->info)) {
818 // ugly hack to make sure fullname is shown correctly
819 if (($ld->mtable == 'user') and ($ld->field == $DB->sql_concat('firstname', "' '" , 'lastname'))) {
820 $log->info = fullname($DB->get_record($ld->mtable, array('id'=>$log->info)), true);
821 } else {
822 $log->info = $DB->get_field($ld->mtable, $ld->field, array('id'=>$log->info));
826 // Filter log->info
827 $log->info = format_string($log->info);
828 $log->info = strip_tags(urldecode($log->info)); // Some XSS protection
830 if ($nroPages>1) {
831 if ($row > EXCELROWS) {
832 $wsnumber++;
833 $myxls =& $worksheet[$wsnumber];
834 $row = FIRSTUSEDEXCELROW;
838 $coursecontext = context_course::instance($course->id);
840 $myxls->write_string($row, 0, format_string($courses[$log->course], true, array('context' => $coursecontext)));
841 $myxls->write_date($row, 1, $log->time);
842 $myxls->write_string($row, 2, $log->ip);
843 $fullname = fullname($log, has_capability('moodle/site:viewfullnames', $coursecontext));
844 $myxls->write_string($row, 3, $fullname);
845 $actionurl = $CFG->wwwroot. make_log_url($log->module,$log->url);
846 $myxls->write_string($row, 4, $log->module.' '.$log->action.' ('.$actionurl.')');
847 $myxls->write_string($row, 5, $log->info);
849 $row++;
852 $workbook->close();
853 return true;
857 * Checks the integrity of the course data.
859 * In summary - compares course_sections.sequence and course_modules.section.
861 * More detailed, checks that:
862 * - course_sections.sequence contains each module id not more than once in the course
863 * - for each moduleid from course_sections.sequence the field course_modules.section
864 * refers to the same section id (this means course_sections.sequence is more
865 * important if they are different)
866 * - ($fullcheck only) each module in the course is present in one of
867 * course_sections.sequence
868 * - ($fullcheck only) removes non-existing course modules from section sequences
870 * If there are any mismatches, the changes are made and records are updated in DB.
872 * Course cache is NOT rebuilt if there are any errors!
874 * This function is used each time when course cache is being rebuilt with $fullcheck = false
875 * and in CLI script admin/cli/fix_course_sequence.php with $fullcheck = true
877 * @param int $courseid id of the course
878 * @param array $rawmods result of funciton {@link get_course_mods()} - containst
879 * the list of enabled course modules in the course. Retrieved from DB if not specified.
880 * Argument ignored in cashe of $fullcheck, the list is retrieved form DB anyway.
881 * @param array $sections records from course_sections table for this course.
882 * Retrieved from DB if not specified
883 * @param bool $fullcheck Will add orphaned modules to their sections and remove non-existing
884 * course modules from sequences. Only to be used in site maintenance mode when we are
885 * sure that another user is not in the middle of the process of moving/removing a module.
886 * @param bool $checkonly Only performs the check without updating DB, outputs all errors as debug messages.
887 * @return array array of messages with found problems. Empty output means everything is ok
889 function course_integrity_check($courseid, $rawmods = null, $sections = null, $fullcheck = false, $checkonly = false) {
890 global $DB;
891 $messages = array();
892 if ($sections === null) {
893 $sections = $DB->get_records('course_sections', array('course' => $courseid), 'section', 'id,section,sequence');
895 if ($fullcheck) {
896 // Retrieve all records from course_modules regardless of module type visibility.
897 $rawmods = $DB->get_records('course_modules', array('course' => $courseid), 'id', 'id,section');
899 if ($rawmods === null) {
900 $rawmods = get_course_mods($courseid);
902 if (!$fullcheck && (empty($sections) || empty($rawmods))) {
903 // If either of the arrays is empty, no modules are displayed anyway.
904 return true;
906 $debuggingprefix = 'Failed integrity check for course ['.$courseid.']. ';
908 // First make sure that each module id appears in section sequences only once.
909 // If it appears in several section sequences the last section wins.
910 // If it appears twice in one section sequence, the first occurence wins.
911 $modsection = array();
912 foreach ($sections as $sectionid => $section) {
913 $sections[$sectionid]->newsequence = $section->sequence;
914 if (!empty($section->sequence)) {
915 $sequence = explode(",", $section->sequence);
916 $sequenceunique = array_unique($sequence);
917 if (count($sequenceunique) != count($sequence)) {
918 // Some course module id appears in this section sequence more than once.
919 ksort($sequenceunique); // Preserve initial order of modules.
920 $sequence = array_values($sequenceunique);
921 $sections[$sectionid]->newsequence = join(',', $sequence);
922 $messages[] = $debuggingprefix.'Sequence for course section ['.
923 $sectionid.'] is "'.$sections[$sectionid]->sequence.'", must be "'.$sections[$sectionid]->newsequence.'"';
925 foreach ($sequence as $cmid) {
926 if (array_key_exists($cmid, $modsection) && isset($rawmods[$cmid])) {
927 // Some course module id appears to be in more than one section's sequences.
928 $wrongsectionid = $modsection[$cmid];
929 $sections[$wrongsectionid]->newsequence = trim(preg_replace("/,$cmid,/", ',', ','.$sections[$wrongsectionid]->newsequence. ','), ',');
930 $messages[] = $debuggingprefix.'Course module ['.$cmid.'] must be removed from sequence of section ['.
931 $wrongsectionid.'] because it is also present in sequence of section ['.$sectionid.']';
933 $modsection[$cmid] = $sectionid;
938 // Add orphaned modules to their sections if they exist or to section 0 otherwise.
939 if ($fullcheck) {
940 foreach ($rawmods as $cmid => $mod) {
941 if (!isset($modsection[$cmid])) {
942 // This is a module that is not mentioned in course_section.sequence at all.
943 // Add it to the section $mod->section or to the last available section.
944 if ($mod->section && isset($sections[$mod->section])) {
945 $modsection[$cmid] = $mod->section;
946 } else {
947 $firstsection = reset($sections);
948 $modsection[$cmid] = $firstsection->id;
950 $sections[$modsection[$cmid]]->newsequence = trim($sections[$modsection[$cmid]]->newsequence.','.$cmid, ',');
951 $messages[] = $debuggingprefix.'Course module ['.$cmid.'] is missing from sequence of section ['.
952 $modsection[$cmid].']';
955 foreach ($modsection as $cmid => $sectionid) {
956 if (!isset($rawmods[$cmid])) {
957 // Section $sectionid refers to module id that does not exist.
958 $sections[$sectionid]->newsequence = trim(preg_replace("/,$cmid,/", ',', ','.$sections[$sectionid]->newsequence.','), ',');
959 $messages[] = $debuggingprefix.'Course module ['.$cmid.
960 '] does not exist but is present in the sequence of section ['.$sectionid.']';
965 // Update changed sections.
966 if (!$checkonly && !empty($messages)) {
967 foreach ($sections as $sectionid => $section) {
968 if ($section->newsequence !== $section->sequence) {
969 $DB->update_record('course_sections', array('id' => $sectionid, 'sequence' => $section->newsequence));
974 // Now make sure that all modules point to the correct sections.
975 foreach ($rawmods as $cmid => $mod) {
976 if (isset($modsection[$cmid]) && $modsection[$cmid] != $mod->section) {
977 if (!$checkonly) {
978 $DB->update_record('course_modules', array('id' => $cmid, 'section' => $modsection[$cmid]));
980 $messages[] = $debuggingprefix.'Course module ['.$cmid.
981 '] points to section ['.$mod->section.'] instead of ['.$modsection[$cmid].']';
985 return $messages;
989 * For a given course, returns an array of course activity objects
990 * Each item in the array contains he following properties:
992 function get_array_of_activities($courseid) {
993 // cm - course module id
994 // mod - name of the module (eg forum)
995 // section - the number of the section (eg week or topic)
996 // name - the name of the instance
997 // visible - is the instance visible or not
998 // groupingid - grouping id
999 // groupmembersonly - is this instance visible to group members only
1000 // extra - contains extra string to include in any link
1001 global $CFG, $DB;
1002 if(!empty($CFG->enableavailability)) {
1003 require_once($CFG->libdir.'/conditionlib.php');
1006 $course = $DB->get_record('course', array('id'=>$courseid));
1008 if (empty($course)) {
1009 throw new moodle_exception('courseidnotfound');
1012 $mod = array();
1014 $rawmods = get_course_mods($courseid);
1015 if (empty($rawmods)) {
1016 return $mod; // always return array
1019 if ($sections = $DB->get_records('course_sections', array('course' => $courseid), 'section ASC', 'id,section,sequence')) {
1020 // First check and correct obvious mismatches between course_sections.sequence and course_modules.section.
1021 if ($errormessages = course_integrity_check($courseid, $rawmods, $sections)) {
1022 debugging(join('<br>', $errormessages));
1023 $rawmods = get_course_mods($courseid);
1024 $sections = $DB->get_records('course_sections', array('course' => $courseid), 'section ASC', 'id,section,sequence');
1026 // Build array of activities.
1027 foreach ($sections as $section) {
1028 if (!empty($section->sequence)) {
1029 $sequence = explode(",", $section->sequence);
1030 foreach ($sequence as $seq) {
1031 if (empty($rawmods[$seq])) {
1032 continue;
1034 $mod[$seq] = new stdClass();
1035 $mod[$seq]->id = $rawmods[$seq]->instance;
1036 $mod[$seq]->cm = $rawmods[$seq]->id;
1037 $mod[$seq]->mod = $rawmods[$seq]->modname;
1039 // Oh dear. Inconsistent names left here for backward compatibility.
1040 $mod[$seq]->section = $section->section;
1041 $mod[$seq]->sectionid = $rawmods[$seq]->section;
1043 $mod[$seq]->module = $rawmods[$seq]->module;
1044 $mod[$seq]->added = $rawmods[$seq]->added;
1045 $mod[$seq]->score = $rawmods[$seq]->score;
1046 $mod[$seq]->idnumber = $rawmods[$seq]->idnumber;
1047 $mod[$seq]->visible = $rawmods[$seq]->visible;
1048 $mod[$seq]->visibleold = $rawmods[$seq]->visibleold;
1049 $mod[$seq]->groupmode = $rawmods[$seq]->groupmode;
1050 $mod[$seq]->groupingid = $rawmods[$seq]->groupingid;
1051 $mod[$seq]->groupmembersonly = $rawmods[$seq]->groupmembersonly;
1052 $mod[$seq]->indent = $rawmods[$seq]->indent;
1053 $mod[$seq]->completion = $rawmods[$seq]->completion;
1054 $mod[$seq]->extra = "";
1055 $mod[$seq]->completiongradeitemnumber =
1056 $rawmods[$seq]->completiongradeitemnumber;
1057 $mod[$seq]->completionview = $rawmods[$seq]->completionview;
1058 $mod[$seq]->completionexpected = $rawmods[$seq]->completionexpected;
1059 $mod[$seq]->showdescription = $rawmods[$seq]->showdescription;
1060 $mod[$seq]->availability = $rawmods[$seq]->availability;
1062 $modname = $mod[$seq]->mod;
1063 $functionname = $modname."_get_coursemodule_info";
1065 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
1066 continue;
1069 include_once("$CFG->dirroot/mod/$modname/lib.php");
1071 if ($hasfunction = function_exists($functionname)) {
1072 if ($info = $functionname($rawmods[$seq])) {
1073 if (!empty($info->icon)) {
1074 $mod[$seq]->icon = $info->icon;
1076 if (!empty($info->iconcomponent)) {
1077 $mod[$seq]->iconcomponent = $info->iconcomponent;
1079 if (!empty($info->name)) {
1080 $mod[$seq]->name = $info->name;
1082 if ($info instanceof cached_cm_info) {
1083 // When using cached_cm_info you can include three new fields
1084 // that aren't available for legacy code
1085 if (!empty($info->content)) {
1086 $mod[$seq]->content = $info->content;
1088 if (!empty($info->extraclasses)) {
1089 $mod[$seq]->extraclasses = $info->extraclasses;
1091 if (!empty($info->iconurl)) {
1092 // Convert URL to string as it's easier to store. Also serialized object contains \0 byte and can not be written to Postgres DB.
1093 $url = new moodle_url($info->iconurl);
1094 $mod[$seq]->iconurl = $url->out(false);
1096 if (!empty($info->onclick)) {
1097 $mod[$seq]->onclick = $info->onclick;
1099 if (!empty($info->customdata)) {
1100 $mod[$seq]->customdata = $info->customdata;
1102 } else {
1103 // When using a stdclass, the (horrible) deprecated ->extra field
1104 // is available for BC
1105 if (!empty($info->extra)) {
1106 $mod[$seq]->extra = $info->extra;
1111 // When there is no modname_get_coursemodule_info function,
1112 // but showdescriptions is enabled, then we use the 'intro'
1113 // and 'introformat' fields in the module table
1114 if (!$hasfunction && $rawmods[$seq]->showdescription) {
1115 if ($modvalues = $DB->get_record($rawmods[$seq]->modname,
1116 array('id' => $rawmods[$seq]->instance), 'name, intro, introformat')) {
1117 // Set content from intro and introformat. Filters are disabled
1118 // because we filter it with format_text at display time
1119 $mod[$seq]->content = format_module_intro($rawmods[$seq]->modname,
1120 $modvalues, $rawmods[$seq]->id, false);
1122 // To save making another query just below, put name in here
1123 $mod[$seq]->name = $modvalues->name;
1126 if (!isset($mod[$seq]->name)) {
1127 $mod[$seq]->name = $DB->get_field($rawmods[$seq]->modname, "name", array("id"=>$rawmods[$seq]->instance));
1130 // Minimise the database size by unsetting default options when they are
1131 // 'empty'. This list corresponds to code in the cm_info constructor.
1132 foreach (array('idnumber', 'groupmode', 'groupingid', 'groupmembersonly',
1133 'indent', 'completion', 'extra', 'extraclasses', 'iconurl', 'onclick', 'content',
1134 'icon', 'iconcomponent', 'customdata', 'availability',
1135 'completionview', 'completionexpected', 'score', 'showdescription')
1136 as $property) {
1137 if (property_exists($mod[$seq], $property) &&
1138 empty($mod[$seq]->{$property})) {
1139 unset($mod[$seq]->{$property});
1142 // Special case: this value is usually set to null, but may be 0
1143 if (property_exists($mod[$seq], 'completiongradeitemnumber') &&
1144 is_null($mod[$seq]->completiongradeitemnumber)) {
1145 unset($mod[$seq]->completiongradeitemnumber);
1151 return $mod;
1155 * Returns the localised human-readable names of all used modules
1157 * @param bool $plural if true returns the plural forms of the names
1158 * @return array where key is the module name (component name without 'mod_') and
1159 * the value is the human-readable string. Array sorted alphabetically by value
1161 function get_module_types_names($plural = false) {
1162 static $modnames = null;
1163 global $DB, $CFG;
1164 if ($modnames === null) {
1165 $modnames = array(0 => array(), 1 => array());
1166 if ($allmods = $DB->get_records("modules")) {
1167 foreach ($allmods as $mod) {
1168 if (file_exists("$CFG->dirroot/mod/$mod->name/lib.php") && $mod->visible) {
1169 $modnames[0][$mod->name] = get_string("modulename", "$mod->name");
1170 $modnames[1][$mod->name] = get_string("modulenameplural", "$mod->name");
1173 core_collator::asort($modnames[0]);
1174 core_collator::asort($modnames[1]);
1177 return $modnames[(int)$plural];
1181 * Set highlighted section. Only one section can be highlighted at the time.
1183 * @param int $courseid course id
1184 * @param int $marker highlight section with this number, 0 means remove higlightin
1185 * @return void
1187 function course_set_marker($courseid, $marker) {
1188 global $DB;
1189 $DB->set_field("course", "marker", $marker, array('id' => $courseid));
1190 format_base::reset_course_cache($courseid);
1194 * For a given course section, marks it visible or hidden,
1195 * and does the same for every activity in that section
1197 * @param int $courseid course id
1198 * @param int $sectionnumber The section number to adjust
1199 * @param int $visibility The new visibility
1200 * @return array A list of resources which were hidden in the section
1202 function set_section_visible($courseid, $sectionnumber, $visibility) {
1203 global $DB;
1205 $resourcestotoggle = array();
1206 if ($section = $DB->get_record("course_sections", array("course"=>$courseid, "section"=>$sectionnumber))) {
1207 $DB->set_field("course_sections", "visible", "$visibility", array("id"=>$section->id));
1209 $event = \core\event\course_section_updated::create(array(
1210 'context' => context_course::instance($courseid),
1211 'objectid' => $section->id,
1212 'other' => array(
1213 'sectionnum' => $sectionnumber
1216 $event->add_record_snapshot('course_sections', $section);
1217 $event->trigger();
1219 if (!empty($section->sequence)) {
1220 $modules = explode(",", $section->sequence);
1221 foreach ($modules as $moduleid) {
1222 if ($cm = get_coursemodule_from_id(null, $moduleid, $courseid)) {
1223 if ($visibility) {
1224 // As we unhide the section, we use the previously saved visibility stored in visibleold.
1225 set_coursemodule_visible($moduleid, $cm->visibleold);
1226 } else {
1227 // We hide the section, so we hide the module but we store the original state in visibleold.
1228 set_coursemodule_visible($moduleid, 0);
1229 $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id' => $moduleid));
1231 \core\event\course_module_updated::create_from_cm($cm)->trigger();
1235 rebuild_course_cache($courseid, true);
1237 // Determine which modules are visible for AJAX update
1238 if (!empty($modules)) {
1239 list($insql, $params) = $DB->get_in_or_equal($modules);
1240 $select = 'id ' . $insql . ' AND visible = ?';
1241 array_push($params, $visibility);
1242 if (!$visibility) {
1243 $select .= ' AND visibleold = 1';
1245 $resourcestotoggle = $DB->get_fieldset_select('course_modules', 'id', $select, $params);
1248 return $resourcestotoggle;
1252 * Retrieve all metadata for the requested modules
1254 * @param object $course The Course
1255 * @param array $modnames An array containing the list of modules and their
1256 * names
1257 * @param int $sectionreturn The section to return to
1258 * @return array A list of stdClass objects containing metadata about each
1259 * module
1261 function get_module_metadata($course, $modnames, $sectionreturn = null) {
1262 global $CFG, $OUTPUT;
1264 // get_module_metadata will be called once per section on the page and courses may show
1265 // different modules to one another
1266 static $modlist = array();
1267 if (!isset($modlist[$course->id])) {
1268 $modlist[$course->id] = array();
1271 $return = array();
1272 $urlbase = new moodle_url('/course/mod.php', array('id' => $course->id, 'sesskey' => sesskey()));
1273 if ($sectionreturn !== null) {
1274 $urlbase->param('sr', $sectionreturn);
1276 foreach($modnames as $modname => $modnamestr) {
1277 if (!course_allowed_module($course, $modname)) {
1278 continue;
1280 if (isset($modlist[$course->id][$modname])) {
1281 // This module is already cached
1282 $return[$modname] = $modlist[$course->id][$modname];
1283 continue;
1286 // Include the module lib
1287 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
1288 if (!file_exists($libfile)) {
1289 continue;
1291 include_once($libfile);
1293 // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
1294 $gettypesfunc = $modname.'_get_types';
1295 $types = MOD_SUBTYPE_NO_CHILDREN;
1296 if (function_exists($gettypesfunc)) {
1297 $types = $gettypesfunc();
1299 if ($types !== MOD_SUBTYPE_NO_CHILDREN) {
1300 if (is_array($types) && count($types) > 0) {
1301 $group = new stdClass();
1302 $group->name = $modname;
1303 $group->icon = $OUTPUT->pix_icon('icon', '', $modname, array('class' => 'icon'));
1304 foreach($types as $type) {
1305 if ($type->typestr === '--') {
1306 continue;
1308 if (strpos($type->typestr, '--') === 0) {
1309 $group->title = str_replace('--', '', $type->typestr);
1310 continue;
1312 // Set the Sub Type metadata
1313 $subtype = new stdClass();
1314 $subtype->title = $type->typestr;
1315 $subtype->type = str_replace('&amp;', '&', $type->type);
1316 $subtype->name = preg_replace('/.*type=/', '', $subtype->type);
1317 $subtype->archetype = $type->modclass;
1319 // The group archetype should match the subtype archetypes and all subtypes
1320 // should have the same archetype
1321 $group->archetype = $subtype->archetype;
1323 if (!empty($type->help)) {
1324 $subtype->help = $type->help;
1325 } else if (get_string_manager()->string_exists('help' . $subtype->name, $modname)) {
1326 $subtype->help = get_string('help' . $subtype->name, $modname);
1328 $subtype->link = new moodle_url($urlbase, array('add' => $modname, 'type' => $subtype->name));
1329 $group->types[] = $subtype;
1331 $modlist[$course->id][$modname] = $group;
1333 } else {
1334 $module = new stdClass();
1335 $module->title = $modnamestr;
1336 $module->name = $modname;
1337 $module->link = new moodle_url($urlbase, array('add' => $modname));
1338 $module->icon = $OUTPUT->pix_icon('icon', '', $module->name, array('class' => 'icon'));
1339 $sm = get_string_manager();
1340 if ($sm->string_exists('modulename_help', $modname)) {
1341 $module->help = get_string('modulename_help', $modname);
1342 if ($sm->string_exists('modulename_link', $modname)) { // Link to further info in Moodle docs
1343 $link = get_string('modulename_link', $modname);
1344 $linktext = get_string('morehelp');
1345 $module->help .= html_writer::tag('div', $OUTPUT->doc_link($link, $linktext, true), array('class' => 'helpdoclink'));
1348 $module->archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
1349 $modlist[$course->id][$modname] = $module;
1351 if (isset($modlist[$course->id][$modname])) {
1352 $return[$modname] = $modlist[$course->id][$modname];
1353 } else {
1354 debugging("Invalid module metadata configuration for {$modname}");
1358 return $return;
1362 * Return the course category context for the category with id $categoryid, except
1363 * that if $categoryid is 0, return the system context.
1365 * @param integer $categoryid a category id or 0.
1366 * @return context the corresponding context
1368 function get_category_or_system_context($categoryid) {
1369 if ($categoryid) {
1370 return context_coursecat::instance($categoryid, IGNORE_MISSING);
1371 } else {
1372 return context_system::instance();
1377 * Returns full course categories trees to be used in html_writer::select()
1379 * Calls {@link coursecat::make_categories_list()} to build the tree and
1380 * adds whitespace to denote nesting
1382 * @return array array mapping coursecat id to the display name
1384 function make_categories_options() {
1385 global $CFG;
1386 require_once($CFG->libdir. '/coursecatlib.php');
1387 $cats = coursecat::make_categories_list('', 0, ' / ');
1388 foreach ($cats as $key => $value) {
1389 // Prefix the value with the number of spaces equal to category depth (number of separators in the value).
1390 $cats[$key] = str_repeat('&nbsp;', substr_count($value, ' / ')). $value;
1392 return $cats;
1396 * Print the buttons relating to course requests.
1398 * @param object $context current page context.
1400 function print_course_request_buttons($context) {
1401 global $CFG, $DB, $OUTPUT;
1402 if (empty($CFG->enablecourserequests)) {
1403 return;
1405 if (!has_capability('moodle/course:create', $context) && has_capability('moodle/course:request', $context)) {
1406 /// Print a button to request a new course
1407 echo $OUTPUT->single_button(new moodle_url('/course/request.php'), get_string('requestcourse'), 'get');
1409 /// Print a button to manage pending requests
1410 if ($context->contextlevel == CONTEXT_SYSTEM && has_capability('moodle/site:approvecourse', $context)) {
1411 $disabled = !$DB->record_exists('course_request', array());
1412 echo $OUTPUT->single_button(new moodle_url('/course/pending.php'), get_string('coursespending'), 'get', array('disabled' => $disabled));
1417 * Does the user have permission to edit things in this category?
1419 * @param integer $categoryid The id of the category we are showing, or 0 for system context.
1420 * @return boolean has_any_capability(array(...), ...); in the appropriate context.
1422 function can_edit_in_category($categoryid = 0) {
1423 $context = get_category_or_system_context($categoryid);
1424 return has_any_capability(array('moodle/category:manage', 'moodle/course:create'), $context);
1427 /// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1429 function add_course_module($mod) {
1430 global $DB;
1432 $mod->added = time();
1433 unset($mod->id);
1435 $cmid = $DB->insert_record("course_modules", $mod);
1436 rebuild_course_cache($mod->course, true);
1437 return $cmid;
1441 * Creates missing course section(s) and rebuilds course cache
1443 * @param int|stdClass $courseorid course id or course object
1444 * @param int|array $sections list of relative section numbers to create
1445 * @return bool if there were any sections created
1447 function course_create_sections_if_missing($courseorid, $sections) {
1448 global $DB;
1449 if (!is_array($sections)) {
1450 $sections = array($sections);
1452 $existing = array_keys(get_fast_modinfo($courseorid)->get_section_info_all());
1453 if (is_object($courseorid)) {
1454 $courseorid = $courseorid->id;
1456 $coursechanged = false;
1457 foreach ($sections as $sectionnum) {
1458 if (!in_array($sectionnum, $existing)) {
1459 $cw = new stdClass();
1460 $cw->course = $courseorid;
1461 $cw->section = $sectionnum;
1462 $cw->summary = '';
1463 $cw->summaryformat = FORMAT_HTML;
1464 $cw->sequence = '';
1465 $id = $DB->insert_record("course_sections", $cw);
1466 $coursechanged = true;
1469 if ($coursechanged) {
1470 rebuild_course_cache($courseorid, true);
1472 return $coursechanged;
1476 * Adds an existing module to the section
1478 * Updates both tables {course_sections} and {course_modules}
1480 * Note: This function does not use modinfo PROVIDED that the section you are
1481 * adding the module to already exists. If the section does not exist, it will
1482 * build modinfo if necessary and create the section.
1484 * @param int|stdClass $courseorid course id or course object
1485 * @param int $cmid id of the module already existing in course_modules table
1486 * @param int $sectionnum relative number of the section (field course_sections.section)
1487 * If section does not exist it will be created
1488 * @param int|stdClass $beforemod id or object with field id corresponding to the module
1489 * before which the module needs to be included. Null for inserting in the
1490 * end of the section
1491 * @return int The course_sections ID where the module is inserted
1493 function course_add_cm_to_section($courseorid, $cmid, $sectionnum, $beforemod = null) {
1494 global $DB, $COURSE;
1495 if (is_object($beforemod)) {
1496 $beforemod = $beforemod->id;
1498 if (is_object($courseorid)) {
1499 $courseid = $courseorid->id;
1500 } else {
1501 $courseid = $courseorid;
1503 // Do not try to use modinfo here, there is no guarantee it is valid!
1504 $section = $DB->get_record('course_sections',
1505 array('course' => $courseid, 'section' => $sectionnum), '*', IGNORE_MISSING);
1506 if (!$section) {
1507 // This function call requires modinfo.
1508 course_create_sections_if_missing($courseorid, $sectionnum);
1509 $section = $DB->get_record('course_sections',
1510 array('course' => $courseid, 'section' => $sectionnum), '*', MUST_EXIST);
1513 $modarray = explode(",", trim($section->sequence));
1514 if (empty($section->sequence)) {
1515 $newsequence = "$cmid";
1516 } else if ($beforemod && ($key = array_keys($modarray, $beforemod))) {
1517 $insertarray = array($cmid, $beforemod);
1518 array_splice($modarray, $key[0], 1, $insertarray);
1519 $newsequence = implode(",", $modarray);
1520 } else {
1521 $newsequence = "$section->sequence,$cmid";
1523 $DB->set_field("course_sections", "sequence", $newsequence, array("id" => $section->id));
1524 $DB->set_field('course_modules', 'section', $section->id, array('id' => $cmid));
1525 if (is_object($courseorid)) {
1526 rebuild_course_cache($courseorid->id, true);
1527 } else {
1528 rebuild_course_cache($courseorid, true);
1530 return $section->id; // Return course_sections ID that was used.
1534 * Change the group mode of a course module.
1536 * Note: Do not forget to trigger the event \core\event\course_module_updated as it needs
1537 * to be triggered manually, refer to {@link \core\event\course_module_updated::create_from_cm()}.
1539 * @param int $id course module ID.
1540 * @param int $groupmode the new groupmode value.
1541 * @return bool True if the $groupmode was updated.
1543 function set_coursemodule_groupmode($id, $groupmode) {
1544 global $DB;
1545 $cm = $DB->get_record('course_modules', array('id' => $id), 'id,course,groupmode', MUST_EXIST);
1546 if ($cm->groupmode != $groupmode) {
1547 $DB->set_field('course_modules', 'groupmode', $groupmode, array('id' => $cm->id));
1548 rebuild_course_cache($cm->course, true);
1550 return ($cm->groupmode != $groupmode);
1553 function set_coursemodule_idnumber($id, $idnumber) {
1554 global $DB;
1555 $cm = $DB->get_record('course_modules', array('id' => $id), 'id,course,idnumber', MUST_EXIST);
1556 if ($cm->idnumber != $idnumber) {
1557 $DB->set_field('course_modules', 'idnumber', $idnumber, array('id' => $cm->id));
1558 rebuild_course_cache($cm->course, true);
1560 return ($cm->idnumber != $idnumber);
1564 * Set the visibility of a module and inherent properties.
1566 * Note: Do not forget to trigger the event \core\event\course_module_updated as it needs
1567 * to be triggered manually, refer to {@link \core\event\course_module_updated::create_from_cm()}.
1569 * From 2.4 the parameter $prevstateoverrides has been removed, the logic it triggered
1570 * has been moved to {@link set_section_visible()} which was the only place from which
1571 * the parameter was used.
1573 * @param int $id of the module
1574 * @param int $visible state of the module
1575 * @return bool false when the module was not found, true otherwise
1577 function set_coursemodule_visible($id, $visible) {
1578 global $DB, $CFG;
1579 require_once($CFG->libdir.'/gradelib.php');
1580 require_once($CFG->dirroot.'/calendar/lib.php');
1582 // Trigger developer's attention when using the previously removed argument.
1583 if (func_num_args() > 2) {
1584 debugging('Wrong number of arguments passed to set_coursemodule_visible(), $prevstateoverrides
1585 has been removed.', DEBUG_DEVELOPER);
1588 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
1589 return false;
1592 // Create events and propagate visibility to associated grade items if the value has changed.
1593 // Only do this if it's changed to avoid accidently overwriting manual showing/hiding of student grades.
1594 if ($cm->visible == $visible) {
1595 return true;
1598 if (!$modulename = $DB->get_field('modules', 'name', array('id'=>$cm->module))) {
1599 return false;
1601 if ($events = $DB->get_records('event', array('instance'=>$cm->instance, 'modulename'=>$modulename))) {
1602 foreach($events as $event) {
1603 if ($visible) {
1604 $event = new calendar_event($event);
1605 $event->toggle_visibility(true);
1606 } else {
1607 $event = new calendar_event($event);
1608 $event->toggle_visibility(false);
1613 // Updating visible and visibleold to keep them in sync. Only changing a section visibility will
1614 // affect visibleold to allow for an original visibility restore. See set_section_visible().
1615 $cminfo = new stdClass();
1616 $cminfo->id = $id;
1617 $cminfo->visible = $visible;
1618 $cminfo->visibleold = $visible;
1619 $DB->update_record('course_modules', $cminfo);
1621 // Hide the associated grade items so the teacher doesn't also have to go to the gradebook and hide them there.
1622 // Note that this must be done after updating the row in course_modules, in case
1623 // the modules grade_item_update function needs to access $cm->visible.
1624 if (plugin_supports('mod', $modulename, FEATURE_CONTROLS_GRADE_VISIBILITY) &&
1625 component_callback_exists('mod_' . $modulename, 'grade_item_update')) {
1626 $instance = $DB->get_record($modulename, array('id' => $cm->instance), '*', MUST_EXIST);
1627 component_callback('mod_' . $modulename, 'grade_item_update', array($instance));
1628 } else {
1629 $grade_items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course));
1630 if ($grade_items) {
1631 foreach ($grade_items as $grade_item) {
1632 $grade_item->set_hidden(!$visible);
1637 rebuild_course_cache($cm->course, true);
1638 return true;
1642 * This function will handles the whole deletion process of a module. This includes calling
1643 * the modules delete_instance function, deleting files, events, grades, conditional data,
1644 * the data in the course_module and course_sections table and adding a module deletion
1645 * event to the DB.
1647 * @param int $cmid the course module id
1648 * @since Moodle 2.5
1650 function course_delete_module($cmid) {
1651 global $CFG, $DB;
1653 require_once($CFG->libdir.'/gradelib.php');
1654 require_once($CFG->dirroot.'/blog/lib.php');
1655 require_once($CFG->dirroot.'/calendar/lib.php');
1656 require_once($CFG->dirroot . '/tag/lib.php');
1658 // Get the course module.
1659 if (!$cm = $DB->get_record('course_modules', array('id' => $cmid))) {
1660 return true;
1663 // Get the module context.
1664 $modcontext = context_module::instance($cm->id);
1666 // Get the course module name.
1667 $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST);
1669 // Get the file location of the delete_instance function for this module.
1670 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
1672 // Include the file required to call the delete_instance function for this module.
1673 if (file_exists($modlib)) {
1674 require_once($modlib);
1675 } else {
1676 throw new moodle_exception('cannotdeletemodulemissinglib', '', '', null,
1677 "Cannot delete this module as the file mod/$modulename/lib.php is missing.");
1680 $deleteinstancefunction = $modulename . '_delete_instance';
1682 // Ensure the delete_instance function exists for this module.
1683 if (!function_exists($deleteinstancefunction)) {
1684 throw new moodle_exception('cannotdeletemodulemissingfunc', '', '', null,
1685 "Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/$modulename/lib.php.");
1688 // Call the delete_instance function, if it returns false throw an exception.
1689 if (!$deleteinstancefunction($cm->instance)) {
1690 throw new moodle_exception('cannotdeletemoduleinstance', '', '', null,
1691 "Cannot delete the module $modulename (instance).");
1694 // Remove all module files in case modules forget to do that.
1695 $fs = get_file_storage();
1696 $fs->delete_area_files($modcontext->id);
1698 // Delete events from calendar.
1699 if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
1700 foreach($events as $event) {
1701 $calendarevent = calendar_event::load($event->id);
1702 $calendarevent->delete();
1706 // Delete grade items, outcome items and grades attached to modules.
1707 if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename,
1708 'iteminstance' => $cm->instance, 'courseid' => $cm->course))) {
1709 foreach ($grade_items as $grade_item) {
1710 $grade_item->delete('moddelete');
1714 // Delete completion and availability data; it is better to do this even if the
1715 // features are not turned on, in case they were turned on previously (these will be
1716 // very quick on an empty table).
1717 $DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
1718 $DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id,
1719 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
1721 // Delete all tag instances associated with the instance of this module.
1722 tag_delete_instances('mod_' . $modulename, $modcontext->id);
1724 // Delete the context.
1725 context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
1727 // Delete the module from the course_modules table.
1728 $DB->delete_records('course_modules', array('id' => $cm->id));
1730 // Delete module from that section.
1731 if (!delete_mod_from_section($cm->id, $cm->section)) {
1732 throw new moodle_exception('cannotdeletemodulefromsection', '', '', null,
1733 "Cannot delete the module $modulename (instance) from section.");
1736 // Trigger event for course module delete action.
1737 $event = \core\event\course_module_deleted::create(array(
1738 'courseid' => $cm->course,
1739 'context' => $modcontext,
1740 'objectid' => $cm->id,
1741 'other' => array(
1742 'modulename' => $modulename,
1743 'instanceid' => $cm->instance,
1746 $event->add_record_snapshot('course_modules', $cm);
1747 $event->trigger();
1748 rebuild_course_cache($cm->course, true);
1751 function delete_mod_from_section($modid, $sectionid) {
1752 global $DB;
1754 if ($section = $DB->get_record("course_sections", array("id"=>$sectionid)) ) {
1756 $modarray = explode(",", $section->sequence);
1758 if ($key = array_keys ($modarray, $modid)) {
1759 array_splice($modarray, $key[0], 1);
1760 $newsequence = implode(",", $modarray);
1761 $DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));
1762 rebuild_course_cache($section->course, true);
1763 return true;
1764 } else {
1765 return false;
1769 return false;
1773 * Moves a section within a course, from a position to another.
1774 * Be very careful: $section and $destination refer to section number,
1775 * not id!.
1777 * @param object $course
1778 * @param int $section Section number (not id!!!)
1779 * @param int $destination
1780 * @return boolean Result
1782 function move_section_to($course, $section, $destination) {
1783 /// Moves a whole course section up and down within the course
1784 global $USER, $DB;
1786 if (!$destination && $destination != 0) {
1787 return true;
1790 // compartibility with course formats using field 'numsections'
1791 $courseformatoptions = course_get_format($course)->get_format_options();
1792 if ((array_key_exists('numsections', $courseformatoptions) &&
1793 ($destination > $courseformatoptions['numsections'])) || ($destination < 1)) {
1794 return false;
1797 // Get all sections for this course and re-order them (2 of them should now share the same section number)
1798 if (!$sections = $DB->get_records_menu('course_sections', array('course' => $course->id),
1799 'section ASC, id ASC', 'id, section')) {
1800 return false;
1803 $movedsections = reorder_sections($sections, $section, $destination);
1805 // Update all sections. Do this in 2 steps to avoid breaking database
1806 // uniqueness constraint
1807 $transaction = $DB->start_delegated_transaction();
1808 foreach ($movedsections as $id => $position) {
1809 if ($sections[$id] !== $position) {
1810 $DB->set_field('course_sections', 'section', -$position, array('id' => $id));
1813 foreach ($movedsections as $id => $position) {
1814 if ($sections[$id] !== $position) {
1815 $DB->set_field('course_sections', 'section', $position, array('id' => $id));
1819 // If we move the highlighted section itself, then just highlight the destination.
1820 // Adjust the higlighted section location if we move something over it either direction.
1821 if ($section == $course->marker) {
1822 course_set_marker($course->id, $destination);
1823 } elseif ($section > $course->marker && $course->marker >= $destination) {
1824 course_set_marker($course->id, $course->marker+1);
1825 } elseif ($section < $course->marker && $course->marker <= $destination) {
1826 course_set_marker($course->id, $course->marker-1);
1829 $transaction->allow_commit();
1830 rebuild_course_cache($course->id, true);
1831 return true;
1835 * Reordering algorithm for course sections. Given an array of section->section indexed by section->id,
1836 * an original position number and a target position number, rebuilds the array so that the
1837 * move is made without any duplication of section positions.
1838 * Note: The target_position is the position AFTER WHICH the moved section will be inserted. If you want to
1839 * insert a section before the first one, you must give 0 as the target (section 0 can never be moved).
1841 * @param array $sections
1842 * @param int $origin_position
1843 * @param int $target_position
1844 * @return array
1846 function reorder_sections($sections, $origin_position, $target_position) {
1847 if (!is_array($sections)) {
1848 return false;
1851 // We can't move section position 0
1852 if ($origin_position < 1) {
1853 echo "We can't move section position 0";
1854 return false;
1857 // Locate origin section in sections array
1858 if (!$origin_key = array_search($origin_position, $sections)) {
1859 echo "searched position not in sections array";
1860 return false; // searched position not in sections array
1863 // Extract origin section
1864 $origin_section = $sections[$origin_key];
1865 unset($sections[$origin_key]);
1867 // Find offset of target position (stupid PHP's array_splice requires offset instead of key index!)
1868 $found = false;
1869 $append_array = array();
1870 foreach ($sections as $id => $position) {
1871 if ($found) {
1872 $append_array[$id] = $position;
1873 unset($sections[$id]);
1875 if ($position == $target_position) {
1876 if ($target_position < $origin_position) {
1877 $append_array[$id] = $position;
1878 unset($sections[$id]);
1880 $found = true;
1884 // Append moved section
1885 $sections[$origin_key] = $origin_section;
1887 // Append rest of array (if applicable)
1888 if (!empty($append_array)) {
1889 foreach ($append_array as $id => $position) {
1890 $sections[$id] = $position;
1894 // Renumber positions
1895 $position = 0;
1896 foreach ($sections as $id => $p) {
1897 $sections[$id] = $position;
1898 $position++;
1901 return $sections;
1906 * Move the module object $mod to the specified $section
1907 * If $beforemod exists then that is the module
1908 * before which $modid should be inserted
1910 * @param stdClass|cm_info $mod
1911 * @param stdClass|section_info $section
1912 * @param int|stdClass $beforemod id or object with field id corresponding to the module
1913 * before which the module needs to be included. Null for inserting in the
1914 * end of the section
1915 * @return int new value for module visibility (0 or 1)
1917 function moveto_module($mod, $section, $beforemod=NULL) {
1918 global $OUTPUT, $DB;
1920 // Current module visibility state - return value of this function.
1921 $modvisible = $mod->visible;
1923 // Remove original module from original section.
1924 if (! delete_mod_from_section($mod->id, $mod->section)) {
1925 echo $OUTPUT->notification("Could not delete module from existing section");
1928 // If moving to a hidden section then hide module.
1929 if ($mod->section != $section->id) {
1930 if (!$section->visible && $mod->visible) {
1931 // Module was visible but must become hidden after moving to hidden section.
1932 $modvisible = 0;
1933 set_coursemodule_visible($mod->id, 0);
1934 // Set visibleold to 1 so module will be visible when section is made visible.
1935 $DB->set_field('course_modules', 'visibleold', 1, array('id' => $mod->id));
1937 if ($section->visible && !$mod->visible) {
1938 // Hidden module was moved to the visible section, restore the module visibility from visibleold.
1939 set_coursemodule_visible($mod->id, $mod->visibleold);
1940 $modvisible = $mod->visibleold;
1944 // Add the module into the new section.
1945 course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod);
1946 return $modvisible;
1950 * Returns the list of all editing actions that current user can perform on the module
1952 * @param cm_info $mod The module to produce editing buttons for
1953 * @param int $indent The current indenting (default -1 means no move left-right actions)
1954 * @param int $sr The section to link back to (used for creating the links)
1955 * @return array array of action_link or pix_icon objects
1957 function course_get_cm_edit_actions(cm_info $mod, $indent = -1, $sr = null) {
1958 global $COURSE, $SITE;
1960 static $str;
1962 $coursecontext = context_course::instance($mod->course);
1963 $modcontext = context_module::instance($mod->id);
1965 $editcaps = array('moodle/course:manageactivities', 'moodle/course:activityvisibility', 'moodle/role:assign');
1966 $dupecaps = array('moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport');
1968 // No permission to edit anything.
1969 if (!has_any_capability($editcaps, $modcontext) and !has_all_capabilities($dupecaps, $coursecontext)) {
1970 return array();
1973 $hasmanageactivities = has_capability('moodle/course:manageactivities', $modcontext);
1975 if (!isset($str)) {
1976 $str = get_strings(array('delete', 'move', 'moveright', 'moveleft',
1977 'editsettings', 'duplicate', 'hide', 'show'), 'moodle');
1978 $str->assign = get_string('assignroles', 'role');
1979 $str->groupsnone = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsnone"));
1980 $str->groupsseparate = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsseparate"));
1981 $str->groupsvisible = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsvisible"));
1984 $baseurl = new moodle_url('/course/mod.php', array('sesskey' => sesskey()));
1986 if ($sr !== null) {
1987 $baseurl->param('sr', $sr);
1989 $actions = array();
1991 // Update.
1992 if ($hasmanageactivities) {
1993 $actions['update'] = new action_menu_link_secondary(
1994 new moodle_url($baseurl, array('update' => $mod->id)),
1995 new pix_icon('t/edit', $str->editsettings, 'moodle', array('class' => 'iconsmall', 'title' => '')),
1996 $str->editsettings,
1997 array('class' => 'editing_update', 'data-action' => 'update')
2001 // Indent.
2002 if ($hasmanageactivities && $indent >= 0) {
2003 $indentlimits = new stdClass();
2004 $indentlimits->min = 0;
2005 $indentlimits->max = 16;
2006 if (right_to_left()) { // Exchange arrows on RTL
2007 $rightarrow = 't/left';
2008 $leftarrow = 't/right';
2009 } else {
2010 $rightarrow = 't/right';
2011 $leftarrow = 't/left';
2014 if ($indent >= $indentlimits->max) {
2015 $enabledclass = 'hidden';
2016 } else {
2017 $enabledclass = '';
2019 $actions['moveright'] = new action_menu_link_secondary(
2020 new moodle_url($baseurl, array('id' => $mod->id, 'indent' => '1')),
2021 new pix_icon($rightarrow, $str->moveright, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2022 $str->moveright,
2023 array('class' => 'editing_moveright ' . $enabledclass, 'data-action' => 'moveright', 'data-keepopen' => true)
2026 if ($indent <= $indentlimits->min) {
2027 $enabledclass = 'hidden';
2028 } else {
2029 $enabledclass = '';
2031 $actions['moveleft'] = new action_menu_link_secondary(
2032 new moodle_url($baseurl, array('id' => $mod->id, 'indent' => '-1')),
2033 new pix_icon($leftarrow, $str->moveleft, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2034 $str->moveleft,
2035 array('class' => 'editing_moveleft ' . $enabledclass, 'data-action' => 'moveleft', 'data-keepopen' => true)
2040 // Hide/Show.
2041 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
2042 if ($mod->visible) {
2043 $actions['hide'] = new action_menu_link_secondary(
2044 new moodle_url($baseurl, array('hide' => $mod->id)),
2045 new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2046 $str->hide,
2047 array('class' => 'editing_hide', 'data-action' => 'hide')
2049 } else {
2050 $actions['show'] = new action_menu_link_secondary(
2051 new moodle_url($baseurl, array('show' => $mod->id)),
2052 new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2053 $str->show,
2054 array('class' => 'editing_show', 'data-action' => 'show')
2059 // Duplicate (require both target import caps to be able to duplicate and backup2 support, see modduplicate.php)
2060 // Note that restoring on front page is never allowed.
2061 if ($mod->course != SITEID && has_all_capabilities($dupecaps, $coursecontext) &&
2062 plugin_supports('mod', $mod->modname, FEATURE_BACKUP_MOODLE2)) {
2063 $actions['duplicate'] = new action_menu_link_secondary(
2064 new moodle_url($baseurl, array('duplicate' => $mod->id)),
2065 new pix_icon('t/copy', $str->duplicate, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2066 $str->duplicate,
2067 array('class' => 'editing_duplicate', 'data-action' => 'duplicate', 'data-sr' => $sr)
2071 // Groupmode.
2072 if ($hasmanageactivities && !$mod->coursegroupmodeforce) {
2073 if (plugin_supports('mod', $mod->modname, FEATURE_GROUPS, 0)) {
2074 if ($mod->effectivegroupmode == SEPARATEGROUPS) {
2075 $nextgroupmode = VISIBLEGROUPS;
2076 $grouptitle = $str->groupsseparate;
2077 $actionname = 'groupsseparate';
2078 $groupimage = 'i/groups';
2079 } else if ($mod->effectivegroupmode == VISIBLEGROUPS) {
2080 $nextgroupmode = NOGROUPS;
2081 $grouptitle = $str->groupsvisible;
2082 $actionname = 'groupsvisible';
2083 $groupimage = 'i/groupv';
2084 } else {
2085 $nextgroupmode = SEPARATEGROUPS;
2086 $grouptitle = $str->groupsnone;
2087 $actionname = 'groupsnone';
2088 $groupimage = 'i/groupn';
2091 $actions[$actionname] = new action_menu_link_primary(
2092 new moodle_url($baseurl, array('id' => $mod->id, 'groupmode' => $nextgroupmode)),
2093 new pix_icon($groupimage, null, 'moodle', array('class' => 'iconsmall')),
2094 $grouptitle,
2095 array('class' => 'editing_'. $actionname, 'data-action' => $actionname, 'data-nextgroupmode' => $nextgroupmode, 'aria-live' => 'assertive')
2097 } else {
2098 $actions['nogroupsupport'] = new action_menu_filler();
2102 // Assign.
2103 if (has_capability('moodle/role:assign', $modcontext)){
2104 $actions['assign'] = new action_menu_link_secondary(
2105 new moodle_url('/admin/roles/assign.php', array('contextid' => $modcontext->id)),
2106 new pix_icon('t/assignroles', $str->assign, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2107 $str->assign,
2108 array('class' => 'editing_assign', 'data-action' => 'assignroles')
2112 // Delete.
2113 if ($hasmanageactivities) {
2114 $actions['delete'] = new action_menu_link_secondary(
2115 new moodle_url($baseurl, array('delete' => $mod->id)),
2116 new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2117 $str->delete,
2118 array('class' => 'editing_delete', 'data-action' => 'delete')
2122 return $actions;
2126 * Returns the rename action.
2128 * @param cm_info $mod The module to produce editing buttons for
2129 * @param int $sr The section to link back to (used for creating the links)
2130 * @return The markup for the rename action, or an empty string if not available.
2132 function course_get_cm_rename_action(cm_info $mod, $sr = null) {
2133 global $COURSE, $OUTPUT;
2135 static $str;
2136 static $baseurl;
2138 $modcontext = context_module::instance($mod->id);
2139 $hasmanageactivities = has_capability('moodle/course:manageactivities', $modcontext);
2141 if (!isset($str)) {
2142 $str = get_strings(array('edittitle'));
2145 if (!isset($baseurl)) {
2146 $baseurl = new moodle_url('/course/mod.php', array('sesskey' => sesskey()));
2149 if ($sr !== null) {
2150 $baseurl->param('sr', $sr);
2153 // AJAX edit title.
2154 if ($mod->has_view() && $hasmanageactivities && course_ajax_enabled($COURSE) &&
2155 (($mod->course == $COURSE->id) || ($mod->course == SITEID))) {
2156 // we will not display link if we are on some other-course page (where we should not see this module anyway)
2157 return html_writer::span(
2158 html_writer::link(
2159 new moodle_url($baseurl, array('update' => $mod->id)),
2160 $OUTPUT->pix_icon('t/editstring', '', 'moodle', array('class' => 'iconsmall visibleifjs', 'title' => '')),
2161 array(
2162 'class' => 'editing_title',
2163 'data-action' => 'edittitle',
2164 'title' => $str->edittitle,
2169 return '';
2173 * Returns the move action.
2175 * @param cm_info $mod The module to produce a move button for
2176 * @param int $sr The section to link back to (used for creating the links)
2177 * @return The markup for the move action, or an empty string if not available.
2179 function course_get_cm_move(cm_info $mod, $sr = null) {
2180 global $OUTPUT;
2182 static $str;
2183 static $baseurl;
2185 $modcontext = context_module::instance($mod->id);
2186 $hasmanageactivities = has_capability('moodle/course:manageactivities', $modcontext);
2188 if (!isset($str)) {
2189 $str = get_strings(array('move'));
2192 if (!isset($baseurl)) {
2193 $baseurl = new moodle_url('/course/mod.php', array('sesskey' => sesskey()));
2195 if ($sr !== null) {
2196 $baseurl->param('sr', $sr);
2200 if ($hasmanageactivities) {
2201 $pixicon = 'i/dragdrop';
2203 if (!course_ajax_enabled($mod->get_course())) {
2204 // Override for course frontpage until we get drag/drop working there.
2205 $pixicon = 't/move';
2208 return html_writer::link(
2209 new moodle_url($baseurl, array('copy' => $mod->id)),
2210 $OUTPUT->pix_icon($pixicon, $str->move, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2211 array('class' => 'editing_move', 'data-action' => 'move')
2214 return '';
2218 * given a course object with shortname & fullname, this function will
2219 * truncate the the number of chars allowed and add ... if it was too long
2221 function course_format_name ($course,$max=100) {
2223 $context = context_course::instance($course->id);
2224 $shortname = format_string($course->shortname, true, array('context' => $context));
2225 $fullname = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
2226 $str = $shortname.': '. $fullname;
2227 if (core_text::strlen($str) <= $max) {
2228 return $str;
2230 else {
2231 return core_text::substr($str,0,$max-3).'...';
2236 * Is the user allowed to add this type of module to this course?
2237 * @param object $course the course settings. Only $course->id is used.
2238 * @param string $modname the module name. E.g. 'forum' or 'quiz'.
2239 * @return bool whether the current user is allowed to add this type of module to this course.
2241 function course_allowed_module($course, $modname) {
2242 if (is_numeric($modname)) {
2243 throw new coding_exception('Function course_allowed_module no longer
2244 supports numeric module ids. Please update your code to pass the module name.');
2247 $capability = 'mod/' . $modname . ':addinstance';
2248 if (!get_capability_info($capability)) {
2249 // Debug warning that the capability does not exist, but no more than once per page.
2250 static $warned = array();
2251 $archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
2252 if (!isset($warned[$modname]) && $archetype !== MOD_ARCHETYPE_SYSTEM) {
2253 debugging('The module ' . $modname . ' does not define the standard capability ' .
2254 $capability , DEBUG_DEVELOPER);
2255 $warned[$modname] = 1;
2258 // If the capability does not exist, the module can always be added.
2259 return true;
2262 $coursecontext = context_course::instance($course->id);
2263 return has_capability($capability, $coursecontext);
2267 * Efficiently moves many courses around while maintaining
2268 * sortorder in order.
2270 * @param array $courseids is an array of course ids
2271 * @param int $categoryid
2272 * @return bool success
2274 function move_courses($courseids, $categoryid) {
2275 global $DB;
2277 if (empty($courseids)) {
2278 // Nothing to do.
2279 return false;
2282 if (!$category = $DB->get_record('course_categories', array('id' => $categoryid))) {
2283 return false;
2286 $courseids = array_reverse($courseids);
2287 $newparent = context_coursecat::instance($category->id);
2288 $i = 1;
2290 list($where, $params) = $DB->get_in_or_equal($courseids);
2291 $dbcourses = $DB->get_records_select('course', 'id ' . $where, $params, '', 'id, category, shortname, fullname');
2292 foreach ($dbcourses as $dbcourse) {
2293 $course = new stdClass();
2294 $course->id = $dbcourse->id;
2295 $course->category = $category->id;
2296 $course->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - $i++;
2297 if ($category->visible == 0) {
2298 // Hide the course when moving into hidden category, do not update the visibleold flag - we want to get
2299 // to previous state if somebody unhides the category.
2300 $course->visible = 0;
2303 $DB->update_record('course', $course);
2305 // Update context, so it can be passed to event.
2306 $context = context_course::instance($course->id);
2307 $context->update_moved($newparent);
2309 // Trigger a course updated event.
2310 $event = \core\event\course_updated::create(array(
2311 'objectid' => $course->id,
2312 'context' => context_course::instance($course->id),
2313 'other' => array('shortname' => $dbcourse->shortname,
2314 'fullname' => $dbcourse->fullname)
2316 $event->set_legacy_logdata(array($course->id, 'course', 'move', 'edit.php?id=' . $course->id, $course->id));
2317 $event->trigger();
2319 fix_course_sortorder();
2320 cache_helper::purge_by_event('changesincourse');
2322 return true;
2326 * Returns the display name of the given section that the course prefers
2328 * Implementation of this function is provided by course format
2329 * @see format_base::get_section_name()
2331 * @param int|stdClass $courseorid The course to get the section name for (object or just course id)
2332 * @param int|stdClass $section Section object from database or just field course_sections.section
2333 * @return string Display name that the course format prefers, e.g. "Week 2"
2335 function get_section_name($courseorid, $section) {
2336 return course_get_format($courseorid)->get_section_name($section);
2340 * Tells if current course format uses sections
2342 * @param string $format Course format ID e.g. 'weeks' $course->format
2343 * @return bool
2345 function course_format_uses_sections($format) {
2346 $course = new stdClass();
2347 $course->format = $format;
2348 return course_get_format($course)->uses_sections();
2352 * Returns the information about the ajax support in the given source format
2354 * The returned object's property (boolean)capable indicates that
2355 * the course format supports Moodle course ajax features.
2357 * @param string $format
2358 * @return stdClass
2360 function course_format_ajax_support($format) {
2361 $course = new stdClass();
2362 $course->format = $format;
2363 return course_get_format($course)->supports_ajax();
2367 * Can the current user delete this course?
2368 * Course creators have exception,
2369 * 1 day after the creation they can sill delete the course.
2370 * @param int $courseid
2371 * @return boolean
2373 function can_delete_course($courseid) {
2374 global $USER;
2376 $context = context_course::instance($courseid);
2378 if (has_capability('moodle/course:delete', $context)) {
2379 return true;
2382 // hack: now try to find out if creator created this course recently (1 day)
2383 if (!has_capability('moodle/course:create', $context)) {
2384 return false;
2387 $since = time() - 60*60*24;
2388 $course = get_course($courseid);
2390 if ($course->timecreated < $since) {
2391 return false; // Return if the course was not created in last 24 hours.
2394 $logmanger = get_log_manager();
2395 $readers = $logmanger->get_readers('\core\log\sql_select_reader');
2396 $reader = reset($readers);
2398 if (empty($reader)) {
2399 return false; // No log reader found.
2402 // A proper reader.
2403 $select = "userid = :userid AND courseid = :courseid AND eventname = :eventname AND timecreated > :since";
2404 $params = array('userid' => $USER->id, 'since' => $since, 'courseid' => $course->id, 'eventname' => '\core\event\course_created');
2406 return (bool)$reader->get_events_select_count($select, $params);
2410 * Save the Your name for 'Some role' strings.
2412 * @param integer $courseid the id of this course.
2413 * @param array $data the data that came from the course settings form.
2415 function save_local_role_names($courseid, $data) {
2416 global $DB;
2417 $context = context_course::instance($courseid);
2419 foreach ($data as $fieldname => $value) {
2420 if (strpos($fieldname, 'role_') !== 0) {
2421 continue;
2423 list($ignored, $roleid) = explode('_', $fieldname);
2425 // make up our mind whether we want to delete, update or insert
2426 if (!$value) {
2427 $DB->delete_records('role_names', array('contextid' => $context->id, 'roleid' => $roleid));
2429 } else if ($rolename = $DB->get_record('role_names', array('contextid' => $context->id, 'roleid' => $roleid))) {
2430 $rolename->name = $value;
2431 $DB->update_record('role_names', $rolename);
2433 } else {
2434 $rolename = new stdClass;
2435 $rolename->contextid = $context->id;
2436 $rolename->roleid = $roleid;
2437 $rolename->name = $value;
2438 $DB->insert_record('role_names', $rolename);
2444 * Returns options to use in course overviewfiles filemanager
2446 * @param null|stdClass|course_in_list|int $course either object that has 'id' property or just the course id;
2447 * may be empty if course does not exist yet (course create form)
2448 * @return array|null array of options such as maxfiles, maxbytes, accepted_types, etc.
2449 * or null if overviewfiles are disabled
2451 function course_overviewfiles_options($course) {
2452 global $CFG;
2453 if (empty($CFG->courseoverviewfileslimit)) {
2454 return null;
2456 $accepted_types = preg_split('/\s*,\s*/', trim($CFG->courseoverviewfilesext), -1, PREG_SPLIT_NO_EMPTY);
2457 if (in_array('*', $accepted_types) || empty($accepted_types)) {
2458 $accepted_types = '*';
2459 } else {
2460 // Since config for $CFG->courseoverviewfilesext is a text box, human factor must be considered.
2461 // Make sure extensions are prefixed with dot unless they are valid typegroups
2462 foreach ($accepted_types as $i => $type) {
2463 if (substr($type, 0, 1) !== '.') {
2464 require_once($CFG->libdir. '/filelib.php');
2465 if (!count(file_get_typegroup('extension', $type))) {
2466 // It does not start with dot and is not a valid typegroup, this is most likely extension.
2467 $accepted_types[$i] = '.'. $type;
2468 $corrected = true;
2472 if (!empty($corrected)) {
2473 set_config('courseoverviewfilesext', join(',', $accepted_types));
2476 $options = array(
2477 'maxfiles' => $CFG->courseoverviewfileslimit,
2478 'maxbytes' => $CFG->maxbytes,
2479 'subdirs' => 0,
2480 'accepted_types' => $accepted_types
2482 if (!empty($course->id)) {
2483 $options['context'] = context_course::instance($course->id);
2484 } else if (is_int($course) && $course > 0) {
2485 $options['context'] = context_course::instance($course);
2487 return $options;
2491 * Create a course and either return a $course object
2493 * Please note this functions does not verify any access control,
2494 * the calling code is responsible for all validation (usually it is the form definition).
2496 * @param array $editoroptions course description editor options
2497 * @param object $data - all the data needed for an entry in the 'course' table
2498 * @return object new course instance
2500 function create_course($data, $editoroptions = NULL) {
2501 global $DB;
2503 //check the categoryid - must be given for all new courses
2504 $category = $DB->get_record('course_categories', array('id'=>$data->category), '*', MUST_EXIST);
2506 // Check if the shortname already exists.
2507 if (!empty($data->shortname)) {
2508 if ($DB->record_exists('course', array('shortname' => $data->shortname))) {
2509 throw new moodle_exception('shortnametaken', '', '', $data->shortname);
2513 // Check if the idnumber already exists.
2514 if (!empty($data->idnumber)) {
2515 if ($DB->record_exists('course', array('idnumber' => $data->idnumber))) {
2516 throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
2520 $data->timecreated = time();
2521 $data->timemodified = $data->timecreated;
2523 // place at beginning of any category
2524 $data->sortorder = 0;
2526 if ($editoroptions) {
2527 // summary text is updated later, we need context to store the files first
2528 $data->summary = '';
2529 $data->summary_format = FORMAT_HTML;
2532 if (!isset($data->visible)) {
2533 // data not from form, add missing visibility info
2534 $data->visible = $category->visible;
2536 $data->visibleold = $data->visible;
2538 $newcourseid = $DB->insert_record('course', $data);
2539 $context = context_course::instance($newcourseid, MUST_EXIST);
2541 if ($editoroptions) {
2542 // Save the files used in the summary editor and store
2543 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
2544 $DB->set_field('course', 'summary', $data->summary, array('id'=>$newcourseid));
2545 $DB->set_field('course', 'summaryformat', $data->summary_format, array('id'=>$newcourseid));
2547 if ($overviewfilesoptions = course_overviewfiles_options($newcourseid)) {
2548 // Save the course overviewfiles
2549 $data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0);
2552 // update course format options
2553 course_get_format($newcourseid)->update_course_format_options($data);
2555 $course = course_get_format($newcourseid)->get_course();
2557 // Setup the blocks
2558 blocks_add_default_course_blocks($course);
2560 // Create a default section.
2561 course_create_sections_if_missing($course, 0);
2563 fix_course_sortorder();
2564 // purge appropriate caches in case fix_course_sortorder() did not change anything
2565 cache_helper::purge_by_event('changesincourse');
2567 // new context created - better mark it as dirty
2568 $context->mark_dirty();
2570 // Save any custom role names.
2571 save_local_role_names($course->id, (array)$data);
2573 // set up enrolments
2574 enrol_course_updated(true, $course, $data);
2576 // Trigger a course created event.
2577 $event = \core\event\course_created::create(array(
2578 'objectid' => $course->id,
2579 'context' => context_course::instance($course->id),
2580 'other' => array('shortname' => $course->shortname,
2581 'fullname' => $course->fullname)
2583 $event->trigger();
2585 return $course;
2589 * Update a course.
2591 * Please note this functions does not verify any access control,
2592 * the calling code is responsible for all validation (usually it is the form definition).
2594 * @param object $data - all the data needed for an entry in the 'course' table
2595 * @param array $editoroptions course description editor options
2596 * @return void
2598 function update_course($data, $editoroptions = NULL) {
2599 global $DB;
2601 $data->timemodified = time();
2603 $oldcourse = course_get_format($data->id)->get_course();
2604 $context = context_course::instance($oldcourse->id);
2606 if ($editoroptions) {
2607 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
2609 if ($overviewfilesoptions = course_overviewfiles_options($data->id)) {
2610 $data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0);
2613 // Check we don't have a duplicate shortname.
2614 if (!empty($data->shortname) && $oldcourse->shortname != $data->shortname) {
2615 if ($DB->record_exists_sql('SELECT id from {course} WHERE shortname = ? AND id <> ?', array($data->shortname, $data->id))) {
2616 throw new moodle_exception('shortnametaken', '', '', $data->shortname);
2620 // Check we don't have a duplicate idnumber.
2621 if (!empty($data->idnumber) && $oldcourse->idnumber != $data->idnumber) {
2622 if ($DB->record_exists_sql('SELECT id from {course} WHERE idnumber = ? AND id <> ?', array($data->idnumber, $data->id))) {
2623 throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
2627 if (!isset($data->category) or empty($data->category)) {
2628 // prevent nulls and 0 in category field
2629 unset($data->category);
2631 $changesincoursecat = $movecat = (isset($data->category) and $oldcourse->category != $data->category);
2633 if (!isset($data->visible)) {
2634 // data not from form, add missing visibility info
2635 $data->visible = $oldcourse->visible;
2638 if ($data->visible != $oldcourse->visible) {
2639 // reset the visibleold flag when manually hiding/unhiding course
2640 $data->visibleold = $data->visible;
2641 $changesincoursecat = true;
2642 } else {
2643 if ($movecat) {
2644 $newcategory = $DB->get_record('course_categories', array('id'=>$data->category));
2645 if (empty($newcategory->visible)) {
2646 // make sure when moving into hidden category the course is hidden automatically
2647 $data->visible = 0;
2652 // Update with the new data
2653 $DB->update_record('course', $data);
2654 // make sure the modinfo cache is reset
2655 rebuild_course_cache($data->id);
2657 // update course format options with full course data
2658 course_get_format($data->id)->update_course_format_options($data, $oldcourse);
2660 $course = $DB->get_record('course', array('id'=>$data->id));
2662 if ($movecat) {
2663 $newparent = context_coursecat::instance($course->category);
2664 $context->update_moved($newparent);
2666 $fixcoursesortorder = $movecat || (isset($data->sortorder) && ($oldcourse->sortorder != $data->sortorder));
2667 if ($fixcoursesortorder) {
2668 fix_course_sortorder();
2671 // purge appropriate caches in case fix_course_sortorder() did not change anything
2672 cache_helper::purge_by_event('changesincourse');
2673 if ($changesincoursecat) {
2674 cache_helper::purge_by_event('changesincoursecat');
2677 // Test for and remove blocks which aren't appropriate anymore
2678 blocks_remove_inappropriate($course);
2680 // Save any custom role names.
2681 save_local_role_names($course->id, $data);
2683 // update enrol settings
2684 enrol_course_updated(false, $course, $data);
2686 // Trigger a course updated event.
2687 $event = \core\event\course_updated::create(array(
2688 'objectid' => $course->id,
2689 'context' => context_course::instance($course->id),
2690 'other' => array('shortname' => $course->shortname,
2691 'fullname' => $course->fullname)
2694 $event->set_legacy_logdata(array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id));
2695 $event->trigger();
2697 if ($oldcourse->format !== $course->format) {
2698 // Remove all options stored for the previous format
2699 // We assume that new course format migrated everything it needed watching trigger
2700 // 'course_updated' and in method format_XXX::update_course_format_options()
2701 $DB->delete_records('course_format_options',
2702 array('courseid' => $course->id, 'format' => $oldcourse->format));
2707 * Average number of participants
2708 * @return integer
2710 function average_number_of_participants() {
2711 global $DB, $SITE;
2713 //count total of enrolments for visible course (except front page)
2714 $sql = 'SELECT COUNT(*) FROM (
2715 SELECT DISTINCT ue.userid, e.courseid
2716 FROM {user_enrolments} ue, {enrol} e, {course} c
2717 WHERE ue.enrolid = e.id
2718 AND e.courseid <> :siteid
2719 AND c.id = e.courseid
2720 AND c.visible = 1) total';
2721 $params = array('siteid' => $SITE->id);
2722 $enrolmenttotal = $DB->count_records_sql($sql, $params);
2725 //count total of visible courses (minus front page)
2726 $coursetotal = $DB->count_records('course', array('visible' => 1));
2727 $coursetotal = $coursetotal - 1 ;
2729 //average of enrolment
2730 if (empty($coursetotal)) {
2731 $participantaverage = 0;
2732 } else {
2733 $participantaverage = $enrolmenttotal / $coursetotal;
2736 return $participantaverage;
2740 * Average number of course modules
2741 * @return integer
2743 function average_number_of_courses_modules() {
2744 global $DB, $SITE;
2746 //count total of visible course module (except front page)
2747 $sql = 'SELECT COUNT(*) FROM (
2748 SELECT cm.course, cm.module
2749 FROM {course} c, {course_modules} cm
2750 WHERE c.id = cm.course
2751 AND c.id <> :siteid
2752 AND cm.visible = 1
2753 AND c.visible = 1) total';
2754 $params = array('siteid' => $SITE->id);
2755 $moduletotal = $DB->count_records_sql($sql, $params);
2758 //count total of visible courses (minus front page)
2759 $coursetotal = $DB->count_records('course', array('visible' => 1));
2760 $coursetotal = $coursetotal - 1 ;
2762 //average of course module
2763 if (empty($coursetotal)) {
2764 $coursemoduleaverage = 0;
2765 } else {
2766 $coursemoduleaverage = $moduletotal / $coursetotal;
2769 return $coursemoduleaverage;
2773 * This class pertains to course requests and contains methods associated with
2774 * create, approving, and removing course requests.
2776 * Please note we do not allow embedded images here because there is no context
2777 * to store them with proper access control.
2779 * @copyright 2009 Sam Hemelryk
2780 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2781 * @since Moodle 2.0
2783 * @property-read int $id
2784 * @property-read string $fullname
2785 * @property-read string $shortname
2786 * @property-read string $summary
2787 * @property-read int $summaryformat
2788 * @property-read int $summarytrust
2789 * @property-read string $reason
2790 * @property-read int $requester
2792 class course_request {
2795 * This is the stdClass that stores the properties for the course request
2796 * and is externally accessed through the __get magic method
2797 * @var stdClass
2799 protected $properties;
2802 * An array of options for the summary editor used by course request forms.
2803 * This is initially set by {@link summary_editor_options()}
2804 * @var array
2805 * @static
2807 protected static $summaryeditoroptions;
2810 * Static function to prepare the summary editor for working with a course
2811 * request.
2813 * @static
2814 * @param null|stdClass $data Optional, an object containing the default values
2815 * for the form, these may be modified when preparing the
2816 * editor so this should be called before creating the form
2817 * @return stdClass An object that can be used to set the default values for
2818 * an mforms form
2820 public static function prepare($data=null) {
2821 if ($data === null) {
2822 $data = new stdClass;
2824 $data = file_prepare_standard_editor($data, 'summary', self::summary_editor_options());
2825 return $data;
2829 * Static function to create a new course request when passed an array of properties
2830 * for it.
2832 * This function also handles saving any files that may have been used in the editor
2834 * @static
2835 * @param stdClass $data
2836 * @return course_request The newly created course request
2838 public static function create($data) {
2839 global $USER, $DB, $CFG;
2840 $data->requester = $USER->id;
2842 // Setting the default category if none set.
2843 if (empty($data->category) || empty($CFG->requestcategoryselection)) {
2844 $data->category = $CFG->defaultrequestcategory;
2847 // Summary is a required field so copy the text over
2848 $data->summary = $data->summary_editor['text'];
2849 $data->summaryformat = $data->summary_editor['format'];
2851 $data->id = $DB->insert_record('course_request', $data);
2853 // Create a new course_request object and return it
2854 $request = new course_request($data);
2856 // Notify the admin if required.
2857 if ($users = get_users_from_config($CFG->courserequestnotify, 'moodle/site:approvecourse')) {
2859 $a = new stdClass;
2860 $a->link = "$CFG->wwwroot/course/pending.php";
2861 $a->user = fullname($USER);
2862 $subject = get_string('courserequest');
2863 $message = get_string('courserequestnotifyemail', 'admin', $a);
2864 foreach ($users as $user) {
2865 $request->notify($user, $USER, 'courserequested', $subject, $message);
2869 return $request;
2873 * Returns an array of options to use with a summary editor
2875 * @uses course_request::$summaryeditoroptions
2876 * @return array An array of options to use with the editor
2878 public static function summary_editor_options() {
2879 global $CFG;
2880 if (self::$summaryeditoroptions === null) {
2881 self::$summaryeditoroptions = array('maxfiles' => 0, 'maxbytes'=>0);
2883 return self::$summaryeditoroptions;
2887 * Loads the properties for this course request object. Id is required and if
2888 * only id is provided then we load the rest of the properties from the database
2890 * @param stdClass|int $properties Either an object containing properties
2891 * or the course_request id to load
2893 public function __construct($properties) {
2894 global $DB;
2895 if (empty($properties->id)) {
2896 if (empty($properties)) {
2897 throw new coding_exception('You must provide a course request id when creating a course_request object');
2899 $id = $properties;
2900 $properties = new stdClass;
2901 $properties->id = (int)$id;
2902 unset($id);
2904 if (empty($properties->requester)) {
2905 if (!($this->properties = $DB->get_record('course_request', array('id' => $properties->id)))) {
2906 print_error('unknowncourserequest');
2908 } else {
2909 $this->properties = $properties;
2911 $this->properties->collision = null;
2915 * Returns the requested property
2917 * @param string $key
2918 * @return mixed
2920 public function __get($key) {
2921 return $this->properties->$key;
2925 * Override this to ensure empty($request->blah) calls return a reliable answer...
2927 * This is required because we define the __get method
2929 * @param mixed $key
2930 * @return bool True is it not empty, false otherwise
2932 public function __isset($key) {
2933 return (!empty($this->properties->$key));
2937 * Returns the user who requested this course
2939 * Uses a static var to cache the results and cut down the number of db queries
2941 * @staticvar array $requesters An array of cached users
2942 * @return stdClass The user who requested the course
2944 public function get_requester() {
2945 global $DB;
2946 static $requesters= array();
2947 if (!array_key_exists($this->properties->requester, $requesters)) {
2948 $requesters[$this->properties->requester] = $DB->get_record('user', array('id'=>$this->properties->requester));
2950 return $requesters[$this->properties->requester];
2954 * Checks that the shortname used by the course does not conflict with any other
2955 * courses that exist
2957 * @param string|null $shortnamemark The string to append to the requests shortname
2958 * should a conflict be found
2959 * @return bool true is there is a conflict, false otherwise
2961 public function check_shortname_collision($shortnamemark = '[*]') {
2962 global $DB;
2964 if ($this->properties->collision !== null) {
2965 return $this->properties->collision;
2968 if (empty($this->properties->shortname)) {
2969 debugging('Attempting to check a course request shortname before it has been set', DEBUG_DEVELOPER);
2970 $this->properties->collision = false;
2971 } else if ($DB->record_exists('course', array('shortname' => $this->properties->shortname))) {
2972 if (!empty($shortnamemark)) {
2973 $this->properties->shortname .= ' '.$shortnamemark;
2975 $this->properties->collision = true;
2976 } else {
2977 $this->properties->collision = false;
2979 return $this->properties->collision;
2983 * Returns the category where this course request should be created
2985 * Note that we don't check here that user has a capability to view
2986 * hidden categories if he has capabilities 'moodle/site:approvecourse' and
2987 * 'moodle/course:changecategory'
2989 * @return coursecat
2991 public function get_category() {
2992 global $CFG;
2993 require_once($CFG->libdir.'/coursecatlib.php');
2994 // If the category is not set, if the current user does not have the rights to change the category, or if the
2995 // category does not exist, we set the default category to the course to be approved.
2996 // The system level is used because the capability moodle/site:approvecourse is based on a system level.
2997 if (empty($this->properties->category) || !has_capability('moodle/course:changecategory', context_system::instance()) ||
2998 (!$category = coursecat::get($this->properties->category, IGNORE_MISSING, true))) {
2999 $category = coursecat::get($CFG->defaultrequestcategory, IGNORE_MISSING, true);
3001 if (!$category) {
3002 $category = coursecat::get_default();
3004 return $category;
3008 * This function approves the request turning it into a course
3010 * This function converts the course request into a course, at the same time
3011 * transferring any files used in the summary to the new course and then removing
3012 * the course request and the files associated with it.
3014 * @return int The id of the course that was created from this request
3016 public function approve() {
3017 global $CFG, $DB, $USER;
3019 $user = $DB->get_record('user', array('id' => $this->properties->requester, 'deleted'=>0), '*', MUST_EXIST);
3021 $courseconfig = get_config('moodlecourse');
3023 // Transfer appropriate settings
3024 $data = clone($this->properties);
3025 unset($data->id);
3026 unset($data->reason);
3027 unset($data->requester);
3029 // Set category
3030 $category = $this->get_category();
3031 $data->category = $category->id;
3032 // Set misc settings
3033 $data->requested = 1;
3035 // Apply course default settings
3036 $data->format = $courseconfig->format;
3037 $data->newsitems = $courseconfig->newsitems;
3038 $data->showgrades = $courseconfig->showgrades;
3039 $data->showreports = $courseconfig->showreports;
3040 $data->maxbytes = $courseconfig->maxbytes;
3041 $data->groupmode = $courseconfig->groupmode;
3042 $data->groupmodeforce = $courseconfig->groupmodeforce;
3043 $data->visible = $courseconfig->visible;
3044 $data->visibleold = $data->visible;
3045 $data->lang = $courseconfig->lang;
3047 $course = create_course($data);
3048 $context = context_course::instance($course->id, MUST_EXIST);
3050 // add enrol instances
3051 if (!$DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) {
3052 if ($manual = enrol_get_plugin('manual')) {
3053 $manual->add_default_instance($course);
3057 // enrol the requester as teacher if necessary
3058 if (!empty($CFG->creatornewroleid) and !is_viewing($context, $user, 'moodle/role:assign') and !is_enrolled($context, $user, 'moodle/role:assign')) {
3059 enrol_try_internal_enrol($course->id, $user->id, $CFG->creatornewroleid);
3062 $this->delete();
3064 $a = new stdClass();
3065 $a->name = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
3066 $a->url = $CFG->wwwroot.'/course/view.php?id=' . $course->id;
3067 $this->notify($user, $USER, 'courserequestapproved', get_string('courseapprovedsubject'), get_string('courseapprovedemail2', 'moodle', $a));
3069 return $course->id;
3073 * Reject a course request
3075 * This function rejects a course request, emailing the requesting user the
3076 * provided notice and then removing the request from the database
3078 * @param string $notice The message to display to the user
3080 public function reject($notice) {
3081 global $USER, $DB;
3082 $user = $DB->get_record('user', array('id' => $this->properties->requester), '*', MUST_EXIST);
3083 $this->notify($user, $USER, 'courserequestrejected', get_string('courserejectsubject'), get_string('courserejectemail', 'moodle', $notice));
3084 $this->delete();
3088 * Deletes the course request and any associated files
3090 public function delete() {
3091 global $DB;
3092 $DB->delete_records('course_request', array('id' => $this->properties->id));
3096 * Send a message from one user to another using events_trigger
3098 * @param object $touser
3099 * @param object $fromuser
3100 * @param string $name
3101 * @param string $subject
3102 * @param string $message
3104 protected function notify($touser, $fromuser, $name='courserequested', $subject, $message) {
3105 $eventdata = new stdClass();
3106 $eventdata->component = 'moodle';
3107 $eventdata->name = $name;
3108 $eventdata->userfrom = $fromuser;
3109 $eventdata->userto = $touser;
3110 $eventdata->subject = $subject;
3111 $eventdata->fullmessage = $message;
3112 $eventdata->fullmessageformat = FORMAT_PLAIN;
3113 $eventdata->fullmessagehtml = '';
3114 $eventdata->smallmessage = '';
3115 $eventdata->notification = 1;
3116 message_send($eventdata);
3121 * Return a list of page types
3122 * @param string $pagetype current page type
3123 * @param context $parentcontext Block's parent context
3124 * @param context $currentcontext Current context of block
3125 * @return array array of page types
3127 function course_page_type_list($pagetype, $parentcontext, $currentcontext) {
3128 if ($pagetype === 'course-index' || $pagetype === 'course-index-category') {
3129 // For courses and categories browsing pages (/course/index.php) add option to show on ANY category page
3130 $pagetypes = array('*' => get_string('page-x', 'pagetype'),
3131 'course-index-*' => get_string('page-course-index-x', 'pagetype'),
3133 } else if ($currentcontext && (!($coursecontext = $currentcontext->get_course_context(false)) || $coursecontext->instanceid == SITEID)) {
3134 // We know for sure that despite pagetype starts with 'course-' this is not a page in course context (i.e. /course/search.php, etc.)
3135 $pagetypes = array('*' => get_string('page-x', 'pagetype'));
3136 } else {
3137 // Otherwise consider it a page inside a course even if $currentcontext is null
3138 $pagetypes = array('*' => get_string('page-x', 'pagetype'),
3139 'course-*' => get_string('page-course-x', 'pagetype'),
3140 'course-view-*' => get_string('page-course-view-x', 'pagetype')
3143 return $pagetypes;
3147 * Determine whether course ajax should be enabled for the specified course
3149 * @param stdClass $course The course to test against
3150 * @return boolean Whether course ajax is enabled or note
3152 function course_ajax_enabled($course) {
3153 global $CFG, $PAGE, $SITE;
3155 // The user must be editing for AJAX to be included
3156 if (!$PAGE->user_is_editing()) {
3157 return false;
3160 // Check that the theme suports
3161 if (!$PAGE->theme->enablecourseajax) {
3162 return false;
3165 // Check that the course format supports ajax functionality
3166 // The site 'format' doesn't have information on course format support
3167 if ($SITE->id !== $course->id) {
3168 $courseformatajaxsupport = course_format_ajax_support($course->format);
3169 if (!$courseformatajaxsupport->capable) {
3170 return false;
3174 // All conditions have been met so course ajax should be enabled
3175 return true;
3179 * Include the relevant javascript and language strings for the resource
3180 * toolbox YUI module
3182 * @param integer $id The ID of the course being applied to
3183 * @param array $usedmodules An array containing the names of the modules in use on the page
3184 * @param array $enabledmodules An array containing the names of the enabled (visible) modules on this site
3185 * @param stdClass $config An object containing configuration parameters for ajax modules including:
3186 * * resourceurl The URL to post changes to for resource changes
3187 * * sectionurl The URL to post changes to for section changes
3188 * * pageparams Additional parameters to pass through in the post
3189 * @return bool
3191 function include_course_ajax($course, $usedmodules = array(), $enabledmodules = null, $config = null) {
3192 global $CFG, $PAGE, $SITE;
3194 // Ensure that ajax should be included
3195 if (!course_ajax_enabled($course)) {
3196 return false;
3199 if (!$config) {
3200 $config = new stdClass();
3203 // The URL to use for resource changes
3204 if (!isset($config->resourceurl)) {
3205 $config->resourceurl = '/course/rest.php';
3208 // The URL to use for section changes
3209 if (!isset($config->sectionurl)) {
3210 $config->sectionurl = '/course/rest.php';
3213 // Any additional parameters which need to be included on page submission
3214 if (!isset($config->pageparams)) {
3215 $config->pageparams = array();
3218 // Include toolboxes
3219 $PAGE->requires->yui_module('moodle-course-toolboxes',
3220 'M.course.init_resource_toolbox',
3221 array(array(
3222 'courseid' => $course->id,
3223 'ajaxurl' => $config->resourceurl,
3224 'config' => $config,
3227 $PAGE->requires->yui_module('moodle-course-toolboxes',
3228 'M.course.init_section_toolbox',
3229 array(array(
3230 'courseid' => $course->id,
3231 'format' => $course->format,
3232 'ajaxurl' => $config->sectionurl,
3233 'config' => $config,
3237 // Include course dragdrop
3238 if (course_format_uses_sections($course->format)) {
3239 $PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_section_dragdrop',
3240 array(array(
3241 'courseid' => $course->id,
3242 'ajaxurl' => $config->sectionurl,
3243 'config' => $config,
3244 )), null, true);
3246 $PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_resource_dragdrop',
3247 array(array(
3248 'courseid' => $course->id,
3249 'ajaxurl' => $config->resourceurl,
3250 'config' => $config,
3251 )), null, true);
3254 // Require various strings for the command toolbox
3255 $PAGE->requires->strings_for_js(array(
3256 'moveleft',
3257 'deletechecktype',
3258 'deletechecktypename',
3259 'edittitle',
3260 'edittitleinstructions',
3261 'show',
3262 'hide',
3263 'groupsnone',
3264 'groupsvisible',
3265 'groupsseparate',
3266 'clicktochangeinbrackets',
3267 'markthistopic',
3268 'markedthistopic',
3269 'movesection',
3270 'movecoursemodule',
3271 'movecoursesection',
3272 'movecontent',
3273 'tocontent',
3274 'emptydragdropregion',
3275 'afterresource',
3276 'aftersection',
3277 'totopofsection',
3278 ), 'moodle');
3280 // Include section-specific strings for formats which support sections.
3281 if (course_format_uses_sections($course->format)) {
3282 $PAGE->requires->strings_for_js(array(
3283 'showfromothers',
3284 'hidefromothers',
3285 ), 'format_' . $course->format);
3288 // For confirming resource deletion we need the name of the module in question
3289 foreach ($usedmodules as $module => $modname) {
3290 $PAGE->requires->string_for_js('pluginname', $module);
3293 // Load drag and drop upload AJAX.
3294 require_once($CFG->dirroot.'/course/dnduploadlib.php');
3295 dndupload_add_to_course($course, $enabledmodules);
3297 return true;
3301 * Returns the sorted list of available course formats, filtered by enabled if necessary
3303 * @param bool $enabledonly return only formats that are enabled
3304 * @return array array of sorted format names
3306 function get_sorted_course_formats($enabledonly = false) {
3307 global $CFG;
3308 $formats = core_component::get_plugin_list('format');
3310 if (!empty($CFG->format_plugins_sortorder)) {
3311 $order = explode(',', $CFG->format_plugins_sortorder);
3312 $order = array_merge(array_intersect($order, array_keys($formats)),
3313 array_diff(array_keys($formats), $order));
3314 } else {
3315 $order = array_keys($formats);
3317 if (!$enabledonly) {
3318 return $order;
3320 $sortedformats = array();
3321 foreach ($order as $formatname) {
3322 if (!get_config('format_'.$formatname, 'disabled')) {
3323 $sortedformats[] = $formatname;
3326 return $sortedformats;
3330 * The URL to use for the specified course (with section)
3332 * @param int|stdClass $courseorid The course to get the section name for (either object or just course id)
3333 * @param int|stdClass $section Section object from database or just field course_sections.section
3334 * if omitted the course view page is returned
3335 * @param array $options options for view URL. At the moment core uses:
3336 * 'navigation' (bool) if true and section has no separate page, the function returns null
3337 * 'sr' (int) used by multipage formats to specify to which section to return
3338 * @return moodle_url The url of course
3340 function course_get_url($courseorid, $section = null, $options = array()) {
3341 return course_get_format($courseorid)->get_view_url($section, $options);
3345 * Create a module.
3347 * It includes:
3348 * - capability checks and other checks
3349 * - create the module from the module info
3351 * @param object $module
3352 * @return object the created module info
3353 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
3355 function create_module($moduleinfo) {
3356 global $DB, $CFG;
3358 require_once($CFG->dirroot . '/course/modlib.php');
3360 // Check manadatory attributs.
3361 $mandatoryfields = array('modulename', 'course', 'section', 'visible');
3362 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
3363 $mandatoryfields[] = 'introeditor';
3365 foreach($mandatoryfields as $mandatoryfield) {
3366 if (!isset($moduleinfo->{$mandatoryfield})) {
3367 throw new moodle_exception('createmodulemissingattribut', '', '', $mandatoryfield);
3371 // Some additional checks (capability / existing instances).
3372 $course = $DB->get_record('course', array('id'=>$moduleinfo->course), '*', MUST_EXIST);
3373 list($module, $context, $cw) = can_add_moduleinfo($course, $moduleinfo->modulename, $moduleinfo->section);
3375 // Add the module.
3376 $moduleinfo->module = $module->id;
3377 $moduleinfo = add_moduleinfo($moduleinfo, $course, null);
3379 return $moduleinfo;
3383 * Update a module.
3385 * It includes:
3386 * - capability and other checks
3387 * - update the module
3389 * @param object $module
3390 * @return object the updated module info
3391 * @throws moodle_exception if current user is not allowed to update the module
3393 function update_module($moduleinfo) {
3394 global $DB, $CFG;
3396 require_once($CFG->dirroot . '/course/modlib.php');
3398 // Check the course module exists.
3399 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST);
3401 // Check the course exists.
3402 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
3404 // Some checks (capaibility / existing instances).
3405 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
3407 // Retrieve few information needed by update_moduleinfo.
3408 $moduleinfo->modulename = $cm->modname;
3409 if (!isset($moduleinfo->scale)) {
3410 $moduleinfo->scale = 0;
3412 $moduleinfo->type = 'mod';
3414 // Update the module.
3415 list($cm, $moduleinfo) = update_moduleinfo($cm, $moduleinfo, $course, null);
3417 return $moduleinfo;
3421 * Duplicate a module on the course.
3423 * @param object $course The course
3424 * @param object $cm The course module to duplicate
3425 * @throws moodle_exception if the plugin doesn't support duplication
3426 * @return Object containing:
3427 * - fullcontent: The HTML markup for the created CM
3428 * - cmid: The CMID of the newly created CM
3429 * - redirect: Whether to trigger a redirect following this change
3431 function mod_duplicate_activity($course, $cm, $sr = null) {
3432 global $CFG, $USER, $PAGE, $DB;
3434 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
3435 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
3436 require_once($CFG->libdir . '/filelib.php');
3438 $a = new stdClass();
3439 $a->modtype = get_string('modulename', $cm->modname);
3440 $a->modname = format_string($cm->name);
3442 if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
3443 throw new moodle_exception('duplicatenosupport', 'error');
3446 // backup the activity
3448 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
3449 backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
3451 $backupid = $bc->get_backupid();
3452 $backupbasepath = $bc->get_plan()->get_basepath();
3454 $bc->execute_plan();
3456 $bc->destroy();
3458 // restore the backup immediately
3460 $rc = new restore_controller($backupid, $course->id,
3461 backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
3463 $cmcontext = context_module::instance($cm->id);
3464 if (!$rc->execute_precheck()) {
3465 $precheckresults = $rc->get_precheck_results();
3466 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
3467 if (empty($CFG->keeptempdirectoriesonbackup)) {
3468 fulldelete($backupbasepath);
3473 $rc->execute_plan();
3475 // now a bit hacky part follows - we try to get the cmid of the newly
3476 // restored copy of the module
3477 $newcmid = null;
3478 $tasks = $rc->get_plan()->get_tasks();
3479 foreach ($tasks as $task) {
3480 error_log("Looking at a task");
3481 if (is_subclass_of($task, 'restore_activity_task')) {
3482 error_log("Looking at a restore_activity_task task");
3483 if ($task->get_old_contextid() == $cmcontext->id) {
3484 error_log("Contexts match");
3485 $newcmid = $task->get_moduleid();
3486 break;
3491 // if we know the cmid of the new course module, let us move it
3492 // right below the original one. otherwise it will stay at the
3493 // end of the section
3494 if ($newcmid) {
3495 $info = get_fast_modinfo($course);
3496 $newcm = $info->get_cm($newcmid);
3497 $section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
3498 moveto_module($newcm, $section, $cm);
3499 moveto_module($cm, $section, $newcm);
3501 // Trigger course module created event. We can trigger the event only if we know the newcmid.
3502 $event = \core\event\course_module_created::create_from_cm($newcm);
3503 $event->trigger();
3505 rebuild_course_cache($cm->course);
3507 $rc->destroy();
3509 if (empty($CFG->keeptempdirectoriesonbackup)) {
3510 fulldelete($backupbasepath);
3513 $resp = new stdClass();
3514 if ($newcm) {
3515 $courserenderer = $PAGE->get_renderer('core', 'course');
3516 $completioninfo = new completion_info($course);
3517 $modulehtml = $courserenderer->course_section_cm($course, $completioninfo,
3518 $newcm, null, array());
3520 $resp->fullcontent = $courserenderer->course_section_cm_list_item($course, $completioninfo, $newcm, $sr);
3521 $resp->cmid = $newcm->id;
3522 } else {
3523 // Trigger a redirect
3524 $resp->redirect = true;
3526 return $resp;
3530 * Compare two objects to find out their correct order based on timestamp (to be used by usort).
3531 * Sorts by descending order of time.
3533 * @param stdClass $a First object
3534 * @param stdClass $b Second object
3535 * @return int 0,1,-1 representing the order
3537 function compare_activities_by_time_desc($a, $b) {
3538 // Make sure the activities actually have a timestamp property.
3539 if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3540 return 0;
3542 // We treat instances without timestamp as if they have a timestamp of 0.
3543 if ((!property_exists($a, 'timestamp')) && (property_exists($b,'timestamp'))) {
3544 return 1;
3546 if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3547 return -1;
3549 if ($a->timestamp == $b->timestamp) {
3550 return 0;
3552 return ($a->timestamp > $b->timestamp) ? -1 : 1;
3556 * Compare two objects to find out their correct order based on timestamp (to be used by usort).
3557 * Sorts by ascending order of time.
3559 * @param stdClass $a First object
3560 * @param stdClass $b Second object
3561 * @return int 0,1,-1 representing the order
3563 function compare_activities_by_time_asc($a, $b) {
3564 // Make sure the activities actually have a timestamp property.
3565 if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3566 return 0;
3568 // We treat instances without timestamp as if they have a timestamp of 0.
3569 if ((!property_exists($a, 'timestamp')) && (property_exists($b, 'timestamp'))) {
3570 return -1;
3572 if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3573 return 1;
3575 if ($a->timestamp == $b->timestamp) {
3576 return 0;
3578 return ($a->timestamp < $b->timestamp) ? -1 : 1;
3582 * Changes the visibility of a course.
3584 * @param int $courseid The course to change.
3585 * @param bool $show True to make it visible, false otherwise.
3586 * @return bool
3588 function course_change_visibility($courseid, $show = true) {
3589 $course = new stdClass;
3590 $course->id = $courseid;
3591 $course->visible = ($show) ? '1' : '0';
3592 $course->visibleold = $course->visible;
3593 update_course($course);
3594 return true;
3598 * Changes the course sortorder by one, moving it up or down one in respect to sort order.
3600 * @param stdClass|course_in_list $course
3601 * @param bool $up If set to true the course will be moved up one. Otherwise down one.
3602 * @return bool
3604 function course_change_sortorder_by_one($course, $up) {
3605 global $DB;
3606 $params = array($course->sortorder, $course->category);
3607 if ($up) {
3608 $select = 'sortorder < ? AND category = ?';
3609 $sort = 'sortorder DESC';
3610 } else {
3611 $select = 'sortorder > ? AND category = ?';
3612 $sort = 'sortorder ASC';
3614 fix_course_sortorder();
3615 $swapcourse = $DB->get_records_select('course', $select, $params, $sort, '*', 0, 1);
3616 if ($swapcourse) {
3617 $swapcourse = reset($swapcourse);
3618 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $course->id));
3619 $DB->set_field('course', 'sortorder', $course->sortorder, array('id' => $swapcourse->id));
3620 // Finally reorder courses.
3621 fix_course_sortorder();
3622 cache_helper::purge_by_event('changesincourse');
3623 return true;
3625 return false;
3629 * Changes the sort order of courses in a category so that the first course appears after the second.
3631 * @param int|stdClass $courseorid The course to focus on.
3632 * @param int $moveaftercourseid The course to shifter after or 0 if you want it to be the first course in the category.
3633 * @return bool
3635 function course_change_sortorder_after_course($courseorid, $moveaftercourseid) {
3636 global $DB;
3638 if (!is_object($courseorid)) {
3639 $course = get_course($courseorid);
3640 } else {
3641 $course = $courseorid;
3644 if ((int)$moveaftercourseid === 0) {
3645 // We've moving the course to the start of the queue.
3646 $sql = 'SELECT sortorder
3647 FROM {course}
3648 WHERE category = :categoryid
3649 ORDER BY sortorder';
3650 $params = array(
3651 'categoryid' => $course->category
3653 $sortorder = $DB->get_field_sql($sql, $params, IGNORE_MULTIPLE);
3655 $sql = 'UPDATE {course}
3656 SET sortorder = sortorder + 1
3657 WHERE category = :categoryid
3658 AND id <> :id';
3659 $params = array(
3660 'categoryid' => $course->category,
3661 'id' => $course->id,
3663 $DB->execute($sql, $params);
3664 $DB->set_field('course', 'sortorder', $sortorder, array('id' => $course->id));
3665 } else if ($course->id === $moveaftercourseid) {
3666 // They're the same - moronic.
3667 debugging("Invalid move after course given.", DEBUG_DEVELOPER);
3668 return false;
3669 } else {
3670 // Moving this course after the given course. It could be before it could be after.
3671 $moveaftercourse = get_course($moveaftercourseid);
3672 if ($course->category !== $moveaftercourse->category) {
3673 debugging("Cannot re-order courses. The given courses do not belong to the same category.", DEBUG_DEVELOPER);
3674 return false;
3676 // Increment all courses in the same category that are ordered after the moveafter course.
3677 // This makes a space for the course we're moving.
3678 $sql = 'UPDATE {course}
3679 SET sortorder = sortorder + 1
3680 WHERE category = :categoryid
3681 AND sortorder > :sortorder';
3682 $params = array(
3683 'categoryid' => $moveaftercourse->category,
3684 'sortorder' => $moveaftercourse->sortorder
3686 $DB->execute($sql, $params);
3687 $DB->set_field('course', 'sortorder', $moveaftercourse->sortorder + 1, array('id' => $course->id));
3689 fix_course_sortorder();
3690 cache_helper::purge_by_event('changesincourse');
3691 return true;