MDL-41806 Add assessors to moodle_url class
[moodle.git] / course / lib.php
blob50494d5e5e2f38b080afa9cab8cd4b92e72f91fc
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Library of useful functions
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package core
24 * @subpackage course
27 defined('MOODLE_INTERNAL') || die;
29 require_once($CFG->libdir.'/completionlib.php');
30 require_once($CFG->libdir.'/filelib.php');
31 require_once($CFG->dirroot.'/course/format/lib.php');
33 define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records
34 define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds
36 /**
37 * Number of courses to display when summaries are included.
38 * @var int
39 * @deprecated since 2.4, use $CFG->courseswithsummarieslimit instead.
41 define('COURSE_MAX_SUMMARIES_PER_PAGE', 10);
43 define('COURSE_MAX_COURSES_PER_DROPDOWN',1000); // max courses in log dropdown before switching to optional
44 define('COURSE_MAX_USERS_PER_DROPDOWN',1000); // max users in log dropdown before switching to optional
45 define('FRONTPAGENEWS', '0');
46 define('FRONTPAGECOURSELIST', '1'); // Not used. TODO MDL-38832 remove
47 define('FRONTPAGECATEGORYNAMES', '2');
48 define('FRONTPAGETOPICONLY', '3'); // Not used. TODO MDL-38832 remove
49 define('FRONTPAGECATEGORYCOMBO', '4');
50 define('FRONTPAGEENROLLEDCOURSELIST', '5');
51 define('FRONTPAGEALLCOURSELIST', '6');
52 define('FRONTPAGECOURSESEARCH', '7');
53 define('FRONTPAGECOURSELIMIT', 200); // Important! Replaced with $CFG->frontpagecourselimit - maximum number of courses displayed on the frontpage. TODO MDL-38832 remove
54 define('EXCELROWS', 65535);
55 define('FIRSTUSEDEXCELROW', 3);
57 define('MOD_CLASS_ACTIVITY', 0);
58 define('MOD_CLASS_RESOURCE', 1);
60 function make_log_url($module, $url) {
61 switch ($module) {
62 case 'course':
63 if (strpos($url, 'report/') === 0) {
64 // there is only one report type, course reports are deprecated
65 $url = "/$url";
66 break;
68 case 'file':
69 case 'login':
70 case 'lib':
71 case 'admin':
72 case 'calendar':
73 case 'category':
74 case 'mnet course':
75 if (strpos($url, '../') === 0) {
76 $url = ltrim($url, '.');
77 } else {
78 $url = "/course/$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]->availablefrom = $rawmods[$seq]->availablefrom;
1060 $mod[$seq]->availableuntil = $rawmods[$seq]->availableuntil;
1061 $mod[$seq]->showavailability = $rawmods[$seq]->showavailability;
1062 $mod[$seq]->showdescription = $rawmods[$seq]->showdescription;
1063 if (!empty($CFG->enableavailability)) {
1064 condition_info::fill_availability_conditions($rawmods[$seq]);
1065 $mod[$seq]->conditionscompletion = $rawmods[$seq]->conditionscompletion;
1066 $mod[$seq]->conditionsgrade = $rawmods[$seq]->conditionsgrade;
1067 $mod[$seq]->conditionsfield = $rawmods[$seq]->conditionsfield;
1070 $modname = $mod[$seq]->mod;
1071 $functionname = $modname."_get_coursemodule_info";
1073 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
1074 continue;
1077 include_once("$CFG->dirroot/mod/$modname/lib.php");
1079 if ($hasfunction = function_exists($functionname)) {
1080 if ($info = $functionname($rawmods[$seq])) {
1081 if (!empty($info->icon)) {
1082 $mod[$seq]->icon = $info->icon;
1084 if (!empty($info->iconcomponent)) {
1085 $mod[$seq]->iconcomponent = $info->iconcomponent;
1087 if (!empty($info->name)) {
1088 $mod[$seq]->name = $info->name;
1090 if ($info instanceof cached_cm_info) {
1091 // When using cached_cm_info you can include three new fields
1092 // that aren't available for legacy code
1093 if (!empty($info->content)) {
1094 $mod[$seq]->content = $info->content;
1096 if (!empty($info->extraclasses)) {
1097 $mod[$seq]->extraclasses = $info->extraclasses;
1099 if (!empty($info->iconurl)) {
1100 // Convert URL to string as it's easier to store. Also serialized object contains \0 byte and can not be written to Postgres DB.
1101 $url = new moodle_url($info->iconurl);
1102 $mod[$seq]->iconurl = $url->out(false);
1104 if (!empty($info->onclick)) {
1105 $mod[$seq]->onclick = $info->onclick;
1107 if (!empty($info->customdata)) {
1108 $mod[$seq]->customdata = $info->customdata;
1110 } else {
1111 // When using a stdclass, the (horrible) deprecated ->extra field
1112 // is available for BC
1113 if (!empty($info->extra)) {
1114 $mod[$seq]->extra = $info->extra;
1119 // When there is no modname_get_coursemodule_info function,
1120 // but showdescriptions is enabled, then we use the 'intro'
1121 // and 'introformat' fields in the module table
1122 if (!$hasfunction && $rawmods[$seq]->showdescription) {
1123 if ($modvalues = $DB->get_record($rawmods[$seq]->modname,
1124 array('id' => $rawmods[$seq]->instance), 'name, intro, introformat')) {
1125 // Set content from intro and introformat. Filters are disabled
1126 // because we filter it with format_text at display time
1127 $mod[$seq]->content = format_module_intro($rawmods[$seq]->modname,
1128 $modvalues, $rawmods[$seq]->id, false);
1130 // To save making another query just below, put name in here
1131 $mod[$seq]->name = $modvalues->name;
1134 if (!isset($mod[$seq]->name)) {
1135 $mod[$seq]->name = $DB->get_field($rawmods[$seq]->modname, "name", array("id"=>$rawmods[$seq]->instance));
1138 // Minimise the database size by unsetting default options when they are
1139 // 'empty'. This list corresponds to code in the cm_info constructor.
1140 foreach (array('idnumber', 'groupmode', 'groupingid', 'groupmembersonly',
1141 'indent', 'completion', 'extra', 'extraclasses', 'iconurl', 'onclick', 'content',
1142 'icon', 'iconcomponent', 'customdata', 'showavailability', 'availablefrom',
1143 'availableuntil', 'conditionscompletion', 'conditionsgrade',
1144 'completionview', 'completionexpected', 'score', 'showdescription')
1145 as $property) {
1146 if (property_exists($mod[$seq], $property) &&
1147 empty($mod[$seq]->{$property})) {
1148 unset($mod[$seq]->{$property});
1151 // Special case: this value is usually set to null, but may be 0
1152 if (property_exists($mod[$seq], 'completiongradeitemnumber') &&
1153 is_null($mod[$seq]->completiongradeitemnumber)) {
1154 unset($mod[$seq]->completiongradeitemnumber);
1160 return $mod;
1164 * Returns the localised human-readable names of all used modules
1166 * @param bool $plural if true returns the plural forms of the names
1167 * @return array where key is the module name (component name without 'mod_') and
1168 * the value is the human-readable string. Array sorted alphabetically by value
1170 function get_module_types_names($plural = false) {
1171 static $modnames = null;
1172 global $DB, $CFG;
1173 if ($modnames === null) {
1174 $modnames = array(0 => array(), 1 => array());
1175 if ($allmods = $DB->get_records("modules")) {
1176 foreach ($allmods as $mod) {
1177 if (file_exists("$CFG->dirroot/mod/$mod->name/lib.php") && $mod->visible) {
1178 $modnames[0][$mod->name] = get_string("modulename", "$mod->name");
1179 $modnames[1][$mod->name] = get_string("modulenameplural", "$mod->name");
1182 core_collator::asort($modnames[0]);
1183 core_collator::asort($modnames[1]);
1186 return $modnames[(int)$plural];
1190 * Set highlighted section. Only one section can be highlighted at the time.
1192 * @param int $courseid course id
1193 * @param int $marker highlight section with this number, 0 means remove higlightin
1194 * @return void
1196 function course_set_marker($courseid, $marker) {
1197 global $DB;
1198 $DB->set_field("course", "marker", $marker, array('id' => $courseid));
1199 format_base::reset_course_cache($courseid);
1203 * For a given course section, marks it visible or hidden,
1204 * and does the same for every activity in that section
1206 * @param int $courseid course id
1207 * @param int $sectionnumber The section number to adjust
1208 * @param int $visibility The new visibility
1209 * @return array A list of resources which were hidden in the section
1211 function set_section_visible($courseid, $sectionnumber, $visibility) {
1212 global $DB;
1214 $resourcestotoggle = array();
1215 if ($section = $DB->get_record("course_sections", array("course"=>$courseid, "section"=>$sectionnumber))) {
1216 $DB->set_field("course_sections", "visible", "$visibility", array("id"=>$section->id));
1217 if (!empty($section->sequence)) {
1218 $modules = explode(",", $section->sequence);
1219 foreach ($modules as $moduleid) {
1220 if ($cm = $DB->get_record('course_modules', array('id' => $moduleid), 'visible, visibleold')) {
1221 if ($visibility) {
1222 // As we unhide the section, we use the previously saved visibility stored in visibleold.
1223 set_coursemodule_visible($moduleid, $cm->visibleold);
1224 } else {
1225 // We hide the section, so we hide the module but we store the original state in visibleold.
1226 set_coursemodule_visible($moduleid, 0);
1227 $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id' => $moduleid));
1232 rebuild_course_cache($courseid, true);
1234 // Determine which modules are visible for AJAX update
1235 if (!empty($modules)) {
1236 list($insql, $params) = $DB->get_in_or_equal($modules);
1237 $select = 'id ' . $insql . ' AND visible = ?';
1238 array_push($params, $visibility);
1239 if (!$visibility) {
1240 $select .= ' AND visibleold = 1';
1242 $resourcestotoggle = $DB->get_fieldset_select('course_modules', 'id', $select, $params);
1245 return $resourcestotoggle;
1249 * Retrieve all metadata for the requested modules
1251 * @param object $course The Course
1252 * @param array $modnames An array containing the list of modules and their
1253 * names
1254 * @param int $sectionreturn The section to return to
1255 * @return array A list of stdClass objects containing metadata about each
1256 * module
1258 function get_module_metadata($course, $modnames, $sectionreturn = null) {
1259 global $CFG, $OUTPUT;
1261 // get_module_metadata will be called once per section on the page and courses may show
1262 // different modules to one another
1263 static $modlist = array();
1264 if (!isset($modlist[$course->id])) {
1265 $modlist[$course->id] = array();
1268 $return = array();
1269 $urlbase = new moodle_url('/course/mod.php', array('id' => $course->id, 'sesskey' => sesskey()));
1270 if ($sectionreturn !== null) {
1271 $urlbase->param('sr', $sectionreturn);
1273 foreach($modnames as $modname => $modnamestr) {
1274 if (!course_allowed_module($course, $modname)) {
1275 continue;
1277 if (isset($modlist[$course->id][$modname])) {
1278 // This module is already cached
1279 $return[$modname] = $modlist[$course->id][$modname];
1280 continue;
1283 // Include the module lib
1284 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
1285 if (!file_exists($libfile)) {
1286 continue;
1288 include_once($libfile);
1290 // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
1291 $gettypesfunc = $modname.'_get_types';
1292 if (function_exists($gettypesfunc)) {
1293 $types = $gettypesfunc();
1294 if (is_array($types) && count($types) > 0) {
1295 $group = new stdClass();
1296 $group->name = $modname;
1297 $group->icon = $OUTPUT->pix_icon('icon', '', $modname, array('class' => 'icon'));
1298 foreach($types as $type) {
1299 if ($type->typestr === '--') {
1300 continue;
1302 if (strpos($type->typestr, '--') === 0) {
1303 $group->title = str_replace('--', '', $type->typestr);
1304 continue;
1306 // Set the Sub Type metadata
1307 $subtype = new stdClass();
1308 $subtype->title = $type->typestr;
1309 $subtype->type = str_replace('&amp;', '&', $type->type);
1310 $subtype->name = preg_replace('/.*type=/', '', $subtype->type);
1311 $subtype->archetype = $type->modclass;
1313 // The group archetype should match the subtype archetypes and all subtypes
1314 // should have the same archetype
1315 $group->archetype = $subtype->archetype;
1317 if (get_string_manager()->string_exists('help' . $subtype->name, $modname)) {
1318 $subtype->help = get_string('help' . $subtype->name, $modname);
1320 $subtype->link = new moodle_url($urlbase, array('add' => $modname, 'type' => $subtype->name));
1321 $group->types[] = $subtype;
1323 $modlist[$course->id][$modname] = $group;
1325 } else {
1326 $module = new stdClass();
1327 $module->title = $modnamestr;
1328 $module->name = $modname;
1329 $module->link = new moodle_url($urlbase, array('add' => $modname));
1330 $module->icon = $OUTPUT->pix_icon('icon', '', $module->name, array('class' => 'icon'));
1331 $sm = get_string_manager();
1332 if ($sm->string_exists('modulename_help', $modname)) {
1333 $module->help = get_string('modulename_help', $modname);
1334 if ($sm->string_exists('modulename_link', $modname)) { // Link to further info in Moodle docs
1335 $link = get_string('modulename_link', $modname);
1336 $linktext = get_string('morehelp');
1337 $module->help .= html_writer::tag('div', $OUTPUT->doc_link($link, $linktext, true), array('class' => 'helpdoclink'));
1340 $module->archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
1341 $modlist[$course->id][$modname] = $module;
1343 if (isset($modlist[$course->id][$modname])) {
1344 $return[$modname] = $modlist[$course->id][$modname];
1345 } else {
1346 debugging("Invalid module metadata configuration for {$modname}");
1350 return $return;
1354 * Return the course category context for the category with id $categoryid, except
1355 * that if $categoryid is 0, return the system context.
1357 * @param integer $categoryid a category id or 0.
1358 * @return object the corresponding context
1360 function get_category_or_system_context($categoryid) {
1361 if ($categoryid) {
1362 return context_coursecat::instance($categoryid, IGNORE_MISSING);
1363 } else {
1364 return context_system::instance();
1369 * Returns full course categories trees to be used in html_writer::select()
1371 * Calls {@link coursecat::make_categories_list()} to build the tree and
1372 * adds whitespace to denote nesting
1374 * @return array array mapping coursecat id to the display name
1376 function make_categories_options() {
1377 global $CFG;
1378 require_once($CFG->libdir. '/coursecatlib.php');
1379 $cats = coursecat::make_categories_list('', 0, ' / ');
1380 foreach ($cats as $key => $value) {
1381 // Prefix the value with the number of spaces equal to category depth (number of separators in the value).
1382 $cats[$key] = str_repeat('&nbsp;', substr_count($value, ' / ')). $value;
1384 return $cats;
1388 * Print the buttons relating to course requests.
1390 * @param object $context current page context.
1392 function print_course_request_buttons($context) {
1393 global $CFG, $DB, $OUTPUT;
1394 if (empty($CFG->enablecourserequests)) {
1395 return;
1397 if (!has_capability('moodle/course:create', $context) && has_capability('moodle/course:request', $context)) {
1398 /// Print a button to request a new course
1399 echo $OUTPUT->single_button(new moodle_url('/course/request.php'), get_string('requestcourse'), 'get');
1401 /// Print a button to manage pending requests
1402 if ($context->contextlevel == CONTEXT_SYSTEM && has_capability('moodle/site:approvecourse', $context)) {
1403 $disabled = !$DB->record_exists('course_request', array());
1404 echo $OUTPUT->single_button(new moodle_url('/course/pending.php'), get_string('coursespending'), 'get', array('disabled' => $disabled));
1409 * Does the user have permission to edit things in this category?
1411 * @param integer $categoryid The id of the category we are showing, or 0 for system context.
1412 * @return boolean has_any_capability(array(...), ...); in the appropriate context.
1414 function can_edit_in_category($categoryid = 0) {
1415 $context = get_category_or_system_context($categoryid);
1416 return has_any_capability(array('moodle/category:manage', 'moodle/course:create'), $context);
1419 /// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
1421 function add_course_module($mod) {
1422 global $DB;
1424 $mod->added = time();
1425 unset($mod->id);
1427 $cmid = $DB->insert_record("course_modules", $mod);
1428 rebuild_course_cache($mod->course, true);
1429 return $cmid;
1433 * Creates missing course section(s) and rebuilds course cache
1435 * @param int|stdClass $courseorid course id or course object
1436 * @param int|array $sections list of relative section numbers to create
1437 * @return bool if there were any sections created
1439 function course_create_sections_if_missing($courseorid, $sections) {
1440 global $DB;
1441 if (!is_array($sections)) {
1442 $sections = array($sections);
1444 $existing = array_keys(get_fast_modinfo($courseorid)->get_section_info_all());
1445 if (is_object($courseorid)) {
1446 $courseorid = $courseorid->id;
1448 $coursechanged = false;
1449 foreach ($sections as $sectionnum) {
1450 if (!in_array($sectionnum, $existing)) {
1451 $cw = new stdClass();
1452 $cw->course = $courseorid;
1453 $cw->section = $sectionnum;
1454 $cw->summary = '';
1455 $cw->summaryformat = FORMAT_HTML;
1456 $cw->sequence = '';
1457 $id = $DB->insert_record("course_sections", $cw);
1458 $coursechanged = true;
1461 if ($coursechanged) {
1462 rebuild_course_cache($courseorid, true);
1464 return $coursechanged;
1468 * Adds an existing module to the section
1470 * Updates both tables {course_sections} and {course_modules}
1472 * Note: This function does not use modinfo PROVIDED that the section you are
1473 * adding the module to already exists. If the section does not exist, it will
1474 * build modinfo if necessary and create the section.
1476 * @param int|stdClass $courseorid course id or course object
1477 * @param int $cmid id of the module already existing in course_modules table
1478 * @param int $sectionnum relative number of the section (field course_sections.section)
1479 * If section does not exist it will be created
1480 * @param int|stdClass $beforemod id or object with field id corresponding to the module
1481 * before which the module needs to be included. Null for inserting in the
1482 * end of the section
1483 * @return int The course_sections ID where the module is inserted
1485 function course_add_cm_to_section($courseorid, $cmid, $sectionnum, $beforemod = null) {
1486 global $DB, $COURSE;
1487 if (is_object($beforemod)) {
1488 $beforemod = $beforemod->id;
1490 if (is_object($courseorid)) {
1491 $courseid = $courseorid->id;
1492 } else {
1493 $courseid = $courseorid;
1495 // Do not try to use modinfo here, there is no guarantee it is valid!
1496 $section = $DB->get_record('course_sections',
1497 array('course' => $courseid, 'section' => $sectionnum), '*', IGNORE_MISSING);
1498 if (!$section) {
1499 // This function call requires modinfo.
1500 course_create_sections_if_missing($courseorid, $sectionnum);
1501 $section = $DB->get_record('course_sections',
1502 array('course' => $courseid, 'section' => $sectionnum), '*', MUST_EXIST);
1505 $modarray = explode(",", trim($section->sequence));
1506 if (empty($section->sequence)) {
1507 $newsequence = "$cmid";
1508 } else if ($beforemod && ($key = array_keys($modarray, $beforemod))) {
1509 $insertarray = array($cmid, $beforemod);
1510 array_splice($modarray, $key[0], 1, $insertarray);
1511 $newsequence = implode(",", $modarray);
1512 } else {
1513 $newsequence = "$section->sequence,$cmid";
1515 $DB->set_field("course_sections", "sequence", $newsequence, array("id" => $section->id));
1516 $DB->set_field('course_modules', 'section', $section->id, array('id' => $cmid));
1517 if (is_object($courseorid)) {
1518 rebuild_course_cache($courseorid->id, true);
1519 } else {
1520 rebuild_course_cache($courseorid, true);
1522 return $section->id; // Return course_sections ID that was used.
1525 function set_coursemodule_groupmode($id, $groupmode) {
1526 global $DB;
1527 $cm = $DB->get_record('course_modules', array('id' => $id), 'id,course,groupmode', MUST_EXIST);
1528 if ($cm->groupmode != $groupmode) {
1529 $DB->set_field('course_modules', 'groupmode', $groupmode, array('id' => $cm->id));
1530 rebuild_course_cache($cm->course, true);
1532 return ($cm->groupmode != $groupmode);
1535 function set_coursemodule_idnumber($id, $idnumber) {
1536 global $DB;
1537 $cm = $DB->get_record('course_modules', array('id' => $id), 'id,course,idnumber', MUST_EXIST);
1538 if ($cm->idnumber != $idnumber) {
1539 $DB->set_field('course_modules', 'idnumber', $idnumber, array('id' => $cm->id));
1540 rebuild_course_cache($cm->course, true);
1542 return ($cm->idnumber != $idnumber);
1546 * Set the visibility of a module and inherent properties.
1548 * From 2.4 the parameter $prevstateoverrides has been removed, the logic it triggered
1549 * has been moved to {@link set_section_visible()} which was the only place from which
1550 * the parameter was used.
1552 * @param int $id of the module
1553 * @param int $visible state of the module
1554 * @return bool false when the module was not found, true otherwise
1556 function set_coursemodule_visible($id, $visible) {
1557 global $DB, $CFG;
1558 require_once($CFG->libdir.'/gradelib.php');
1559 require_once($CFG->dirroot.'/calendar/lib.php');
1561 // Trigger developer's attention when using the previously removed argument.
1562 if (func_num_args() > 2) {
1563 debugging('Wrong number of arguments passed to set_coursemodule_visible(), $prevstateoverrides
1564 has been removed.', DEBUG_DEVELOPER);
1567 if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
1568 return false;
1571 // Create events and propagate visibility to associated grade items if the value has changed.
1572 // Only do this if it's changed to avoid accidently overwriting manual showing/hiding of student grades.
1573 if ($cm->visible == $visible) {
1574 return true;
1577 if (!$modulename = $DB->get_field('modules', 'name', array('id'=>$cm->module))) {
1578 return false;
1580 if ($events = $DB->get_records('event', array('instance'=>$cm->instance, 'modulename'=>$modulename))) {
1581 foreach($events as $event) {
1582 if ($visible) {
1583 $event = new calendar_event($event);
1584 $event->toggle_visibility(true);
1585 } else {
1586 $event = new calendar_event($event);
1587 $event->toggle_visibility(false);
1592 // Hide the associated grade items so the teacher doesn't also have to go to the gradebook and hide them there.
1593 $grade_items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course));
1594 if ($grade_items) {
1595 foreach ($grade_items as $grade_item) {
1596 $grade_item->set_hidden(!$visible);
1600 // Updating visible and visibleold to keep them in sync. Only changing a section visibility will
1601 // affect visibleold to allow for an original visibility restore. See set_section_visible().
1602 $cminfo = new stdClass();
1603 $cminfo->id = $id;
1604 $cminfo->visible = $visible;
1605 $cminfo->visibleold = $visible;
1606 $DB->update_record('course_modules', $cminfo);
1608 rebuild_course_cache($cm->course, true);
1609 return true;
1613 * This function will handles the whole deletion process of a module. This includes calling
1614 * the modules delete_instance function, deleting files, events, grades, conditional data,
1615 * the data in the course_module and course_sections table and adding a module deletion
1616 * event to the DB.
1618 * @param int $cmid the course module id
1619 * @since 2.5
1621 function course_delete_module($cmid) {
1622 global $CFG, $DB, $USER;
1624 require_once($CFG->libdir.'/gradelib.php');
1625 require_once($CFG->dirroot.'/blog/lib.php');
1626 require_once($CFG->dirroot.'/calendar/lib.php');
1628 // Get the course module.
1629 if (!$cm = $DB->get_record('course_modules', array('id' => $cmid))) {
1630 return true;
1633 // Get the module context.
1634 $modcontext = context_module::instance($cm->id);
1636 // Get the course module name.
1637 $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST);
1639 // Get the file location of the delete_instance function for this module.
1640 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
1642 // Include the file required to call the delete_instance function for this module.
1643 if (file_exists($modlib)) {
1644 require_once($modlib);
1645 } else {
1646 throw new moodle_exception('cannotdeletemodulemissinglib', '', '', null,
1647 "Cannot delete this module as the file mod/$modulename/lib.php is missing.");
1650 $deleteinstancefunction = $modulename . '_delete_instance';
1652 // Ensure the delete_instance function exists for this module.
1653 if (!function_exists($deleteinstancefunction)) {
1654 throw new moodle_exception('cannotdeletemodulemissingfunc', '', '', null,
1655 "Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/$modulename/lib.php.");
1658 // Call the delete_instance function, if it returns false throw an exception.
1659 if (!$deleteinstancefunction($cm->instance)) {
1660 throw new moodle_exception('cannotdeletemoduleinstance', '', '', null,
1661 "Cannot delete the module $modulename (instance).");
1664 // Remove all module files in case modules forget to do that.
1665 $fs = get_file_storage();
1666 $fs->delete_area_files($modcontext->id);
1668 // Delete events from calendar.
1669 if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
1670 foreach($events as $event) {
1671 $calendarevent = calendar_event::load($event->id);
1672 $calendarevent->delete();
1676 // Delete grade items, outcome items and grades attached to modules.
1677 if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename,
1678 'iteminstance' => $cm->instance, 'courseid' => $cm->course))) {
1679 foreach ($grade_items as $grade_item) {
1680 $grade_item->delete('moddelete');
1684 // Delete completion and availability data; it is better to do this even if the
1685 // features are not turned on, in case they were turned on previously (these will be
1686 // very quick on an empty table).
1687 $DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
1688 $DB->delete_records('course_modules_availability', array('coursemoduleid'=> $cm->id));
1689 $DB->delete_records('course_modules_avail_fields', array('coursemoduleid' => $cm->id));
1690 $DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id,
1691 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
1693 // Delete the context.
1694 context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
1696 // Delete the module from the course_modules table.
1697 $DB->delete_records('course_modules', array('id' => $cm->id));
1699 // Delete module from that section.
1700 if (!delete_mod_from_section($cm->id, $cm->section)) {
1701 throw new moodle_exception('cannotdeletemodulefromsection', '', '', null,
1702 "Cannot delete the module $modulename (instance) from section.");
1705 // Trigger a mod_deleted event with information about this module.
1706 $eventdata = new stdClass();
1707 $eventdata->modulename = $modulename;
1708 $eventdata->cmid = $cm->id;
1709 $eventdata->courseid = $cm->course;
1710 $eventdata->userid = $USER->id;
1711 events_trigger('mod_deleted', $eventdata);
1713 add_to_log($cm->course, 'course', "delete mod",
1714 "view.php?id=$cm->course",
1715 "$modulename $cm->instance", $cm->id);
1717 rebuild_course_cache($cm->course, true);
1720 function delete_mod_from_section($modid, $sectionid) {
1721 global $DB;
1723 if ($section = $DB->get_record("course_sections", array("id"=>$sectionid)) ) {
1725 $modarray = explode(",", $section->sequence);
1727 if ($key = array_keys ($modarray, $modid)) {
1728 array_splice($modarray, $key[0], 1);
1729 $newsequence = implode(",", $modarray);
1730 $DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));
1731 rebuild_course_cache($section->course, true);
1732 return true;
1733 } else {
1734 return false;
1738 return false;
1742 * Moves a section within a course, from a position to another.
1743 * Be very careful: $section and $destination refer to section number,
1744 * not id!.
1746 * @param object $course
1747 * @param int $section Section number (not id!!!)
1748 * @param int $destination
1749 * @return boolean Result
1751 function move_section_to($course, $section, $destination) {
1752 /// Moves a whole course section up and down within the course
1753 global $USER, $DB;
1755 if (!$destination && $destination != 0) {
1756 return true;
1759 // compartibility with course formats using field 'numsections'
1760 $courseformatoptions = course_get_format($course)->get_format_options();
1761 if ((array_key_exists('numsections', $courseformatoptions) &&
1762 ($destination > $courseformatoptions['numsections'])) || ($destination < 1)) {
1763 return false;
1766 // Get all sections for this course and re-order them (2 of them should now share the same section number)
1767 if (!$sections = $DB->get_records_menu('course_sections', array('course' => $course->id),
1768 'section ASC, id ASC', 'id, section')) {
1769 return false;
1772 $movedsections = reorder_sections($sections, $section, $destination);
1774 // Update all sections. Do this in 2 steps to avoid breaking database
1775 // uniqueness constraint
1776 $transaction = $DB->start_delegated_transaction();
1777 foreach ($movedsections as $id => $position) {
1778 if ($sections[$id] !== $position) {
1779 $DB->set_field('course_sections', 'section', -$position, array('id' => $id));
1782 foreach ($movedsections as $id => $position) {
1783 if ($sections[$id] !== $position) {
1784 $DB->set_field('course_sections', 'section', $position, array('id' => $id));
1788 // If we move the highlighted section itself, then just highlight the destination.
1789 // Adjust the higlighted section location if we move something over it either direction.
1790 if ($section == $course->marker) {
1791 course_set_marker($course->id, $destination);
1792 } elseif ($section > $course->marker && $course->marker >= $destination) {
1793 course_set_marker($course->id, $course->marker+1);
1794 } elseif ($section < $course->marker && $course->marker <= $destination) {
1795 course_set_marker($course->id, $course->marker-1);
1798 $transaction->allow_commit();
1799 rebuild_course_cache($course->id, true);
1800 return true;
1804 * Reordering algorithm for course sections. Given an array of section->section indexed by section->id,
1805 * an original position number and a target position number, rebuilds the array so that the
1806 * move is made without any duplication of section positions.
1807 * Note: The target_position is the position AFTER WHICH the moved section will be inserted. If you want to
1808 * insert a section before the first one, you must give 0 as the target (section 0 can never be moved).
1810 * @param array $sections
1811 * @param int $origin_position
1812 * @param int $target_position
1813 * @return array
1815 function reorder_sections($sections, $origin_position, $target_position) {
1816 if (!is_array($sections)) {
1817 return false;
1820 // We can't move section position 0
1821 if ($origin_position < 1) {
1822 echo "We can't move section position 0";
1823 return false;
1826 // Locate origin section in sections array
1827 if (!$origin_key = array_search($origin_position, $sections)) {
1828 echo "searched position not in sections array";
1829 return false; // searched position not in sections array
1832 // Extract origin section
1833 $origin_section = $sections[$origin_key];
1834 unset($sections[$origin_key]);
1836 // Find offset of target position (stupid PHP's array_splice requires offset instead of key index!)
1837 $found = false;
1838 $append_array = array();
1839 foreach ($sections as $id => $position) {
1840 if ($found) {
1841 $append_array[$id] = $position;
1842 unset($sections[$id]);
1844 if ($position == $target_position) {
1845 if ($target_position < $origin_position) {
1846 $append_array[$id] = $position;
1847 unset($sections[$id]);
1849 $found = true;
1853 // Append moved section
1854 $sections[$origin_key] = $origin_section;
1856 // Append rest of array (if applicable)
1857 if (!empty($append_array)) {
1858 foreach ($append_array as $id => $position) {
1859 $sections[$id] = $position;
1863 // Renumber positions
1864 $position = 0;
1865 foreach ($sections as $id => $p) {
1866 $sections[$id] = $position;
1867 $position++;
1870 return $sections;
1875 * Move the module object $mod to the specified $section
1876 * If $beforemod exists then that is the module
1877 * before which $modid should be inserted
1879 * @param stdClass|cm_info $mod
1880 * @param stdClass|section_info $section
1881 * @param int|stdClass $beforemod id or object with field id corresponding to the module
1882 * before which the module needs to be included. Null for inserting in the
1883 * end of the section
1884 * @return int new value for module visibility (0 or 1)
1886 function moveto_module($mod, $section, $beforemod=NULL) {
1887 global $OUTPUT, $DB;
1889 // Current module visibility state - return value of this function.
1890 $modvisible = $mod->visible;
1892 // Remove original module from original section.
1893 if (! delete_mod_from_section($mod->id, $mod->section)) {
1894 echo $OUTPUT->notification("Could not delete module from existing section");
1897 // If moving to a hidden section then hide module.
1898 if ($mod->section != $section->id) {
1899 if (!$section->visible && $mod->visible) {
1900 // Module was visible but must become hidden after moving to hidden section.
1901 $modvisible = 0;
1902 set_coursemodule_visible($mod->id, 0);
1903 // Set visibleold to 1 so module will be visible when section is made visible.
1904 $DB->set_field('course_modules', 'visibleold', 1, array('id' => $mod->id));
1906 if ($section->visible && !$mod->visible) {
1907 // Hidden module was moved to the visible section, restore the module visibility from visibleold.
1908 set_coursemodule_visible($mod->id, $mod->visibleold);
1909 $modvisible = $mod->visibleold;
1913 // Add the module into the new section.
1914 course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod);
1915 return $modvisible;
1919 * Returns the list of all editing actions that current user can perform on the module
1921 * @param cm_info $mod The module to produce editing buttons for
1922 * @param int $indent The current indenting (default -1 means no move left-right actions)
1923 * @param int $sr The section to link back to (used for creating the links)
1924 * @return array array of action_link or pix_icon objects
1926 function course_get_cm_edit_actions(cm_info $mod, $indent = -1, $sr = null) {
1927 global $COURSE, $SITE;
1929 static $str;
1931 $coursecontext = context_course::instance($mod->course);
1932 $modcontext = context_module::instance($mod->id);
1934 $editcaps = array('moodle/course:manageactivities', 'moodle/course:activityvisibility', 'moodle/role:assign');
1935 $dupecaps = array('moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport');
1937 // No permission to edit anything.
1938 if (!has_any_capability($editcaps, $modcontext) and !has_all_capabilities($dupecaps, $coursecontext)) {
1939 return array();
1942 $hasmanageactivities = has_capability('moodle/course:manageactivities', $modcontext);
1944 if (!isset($str)) {
1945 $str = get_strings(array('delete', 'move', 'moveright', 'moveleft',
1946 'update', 'duplicate', 'hide', 'show', 'edittitle'), 'moodle');
1947 $str->assign = get_string('assignroles', 'role');
1948 $str->groupsnone = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsnone"));
1949 $str->groupsseparate = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsseparate"));
1950 $str->groupsvisible = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsvisible"));
1951 $str->forcedgroupsnone = get_string('forcedmodeinbrackets', 'moodle', get_string("groupsnone"));
1952 $str->forcedgroupsseparate = get_string('forcedmodeinbrackets', 'moodle', get_string("groupsseparate"));
1953 $str->forcedgroupsvisible = get_string('forcedmodeinbrackets', 'moodle', get_string("groupsvisible"));
1956 $baseurl = new moodle_url('/course/mod.php', array('sesskey' => sesskey()));
1958 if ($sr !== null) {
1959 $baseurl->param('sr', $sr);
1961 $actions = array();
1963 // AJAX edit title.
1964 if ($mod->has_view() && $hasmanageactivities &&
1965 (($mod->course == $COURSE->id && course_ajax_enabled($COURSE)) ||
1966 ($mod->course == SITEID && course_ajax_enabled($SITE)))) {
1967 // we will not display link if we are on some other-course page (where we should not see this module anyway)
1968 $actions['title'] = new action_menu_link_secondary(
1969 new moodle_url($baseurl, array('update' => $mod->id)),
1970 new pix_icon('t/editstring', $str->edittitle, 'moodle', array('class' => 'iconsmall visibleifjs', 'title' => '')),
1971 $str->edittitle,
1972 array('class' => 'editing_title', 'data-action' => 'edittitle')
1976 // Indent.
1977 if ($hasmanageactivities) {
1978 if (right_to_left()) { // Exchange arrows on RTL
1979 $rightarrow = 't/left';
1980 $leftarrow = 't/right';
1981 } else {
1982 $rightarrow = 't/right';
1983 $leftarrow = 't/left';
1986 $hiddenclass = 'hidden';
1987 if ($indent > 0) {
1988 $hiddenclass = '';
1990 $actions['moveleft'] = new action_menu_link_secondary(
1991 new moodle_url($baseurl, array('id' => $mod->id, 'indent' => '-1')),
1992 new pix_icon($leftarrow, $str->moveleft, 'moodle', array('class' => 'iconsmall', 'title' => '')),
1993 $str->moveleft,
1994 array('class' => 'editing_moveleft ' . $hiddenclass, 'data-action' => 'moveleft')
1996 $hiddenclass = 'hidden';
1997 if ($indent >= 0) {
1998 $hiddenclass = '';
2000 $actions['moveright'] = new action_menu_link_secondary(
2001 new moodle_url($baseurl, array('id' => $mod->id, 'indent' => '1')),
2002 new pix_icon($rightarrow, $str->moveright, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2003 $str->moveright,
2004 array('class' => 'editing_moveright ' . $hiddenclass, 'data-action' => 'moveright')
2008 // Move.
2009 if ($hasmanageactivities) {
2010 $actions['move'] = new action_menu_link_primary(
2011 new moodle_url($baseurl, array('copy' => $mod->id)),
2012 new pix_icon('t/move', $str->move, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2013 $str->move,
2014 array('class' => 'editing_move status', 'data-action' => 'move')
2018 // Update.
2019 if ($hasmanageactivities) {
2020 $actions['update'] = new action_menu_link_secondary(
2021 new moodle_url($baseurl, array('update' => $mod->id)),
2022 new pix_icon('t/edit', $str->update, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2023 $str->update,
2024 array('class' => 'editing_update', 'data-action' => 'update')
2028 // Duplicate (require both target import caps to be able to duplicate and backup2 support, see modduplicate.php)
2029 // Note that restoring on front page is never allowed.
2030 if ($mod->course != SITEID && has_all_capabilities($dupecaps, $coursecontext) &&
2031 plugin_supports('mod', $mod->modname, FEATURE_BACKUP_MOODLE2)) {
2032 $actions['duplicate'] = new action_menu_link_secondary(
2033 new moodle_url($baseurl, array('duplicate' => $mod->id)),
2034 new pix_icon('t/copy', $str->duplicate, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2035 $str->duplicate,
2036 array('class' => 'editing_duplicate', 'data-action' => 'duplicate')
2040 // Delete.
2041 if ($hasmanageactivities) {
2042 $actions['delete'] = new action_menu_link_secondary(
2043 new moodle_url($baseurl, array('delete' => $mod->id)),
2044 new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2045 $str->delete,
2046 array('class' => 'editing_delete', 'data-action' => 'delete')
2050 // Hide/Show.
2051 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
2052 if ($mod->visible) {
2053 $actions['hide'] = new action_menu_link_primary(
2054 new moodle_url($baseurl, array('hide' => $mod->id)),
2055 new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2056 $str->hide,
2057 array('class' => 'editing_hide', 'data-action' => 'hide')
2059 } else {
2060 $actions['show'] = new action_menu_link_primary(
2061 new moodle_url($baseurl, array('show' => $mod->id)),
2062 new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2063 $str->show,
2064 array('class' => 'editing_show', 'data-action' => 'show')
2069 // Groupmode.
2070 if ($hasmanageactivities and plugin_supports('mod', $mod->modname, FEATURE_GROUPS, 0)) {
2071 if ($mod->effectivegroupmode == SEPARATEGROUPS) {
2072 $nextgroupmode = VISIBLEGROUPS;
2073 $grouptitle = $str->groupsseparate;
2074 $forcedgrouptitle = $str->forcedgroupsseparate;
2075 $actionname = 'groupsseparate';
2076 $groupimage = 't/groups';
2077 } else if ($mod->effectivegroupmode == VISIBLEGROUPS) {
2078 $nextgroupmode = NOGROUPS;
2079 $grouptitle = $str->groupsvisible;
2080 $forcedgrouptitle = $str->forcedgroupsvisible;
2081 $actionname = 'groupsvisible';
2082 $groupimage = 't/groupv';
2083 } else {
2084 $nextgroupmode = SEPARATEGROUPS;
2085 $grouptitle = $str->groupsnone;
2086 $forcedgrouptitle = $str->forcedgroupsnone;
2087 $actionname = 'groupsnone';
2088 $groupimage = 't/groupn';
2090 if (!$mod->coursegroupmodeforce) {
2091 $actions[$actionname] = new action_menu_link_primary(
2092 new moodle_url($baseurl, array('id' => $mod->id, 'groupmode' => $nextgroupmode)),
2093 new pix_icon($groupimage, $grouptitle, 'moodle', array('class' => 'iconsmall', 'title' => '')),
2094 $grouptitle,
2095 array('class' => 'editing_'. $actionname, 'data-action' => $actionname, 'data-nextgroupmode' => $nextgroupmode)
2097 } else {
2098 $actions[$actionname] = new pix_icon($groupimage, $forcedgrouptitle, 'moodle', array('class' => 'iconsmall'));
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 return $actions;
2116 * given a course object with shortname & fullname, this function will
2117 * truncate the the number of chars allowed and add ... if it was too long
2119 function course_format_name ($course,$max=100) {
2121 $context = context_course::instance($course->id);
2122 $shortname = format_string($course->shortname, true, array('context' => $context));
2123 $fullname = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
2124 $str = $shortname.': '. $fullname;
2125 if (core_text::strlen($str) <= $max) {
2126 return $str;
2128 else {
2129 return core_text::substr($str,0,$max-3).'...';
2134 * Is the user allowed to add this type of module to this course?
2135 * @param object $course the course settings. Only $course->id is used.
2136 * @param string $modname the module name. E.g. 'forum' or 'quiz'.
2137 * @return bool whether the current user is allowed to add this type of module to this course.
2139 function course_allowed_module($course, $modname) {
2140 if (is_numeric($modname)) {
2141 throw new coding_exception('Function course_allowed_module no longer
2142 supports numeric module ids. Please update your code to pass the module name.');
2145 $capability = 'mod/' . $modname . ':addinstance';
2146 if (!get_capability_info($capability)) {
2147 // Debug warning that the capability does not exist, but no more than once per page.
2148 static $warned = array();
2149 $archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
2150 if (!isset($warned[$modname]) && $archetype !== MOD_ARCHETYPE_SYSTEM) {
2151 debugging('The module ' . $modname . ' does not define the standard capability ' .
2152 $capability , DEBUG_DEVELOPER);
2153 $warned[$modname] = 1;
2156 // If the capability does not exist, the module can always be added.
2157 return true;
2160 $coursecontext = context_course::instance($course->id);
2161 return has_capability($capability, $coursecontext);
2165 * Efficiently moves many courses around while maintaining
2166 * sortorder in order.
2168 * @param array $courseids is an array of course ids
2169 * @param int $categoryid
2170 * @return bool success
2172 function move_courses($courseids, $categoryid) {
2173 global $DB;
2175 if (empty($courseids)) {
2176 // Nothing to do.
2177 return;
2180 if (!$category = $DB->get_record('course_categories', array('id' => $categoryid))) {
2181 return false;
2184 $courseids = array_reverse($courseids);
2185 $newparent = context_coursecat::instance($category->id);
2186 $i = 1;
2188 list($where, $params) = $DB->get_in_or_equal($courseids);
2189 $dbcourses = $DB->get_records_select('course', 'id ' . $where, $params);
2190 foreach ($dbcourses as $dbcourse) {
2191 $course = new stdClass();
2192 $course->id = $dbcourse->id;
2193 $course->category = $category->id;
2194 $course->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - $i++;
2195 if ($category->visible == 0) {
2196 // Hide the course when moving into hidden category, do not update the visibleold flag - we want to get
2197 // to previous state if somebody unhides the category.
2198 $course->visible = 0;
2201 $DB->update_record('course', $course);
2203 // Store the context.
2204 $context = context_course::instance($course->id);
2206 // Update the course object we are passing to the event.
2207 $dbcourse->category = $course->category;
2208 $dbcourse->sortorder = $course->sortorder;
2209 if (isset($course->visible)) {
2210 $dbcourse->visible = $course->visible;
2213 // Trigger a course updated event.
2214 $event = \core\event\course_updated::create(array(
2215 'objectid' => $course->id,
2216 'context' => $context,
2217 'other' => array('shortname' => $dbcourse->shortname,
2218 'fullname' => $dbcourse->fullname)
2220 $event->add_record_snapshot('course', $dbcourse);
2221 $event->set_legacy_logdata(array($course->id, 'course', 'move', 'edit.php?id=' . $course->id, $course->id));
2222 $event->trigger();
2224 $context->update_moved($newparent);
2226 fix_course_sortorder();
2227 cache_helper::purge_by_event('changesincourse');
2229 return true;
2233 * Returns the display name of the given section that the course prefers
2235 * Implementation of this function is provided by course format
2236 * @see format_base::get_section_name()
2238 * @param int|stdClass $courseorid The course to get the section name for (object or just course id)
2239 * @param int|stdClass $section Section object from database or just field course_sections.section
2240 * @return string Display name that the course format prefers, e.g. "Week 2"
2242 function get_section_name($courseorid, $section) {
2243 return course_get_format($courseorid)->get_section_name($section);
2247 * Tells if current course format uses sections
2249 * @param string $format Course format ID e.g. 'weeks' $course->format
2250 * @return bool
2252 function course_format_uses_sections($format) {
2253 $course = new stdClass();
2254 $course->format = $format;
2255 return course_get_format($course)->uses_sections();
2259 * Returns the information about the ajax support in the given source format
2261 * The returned object's property (boolean)capable indicates that
2262 * the course format supports Moodle course ajax features.
2263 * The property (array)testedbrowsers can be used as a parameter for {@see ajaxenabled()}.
2265 * @param string $format
2266 * @return stdClass
2268 function course_format_ajax_support($format) {
2269 $course = new stdClass();
2270 $course->format = $format;
2271 return course_get_format($course)->supports_ajax();
2275 * Can the current user delete this course?
2276 * Course creators have exception,
2277 * 1 day after the creation they can sill delete the course.
2278 * @param int $courseid
2279 * @return boolean
2281 function can_delete_course($courseid) {
2282 global $USER, $DB;
2284 $context = context_course::instance($courseid);
2286 if (has_capability('moodle/course:delete', $context)) {
2287 return true;
2290 // hack: now try to find out if creator created this course recently (1 day)
2291 if (!has_capability('moodle/course:create', $context)) {
2292 return false;
2295 $since = time() - 60*60*24;
2297 $params = array('userid'=>$USER->id, 'url'=>"view.php?id=$courseid", 'since'=>$since);
2298 $select = "module = 'course' AND action = 'new' AND userid = :userid AND url = :url AND time > :since";
2300 return $DB->record_exists_select('log', $select, $params);
2304 * Save the Your name for 'Some role' strings.
2306 * @param integer $courseid the id of this course.
2307 * @param array $data the data that came from the course settings form.
2309 function save_local_role_names($courseid, $data) {
2310 global $DB;
2311 $context = context_course::instance($courseid);
2313 foreach ($data as $fieldname => $value) {
2314 if (strpos($fieldname, 'role_') !== 0) {
2315 continue;
2317 list($ignored, $roleid) = explode('_', $fieldname);
2319 // make up our mind whether we want to delete, update or insert
2320 if (!$value) {
2321 $DB->delete_records('role_names', array('contextid' => $context->id, 'roleid' => $roleid));
2323 } else if ($rolename = $DB->get_record('role_names', array('contextid' => $context->id, 'roleid' => $roleid))) {
2324 $rolename->name = $value;
2325 $DB->update_record('role_names', $rolename);
2327 } else {
2328 $rolename = new stdClass;
2329 $rolename->contextid = $context->id;
2330 $rolename->roleid = $roleid;
2331 $rolename->name = $value;
2332 $DB->insert_record('role_names', $rolename);
2338 * Returns options to use in course overviewfiles filemanager
2340 * @param null|stdClass|course_in_list|int $course either object that has 'id' property or just the course id;
2341 * may be empty if course does not exist yet (course create form)
2342 * @return array|null array of options such as maxfiles, maxbytes, accepted_types, etc.
2343 * or null if overviewfiles are disabled
2345 function course_overviewfiles_options($course) {
2346 global $CFG;
2347 if (empty($CFG->courseoverviewfileslimit)) {
2348 return null;
2350 $accepted_types = preg_split('/\s*,\s*/', trim($CFG->courseoverviewfilesext), -1, PREG_SPLIT_NO_EMPTY);
2351 if (in_array('*', $accepted_types) || empty($accepted_types)) {
2352 $accepted_types = '*';
2353 } else {
2354 // Since config for $CFG->courseoverviewfilesext is a text box, human factor must be considered.
2355 // Make sure extensions are prefixed with dot unless they are valid typegroups
2356 foreach ($accepted_types as $i => $type) {
2357 if (substr($type, 0, 1) !== '.') {
2358 require_once($CFG->libdir. '/filelib.php');
2359 if (!count(file_get_typegroup('extension', $type))) {
2360 // It does not start with dot and is not a valid typegroup, this is most likely extension.
2361 $accepted_types[$i] = '.'. $type;
2362 $corrected = true;
2366 if (!empty($corrected)) {
2367 set_config('courseoverviewfilesext', join(',', $accepted_types));
2370 $options = array(
2371 'maxfiles' => $CFG->courseoverviewfileslimit,
2372 'maxbytes' => $CFG->maxbytes,
2373 'subdirs' => 0,
2374 'accepted_types' => $accepted_types
2376 if (!empty($course->id)) {
2377 $options['context'] = context_course::instance($course->id);
2378 } else if (is_int($course) && $course > 0) {
2379 $options['context'] = context_course::instance($course);
2381 return $options;
2385 * Create a course and either return a $course object
2387 * Please note this functions does not verify any access control,
2388 * the calling code is responsible for all validation (usually it is the form definition).
2390 * @param array $editoroptions course description editor options
2391 * @param object $data - all the data needed for an entry in the 'course' table
2392 * @return object new course instance
2394 function create_course($data, $editoroptions = NULL) {
2395 global $DB;
2397 //check the categoryid - must be given for all new courses
2398 $category = $DB->get_record('course_categories', array('id'=>$data->category), '*', MUST_EXIST);
2400 // Check if the shortname already exists.
2401 if (!empty($data->shortname)) {
2402 if ($DB->record_exists('course', array('shortname' => $data->shortname))) {
2403 throw new moodle_exception('shortnametaken', '', '', $data->shortname);
2407 // Check if the idnumber already exists.
2408 if (!empty($data->idnumber)) {
2409 if ($DB->record_exists('course', array('idnumber' => $data->idnumber))) {
2410 throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
2414 $data->timecreated = time();
2415 $data->timemodified = $data->timecreated;
2417 // place at beginning of any category
2418 $data->sortorder = 0;
2420 if ($editoroptions) {
2421 // summary text is updated later, we need context to store the files first
2422 $data->summary = '';
2423 $data->summary_format = FORMAT_HTML;
2426 if (!isset($data->visible)) {
2427 // data not from form, add missing visibility info
2428 $data->visible = $category->visible;
2430 $data->visibleold = $data->visible;
2432 $newcourseid = $DB->insert_record('course', $data);
2433 $context = context_course::instance($newcourseid, MUST_EXIST);
2435 if ($editoroptions) {
2436 // Save the files used in the summary editor and store
2437 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
2438 $DB->set_field('course', 'summary', $data->summary, array('id'=>$newcourseid));
2439 $DB->set_field('course', 'summaryformat', $data->summary_format, array('id'=>$newcourseid));
2441 if ($overviewfilesoptions = course_overviewfiles_options($newcourseid)) {
2442 // Save the course overviewfiles
2443 $data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0);
2446 // update course format options
2447 course_get_format($newcourseid)->update_course_format_options($data);
2449 $course = course_get_format($newcourseid)->get_course();
2451 // Setup the blocks
2452 blocks_add_default_course_blocks($course);
2454 // Create a default section.
2455 course_create_sections_if_missing($course, 0);
2457 fix_course_sortorder();
2458 // purge appropriate caches in case fix_course_sortorder() did not change anything
2459 cache_helper::purge_by_event('changesincourse');
2461 // new context created - better mark it as dirty
2462 $context->mark_dirty();
2464 // Save any custom role names.
2465 save_local_role_names($course->id, (array)$data);
2467 // set up enrolments
2468 enrol_course_updated(true, $course, $data);
2470 // Trigger a course created event.
2471 $event = \core\event\course_created::create(array(
2472 'objectid' => $course->id,
2473 'context' => context_course::instance($course->id),
2474 'other' => array('shortname' => $course->shortname,
2475 'fullname' => $course->fullname)
2477 $event->add_record_snapshot('course', $course);
2478 $event->trigger();
2480 return $course;
2484 * Update a course.
2486 * Please note this functions does not verify any access control,
2487 * the calling code is responsible for all validation (usually it is the form definition).
2489 * @param object $data - all the data needed for an entry in the 'course' table
2490 * @param array $editoroptions course description editor options
2491 * @return void
2493 function update_course($data, $editoroptions = NULL) {
2494 global $DB;
2496 $data->timemodified = time();
2498 $oldcourse = course_get_format($data->id)->get_course();
2499 $context = context_course::instance($oldcourse->id);
2501 if ($editoroptions) {
2502 $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
2504 if ($overviewfilesoptions = course_overviewfiles_options($data->id)) {
2505 $data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0);
2508 // Check we don't have a duplicate shortname.
2509 if (!empty($data->shortname) && $oldcourse->shortname != $data->shortname) {
2510 if ($DB->record_exists('course', array('shortname' => $data->shortname))) {
2511 throw new moodle_exception('shortnametaken', '', '', $data->shortname);
2515 // Check we don't have a duplicate idnumber.
2516 if (!empty($data->idnumber) && $oldcourse->idnumber != $data->idnumber) {
2517 if ($DB->record_exists('course', array('idnumber' => $data->idnumber))) {
2518 throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
2522 if (!isset($data->category) or empty($data->category)) {
2523 // prevent nulls and 0 in category field
2524 unset($data->category);
2526 $changesincoursecat = $movecat = (isset($data->category) and $oldcourse->category != $data->category);
2528 if (!isset($data->visible)) {
2529 // data not from form, add missing visibility info
2530 $data->visible = $oldcourse->visible;
2533 if ($data->visible != $oldcourse->visible) {
2534 // reset the visibleold flag when manually hiding/unhiding course
2535 $data->visibleold = $data->visible;
2536 $changesincoursecat = true;
2537 } else {
2538 if ($movecat) {
2539 $newcategory = $DB->get_record('course_categories', array('id'=>$data->category));
2540 if (empty($newcategory->visible)) {
2541 // make sure when moving into hidden category the course is hidden automatically
2542 $data->visible = 0;
2547 // Update with the new data
2548 $DB->update_record('course', $data);
2549 // make sure the modinfo cache is reset
2550 rebuild_course_cache($data->id);
2552 // update course format options with full course data
2553 course_get_format($data->id)->update_course_format_options($data, $oldcourse);
2555 $course = $DB->get_record('course', array('id'=>$data->id));
2557 if ($movecat) {
2558 $newparent = context_coursecat::instance($course->category);
2559 $context->update_moved($newparent);
2562 if ($movecat || (isset($data->sortorder) && $oldcourse->sortorder != $data->sortorder)) {
2563 fix_course_sortorder();
2566 // purge appropriate caches in case fix_course_sortorder() did not change anything
2567 cache_helper::purge_by_event('changesincourse');
2568 if ($changesincoursecat) {
2569 cache_helper::purge_by_event('changesincoursecat');
2572 // Test for and remove blocks which aren't appropriate anymore
2573 blocks_remove_inappropriate($course);
2575 // Save any custom role names.
2576 save_local_role_names($course->id, $data);
2578 // update enrol settings
2579 enrol_course_updated(false, $course, $data);
2581 // Trigger a course updated event.
2582 $event = \core\event\course_updated::create(array(
2583 'objectid' => $course->id,
2584 'context' => $context,
2585 'other' => array('shortname' => $course->shortname,
2586 'fullname' => $course->fullname)
2588 $event->add_record_snapshot('course', $course);
2589 $event->set_legacy_logdata(array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id));
2590 $event->trigger();
2592 if ($oldcourse->format !== $course->format) {
2593 // Remove all options stored for the previous format
2594 // We assume that new course format migrated everything it needed watching trigger
2595 // 'course_updated' and in method format_XXX::update_course_format_options()
2596 $DB->delete_records('course_format_options',
2597 array('courseid' => $course->id, 'format' => $oldcourse->format));
2602 * Average number of participants
2603 * @return integer
2605 function average_number_of_participants() {
2606 global $DB, $SITE;
2608 //count total of enrolments for visible course (except front page)
2609 $sql = 'SELECT COUNT(*) FROM (
2610 SELECT DISTINCT ue.userid, e.courseid
2611 FROM {user_enrolments} ue, {enrol} e, {course} c
2612 WHERE ue.enrolid = e.id
2613 AND e.courseid <> :siteid
2614 AND c.id = e.courseid
2615 AND c.visible = 1) total';
2616 $params = array('siteid' => $SITE->id);
2617 $enrolmenttotal = $DB->count_records_sql($sql, $params);
2620 //count total of visible courses (minus front page)
2621 $coursetotal = $DB->count_records('course', array('visible' => 1));
2622 $coursetotal = $coursetotal - 1 ;
2624 //average of enrolment
2625 if (empty($coursetotal)) {
2626 $participantaverage = 0;
2627 } else {
2628 $participantaverage = $enrolmenttotal / $coursetotal;
2631 return $participantaverage;
2635 * Average number of course modules
2636 * @return integer
2638 function average_number_of_courses_modules() {
2639 global $DB, $SITE;
2641 //count total of visible course module (except front page)
2642 $sql = 'SELECT COUNT(*) FROM (
2643 SELECT cm.course, cm.module
2644 FROM {course} c, {course_modules} cm
2645 WHERE c.id = cm.course
2646 AND c.id <> :siteid
2647 AND cm.visible = 1
2648 AND c.visible = 1) total';
2649 $params = array('siteid' => $SITE->id);
2650 $moduletotal = $DB->count_records_sql($sql, $params);
2653 //count total of visible courses (minus front page)
2654 $coursetotal = $DB->count_records('course', array('visible' => 1));
2655 $coursetotal = $coursetotal - 1 ;
2657 //average of course module
2658 if (empty($coursetotal)) {
2659 $coursemoduleaverage = 0;
2660 } else {
2661 $coursemoduleaverage = $moduletotal / $coursetotal;
2664 return $coursemoduleaverage;
2668 * This class pertains to course requests and contains methods associated with
2669 * create, approving, and removing course requests.
2671 * Please note we do not allow embedded images here because there is no context
2672 * to store them with proper access control.
2674 * @copyright 2009 Sam Hemelryk
2675 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2676 * @since Moodle 2.0
2678 * @property-read int $id
2679 * @property-read string $fullname
2680 * @property-read string $shortname
2681 * @property-read string $summary
2682 * @property-read int $summaryformat
2683 * @property-read int $summarytrust
2684 * @property-read string $reason
2685 * @property-read int $requester
2687 class course_request {
2690 * This is the stdClass that stores the properties for the course request
2691 * and is externally accessed through the __get magic method
2692 * @var stdClass
2694 protected $properties;
2697 * An array of options for the summary editor used by course request forms.
2698 * This is initially set by {@link summary_editor_options()}
2699 * @var array
2700 * @static
2702 protected static $summaryeditoroptions;
2705 * Static function to prepare the summary editor for working with a course
2706 * request.
2708 * @static
2709 * @param null|stdClass $data Optional, an object containing the default values
2710 * for the form, these may be modified when preparing the
2711 * editor so this should be called before creating the form
2712 * @return stdClass An object that can be used to set the default values for
2713 * an mforms form
2715 public static function prepare($data=null) {
2716 if ($data === null) {
2717 $data = new stdClass;
2719 $data = file_prepare_standard_editor($data, 'summary', self::summary_editor_options());
2720 return $data;
2724 * Static function to create a new course request when passed an array of properties
2725 * for it.
2727 * This function also handles saving any files that may have been used in the editor
2729 * @static
2730 * @param stdClass $data
2731 * @return course_request The newly created course request
2733 public static function create($data) {
2734 global $USER, $DB, $CFG;
2735 $data->requester = $USER->id;
2737 // Setting the default category if none set.
2738 if (empty($data->category) || empty($CFG->requestcategoryselection)) {
2739 $data->category = $CFG->defaultrequestcategory;
2742 // Summary is a required field so copy the text over
2743 $data->summary = $data->summary_editor['text'];
2744 $data->summaryformat = $data->summary_editor['format'];
2746 $data->id = $DB->insert_record('course_request', $data);
2748 // Create a new course_request object and return it
2749 $request = new course_request($data);
2751 // Notify the admin if required.
2752 if ($users = get_users_from_config($CFG->courserequestnotify, 'moodle/site:approvecourse')) {
2754 $a = new stdClass;
2755 $a->link = "$CFG->wwwroot/course/pending.php";
2756 $a->user = fullname($USER);
2757 $subject = get_string('courserequest');
2758 $message = get_string('courserequestnotifyemail', 'admin', $a);
2759 foreach ($users as $user) {
2760 $request->notify($user, $USER, 'courserequested', $subject, $message);
2764 return $request;
2768 * Returns an array of options to use with a summary editor
2770 * @uses course_request::$summaryeditoroptions
2771 * @return array An array of options to use with the editor
2773 public static function summary_editor_options() {
2774 global $CFG;
2775 if (self::$summaryeditoroptions === null) {
2776 self::$summaryeditoroptions = array('maxfiles' => 0, 'maxbytes'=>0);
2778 return self::$summaryeditoroptions;
2782 * Loads the properties for this course request object. Id is required and if
2783 * only id is provided then we load the rest of the properties from the database
2785 * @param stdClass|int $properties Either an object containing properties
2786 * or the course_request id to load
2788 public function __construct($properties) {
2789 global $DB;
2790 if (empty($properties->id)) {
2791 if (empty($properties)) {
2792 throw new coding_exception('You must provide a course request id when creating a course_request object');
2794 $id = $properties;
2795 $properties = new stdClass;
2796 $properties->id = (int)$id;
2797 unset($id);
2799 if (empty($properties->requester)) {
2800 if (!($this->properties = $DB->get_record('course_request', array('id' => $properties->id)))) {
2801 print_error('unknowncourserequest');
2803 } else {
2804 $this->properties = $properties;
2806 $this->properties->collision = null;
2810 * Returns the requested property
2812 * @param string $key
2813 * @return mixed
2815 public function __get($key) {
2816 return $this->properties->$key;
2820 * Override this to ensure empty($request->blah) calls return a reliable answer...
2822 * This is required because we define the __get method
2824 * @param mixed $key
2825 * @return bool True is it not empty, false otherwise
2827 public function __isset($key) {
2828 return (!empty($this->properties->$key));
2832 * Returns the user who requested this course
2834 * Uses a static var to cache the results and cut down the number of db queries
2836 * @staticvar array $requesters An array of cached users
2837 * @return stdClass The user who requested the course
2839 public function get_requester() {
2840 global $DB;
2841 static $requesters= array();
2842 if (!array_key_exists($this->properties->requester, $requesters)) {
2843 $requesters[$this->properties->requester] = $DB->get_record('user', array('id'=>$this->properties->requester));
2845 return $requesters[$this->properties->requester];
2849 * Checks that the shortname used by the course does not conflict with any other
2850 * courses that exist
2852 * @param string|null $shortnamemark The string to append to the requests shortname
2853 * should a conflict be found
2854 * @return bool true is there is a conflict, false otherwise
2856 public function check_shortname_collision($shortnamemark = '[*]') {
2857 global $DB;
2859 if ($this->properties->collision !== null) {
2860 return $this->properties->collision;
2863 if (empty($this->properties->shortname)) {
2864 debugging('Attempting to check a course request shortname before it has been set', DEBUG_DEVELOPER);
2865 $this->properties->collision = false;
2866 } else if ($DB->record_exists('course', array('shortname' => $this->properties->shortname))) {
2867 if (!empty($shortnamemark)) {
2868 $this->properties->shortname .= ' '.$shortnamemark;
2870 $this->properties->collision = true;
2871 } else {
2872 $this->properties->collision = false;
2874 return $this->properties->collision;
2878 * Returns the category where this course request should be created
2880 * Note that we don't check here that user has a capability to view
2881 * hidden categories if he has capabilities 'moodle/site:approvecourse' and
2882 * 'moodle/course:changecategory'
2884 * @return coursecat
2886 public function get_category() {
2887 global $CFG;
2888 require_once($CFG->libdir.'/coursecatlib.php');
2889 // If the category is not set, if the current user does not have the rights to change the category, or if the
2890 // category does not exist, we set the default category to the course to be approved.
2891 // The system level is used because the capability moodle/site:approvecourse is based on a system level.
2892 if (empty($this->properties->category) || !has_capability('moodle/course:changecategory', context_system::instance()) ||
2893 (!$category = coursecat::get($this->properties->category, IGNORE_MISSING, true))) {
2894 $category = coursecat::get($CFG->defaultrequestcategory, IGNORE_MISSING, true);
2896 if (!$category) {
2897 $category = coursecat::get_default();
2899 return $category;
2903 * This function approves the request turning it into a course
2905 * This function converts the course request into a course, at the same time
2906 * transferring any files used in the summary to the new course and then removing
2907 * the course request and the files associated with it.
2909 * @return int The id of the course that was created from this request
2911 public function approve() {
2912 global $CFG, $DB, $USER;
2914 $user = $DB->get_record('user', array('id' => $this->properties->requester, 'deleted'=>0), '*', MUST_EXIST);
2916 $courseconfig = get_config('moodlecourse');
2918 // Transfer appropriate settings
2919 $data = clone($this->properties);
2920 unset($data->id);
2921 unset($data->reason);
2922 unset($data->requester);
2924 // Set category
2925 $category = $this->get_category();
2926 $data->category = $category->id;
2927 // Set misc settings
2928 $data->requested = 1;
2930 // Apply course default settings
2931 $data->format = $courseconfig->format;
2932 $data->newsitems = $courseconfig->newsitems;
2933 $data->showgrades = $courseconfig->showgrades;
2934 $data->showreports = $courseconfig->showreports;
2935 $data->maxbytes = $courseconfig->maxbytes;
2936 $data->groupmode = $courseconfig->groupmode;
2937 $data->groupmodeforce = $courseconfig->groupmodeforce;
2938 $data->visible = $courseconfig->visible;
2939 $data->visibleold = $data->visible;
2940 $data->lang = $courseconfig->lang;
2942 $course = create_course($data);
2943 $context = context_course::instance($course->id, MUST_EXIST);
2945 // add enrol instances
2946 if (!$DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) {
2947 if ($manual = enrol_get_plugin('manual')) {
2948 $manual->add_default_instance($course);
2952 // enrol the requester as teacher if necessary
2953 if (!empty($CFG->creatornewroleid) and !is_viewing($context, $user, 'moodle/role:assign') and !is_enrolled($context, $user, 'moodle/role:assign')) {
2954 enrol_try_internal_enrol($course->id, $user->id, $CFG->creatornewroleid);
2957 $this->delete();
2959 $a = new stdClass();
2960 $a->name = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
2961 $a->url = $CFG->wwwroot.'/course/view.php?id=' . $course->id;
2962 $this->notify($user, $USER, 'courserequestapproved', get_string('courseapprovedsubject'), get_string('courseapprovedemail2', 'moodle', $a));
2964 return $course->id;
2968 * Reject a course request
2970 * This function rejects a course request, emailing the requesting user the
2971 * provided notice and then removing the request from the database
2973 * @param string $notice The message to display to the user
2975 public function reject($notice) {
2976 global $USER, $DB;
2977 $user = $DB->get_record('user', array('id' => $this->properties->requester), '*', MUST_EXIST);
2978 $this->notify($user, $USER, 'courserequestrejected', get_string('courserejectsubject'), get_string('courserejectemail', 'moodle', $notice));
2979 $this->delete();
2983 * Deletes the course request and any associated files
2985 public function delete() {
2986 global $DB;
2987 $DB->delete_records('course_request', array('id' => $this->properties->id));
2991 * Send a message from one user to another using events_trigger
2993 * @param object $touser
2994 * @param object $fromuser
2995 * @param string $name
2996 * @param string $subject
2997 * @param string $message
2999 protected function notify($touser, $fromuser, $name='courserequested', $subject, $message) {
3000 $eventdata = new stdClass();
3001 $eventdata->component = 'moodle';
3002 $eventdata->name = $name;
3003 $eventdata->userfrom = $fromuser;
3004 $eventdata->userto = $touser;
3005 $eventdata->subject = $subject;
3006 $eventdata->fullmessage = $message;
3007 $eventdata->fullmessageformat = FORMAT_PLAIN;
3008 $eventdata->fullmessagehtml = '';
3009 $eventdata->smallmessage = '';
3010 $eventdata->notification = 1;
3011 message_send($eventdata);
3016 * Return a list of page types
3017 * @param string $pagetype current page type
3018 * @param context $parentcontext Block's parent context
3019 * @param context $currentcontext Current context of block
3020 * @return array array of page types
3022 function course_page_type_list($pagetype, $parentcontext, $currentcontext) {
3023 if ($pagetype === 'course-index' || $pagetype === 'course-index-category') {
3024 // For courses and categories browsing pages (/course/index.php) add option to show on ANY category page
3025 $pagetypes = array('*' => get_string('page-x', 'pagetype'),
3026 'course-index-*' => get_string('page-course-index-x', 'pagetype'),
3028 } else if ($currentcontext && (!($coursecontext = $currentcontext->get_course_context(false)) || $coursecontext->instanceid == SITEID)) {
3029 // We know for sure that despite pagetype starts with 'course-' this is not a page in course context (i.e. /course/search.php, etc.)
3030 $pagetypes = array('*' => get_string('page-x', 'pagetype'));
3031 } else {
3032 // Otherwise consider it a page inside a course even if $currentcontext is null
3033 $pagetypes = array('*' => get_string('page-x', 'pagetype'),
3034 'course-*' => get_string('page-course-x', 'pagetype'),
3035 'course-view-*' => get_string('page-course-view-x', 'pagetype')
3038 return $pagetypes;
3042 * Determine whether course ajax should be enabled for the specified course
3044 * @param stdClass $course The course to test against
3045 * @return boolean Whether course ajax is enabled or note
3047 function course_ajax_enabled($course) {
3048 global $CFG, $PAGE, $SITE;
3050 // Ajax must be enabled globally
3051 if (!$CFG->enableajax) {
3052 return false;
3055 // The user must be editing for AJAX to be included
3056 if (!$PAGE->user_is_editing()) {
3057 return false;
3060 // Check that the theme suports
3061 if (!$PAGE->theme->enablecourseajax) {
3062 return false;
3065 // Check that the course format supports ajax functionality
3066 // The site 'format' doesn't have information on course format support
3067 if ($SITE->id !== $course->id) {
3068 $courseformatajaxsupport = course_format_ajax_support($course->format);
3069 if (!$courseformatajaxsupport->capable) {
3070 return false;
3074 // All conditions have been met so course ajax should be enabled
3075 return true;
3079 * Include the relevant javascript and language strings for the resource
3080 * toolbox YUI module
3082 * @param integer $id The ID of the course being applied to
3083 * @param array $usedmodules An array containing the names of the modules in use on the page
3084 * @param array $enabledmodules An array containing the names of the enabled (visible) modules on this site
3085 * @param stdClass $config An object containing configuration parameters for ajax modules including:
3086 * * resourceurl The URL to post changes to for resource changes
3087 * * sectionurl The URL to post changes to for section changes
3088 * * pageparams Additional parameters to pass through in the post
3089 * @return bool
3091 function include_course_ajax($course, $usedmodules = array(), $enabledmodules = null, $config = null) {
3092 global $CFG, $PAGE, $SITE;
3094 // Ensure that ajax should be included
3095 if (!course_ajax_enabled($course)) {
3096 return false;
3099 if (!$config) {
3100 $config = new stdClass();
3103 // The URL to use for resource changes
3104 if (!isset($config->resourceurl)) {
3105 $config->resourceurl = '/course/rest.php';
3108 // The URL to use for section changes
3109 if (!isset($config->sectionurl)) {
3110 $config->sectionurl = '/course/rest.php';
3113 // Any additional parameters which need to be included on page submission
3114 if (!isset($config->pageparams)) {
3115 $config->pageparams = array();
3118 // Include toolboxes
3119 $PAGE->requires->yui_module('moodle-course-toolboxes',
3120 'M.course.init_resource_toolbox',
3121 array(array(
3122 'courseid' => $course->id,
3123 'ajaxurl' => $config->resourceurl,
3124 'config' => $config,
3127 $PAGE->requires->yui_module('moodle-course-toolboxes',
3128 'M.course.init_section_toolbox',
3129 array(array(
3130 'courseid' => $course->id,
3131 'format' => $course->format,
3132 'ajaxurl' => $config->sectionurl,
3133 'config' => $config,
3137 // Include course dragdrop
3138 if ($course->id != $SITE->id) {
3139 $PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_section_dragdrop',
3140 array(array(
3141 'courseid' => $course->id,
3142 'ajaxurl' => $config->sectionurl,
3143 'config' => $config,
3144 )), null, true);
3146 $PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_resource_dragdrop',
3147 array(array(
3148 'courseid' => $course->id,
3149 'ajaxurl' => $config->resourceurl,
3150 'config' => $config,
3151 )), null, true);
3154 // Require various strings for the command toolbox
3155 $PAGE->requires->strings_for_js(array(
3156 'moveleft',
3157 'deletechecktype',
3158 'deletechecktypename',
3159 'edittitle',
3160 'edittitleinstructions',
3161 'show',
3162 'hide',
3163 'groupsnone',
3164 'groupsvisible',
3165 'groupsseparate',
3166 'clicktochangeinbrackets',
3167 'markthistopic',
3168 'markedthistopic',
3169 'move',
3170 'movesection',
3171 'movecontent',
3172 'tocontent',
3173 'emptydragdropregion'
3174 ), 'moodle');
3176 // Include format-specific strings
3177 if ($course->id != $SITE->id) {
3178 $PAGE->requires->strings_for_js(array(
3179 'showfromothers',
3180 'hidefromothers',
3181 ), 'format_' . $course->format);
3184 // For confirming resource deletion we need the name of the module in question
3185 foreach ($usedmodules as $module => $modname) {
3186 $PAGE->requires->string_for_js('pluginname', $module);
3189 // Load drag and drop upload AJAX.
3190 require_once($CFG->dirroot.'/course/dnduploadlib.php');
3191 dndupload_add_to_course($course, $enabledmodules);
3193 return true;
3197 * Returns the sorted list of available course formats, filtered by enabled if necessary
3199 * @param bool $enabledonly return only formats that are enabled
3200 * @return array array of sorted format names
3202 function get_sorted_course_formats($enabledonly = false) {
3203 global $CFG;
3204 $formats = core_component::get_plugin_list('format');
3206 if (!empty($CFG->format_plugins_sortorder)) {
3207 $order = explode(',', $CFG->format_plugins_sortorder);
3208 $order = array_merge(array_intersect($order, array_keys($formats)),
3209 array_diff(array_keys($formats), $order));
3210 } else {
3211 $order = array_keys($formats);
3213 if (!$enabledonly) {
3214 return $order;
3216 $sortedformats = array();
3217 foreach ($order as $formatname) {
3218 if (!get_config('format_'.$formatname, 'disabled')) {
3219 $sortedformats[] = $formatname;
3222 return $sortedformats;
3226 * The URL to use for the specified course (with section)
3228 * @param int|stdClass $courseorid The course to get the section name for (either object or just course id)
3229 * @param int|stdClass $section Section object from database or just field course_sections.section
3230 * if omitted the course view page is returned
3231 * @param array $options options for view URL. At the moment core uses:
3232 * 'navigation' (bool) if true and section has no separate page, the function returns null
3233 * 'sr' (int) used by multipage formats to specify to which section to return
3234 * @return moodle_url The url of course
3236 function course_get_url($courseorid, $section = null, $options = array()) {
3237 return course_get_format($courseorid)->get_view_url($section, $options);
3241 * Create a module.
3243 * It includes:
3244 * - capability checks and other checks
3245 * - create the module from the module info
3247 * @param object $module
3248 * @return object the created module info
3250 function create_module($moduleinfo) {
3251 global $DB, $CFG;
3253 require_once($CFG->dirroot . '/course/modlib.php');
3255 // Check manadatory attributs.
3256 $mandatoryfields = array('modulename', 'course', 'section', 'visible');
3257 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
3258 $mandatoryfields[] = 'introeditor';
3260 foreach($mandatoryfields as $mandatoryfield) {
3261 if (!isset($moduleinfo->{$mandatoryfield})) {
3262 throw new moodle_exception('createmodulemissingattribut', '', '', $mandatoryfield);
3266 // Some additional checks (capability / existing instances).
3267 $course = $DB->get_record('course', array('id'=>$moduleinfo->course), '*', MUST_EXIST);
3268 list($module, $context, $cw) = can_add_moduleinfo($course, $moduleinfo->modulename, $moduleinfo->section);
3270 // Load module library.
3271 include_modulelib($module->name);
3273 // Add the module.
3274 $moduleinfo->module = $module->id;
3275 $moduleinfo = add_moduleinfo($moduleinfo, $course, null);
3277 return $moduleinfo;
3281 * Update a module.
3283 * It includes:
3284 * - capability and other checks
3285 * - update the module
3287 * @param object $module
3288 * @return object the updated module info
3290 function update_module($moduleinfo) {
3291 global $DB, $CFG;
3293 require_once($CFG->dirroot . '/course/modlib.php');
3295 // Check the course module exists.
3296 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST);
3298 // Check the course exists.
3299 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
3301 // Some checks (capaibility / existing instances).
3302 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
3304 // Load module library.
3305 include_modulelib($module->name);
3307 // Retrieve few information needed by update_moduleinfo.
3308 $moduleinfo->modulename = $cm->modname;
3309 if (!isset($moduleinfo->scale)) {
3310 $moduleinfo->scale = 0;
3312 $moduleinfo->type = 'mod';
3314 // Update the module.
3315 list($cm, $moduleinfo) = update_moduleinfo($cm, $moduleinfo, $course, null);
3317 return $moduleinfo;
3321 * Compare two objects to find out their correct order based on timestamp (to be used by usort).
3322 * Sorts by descending order of time.
3324 * @param stdClass $a First object
3325 * @param stdClass $b Second object
3326 * @return int 0,1,-1 representing the order
3328 function compare_activities_by_time_desc($a, $b) {
3329 // Make sure the activities actually have a timestamp property.
3330 if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3331 return 0;
3333 // We treat instances without timestamp as if they have a timestamp of 0.
3334 if ((!property_exists($a, 'timestamp')) && (property_exists($b,'timestamp'))) {
3335 return 1;
3337 if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3338 return -1;
3340 if ($a->timestamp == $b->timestamp) {
3341 return 0;
3343 return ($a->timestamp > $b->timestamp) ? -1 : 1;
3347 * Compare two objects to find out their correct order based on timestamp (to be used by usort).
3348 * Sorts by ascending order of time.
3350 * @param stdClass $a First object
3351 * @param stdClass $b Second object
3352 * @return int 0,1,-1 representing the order
3354 function compare_activities_by_time_asc($a, $b) {
3355 // Make sure the activities actually have a timestamp property.
3356 if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3357 return 0;
3359 // We treat instances without timestamp as if they have a timestamp of 0.
3360 if ((!property_exists($a, 'timestamp')) && (property_exists($b, 'timestamp'))) {
3361 return -1;
3363 if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
3364 return 1;
3366 if ($a->timestamp == $b->timestamp) {
3367 return 0;
3369 return ($a->timestamp < $b->timestamp) ? -1 : 1;