Change tooltip text by Helen's suggestion. MDL-13940
[moodle-linuxchix.git] / lib / datalib.php
blob4105429e83dfa564bd5ec444159c1d7cd5ea8181
1 <?php // $Id$
2 /**
3 * Library of functions for database manipulation.
5 * Other main libraries:
6 * - weblib.php - functions that produce web output
7 * - moodlelib.php - general-purpose Moodle functions
8 * @author Martin Dougiamas and many others
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package moodlecore
14 /**
15 * Escape all dangerous characters in a data record
17 * $dataobject is an object containing needed data
18 * Run over each field exectuting addslashes() function
19 * to escape SQL unfriendly characters (e.g. quotes)
20 * Handy when writing back data read from the database
22 * @param $dataobject Object containing the database record
23 * @return object Same object with neccessary characters escaped
25 function addslashes_object( $dataobject ) {
26 $a = get_object_vars( $dataobject);
27 foreach ($a as $key=>$value) {
28 $a[$key] = addslashes( $value );
30 return (object)$a;
33 /// USER DATABASE ////////////////////////////////////////////////
35 /**
36 * Returns $user object of the main admin user
37 * primary admin = admin with lowest role_assignment id among admins
38 * @uses $CFG
39 * @return object(admin) An associative array representing the admin user.
41 function get_admin () {
43 global $CFG;
44 static $myadmin;
46 if (isset($myadmin)) {
47 return $myadmin;
50 if ( $admins = get_admins() ) {
51 foreach ($admins as $admin) {
52 $myadmin = $admin;
53 return $admin; // ie the first one
55 } else {
56 return false;
60 /**
61 * Returns list of all admins, using 1 DB query. It depends on DB schema v1.7
62 * but does not depend on the v1.9 datastructures (context.path, etc).
64 * @uses $CFG
65 * @return object
67 function get_admins() {
69 global $CFG;
71 $sql = "SELECT ra.userid, SUM(rc.permission) AS permission, MIN(ra.id) AS adminid
72 FROM " . $CFG->prefix . "role_capabilities rc
73 JOIN " . $CFG->prefix . "context ctx
74 ON ctx.id=rc.contextid
75 JOIN " . $CFG->prefix . "role_assignments ra
76 ON ra.roleid=rc.roleid AND ra.contextid=ctx.id
77 WHERE ctx.contextlevel=10
78 AND rc.capability IN ('moodle/site:config',
79 'moodle/legacy:admin',
80 'moodle/site:doanything')
81 GROUP BY ra.userid
82 HAVING SUM(rc.permission) > 0";
84 $sql = "SELECT u.*, ra.adminid
85 FROM " . $CFG->prefix . "user u
86 JOIN ($sql) ra
87 ON u.id=ra.userid
88 ORDER BY ra.adminid ASC";
90 return get_records_sql($sql);
94 function get_courses_in_metacourse($metacourseid) {
95 global $CFG;
97 $sql = "SELECT c.id,c.shortname,c.fullname FROM {$CFG->prefix}course c, {$CFG->prefix}course_meta mc WHERE mc.parent_course = $metacourseid
98 AND mc.child_course = c.id ORDER BY c.shortname";
100 return get_records_sql($sql);
103 function get_courses_notin_metacourse($metacourseid,$count=false) {
105 global $CFG;
107 if ($count) {
108 $sql = "SELECT COUNT(c.id)";
109 } else {
110 $sql = "SELECT c.id,c.shortname,c.fullname";
113 $alreadycourses = get_courses_in_metacourse($metacourseid);
115 $sql .= " FROM {$CFG->prefix}course c WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).")
116 AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1 ".((empty($count)) ? " ORDER BY c.shortname" : "");
118 return get_records_sql($sql);
121 function count_courses_notin_metacourse($metacourseid) {
122 global $CFG;
124 $alreadycourses = get_courses_in_metacourse($metacourseid);
126 $sql = "SELECT COUNT(c.id) AS notin FROM {$CFG->prefix}course c
127 WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).")
128 AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1";
130 if (!$count = get_record_sql($sql)) {
131 return 0;
134 return $count->notin;
138 * Search through course users
140 * If $coursid specifies the site course then this function searches
141 * through all undeleted and confirmed users
143 * @uses $CFG
144 * @uses SITEID
145 * @param int $courseid The course in question.
146 * @param int $groupid The group in question.
147 * @param string $searchtext ?
148 * @param string $sort ?
149 * @param string $exceptions ?
150 * @return object
152 function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions='') {
153 global $CFG;
155 $LIKE = sql_ilike();
156 $fullname = sql_fullname('u.firstname', 'u.lastname');
158 if (!empty($exceptions)) {
159 $except = ' AND u.id NOT IN ('. $exceptions .') ';
160 } else {
161 $except = '';
164 if (!empty($sort)) {
165 $order = ' ORDER BY '. $sort;
166 } else {
167 $order = '';
170 $select = 'u.deleted = \'0\' AND u.confirmed = \'1\'';
172 if (!$courseid or $courseid == SITEID) {
173 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
174 FROM {$CFG->prefix}user u
175 WHERE $select
176 AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
177 $except $order");
178 } else {
180 if ($groupid) {
181 //TODO:check. Remove group DB dependencies.
182 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
183 FROM {$CFG->prefix}user u,
184 {$CFG->prefix}groups_members gm
185 WHERE $select AND gm.groupid = '$groupid' AND gm.userid = u.id
186 AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
187 $except $order");
188 } else {
189 $context = get_context_instance(CONTEXT_COURSE, $courseid);
190 $contextlists = get_related_contexts_string($context);
191 $users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
192 FROM {$CFG->prefix}user u,
193 {$CFG->prefix}role_assignments ra
194 WHERE $select AND ra.contextid $contextlists AND ra.userid = u.id
195 AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
196 $except $order");
198 return $users;
204 * Returns a list of all site users
205 * Obsolete, just calls get_course_users(SITEID)
207 * @uses SITEID
208 * @deprecated Use {@link get_course_users()} instead.
209 * @param string $fields A comma separated list of fields to be returned from the chosen table.
210 * @return object|false {@link $USER} records or false if error.
212 function get_site_users($sort='u.lastaccess DESC', $fields='*', $exceptions='') {
214 return get_course_users(SITEID, $sort, $exceptions, $fields);
219 * Returns a subset of users
221 * @uses $CFG
222 * @param bool $get If false then only a count of the records is returned
223 * @param string $search A simple string to search for
224 * @param bool $confirmed A switch to allow/disallow unconfirmed users
225 * @param array(int) $exceptions A list of IDs to ignore, eg 2,4,5,8,9,10
226 * @param string $sort A SQL snippet for the sorting criteria to use
227 * @param string $firstinitial ?
228 * @param string $lastinitial ?
229 * @param string $page ?
230 * @param string $recordsperpage ?
231 * @param string $fields A comma separated list of fields to be returned from the chosen table.
232 * @return object|false|int {@link $USER} records unless get is false in which case the integer count of the records found is returned. False is returned if an error is encountered.
234 function get_users($get=true, $search='', $confirmed=false, $exceptions='', $sort='firstname ASC',
235 $firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*', $extraselect='') {
237 global $CFG;
239 if ($get && !$recordsperpage) {
240 debugging('Call to get_users with $get = true no $recordsperpage limit. ' .
241 'On large installations, this will probably cause an out of memory error. ' .
242 'Please think again and change your code so that it does not try to ' .
243 'load so much data into memory.', DEBUG_DEVELOPER);
246 $LIKE = sql_ilike();
247 $fullname = sql_fullname();
249 $select = 'username <> \'guest\' AND deleted = 0';
251 if (!empty($search)){
252 $search = trim($search);
253 $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
256 if ($confirmed) {
257 $select .= ' AND confirmed = \'1\' ';
260 if ($exceptions) {
261 $select .= ' AND id NOT IN ('. $exceptions .') ';
264 if ($firstinitial) {
265 $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\'';
267 if ($lastinitial) {
268 $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\'';
271 if ($extraselect) {
272 $select .= " AND $extraselect ";
275 if ($get) {
276 return get_records_select('user', $select, $sort, $fields, $page, $recordsperpage);
277 } else {
278 return count_records_select('user', $select);
284 * shortdesc (optional)
286 * longdesc
288 * @uses $CFG
289 * @param string $sort ?
290 * @param string $dir ?
291 * @param int $categoryid ?
292 * @param int $categoryid ?
293 * @param string $search ?
294 * @param string $firstinitial ?
295 * @param string $lastinitial ?
296 * @returnobject {@link $USER} records
297 * @todo Finish documenting this function
300 function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0,
301 $search='', $firstinitial='', $lastinitial='', $extraselect='') {
303 global $CFG;
305 $LIKE = sql_ilike();
306 $fullname = sql_fullname();
308 $select = "deleted <> '1'";
310 if (!empty($search)) {
311 $search = trim($search);
312 $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%' OR username='$search') ";
315 if ($firstinitial) {
316 $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\' ';
319 if ($lastinitial) {
320 $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\' ';
323 if ($extraselect) {
324 $select .= " AND $extraselect ";
327 if ($sort) {
328 $sort = ' ORDER BY '. $sort .' '. $dir;
331 /// warning: will return UNCONFIRMED USERS
332 return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid
333 FROM {$CFG->prefix}user
334 WHERE $select $sort", $page, $recordsperpage);
340 * Full list of users that have confirmed their accounts.
342 * @uses $CFG
343 * @return object
345 function get_users_confirmed() {
346 global $CFG;
347 return get_records_sql("SELECT *
348 FROM {$CFG->prefix}user
349 WHERE confirmed = 1
350 AND deleted = 0
351 AND username <> 'guest'");
355 /// OTHER SITE AND COURSE FUNCTIONS /////////////////////////////////////////////
359 * Returns $course object of the top-level site.
361 * @return course A {@link $COURSE} object for the site
363 function get_site() {
365 global $SITE;
367 if (!empty($SITE->id)) { // We already have a global to use, so return that
368 return $SITE;
371 if ($course = get_record('course', 'category', 0)) {
372 return $course;
373 } else {
374 return false;
379 * Returns list of courses, for whole site, or category
381 * Returns list of courses, for whole site, or category
382 * Important: Using c.* for fields is extremely expensive because
383 * we are using distinct. You almost _NEVER_ need all the fields
384 * in such a large SELECT
386 * @param type description
389 function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") {
391 global $USER, $CFG;
393 if ($categoryid != "all" && is_numeric($categoryid)) {
394 $categoryselect = "WHERE c.category = '$categoryid'";
395 } else {
396 $categoryselect = "";
399 if (empty($sort)) {
400 $sortstatement = "";
401 } else {
402 $sortstatement = "ORDER BY $sort";
405 $visiblecourses = array();
407 // pull out all course matching the cat
408 if ($courses = get_records_sql("SELECT $fields,
409 ctx.id AS ctxid, ctx.path AS ctxpath,
410 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
411 FROM {$CFG->prefix}course c
412 JOIN {$CFG->prefix}context ctx
413 ON (c.id = ctx.instanceid
414 AND ctx.contextlevel=".CONTEXT_COURSE.")
415 $categoryselect
416 $sortstatement")) {
418 // loop throught them
419 foreach ($courses as $course) {
420 $course = make_context_subobj($course);
421 if (isset($course->visible) && $course->visible <= 0) {
422 // for hidden courses, require visibility check
423 if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
424 $visiblecourses [] = $course;
426 } else {
427 $visiblecourses [] = $course;
431 return $visiblecourses;
434 $teachertable = "";
435 $visiblecourses = "";
436 $sqland = "";
437 if (!empty($categoryselect)) {
438 $sqland = "AND ";
440 if (!empty($USER->id)) { // May need to check they are a teacher
441 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
442 $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
443 $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course = c.id";
445 } else {
446 $visiblecourses = "$sqland c.visible > 0";
449 if ($categoryselect or $visiblecourses) {
450 $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
451 } else {
452 $selectsql = "{$CFG->prefix}course c $teachertable";
455 $extrafield = str_replace('ASC','',$sort);
456 $extrafield = str_replace('DESC','',$extrafield);
457 $extrafield = trim($extrafield);
458 if (!empty($extrafield)) {
459 $extrafield = ','.$extrafield;
461 return get_records_sql("SELECT ".((!empty($teachertable)) ? " DISTINCT " : "")." $fields $extrafield FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : ""));
467 * Returns list of courses, for whole site, or category
469 * Similar to get_courses, but allows paging
470 * Important: Using c.* for fields is extremely expensive because
471 * we are using distinct. You almost _NEVER_ need all the fields
472 * in such a large SELECT
474 * @param type description
477 function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*",
478 &$totalcount, $limitfrom="", $limitnum="") {
480 global $USER, $CFG;
482 $categoryselect = "";
483 if ($categoryid != "all" && is_numeric($categoryid)) {
484 $categoryselect = "WHERE c.category = '$categoryid'";
485 } else {
486 $categoryselect = "";
489 // pull out all course matching the cat
490 $visiblecourses = array();
491 if (!($rs = get_recordset_sql("SELECT $fields,
492 ctx.id AS ctxid, ctx.path AS ctxpath,
493 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
494 FROM {$CFG->prefix}course c
495 JOIN {$CFG->prefix}context ctx
496 ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
497 $categoryselect
498 ORDER BY $sort"))) {
499 return $visiblecourses;
501 $totalcount = 0;
503 if (!$limitfrom) {
504 $limitfrom = 0;
507 // iteration will have to be done inside loop to keep track of the limitfrom and limitnum
508 while ($course = rs_fetch_next_record($rs)) {
509 $course = make_context_subobj($course);
510 if ($course->visible <= 0) {
511 // for hidden courses, require visibility check
512 if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
513 $totalcount++;
514 if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
515 $visiblecourses [] = $course;
518 } else {
519 $totalcount++;
520 if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
521 $visiblecourses [] = $course;
525 rs_close($rs);
526 return $visiblecourses;
530 $categoryselect = "";
531 if ($categoryid != "all" && is_numeric($categoryid)) {
532 $categoryselect = "c.category = '$categoryid'";
535 $teachertable = "";
536 $visiblecourses = "";
537 $sqland = "";
538 if (!empty($categoryselect)) {
539 $sqland = "AND ";
541 if (!empty($USER) and !empty($USER->id)) { // May need to check they are a teacher
542 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
543 $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
544 $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course=c.id";
546 } else {
547 $visiblecourses = "$sqland c.visible > 0";
550 if ($limitfrom !== "") {
551 $limit = sql_paging_limit($limitfrom, $limitnum);
552 } else {
553 $limit = "";
556 $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
558 $totalcount = count_records_sql("SELECT COUNT(DISTINCT c.id) FROM $selectsql");
560 return get_records_sql("SELECT $fields FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : "")." $limit");
565 * Retrieve course records with the course managers and other related records
566 * that we need for print_course(). This allows print_courses() to do its job
567 * in a constant number of DB queries, regardless of the number of courses,
568 * role assignments, etc.
570 * The returned array is indexed on c.id, and each course will have
571 * - $course->context - a context obj
572 * - $course->managers - array containing RA objects that include a $user obj
573 * with the minimal fields needed for fullname()
576 function get_courses_wmanagers($categoryid=0, $sort="c.sortorder ASC", $fields=array()) {
578 * The plan is to
580 * - Grab the courses JOINed w/context
582 * - Grab the interesting course-manager RAs
583 * JOINed with a base user obj and add them to each course
585 * So as to do all the work in 2 DB queries. The RA+user JOIN
586 * ends up being pretty expensive if it happens over _all_
587 * courses on a large site. (Are we surprised!?)
589 * So this should _never_ get called with 'all' on a large site.
592 global $USER, $CFG;
594 $allcats = false; // bool flag
595 if ($categoryid === 'all') {
596 $categoryclause = '';
597 $allcats = true;
598 } elseif (is_numeric($categoryid)) {
599 $categoryclause = "c.category = $categoryid";
600 } else {
601 debugging("Could not recognise categoryid = $categoryid");
602 $categoryclause = '';
605 $basefields = array('id', 'category', 'sortorder',
606 'shortname', 'fullname', 'idnumber',
607 'teacher', 'teachers', 'student', 'students',
608 'guest', 'startdate', 'visible',
609 'newsitems', 'cost', 'enrol',
610 'groupmode', 'groupmodeforce');
612 if (!is_null($fields) && is_string($fields)) {
613 if (empty($fields)) {
614 $fields = $basefields;
615 } else {
616 // turn the fields from a string to an array that
617 // get_user_courses_bycap() will like...
618 $fields = explode(',',$fields);
619 $fields = array_map('trim', $fields);
620 $fields = array_unique(array_merge($basefields, $fields));
622 } elseif (is_array($fields)) {
623 $fields = array_merge($basefields,$fields);
625 $coursefields = 'c.' .join(',c.', $fields);
627 if (empty($sort)) {
628 $sortstatement = "";
629 } else {
630 $sortstatement = "ORDER BY $sort";
633 $where = 'WHERE c.id != ' . SITEID;
634 if ($categoryclause !== ''){
635 $where = "$where AND $categoryclause";
638 // pull out all courses matching the cat
639 $sql = "SELECT $coursefields,
640 ctx.id AS ctxid, ctx.path AS ctxpath,
641 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
642 FROM {$CFG->prefix}course c
643 JOIN {$CFG->prefix}context ctx
644 ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
645 $where
646 $sortstatement";
648 $catpaths = array();
649 $catpath = NULL;
650 if ($courses = get_records_sql($sql)) {
651 // loop on courses materialising
652 // the context, and prepping data to fetch the
653 // managers efficiently later...
654 foreach ($courses as $k => $course) {
655 $courses[$k] = make_context_subobj($courses[$k]);
656 $courses[$k]->managers = array();
657 if ($allcats === false) {
658 // single cat, so take just the first one...
659 if ($catpath === NULL) {
660 $catpath = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
662 } else {
663 // chop off the contextid of the course itself
664 // like dirname() does...
665 $catpaths[] = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
668 } else {
669 return array(); // no courses!
672 $CFG->coursemanager = trim($CFG->coursemanager);
673 if (empty($CFG->coursemanager)) {
674 return $courses;
677 $managerroles = split(',', $CFG->coursemanager);
678 $catctxids = '';
679 if (count($managerroles)) {
680 if ($allcats === true) {
681 $catpaths = array_unique($catpaths);
682 $ctxids = array();
683 foreach ($catpaths as $cpath) {
684 $ctxids = array_merge($ctxids, explode('/',substr($cpath,1)));
686 $ctxids = array_unique($ctxids);
687 $catctxids = implode( ',' , $ctxids);
688 unset($catpaths);
689 unset($cpath);
690 } else {
691 // take the ctx path from the first course
692 // as all categories will be the same...
693 $catpath = substr($catpath,1);
694 $catpath = preg_replace(':/\d+$:','',$catpath);
695 $catctxids = str_replace('/',',',$catpath);
697 if ($categoryclause !== '') {
698 $categoryclause = "AND $categoryclause";
701 * Note: Here we use a LEFT OUTER JOIN that can
702 * "optionally" match to avoid passing a ton of context
703 * ids in an IN() clause. Perhaps a subselect is faster.
705 * In any case, this SQL is not-so-nice over large sets of
706 * courses with no $categoryclause.
709 $sql = "SELECT ctx.path, ctx.instanceid, ctx.contextlevel,
710 ra.hidden,
711 r.id AS roleid, r.name as rolename,
712 u.id AS userid, u.firstname, u.lastname
713 FROM {$CFG->prefix}role_assignments ra
714 JOIN {$CFG->prefix}context ctx
715 ON ra.contextid = ctx.id
716 JOIN {$CFG->prefix}user u
717 ON ra.userid = u.id
718 JOIN {$CFG->prefix}role r
719 ON ra.roleid = r.id
720 LEFT OUTER JOIN {$CFG->prefix}course c
721 ON (ctx.instanceid=c.id AND ctx.contextlevel=".CONTEXT_COURSE.")
722 WHERE ( c.id IS NOT NULL";
723 // under certain conditions, $catctxids is NULL
724 if($catctxids == NULL){
725 $sql .= ") ";
726 }else{
727 $sql .= " OR ra.contextid IN ($catctxids) )";
730 $sql .= "AND ra.roleid IN ({$CFG->coursemanager})
731 $categoryclause
732 ORDER BY r.sortorder ASC, ctx.contextlevel ASC, ra.sortorder ASC";
733 $rs = get_recordset_sql($sql);
735 // This loop is fairly stupid as it stands - might get better
736 // results doing an initial pass clustering RAs by path.
737 while ($ra = rs_fetch_next_record($rs)) {
738 $user = new StdClass;
739 $user->id = $ra->userid; unset($ra->userid);
740 $user->firstname = $ra->firstname; unset($ra->firstname);
741 $user->lastname = $ra->lastname; unset($ra->lastname);
742 $ra->user = $user;
743 if ($ra->contextlevel == CONTEXT_SYSTEM) {
744 foreach ($courses as $k => $course) {
745 $courses[$k]->managers[] = $ra;
747 } elseif ($ra->contextlevel == CONTEXT_COURSECAT) {
748 if ($allcats === false) {
749 // It always applies
750 foreach ($courses as $k => $course) {
751 $courses[$k]->managers[] = $ra;
753 } else {
754 foreach ($courses as $k => $course) {
755 // Note that strpos() returns 0 as "matched at pos 0"
756 if (strpos($course->context->path, $ra->path.'/')===0) {
757 // Only add it to subpaths
758 $courses[$k]->managers[] = $ra;
762 } else { // course-level
763 if(!array_key_exists($ra->instanceid, $courses)) {
764 //this course is not in a list, probably a frontpage course
765 continue;
767 $courses[$ra->instanceid]->managers[] = $ra;
770 rs_close($rs);
773 return $courses;
777 * Convenience function - lists courses that a user has access to view.
779 * For admins and others with access to "every" course in the system, we should
780 * try to get courses with explicit RAs.
782 * NOTE: this function is heavily geared towards the perspective of the user
783 * passed in $userid. So it will hide courses that the user cannot see
784 * (for any reason) even if called from cron or from another $USER's
785 * perspective.
787 * If you really want to know what courses are assigned to the user,
788 * without any hiding or scheming, call the lower-level
789 * get_user_courses_bycap().
792 * Notes inherited from get_user_courses_bycap():
794 * - $fields is an array of fieldnames to ADD
795 * so name the fields you really need, which will
796 * be added and uniq'd
798 * - the course records have $c->context which is a fully
799 * valid context object. Saves you a query per course!
801 * @uses $CFG,$USER
802 * @param int $userid The user of interest
803 * @param string $sort the sortorder in the course table
804 * @param array $fields - names of _additional_ fields to return (also accepts a string)
805 * @param bool $doanything True if using the doanything flag
806 * @param int $limit Maximum number of records to return, or 0 for unlimited
807 * @return array {@link $COURSE} of course objects
809 function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NULL, $doanything=false,$limit=0) {
811 global $CFG,$USER;
813 // Guest's do not have any courses
814 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
815 if (has_capability('moodle/legacy:guest',$sitecontext,$userid,false)) {
816 return(array());
819 $basefields = array('id', 'category', 'sortorder',
820 'shortname', 'fullname', 'idnumber',
821 'teacher', 'teachers', 'student', 'students',
822 'guest', 'startdate', 'visible',
823 'newsitems', 'cost', 'enrol',
824 'groupmode', 'groupmodeforce');
826 if (!is_null($fields) && is_string($fields)) {
827 if (empty($fields)) {
828 $fields = $basefields;
829 } else {
830 // turn the fields from a string to an array that
831 // get_user_courses_bycap() will like...
832 $fields = explode(',',$fields);
833 $fields = array_map('trim', $fields);
834 $fields = array_unique(array_merge($basefields, $fields));
836 } elseif (is_array($fields)) {
837 $fields = array_unique(array_merge($basefields, $fields));
838 } else {
839 $fields = $basefields;
842 $orderby = '';
843 $sort = trim($sort);
844 if (!empty($sort)) {
845 $rawsorts = explode(',', $sort);
846 $sorts = array();
847 foreach ($rawsorts as $rawsort) {
848 $rawsort = trim($rawsort);
849 if (strpos($rawsort, 'c.') === 0) {
850 $rawsort = substr($rawsort, 2);
852 $sorts[] = trim($rawsort);
854 $sort = 'c.'.implode(',c.', $sorts);
855 $orderby = "ORDER BY $sort";
859 // Logged-in user - Check cached courses
861 // NOTE! it's a _string_ because
862 // - it's all we'll ever use
863 // - it serialises much more compact than an array
864 // this a big concern here - cost of serialise
865 // and unserialise gets huge as the session grows
867 // If the courses are too many - it won't be set
868 // for large numbers of courses, caching in the session
869 // has marginal benefits (costs too much, not
870 // worthwhile...) and we may hit SQL parser limits
871 // because we use IN()
873 if ($userid === $USER->id) {
874 if (isset($USER->loginascontext)
875 && $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
876 // list _only_ this course
877 // anything else is asking for trouble...
878 $courseids = $USER->loginascontext->instanceid;
879 } elseif (isset($USER->mycourses)
880 && is_string($USER->mycourses)) {
881 if ($USER->mycourses === '') {
882 // empty str means: user has no courses
883 // ... so do the easy thing...
884 return array();
885 } else {
886 $courseids = $USER->mycourses;
889 if (isset($courseids)) {
890 // The data massaging here MUST be kept in sync with
891 // get_user_courses_bycap() so we return
892 // the same...
893 // (but here we don't need to check has_cap)
894 $coursefields = 'c.' .join(',c.', $fields);
895 $sql = "SELECT $coursefields,
896 ctx.id AS ctxid, ctx.path AS ctxpath,
897 ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel,
898 cc.path AS categorypath
899 FROM {$CFG->prefix}course c
900 JOIN {$CFG->prefix}course_categories cc
901 ON c.category=cc.id
902 JOIN {$CFG->prefix}context ctx
903 ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
904 WHERE c.id IN ($courseids)
905 $orderby";
906 $rs = get_recordset_sql($sql);
907 $courses = array();
908 $cc = 0; // keep count
909 while ($c = rs_fetch_next_record($rs)) {
910 // build the context obj
911 $c = make_context_subobj($c);
913 $courses[$c->id] = $c;
914 if ($limit > 0 && $cc++ > $limit) {
915 break;
918 rs_close($rs);
919 return $courses;
923 // Non-cached - get accessinfo
924 if ($userid === $USER->id && isset($USER->access)) {
925 $accessinfo = $USER->access;
926 } else {
927 $accessinfo = get_user_access_sitewide($userid);
931 $courses = get_user_courses_bycap($userid, 'moodle/course:view', $accessinfo,
932 $doanything, $sort, $fields,
933 $limit);
935 $cats = NULL;
936 // If we have to walk category visibility
937 // to eval course visibility, get the categories
938 if (empty($CFG->allowvisiblecoursesinhiddencategories)) {
939 $sql = "SELECT cc.id, cc.path, cc.visible,
940 ctx.id AS ctxid, ctx.path AS ctxpath,
941 ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel
942 FROM {$CFG->prefix}course_categories cc
943 JOIN {$CFG->prefix}context ctx ON (cc.id = ctx.instanceid)
944 WHERE ctx.contextlevel = ".CONTEXT_COURSECAT."
945 ORDER BY cc.id";
946 $rs = get_recordset_sql($sql);
948 // Using a temporary array instead of $cats here, to avoid a "true" result when isnull($cats) further down
949 $categories = array();
950 while ($course_cat = rs_fetch_next_record($rs)) {
951 // build the context obj
952 $course_cat = make_context_subobj($course_cat);
953 $categories[$course_cat->id] = $course_cat;
955 rs_close($rs);
957 if (!empty($categories)) {
958 $cats = $categories;
961 unset($course_cat);
964 // Strangely, get_my_courses() is expected to return the
965 // array keyed on id, which messes up the sorting
966 // So do that, and also cache the ids in the session if appropriate
968 $kcourses = array();
969 $courses_count = count($courses);
970 $cacheids = NULL;
971 $vcatpaths = array();
972 if ($userid === $USER->id && $courses_count < 500) {
973 $cacheids = array();
975 for ($n=0; $n<$courses_count; $n++) {
978 // Check whether $USER (not $userid) can _actually_ see them
979 // Easy if $CFG->allowvisiblecoursesinhiddencategories
980 // is set, and we don't have to care about categories.
981 // Lots of work otherwise... (all in mem though!)
983 $cansee = false;
984 if (is_null($cats)) { // easy rules!
985 if ($courses[$n]->visible == true) {
986 $cansee = true;
987 } elseif (has_capability('moodle/course:viewhiddencourses',
988 $courses[$n]->context, $USER->id)) {
989 $cansee = true;
991 } else {
993 // Is the cat visible?
994 // we have to assume it _is_ visible
995 // so we can shortcut when we find a hidden one
997 $viscat = true;
998 $cpath = $courses[$n]->categorypath;
999 if (isset($vcatpaths[$cpath])) {
1000 $viscat = $vcatpaths[$cpath];
1001 } else {
1002 $cpath = substr($cpath,1); // kill leading slash
1003 $cpath = explode('/',$cpath);
1004 $ccct = count($cpath);
1005 for ($m=0;$m<$ccct;$m++) {
1006 $ccid = $cpath[$m];
1007 if ($cats[$ccid]->visible==false) {
1008 $viscat = false;
1009 break;
1012 $vcatpaths[$courses[$n]->categorypath] = $viscat;
1016 // Perhaps it's actually visible to $USER
1017 // check moodle/category:visibility
1019 // The name isn't obvious, but the description says
1020 // "See hidden categories" so the user shall see...
1021 // But also check if the allowvisiblecoursesinhiddencategories setting is true, and check for course visibility
1022 if ($viscat === false) {
1023 $catctx = $cats[$courses[$n]->category]->context;
1024 if (has_capability('moodle/category:visibility', $catctx, $USER->id)) {
1025 $vcatpaths[$courses[$n]->categorypath] = true;
1026 $viscat = true;
1027 } elseif ($CFG->allowvisiblecoursesinhiddencategories && $courses[$n]->visible == true) {
1028 $viscat = true;
1033 // Decision matrix
1035 if ($viscat === true) {
1036 if ($courses[$n]->visible == true) {
1037 $cansee = true;
1038 } elseif (has_capability('moodle/course:viewhiddencourses',
1039 $courses[$n]->context, $USER->id)) {
1040 $cansee = true;
1044 if ($cansee === true) {
1045 $kcourses[$courses[$n]->id] = $courses[$n];
1046 if (is_array($cacheids)) {
1047 $cacheids[] = $courses[$n]->id;
1051 if (is_array($cacheids)) {
1052 // Only happens
1053 // - for the logged in user
1054 // - below the threshold (500)
1055 // empty string is _valid_
1056 $USER->mycourses = join(',',$cacheids);
1057 } elseif ($userid === $USER->id && isset($USER->mycourses)) {
1058 // cheap sanity check
1059 unset($USER->mycourses);
1062 return $kcourses;
1066 * A list of courses that match a search
1068 * @uses $CFG
1069 * @param array $searchterms ?
1070 * @param string $sort ?
1071 * @param int $page ?
1072 * @param int $recordsperpage ?
1073 * @param int $totalcount Passed in by reference. ?
1074 * @return object {@link $COURSE} records
1076 function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $recordsperpage=50, &$totalcount) {
1078 global $CFG;
1080 //to allow case-insensitive search for postgesql
1081 if ($CFG->dbfamily == 'postgres') {
1082 $LIKE = 'ILIKE';
1083 $NOTLIKE = 'NOT ILIKE'; // case-insensitive
1084 $REGEXP = '~*';
1085 $NOTREGEXP = '!~*';
1086 } else {
1087 $LIKE = 'LIKE';
1088 $NOTLIKE = 'NOT LIKE';
1089 $REGEXP = 'REGEXP';
1090 $NOTREGEXP = 'NOT REGEXP';
1093 $fullnamesearch = '';
1094 $summarysearch = '';
1096 foreach ($searchterms as $searchterm) {
1098 $NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
1099 /// will use it to simulate the "-" operator with LIKE clause
1101 /// Under Oracle and MSSQL, trim the + and - operators and perform
1102 /// simpler LIKE (or NOT LIKE) queries
1103 if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') {
1104 if (substr($searchterm, 0, 1) == '-') {
1105 $NOT = ' NOT ';
1107 $searchterm = trim($searchterm, '+-');
1110 if ($fullnamesearch) {
1111 $fullnamesearch .= ' AND ';
1113 if ($summarysearch) {
1114 $summarysearch .= ' AND ';
1117 if (substr($searchterm,0,1) == '+') {
1118 $searchterm = substr($searchterm,1);
1119 $summarysearch .= " c.summary $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1120 $fullnamesearch .= " c.fullname $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1121 } else if (substr($searchterm,0,1) == "-") {
1122 $searchterm = substr($searchterm,1);
1123 $summarysearch .= " c.summary $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1124 $fullnamesearch .= " c.fullname $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1125 } else {
1126 $summarysearch .= ' summary '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
1127 $fullnamesearch .= ' fullname '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
1132 $sql = "SELECT c.*,
1133 ctx.id AS ctxid, ctx.path AS ctxpath,
1134 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
1135 FROM {$CFG->prefix}course c
1136 JOIN {$CFG->prefix}context ctx
1137 ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
1138 WHERE (( $fullnamesearch ) OR ( $summarysearch ))
1139 AND category > 0
1140 ORDER BY " . $sort;
1142 $courses = array();
1144 if ($rs = get_recordset_sql($sql)) {
1147 // Tiki pagination
1148 $limitfrom = $page * $recordsperpage;
1149 $limitto = $limitfrom + $recordsperpage;
1150 $c = 0; // counts how many visible courses we've seen
1152 while ($course = rs_fetch_next_record($rs)) {
1153 $course = make_context_subobj($course);
1154 if ($course->visible || has_capability('moodle/course:viewhiddencourses', $course->context)) {
1155 // Don't exit this loop till the end
1156 // we need to count all the visible courses
1157 // to update $totalcount
1158 if ($c >= $limitfrom && $c < $limitto) {
1159 $courses[] = $course;
1161 $c++;
1166 // our caller expects 2 bits of data - our return
1167 // array, and an updated $totalcount
1168 $totalcount = $c;
1169 return $courses;
1174 * Returns a sorted list of categories. Each category object has a context
1175 * property that is a context object.
1177 * When asking for $parent='none' it will return all the categories, regardless
1178 * of depth. Wheen asking for a specific parent, the default is to return
1179 * a "shallow" resultset. Pass false to $shallow and it will return all
1180 * the child categories as well.
1183 * @param string $parent The parent category if any
1184 * @param string $sort the sortorder
1185 * @param bool $shallow - set to false to get the children too
1186 * @return array of categories
1188 function get_categories($parent='none', $sort=NULL, $shallow=true) {
1189 global $CFG;
1191 if ($sort === NULL) {
1192 $sort = 'ORDER BY cc.sortorder ASC';
1193 } elseif ($sort ==='') {
1194 // leave it as empty
1195 } else {
1196 $sort = "ORDER BY $sort";
1199 if ($parent === 'none') {
1200 $sql = "SELECT cc.*,
1201 ctx.id AS ctxid, ctx.path AS ctxpath,
1202 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
1203 FROM {$CFG->prefix}course_categories cc
1204 JOIN {$CFG->prefix}context ctx
1205 ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
1206 $sort";
1207 } elseif ($shallow) {
1208 $parent = (int)$parent;
1209 $sql = "SELECT cc.*,
1210 ctx.id AS ctxid, ctx.path AS ctxpath,
1211 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
1212 FROM {$CFG->prefix}course_categories cc
1213 JOIN {$CFG->prefix}context ctx
1214 ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
1215 WHERE cc.parent=$parent
1216 $sort";
1217 } else {
1218 $parent = (int)$parent;
1219 $sql = "SELECT cc.*,
1220 ctx.id AS ctxid, ctx.path AS ctxpath,
1221 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
1222 FROM {$CFG->prefix}course_categories cc
1223 JOIN {$CFG->prefix}context ctx
1224 ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
1225 JOIN {$CFG->prefix}course_categories ccp
1226 ON (cc.path LIKE ".sql_concat('ccp.path',"'%'").")
1227 WHERE ccp.id=$parent
1228 $sort";
1230 $categories = array();
1232 if( $rs = get_recordset_sql($sql) ){
1233 while ($cat = rs_fetch_next_record($rs)) {
1234 $cat = make_context_subobj($cat);
1235 if ($cat->visible || has_capability('moodle/course:create',$cat->context)) {
1236 $categories[$cat->id] = $cat;
1240 return $categories;
1245 * Returns an array of category ids of all the subcategories for a given
1246 * category.
1247 * @param $catid - The id of the category whose subcategories we want to find.
1248 * @return array of category ids.
1250 function get_all_subcategories($catid) {
1252 $subcats = array();
1254 if ($categories = get_records('course_categories', 'parent', $catid)) {
1255 foreach ($categories as $cat) {
1256 array_push($subcats, $cat->id);
1257 $subcats = array_merge($subcats, get_all_subcategories($cat->id));
1260 return $subcats;
1265 * This recursive function makes sure that the courseorder is consecutive
1267 * @param type description
1269 * $n is the starting point, offered only for compatilibity -- will be ignored!
1270 * $safe (bool) prevents it from assuming category-sortorder is unique, used to upgrade
1271 * safely from 1.4 to 1.5
1273 function fix_course_sortorder($categoryid=0, $n=0, $safe=0, $depth=0, $path='') {
1275 global $CFG;
1277 $count = 0;
1279 $catgap = 1000; // "standard" category gap
1280 $tolerance = 200; // how "close" categories can get
1282 if ($categoryid > 0){
1283 // update depth and path
1284 $cat = get_record('course_categories', 'id', $categoryid);
1285 if ($cat->parent == 0) {
1286 $depth = 0;
1287 $path = '';
1288 } else if ($depth == 0 ) { // doesn't make sense; get from DB
1289 // this is only called if the $depth parameter looks dodgy
1290 $parent = get_record('course_categories', 'id', $cat->parent);
1291 $path = $parent->path;
1292 $depth = $parent->depth;
1294 $path = $path . '/' . $categoryid;
1295 $depth = $depth + 1;
1297 if ($cat->path !== $path) {
1298 set_field('course_categories', 'path', addslashes($path), 'id', $categoryid);
1300 if ($cat->depth != $depth) {
1301 set_field('course_categories', 'depth', $depth, 'id', $categoryid);
1305 // get some basic info about courses in the category
1306 $info = get_record_sql('SELECT MIN(sortorder) AS min,
1307 MAX(sortorder) AS max,
1308 COUNT(sortorder) AS count
1309 FROM ' . $CFG->prefix . 'course
1310 WHERE category=' . $categoryid);
1311 if (is_object($info)) { // no courses?
1312 $max = $info->max;
1313 $count = $info->count;
1314 $min = $info->min;
1315 unset($info);
1318 if ($categoryid > 0 && $n==0) { // only passed category so don't shift it
1319 $n = $min;
1322 // $hasgap flag indicates whether there's a gap in the sequence
1323 $hasgap = false;
1324 if ($max-$min+1 != $count) {
1325 $hasgap = true;
1328 // $mustshift indicates whether the sequence must be shifted to
1329 // meet its range
1330 $mustshift = false;
1331 if ($min < $n+$tolerance || $min > $n+$tolerance+$catgap ) {
1332 $mustshift = true;
1335 // actually sort only if there are courses,
1336 // and we meet one ofthe triggers:
1337 // - safe flag
1338 // - they are not in a continuos block
1339 // - they are too close to the 'bottom'
1340 if ($count && ( $safe || $hasgap || $mustshift ) ) {
1341 // special, optimized case where all we need is to shift
1342 if ( $mustshift && !$safe && !$hasgap) {
1343 $shift = $n + $catgap - $min;
1344 if ($shift < $count) {
1345 $shift = $count + $catgap;
1347 // UPDATE course SET sortorder=sortorder+$shift
1348 execute_sql("UPDATE {$CFG->prefix}course
1349 SET sortorder=sortorder+$shift
1350 WHERE category=$categoryid", 0);
1351 $n = $n + $catgap + $count;
1353 } else { // do it slowly
1354 $n = $n + $catgap;
1355 // if the new sequence overlaps the current sequence, lack of transactions
1356 // will stop us -- shift things aside for a moment...
1357 if ($safe || ($n >= $min && $n+$count+1 < $min && $CFG->dbfamily==='mysql')) {
1358 $shift = $max + $n + 1000;
1359 execute_sql("UPDATE {$CFG->prefix}course
1360 SET sortorder=sortorder+$shift
1361 WHERE category=$categoryid", 0);
1364 $courses = get_courses($categoryid, 'c.sortorder ASC', 'c.id,c.sortorder');
1365 begin_sql();
1366 $tx = true; // transaction sanity
1367 foreach ($courses as $course) {
1368 if ($tx && $course->sortorder != $n ) { // save db traffic
1369 $tx = $tx && set_field('course', 'sortorder', $n,
1370 'id', $course->id);
1372 $n++;
1374 if ($tx) {
1375 commit_sql();
1376 } else {
1377 rollback_sql();
1378 if (!$safe) {
1379 // if we failed when called with !safe, try
1380 // to recover calling self with safe=true
1381 return fix_course_sortorder($categoryid, $n, true, $depth, $path);
1386 set_field('course_categories', 'coursecount', $count, 'id', $categoryid);
1388 // $n could need updating
1389 $max = get_field_sql("SELECT MAX(sortorder) from {$CFG->prefix}course WHERE category=$categoryid");
1390 if ($max > $n) {
1391 $n = $max;
1394 if ($categories = get_categories($categoryid)) {
1395 foreach ($categories as $category) {
1396 $n = fix_course_sortorder($category->id, $n, $safe, $depth, $path);
1400 return $n+1;
1404 * Ensure all courses have a valid course category
1405 * useful if a category has been removed manually
1407 function fix_coursecategory_orphans() {
1409 global $CFG;
1411 // Note: the handling of sortorder here is arguably
1412 // open to race conditions. Hard to fix here, unlikely
1413 // to hit anyone in production.
1415 $sql = "SELECT c.id, c.category, c.shortname
1416 FROM {$CFG->prefix}course c
1417 LEFT OUTER JOIN {$CFG->prefix}course_categories cc ON c.category=cc.id
1418 WHERE cc.id IS NULL AND c.id != " . SITEID;
1420 $rs = get_recordset_sql($sql);
1422 if (!rs_EOF($rs)) { // we have some orphans
1424 // the "default" category is the lowest numbered...
1425 $default = get_field_sql("SELECT MIN(id)
1426 FROM {$CFG->prefix}course_categories");
1427 $sortorder = get_field_sql("SELECT MAX(sortorder)
1428 FROM {$CFG->prefix}course
1429 WHERE category=$default");
1432 begin_sql();
1433 $tx = true;
1434 while ($tx && $course = rs_fetch_next_record($rs)) {
1435 $tx = $tx && set_field('course', 'category', $default, 'id', $course->id);
1436 $tx = $tx && set_field('course', 'sortorder', ++$sortorder, 'id', $course->id);
1438 if ($tx) {
1439 commit_sql();
1440 } else {
1441 rollback_sql();
1444 rs_close($rs);
1448 * List of remote courses that a user has access to via MNET.
1449 * Works only on the IDP
1451 * @uses $CFG, $USER
1452 * @return array {@link $COURSE} of course objects
1454 function get_my_remotecourses($userid=0) {
1455 global $CFG, $USER;
1457 if (empty($userid)) {
1458 $userid = $USER->id;
1461 $sql = "SELECT c.remoteid, c.shortname, c.fullname,
1462 c.hostid, c.summary, c.cat_name,
1463 h.name AS hostname
1464 FROM {$CFG->prefix}mnet_enrol_course c
1465 JOIN {$CFG->prefix}mnet_enrol_assignments a ON c.id=a.courseid
1466 JOIN {$CFG->prefix}mnet_host h ON c.hostid=h.id
1467 WHERE a.userid={$userid}";
1469 return get_records_sql($sql);
1473 * List of remote hosts that a user has access to via MNET.
1474 * Works on the SP
1476 * @uses $CFG, $USER
1477 * @return array of host objects
1479 function get_my_remotehosts() {
1480 global $CFG, $USER;
1482 if ($USER->mnethostid == $CFG->mnet_localhost_id) {
1483 return false; // Return nothing on the IDP
1485 if (!empty($USER->mnet_foreign_host_array) && is_array($USER->mnet_foreign_host_array)) {
1486 return $USER->mnet_foreign_host_array;
1488 return false;
1492 * This function creates a default separated/connected scale
1494 * This function creates a default separated/connected scale
1495 * so there's something in the database. The locations of
1496 * strings and files is a bit odd, but this is because we
1497 * need to maintain backward compatibility with many different
1498 * existing language translations and older sites.
1500 * @uses $CFG
1502 function make_default_scale() {
1504 global $CFG;
1506 $defaultscale = NULL;
1507 $defaultscale->courseid = 0;
1508 $defaultscale->userid = 0;
1509 $defaultscale->name = get_string('separateandconnected');
1510 $defaultscale->scale = get_string('postrating1', 'forum').','.
1511 get_string('postrating2', 'forum').','.
1512 get_string('postrating3', 'forum');
1513 $defaultscale->timemodified = time();
1515 /// Read in the big description from the file. Note this is not
1516 /// HTML (despite the file extension) but Moodle format text.
1517 $parentlang = get_string('parentlanguage');
1518 if ($parentlang[0] == '[') {
1519 $parentlang = '';
1521 if (is_readable($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
1522 $file = file($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
1523 } else if (is_readable($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
1524 $file = file($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
1525 } else if ($parentlang and is_readable($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
1526 $file = file($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
1527 } else if ($parentlang and is_readable($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
1528 $file = file($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
1529 } else if (is_readable($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html')) {
1530 $file = file($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html');
1531 } else {
1532 $file = '';
1535 $defaultscale->description = addslashes(implode('', $file));
1537 if ($defaultscale->id = insert_record('scale', $defaultscale)) {
1538 execute_sql('UPDATE '. $CFG->prefix .'forum SET scale = \''. $defaultscale->id .'\'', false);
1544 * Returns a menu of all available scales from the site as well as the given course
1546 * @uses $CFG
1547 * @param int $courseid The id of the course as found in the 'course' table.
1548 * @return object
1550 function get_scales_menu($courseid=0) {
1552 global $CFG;
1554 $sql = "SELECT id, name FROM {$CFG->prefix}scale
1555 WHERE courseid = '0' or courseid = '$courseid'
1556 ORDER BY courseid ASC, name ASC";
1558 if ($scales = get_records_sql_menu($sql)) {
1559 return $scales;
1562 make_default_scale();
1564 return get_records_sql_menu($sql);
1570 * Given a set of timezone records, put them in the database, replacing what is there
1572 * @uses $CFG
1573 * @param array $timezones An array of timezone records
1575 function update_timezone_records($timezones) {
1576 /// Given a set of timezone records, put them in the database
1578 global $CFG;
1580 /// Clear out all the old stuff
1581 execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone', false);
1583 /// Insert all the new stuff
1584 foreach ($timezones as $timezone) {
1585 if (is_array($timezone)) {
1586 $timezone = (object)$timezone;
1588 insert_record('timezone', $timezone);
1593 /// MODULE FUNCTIONS /////////////////////////////////////////////////
1596 * Just gets a raw list of all modules in a course
1598 * @uses $CFG
1599 * @param int $courseid The id of the course as found in the 'course' table.
1600 * @return object
1602 function get_course_mods($courseid) {
1603 global $CFG;
1605 if (empty($courseid)) {
1606 return false; // avoid warnings
1609 return get_records_sql("SELECT cm.*, m.name as modname
1610 FROM {$CFG->prefix}modules m,
1611 {$CFG->prefix}course_modules cm
1612 WHERE cm.course = ".intval($courseid)."
1613 AND cm.module = m.id AND m.visible = 1"); // no disabled mods
1618 * Given an id of a course module, finds the coursemodule description
1620 * @param string $modulename name of module type, eg. resource, assignment,...
1621 * @param int $cmid course module id (id in course_modules table)
1622 * @param int $courseid optional course id for extra validation
1623 * @return object course module instance with instance and module name
1625 function get_coursemodule_from_id($modulename, $cmid, $courseid=0) {
1627 global $CFG;
1629 $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
1631 return get_record_sql("SELECT cm.*, m.name, md.name as modname
1632 FROM {$CFG->prefix}course_modules cm,
1633 {$CFG->prefix}modules md,
1634 {$CFG->prefix}$modulename m
1635 WHERE $courseselect
1636 cm.id = ".intval($cmid)." AND
1637 cm.instance = m.id AND
1638 md.name = '$modulename' AND
1639 md.id = cm.module");
1643 * Given an instance number of a module, finds the coursemodule description
1645 * @param string $modulename name of module type, eg. resource, assignment,...
1646 * @param int $instance module instance number (id in resource, assignment etc. table)
1647 * @param int $courseid optional course id for extra validation
1648 * @return object course module instance with instance and module name
1650 function get_coursemodule_from_instance($modulename, $instance, $courseid=0) {
1652 global $CFG;
1654 $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
1656 return get_record_sql("SELECT cm.*, m.name, md.name as modname
1657 FROM {$CFG->prefix}course_modules cm,
1658 {$CFG->prefix}modules md,
1659 {$CFG->prefix}$modulename m
1660 WHERE $courseselect
1661 cm.instance = m.id AND
1662 md.name = '$modulename' AND
1663 md.id = cm.module AND
1664 m.id = ".intval($instance));
1669 * Returns all course modules of given activity in course
1670 * @param string $modulename (forum, quiz, etc.)
1671 * @param int $courseid
1672 * @param string $extrafields extra fields starting with m.
1673 * @return array of cm objects, false if not found or error
1675 function get_coursemodules_in_course($modulename, $courseid, $extrafields='') {
1676 global $CFG;
1678 if (!empty($extrafields)) {
1679 $extrafields = ", $extrafields";
1681 return get_records_sql("SELECT cm.*, m.name, md.name as modname $extrafields
1682 FROM {$CFG->prefix}course_modules cm,
1683 {$CFG->prefix}modules md,
1684 {$CFG->prefix}$modulename m
1685 WHERE cm.course = $courseid AND
1686 cm.instance = m.id AND
1687 md.name = '$modulename' AND
1688 md.id = cm.module");
1692 * Returns an array of all the active instances of a particular module in given courses, sorted in the order they are defined
1694 * Returns an array of all the active instances of a particular
1695 * module in given courses, sorted in the order they are defined
1696 * in the course. Returns an empty array on any errors.
1698 * The returned objects includle the columns cw.section, cm.visible,
1699 * cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
1701 * @param string $modulename The name of the module to get instances for
1702 * @param array $courses an array of course objects.
1703 * @return array of module instance objects, including some extra fields from the course_modules
1704 * and course_sections tables, or an empty array if an error occurred.
1706 function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $includeinvisible=false) {
1707 global $CFG;
1709 $outputarray = array();
1711 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1712 return $outputarray;
1715 if (!$rawmods = get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
1716 cm.groupmode, cm.groupingid, cm.groupmembersonly
1717 FROM {$CFG->prefix}course_modules cm,
1718 {$CFG->prefix}course_sections cw,
1719 {$CFG->prefix}modules md,
1720 {$CFG->prefix}$modulename m
1721 WHERE cm.course IN (".implode(',',array_keys($courses)).") AND
1722 cm.instance = m.id AND
1723 cm.section = cw.id AND
1724 md.name = '$modulename' AND
1725 md.id = cm.module")) {
1726 return $outputarray;
1729 require_once($CFG->dirroot.'/course/lib.php');
1731 foreach ($courses as $course) {
1732 $modinfo = get_fast_modinfo($course, $userid);
1734 if (empty($modinfo->instances[$modulename])) {
1735 continue;
1738 foreach ($modinfo->instances[$modulename] as $cm) {
1739 if (!$includeinvisible and !$cm->uservisible) {
1740 continue;
1742 if (!isset($rawmods[$cm->id])) {
1743 continue;
1745 $instance = $rawmods[$cm->id];
1746 if (!empty($cm->extra)) {
1747 $instance->extra = urlencode($cm->extra); // bc compatibility
1749 $outputarray[] = $instance;
1753 return $outputarray;
1757 * Returns an array of all the active instances of a particular module in a given course,
1758 * sorted in the order they are defined.
1760 * Returns an array of all the active instances of a particular
1761 * module in a given course, sorted in the order they are defined
1762 * in the course. Returns an empty array on any errors.
1764 * The returned objects includle the columns cw.section, cm.visible,
1765 * cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
1767 * @param string $modulename The name of the module to get instances for
1768 * @param object $course The course obect.
1769 * @return array of module instance objects, including some extra fields from the course_modules
1770 * and course_sections tables, or an empty array if an error occurred.
1772 function get_all_instances_in_course($modulename, $course, $userid=NULL, $includeinvisible=false) {
1773 return get_all_instances_in_courses($modulename, array($course->id => $course), $userid, $includeinvisible);
1778 * Determine whether a module instance is visible within a course
1780 * Given a valid module object with info about the id and course,
1781 * and the module's type (eg "forum") returns whether the object
1782 * is visible or not, groupmembersonly visibility not tested
1784 * @uses $CFG
1785 * @param $moduletype Name of the module eg 'forum'
1786 * @param $module Object which is the instance of the module
1787 * @return bool
1789 function instance_is_visible($moduletype, $module) {
1791 global $CFG;
1793 if (!empty($module->id)) {
1794 if ($records = get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course
1795 FROM {$CFG->prefix}course_modules cm,
1796 {$CFG->prefix}modules m
1797 WHERE cm.course = '$module->course' AND
1798 cm.module = m.id AND
1799 m.name = '$moduletype' AND
1800 cm.instance = '$module->id'")) {
1802 foreach ($records as $record) { // there should only be one - use the first one
1803 return $record->visible;
1807 return true; // visible by default!
1811 * Determine whether a course module is visible within a course,
1812 * this is different from instance_is_visible() - faster and visibility for user
1814 * @param object $cm object
1815 * @param int $userid empty means current user
1816 * @return bool
1818 function coursemodule_visible_for_user($cm, $userid=0) {
1819 global $USER;
1821 if (empty($cm->id)) {
1822 debugging("Incorrect course module parameter!", DEBUG_DEVELOPER);
1823 return false;
1825 if (empty($userid)) {
1826 $userid = $USER->id;
1828 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
1829 return false;
1831 return groups_course_module_visible($cm, $userid);
1837 /// LOG FUNCTIONS /////////////////////////////////////////////////////
1841 * Add an entry to the log table.
1843 * Add an entry to the log table. These are "action" focussed rather
1844 * than web server hits, and provide a way to easily reconstruct what
1845 * any particular student has been doing.
1847 * @uses $CFG
1848 * @uses $USER
1849 * @uses $db
1850 * @uses $REMOTE_ADDR
1851 * @uses SITEID
1852 * @param int $courseid The course id
1853 * @param string $module The module name - e.g. forum, journal, resource, course, user etc
1854 * @param string $action 'view', 'update', 'add' or 'delete', possibly followed by another word to clarify.
1855 * @param string $url The file and parameters used to see the results of the action
1856 * @param string $info Additional description information
1857 * @param string $cm The course_module->id if there is one
1858 * @param string $user If log regards $user other than $USER
1860 function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) {
1861 // Note that this function intentionally does not follow the normal Moodle DB access idioms.
1862 // This is for a good reason: it is the most frequently used DB update function,
1863 // so it has been optimised for speed.
1864 global $db, $CFG, $USER;
1866 if ($cm === '' || is_null($cm)) { // postgres won't translate empty string to its default
1867 $cm = 0;
1870 if ($user) {
1871 $userid = $user;
1872 } else {
1873 if (!empty($USER->realuser)) { // Don't log
1874 return;
1876 $userid = empty($USER->id) ? '0' : $USER->id;
1879 $REMOTE_ADDR = getremoteaddr();
1881 $timenow = time();
1882 $info = addslashes($info);
1883 if (!empty($url)) { // could break doing html_entity_decode on an empty var.
1884 $url = html_entity_decode($url); // for php < 4.3.0 this is defined in moodlelib.php
1887 // Restrict length of log lines to the space actually available in the
1888 // database so that it doesn't cause a DB error. Log a warning so that
1889 // developers can avoid doing things which are likely to cause this on a
1890 // routine basis.
1891 $tl=textlib_get_instance();
1892 if(!empty($info) && $tl->strlen($info)>255) {
1893 $info=$tl->substr($info,0,252).'...';
1894 debugging('Warning: logged very long info',DEBUG_DEVELOPER);
1896 // Note: Unlike $info, URL appears to be already slashed before this function
1897 // is called. Since database limits are for the data before slashes, we need
1898 // to remove them...
1899 $url=stripslashes($url);
1900 // If the 100 field size is changed, also need to alter print_log in course/lib.php
1901 if(!empty($url) && $tl->strlen($url)>100) {
1902 $url=$tl->substr($url,0,97).'...';
1903 debugging('Warning: logged very long URL',DEBUG_DEVELOPER);
1905 $url=addslashes($url);
1907 if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; $PERF->logwrites++;};
1909 if ($CFG->type = 'oci8po') {
1910 if (empty($info)) {
1911 $info = ' ';
1915 $result = $db->Execute('INSERT INTO '. $CFG->prefix .'log (time, userid, course, ip, module, cmid, action, url, info)
1916 VALUES (' . "'$timenow', '$userid', '$courseid', '$REMOTE_ADDR', '$module', '$cm', '$action', '$url', '$info')");
1918 // MDL-11893, alert $CFG->supportemail if insert into log failed
1919 if (!$result && $CFG->supportemail) {
1920 $site = get_site();
1921 $subject = 'Insert into log failed at your moodle site '.$site->fullname;
1922 $message = 'Insert into log table failed at '.date('l dS \of F Y h:i:s A').'. It is possible that your disk is full.';
1924 // email_to_user is not usable because email_to_user tries to write to the logs table, and this will get caught
1925 // in an infinite loop, if disk is full
1926 if (empty($CFG->noemailever)) {
1927 mail($CFG->supportemail, $subject, $message);
1931 if (!$result and debugging()) {
1932 echo '<p>Error: Could not insert a new entry to the Moodle log</p>'; // Don't throw an error
1935 /// Store lastaccess times for the current user, do not use in cron and other commandline scripts
1936 /// only update the lastaccess/timeaccess fields only once every 60s
1937 if (!empty($USER->id) && ($userid == $USER->id) && !defined('FULLME')) {
1938 $res = $db->Execute('UPDATE '. $CFG->prefix .'user
1939 SET lastip=\''. $REMOTE_ADDR .'\', lastaccess=\''. $timenow .'\'
1940 WHERE id = \''. $userid .'\' AND '.$timenow.' - lastaccess > 60');
1941 if (!$res) {
1942 debugging('<p>Error: Could not insert a new entry to the Moodle log</p>'); // Don't throw an error
1944 if ($courseid != SITEID && !empty($courseid)) {
1945 if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++;};
1947 if ($ulid = get_field('user_lastaccess', 'id', 'userid', $userid, 'courseid', $courseid)) {
1948 $res = $db->Execute("UPDATE {$CFG->prefix}user_lastaccess
1949 SET timeaccess=$timenow
1950 WHERE id = $ulid AND $timenow - timeaccess > 60");
1951 if (!$res) {
1952 debugging('Error: Could not insert a new entry to the Moodle log'); // Don't throw an error
1954 } else {
1955 $res = $db->Execute("INSERT INTO {$CFG->prefix}user_lastaccess
1956 ( userid, courseid, timeaccess)
1957 VALUES ($userid, $courseid, $timenow)");
1958 if (!$res) {
1959 debugging('Error: Could not insert a new entry to the Moodle log'); // Don't throw an error
1962 if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++;};
1969 * Select all log records based on SQL criteria
1971 * @uses $CFG
1972 * @param string $select SQL select criteria
1973 * @param string $order SQL order by clause to sort the records returned
1974 * @param string $limitfrom ?
1975 * @param int $limitnum ?
1976 * @param int $totalcount Passed in by reference.
1977 * @return object
1978 * @todo Finish documenting this function
1980 function get_logs($select, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount) {
1981 global $CFG;
1983 if ($order) {
1984 $order = 'ORDER BY '. $order;
1987 $selectsql = $CFG->prefix .'log l LEFT JOIN '. $CFG->prefix .'user u ON l.userid = u.id '. ((strlen($select) > 0) ? 'WHERE '. $select : '');
1988 $countsql = $CFG->prefix.'log l '.((strlen($select) > 0) ? ' WHERE '. $select : '');
1990 $totalcount = count_records_sql("SELECT COUNT(*) FROM $countsql");
1992 return get_records_sql('SELECT l.*, u.firstname, u.lastname, u.picture
1993 FROM '. $selectsql .' '. $order, $limitfrom, $limitnum) ;
1998 * Select all log records for a given course and user
2000 * @uses $CFG
2001 * @uses DAYSECS
2002 * @param int $userid The id of the user as found in the 'user' table.
2003 * @param int $courseid The id of the course as found in the 'course' table.
2004 * @param string $coursestart ?
2005 * @todo Finish documenting this function
2007 function get_logs_usercourse($userid, $courseid, $coursestart) {
2008 global $CFG;
2010 if ($courseid) {
2011 $courseselect = ' AND course = \''. $courseid .'\' ';
2012 } else {
2013 $courseselect = '';
2016 return get_records_sql("SELECT floor((time - $coursestart)/". DAYSECS .") as day, count(*) as num
2017 FROM {$CFG->prefix}log
2018 WHERE userid = '$userid'
2019 AND time > '$coursestart' $courseselect
2020 GROUP BY floor((time - $coursestart)/". DAYSECS .") ");
2024 * Select all log records for a given course, user, and day
2026 * @uses $CFG
2027 * @uses HOURSECS
2028 * @param int $userid The id of the user as found in the 'user' table.
2029 * @param int $courseid The id of the course as found in the 'course' table.
2030 * @param string $daystart ?
2031 * @return object
2032 * @todo Finish documenting this function
2034 function get_logs_userday($userid, $courseid, $daystart) {
2035 global $CFG;
2037 if ($courseid) {
2038 $courseselect = ' AND course = \''. $courseid .'\' ';
2039 } else {
2040 $courseselect = '';
2043 return get_records_sql("SELECT floor((time - $daystart)/". HOURSECS .") as hour, count(*) as num
2044 FROM {$CFG->prefix}log
2045 WHERE userid = '$userid'
2046 AND time > '$daystart' $courseselect
2047 GROUP BY floor((time - $daystart)/". HOURSECS .") ");
2051 * Returns an object with counts of failed login attempts
2053 * Returns information about failed login attempts. If the current user is
2054 * an admin, then two numbers are returned: the number of attempts and the
2055 * number of accounts. For non-admins, only the attempts on the given user
2056 * are shown.
2058 * @param string $mode Either 'admin', 'teacher' or 'everybody'
2059 * @param string $username The username we are searching for
2060 * @param string $lastlogin The date from which we are searching
2061 * @return int
2063 function count_login_failures($mode, $username, $lastlogin) {
2065 $select = 'module=\'login\' AND action=\'error\' AND time > '. $lastlogin;
2067 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Return information about all accounts
2068 if ($count->attempts = count_records_select('log', $select)) {
2069 $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)');
2070 return $count;
2072 } else if ($mode == 'everybody' or ($mode == 'teacher' and isteacherinanycourse())) {
2073 if ($count->attempts = count_records_select('log', $select .' AND info = \''. $username .'\'')) {
2074 return $count;
2077 return NULL;
2081 /// GENERAL HELPFUL THINGS ///////////////////////////////////
2084 * Dump a given object's information in a PRE block.
2086 * Mostly just used for debugging.
2088 * @param mixed $object The data to be printed
2090 function print_object($object) {
2091 echo '<pre class="notifytiny">' . htmlspecialchars(print_r($object,true)) . '</pre>';
2095 * Check whether a course is visible through its parents
2096 * path.
2098 * Notes:
2100 * - All we need from the course is ->category. _However_
2101 * if the course object has a categorypath property,
2102 * we'll save a dbquery
2104 * - If we return false, you'll still need to check if
2105 * the user can has the 'moodle/category:visibility'
2106 * capability...
2108 * - Will generate 2 DB calls.
2110 * - It does have a small local cache, however...
2112 * - Do NOT call this over many courses as it'll generate
2113 * DB traffic. Instead, see what get_my_courses() does.
2115 * @param mixed $object A course object
2116 * @return bool
2118 function course_parent_visible($course = null) {
2119 global $CFG;
2120 //return true;
2121 static $mycache;
2123 if (!is_object($course)) {
2124 return true;
2126 if (!empty($CFG->allowvisiblecoursesinhiddencategories)) {
2127 return true;
2130 if (!isset($mycache)) {
2131 $mycache = array();
2132 } else {
2133 // cast to force assoc array
2134 $k = (string)$course->category;
2135 if (isset($mycache[$k])) {
2136 return $mycache[$k];
2140 if (isset($course->categorypath)) {
2141 $path = $course->categorypath;
2142 } else {
2143 $path = get_field('course_categories', 'path',
2144 'id', $course->category);
2146 $catids = substr($path,1); // strip leading slash
2147 $catids = str_replace('/',',',$catids);
2149 $sql = "SELECT MIN(visible)
2150 FROM {$CFG->prefix}course_categories
2151 WHERE id IN ($catids)";
2152 $vis = get_field_sql($sql);
2154 // cast to force assoc array
2155 $k = (string)$course->category;
2156 $mycache[$k] = $vis;
2158 return $vis;
2162 * This function is the official hook inside XMLDB stuff to delegate its debug to one
2163 * external function.
2165 * Any script can avoid calls to this function by defining XMLDB_SKIP_DEBUG_HOOK before
2166 * using XMLDB classes. Obviously, also, if this function doesn't exist, it isn't invoked ;-)
2168 * @param $message string contains the error message
2169 * @param $object object XMLDB object that fired the debug
2171 function xmldb_debug($message, $object) {
2173 debugging($message, DEBUG_DEVELOPER);
2177 * true or false function to see if user can create any courses at all
2178 * @return bool
2180 function user_can_create_courses() {
2181 global $USER;
2182 // if user has course creation capability at any site or course cat, then return true;
2184 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
2185 return true;
2186 } else {
2187 return (bool) count(get_creatable_categories());
2193 * get the list of categories the current user can create courses in
2194 * @return array
2196 function get_creatable_categories() {
2198 $creatablecats = array();
2199 if ($cats = get_records('course_categories')) {
2200 foreach ($cats as $cat) {
2201 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
2202 $creatablecats[$cat->id] = $cat->name;
2206 return $creatablecats;
2209 // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: