MDL-14974 improved columns caching implementation
[moodle.git] / lib / datalib.php
CommitLineData
6078ba30 1<?php // $Id$
341b5ed2 2
7cf1c7bd 3/**
4 * Library of functions for database manipulation.
5930cded 5 *
7cf1c7bd 6 * Other main libraries:
7 * - weblib.php - functions that produce web output
8 * - moodlelib.php - general-purpose Moodle functions
6159ce65 9 * @author Martin Dougiamas and many others
89dcb99d 10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7cf1c7bd 11 * @package moodlecore
12 */
13
341b5ed2 14 /// Some constants
15 define('LASTACCESS_UPDATE_SECS', 60); /// Number of seconds to wait before
16 /// updating lastaccess information in DB.
df28d6c5 17
5930cded 18/**
11a052a6 19 * Escape all dangerous characters in a data record
20 *
21 * $dataobject is an object containing needed data
22 * Run over each field exectuting addslashes() function
23 * to escape SQL unfriendly characters (e.g. quotes)
24 * Handy when writing back data read from the database
25 *
26 * @param $dataobject Object containing the database record
27 * @return object Same object with neccessary characters escaped
28 */
29function addslashes_object( $dataobject ) {
30 $a = get_object_vars( $dataobject);
31 foreach ($a as $key=>$value) {
32 $a[$key] = addslashes( $value );
33 }
34 return (object)$a;
35}
0892f7bd 36
df28d6c5 37/// USER DATABASE ////////////////////////////////////////////////
38
18a97fd8 39/**
fbc21ae8 40 * Returns $user object of the main admin user
20aeb4b8 41 * primary admin = admin with lowest role_assignment id among admins
fbc21ae8 42 * @uses $CFG
43 * @return object(admin) An associative array representing the admin user.
fbc21ae8 44 */
df28d6c5 45function get_admin () {
df28d6c5 46
47 global $CFG;
2965f8fd 48 static $myadmin;
49
50 if (isset($myadmin)) {
51 return $myadmin;
52 }
df28d6c5 53
54 if ( $admins = get_admins() ) {
55 foreach ($admins as $admin) {
2965f8fd 56 $myadmin = $admin;
8f0cd6ef 57 return $admin; // ie the first one
df28d6c5 58 }
59 } else {
60 return false;
61 }
62}
63
18a97fd8 64/**
c26ecb1a 65 * Returns list of all admins, using 1 DB query. It depends on DB schema v1.7
66 * but does not depend on the v1.9 datastructures (context.path, etc).
fbc21ae8 67 *
68 * @uses $CFG
7290c7fa 69 * @return object
fbc21ae8 70 */
df28d6c5 71function get_admins() {
df28d6c5 72
73 global $CFG;
5930cded 74
c26ecb1a 75 $sql = "SELECT ra.userid, SUM(rc.permission) AS permission, MIN(ra.id) AS adminid
76 FROM " . $CFG->prefix . "role_capabilities rc
77 JOIN " . $CFG->prefix . "context ctx
78 ON ctx.id=rc.contextid
79 JOIN " . $CFG->prefix . "role_assignments ra
80 ON ra.roleid=rc.roleid AND ra.contextid=ctx.id
81 WHERE ctx.contextlevel=10
82 AND rc.capability IN ('moodle/site:config',
83 'moodle/legacy:admin',
84 'moodle/site:doanything')
85 GROUP BY ra.userid
86 HAVING SUM(rc.permission) > 0";
87
88 $sql = "SELECT u.*, ra.adminid
89 FROM " . $CFG->prefix . "user u
90 JOIN ($sql) ra
91 ON u.id=ra.userid
92 ORDER BY ra.adminid ASC";
5930cded 93
c26ecb1a 94 return get_records_sql($sql);
df28d6c5 95}
96
97
b61efafb 98function get_courses_in_metacourse($metacourseid) {
99 global $CFG;
100
5f37b628 101 $sql = "SELECT c.id,c.shortname,c.fullname FROM {$CFG->prefix}course c, {$CFG->prefix}course_meta mc WHERE mc.parent_course = $metacourseid
5afa0de6 102 AND mc.child_course = c.id ORDER BY c.shortname";
b61efafb 103
104 return get_records_sql($sql);
105}
106
107function get_courses_notin_metacourse($metacourseid,$count=false) {
108
109 global $CFG;
110
b61efafb 111 if ($count) {
112 $sql = "SELECT COUNT(c.id)";
c44d5d42 113 } else {
b61efafb 114 $sql = "SELECT c.id,c.shortname,c.fullname";
115 }
178ccd11 116
ffed6bf3 117 $alreadycourses = get_courses_in_metacourse($metacourseid);
5930cded 118
c44d5d42 119 $sql .= " FROM {$CFG->prefix}course c WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).")
5afa0de6 120 AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1 ".((empty($count)) ? " ORDER BY c.shortname" : "");
5930cded 121
b61efafb 122 return get_records_sql($sql);
123}
124
493cde24 125function count_courses_notin_metacourse($metacourseid) {
126 global $CFG;
127
128 $alreadycourses = get_courses_in_metacourse($metacourseid);
129
69cd298a 130 $sql = "SELECT COUNT(c.id) AS notin FROM {$CFG->prefix}course c
493cde24 131 WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).")
132 AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1";
133
69cd298a 134 if (!$count = get_record_sql($sql)) {
493cde24 135 return 0;
136 }
137
69cd298a 138 return $count->notin;
493cde24 139}
140
900df8b6 141/**
fbc21ae8 142 * Search through course users
143 *
5930cded 144 * If $coursid specifies the site course then this function searches
fbc21ae8 145 * through all undeleted and confirmed users
146 *
147 * @uses $CFG
148 * @uses SITEID
149 * @param int $courseid The course in question.
150 * @param int $groupid The group in question.
151 * @param string $searchtext ?
152 * @param string $sort ?
5930cded 153 * @param string $exceptions ?
7290c7fa 154 * @return object
fbc21ae8 155 */
900df8b6 156function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions='') {
157 global $CFG;
0720313b 158
29daf3a0 159 $LIKE = sql_ilike();
160 $fullname = sql_fullname('u.firstname', 'u.lastname');
8f0cd6ef 161
900df8b6 162 if (!empty($exceptions)) {
d4419d55 163 $except = ' AND u.id NOT IN ('. $exceptions .') ';
900df8b6 164 } else {
165 $except = '';
166 }
2700d113 167
900df8b6 168 if (!empty($sort)) {
d4419d55 169 $order = ' ORDER BY '. $sort;
900df8b6 170 } else {
171 $order = '';
172 }
8f0cd6ef 173
d4419d55 174 $select = 'u.deleted = \'0\' AND u.confirmed = \'1\'';
2700d113 175
222ac91b 176 if (!$courseid or $courseid == SITEID) {
2700d113 177 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
178 FROM {$CFG->prefix}user u
179 WHERE $select
900df8b6 180 AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
2700d113 181 $except $order");
8f0cd6ef 182 } else {
2700d113 183
900df8b6 184 if ($groupid) {
f3f7610c 185//TODO:check. Remove group DB dependencies.
900df8b6 186 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
8f0cd6ef 187 FROM {$CFG->prefix}user u,
1d684195 188 {$CFG->prefix}groups_members gm
189 WHERE $select AND gm.groupid = '$groupid' AND gm.userid = u.id
900df8b6 190 AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
191 $except $order");
192 } else {
ea8158c1 193 $context = get_context_instance(CONTEXT_COURSE, $courseid);
194 $contextlists = get_related_contexts_string($context);
195 $users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
8f0cd6ef 196 FROM {$CFG->prefix}user u,
ea8158c1 197 {$CFG->prefix}role_assignments ra
198 WHERE $select AND ra.contextid $contextlists AND ra.userid = u.id
900df8b6 199 AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
ea8158c1 200 $except $order");
900df8b6 201 }
ea8158c1 202 return $users;
900df8b6 203 }
df28d6c5 204}
205
2700d113 206
18a97fd8 207/**
fbc21ae8 208 * Returns a list of all site users
209 * Obsolete, just calls get_course_users(SITEID)
210 *
211 * @uses SITEID
c6d15803 212 * @deprecated Use {@link get_course_users()} instead.
fbc21ae8 213 * @param string $fields A comma separated list of fields to be returned from the chosen table.
7290c7fa 214 * @return object|false {@link $USER} records or false if error.
fbc21ae8 215 */
d4419d55 216function get_site_users($sort='u.lastaccess DESC', $fields='*', $exceptions='') {
2d0b30a0 217
65ee9c16 218 return get_course_users(SITEID, $sort, $exceptions, $fields);
2d0b30a0 219}
220
9fa49e22 221
18a97fd8 222/**
fbc21ae8 223 * Returns a subset of users
224 *
225 * @uses $CFG
7290c7fa 226 * @param bool $get If false then only a count of the records is returned
fbc21ae8 227 * @param string $search A simple string to search for
7290c7fa 228 * @param bool $confirmed A switch to allow/disallow unconfirmed users
fbc21ae8 229 * @param array(int) $exceptions A list of IDs to ignore, eg 2,4,5,8,9,10
230 * @param string $sort A SQL snippet for the sorting criteria to use
231 * @param string $firstinitial ?
232 * @param string $lastinitial ?
233 * @param string $page ?
234 * @param string $recordsperpage ?
235 * @param string $fields A comma separated list of fields to be returned from the chosen table.
7290c7fa 236 * @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.
fbc21ae8 237 */
d4419d55 238function get_users($get=true, $search='', $confirmed=false, $exceptions='', $sort='firstname ASC',
cd1edf9e 239 $firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*', $extraselect='') {
18a97fd8 240
241 global $CFG;
5930cded 242
36075e09 243 if ($get && !$recordsperpage) {
244 debugging('Call to get_users with $get = true no $recordsperpage limit. ' .
245 'On large installations, this will probably cause an out of memory error. ' .
246 'Please think again and change your code so that it does not try to ' .
03517306 247 'load so much data into memory.', DEBUG_DEVELOPER);
36075e09 248 }
18a97fd8 249
29daf3a0 250 $LIKE = sql_ilike();
251 $fullname = sql_fullname();
e384fb7b 252
e8e0bb2d 253 $select = 'username <> \'guest\' AND deleted = 0';
488acd1b 254
0044147e 255 if (!empty($search)){
256 $search = trim($search);
488acd1b 257 $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
e384fb7b 258 }
259
5a741655 260 if ($confirmed) {
d4419d55 261 $select .= ' AND confirmed = \'1\' ';
5a741655 262 }
263
264 if ($exceptions) {
d4419d55 265 $select .= ' AND id NOT IN ('. $exceptions .') ';
5a741655 266 }
267
488acd1b 268 if ($firstinitial) {
d4419d55 269 $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\'';
8f0cd6ef 270 }
488acd1b 271 if ($lastinitial) {
d4419d55 272 $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\'';
8f0cd6ef 273 }
488acd1b 274
cd1edf9e 275 if ($extraselect) {
276 $select .= " AND $extraselect ";
277 }
278
5a741655 279 if ($get) {
36075e09 280 return get_records_select('user', $select, $sort, $fields, $page, $recordsperpage);
5a741655 281 } else {
36075e09 282 return count_records_select('user', $select);
5a741655 283 }
9fa49e22 284}
285
5a741655 286
18a97fd8 287/**
fbc21ae8 288 * shortdesc (optional)
289 *
290 * longdesc
291 *
292 * @uses $CFG
293 * @param string $sort ?
294 * @param string $dir ?
295 * @param int $categoryid ?
296 * @param int $categoryid ?
297 * @param string $search ?
298 * @param string $firstinitial ?
299 * @param string $lastinitial ?
7290c7fa 300 * @returnobject {@link $USER} records
fbc21ae8 301 * @todo Finish documenting this function
302 */
303
36075e09 304function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0,
cd1edf9e 305 $search='', $firstinitial='', $lastinitial='', $extraselect='') {
488acd1b 306
9fa49e22 307 global $CFG;
31fefa63 308
29daf3a0 309 $LIKE = sql_ilike();
310 $fullname = sql_fullname();
c2a96d6b 311
e8e0bb2d 312 $select = "deleted <> '1'";
488acd1b 313
0044147e 314 if (!empty($search)) {
315 $search = trim($search);
39dc779a 316 $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%' OR username='$search') ";
488acd1b 317 }
318
319 if ($firstinitial) {
d4419d55 320 $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\' ';
488acd1b 321 }
322
323 if ($lastinitial) {
d4419d55 324 $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\' ';
c750592a 325 }
326
cd1edf9e 327 if ($extraselect) {
328 $select .= " AND $extraselect ";
329 }
03d820c7 330
488acd1b 331 if ($sort) {
d4419d55 332 $sort = ' ORDER BY '. $sort .' '. $dir;
488acd1b 333 }
334
335/// warning: will return UNCONFIRMED USERS
03d820c7 336 return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid
8f0cd6ef 337 FROM {$CFG->prefix}user
422770d8 338 WHERE $select $sort", $page, $recordsperpage);
9fa49e22 339
340}
341
488acd1b 342
18a97fd8 343/**
7290c7fa 344 * Full list of users that have confirmed their accounts.
fbc21ae8 345 *
346 * @uses $CFG
7290c7fa 347 * @return object
fbc21ae8 348 */
9fa49e22 349function get_users_confirmed() {
350 global $CFG;
8f0cd6ef 351 return get_records_sql("SELECT *
352 FROM {$CFG->prefix}user
353 WHERE confirmed = 1
9fa49e22 354 AND deleted = 0
e8e0bb2d 355 AND username <> 'guest'");
9fa49e22 356}
357
358
02ebf404 359/// OTHER SITE AND COURSE FUNCTIONS /////////////////////////////////////////////
360
361
18a97fd8 362/**
fbc21ae8 363 * Returns $course object of the top-level site.
364 *
89dcb99d 365 * @return course A {@link $COURSE} object for the site
fbc21ae8 366 */
c44d5d42 367function get_site() {
368
369 global $SITE;
370
371 if (!empty($SITE->id)) { // We already have a global to use, so return that
372 return $SITE;
373 }
02ebf404 374
c44d5d42 375 if ($course = get_record('course', 'category', 0)) {
02ebf404 376 return $course;
377 } else {
378 return false;
379 }
380}
381
18a97fd8 382/**
613bbd7c 383 * Returns list of courses, for whole site, or category
384 *
385 * Returns list of courses, for whole site, or category
bfbfdb53 386 * Important: Using c.* for fields is extremely expensive because
613bbd7c 387 * we are using distinct. You almost _NEVER_ need all the fields
388 * in such a large SELECT
389 *
390 * @param type description
391 *
613bbd7c 392 */
6315b1c8 393function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") {
02ebf404 394
8ef9cb56 395 global $USER, $CFG;
5930cded 396
6315b1c8 397 if ($categoryid != "all" && is_numeric($categoryid)) {
71dea306 398 $categoryselect = "WHERE c.category = '$categoryid'";
399 } else {
5930cded 400 $categoryselect = "";
09575480 401 }
402
403 if (empty($sort)) {
404 $sortstatement = "";
405 } else {
406 $sortstatement = "ORDER BY $sort";
407 }
408
409 $visiblecourses = array();
5930cded 410
71dea306 411 // pull out all course matching the cat
656418b1 412 if ($courses = get_records_sql("SELECT $fields,
413 ctx.id AS ctxid, ctx.path AS ctxpath,
414 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
415 FROM {$CFG->prefix}course c
416 JOIN {$CFG->prefix}context ctx
417 ON (c.id = ctx.instanceid
418 AND ctx.contextlevel=".CONTEXT_COURSE.")
419 $categoryselect
420 $sortstatement")) {
09575480 421
422 // loop throught them
423 foreach ($courses as $course) {
656418b1 424 $course = make_context_subobj($course);
285f94f5 425 if (isset($course->visible) && $course->visible <= 0) {
09575480 426 // for hidden courses, require visibility check
656418b1 427 if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
5930cded 428 $visiblecourses [] = $course;
09575480 429 }
430 } else {
5930cded 431 $visiblecourses [] = $course;
432 }
09575480 433 }
6315b1c8 434 }
71dea306 435 return $visiblecourses;
6315b1c8 436
71dea306 437/*
6315b1c8 438 $teachertable = "";
439 $visiblecourses = "";
440 $sqland = "";
441 if (!empty($categoryselect)) {
442 $sqland = "AND ";
443 }
444 if (!empty($USER->id)) { // May need to check they are a teacher
12d06877 445 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
6315b1c8 446 $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
447 $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course = c.id";
448 }
449 } else {
450 $visiblecourses = "$sqland c.visible > 0";
8ef9cb56 451 }
452
6315b1c8 453 if ($categoryselect or $visiblecourses) {
454 $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
14f32609 455 } else {
6315b1c8 456 $selectsql = "{$CFG->prefix}course c $teachertable";
14f32609 457 }
458
5b66416f 459 $extrafield = str_replace('ASC','',$sort);
460 $extrafield = str_replace('DESC','',$extrafield);
461 $extrafield = trim($extrafield);
462 if (!empty($extrafield)) {
463 $extrafield = ','.$extrafield;
464 }
465 return get_records_sql("SELECT ".((!empty($teachertable)) ? " DISTINCT " : "")." $fields $extrafield FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : ""));
71dea306 466 */
8130b77b 467}
468
8130b77b 469
6315b1c8 470/**
613bbd7c 471 * Returns list of courses, for whole site, or category
472 *
473 * Similar to get_courses, but allows paging
5930cded 474 * Important: Using c.* for fields is extremely expensive because
613bbd7c 475 * we are using distinct. You almost _NEVER_ need all the fields
476 * in such a large SELECT
477 *
478 * @param type description
479 *
613bbd7c 480 */
6315b1c8 481function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*",
482 &$totalcount, $limitfrom="", $limitnum="") {
c7fe5c6f 483
8130b77b 484 global $USER, $CFG;
5930cded 485
71dea306 486 $categoryselect = "";
487 if ($categoryid != "all" && is_numeric($categoryid)) {
488 $categoryselect = "WHERE c.category = '$categoryid'";
489 } else {
5930cded 490 $categoryselect = "";
491 }
492
71dea306 493 // pull out all course matching the cat
12490fc2 494 $visiblecourses = array();
679b6179 495 if (!($rs = get_recordset_sql("SELECT $fields,
45ea1afb 496 ctx.id AS ctxid, ctx.path AS ctxpath,
497 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
679b6179 498 FROM {$CFG->prefix}course c
499 JOIN {$CFG->prefix}context ctx
500 ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
501 $categoryselect
502 ORDER BY $sort"))) {
12490fc2 503 return $visiblecourses;
504 }
71dea306 505 $totalcount = 0;
5930cded 506
285f94f5 507 if (!$limitfrom) {
5930cded 508 $limitfrom = 0;
71dea306 509 }
5930cded 510
71dea306 511 // iteration will have to be done inside loop to keep track of the limitfrom and limitnum
03cedd62 512 while ($course = rs_fetch_next_record($rs)) {
513 $course = make_context_subobj($course);
514 if ($course->visible <= 0) {
515 // for hidden courses, require visibility check
516 if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
71dea306 517 $totalcount++;
03cedd62 518 if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
71dea306 519 $visiblecourses [] = $course;
520 }
521 }
03cedd62 522 } else {
523 $totalcount++;
524 if ($totalcount > $limitfrom && (!$limitnum or count($visiblecourses) < $limitnum)) {
525 $visiblecourses [] = $course;
526 }
5930cded 527 }
71dea306 528 }
03cedd62 529 rs_close($rs);
71dea306 530 return $visiblecourses;
531
532/**
8130b77b 533
6315b1c8 534 $categoryselect = "";
b565bbdf 535 if ($categoryid != "all" && is_numeric($categoryid)) {
6315b1c8 536 $categoryselect = "c.category = '$categoryid'";
8130b77b 537 }
538
6315b1c8 539 $teachertable = "";
540 $visiblecourses = "";
541 $sqland = "";
542 if (!empty($categoryselect)) {
543 $sqland = "AND ";
c7fe5c6f 544 }
2d2da684 545 if (!empty($USER) and !empty($USER->id)) { // May need to check they are a teacher
12d06877 546 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
6315b1c8 547 $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')";
548 $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course=c.id";
549 }
8130b77b 550 } else {
6315b1c8 551 $visiblecourses = "$sqland c.visible > 0";
8130b77b 552 }
553
6315b1c8 554 if ($limitfrom !== "") {
29daf3a0 555 $limit = sql_paging_limit($limitfrom, $limitnum);
6315b1c8 556 } else {
557 $limit = "";
02ebf404 558 }
8ef9cb56 559
6315b1c8 560 $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses";
8ef9cb56 561
6315b1c8 562 $totalcount = count_records_sql("SELECT COUNT(DISTINCT c.id) FROM $selectsql");
8ef9cb56 563
2338ad32 564 return get_records_sql("SELECT $fields FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : "")." $limit");
71dea306 565 */
02ebf404 566}
567
70f15878 568/*
569 * Retrieve course records with the course managers and other related records
570 * that we need for print_course(). This allows print_courses() to do its job
571 * in a constant number of DB queries, regardless of the number of courses,
572 * role assignments, etc.
bfbfdb53 573 *
70f15878 574 * The returned array is indexed on c.id, and each course will have
575 * - $course->context - a context obj
576 * - $course->managers - array containing RA objects that include a $user obj
577 * with the minimal fields needed for fullname()
578 *
579 */
580function get_courses_wmanagers($categoryid=0, $sort="c.sortorder ASC", $fields=array()) {
581 /*
bfbfdb53 582 * The plan is to
70f15878 583 *
584 * - Grab the courses JOINed w/context
585 *
586 * - Grab the interesting course-manager RAs
587 * JOINed with a base user obj and add them to each course
588 *
589 * So as to do all the work in 2 DB queries. The RA+user JOIN
590 * ends up being pretty expensive if it happens over _all_
591 * courses on a large site. (Are we surprised!?)
592 *
593 * So this should _never_ get called with 'all' on a large site.
594 *
595 */
596 global $USER, $CFG;
597
598 $allcats = false; // bool flag
599 if ($categoryid === 'all') {
600 $categoryclause = '';
601 $allcats = true;
602 } elseif (is_numeric($categoryid)) {
603 $categoryclause = "c.category = $categoryid";
604 } else {
605 debugging("Could not recognise categoryid = $categoryid");
606 $categoryclause = '';
607 }
608
609 $basefields = array('id', 'category', 'sortorder',
610 'shortname', 'fullname', 'idnumber',
611 'teacher', 'teachers', 'student', 'students',
612 'guest', 'startdate', 'visible',
613 'newsitems', 'cost', 'enrol',
614 'groupmode', 'groupmodeforce');
615
616 if (!is_null($fields) && is_string($fields)) {
617 if (empty($fields)) {
618 $fields = $basefields;
619 } else {
bfbfdb53 620 // turn the fields from a string to an array that
70f15878 621 // get_user_courses_bycap() will like...
622 $fields = explode(',',$fields);
623 $fields = array_map('trim', $fields);
624 $fields = array_unique(array_merge($basefields, $fields));
625 }
626 } elseif (is_array($fields)) {
627 $fields = array_merge($basefields,$fields);
628 }
629 $coursefields = 'c.' .join(',c.', $fields);
630
631 if (empty($sort)) {
632 $sortstatement = "";
633 } else {
634 $sortstatement = "ORDER BY $sort";
635 }
636
e89f157b 637 $where = 'WHERE c.id != ' . SITEID;
70f15878 638 if ($categoryclause !== ''){
e89f157b 639 $where = "$where AND $categoryclause";
70f15878 640 }
641
642 // pull out all courses matching the cat
643 $sql = "SELECT $coursefields,
45ea1afb 644 ctx.id AS ctxid, ctx.path AS ctxpath,
645 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
70f15878 646 FROM {$CFG->prefix}course c
647 JOIN {$CFG->prefix}context ctx
648 ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
649 $where
650 $sortstatement";
651
652 $catpaths = array();
653 $catpath = NULL;
654 if ($courses = get_records_sql($sql)) {
655 // loop on courses materialising
bfbfdb53 656 // the context, and prepping data to fetch the
70f15878 657 // managers efficiently later...
658 foreach ($courses as $k => $course) {
659 $courses[$k] = make_context_subobj($courses[$k]);
660 $courses[$k]->managers = array();
661 if ($allcats === false) {
662 // single cat, so take just the first one...
663 if ($catpath === NULL) {
664 $catpath = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
665 }
666 } else {
667 // chop off the contextid of the course itself
668 // like dirname() does...
669 $catpaths[] = preg_replace(':/\d+$:', '',$courses[$k]->context->path);
670 }
671 }
672 } else {
673 return array(); // no courses!
674 }
675
b1cff118 676 $CFG->coursemanager = trim($CFG->coursemanager);
677 if (empty($CFG->coursemanager)) {
678 return $courses;
679 }
680
70f15878 681 $managerroles = split(',', $CFG->coursemanager);
682 $catctxids = '';
683 if (count($managerroles)) {
684 if ($allcats === true) {
685 $catpaths = array_unique($catpaths);
686 $ctxids = array();
687 foreach ($catpaths as $cpath) {
688 $ctxids = array_merge($ctxids, explode('/',substr($cpath,1)));
689 }
690 $ctxids = array_unique($ctxids);
691 $catctxids = implode( ',' , $ctxids);
c7a71127 692 unset($catpaths);
693 unset($cpath);
70f15878 694 } else {
695 // take the ctx path from the first course
696 // as all categories will be the same...
697 $catpath = substr($catpath,1);
698 $catpath = preg_replace(':/\d+$:','',$catpath);
699 $catctxids = str_replace('/',',',$catpath);
700 }
701 if ($categoryclause !== '') {
702 $categoryclause = "AND $categoryclause";
703 }
704 /*
bfbfdb53 705 * Note: Here we use a LEFT OUTER JOIN that can
70f15878 706 * "optionally" match to avoid passing a ton of context
707 * ids in an IN() clause. Perhaps a subselect is faster.
708 *
709 * In any case, this SQL is not-so-nice over large sets of
710 * courses with no $categoryclause.
711 *
712 */
713 $sql = "SELECT ctx.path, ctx.instanceid, ctx.contextlevel,
bfbfdb53 714 ra.hidden,
70f15878 715 r.id AS roleid, r.name as rolename,
716 u.id AS userid, u.firstname, u.lastname
717 FROM {$CFG->prefix}role_assignments ra
718 JOIN {$CFG->prefix}context ctx
719 ON ra.contextid = ctx.id
720 JOIN {$CFG->prefix}user u
721 ON ra.userid = u.id
722 JOIN {$CFG->prefix}role r
723 ON ra.roleid = r.id
724 LEFT OUTER JOIN {$CFG->prefix}course c
725 ON (ctx.instanceid=c.id AND ctx.contextlevel=".CONTEXT_COURSE.")
c7a71127 726 WHERE ( c.id IS NOT NULL";
727 // under certain conditions, $catctxids is NULL
728 if($catctxids == NULL){
729 $sql .= ") ";
730 }else{
731 $sql .= " OR ra.contextid IN ($catctxids) )";
732 }
733
734 $sql .= "AND ra.roleid IN ({$CFG->coursemanager})
70f15878 735 $categoryclause
736 ORDER BY r.sortorder ASC, ctx.contextlevel ASC, ra.sortorder ASC";
70f15878 737 $rs = get_recordset_sql($sql);
bfbfdb53 738
70f15878 739 // This loop is fairly stupid as it stands - might get better
740 // results doing an initial pass clustering RAs by path.
03cedd62 741 while ($ra = rs_fetch_next_record($rs)) {
742 $user = new StdClass;
743 $user->id = $ra->userid; unset($ra->userid);
744 $user->firstname = $ra->firstname; unset($ra->firstname);
745 $user->lastname = $ra->lastname; unset($ra->lastname);
746 $ra->user = $user;
747 if ($ra->contextlevel == CONTEXT_SYSTEM) {
748 foreach ($courses as $k => $course) {
749 $courses[$k]->managers[] = $ra;
750 }
751 } elseif ($ra->contextlevel == CONTEXT_COURSECAT) {
752 if ($allcats === false) {
753 // It always applies
70f15878 754 foreach ($courses as $k => $course) {
755 $courses[$k]->managers[] = $ra;
756 }
03cedd62 757 } else {
758 foreach ($courses as $k => $course) {
759 // Note that strpos() returns 0 as "matched at pos 0"
760 if (strpos($course->context->path, $ra->path.'/')===0) {
761 // Only add it to subpaths
70f15878 762 $courses[$k]->managers[] = $ra;
763 }
70f15878 764 }
70f15878 765 }
03cedd62 766 } else { // course-level
767 if(!array_key_exists($ra->instanceid, $courses)) {
768 //this course is not in a list, probably a frontpage course
769 continue;
770 }
771 $courses[$ra->instanceid]->managers[] = $ra;
70f15878 772 }
773 }
03cedd62 774 rs_close($rs);
70f15878 775 }
776
777 return $courses;
778}
02ebf404 779
18a97fd8 780/**
bfbfdb53 781 * Convenience function - lists courses that a user has access to view.
fbc21ae8 782 *
82c62d1b 783 * For admins and others with access to "every" course in the system, we should
784 * try to get courses with explicit RAs.
785 *
786 * NOTE: this function is heavily geared towards the perspective of the user
bfbfdb53 787 * passed in $userid. So it will hide courses that the user cannot see
82c62d1b 788 * (for any reason) even if called from cron or from another $USER's
789 * perspective.
bfbfdb53 790 *
82c62d1b 791 * If you really want to know what courses are assigned to the user,
bfbfdb53 792 * without any hiding or scheming, call the lower-level
82c62d1b 793 * get_user_courses_bycap().
794 *
795 *
796 * Notes inherited from get_user_courses_bycap():
e1d5e5c1 797 *
798 * - $fields is an array of fieldnames to ADD
799 * so name the fields you really need, which will
800 * be added and uniq'd
801 *
802 * - the course records have $c->context which is a fully
803 * valid context object. Saves you a query per course!
804 *
352f6f74 805 * @uses $CFG,$USER
7290c7fa 806 * @param int $userid The user of interest
33f85740 807 * @param string $sort the sortorder in the course table
e1d5e5c1 808 * @param array $fields - names of _additional_ fields to return (also accepts a string)
f8e1c7af 809 * @param bool $doanything True if using the doanything flag
810 * @param int $limit Maximum number of records to return, or 0 for unlimited
33f85740 811 * @return array {@link $COURSE} of course objects
fbc21ae8 812 */
e1d5e5c1 813function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NULL, $doanything=false,$limit=0) {
bdf3bbd1 814
352f6f74 815 global $CFG,$USER;
5930cded 816
4dbca99e 817 // Guest's do not have any courses
12d06877 818 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
61f774e8 819 if (has_capability('moodle/legacy:guest',$sitecontext,$userid,false)) {
4dbca99e 820 return(array());
821 }
601edb90 822
352f6f74 823 $basefields = array('id', 'category', 'sortorder',
824 'shortname', 'fullname', 'idnumber',
825 'teacher', 'teachers', 'student', 'students',
826 'guest', 'startdate', 'visible',
827 'newsitems', 'cost', 'enrol',
828 'groupmode', 'groupmodeforce');
829
e1d5e5c1 830 if (!is_null($fields) && is_string($fields)) {
831 if (empty($fields)) {
352f6f74 832 $fields = $basefields;
e1d5e5c1 833 } else {
bfbfdb53 834 // turn the fields from a string to an array that
573674bf 835 // get_user_courses_bycap() will like...
352f6f74 836 $fields = explode(',',$fields);
837 $fields = array_map('trim', $fields);
838 $fields = array_unique(array_merge($basefields, $fields));
839 }
bbfed0ec 840 } elseif (is_array($fields)) {
bfbfdb53 841 $fields = array_unique(array_merge($basefields, $fields));
352f6f74 842 } else {
843 $fields = $basefields;
844 }
845
b9e9491a 846 $orderby = '';
847 $sort = trim($sort);
848 if (!empty($sort)) {
70070493 849 $rawsorts = explode(',', $sort);
850 $sorts = array();
851 foreach ($rawsorts as $rawsort) {
852 $rawsort = trim($rawsort);
c7e6b7e4 853 if (strpos($rawsort, 'c.') === 0) {
70070493 854 $rawsort = substr($rawsort, 2);
855 }
856 $sorts[] = trim($rawsort);
857 }
858 $sort = 'c.'.implode(',c.', $sorts);
b9e9491a 859 $orderby = "ORDER BY $sort";
860 }
861
352f6f74 862 //
863 // Logged-in user - Check cached courses
864 //
865 // NOTE! it's a _string_ because
866 // - it's all we'll ever use
867 // - it serialises much more compact than an array
82c62d1b 868 // this a big concern here - cost of serialise
869 // and unserialise gets huge as the session grows
352f6f74 870 //
871 // If the courses are too many - it won't be set
872 // for large numbers of courses, caching in the session
873 // has marginal benefits (costs too much, not
874 // worthwhile...) and we may hit SQL parser limits
875 // because we use IN()
876 //
ae1555ae 877 if ($userid === $USER->id) {
bfbfdb53 878 if (isset($USER->loginascontext)
fe3141e0 879 && $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
ae1555ae 880 // list _only_ this course
881 // anything else is asking for trouble...
882 $courseids = $USER->loginascontext->instanceid;
bfbfdb53 883 } elseif (isset($USER->mycourses)
ae1555ae 884 && is_string($USER->mycourses)) {
885 if ($USER->mycourses === '') {
886 // empty str means: user has no courses
887 // ... so do the easy thing...
888 return array();
889 } else {
890 $courseids = $USER->mycourses;
891 }
892 }
893 if (isset($courseids)) {
bfbfdb53 894 // The data massaging here MUST be kept in sync with
352f6f74 895 // get_user_courses_bycap() so we return
896 // the same...
897 // (but here we don't need to check has_cap)
898 $coursefields = 'c.' .join(',c.', $fields);
899 $sql = "SELECT $coursefields,
45ea1afb 900 ctx.id AS ctxid, ctx.path AS ctxpath,
901 ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel,
82c62d1b 902 cc.path AS categorypath
352f6f74 903 FROM {$CFG->prefix}course c
82c62d1b 904 JOIN {$CFG->prefix}course_categories cc
905 ON c.category=cc.id
bfbfdb53 906 JOIN {$CFG->prefix}context ctx
352f6f74 907 ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
ae1555ae 908 WHERE c.id IN ($courseids)
b9e9491a 909 $orderby";
352f6f74 910 $rs = get_recordset_sql($sql);
911 $courses = array();
912 $cc = 0; // keep count
03cedd62 913 while ($c = rs_fetch_next_record($rs)) {
914 // build the context obj
915 $c = make_context_subobj($c);
c1b7a5e5 916
03cedd62 917 $courses[$c->id] = $c;
918 if ($limit > 0 && $cc++ > $limit) {
919 break;
352f6f74 920 }
921 }
922 rs_close($rs);
923 return $courses;
2f3499b7 924 }
925 }
152a9060 926
352f6f74 927 // Non-cached - get accessinfo
e1d5e5c1 928 if ($userid === $USER->id && isset($USER->access)) {
aeb3916b 929 $accessinfo = $USER->access;
bdf3bbd1 930 } else {
e1d5e5c1 931 $accessinfo = get_user_access_sitewide($userid);
aeb3916b 932 }
352f6f74 933
bfbfdb53 934
573674bf 935 $courses = get_user_courses_bycap($userid, 'moodle/course:view', $accessinfo,
936 $doanything, $sort, $fields,
937 $limit);
352f6f74 938
82c62d1b 939 $cats = NULL;
940 // If we have to walk category visibility
941 // to eval course visibility, get the categories
942 if (empty($CFG->allowvisiblecoursesinhiddencategories)) {
943 $sql = "SELECT cc.id, cc.path, cc.visible,
45ea1afb 944 ctx.id AS ctxid, ctx.path AS ctxpath,
945 ctx.depth as ctxdepth, ctx.contextlevel AS ctxlevel
115a4311 946 FROM {$CFG->prefix}course_categories cc
947 JOIN {$CFG->prefix}context ctx ON (cc.id = ctx.instanceid)
948 WHERE ctx.contextlevel = ".CONTEXT_COURSECAT."
949 ORDER BY cc.id";
82c62d1b 950 $rs = get_recordset_sql($sql);
bfbfdb53 951
952 // Using a temporary array instead of $cats here, to avoid a "true" result when isnull($cats) further down
953 $categories = array();
03cedd62 954 while ($course_cat = rs_fetch_next_record($rs)) {
955 // build the context obj
956 $course_cat = make_context_subobj($course_cat);
957 $categories[$course_cat->id] = $course_cat;
82c62d1b 958 }
03cedd62 959 rs_close($rs);
bfbfdb53 960
961 if (!empty($categories)) {
962 $cats = $categories;
963 }
964
965 unset($course_cat);
82c62d1b 966 }
352f6f74 967 //
968 // Strangely, get_my_courses() is expected to return the
aeb3916b 969 // array keyed on id, which messes up the sorting
352f6f74 970 // So do that, and also cache the ids in the session if appropriate
971 //
aeb3916b 972 $kcourses = array();
bfbfdb53 973 $courses_count = count($courses);
352f6f74 974 $cacheids = NULL;
82c62d1b 975 $vcatpaths = array();
bfbfdb53 976 if ($userid === $USER->id && $courses_count < 500) {
352f6f74 977 $cacheids = array();
978 }
bfbfdb53 979 for ($n=0; $n<$courses_count; $n++) {
82c62d1b 980
981 //
b00cb46b 982 // Check whether $USER (not $userid) can _actually_ see them
82c62d1b 983 // Easy if $CFG->allowvisiblecoursesinhiddencategories
984 // is set, and we don't have to care about categories.
985 // Lots of work otherwise... (all in mem though!)
986 //
bfbfdb53 987 $cansee = false;
82c62d1b 988 if (is_null($cats)) { // easy rules!
989 if ($courses[$n]->visible == true) {
990 $cansee = true;
991 } elseif (has_capability('moodle/course:viewhiddencourses',
b00cb46b 992 $courses[$n]->context, $USER->id)) {
82c62d1b 993 $cansee = true;
994 }
995 } else {
996 //
997 // Is the cat visible?
998 // we have to assume it _is_ visible
999 // so we can shortcut when we find a hidden one
1000 //
1001 $viscat = true;
1002 $cpath = $courses[$n]->categorypath;
1003 if (isset($vcatpaths[$cpath])) {
1004 $viscat = $vcatpaths[$cpath];
1005 } else {
1006 $cpath = substr($cpath,1); // kill leading slash
1007 $cpath = explode('/',$cpath);
1008 $ccct = count($cpath);
1009 for ($m=0;$m<$ccct;$m++) {
1010 $ccid = $cpath[$m];
1011 if ($cats[$ccid]->visible==false) {
1012 $viscat = false;
1013 break;
1014 }
1015 }
1016 $vcatpaths[$courses[$n]->categorypath] = $viscat;
1017 }
1018
1019 //
b00cb46b 1020 // Perhaps it's actually visible to $USER
82c62d1b 1021 // check moodle/category:visibility
bfbfdb53 1022 //
82c62d1b 1023 // The name isn't obvious, but the description says
1024 // "See hidden categories" so the user shall see...
bfbfdb53 1025 // But also check if the allowvisiblecoursesinhiddencategories setting is true, and check for course visibility
82c62d1b 1026 if ($viscat === false) {
bfbfdb53 1027 $catctx = $cats[$courses[$n]->category]->context;
1028 if (has_capability('moodle/category:visibility', $catctx, $USER->id)) {
82c62d1b 1029 $vcatpaths[$courses[$n]->categorypath] = true;
1030 $viscat = true;
bfbfdb53 1031 } elseif ($CFG->allowvisiblecoursesinhiddencategories && $courses[$n]->visible == true) {
1032 $viscat = true;
82c62d1b 1033 }
1034 }
1035
1036 //
1037 // Decision matrix
1038 //
1039 if ($viscat === true) {
1040 if ($courses[$n]->visible == true) {
1041 $cansee = true;
1042 } elseif (has_capability('moodle/course:viewhiddencourses',
b00cb46b 1043 $courses[$n]->context, $USER->id)) {
82c62d1b 1044 $cansee = true;
1045 }
1046 }
1047 }
1048 if ($cansee === true) {
1049 $kcourses[$courses[$n]->id] = $courses[$n];
1050 if (is_array($cacheids)) {
1051 $cacheids[] = $courses[$n]->id;
1052 }
352f6f74 1053 }
1054 }
1055 if (is_array($cacheids)) {
1056 // Only happens
1057 // - for the logged in user
1058 // - below the threshold (500)
1059 // empty string is _valid_
1060 $USER->mycourses = join(',',$cacheids);
1061 } elseif ($userid === $USER->id && isset($USER->mycourses)) {
1062 // cheap sanity check
1063 unset($USER->mycourses);
aeb3916b 1064 }
352f6f74 1065
aeb3916b 1066 return $kcourses;
02ebf404 1067}
1068
18a97fd8 1069/**
7290c7fa 1070 * A list of courses that match a search
fbc21ae8 1071 *
1072 * @uses $CFG
1073 * @param array $searchterms ?
1074 * @param string $sort ?
1075 * @param int $page ?
1076 * @param int $recordsperpage ?
1077 * @param int $totalcount Passed in by reference. ?
7290c7fa 1078 * @return object {@link $COURSE} records
fbc21ae8 1079 */
d4419d55 1080function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $recordsperpage=50, &$totalcount) {
02ebf404 1081
1082 global $CFG;
1083
18a97fd8 1084 //to allow case-insensitive search for postgesql
48505662 1085 if ($CFG->dbfamily == 'postgres') {
d4419d55 1086 $LIKE = 'ILIKE';
1087 $NOTLIKE = 'NOT ILIKE'; // case-insensitive
1088 $REGEXP = '~*';
1089 $NOTREGEXP = '!~*';
02ebf404 1090 } else {
d4419d55 1091 $LIKE = 'LIKE';
1092 $NOTLIKE = 'NOT LIKE';
1093 $REGEXP = 'REGEXP';
1094 $NOTREGEXP = 'NOT REGEXP';
02ebf404 1095 }
1096
d4419d55 1097 $fullnamesearch = '';
1098 $summarysearch = '';
02ebf404 1099
02ebf404 1100 foreach ($searchterms as $searchterm) {
6bb0f67f 1101
0f62a5b5 1102 $NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
1103 /// will use it to simulate the "-" operator with LIKE clause
1104
6bb0f67f 1105 /// Under Oracle and MSSQL, trim the + and - operators and perform
0f62a5b5 1106 /// simpler LIKE (or NOT LIKE) queries
48505662 1107 if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') {
0f62a5b5 1108 if (substr($searchterm, 0, 1) == '-') {
1109 $NOT = ' NOT ';
1110 }
6bb0f67f 1111 $searchterm = trim($searchterm, '+-');
1112 }
1113
02ebf404 1114 if ($fullnamesearch) {
d4419d55 1115 $fullnamesearch .= ' AND ';
02ebf404 1116 }
02ebf404 1117 if ($summarysearch) {
d4419d55 1118 $summarysearch .= ' AND ';
02ebf404 1119 }
a8b56716 1120
d4419d55 1121 if (substr($searchterm,0,1) == '+') {
2c64f65c 1122 $searchterm = substr($searchterm,1);
1123 $summarysearch .= " c.summary $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1124 $fullnamesearch .= " c.fullname $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
a8b56716 1125 } else if (substr($searchterm,0,1) == "-") {
2c64f65c 1126 $searchterm = substr($searchterm,1);
1127 $summarysearch .= " c.summary $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1128 $fullnamesearch .= " c.fullname $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
a8b56716 1129 } else {
0f62a5b5 1130 $summarysearch .= ' summary '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
1131 $fullnamesearch .= ' fullname '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
a8b56716 1132 }
1133
02ebf404 1134 }
1135
2c64f65c 1136 $sql = "SELECT c.*,
45ea1afb 1137 ctx.id AS ctxid, ctx.path AS ctxpath,
1138 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
2c64f65c 1139 FROM {$CFG->prefix}course c
1140 JOIN {$CFG->prefix}context ctx
1141 ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
0f62a5b5 1142 WHERE (( $fullnamesearch ) OR ( $summarysearch ))
2c64f65c 1143 AND category > 0
1144 ORDER BY " . $sort;
02ebf404 1145
2c64f65c 1146 $courses = array();
02ebf404 1147
2c64f65c 1148 if ($rs = get_recordset_sql($sql)) {
bfbfdb53 1149
2c64f65c 1150
1151 // Tiki pagination
1152 $limitfrom = $page * $recordsperpage;
1153 $limitto = $limitfrom + $recordsperpage;
1154 $c = 0; // counts how many visible courses we've seen
1155
1156 while ($course = rs_fetch_next_record($rs)) {
1157 $course = make_context_subobj($course);
1158 if ($course->visible || has_capability('moodle/course:viewhiddencourses', $course->context)) {
1159 // Don't exit this loop till the end
1160 // we need to count all the visible courses
1161 // to update $totalcount
1162 if ($c >= $limitfrom && $c < $limitto) {
1163 $courses[] = $course;
02ebf404 1164 }
2c64f65c 1165 $c++;
02ebf404 1166 }
1167 }
1168 }
1169
2c64f65c 1170 // our caller expects 2 bits of data - our return
1171 // array, and an updated $totalcount
1172 $totalcount = $c;
02ebf404 1173 return $courses;
1174}
1175
1176
18a97fd8 1177/**
40fb8aa6 1178 * Returns a sorted list of categories. Each category object has a context
1179 * property that is a context object.
bfbfdb53 1180 *
40fb8aa6 1181 * When asking for $parent='none' it will return all the categories, regardless
1182 * of depth. Wheen asking for a specific parent, the default is to return
1183 * a "shallow" resultset. Pass false to $shallow and it will return all
bfbfdb53 1184 * the child categories as well.
1185 *
fbc21ae8 1186 *
613bbd7c 1187 * @param string $parent The parent category if any
1188 * @param string $sort the sortorder
40fb8aa6 1189 * @param bool $shallow - set to false to get the children too
613bbd7c 1190 * @return array of categories
fbc21ae8 1191 */
40fb8aa6 1192function get_categories($parent='none', $sort=NULL, $shallow=true) {
1193 global $CFG;
1194
1195 if ($sort === NULL) {
1196 $sort = 'ORDER BY cc.sortorder ASC';
1197 } elseif ($sort ==='') {
1198 // leave it as empty
1199 } else {
1200 $sort = "ORDER BY $sort";
1201 }
02ebf404 1202
814748c9 1203 if ($parent === 'none') {
40fb8aa6 1204 $sql = "SELECT cc.*,
45ea1afb 1205 ctx.id AS ctxid, ctx.path AS ctxpath,
1206 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
40fb8aa6 1207 FROM {$CFG->prefix}course_categories cc
1208 JOIN {$CFG->prefix}context ctx
1209 ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
1210 $sort";
1211 } elseif ($shallow) {
1212 $parent = (int)$parent;
1213 $sql = "SELECT cc.*,
45ea1afb 1214 ctx.id AS ctxid, ctx.path AS ctxpath,
1215 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
40fb8aa6 1216 FROM {$CFG->prefix}course_categories cc
1217 JOIN {$CFG->prefix}context ctx
1218 ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
1219 WHERE cc.parent=$parent
1220 $sort";
02ebf404 1221 } else {
40fb8aa6 1222 $parent = (int)$parent;
1223 $sql = "SELECT cc.*,
45ea1afb 1224 ctx.id AS ctxid, ctx.path AS ctxpath,
1225 ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
40fb8aa6 1226 FROM {$CFG->prefix}course_categories cc
1227 JOIN {$CFG->prefix}context ctx
1228 ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
1229 JOIN {$CFG->prefix}course_categories ccp
44f1636e 1230 ON (cc.path LIKE ".sql_concat('ccp.path',"'%'").")
40fb8aa6 1231 WHERE ccp.id=$parent
1232 $sort";
02ebf404 1233 }
40fb8aa6 1234 $categories = array();
1235
3b0ee367 1236 if( $rs = get_recordset_sql($sql) ){
40fb8aa6 1237 while ($cat = rs_fetch_next_record($rs)) {
1238 $cat = make_context_subobj($cat);
1239 if ($cat->visible || has_capability('moodle/course:create',$cat->context)) {
1240 $categories[$cat->id] = $cat;
02ebf404 1241 }
1242 }
1243 }
1244 return $categories;
1245}
1246
1247
2327b9df 1248/**
1249 * Returns an array of category ids of all the subcategories for a given
1250 * category.
1251 * @param $catid - The id of the category whose subcategories we want to find.
1252 * @return array of category ids.
1253 */
1254function get_all_subcategories($catid) {
1255
1256 $subcats = array();
1257
1258 if ($categories = get_records('course_categories', 'parent', $catid)) {
1259 foreach ($categories as $cat) {
1260 array_push($subcats, $cat->id);
1261 $subcats = array_merge($subcats, get_all_subcategories($cat->id));
1262 }
1263 }
1264 return $subcats;
1265}
1266
1267
18a97fd8 1268/**
ba87a4da 1269* This recursive function makes sure that the courseorder is consecutive
1270*
1271* @param type description
1272*
1273* $n is the starting point, offered only for compatilibity -- will be ignored!
1274* $safe (bool) prevents it from assuming category-sortorder is unique, used to upgrade
1275* safely from 1.4 to 1.5
1276*/
f41ef63e 1277function fix_course_sortorder($categoryid=0, $n=0, $safe=0, $depth=0, $path='') {
5930cded 1278
ba87a4da 1279 global $CFG;
8f0cd6ef 1280
02ebf404 1281 $count = 0;
5930cded 1282
f41ef63e 1283 $catgap = 1000; // "standard" category gap
1284 $tolerance = 200; // how "close" categories can get
5930cded 1285
f41ef63e 1286 if ($categoryid > 0){
1287 // update depth and path
1288 $cat = get_record('course_categories', 'id', $categoryid);
1289 if ($cat->parent == 0) {
1290 $depth = 0;
1291 $path = '';
1292 } else if ($depth == 0 ) { // doesn't make sense; get from DB
1293 // this is only called if the $depth parameter looks dodgy
1294 $parent = get_record('course_categories', 'id', $cat->parent);
1295 $path = $parent->path;
1296 $depth = $parent->depth;
1297 }
1298 $path = $path . '/' . $categoryid;
1299 $depth = $depth + 1;
ba87a4da 1300
c5d13b68 1301 if ($cat->path !== $path) {
1302 set_field('course_categories', 'path', addslashes($path), 'id', $categoryid);
1303 }
1304 if ($cat->depth != $depth) {
1305 set_field('course_categories', 'depth', $depth, 'id', $categoryid);
1306 }
f41ef63e 1307 }
39f65595 1308
1309 // get some basic info about courses in the category
5930cded 1310 $info = get_record_sql('SELECT MIN(sortorder) AS min,
ba87a4da 1311 MAX(sortorder) AS max,
5930cded 1312 COUNT(sortorder) AS count
1313 FROM ' . $CFG->prefix . 'course
ba87a4da 1314 WHERE category=' . $categoryid);
1315 if (is_object($info)) { // no courses?
1316 $max = $info->max;
1317 $count = $info->count;
1318 $min = $info->min;
1319 unset($info);
1320 }
1321
814748c9 1322 if ($categoryid > 0 && $n==0) { // only passed category so don't shift it
1323 $n = $min;
1324 }
1325
39f65595 1326 // $hasgap flag indicates whether there's a gap in the sequence
5930cded 1327 $hasgap = false;
39f65595 1328 if ($max-$min+1 != $count) {
1329 $hasgap = true;
1330 }
5930cded 1331
39f65595 1332 // $mustshift indicates whether the sequence must be shifted to
1333 // meet its range
1334 $mustshift = false;
1335 if ($min < $n+$tolerance || $min > $n+$tolerance+$catgap ) {
1336 $mustshift = true;
1337 }
1338
ba87a4da 1339 // actually sort only if there are courses,
1340 // and we meet one ofthe triggers:
1341 // - safe flag
1342 // - they are not in a continuos block
1343 // - they are too close to the 'bottom'
39f65595 1344 if ($count && ( $safe || $hasgap || $mustshift ) ) {
1345 // special, optimized case where all we need is to shift
1346 if ( $mustshift && !$safe && !$hasgap) {
1347 $shift = $n + $catgap - $min;
f8ea6077 1348 if ($shift < $count) {
1349 $shift = $count + $catgap;
1350 }
39f65595 1351 // UPDATE course SET sortorder=sortorder+$shift
5930cded 1352 execute_sql("UPDATE {$CFG->prefix}course
1353 SET sortorder=sortorder+$shift
39f65595 1354 WHERE category=$categoryid", 0);
5930cded 1355 $n = $n + $catgap + $count;
1356
39f65595 1357 } else { // do it slowly
5930cded 1358 $n = $n + $catgap;
39f65595 1359 // if the new sequence overlaps the current sequence, lack of transactions
1360 // will stop us -- shift things aside for a moment...
48505662 1361 if ($safe || ($n >= $min && $n+$count+1 < $min && $CFG->dbfamily==='mysql')) {
d6a49dab 1362 $shift = $max + $n + 1000;
5930cded 1363 execute_sql("UPDATE {$CFG->prefix}course
1364 SET sortorder=sortorder+$shift
39f65595 1365 WHERE category=$categoryid", 0);
ba87a4da 1366 }
1367
39f65595 1368 $courses = get_courses($categoryid, 'c.sortorder ASC', 'c.id,c.sortorder');
1369 begin_sql();
f8ea6077 1370 $tx = true; // transaction sanity
5930cded 1371 foreach ($courses as $course) {
f8ea6077 1372 if ($tx && $course->sortorder != $n ) { // save db traffic
1373 $tx = $tx && set_field('course', 'sortorder', $n,
1374 'id', $course->id);
ba87a4da 1375 }
1376 $n++;
1377 }
f8ea6077 1378 if ($tx) {
1379 commit_sql();
1380 } else {
1381 rollback_sql();
1382 if (!$safe) {
1383 // if we failed when called with !safe, try
1384 // to recover calling self with safe=true
1385 return fix_course_sortorder($categoryid, $n, true, $depth, $path);
1386 }
1387 }
5930cded 1388 }
02ebf404 1389 }
d4419d55 1390 set_field('course_categories', 'coursecount', $count, 'id', $categoryid);
8f0cd6ef 1391
5930cded 1392 // $n could need updating
814748c9 1393 $max = get_field_sql("SELECT MAX(sortorder) from {$CFG->prefix}course WHERE category=$categoryid");
1394 if ($max > $n) {
1395 $n = $max;
1396 }
758b9a4d 1397
6bc502cc 1398 if ($categories = get_categories($categoryid)) {
1399 foreach ($categories as $category) {
f41ef63e 1400 $n = fix_course_sortorder($category->id, $n, $safe, $depth, $path);
6bc502cc 1401 }
1402 }
8f0cd6ef 1403
39f65595 1404 return $n+1;
02ebf404 1405}
1406
d8634192 1407/**
1408 * Ensure all courses have a valid course category
1409 * useful if a category has been removed manually
1410 **/
1411function fix_coursecategory_orphans() {
1412
1413 global $CFG;
1414
1415 // Note: the handling of sortorder here is arguably
1416 // open to race conditions. Hard to fix here, unlikely
1417 // to hit anyone in production.
1418
1419 $sql = "SELECT c.id, c.category, c.shortname
1420 FROM {$CFG->prefix}course c
1421 LEFT OUTER JOIN {$CFG->prefix}course_categories cc ON c.category=cc.id
1422 WHERE cc.id IS NULL AND c.id != " . SITEID;
1423
1424 $rs = get_recordset_sql($sql);
1425
03cedd62 1426 if (!rs_EOF($rs)) { // we have some orphans
d8634192 1427
1428 // the "default" category is the lowest numbered...
1429 $default = get_field_sql("SELECT MIN(id)
1430 FROM {$CFG->prefix}course_categories");
1431 $sortorder = get_field_sql("SELECT MAX(sortorder)
1432 FROM {$CFG->prefix}course
1433 WHERE category=$default");
1434
1435
1436 begin_sql();
1437 $tx = true;
1438 while ($tx && $course = rs_fetch_next_record($rs)) {
1439 $tx = $tx && set_field('course', 'category', $default, 'id', $course->id);
1440 $tx = $tx && set_field('course', 'sortorder', ++$sortorder, 'id', $course->id);
1441 }
1442 if ($tx) {
1443 commit_sql();
1444 } else {
1445 rollback_sql();
1446 }
1447 }
03cedd62 1448 rs_close($rs);
d8634192 1449}
1450
db4b12eb 1451/**
1452 * List of remote courses that a user has access to via MNET.
1453 * Works only on the IDP
1454 *
1455 * @uses $CFG, $USER
1456 * @return array {@link $COURSE} of course objects
1457 */
1458function get_my_remotecourses($userid=0) {
1459 global $CFG, $USER;
1460
1461 if (empty($userid)) {
1462 $userid = $USER->id;
1463 }
1464
5930cded 1465 $sql = "SELECT c.remoteid, c.shortname, c.fullname,
86dd62a7 1466 c.hostid, c.summary, c.cat_name,
1467 h.name AS hostname
db4b12eb 1468 FROM {$CFG->prefix}mnet_enrol_course c
1469 JOIN {$CFG->prefix}mnet_enrol_assignments a ON c.id=a.courseid
86dd62a7 1470 JOIN {$CFG->prefix}mnet_host h ON c.hostid=h.id
db4b12eb 1471 WHERE a.userid={$userid}";
1472
1473 return get_records_sql($sql);
1474}
1475
1476/**
1477 * List of remote hosts that a user has access to via MNET.
1478 * Works on the SP
1479 *
1480 * @uses $CFG, $USER
1481 * @return array of host objects
1482 */
1483function get_my_remotehosts() {
1484 global $CFG, $USER;
1485
1486 if ($USER->mnethostid == $CFG->mnet_localhost_id) {
1487 return false; // Return nothing on the IDP
1488 }
1489 if (!empty($USER->mnet_foreign_host_array) && is_array($USER->mnet_foreign_host_array)) {
1490 return $USER->mnet_foreign_host_array;
1491 }
1492 return false;
1493}
fbc21ae8 1494
18a97fd8 1495/**
fbc21ae8 1496 * This function creates a default separated/connected scale
1497 *
1498 * This function creates a default separated/connected scale
1499 * so there's something in the database. The locations of
1500 * strings and files is a bit odd, but this is because we
1501 * need to maintain backward compatibility with many different
1502 * existing language translations and older sites.
1503 *
1504 * @uses $CFG
1505 */
02ebf404 1506function make_default_scale() {
02ebf404 1507
1508 global $CFG;
1509
1510 $defaultscale = NULL;
1511 $defaultscale->courseid = 0;
1512 $defaultscale->userid = 0;
d4419d55 1513 $defaultscale->name = get_string('separateandconnected');
1514 $defaultscale->scale = get_string('postrating1', 'forum').','.
1515 get_string('postrating2', 'forum').','.
1516 get_string('postrating3', 'forum');
02ebf404 1517 $defaultscale->timemodified = time();
1518
8f0cd6ef 1519 /// Read in the big description from the file. Note this is not
02ebf404 1520 /// HTML (despite the file extension) but Moodle format text.
f191a887 1521 $parentlang = get_string('parentlanguage');
1522 if ($parentlang[0] == '[') {
1523 $parentlang = '';
1524 }
ee6e91d4 1525 if (is_readable($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
1526 $file = file($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
1527 } else if (is_readable($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) {
d4419d55 1528 $file = file($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html');
ee6e91d4 1529 } else if ($parentlang and is_readable($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
1530 $file = file($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
d4419d55 1531 } else if ($parentlang and is_readable($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) {
1532 $file = file($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html');
ee6e91d4 1533 } else if (is_readable($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html')) {
1534 $file = file($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html');
02ebf404 1535 } else {
d4419d55 1536 $file = '';
02ebf404 1537 }
1538
d4419d55 1539 $defaultscale->description = addslashes(implode('', $file));
02ebf404 1540
d4419d55 1541 if ($defaultscale->id = insert_record('scale', $defaultscale)) {
1542 execute_sql('UPDATE '. $CFG->prefix .'forum SET scale = \''. $defaultscale->id .'\'', false);
02ebf404 1543 }
1544}
1545
fbc21ae8 1546
18a97fd8 1547/**
fbc21ae8 1548 * Returns a menu of all available scales from the site as well as the given course
1549 *
1550 * @uses $CFG
1551 * @param int $courseid The id of the course as found in the 'course' table.
7290c7fa 1552 * @return object
fbc21ae8 1553 */
02ebf404 1554function get_scales_menu($courseid=0) {
02ebf404 1555
1556 global $CFG;
8f0cd6ef 1557
1558 $sql = "SELECT id, name FROM {$CFG->prefix}scale
1559 WHERE courseid = '0' or courseid = '$courseid'
02ebf404 1560 ORDER BY courseid ASC, name ASC";
1561
d4419d55 1562 if ($scales = get_records_sql_menu($sql)) {
02ebf404 1563 return $scales;
1564 }
1565
1566 make_default_scale();
1567
d4419d55 1568 return get_records_sql_menu($sql);
02ebf404 1569}
1570
5baa0ad6 1571
1572
1573/**
1574 * Given a set of timezone records, put them in the database, replacing what is there
1575 *
1576 * @uses $CFG
1577 * @param array $timezones An array of timezone records
1578 */
1579function update_timezone_records($timezones) {
1580/// Given a set of timezone records, put them in the database
1581
1582 global $CFG;
1583
1584/// Clear out all the old stuff
1585 execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone', false);
1586
1587/// Insert all the new stuff
1588 foreach ($timezones as $timezone) {
a599aeeb 1589 if (is_array($timezone)) {
1590 $timezone = (object)$timezone;
1591 }
5baa0ad6 1592 insert_record('timezone', $timezone);
1593 }
1594}
1595
1596
df28d6c5 1597/// MODULE FUNCTIONS /////////////////////////////////////////////////
1598
18a97fd8 1599/**
fbc21ae8 1600 * Just gets a raw list of all modules in a course
1601 *
1602 * @uses $CFG
1603 * @param int $courseid The id of the course as found in the 'course' table.
7290c7fa 1604 * @return object
fbc21ae8 1605 */
9fa49e22 1606function get_course_mods($courseid) {
9fa49e22 1607 global $CFG;
1608
3a11c548 1609 if (empty($courseid)) {
1610 return false; // avoid warnings
1611 }
1612
7acaa63d 1613 return get_records_sql("SELECT cm.*, m.name as modname
8f0cd6ef 1614 FROM {$CFG->prefix}modules m,
7acaa63d 1615 {$CFG->prefix}course_modules cm
5b1dbd5c 1616 WHERE cm.course = ".intval($courseid)."
b4619aba 1617 AND cm.module = m.id AND m.visible = 1"); // no disabled mods
9fa49e22 1618}
1619
fbc21ae8 1620
18a97fd8 1621/**
f9d5371b 1622 * Given an id of a course module, finds the coursemodule description
fbc21ae8 1623 *
f9d5371b 1624 * @param string $modulename name of module type, eg. resource, assignment,...
1625 * @param int $cmid course module id (id in course_modules table)
1626 * @param int $courseid optional course id for extra validation
1627 * @return object course module instance with instance and module name
1628 */
1629function get_coursemodule_from_id($modulename, $cmid, $courseid=0) {
1630
1631 global $CFG;
1632
5b1dbd5c 1633 $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
f9d5371b 1634
1635 return get_record_sql("SELECT cm.*, m.name, md.name as modname
1636 FROM {$CFG->prefix}course_modules cm,
1637 {$CFG->prefix}modules md,
1638 {$CFG->prefix}$modulename m
1639 WHERE $courseselect
5b1dbd5c 1640 cm.id = ".intval($cmid)." AND
f9d5371b 1641 cm.instance = m.id AND
1642 md.name = '$modulename' AND
1643 md.id = cm.module");
1644}
1645
1646/**
1647 * Given an instance number of a module, finds the coursemodule description
1648 *
1649 * @param string $modulename name of module type, eg. resource, assignment,...
1650 * @param int $instance module instance number (id in resource, assignment etc. table)
1651 * @param int $courseid optional course id for extra validation
1652 * @return object course module instance with instance and module name
fbc21ae8 1653 */
b63c0ee5 1654function get_coursemodule_from_instance($modulename, $instance, $courseid=0) {
df28d6c5 1655
1656 global $CFG;
f9d5371b 1657
5b1dbd5c 1658 $courseselect = ($courseid) ? 'cm.course = '.intval($courseid).' AND ' : '';
df28d6c5 1659
f9d5371b 1660 return get_record_sql("SELECT cm.*, m.name, md.name as modname
8f0cd6ef 1661 FROM {$CFG->prefix}course_modules cm,
1662 {$CFG->prefix}modules md,
1663 {$CFG->prefix}$modulename m
b63c0ee5 1664 WHERE $courseselect
8f0cd6ef 1665 cm.instance = m.id AND
1666 md.name = '$modulename' AND
df28d6c5 1667 md.id = cm.module AND
5b1dbd5c 1668 m.id = ".intval($instance));
df28d6c5 1669
1670}
1671
dd97c328 1672/**
1673 * Returns all course modules of given activity in course
1674 * @param string $modulename (forum, quiz, etc.)
1675 * @param int $courseid
1676 * @param string $extrafields extra fields starting with m.
1677 * @return array of cm objects, false if not found or error
1678 */
1679function get_coursemodules_in_course($modulename, $courseid, $extrafields='') {
1680 global $CFG;
1681
1682 if (!empty($extrafields)) {
1683 $extrafields = ", $extrafields";
1684 }
1685 return get_records_sql("SELECT cm.*, m.name, md.name as modname $extrafields
1686 FROM {$CFG->prefix}course_modules cm,
1687 {$CFG->prefix}modules md,
1688 {$CFG->prefix}$modulename m
1689 WHERE cm.course = $courseid AND
1690 cm.instance = m.id AND
1691 md.name = '$modulename' AND
1692 md.id = cm.module");
1693}
ac0b1a19 1694
185cfb09 1695/**
1696 * Returns an array of all the active instances of a particular module in given courses, sorted in the order they are defined
1697 *
1698 * Returns an array of all the active instances of a particular
1699 * module in given courses, sorted in the order they are defined
ac0b1a19 1700 * in the course. Returns an empty array on any errors.
185cfb09 1701 *
ac0b1a19 1702 * The returned objects includle the columns cw.section, cm.visible,
1703 * cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
1704 *
1705 * @param string $modulename The name of the module to get instances for
1706 * @param array $courses an array of course objects.
1707 * @return array of module instance objects, including some extra fields from the course_modules
1708 * and course_sections tables, or an empty array if an error occurred.
185cfb09 1709 */
00e12c73 1710function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $includeinvisible=false) {
185cfb09 1711 global $CFG;
ac0b1a19 1712
1713 $outputarray = array();
1714
185cfb09 1715 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
ac0b1a19 1716 return $outputarray;
185cfb09 1717 }
ac0b1a19 1718
1719 if (!$rawmods = get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
1720 cm.groupmode, cm.groupingid, cm.groupmembersonly
1721 FROM {$CFG->prefix}course_modules cm,
1722 {$CFG->prefix}course_sections cw,
1723 {$CFG->prefix}modules md,
1724 {$CFG->prefix}$modulename m
1725 WHERE cm.course IN (".implode(',',array_keys($courses)).") AND
1726 cm.instance = m.id AND
1727 cm.section = cw.id AND
1728 md.name = '$modulename' AND
1729 md.id = cm.module")) {
1730 return $outputarray;
185cfb09 1731 }
1732
ac0b1a19 1733 require_once($CFG->dirroot.'/course/lib.php');
185cfb09 1734
1735 foreach ($courses as $course) {
ac0b1a19 1736 $modinfo = get_fast_modinfo($course, $userid);
fea43a7f 1737
ac0b1a19 1738 if (empty($modinfo->instances[$modulename])) {
185cfb09 1739 continue;
1740 }
ac0b1a19 1741
1742 foreach ($modinfo->instances[$modulename] as $cm) {
1743 if (!$includeinvisible and !$cm->uservisible) {
1744 continue;
1745 }
1746 if (!isset($rawmods[$cm->id])) {
1747 continue;
185cfb09 1748 }
ac0b1a19 1749 $instance = $rawmods[$cm->id];
1750 if (!empty($cm->extra)) {
1751 $instance->extra = urlencode($cm->extra); // bc compatibility
1752 }
1753 $outputarray[] = $instance;
185cfb09 1754 }
1755 }
1756
1757 return $outputarray;
185cfb09 1758}
fbc21ae8 1759
18a97fd8 1760/**
3d96cba7 1761 * Returns an array of all the active instances of a particular module in a given course,
1762 * sorted in the order they are defined.
fbc21ae8 1763 *
1764 * Returns an array of all the active instances of a particular
1765 * module in a given course, sorted in the order they are defined
3d96cba7 1766 * in the course. Returns an empty array on any errors.
1767 *
1768 * The returned objects includle the columns cw.section, cm.visible,
ac0b1a19 1769 * cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
fbc21ae8 1770 *
3d96cba7 1771 * @param string $modulename The name of the module to get instances for
ac0b1a19 1772 * @param object $course The course obect.
3d96cba7 1773 * @return array of module instance objects, including some extra fields from the course_modules
1774 * and course_sections tables, or an empty array if an error occurred.
fbc21ae8 1775 */
00e12c73 1776function get_all_instances_in_course($modulename, $course, $userid=NULL, $includeinvisible=false) {
ac0b1a19 1777 return get_all_instances_in_courses($modulename, array($course->id => $course), $userid, $includeinvisible);
df28d6c5 1778}
1779
9fa49e22 1780
18a97fd8 1781/**
fbc21ae8 1782 * Determine whether a module instance is visible within a course
1783 *
1784 * Given a valid module object with info about the id and course,
1785 * and the module's type (eg "forum") returns whether the object
dd97c328 1786 * is visible or not, groupmembersonly visibility not tested
fbc21ae8 1787 *
1788 * @uses $CFG
613bbd7c 1789 * @param $moduletype Name of the module eg 'forum'
1790 * @param $module Object which is the instance of the module
7290c7fa 1791 * @return bool
fbc21ae8 1792 */
580f2fbc 1793function instance_is_visible($moduletype, $module) {
580f2fbc 1794
1795 global $CFG;
1796
2b49ae96 1797 if (!empty($module->id)) {
e6839677 1798 if ($records = get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course
2b49ae96 1799 FROM {$CFG->prefix}course_modules cm,
1800 {$CFG->prefix}modules m
1801 WHERE cm.course = '$module->course' AND
1802 cm.module = m.id AND
1803 m.name = '$moduletype' AND
1804 cm.instance = '$module->id'")) {
5930cded 1805
2b49ae96 1806 foreach ($records as $record) { // there should only be one - use the first one
dd97c328 1807 return $record->visible;
2b49ae96 1808 }
580f2fbc 1809 }
1810 }
580f2fbc 1811 return true; // visible by default!
1812}
1813
dd97c328 1814/**
1815 * Determine whether a course module is visible within a course,
1816 * this is different from instance_is_visible() - faster and visibility for user
1817 *
1818 * @param object $cm object
1819 * @param int $userid empty means current user
1820 * @return bool
1821 */
1822function coursemodule_visible_for_user($cm, $userid=0) {
1823 global $USER;
1824
1825 if (empty($cm->id)) {
1826 debugging("Incorrect course module parameter!", DEBUG_DEVELOPER);
1827 return false;
1828 }
1829 if (empty($userid)) {
1830 $userid = $USER->id;
1831 }
1832 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
1833 return false;
1834 }
1835 return groups_course_module_visible($cm, $userid);
1836}
1837
a3fb1c45 1838
1839
1840
9fa49e22 1841/// LOG FUNCTIONS /////////////////////////////////////////////////////
1842
1843
18a97fd8 1844/**
fbc21ae8 1845 * Add an entry to the log table.
1846 *
1847 * Add an entry to the log table. These are "action" focussed rather
1848 * than web server hits, and provide a way to easily reconstruct what
1849 * any particular student has been doing.
1850 *
1851 * @uses $CFG
1852 * @uses $USER
1853 * @uses $db
1854 * @uses $REMOTE_ADDR
1855 * @uses SITEID
89dcb99d 1856 * @param int $courseid The course id
fbc21ae8 1857 * @param string $module The module name - e.g. forum, journal, resource, course, user etc
f7664880 1858 * @param string $action 'view', 'update', 'add' or 'delete', possibly followed by another word to clarify.
fbc21ae8 1859 * @param string $url The file and parameters used to see the results of the action
1860 * @param string $info Additional description information
1861 * @param string $cm The course_module->id if there is one
1862 * @param string $user If log regards $user other than $USER
1863 */
d4419d55 1864function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) {
e8395a09 1865 // Note that this function intentionally does not follow the normal Moodle DB access idioms.
1866 // This is for a good reason: it is the most frequently used DB update function,
1867 // so it has been optimised for speed.
f33e1ed4 1868 global $DB, $CFG, $USER;
9fa49e22 1869
7a5b1fc5 1870 if ($cm === '' || is_null($cm)) { // postgres won't translate empty string to its default
f78b3c34 1871 $cm = 0;
1872 }
1873
3d94772d 1874 if ($user) {
1875 $userid = $user;
1876 } else {
cb80265b 1877 if (!empty($USER->realuser)) { // Don't log
3d94772d 1878 return;
1879 }
d4419d55 1880 $userid = empty($USER->id) ? '0' : $USER->id;
9fa49e22 1881 }
1882
fcaff7ff 1883 $REMOTE_ADDR = getremoteaddr();
1884
9fa49e22 1885 $timenow = time();
1886 $info = addslashes($info);
10a760b9 1887 if (!empty($url)) { // could break doing html_entity_decode on an empty var.
1888 $url = html_entity_decode($url); // for php < 4.3.0 this is defined in moodlelib.php
1889 }
853df85e 1890
6c5a2108 1891 // Restrict length of log lines to the space actually available in the
1892 // database so that it doesn't cause a DB error. Log a warning so that
1893 // developers can avoid doing things which are likely to cause this on a
1894 // routine basis.
1895 $tl=textlib_get_instance();
1896 if(!empty($info) && $tl->strlen($info)>255) {
1897 $info=$tl->substr($info,0,252).'...';
1898 debugging('Warning: logged very long info',DEBUG_DEVELOPER);
1899 }
1900 // Note: Unlike $info, URL appears to be already slashed before this function
1901 // is called. Since database limits are for the data before slashes, we need
1902 // to remove them...
1903 $url=stripslashes($url);
1904 // If the 100 field size is changed, also need to alter print_log in course/lib.php
1905 if(!empty($url) && $tl->strlen($url)>100) {
1906 $url=$tl->substr($url,0,97).'...';
1907 debugging('Warning: logged very long URL',DEBUG_DEVELOPER);
1908 }
1909 $url=addslashes($url);
1910
f33e1ed4 1911 if (defined('MDL_PERFDB')) { global $PERF ; $PERF->logwrites++;};
853df85e 1912
8b497bbc 1913 if ($CFG->type = 'oci8po') {
f33e1ed4 1914 if ($info == '') {
8b497bbc 1915 $info = ' ';
1916 }
1917 }
f33e1ed4 1918 $log = array('time'=>$timenow, 'userid'=>$userid, 'course'=>$courseid, 'ip'=>$REMOTE_ADDR, 'module'=>$module,
1919 'cmid'=>$cm, 'action'=>$action, 'url'=>$url, 'info'=>$info);
1920 $result = $DB->insert_record_raw('log', $log, false);
ebc3bd2b 1921
9f064546 1922 // MDL-11893, alert $CFG->supportemail if insert into log failed
f33e1ed4 1923 if (!$result and $CFG->supportemail and empty($CFG->noemailever)) {
1924 // email_to_user is not usable because email_to_user tries to write to the logs table,
1925 // and this will get caught in an infinite loop, if disk is full
9f064546 1926 $site = get_site();
1927 $subject = 'Insert into log failed at your moodle site '.$site->fullname;
0fdf06cd 1928 $message = "Insert into log table failed at ". date('l dS \of F Y h:i:s A') .".\n It is possible that your disk is full.\n\n";
f33e1ed4 1929 $message .= "The failed query parameters are:\n\n" . var_export($log, true);
0fdf06cd 1930
f33e1ed4 1931 $lasttime = get_config('admin', 'lastloginserterrormail');
1932 if(empty($lasttime) || time() - $lasttime > 60*60*24) { // limit to 1 email per day
1933 mail($CFG->supportemail, $subject, $message);
1934 set_config('lastloginserterrormail', time(), 'admin');
58538527 1935 }
9f064546 1936 }
1937
252720c4 1938 if (!$result) {
1939 debugging('Error: Could not insert a new entry to the Moodle log', DEBUG_ALL);
8f0cd6ef 1940 }
cb80265b 1941
341b5ed2 1942}
1943
1944/**
1945 * Store user last access times - called when use enters a course or site
1946 *
1947 * Note: we use ADOdb code directly in this function to save some CPU
1948 * cycles here and there. They are simple operations not needing any
1949 * of the postprocessing performed by dmllib.php
1950 *
1951 * @param int $courseid, empty means site
1952 * @return void
1953 */
1954function user_accesstime_log($courseid=0) {
1955
f33e1ed4 1956 global $USER, $CFG, $DB;
341b5ed2 1957
1958 if (!isloggedin() or !empty($USER->realuser)) {
1959 // no access tracking
1960 return;
1961 }
1962
1963 if (empty($courseid)) {
1964 $courseid = SITEID;
1965 }
1966
1967 $timenow = time();
1968
1969/// Store site lastaccess time for the current user
1970 if ($timenow - $USER->lastaccess > LASTACCESS_UPDATE_SECS) {
1971 /// Update $USER->lastaccess for next checks
1972 $USER->lastaccess = $timenow;
341b5ed2 1973
f33e1ed4 1974 $last = new object();
1975 $last->id = $USER->id;
1976 $last->lastip = getremoteaddr();
1977 $last->lastaccess = $timenow;
1978
1979 if (!$DB->update_record_raw('user', $last)) {
1980 debugging('Error: Could not update global user lastaccess information', DEBUG_ALL); // Don't throw an error
341b5ed2 1981 }
1982 }
1983
1984 if ($courseid == SITEID) {
1985 /// no user_lastaccess for frontpage
1986 return;
1987 }
cb8aaedf 1988
341b5ed2 1989/// Store course lastaccess times for the current user
1990 if (empty($USER->currentcourseaccess[$courseid]) or ($timenow - $USER->currentcourseaccess[$courseid] > LASTACCESS_UPDATE_SECS)) {
341b5ed2 1991
f33e1ed4 1992 $lastaccess = $DB->get_field('user_lastaccess', 'timeaccess', array('userid'=>$USER->id, 'courseid'=>$courseid));
341b5ed2 1993
f33e1ed4 1994 if ($lastaccess === false) {
1995 // Update course lastaccess for next checks
1996 $USER->currentcourseaccess[$courseid] = $timenow;
1997
1998 $last = new object();
1999 $last->userid = $USER->id;
2000 $last->courseid = $courseid;
2001 $last->timeaccess = $timenow;
2002 if (!$DB->insert_record_raw('user_lastaccess', $last, false)) {
2003 debugging('Error: Could not insert course user lastaccess information', DEBUG_ALL); // Don't throw an error
edb15b8f 2004 }
f33e1ed4 2005
2006 } else if ($timenow - $lastaccess < LASTACCESS_UPDATE_SECS) {
2007 // no need to update now, it was updated recently in concurrent login ;-)
341b5ed2 2008
f33e1ed4 2009 } else {
2010 // Update course lastaccess for next checks
2011 $USER->currentcourseaccess[$courseid] = $timenow;
2012
2013 if (!$DB->set_field('user_lastaccess', 'timeaccess', $timenow, array('userid'=>$USER->id, 'courseid'=>$courseid))) {
2014 debugging('Error: Could not update course user lastacess information'); // Don't throw an error
114176a2 2015 }
3d94772d 2016 }
8f0cd6ef 2017 }
9fa49e22 2018}
2019
18a97fd8 2020/**
fbc21ae8 2021 * Select all log records based on SQL criteria
2022 *
2023 * @uses $CFG
2024 * @param string $select SQL select criteria
2025 * @param string $order SQL order by clause to sort the records returned
2026 * @param string $limitfrom ?
2027 * @param int $limitnum ?
2028 * @param int $totalcount Passed in by reference.
7290c7fa 2029 * @return object
fbc21ae8 2030 * @todo Finish documenting this function
2031 */
d4419d55 2032function get_logs($select, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount) {
9fa49e22 2033 global $CFG;
2034
519d369f 2035 if ($order) {
d4419d55 2036 $order = 'ORDER BY '. $order;
519d369f 2037 }
2038
fbc21ae8 2039 $selectsql = $CFG->prefix .'log l LEFT JOIN '. $CFG->prefix .'user u ON l.userid = u.id '. ((strlen($select) > 0) ? 'WHERE '. $select : '');
a2ddd957 2040 $countsql = $CFG->prefix.'log l '.((strlen($select) > 0) ? ' WHERE '. $select : '');
2041
2042 $totalcount = count_records_sql("SELECT COUNT(*) FROM $countsql");
519d369f 2043
d4419d55 2044 return get_records_sql('SELECT l.*, u.firstname, u.lastname, u.picture
93a89227 2045 FROM '. $selectsql .' '. $order, $limitfrom, $limitnum) ;
9fa49e22 2046}
2047
519d369f 2048
18a97fd8 2049/**
fbc21ae8 2050 * Select all log records for a given course and user
2051 *
2052 * @uses $CFG
2f87145b 2053 * @uses DAYSECS
fbc21ae8 2054 * @param int $userid The id of the user as found in the 'user' table.
2055 * @param int $courseid The id of the course as found in the 'course' table.
2056 * @param string $coursestart ?
2057 * @todo Finish documenting this function
2058 */
9fa49e22 2059function get_logs_usercourse($userid, $courseid, $coursestart) {
2060 global $CFG;
2061
da0c90c3 2062 if ($courseid) {
d4419d55 2063 $courseselect = ' AND course = \''. $courseid .'\' ';
2700d113 2064 } else {
2065 $courseselect = '';
da0c90c3 2066 }
2067
1604a0fc 2068 return get_records_sql("SELECT floor((time - $coursestart)/". DAYSECS .") as day, count(*) as num
8f0cd6ef 2069 FROM {$CFG->prefix}log
2070 WHERE userid = '$userid'
1604a0fc 2071 AND time > '$coursestart' $courseselect
1d854cec 2072 GROUP BY floor((time - $coursestart)/". DAYSECS .") ");
9fa49e22 2073}
2074
18a97fd8 2075/**
fbc21ae8 2076 * Select all log records for a given course, user, and day
2077 *
2078 * @uses $CFG
2f87145b 2079 * @uses HOURSECS
fbc21ae8 2080 * @param int $userid The id of the user as found in the 'user' table.
2081 * @param int $courseid The id of the course as found in the 'course' table.
2082 * @param string $daystart ?
7290c7fa 2083 * @return object
fbc21ae8 2084 * @todo Finish documenting this function
2085 */
9fa49e22 2086function get_logs_userday($userid, $courseid, $daystart) {
2087 global $CFG;
2088
7e4a6488 2089 if ($courseid) {
d4419d55 2090 $courseselect = ' AND course = \''. $courseid .'\' ';
2700d113 2091 } else {
2092 $courseselect = '';
7e4a6488 2093 }
2094
1604a0fc 2095 return get_records_sql("SELECT floor((time - $daystart)/". HOURSECS .") as hour, count(*) as num
9fa49e22 2096 FROM {$CFG->prefix}log
8f0cd6ef 2097 WHERE userid = '$userid'
1604a0fc 2098 AND time > '$daystart' $courseselect
1d854cec 2099 GROUP BY floor((time - $daystart)/". HOURSECS .") ");
9fa49e22 2100}
2101
b4bac9b6 2102/**
2103 * Returns an object with counts of failed login attempts
2104 *
8f0cd6ef 2105 * Returns information about failed login attempts. If the current user is
2106 * an admin, then two numbers are returned: the number of attempts and the
b4bac9b6 2107 * number of accounts. For non-admins, only the attempts on the given user
2108 * are shown.
2109 *
fbc21ae8 2110 * @param string $mode Either 'admin', 'teacher' or 'everybody'
2111 * @param string $username The username we are searching for
2112 * @param string $lastlogin The date from which we are searching
2113 * @return int
b4bac9b6 2114 */
b4bac9b6 2115function count_login_failures($mode, $username, $lastlogin) {
2116
d4419d55 2117 $select = 'module=\'login\' AND action=\'error\' AND time > '. $lastlogin;
b4bac9b6 2118
12d06877 2119 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { // Return information about all accounts
b4bac9b6 2120 if ($count->attempts = count_records_select('log', $select)) {
2121 $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)');
2122 return $count;
2123 }
9407d456 2124 } else if ($mode == 'everybody' or ($mode == 'teacher' and isteacherinanycourse())) {
d4419d55 2125 if ($count->attempts = count_records_select('log', $select .' AND info = \''. $username .'\'')) {
b4bac9b6 2126 return $count;
2127 }
2128 }
2129 return NULL;
2130}
2131
2132
a3fb1c45 2133/// GENERAL HELPFUL THINGS ///////////////////////////////////
2134
18a97fd8 2135/**
fbc21ae8 2136 * Dump a given object's information in a PRE block.
2137 *
2138 * Mostly just used for debugging.
2139 *
2140 * @param mixed $object The data to be printed
fbc21ae8 2141 */
a3fb1c45 2142function print_object($object) {
1aa7b31d 2143 echo '<pre class="notifytiny">' . htmlspecialchars(print_r($object,true)) . '</pre>';
a3fb1c45 2144}
2145
3511647c 2146/*
2147 * Check whether a course is visible through its parents
bfbfdb53 2148 * path.
3511647c 2149 *
2150 * Notes:
2151 *
2152 * - All we need from the course is ->category. _However_
2153 * if the course object has a categorypath property,
2154 * we'll save a dbquery
2155 *
2156 * - If we return false, you'll still need to check if
2157 * the user can has the 'moodle/category:visibility'
2158 * capability...
2159 *
bfbfdb53 2160 * - Will generate 2 DB calls.
3511647c 2161 *
2162 * - It does have a small local cache, however...
2163 *
2164 * - Do NOT call this over many courses as it'll generate
2165 * DB traffic. Instead, see what get_my_courses() does.
2166 *
2167 * @param mixed $object A course object
2168 * @return bool
2169 */
0986271b 2170function course_parent_visible($course = null) {
fa145ae1 2171 global $CFG;
3511647c 2172 //return true;
2173 static $mycache;
fa145ae1 2174
3511647c 2175 if (!is_object($course)) {
418b4e5a 2176 return true;
2177 }
2178 if (!empty($CFG->allowvisiblecoursesinhiddencategories)) {
2179 return true;
2180 }
0986271b 2181
3511647c 2182 if (!isset($mycache)) {
2183 $mycache = array();
2184 } else {
2185 // cast to force assoc array
bfbfdb53 2186 $k = (string)$course->category;
3511647c 2187 if (isset($mycache[$k])) {
2188 return $mycache[$k];
2189 }
0986271b 2190 }
5930cded 2191
3511647c 2192 if (isset($course->categorypath)) {
2193 $path = $course->categorypath;
2194 } else {
bfbfdb53 2195 $path = get_field('course_categories', 'path',
3511647c 2196 'id', $course->category);
824f1c40 2197 }
3511647c 2198 $catids = substr($path,1); // strip leading slash
2199 $catids = str_replace('/',',',$catids);
824f1c40 2200
3511647c 2201 $sql = "SELECT MIN(visible)
2202 FROM {$CFG->prefix}course_categories
2203 WHERE id IN ($catids)";
2204 $vis = get_field_sql($sql);
5930cded 2205
3511647c 2206 // cast to force assoc array
2207 $k = (string)$course->category;
2208 $mycache[$k] = $vis;
2209
2210 return $vis;
0986271b 2211}
2212
62d4e774 2213/**
5930cded 2214 * This function is the official hook inside XMLDB stuff to delegate its debug to one
62d4e774 2215 * external function.
2216 *
2217 * Any script can avoid calls to this function by defining XMLDB_SKIP_DEBUG_HOOK before
2218 * using XMLDB classes. Obviously, also, if this function doesn't exist, it isn't invoked ;-)
2219 *
2220 * @param $message string contains the error message
2221 * @param $object object XMLDB object that fired the debug
2222 */
2223function xmldb_debug($message, $object) {
2224
92b564f4 2225 debugging($message, DEBUG_DEVELOPER);
62d4e774 2226}
2227
49860445 2228/**
2229 * true or false function to see if user can create any courses at all
2230 * @return bool
2231 */
2232function user_can_create_courses() {
2233 global $USER;
2234 // if user has course creation capability at any site or course cat, then return true;
5930cded 2235
12d06877 2236 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
5930cded 2237 return true;
49860445 2238 } else {
5930cded 2239 return (bool) count(get_creatable_categories());
49860445 2240 }
5930cded 2241
49860445 2242}
2243
2244/**
2245 * get the list of categories the current user can create courses in
2246 * @return array
2247 */
2248function get_creatable_categories() {
5930cded 2249
49860445 2250 $creatablecats = array();
2251 if ($cats = get_records('course_categories')) {
2252 foreach ($cats as $cat) {
2253 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
2254 $creatablecats[$cat->id] = $cat->name;
2255 }
2256 }
2257 }
2258 return $creatablecats;
2259}
2260
9d5b689c 2261// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
03517306 2262?>