MDL-63185 mod_quiz: more fixes to new behat code
[moodle.git] / lib / datalib.php
blobad64468ad48cd241591c38e8b497ff2c0759c709
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Library of functions for database manipulation.
20 * Other main libraries:
21 * - weblib.php - functions that produce web output
22 * - moodlelib.php - general-purpose Moodle functions
24 * @package core
25 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * The maximum courses in a category
33 * MAX_COURSES_IN_CATEGORY * MAX_COURSE_CATEGORIES must not be more than max integer!
35 define('MAX_COURSES_IN_CATEGORY', 10000);
37 /**
38 * The maximum number of course categories
39 * MAX_COURSES_IN_CATEGORY * MAX_COURSE_CATEGORIES must not be more than max integer!
41 define('MAX_COURSE_CATEGORIES', 10000);
43 /**
44 * Number of seconds to wait before updating lastaccess information in DB.
46 * We allow overwrites from config.php, useful to ensure coherence in performance
47 * tests results.
49 if (!defined('LASTACCESS_UPDATE_SECS')) {
50 define('LASTACCESS_UPDATE_SECS', 60);
53 /**
54 * Returns $user object of the main admin user
56 * @static stdClass $mainadmin
57 * @return stdClass {@link $USER} record from DB, false if not found
59 function get_admin() {
60 global $CFG, $DB;
62 static $mainadmin = null;
63 static $prevadmins = null;
65 if (empty($CFG->siteadmins)) {
66 // Should not happen on an ordinary site.
67 // It does however happen during unit tests.
68 return false;
71 if (isset($mainadmin) and $prevadmins === $CFG->siteadmins) {
72 return clone($mainadmin);
75 $mainadmin = null;
77 foreach (explode(',', $CFG->siteadmins) as $id) {
78 if ($user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0))) {
79 $mainadmin = $user;
80 break;
84 if ($mainadmin) {
85 $prevadmins = $CFG->siteadmins;
86 return clone($mainadmin);
87 } else {
88 // this should not happen
89 return false;
93 /**
94 * Returns list of all admins, using 1 DB query
96 * @return array
98 function get_admins() {
99 global $DB, $CFG;
101 if (empty($CFG->siteadmins)) { // Should not happen on an ordinary site
102 return array();
105 $sql = "SELECT u.*
106 FROM {user} u
107 WHERE u.deleted = 0 AND u.id IN ($CFG->siteadmins)";
109 // We want the same order as in $CFG->siteadmins.
110 $records = $DB->get_records_sql($sql);
111 $admins = array();
112 foreach (explode(',', $CFG->siteadmins) as $id) {
113 $id = (int)$id;
114 if (!isset($records[$id])) {
115 // User does not exist, this should not happen.
116 continue;
118 $admins[$records[$id]->id] = $records[$id];
121 return $admins;
125 * Search through course users
127 * If $coursid specifies the site course then this function searches
128 * through all undeleted and confirmed users
130 * @global object
131 * @uses SITEID
132 * @uses SQL_PARAMS_NAMED
133 * @uses CONTEXT_COURSE
134 * @param int $courseid The course in question.
135 * @param int $groupid The group in question.
136 * @param string $searchtext The string to search for
137 * @param string $sort A field to sort by
138 * @param array $exceptions A list of IDs to ignore, eg 2,4,5,8,9,10
139 * @return array
141 function search_users($courseid, $groupid, $searchtext, $sort='', array $exceptions=null) {
142 global $DB;
144 $fullname = $DB->sql_fullname('u.firstname', 'u.lastname');
146 if (!empty($exceptions)) {
147 list($exceptions, $params) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'ex', false);
148 $except = "AND u.id $exceptions";
149 } else {
150 $except = "";
151 $params = array();
154 if (!empty($sort)) {
155 $order = "ORDER BY $sort";
156 } else {
157 $order = "";
160 $select = "u.deleted = 0 AND u.confirmed = 1 AND (".$DB->sql_like($fullname, ':search1', false)." OR ".$DB->sql_like('u.email', ':search2', false).")";
161 $params['search1'] = "%$searchtext%";
162 $params['search2'] = "%$searchtext%";
164 if (!$courseid or $courseid == SITEID) {
165 $sql = "SELECT u.id, u.firstname, u.lastname, u.email
166 FROM {user} u
167 WHERE $select
168 $except
169 $order";
170 return $DB->get_records_sql($sql, $params);
172 } else {
173 if ($groupid) {
174 $sql = "SELECT u.id, u.firstname, u.lastname, u.email
175 FROM {user} u
176 JOIN {groups_members} gm ON gm.userid = u.id
177 WHERE $select AND gm.groupid = :groupid
178 $except
179 $order";
180 $params['groupid'] = $groupid;
181 return $DB->get_records_sql($sql, $params);
183 } else {
184 $context = context_course::instance($courseid);
186 // We want to query both the current context and parent contexts.
187 list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
189 $sql = "SELECT u.id, u.firstname, u.lastname, u.email
190 FROM {user} u
191 JOIN {role_assignments} ra ON ra.userid = u.id
192 WHERE $select AND ra.contextid $relatedctxsql
193 $except
194 $order";
195 $params = array_merge($params, $relatedctxparams);
196 return $DB->get_records_sql($sql, $params);
202 * Returns SQL used to search through user table to find users (in a query
203 * which may also join and apply other conditions).
205 * You can combine this SQL with an existing query by adding 'AND $sql' to the
206 * WHERE clause of your query (where $sql is the first element in the array
207 * returned by this function), and merging in the $params array to the parameters
208 * of your query (where $params is the second element). Your query should use
209 * named parameters such as :param, rather than the question mark style.
211 * There are examples of basic usage in the unit test for this function.
213 * @param string $search the text to search for (empty string = find all)
214 * @param string $u the table alias for the user table in the query being
215 * built. May be ''.
216 * @param bool $searchanywhere If true (default), searches in the middle of
217 * names, otherwise only searches at start
218 * @param array $extrafields Array of extra user fields to include in search
219 * @param array $exclude Array of user ids to exclude (empty = don't exclude)
220 * @param array $includeonly If specified, only returns users that have ids
221 * incldued in this array (empty = don't restrict)
222 * @return array an array with two elements, a fragment of SQL to go in the
223 * where clause the query, and an associative array containing any required
224 * parameters (using named placeholders).
226 function users_search_sql($search, $u = 'u', $searchanywhere = true, array $extrafields = array(),
227 array $exclude = null, array $includeonly = null) {
228 global $DB, $CFG;
229 $params = array();
230 $tests = array();
232 if ($u) {
233 $u .= '.';
236 // If we have a $search string, put a field LIKE '$search%' condition on each field.
237 if ($search) {
238 $conditions = array(
239 $DB->sql_fullname($u . 'firstname', $u . 'lastname'),
240 $conditions[] = $u . 'lastname'
242 foreach ($extrafields as $field) {
243 $conditions[] = $u . $field;
245 if ($searchanywhere) {
246 $searchparam = '%' . $search . '%';
247 } else {
248 $searchparam = $search . '%';
250 $i = 0;
251 foreach ($conditions as $key => $condition) {
252 $conditions[$key] = $DB->sql_like($condition, ":con{$i}00", false, false);
253 $params["con{$i}00"] = $searchparam;
254 $i++;
256 $tests[] = '(' . implode(' OR ', $conditions) . ')';
259 // Add some additional sensible conditions.
260 $tests[] = $u . "id <> :guestid";
261 $params['guestid'] = $CFG->siteguest;
262 $tests[] = $u . 'deleted = 0';
263 $tests[] = $u . 'confirmed = 1';
265 // If we are being asked to exclude any users, do that.
266 if (!empty($exclude)) {
267 list($usertest, $userparams) = $DB->get_in_or_equal($exclude, SQL_PARAMS_NAMED, 'ex', false);
268 $tests[] = $u . 'id ' . $usertest;
269 $params = array_merge($params, $userparams);
272 // If we are validating a set list of userids, add an id IN (...) test.
273 if (!empty($includeonly)) {
274 list($usertest, $userparams) = $DB->get_in_or_equal($includeonly, SQL_PARAMS_NAMED, 'val');
275 $tests[] = $u . 'id ' . $usertest;
276 $params = array_merge($params, $userparams);
279 // In case there are no tests, add one result (this makes it easier to combine
280 // this with an existing query as you can always add AND $sql).
281 if (empty($tests)) {
282 $tests[] = '1 = 1';
285 // Combing the conditions and return.
286 return array(implode(' AND ', $tests), $params);
291 * This function generates the standard ORDER BY clause for use when generating
292 * lists of users. If you don't have a reason to use a different order, then
293 * you should use this method to generate the order when displaying lists of users.
295 * If the optional $search parameter is passed, then exact matches to the search
296 * will be sorted first. For example, suppose you have two users 'Al Zebra' and
297 * 'Alan Aardvark'. The default sort is Alan, then Al. If, however, you search for
298 * 'Al', then Al will be listed first. (With two users, this is not a big deal,
299 * but with thousands of users, it is essential.)
301 * The list of fields scanned for exact matches are:
302 * - firstname
303 * - lastname
304 * - $DB->sql_fullname
305 * - those returned by get_extra_user_fields
307 * If named parameters are used (which is the default, and highly recommended),
308 * then the parameter names are like :usersortexactN, where N is an int.
310 * The simplest possible example use is:
311 * list($sort, $params) = users_order_by_sql();
312 * $sql = 'SELECT * FROM {users} ORDER BY ' . $sort;
314 * A more complex example, showing that this sort can be combined with other sorts:
315 * list($sort, $sortparams) = users_order_by_sql('u');
316 * $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username
317 * FROM {groups} g
318 * LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
319 * LEFT JOIN {groups_members} gm ON g.id = gm.groupid
320 * LEFT JOIN {user} u ON gm.userid = u.id
321 * WHERE g.courseid = :courseid $groupwhere $groupingwhere
322 * ORDER BY g.name, $sort";
323 * $params += $sortparams;
325 * An example showing the use of $search:
326 * list($sort, $sortparams) = users_order_by_sql('u', $search, $this->get_context());
327 * $order = ' ORDER BY ' . $sort;
328 * $params += $sortparams;
329 * $availableusers = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
331 * @param string $usertablealias (optional) any table prefix for the {users} table. E.g. 'u'.
332 * @param string $search (optional) a current search string. If given,
333 * any exact matches to this string will be sorted first.
334 * @param context $context the context we are in. Use by get_extra_user_fields.
335 * Defaults to $PAGE->context.
336 * @return array with two elements:
337 * string SQL fragment to use in the ORDER BY clause. For example, "firstname, lastname".
338 * array of parameters used in the SQL fragment.
340 function users_order_by_sql($usertablealias = '', $search = null, context $context = null) {
341 global $DB, $PAGE;
343 if ($usertablealias) {
344 $tableprefix = $usertablealias . '.';
345 } else {
346 $tableprefix = '';
349 $sort = "{$tableprefix}lastname, {$tableprefix}firstname, {$tableprefix}id";
350 $params = array();
352 if (!$search) {
353 return array($sort, $params);
356 if (!$context) {
357 $context = $PAGE->context;
360 $exactconditions = array();
361 $paramkey = 'usersortexact1';
363 $exactconditions[] = $DB->sql_fullname($tableprefix . 'firstname', $tableprefix . 'lastname') .
364 ' = :' . $paramkey;
365 $params[$paramkey] = $search;
366 $paramkey++;
368 $fieldstocheck = array_merge(array('firstname', 'lastname'), get_extra_user_fields($context));
369 foreach ($fieldstocheck as $key => $field) {
370 $exactconditions[] = 'LOWER(' . $tableprefix . $field . ') = LOWER(:' . $paramkey . ')';
371 $params[$paramkey] = $search;
372 $paramkey++;
375 $sort = 'CASE WHEN ' . implode(' OR ', $exactconditions) .
376 ' THEN 0 ELSE 1 END, ' . $sort;
378 return array($sort, $params);
382 * Returns a subset of users
384 * @global object
385 * @uses DEBUG_DEVELOPER
386 * @uses SQL_PARAMS_NAMED
387 * @param bool $get If false then only a count of the records is returned
388 * @param string $search A simple string to search for
389 * @param bool $confirmed A switch to allow/disallow unconfirmed users
390 * @param array $exceptions A list of IDs to ignore, eg 2,4,5,8,9,10
391 * @param string $sort A SQL snippet for the sorting criteria to use
392 * @param string $firstinitial Users whose first name starts with $firstinitial
393 * @param string $lastinitial Users whose last name starts with $lastinitial
394 * @param string $page The page or records to return
395 * @param string $recordsperpage The number of records to return per page
396 * @param string $fields A comma separated list of fields to be returned from the chosen table.
397 * @return array|int|bool {@link $USER} records unless get is false in which case the integer count of the records found is returned.
398 * False is returned if an error is encountered.
400 function get_users($get=true, $search='', $confirmed=false, array $exceptions=null, $sort='firstname ASC',
401 $firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*', $extraselect='', array $extraparams=null) {
402 global $DB, $CFG;
404 if ($get && !$recordsperpage) {
405 debugging('Call to get_users with $get = true no $recordsperpage limit. ' .
406 'On large installations, this will probably cause an out of memory error. ' .
407 'Please think again and change your code so that it does not try to ' .
408 'load so much data into memory.', DEBUG_DEVELOPER);
411 $fullname = $DB->sql_fullname();
413 $select = " id <> :guestid AND deleted = 0";
414 $params = array('guestid'=>$CFG->siteguest);
416 if (!empty($search)){
417 $search = trim($search);
418 $select .= " AND (".$DB->sql_like($fullname, ':search1', false)." OR ".$DB->sql_like('email', ':search2', false)." OR username = :search3)";
419 $params['search1'] = "%$search%";
420 $params['search2'] = "%$search%";
421 $params['search3'] = "$search";
424 if ($confirmed) {
425 $select .= " AND confirmed = 1";
428 if ($exceptions) {
429 list($exceptions, $eparams) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'ex', false);
430 $params = $params + $eparams;
431 $select .= " AND id $exceptions";
434 if ($firstinitial) {
435 $select .= " AND ".$DB->sql_like('firstname', ':fni', false, false);
436 $params['fni'] = "$firstinitial%";
438 if ($lastinitial) {
439 $select .= " AND ".$DB->sql_like('lastname', ':lni', false, false);
440 $params['lni'] = "$lastinitial%";
443 if ($extraselect) {
444 $select .= " AND $extraselect";
445 $params = $params + (array)$extraparams;
448 if ($get) {
449 return $DB->get_records_select('user', $select, $params, $sort, $fields, $page, $recordsperpage);
450 } else {
451 return $DB->count_records_select('user', $select, $params);
457 * Return filtered (if provided) list of users in site, except guest and deleted users.
459 * @param string $sort An SQL field to sort by
460 * @param string $dir The sort direction ASC|DESC
461 * @param int $page The page or records to return
462 * @param int $recordsperpage The number of records to return per page
463 * @param string $search A simple string to search for
464 * @param string $firstinitial Users whose first name starts with $firstinitial
465 * @param string $lastinitial Users whose last name starts with $lastinitial
466 * @param string $extraselect An additional SQL select statement to append to the query
467 * @param array $extraparams Additional parameters to use for the above $extraselect
468 * @param stdClass $extracontext If specified, will include user 'extra fields'
469 * as appropriate for current user and given context
470 * @return array Array of {@link $USER} records
472 function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0,
473 $search='', $firstinitial='', $lastinitial='', $extraselect='',
474 array $extraparams=null, $extracontext = null) {
475 global $DB, $CFG;
477 $fullname = $DB->sql_fullname();
479 $select = "deleted <> 1 AND id <> :guestid";
480 $params = array('guestid' => $CFG->siteguest);
482 if (!empty($search)) {
483 $search = trim($search);
484 $select .= " AND (". $DB->sql_like($fullname, ':search1', false, false).
485 " OR ". $DB->sql_like('email', ':search2', false, false).
486 " OR username = :search3)";
487 $params['search1'] = "%$search%";
488 $params['search2'] = "%$search%";
489 $params['search3'] = "$search";
492 if ($firstinitial) {
493 $select .= " AND ". $DB->sql_like('firstname', ':fni', false, false);
494 $params['fni'] = "$firstinitial%";
496 if ($lastinitial) {
497 $select .= " AND ". $DB->sql_like('lastname', ':lni', false, false);
498 $params['lni'] = "$lastinitial%";
501 if ($extraselect) {
502 $select .= " AND $extraselect";
503 $params = $params + (array)$extraparams;
506 if ($sort) {
507 $sort = " ORDER BY $sort $dir";
510 // If a context is specified, get extra user fields that the current user
511 // is supposed to see.
512 $extrafields = '';
513 if ($extracontext) {
514 $extrafields = get_extra_user_fields_sql($extracontext, '', '',
515 array('id', 'username', 'email', 'firstname', 'lastname', 'city', 'country',
516 'lastaccess', 'confirmed', 'mnethostid'));
518 $namefields = get_all_user_name_fields(true);
519 $extrafields = "$extrafields, $namefields";
521 // warning: will return UNCONFIRMED USERS
522 return $DB->get_records_sql("SELECT id, username, email, city, country, lastaccess, confirmed, mnethostid, suspended $extrafields
523 FROM {user}
524 WHERE $select
525 $sort", $params, $page, $recordsperpage);
531 * Full list of users that have confirmed their accounts.
533 * @global object
534 * @return array of unconfirmed users
536 function get_users_confirmed() {
537 global $DB, $CFG;
538 return $DB->get_records_sql("SELECT *
539 FROM {user}
540 WHERE confirmed = 1 AND deleted = 0 AND id <> ?", array($CFG->siteguest));
544 /// OTHER SITE AND COURSE FUNCTIONS /////////////////////////////////////////////
548 * Returns $course object of the top-level site.
550 * @return object A {@link $COURSE} object for the site, exception if not found
552 function get_site() {
553 global $SITE, $DB;
555 if (!empty($SITE->id)) { // We already have a global to use, so return that
556 return $SITE;
559 if ($course = $DB->get_record('course', array('category'=>0))) {
560 return $course;
561 } else {
562 // course table exists, but the site is not there,
563 // unfortunately there is no automatic way to recover
564 throw new moodle_exception('nosite', 'error');
569 * Gets a course object from database. If the course id corresponds to an
570 * already-loaded $COURSE or $SITE object, then the loaded object will be used,
571 * saving a database query.
573 * If it reuses an existing object, by default the object will be cloned. This
574 * means you can modify the object safely without affecting other code.
576 * @param int $courseid Course id
577 * @param bool $clone If true (default), makes a clone of the record
578 * @return stdClass A course object
579 * @throws dml_exception If not found in database
581 function get_course($courseid, $clone = true) {
582 global $DB, $COURSE, $SITE;
583 if (!empty($COURSE->id) && $COURSE->id == $courseid) {
584 return $clone ? clone($COURSE) : $COURSE;
585 } else if (!empty($SITE->id) && $SITE->id == $courseid) {
586 return $clone ? clone($SITE) : $SITE;
587 } else {
588 return $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
593 * Returns list of courses, for whole site, or category
595 * Returns list of courses, for whole site, or category
596 * Important: Using c.* for fields is extremely expensive because
597 * we are using distinct. You almost _NEVER_ need all the fields
598 * in such a large SELECT
600 * @global object
601 * @global object
602 * @global object
603 * @uses CONTEXT_COURSE
604 * @param string|int $categoryid Either a category id or 'all' for everything
605 * @param string $sort A field and direction to sort by
606 * @param string $fields The additional fields to return
607 * @return array Array of courses
609 function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") {
611 global $USER, $CFG, $DB;
613 $params = array();
615 if ($categoryid !== "all" && is_numeric($categoryid)) {
616 $categoryselect = "WHERE c.category = :catid";
617 $params['catid'] = $categoryid;
618 } else {
619 $categoryselect = "";
622 if (empty($sort)) {
623 $sortstatement = "";
624 } else {
625 $sortstatement = "ORDER BY $sort";
628 $visiblecourses = array();
630 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
631 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
632 $params['contextlevel'] = CONTEXT_COURSE;
634 $sql = "SELECT $fields $ccselect
635 FROM {course} c
636 $ccjoin
637 $categoryselect
638 $sortstatement";
640 // pull out all course matching the cat
641 if ($courses = $DB->get_records_sql($sql, $params)) {
643 // loop throught them
644 foreach ($courses as $course) {
645 context_helper::preload_from_record($course);
646 if (isset($course->visible) && $course->visible <= 0) {
647 // for hidden courses, require visibility check
648 if (has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
649 $visiblecourses [$course->id] = $course;
651 } else {
652 $visiblecourses [$course->id] = $course;
656 return $visiblecourses;
661 * Returns list of courses, for whole site, or category
663 * Similar to get_courses, but allows paging
664 * Important: Using c.* for fields is extremely expensive because
665 * we are using distinct. You almost _NEVER_ need all the fields
666 * in such a large SELECT
668 * @global object
669 * @global object
670 * @global object
671 * @uses CONTEXT_COURSE
672 * @param string|int $categoryid Either a category id or 'all' for everything
673 * @param string $sort A field and direction to sort by
674 * @param string $fields The additional fields to return
675 * @param int $totalcount Reference for the number of courses
676 * @param string $limitfrom The course to start from
677 * @param string $limitnum The number of courses to limit to
678 * @return array Array of courses
680 function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*",
681 &$totalcount, $limitfrom="", $limitnum="") {
682 global $USER, $CFG, $DB;
684 $params = array();
686 $categoryselect = "";
687 if ($categoryid !== "all" && is_numeric($categoryid)) {
688 $categoryselect = "WHERE c.category = :catid";
689 $params['catid'] = $categoryid;
690 } else {
691 $categoryselect = "";
694 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
695 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
696 $params['contextlevel'] = CONTEXT_COURSE;
698 $totalcount = 0;
699 if (!$limitfrom) {
700 $limitfrom = 0;
702 $visiblecourses = array();
704 $sql = "SELECT $fields $ccselect
705 FROM {course} c
706 $ccjoin
707 $categoryselect
708 ORDER BY $sort";
710 // pull out all course matching the cat
711 $rs = $DB->get_recordset_sql($sql, $params);
712 // iteration will have to be done inside loop to keep track of the limitfrom and limitnum
713 foreach($rs as $course) {
714 context_helper::preload_from_record($course);
715 if ($course->visible <= 0) {
716 // for hidden courses, require visibility check
717 if (has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
718 $totalcount++;
719 if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
720 $visiblecourses [$course->id] = $course;
723 } else {
724 $totalcount++;
725 if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
726 $visiblecourses [$course->id] = $course;
730 $rs->close();
731 return $visiblecourses;
735 * A list of courses that match a search
737 * @global object
738 * @global object
739 * @param array $searchterms An array of search criteria
740 * @param string $sort A field and direction to sort by
741 * @param int $page The page number to get
742 * @param int $recordsperpage The number of records per page
743 * @param int $totalcount Passed in by reference.
744 * @param array $requiredcapabilities Extra list of capabilities used to filter courses
745 * @return object {@link $COURSE} records
747 function get_courses_search($searchterms, $sort, $page, $recordsperpage, &$totalcount,
748 $requiredcapabilities = array()) {
749 global $CFG, $DB;
751 if ($DB->sql_regex_supported()) {
752 $REGEXP = $DB->sql_regex(true);
753 $NOTREGEXP = $DB->sql_regex(false);
756 $searchcond = array();
757 $params = array();
758 $i = 0;
760 // Thanks Oracle for your non-ansi concat and type limits in coalesce. MDL-29912
761 if ($DB->get_dbfamily() == 'oracle') {
762 $concat = "(c.summary|| ' ' || c.fullname || ' ' || c.idnumber || ' ' || c.shortname)";
763 } else {
764 $concat = $DB->sql_concat("COALESCE(c.summary, '')", "' '", 'c.fullname', "' '", 'c.idnumber', "' '", 'c.shortname');
767 foreach ($searchterms as $searchterm) {
768 $i++;
770 $NOT = false; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
771 /// will use it to simulate the "-" operator with LIKE clause
773 /// Under Oracle and MSSQL, trim the + and - operators and perform
774 /// simpler LIKE (or NOT LIKE) queries
775 if (!$DB->sql_regex_supported()) {
776 if (substr($searchterm, 0, 1) == '-') {
777 $NOT = true;
779 $searchterm = trim($searchterm, '+-');
782 // TODO: +- may not work for non latin languages
784 if (substr($searchterm,0,1) == '+') {
785 $searchterm = trim($searchterm, '+-');
786 $searchterm = preg_quote($searchterm, '|');
787 $searchcond[] = "$concat $REGEXP :ss$i";
788 $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
790 } else if ((substr($searchterm,0,1) == "-") && (core_text::strlen($searchterm) > 1)) {
791 $searchterm = trim($searchterm, '+-');
792 $searchterm = preg_quote($searchterm, '|');
793 $searchcond[] = "$concat $NOTREGEXP :ss$i";
794 $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
796 } else {
797 $searchcond[] = $DB->sql_like($concat,":ss$i", false, true, $NOT);
798 $params['ss'.$i] = "%$searchterm%";
802 if (empty($searchcond)) {
803 $searchcond = array('1 = 1');
806 $searchcond = implode(" AND ", $searchcond);
808 $courses = array();
809 $c = 0; // counts how many visible courses we've seen
811 // Tiki pagination
812 $limitfrom = $page * $recordsperpage;
813 $limitto = $limitfrom + $recordsperpage;
815 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
816 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
817 $params['contextlevel'] = CONTEXT_COURSE;
819 $sql = "SELECT c.* $ccselect
820 FROM {course} c
821 $ccjoin
822 WHERE $searchcond AND c.id <> ".SITEID."
823 ORDER BY $sort";
825 $rs = $DB->get_recordset_sql($sql, $params);
826 foreach($rs as $course) {
827 // Preload contexts only for hidden courses or courses we need to return.
828 context_helper::preload_from_record($course);
829 $coursecontext = context_course::instance($course->id);
830 if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
831 continue;
833 if (!empty($requiredcapabilities)) {
834 if (!has_all_capabilities($requiredcapabilities, $coursecontext)) {
835 continue;
838 // Don't exit this loop till the end
839 // we need to count all the visible courses
840 // to update $totalcount
841 if ($c >= $limitfrom && $c < $limitto) {
842 $courses[$course->id] = $course;
844 $c++;
846 $rs->close();
848 // our caller expects 2 bits of data - our return
849 // array, and an updated $totalcount
850 $totalcount = $c;
851 return $courses;
855 * Fixes course category and course sortorder, also verifies category and course parents and paths.
856 * (circular references are not fixed)
858 * @global object
859 * @global object
860 * @uses MAX_COURSES_IN_CATEGORY
861 * @uses MAX_COURSE_CATEGORIES
862 * @uses SITEID
863 * @uses CONTEXT_COURSE
864 * @return void
866 function fix_course_sortorder() {
867 global $DB, $SITE;
869 //WARNING: this is PHP5 only code!
871 // if there are any changes made to courses or categories we will trigger
872 // the cache events to purge all cached courses/categories data
873 $cacheevents = array();
875 if ($unsorted = $DB->get_records('course_categories', array('sortorder'=>0))) {
876 //move all categories that are not sorted yet to the end
877 $DB->set_field('course_categories', 'sortorder', MAX_COURSES_IN_CATEGORY*MAX_COURSE_CATEGORIES, array('sortorder'=>0));
878 $cacheevents['changesincoursecat'] = true;
881 $allcats = $DB->get_records('course_categories', null, 'sortorder, id', 'id, sortorder, parent, depth, path');
882 $topcats = array();
883 $brokencats = array();
884 foreach ($allcats as $cat) {
885 $sortorder = (int)$cat->sortorder;
886 if (!$cat->parent) {
887 while(isset($topcats[$sortorder])) {
888 $sortorder++;
890 $topcats[$sortorder] = $cat;
891 continue;
893 if (!isset($allcats[$cat->parent])) {
894 $brokencats[] = $cat;
895 continue;
897 if (!isset($allcats[$cat->parent]->children)) {
898 $allcats[$cat->parent]->children = array();
900 while(isset($allcats[$cat->parent]->children[$sortorder])) {
901 $sortorder++;
903 $allcats[$cat->parent]->children[$sortorder] = $cat;
905 unset($allcats);
907 // add broken cats to category tree
908 if ($brokencats) {
909 $defaultcat = reset($topcats);
910 foreach ($brokencats as $cat) {
911 $topcats[] = $cat;
915 // now walk recursively the tree and fix any problems found
916 $sortorder = 0;
917 $fixcontexts = array();
918 if (_fix_course_cats($topcats, $sortorder, 0, 0, '', $fixcontexts)) {
919 $cacheevents['changesincoursecat'] = true;
922 // detect if there are "multiple" frontpage courses and fix them if needed
923 $frontcourses = $DB->get_records('course', array('category'=>0), 'id');
924 if (count($frontcourses) > 1) {
925 if (isset($frontcourses[SITEID])) {
926 $frontcourse = $frontcourses[SITEID];
927 unset($frontcourses[SITEID]);
928 } else {
929 $frontcourse = array_shift($frontcourses);
931 $defaultcat = reset($topcats);
932 foreach ($frontcourses as $course) {
933 $DB->set_field('course', 'category', $defaultcat->id, array('id'=>$course->id));
934 $context = context_course::instance($course->id);
935 $fixcontexts[$context->id] = $context;
936 $cacheevents['changesincourse'] = true;
938 unset($frontcourses);
939 } else {
940 $frontcourse = reset($frontcourses);
943 // now fix the paths and depths in context table if needed
944 if ($fixcontexts) {
945 foreach ($fixcontexts as $fixcontext) {
946 $fixcontext->reset_paths(false);
948 context_helper::build_all_paths(false);
949 unset($fixcontexts);
950 $cacheevents['changesincourse'] = true;
951 $cacheevents['changesincoursecat'] = true;
954 // release memory
955 unset($topcats);
956 unset($brokencats);
957 unset($fixcontexts);
959 // fix frontpage course sortorder
960 if ($frontcourse->sortorder != 1) {
961 $DB->set_field('course', 'sortorder', 1, array('id'=>$frontcourse->id));
962 $cacheevents['changesincourse'] = true;
965 // now fix the course counts in category records if needed
966 $sql = "SELECT cc.id, cc.coursecount, COUNT(c.id) AS newcount
967 FROM {course_categories} cc
968 LEFT JOIN {course} c ON c.category = cc.id
969 GROUP BY cc.id, cc.coursecount
970 HAVING cc.coursecount <> COUNT(c.id)";
972 if ($updatecounts = $DB->get_records_sql($sql)) {
973 // categories with more courses than MAX_COURSES_IN_CATEGORY
974 $categories = array();
975 foreach ($updatecounts as $cat) {
976 $cat->coursecount = $cat->newcount;
977 if ($cat->coursecount >= MAX_COURSES_IN_CATEGORY) {
978 $categories[] = $cat->id;
980 unset($cat->newcount);
981 $DB->update_record_raw('course_categories', $cat, true);
983 if (!empty($categories)) {
984 $str = implode(', ', $categories);
985 debugging("The number of courses (category id: $str) has reached MAX_COURSES_IN_CATEGORY (" . MAX_COURSES_IN_CATEGORY . "), it will cause a sorting performance issue, please increase the value of MAX_COURSES_IN_CATEGORY in lib/datalib.php file. See tracker issue: MDL-25669", DEBUG_DEVELOPER);
987 $cacheevents['changesincoursecat'] = true;
990 // now make sure that sortorders in course table are withing the category sortorder ranges
991 $sql = "SELECT DISTINCT cc.id, cc.sortorder
992 FROM {course_categories} cc
993 JOIN {course} c ON c.category = cc.id
994 WHERE c.sortorder < cc.sortorder OR c.sortorder > cc.sortorder + ".MAX_COURSES_IN_CATEGORY;
996 if ($fixcategories = $DB->get_records_sql($sql)) {
997 //fix the course sortorder ranges
998 foreach ($fixcategories as $cat) {
999 $sql = "UPDATE {course}
1000 SET sortorder = ".$DB->sql_modulo('sortorder', MAX_COURSES_IN_CATEGORY)." + ?
1001 WHERE category = ?";
1002 $DB->execute($sql, array($cat->sortorder, $cat->id));
1004 $cacheevents['changesincoursecat'] = true;
1006 unset($fixcategories);
1008 // categories having courses with sortorder duplicates or having gaps in sortorder
1009 $sql = "SELECT DISTINCT c1.category AS id , cc.sortorder
1010 FROM {course} c1
1011 JOIN {course} c2 ON c1.sortorder = c2.sortorder
1012 JOIN {course_categories} cc ON (c1.category = cc.id)
1013 WHERE c1.id <> c2.id";
1014 $fixcategories = $DB->get_records_sql($sql);
1016 $sql = "SELECT cc.id, cc.sortorder, cc.coursecount, MAX(c.sortorder) AS maxsort, MIN(c.sortorder) AS minsort
1017 FROM {course_categories} cc
1018 JOIN {course} c ON c.category = cc.id
1019 GROUP BY cc.id, cc.sortorder, cc.coursecount
1020 HAVING (MAX(c.sortorder) <> cc.sortorder + cc.coursecount) OR (MIN(c.sortorder) <> cc.sortorder + 1)";
1021 $gapcategories = $DB->get_records_sql($sql);
1023 foreach ($gapcategories as $cat) {
1024 if (isset($fixcategories[$cat->id])) {
1025 // duplicates detected already
1027 } else if ($cat->minsort == $cat->sortorder and $cat->maxsort == $cat->sortorder + $cat->coursecount - 1) {
1028 // easy - new course inserted with sortorder 0, the rest is ok
1029 $sql = "UPDATE {course}
1030 SET sortorder = sortorder + 1
1031 WHERE category = ?";
1032 $DB->execute($sql, array($cat->id));
1034 } else {
1035 // it needs full resorting
1036 $fixcategories[$cat->id] = $cat;
1038 $cacheevents['changesincourse'] = true;
1040 unset($gapcategories);
1042 // fix course sortorders in problematic categories only
1043 foreach ($fixcategories as $cat) {
1044 $i = 1;
1045 $courses = $DB->get_records('course', array('category'=>$cat->id), 'sortorder ASC, id DESC', 'id, sortorder');
1046 foreach ($courses as $course) {
1047 if ($course->sortorder != $cat->sortorder + $i) {
1048 $course->sortorder = $cat->sortorder + $i;
1049 $DB->update_record_raw('course', $course, true);
1050 $cacheevents['changesincourse'] = true;
1052 $i++;
1056 // advise all caches that need to be rebuilt
1057 foreach (array_keys($cacheevents) as $event) {
1058 cache_helper::purge_by_event($event);
1063 * Internal recursive category verification function, do not use directly!
1065 * @todo Document the arguments of this function better
1067 * @global object
1068 * @uses MAX_COURSES_IN_CATEGORY
1069 * @uses CONTEXT_COURSECAT
1070 * @param array $children
1071 * @param int $sortorder
1072 * @param string $parent
1073 * @param int $depth
1074 * @param string $path
1075 * @param array $fixcontexts
1076 * @return bool if changes were made
1078 function _fix_course_cats($children, &$sortorder, $parent, $depth, $path, &$fixcontexts) {
1079 global $DB;
1081 $depth++;
1082 $changesmade = false;
1084 foreach ($children as $cat) {
1085 $sortorder = $sortorder + MAX_COURSES_IN_CATEGORY;
1086 $update = false;
1087 if ($parent != $cat->parent or $depth != $cat->depth or $path.'/'.$cat->id != $cat->path) {
1088 $cat->parent = $parent;
1089 $cat->depth = $depth;
1090 $cat->path = $path.'/'.$cat->id;
1091 $update = true;
1093 // make sure context caches are rebuild and dirty contexts marked
1094 $context = context_coursecat::instance($cat->id);
1095 $fixcontexts[$context->id] = $context;
1097 if ($cat->sortorder != $sortorder) {
1098 $cat->sortorder = $sortorder;
1099 $update = true;
1101 if ($update) {
1102 $DB->update_record('course_categories', $cat, true);
1103 $changesmade = true;
1105 if (isset($cat->children)) {
1106 if (_fix_course_cats($cat->children, $sortorder, $cat->id, $cat->depth, $cat->path, $fixcontexts)) {
1107 $changesmade = true;
1111 return $changesmade;
1115 * List of remote courses that a user has access to via MNET.
1116 * Works only on the IDP
1118 * @global object
1119 * @global object
1120 * @param int @userid The user id to get remote courses for
1121 * @return array Array of {@link $COURSE} of course objects
1123 function get_my_remotecourses($userid=0) {
1124 global $DB, $USER;
1126 if (empty($userid)) {
1127 $userid = $USER->id;
1130 // we can not use SELECT DISTINCT + text field (summary) because of MS SQL and Oracle, subselect used therefore
1131 $sql = "SELECT c.id, c.remoteid, c.shortname, c.fullname,
1132 c.hostid, c.summary, c.summaryformat, c.categoryname AS cat_name,
1133 h.name AS hostname
1134 FROM {mnetservice_enrol_courses} c
1135 JOIN (SELECT DISTINCT hostid, remotecourseid
1136 FROM {mnetservice_enrol_enrolments}
1137 WHERE userid = ?
1138 ) e ON (e.hostid = c.hostid AND e.remotecourseid = c.remoteid)
1139 JOIN {mnet_host} h ON h.id = c.hostid";
1141 return $DB->get_records_sql($sql, array($userid));
1145 * List of remote hosts that a user has access to via MNET.
1146 * Works on the SP
1148 * @global object
1149 * @global object
1150 * @return array|bool Array of host objects or false
1152 function get_my_remotehosts() {
1153 global $CFG, $USER;
1155 if ($USER->mnethostid == $CFG->mnet_localhost_id) {
1156 return false; // Return nothing on the IDP
1158 if (!empty($USER->mnet_foreign_host_array) && is_array($USER->mnet_foreign_host_array)) {
1159 return $USER->mnet_foreign_host_array;
1161 return false;
1166 * Returns a menu of all available scales from the site as well as the given course
1168 * @global object
1169 * @param int $courseid The id of the course as found in the 'course' table.
1170 * @return array
1172 function get_scales_menu($courseid=0) {
1173 global $DB;
1175 $sql = "SELECT id, name, courseid
1176 FROM {scale}
1177 WHERE courseid = 0 or courseid = ?
1178 ORDER BY courseid ASC, name ASC";
1179 $params = array($courseid);
1180 $scales = array();
1181 $results = $DB->get_records_sql($sql, $params);
1182 foreach ($results as $index => $record) {
1183 $context = empty($record->courseid) ? context_system::instance() : context_course::instance($record->courseid);
1184 $scales[$index] = format_string($record->name, false, ["context" => $context]);
1186 // Format: [id => 'scale name'].
1187 return $scales;
1191 * Increment standard revision field.
1193 * The revision are based on current time and are incrementing.
1194 * There is a protection for runaway revisions, it may not go further than
1195 * one hour into future.
1197 * The field has to be XMLDB_TYPE_INTEGER with size 10.
1199 * @param string $table
1200 * @param string $field name of the field containing revision
1201 * @param string $select use empty string when updating all records
1202 * @param array $params optional select parameters
1204 function increment_revision_number($table, $field, $select, array $params = null) {
1205 global $DB;
1207 $now = time();
1208 $sql = "UPDATE {{$table}}
1209 SET $field = (CASE
1210 WHEN $field IS NULL THEN $now
1211 WHEN $field < $now THEN $now
1212 WHEN $field > $now + 3600 THEN $now
1213 ELSE $field + 1 END)";
1214 if ($select) {
1215 $sql = $sql . " WHERE $select";
1217 $DB->execute($sql, $params);
1221 /// MODULE FUNCTIONS /////////////////////////////////////////////////
1224 * Just gets a raw list of all modules in a course
1226 * @global object
1227 * @param int $courseid The id of the course as found in the 'course' table.
1228 * @return array
1230 function get_course_mods($courseid) {
1231 global $DB;
1233 if (empty($courseid)) {
1234 return false; // avoid warnings
1237 return $DB->get_records_sql("SELECT cm.*, m.name as modname
1238 FROM {modules} m, {course_modules} cm
1239 WHERE cm.course = ? AND cm.module = m.id AND m.visible = 1",
1240 array($courseid)); // no disabled mods
1245 * Given an id of a course module, finds the coursemodule description
1247 * Please note that this function performs 1-2 DB queries. When possible use cached
1248 * course modinfo. For example get_fast_modinfo($courseorid)->get_cm($cmid)
1249 * See also {@link cm_info::get_course_module_record()}
1251 * @global object
1252 * @param string $modulename name of module type, eg. resource, assignment,... (optional, slower and less safe if not specified)
1253 * @param int $cmid course module id (id in course_modules table)
1254 * @param int $courseid optional course id for extra validation
1255 * @param bool $sectionnum include relative section number (0,1,2 ...)
1256 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1257 * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1258 * MUST_EXIST means throw exception if no record or multiple records found
1259 * @return stdClass
1261 function get_coursemodule_from_id($modulename, $cmid, $courseid=0, $sectionnum=false, $strictness=IGNORE_MISSING) {
1262 global $DB;
1264 $params = array('cmid'=>$cmid);
1266 if (!$modulename) {
1267 if (!$modulename = $DB->get_field_sql("SELECT md.name
1268 FROM {modules} md
1269 JOIN {course_modules} cm ON cm.module = md.id
1270 WHERE cm.id = :cmid", $params, $strictness)) {
1271 return false;
1273 } else {
1274 if (!core_component::is_valid_plugin_name('mod', $modulename)) {
1275 throw new coding_exception('Invalid modulename parameter');
1279 $params['modulename'] = $modulename;
1281 $courseselect = "";
1282 $sectionfield = "";
1283 $sectionjoin = "";
1285 if ($courseid) {
1286 $courseselect = "AND cm.course = :courseid";
1287 $params['courseid'] = $courseid;
1290 if ($sectionnum) {
1291 $sectionfield = ", cw.section AS sectionnum";
1292 $sectionjoin = "LEFT JOIN {course_sections} cw ON cw.id = cm.section";
1295 $sql = "SELECT cm.*, m.name, md.name AS modname $sectionfield
1296 FROM {course_modules} cm
1297 JOIN {modules} md ON md.id = cm.module
1298 JOIN {".$modulename."} m ON m.id = cm.instance
1299 $sectionjoin
1300 WHERE cm.id = :cmid AND md.name = :modulename
1301 $courseselect";
1303 return $DB->get_record_sql($sql, $params, $strictness);
1307 * Given an instance number of a module, finds the coursemodule description
1309 * Please note that this function performs DB query. When possible use cached course
1310 * modinfo. For example get_fast_modinfo($courseorid)->instances[$modulename][$instance]
1311 * See also {@link cm_info::get_course_module_record()}
1313 * @global object
1314 * @param string $modulename name of module type, eg. resource, assignment,...
1315 * @param int $instance module instance number (id in resource, assignment etc. table)
1316 * @param int $courseid optional course id for extra validation
1317 * @param bool $sectionnum include relative section number (0,1,2 ...)
1318 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1319 * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1320 * MUST_EXIST means throw exception if no record or multiple records found
1321 * @return stdClass
1323 function get_coursemodule_from_instance($modulename, $instance, $courseid=0, $sectionnum=false, $strictness=IGNORE_MISSING) {
1324 global $DB;
1326 if (!core_component::is_valid_plugin_name('mod', $modulename)) {
1327 throw new coding_exception('Invalid modulename parameter');
1330 $params = array('instance'=>$instance, 'modulename'=>$modulename);
1332 $courseselect = "";
1333 $sectionfield = "";
1334 $sectionjoin = "";
1336 if ($courseid) {
1337 $courseselect = "AND cm.course = :courseid";
1338 $params['courseid'] = $courseid;
1341 if ($sectionnum) {
1342 $sectionfield = ", cw.section AS sectionnum";
1343 $sectionjoin = "LEFT JOIN {course_sections} cw ON cw.id = cm.section";
1346 $sql = "SELECT cm.*, m.name, md.name AS modname $sectionfield
1347 FROM {course_modules} cm
1348 JOIN {modules} md ON md.id = cm.module
1349 JOIN {".$modulename."} m ON m.id = cm.instance
1350 $sectionjoin
1351 WHERE m.id = :instance AND md.name = :modulename
1352 $courseselect";
1354 return $DB->get_record_sql($sql, $params, $strictness);
1358 * Returns all course modules of given activity in course
1360 * @param string $modulename The module name (forum, quiz, etc.)
1361 * @param int $courseid The course id to get modules for
1362 * @param string $extrafields extra fields starting with m.
1363 * @return array Array of results
1365 function get_coursemodules_in_course($modulename, $courseid, $extrafields='') {
1366 global $DB;
1368 if (!core_component::is_valid_plugin_name('mod', $modulename)) {
1369 throw new coding_exception('Invalid modulename parameter');
1372 if (!empty($extrafields)) {
1373 $extrafields = ", $extrafields";
1375 $params = array();
1376 $params['courseid'] = $courseid;
1377 $params['modulename'] = $modulename;
1380 return $DB->get_records_sql("SELECT cm.*, m.name, md.name as modname $extrafields
1381 FROM {course_modules} cm, {modules} md, {".$modulename."} m
1382 WHERE cm.course = :courseid AND
1383 cm.instance = m.id AND
1384 md.name = :modulename AND
1385 md.id = cm.module", $params);
1389 * Returns an array of all the active instances of a particular module in given courses, sorted in the order they are defined
1391 * Returns an array of all the active instances of a particular
1392 * module in given courses, sorted in the order they are defined
1393 * in the course. Returns an empty array on any errors.
1395 * The returned objects includle the columns cw.section, cm.visible,
1396 * cm.groupmode, and cm.groupingid, and are indexed by cm.id.
1398 * @global object
1399 * @global object
1400 * @param string $modulename The name of the module to get instances for
1401 * @param array $courses an array of course objects.
1402 * @param int $userid
1403 * @param int $includeinvisible
1404 * @return array of module instance objects, including some extra fields from the course_modules
1405 * and course_sections tables, or an empty array if an error occurred.
1407 function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $includeinvisible=false) {
1408 global $CFG, $DB;
1410 if (!core_component::is_valid_plugin_name('mod', $modulename)) {
1411 throw new coding_exception('Invalid modulename parameter');
1414 $outputarray = array();
1416 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1417 return $outputarray;
1420 list($coursessql, $params) = $DB->get_in_or_equal(array_keys($courses), SQL_PARAMS_NAMED, 'c0');
1421 $params['modulename'] = $modulename;
1423 if (!$rawmods = $DB->get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
1424 cm.groupmode, cm.groupingid
1425 FROM {course_modules} cm, {course_sections} cw, {modules} md,
1426 {".$modulename."} m
1427 WHERE cm.course $coursessql AND
1428 cm.instance = m.id AND
1429 cm.section = cw.id AND
1430 md.name = :modulename AND
1431 md.id = cm.module", $params)) {
1432 return $outputarray;
1435 foreach ($courses as $course) {
1436 $modinfo = get_fast_modinfo($course, $userid);
1438 if (empty($modinfo->instances[$modulename])) {
1439 continue;
1442 foreach ($modinfo->instances[$modulename] as $cm) {
1443 if (!$includeinvisible and !$cm->uservisible) {
1444 continue;
1446 if (!isset($rawmods[$cm->id])) {
1447 continue;
1449 $instance = $rawmods[$cm->id];
1450 if (!empty($cm->extra)) {
1451 $instance->extra = $cm->extra;
1453 $outputarray[] = $instance;
1457 return $outputarray;
1461 * Returns an array of all the active instances of a particular module in a given course,
1462 * sorted in the order they are defined.
1464 * Returns an array of all the active instances of a particular
1465 * module in a given course, sorted in the order they are defined
1466 * in the course. Returns an empty array on any errors.
1468 * The returned objects includle the columns cw.section, cm.visible,
1469 * cm.groupmode, and cm.groupingid, and are indexed by cm.id.
1471 * Simply calls {@link all_instances_in_courses()} with a single provided course
1473 * @param string $modulename The name of the module to get instances for
1474 * @param object $course The course obect.
1475 * @return array of module instance objects, including some extra fields from the course_modules
1476 * and course_sections tables, or an empty array if an error occurred.
1477 * @param int $userid
1478 * @param int $includeinvisible
1480 function get_all_instances_in_course($modulename, $course, $userid=NULL, $includeinvisible=false) {
1481 return get_all_instances_in_courses($modulename, array($course->id => $course), $userid, $includeinvisible);
1486 * Determine whether a module instance is visible within a course
1488 * Given a valid module object with info about the id and course,
1489 * and the module's type (eg "forum") returns whether the object
1490 * is visible or not according to the 'eye' icon only.
1492 * NOTE: This does NOT take into account visibility to a particular user.
1493 * To get visibility access for a specific user, use get_fast_modinfo, get a
1494 * cm_info object from this, and check the ->uservisible property; or use
1495 * the \core_availability\info_module::is_user_visible() static function.
1497 * @global object
1499 * @param $moduletype Name of the module eg 'forum'
1500 * @param $module Object which is the instance of the module
1501 * @return bool Success
1503 function instance_is_visible($moduletype, $module) {
1504 global $DB;
1506 if (!empty($module->id)) {
1507 $params = array('courseid'=>$module->course, 'moduletype'=>$moduletype, 'moduleid'=>$module->id);
1508 if ($records = $DB->get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.course
1509 FROM {course_modules} cm, {modules} m
1510 WHERE cm.course = :courseid AND
1511 cm.module = m.id AND
1512 m.name = :moduletype AND
1513 cm.instance = :moduleid", $params)) {
1515 foreach ($records as $record) { // there should only be one - use the first one
1516 return $record->visible;
1520 return true; // visible by default!
1524 /// LOG FUNCTIONS /////////////////////////////////////////////////////
1527 * Get instance of log manager.
1529 * @param bool $forcereload
1530 * @return \core\log\manager
1532 function get_log_manager($forcereload = false) {
1533 /** @var \core\log\manager $singleton */
1534 static $singleton = null;
1536 if ($forcereload and isset($singleton)) {
1537 $singleton->dispose();
1538 $singleton = null;
1541 if (isset($singleton)) {
1542 return $singleton;
1545 $classname = '\tool_log\log\manager';
1546 if (defined('LOG_MANAGER_CLASS')) {
1547 $classname = LOG_MANAGER_CLASS;
1550 if (!class_exists($classname)) {
1551 if (!empty($classname)) {
1552 debugging("Cannot find log manager class '$classname'.", DEBUG_DEVELOPER);
1554 $classname = '\core\log\dummy_manager';
1557 $singleton = new $classname();
1558 return $singleton;
1562 * Add an entry to the config log table.
1564 * These are "action" focussed rather than web server hits,
1565 * and provide a way to easily reconstruct changes to Moodle configuration.
1567 * @package core
1568 * @category log
1569 * @global moodle_database $DB
1570 * @global stdClass $USER
1571 * @param string $name The name of the configuration change action
1572 For example 'filter_active' when activating or deactivating a filter
1573 * @param string $oldvalue The config setting's previous value
1574 * @param string $value The config setting's new value
1575 * @param string $plugin Plugin name, for example a filter name when changing filter configuration
1576 * @return void
1578 function add_to_config_log($name, $oldvalue, $value, $plugin) {
1579 global $USER, $DB;
1581 $log = new stdClass();
1582 // Use 0 as user id during install.
1583 $log->userid = during_initial_install() ? 0 : $USER->id;
1584 $log->timemodified = time();
1585 $log->name = $name;
1586 $log->oldvalue = $oldvalue;
1587 $log->value = $value;
1588 $log->plugin = $plugin;
1590 $id = $DB->insert_record('config_log', $log);
1592 $event = core\event\config_log_created::create(array(
1593 'objectid' => $id,
1594 'userid' => $log->userid,
1595 'context' => \context_system::instance(),
1596 'other' => array(
1597 'name' => $log->name,
1598 'oldvalue' => $log->oldvalue,
1599 'value' => $log->value,
1600 'plugin' => $log->plugin
1603 $event->trigger();
1607 * Store user last access times - called when use enters a course or site
1609 * @package core
1610 * @category log
1611 * @global stdClass $USER
1612 * @global stdClass $CFG
1613 * @global moodle_database $DB
1614 * @uses LASTACCESS_UPDATE_SECS
1615 * @uses SITEID
1616 * @param int $courseid empty courseid means site
1617 * @return void
1619 function user_accesstime_log($courseid=0) {
1620 global $USER, $CFG, $DB;
1622 if (!isloggedin() or \core\session\manager::is_loggedinas()) {
1623 // no access tracking
1624 return;
1627 if (isguestuser()) {
1628 // Do not update guest access times/ips for performance.
1629 return;
1632 if (empty($courseid)) {
1633 $courseid = SITEID;
1636 $timenow = time();
1638 /// Store site lastaccess time for the current user
1639 if ($timenow - $USER->lastaccess > LASTACCESS_UPDATE_SECS) {
1640 /// Update $USER->lastaccess for next checks
1641 $USER->lastaccess = $timenow;
1643 $last = new stdClass();
1644 $last->id = $USER->id;
1645 $last->lastip = getremoteaddr();
1646 $last->lastaccess = $timenow;
1648 $DB->update_record_raw('user', $last);
1651 if ($courseid == SITEID) {
1652 /// no user_lastaccess for frontpage
1653 return;
1656 /// Store course lastaccess times for the current user
1657 if (empty($USER->currentcourseaccess[$courseid]) or ($timenow - $USER->currentcourseaccess[$courseid] > LASTACCESS_UPDATE_SECS)) {
1659 $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid'=>$USER->id, 'courseid'=>$courseid));
1661 if ($lastaccess === false) {
1662 // Update course lastaccess for next checks
1663 $USER->currentcourseaccess[$courseid] = $timenow;
1665 $last = new stdClass();
1666 $last->userid = $USER->id;
1667 $last->courseid = $courseid;
1668 $last->timeaccess = $timenow;
1669 try {
1670 $DB->insert_record_raw('user_lastaccess', $last, false);
1671 } catch (dml_write_exception $e) {
1672 // During a race condition we can fail to find the data, then it appears.
1673 // If we still can't find it, rethrow the exception.
1674 $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid' => $USER->id,
1675 'courseid' => $courseid));
1676 if ($lastaccess === false) {
1677 throw $e;
1679 // If we did find it, the race condition was true and another thread has inserted the time for us.
1680 // We can just continue without having to do anything.
1683 } else if ($timenow - $lastaccess < LASTACCESS_UPDATE_SECS) {
1684 // no need to update now, it was updated recently in concurrent login ;-)
1686 } else {
1687 // Update course lastaccess for next checks
1688 $USER->currentcourseaccess[$courseid] = $timenow;
1690 $DB->set_field('user_lastaccess', 'timeaccess', $timenow, array('userid'=>$USER->id, 'courseid'=>$courseid));
1695 /// GENERAL HELPFUL THINGS ///////////////////////////////////
1698 * Dumps a given object's information for debugging purposes
1700 * When used in a CLI script, the object's information is written to the standard
1701 * error output stream. When used in a web script, the object is dumped to a
1702 * pre-formatted block with the "notifytiny" CSS class.
1704 * @param mixed $object The data to be printed
1705 * @return void output is echo'd
1707 function print_object($object) {
1709 // we may need a lot of memory here
1710 raise_memory_limit(MEMORY_EXTRA);
1712 if (CLI_SCRIPT) {
1713 fwrite(STDERR, print_r($object, true));
1714 fwrite(STDERR, PHP_EOL);
1715 } else {
1716 echo html_writer::tag('pre', s(print_r($object, true)), array('class' => 'notifytiny'));
1721 * This function is the official hook inside XMLDB stuff to delegate its debug to one
1722 * external function.
1724 * Any script can avoid calls to this function by defining XMLDB_SKIP_DEBUG_HOOK before
1725 * using XMLDB classes. Obviously, also, if this function doesn't exist, it isn't invoked ;-)
1727 * @uses DEBUG_DEVELOPER
1728 * @param string $message string contains the error message
1729 * @param object $object object XMLDB object that fired the debug
1731 function xmldb_debug($message, $object) {
1733 debugging($message, DEBUG_DEVELOPER);
1737 * @global object
1738 * @uses CONTEXT_COURSECAT
1739 * @return boolean Whether the user can create courses in any category in the system.
1741 function user_can_create_courses() {
1742 global $DB;
1743 $catsrs = $DB->get_recordset('course_categories');
1744 foreach ($catsrs as $cat) {
1745 if (has_capability('moodle/course:create', context_coursecat::instance($cat->id))) {
1746 $catsrs->close();
1747 return true;
1750 $catsrs->close();
1751 return false;
1755 * This method can update the values in mulitple database rows for a colum with
1756 * a unique index, without violating that constraint.
1758 * Suppose we have a table with a unique index on (otherid, sortorder), and
1759 * for a particular value of otherid, we want to change all the sort orders.
1760 * You have to do this carefully or you will violate the unique index at some time.
1761 * This method takes care of the details for you.
1763 * Note that, it is the responsibility of the caller to make sure that the
1764 * requested rename is legal. For example, if you ask for [1 => 2, 2 => 2]
1765 * then you will get a unique key violation error from the database.
1767 * @param string $table The database table to modify.
1768 * @param string $field the field that contains the values we are going to change.
1769 * @param array $newvalues oldvalue => newvalue how to change the values.
1770 * E.g. [1 => 4, 2 => 1, 3 => 3, 4 => 2].
1771 * @param array $otherconditions array fieldname => requestedvalue extra WHERE clause
1772 * conditions to restrict which rows are affected. E.g. array('otherid' => 123).
1773 * @param int $unusedvalue (defaults to -1) a value that is never used in $ordercol.
1775 function update_field_with_unique_index($table, $field, array $newvalues,
1776 array $otherconditions, $unusedvalue = -1) {
1777 global $DB;
1778 $safechanges = decompose_update_into_safe_changes($newvalues, $unusedvalue);
1780 $transaction = $DB->start_delegated_transaction();
1781 foreach ($safechanges as $change) {
1782 list($from, $to) = $change;
1783 $otherconditions[$field] = $from;
1784 $DB->set_field($table, $field, $to, $otherconditions);
1786 $transaction->allow_commit();
1790 * Helper used by {@link update_field_with_unique_index()}. Given a desired
1791 * set of changes, break them down into single udpates that can be done one at
1792 * a time without breaking any unique index constraints.
1794 * Suppose the input is array(1 => 2, 2 => 1) and -1. Then the output will be
1795 * array (array(1, -1), array(2, 1), array(-1, 2)). This function solves this
1796 * problem in the general case, not just for simple swaps. The unit tests give
1797 * more examples.
1799 * Note that, it is the responsibility of the caller to make sure that the
1800 * requested rename is legal. For example, if you ask for something impossible
1801 * like array(1 => 2, 2 => 2) then the results are undefined. (You will probably
1802 * get a unique key violation error from the database later.)
1804 * @param array $newvalues The desired re-ordering.
1805 * E.g. array(1 => 4, 2 => 1, 3 => 3, 4 => 2).
1806 * @param int $unusedvalue A value that is not currently used.
1807 * @return array A safe way to perform the re-order. An array of two-element
1808 * arrays array($from, $to).
1809 * E.g. array(array(1, -1), array(2, 1), array(4, 2), array(-1, 4)).
1811 function decompose_update_into_safe_changes(array $newvalues, $unusedvalue) {
1812 $nontrivialmap = array();
1813 foreach ($newvalues as $from => $to) {
1814 if ($from == $unusedvalue || $to == $unusedvalue) {
1815 throw new \coding_exception('Supposedly unused value ' . $unusedvalue . ' is actually used!');
1817 if ($from != $to) {
1818 $nontrivialmap[$from] = $to;
1822 if (empty($nontrivialmap)) {
1823 return array();
1826 // First we deal with all renames that are not part of cycles.
1827 // This bit is O(n^2) and it ought to be possible to do better,
1828 // but it does not seem worth the effort.
1829 $safechanges = array();
1830 $nontrivialmapchanged = true;
1831 while ($nontrivialmapchanged) {
1832 $nontrivialmapchanged = false;
1834 foreach ($nontrivialmap as $from => $to) {
1835 if (array_key_exists($to, $nontrivialmap)) {
1836 continue; // Cannot currenly do this rename.
1838 // Is safe to do this rename now.
1839 $safechanges[] = array($from, $to);
1840 unset($nontrivialmap[$from]);
1841 $nontrivialmapchanged = true;
1845 // Are we done?
1846 if (empty($nontrivialmap)) {
1847 return $safechanges;
1850 // Now what is left in $nontrivialmap must be a permutation,
1851 // which must be a combination of disjoint cycles. We need to break them.
1852 while (!empty($nontrivialmap)) {
1853 // Extract the first cycle.
1854 reset($nontrivialmap);
1855 $current = $cyclestart = key($nontrivialmap);
1856 $cycle = array();
1857 do {
1858 $cycle[] = $current;
1859 $next = $nontrivialmap[$current];
1860 unset($nontrivialmap[$current]);
1861 $current = $next;
1862 } while ($current != $cyclestart);
1864 // Now convert it to a sequence of safe renames by using a temp.
1865 $safechanges[] = array($cyclestart, $unusedvalue);
1866 $cycle[0] = $unusedvalue;
1867 $to = $cyclestart;
1868 while ($from = array_pop($cycle)) {
1869 $safechanges[] = array($from, $to);
1870 $to = $from;
1874 return $safechanges;