6078ba30 |
1 | <?php // $Id$ |
7cf1c7bd |
2 | |
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 |
7cf1c7bd |
10 | * @version $Id$ |
89dcb99d |
11 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
7cf1c7bd |
12 | * @package moodlecore |
13 | */ |
14 | |
df28d6c5 |
15 | |
5930cded |
16 | /** |
11a052a6 |
17 | * Escape all dangerous characters in a data record |
18 | * |
19 | * $dataobject is an object containing needed data |
20 | * Run over each field exectuting addslashes() function |
21 | * to escape SQL unfriendly characters (e.g. quotes) |
22 | * Handy when writing back data read from the database |
23 | * |
24 | * @param $dataobject Object containing the database record |
25 | * @return object Same object with neccessary characters escaped |
26 | */ |
27 | function addslashes_object( $dataobject ) { |
28 | $a = get_object_vars( $dataobject); |
29 | foreach ($a as $key=>$value) { |
30 | $a[$key] = addslashes( $value ); |
31 | } |
32 | return (object)$a; |
33 | } |
0892f7bd |
34 | |
df28d6c5 |
35 | /// USER DATABASE //////////////////////////////////////////////// |
36 | |
18a97fd8 |
37 | /** |
fbc21ae8 |
38 | * Returns $user object of the main admin user |
20aeb4b8 |
39 | * primary admin = admin with lowest role_assignment id among admins |
fbc21ae8 |
40 | * @uses $CFG |
41 | * @return object(admin) An associative array representing the admin user. |
fbc21ae8 |
42 | */ |
df28d6c5 |
43 | function get_admin () { |
df28d6c5 |
44 | |
45 | global $CFG; |
46 | |
47 | if ( $admins = get_admins() ) { |
48 | foreach ($admins as $admin) { |
8f0cd6ef |
49 | return $admin; // ie the first one |
df28d6c5 |
50 | } |
51 | } else { |
52 | return false; |
53 | } |
54 | } |
55 | |
18a97fd8 |
56 | /** |
fbc21ae8 |
57 | * Returns list of all admins |
58 | * |
59 | * @uses $CFG |
7290c7fa |
60 | * @return object |
fbc21ae8 |
61 | */ |
df28d6c5 |
62 | function get_admins() { |
df28d6c5 |
63 | |
64 | global $CFG; |
5930cded |
65 | |
20aeb4b8 |
66 | $context = get_context_instance(CONTEXT_SYSTEM, SITEID); |
df28d6c5 |
67 | |
41f6ed56 |
68 | return get_users_by_capability($context, 'moodle/site:doanything', 'u.*, ra.id as adminid', 'ra.id ASC'); // only need first one |
5930cded |
69 | |
df28d6c5 |
70 | } |
71 | |
72 | |
b61efafb |
73 | function get_courses_in_metacourse($metacourseid) { |
74 | global $CFG; |
75 | |
5f37b628 |
76 | $sql = "SELECT c.id,c.shortname,c.fullname FROM {$CFG->prefix}course c, {$CFG->prefix}course_meta mc WHERE mc.parent_course = $metacourseid |
5afa0de6 |
77 | AND mc.child_course = c.id ORDER BY c.shortname"; |
b61efafb |
78 | |
79 | return get_records_sql($sql); |
80 | } |
81 | |
82 | function get_courses_notin_metacourse($metacourseid,$count=false) { |
83 | |
84 | global $CFG; |
85 | |
b61efafb |
86 | if ($count) { |
87 | $sql = "SELECT COUNT(c.id)"; |
c44d5d42 |
88 | } else { |
b61efafb |
89 | $sql = "SELECT c.id,c.shortname,c.fullname"; |
90 | } |
178ccd11 |
91 | |
ffed6bf3 |
92 | $alreadycourses = get_courses_in_metacourse($metacourseid); |
5930cded |
93 | |
c44d5d42 |
94 | $sql .= " FROM {$CFG->prefix}course c WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).") |
5afa0de6 |
95 | AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1 ".((empty($count)) ? " ORDER BY c.shortname" : ""); |
5930cded |
96 | |
b61efafb |
97 | return get_records_sql($sql); |
98 | } |
99 | |
493cde24 |
100 | function count_courses_notin_metacourse($metacourseid) { |
101 | global $CFG; |
102 | |
103 | $alreadycourses = get_courses_in_metacourse($metacourseid); |
104 | |
69cd298a |
105 | $sql = "SELECT COUNT(c.id) AS notin FROM {$CFG->prefix}course c |
493cde24 |
106 | WHERE ".((!empty($alreadycourses)) ? "c.id NOT IN (".implode(',',array_keys($alreadycourses)).") |
107 | AND " : "")." c.id !=$metacourseid and c.id != ".SITEID." and c.metacourse != 1"; |
108 | |
69cd298a |
109 | if (!$count = get_record_sql($sql)) { |
493cde24 |
110 | return 0; |
111 | } |
112 | |
69cd298a |
113 | return $count->notin; |
493cde24 |
114 | } |
115 | |
900df8b6 |
116 | /** |
fbc21ae8 |
117 | * Search through course users |
118 | * |
5930cded |
119 | * If $coursid specifies the site course then this function searches |
fbc21ae8 |
120 | * through all undeleted and confirmed users |
121 | * |
122 | * @uses $CFG |
123 | * @uses SITEID |
124 | * @param int $courseid The course in question. |
125 | * @param int $groupid The group in question. |
126 | * @param string $searchtext ? |
127 | * @param string $sort ? |
5930cded |
128 | * @param string $exceptions ? |
7290c7fa |
129 | * @return object |
fbc21ae8 |
130 | */ |
900df8b6 |
131 | function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions='') { |
132 | global $CFG; |
0720313b |
133 | |
29daf3a0 |
134 | $LIKE = sql_ilike(); |
135 | $fullname = sql_fullname('u.firstname', 'u.lastname'); |
8f0cd6ef |
136 | |
900df8b6 |
137 | if (!empty($exceptions)) { |
d4419d55 |
138 | $except = ' AND u.id NOT IN ('. $exceptions .') '; |
900df8b6 |
139 | } else { |
140 | $except = ''; |
141 | } |
2700d113 |
142 | |
900df8b6 |
143 | if (!empty($sort)) { |
d4419d55 |
144 | $order = ' ORDER BY '. $sort; |
900df8b6 |
145 | } else { |
146 | $order = ''; |
147 | } |
8f0cd6ef |
148 | |
d4419d55 |
149 | $select = 'u.deleted = \'0\' AND u.confirmed = \'1\''; |
2700d113 |
150 | |
222ac91b |
151 | if (!$courseid or $courseid == SITEID) { |
2700d113 |
152 | return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email |
153 | FROM {$CFG->prefix}user u |
154 | WHERE $select |
900df8b6 |
155 | AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%') |
2700d113 |
156 | $except $order"); |
8f0cd6ef |
157 | } else { |
2700d113 |
158 | |
900df8b6 |
159 | if ($groupid) { |
f3f7610c |
160 | //TODO:check. Remove group DB dependencies. |
900df8b6 |
161 | return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email |
8f0cd6ef |
162 | FROM {$CFG->prefix}user u, |
1d684195 |
163 | {$CFG->prefix}groups_members gm |
164 | WHERE $select AND gm.groupid = '$groupid' AND gm.userid = u.id |
900df8b6 |
165 | AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%') |
166 | $except $order"); |
167 | } else { |
ea8158c1 |
168 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
169 | $contextlists = get_related_contexts_string($context); |
170 | $users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email |
8f0cd6ef |
171 | FROM {$CFG->prefix}user u, |
ea8158c1 |
172 | {$CFG->prefix}role_assignments ra |
173 | WHERE $select AND ra.contextid $contextlists AND ra.userid = u.id |
900df8b6 |
174 | AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%') |
ea8158c1 |
175 | $except $order"); |
900df8b6 |
176 | } |
ea8158c1 |
177 | return $users; |
900df8b6 |
178 | } |
df28d6c5 |
179 | } |
180 | |
2700d113 |
181 | |
18a97fd8 |
182 | /** |
fbc21ae8 |
183 | * Returns a list of all site users |
184 | * Obsolete, just calls get_course_users(SITEID) |
185 | * |
186 | * @uses SITEID |
c6d15803 |
187 | * @deprecated Use {@link get_course_users()} instead. |
fbc21ae8 |
188 | * @param string $fields A comma separated list of fields to be returned from the chosen table. |
7290c7fa |
189 | * @return object|false {@link $USER} records or false if error. |
fbc21ae8 |
190 | */ |
d4419d55 |
191 | function get_site_users($sort='u.lastaccess DESC', $fields='*', $exceptions='') { |
2d0b30a0 |
192 | |
65ee9c16 |
193 | return get_course_users(SITEID, $sort, $exceptions, $fields); |
2d0b30a0 |
194 | } |
195 | |
9fa49e22 |
196 | |
18a97fd8 |
197 | /** |
fbc21ae8 |
198 | * Returns a subset of users |
199 | * |
200 | * @uses $CFG |
7290c7fa |
201 | * @param bool $get If false then only a count of the records is returned |
fbc21ae8 |
202 | * @param string $search A simple string to search for |
7290c7fa |
203 | * @param bool $confirmed A switch to allow/disallow unconfirmed users |
fbc21ae8 |
204 | * @param array(int) $exceptions A list of IDs to ignore, eg 2,4,5,8,9,10 |
205 | * @param string $sort A SQL snippet for the sorting criteria to use |
206 | * @param string $firstinitial ? |
207 | * @param string $lastinitial ? |
208 | * @param string $page ? |
209 | * @param string $recordsperpage ? |
210 | * @param string $fields A comma separated list of fields to be returned from the chosen table. |
7290c7fa |
211 | * @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 |
212 | */ |
d4419d55 |
213 | function get_users($get=true, $search='', $confirmed=false, $exceptions='', $sort='firstname ASC', |
36075e09 |
214 | $firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*') { |
18a97fd8 |
215 | |
216 | global $CFG; |
5930cded |
217 | |
36075e09 |
218 | if ($get && !$recordsperpage) { |
219 | debugging('Call to get_users with $get = true no $recordsperpage limit. ' . |
220 | 'On large installations, this will probably cause an out of memory error. ' . |
221 | 'Please think again and change your code so that it does not try to ' . |
03517306 |
222 | 'load so much data into memory.', DEBUG_DEVELOPER); |
36075e09 |
223 | } |
18a97fd8 |
224 | |
29daf3a0 |
225 | $LIKE = sql_ilike(); |
226 | $fullname = sql_fullname(); |
e384fb7b |
227 | |
e8e0bb2d |
228 | $select = 'username <> \'guest\' AND deleted = 0'; |
488acd1b |
229 | |
0044147e |
230 | if (!empty($search)){ |
231 | $search = trim($search); |
488acd1b |
232 | $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') "; |
e384fb7b |
233 | } |
234 | |
5a741655 |
235 | if ($confirmed) { |
d4419d55 |
236 | $select .= ' AND confirmed = \'1\' '; |
5a741655 |
237 | } |
238 | |
239 | if ($exceptions) { |
d4419d55 |
240 | $select .= ' AND id NOT IN ('. $exceptions .') '; |
5a741655 |
241 | } |
242 | |
488acd1b |
243 | if ($firstinitial) { |
d4419d55 |
244 | $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\''; |
8f0cd6ef |
245 | } |
488acd1b |
246 | if ($lastinitial) { |
d4419d55 |
247 | $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\''; |
8f0cd6ef |
248 | } |
488acd1b |
249 | |
5a741655 |
250 | if ($get) { |
36075e09 |
251 | return get_records_select('user', $select, $sort, $fields, $page, $recordsperpage); |
5a741655 |
252 | } else { |
36075e09 |
253 | return count_records_select('user', $select); |
5a741655 |
254 | } |
9fa49e22 |
255 | } |
256 | |
5a741655 |
257 | |
18a97fd8 |
258 | /** |
fbc21ae8 |
259 | * shortdesc (optional) |
260 | * |
261 | * longdesc |
262 | * |
263 | * @uses $CFG |
264 | * @param string $sort ? |
265 | * @param string $dir ? |
266 | * @param int $categoryid ? |
267 | * @param int $categoryid ? |
268 | * @param string $search ? |
269 | * @param string $firstinitial ? |
270 | * @param string $lastinitial ? |
7290c7fa |
271 | * @returnobject {@link $USER} records |
fbc21ae8 |
272 | * @todo Finish documenting this function |
273 | */ |
274 | |
36075e09 |
275 | function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0, |
03d820c7 |
276 | $search='', $firstinitial='', $lastinitial='', $remotewhere='') { |
488acd1b |
277 | |
9fa49e22 |
278 | global $CFG; |
31fefa63 |
279 | |
29daf3a0 |
280 | $LIKE = sql_ilike(); |
281 | $fullname = sql_fullname(); |
c2a96d6b |
282 | |
e8e0bb2d |
283 | $select = "deleted <> '1'"; |
488acd1b |
284 | |
0044147e |
285 | if (!empty($search)) { |
286 | $search = trim($search); |
39dc779a |
287 | $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%' OR username='$search') "; |
488acd1b |
288 | } |
289 | |
290 | if ($firstinitial) { |
d4419d55 |
291 | $select .= ' AND firstname '. $LIKE .' \''. $firstinitial .'%\' '; |
488acd1b |
292 | } |
293 | |
294 | if ($lastinitial) { |
d4419d55 |
295 | $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\' '; |
c750592a |
296 | } |
297 | |
03d820c7 |
298 | $select .= $remotewhere; |
299 | |
488acd1b |
300 | if ($sort) { |
d4419d55 |
301 | $sort = ' ORDER BY '. $sort .' '. $dir; |
488acd1b |
302 | } |
303 | |
304 | /// warning: will return UNCONFIRMED USERS |
03d820c7 |
305 | return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid |
8f0cd6ef |
306 | FROM {$CFG->prefix}user |
422770d8 |
307 | WHERE $select $sort", $page, $recordsperpage); |
9fa49e22 |
308 | |
309 | } |
310 | |
488acd1b |
311 | |
18a97fd8 |
312 | /** |
7290c7fa |
313 | * Full list of users that have confirmed their accounts. |
fbc21ae8 |
314 | * |
315 | * @uses $CFG |
7290c7fa |
316 | * @return object |
fbc21ae8 |
317 | */ |
9fa49e22 |
318 | function get_users_confirmed() { |
319 | global $CFG; |
8f0cd6ef |
320 | return get_records_sql("SELECT * |
321 | FROM {$CFG->prefix}user |
322 | WHERE confirmed = 1 |
9fa49e22 |
323 | AND deleted = 0 |
e8e0bb2d |
324 | AND username <> 'guest'"); |
9fa49e22 |
325 | } |
326 | |
327 | |
18a97fd8 |
328 | /** |
7290c7fa |
329 | * Full list of users that have not yet confirmed their accounts. |
fbc21ae8 |
330 | * |
331 | * @uses $CFG |
332 | * @param string $cutofftime ? |
7290c7fa |
333 | * @return object {@link $USER} records |
fbc21ae8 |
334 | */ |
99988d1a |
335 | function get_users_unconfirmed($cutofftime=2000000000) { |
9fa49e22 |
336 | global $CFG; |
8f0cd6ef |
337 | return get_records_sql("SELECT * |
338 | FROM {$CFG->prefix}user |
9fa49e22 |
339 | WHERE confirmed = 0 |
8f0cd6ef |
340 | AND firstaccess > 0 |
cf36da64 |
341 | AND firstaccess < $cutofftime"); |
9fa49e22 |
342 | } |
343 | |
613bbd7c |
344 | /** |
345 | * All users that we have not seen for a really long time (ie dead accounts) |
346 | * |
347 | * @uses $CFG |
348 | * @param string $cutofftime ? |
349 | * @return object {@link $USER} records |
613bbd7c |
350 | */ |
351 | function get_users_longtimenosee($cutofftime) { |
352 | global $CFG; |
cc7c0592 |
353 | return get_records_sql("SELECT userid as id, courseid |
354 | FROM {$CFG->prefix}user_lastaccess |
cf36da64 |
355 | WHERE courseid != ".SITEID." |
356 | AND timeaccess > 0 |
357 | AND timeaccess < $cutofftime "); |
613bbd7c |
358 | } |
9fa49e22 |
359 | |
fa22fd5f |
360 | /** |
361 | * Full list of bogus accounts that are probably not ever going to be used |
362 | * |
363 | * @uses $CFG |
364 | * @param string $cutofftime ? |
365 | * @return object {@link $USER} records |
fa22fd5f |
366 | */ |
367 | |
368 | function get_users_not_fully_set_up($cutofftime=2000000000) { |
369 | global $CFG; |
370 | return get_records_sql("SELECT * |
371 | FROM {$CFG->prefix}user |
372 | WHERE confirmed = 1 |
373 | AND lastaccess > 0 |
cf36da64 |
374 | AND lastaccess < $cutofftime |
fa22fd5f |
375 | AND deleted = 0 |
376 | AND (lastname = '' OR firstname = '' OR email = '')"); |
377 | } |
378 | |
02ebf404 |
379 | /// OTHER SITE AND COURSE FUNCTIONS ///////////////////////////////////////////// |
380 | |
381 | |
18a97fd8 |
382 | /** |
fbc21ae8 |
383 | * Returns $course object of the top-level site. |
384 | * |
89dcb99d |
385 | * @return course A {@link $COURSE} object for the site |
fbc21ae8 |
386 | */ |
c44d5d42 |
387 | function get_site() { |
388 | |
389 | global $SITE; |
390 | |
391 | if (!empty($SITE->id)) { // We already have a global to use, so return that |
392 | return $SITE; |
393 | } |
02ebf404 |
394 | |
c44d5d42 |
395 | if ($course = get_record('course', 'category', 0)) { |
02ebf404 |
396 | return $course; |
397 | } else { |
398 | return false; |
399 | } |
400 | } |
401 | |
18a97fd8 |
402 | /** |
613bbd7c |
403 | * Returns list of courses, for whole site, or category |
404 | * |
405 | * Returns list of courses, for whole site, or category |
406 | * Important: Using c.* for fields is extremely expensive because |
407 | * we are using distinct. You almost _NEVER_ need all the fields |
408 | * in such a large SELECT |
409 | * |
410 | * @param type description |
411 | * |
613bbd7c |
412 | */ |
6315b1c8 |
413 | function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*") { |
02ebf404 |
414 | |
8ef9cb56 |
415 | global $USER, $CFG; |
5930cded |
416 | |
6315b1c8 |
417 | if ($categoryid != "all" && is_numeric($categoryid)) { |
71dea306 |
418 | $categoryselect = "WHERE c.category = '$categoryid'"; |
419 | } else { |
5930cded |
420 | $categoryselect = ""; |
09575480 |
421 | } |
422 | |
423 | if (empty($sort)) { |
424 | $sortstatement = ""; |
425 | } else { |
426 | $sortstatement = "ORDER BY $sort"; |
427 | } |
428 | |
429 | $visiblecourses = array(); |
5930cded |
430 | |
71dea306 |
431 | // pull out all course matching the cat |
5930cded |
432 | if ($courses = get_records_sql("SELECT $fields |
433 | FROM {$CFG->prefix}course c |
71dea306 |
434 | $categoryselect |
09575480 |
435 | $sortstatement")) { |
436 | |
437 | // loop throught them |
438 | foreach ($courses as $course) { |
439 | |
285f94f5 |
440 | if (isset($course->visible) && $course->visible <= 0) { |
09575480 |
441 | // for hidden courses, require visibility check |
285f94f5 |
442 | if (has_capability('moodle/course:viewhiddencourses', |
443 | get_context_instance(CONTEXT_COURSE, $course->id))) { |
5930cded |
444 | $visiblecourses [] = $course; |
09575480 |
445 | } |
446 | } else { |
5930cded |
447 | $visiblecourses [] = $course; |
448 | } |
09575480 |
449 | } |
6315b1c8 |
450 | } |
71dea306 |
451 | return $visiblecourses; |
6315b1c8 |
452 | |
71dea306 |
453 | /* |
6315b1c8 |
454 | $teachertable = ""; |
455 | $visiblecourses = ""; |
456 | $sqland = ""; |
457 | if (!empty($categoryselect)) { |
458 | $sqland = "AND "; |
459 | } |
460 | if (!empty($USER->id)) { // May need to check they are a teacher |
ae9e4c06 |
461 | if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { |
6315b1c8 |
462 | $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')"; |
463 | $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course = c.id"; |
464 | } |
465 | } else { |
466 | $visiblecourses = "$sqland c.visible > 0"; |
8ef9cb56 |
467 | } |
468 | |
6315b1c8 |
469 | if ($categoryselect or $visiblecourses) { |
470 | $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses"; |
14f32609 |
471 | } else { |
6315b1c8 |
472 | $selectsql = "{$CFG->prefix}course c $teachertable"; |
14f32609 |
473 | } |
474 | |
5b66416f |
475 | $extrafield = str_replace('ASC','',$sort); |
476 | $extrafield = str_replace('DESC','',$extrafield); |
477 | $extrafield = trim($extrafield); |
478 | if (!empty($extrafield)) { |
479 | $extrafield = ','.$extrafield; |
480 | } |
481 | return get_records_sql("SELECT ".((!empty($teachertable)) ? " DISTINCT " : "")." $fields $extrafield FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : "")); |
71dea306 |
482 | */ |
8130b77b |
483 | } |
484 | |
8130b77b |
485 | |
6315b1c8 |
486 | /** |
613bbd7c |
487 | * Returns list of courses, for whole site, or category |
488 | * |
489 | * Similar to get_courses, but allows paging |
5930cded |
490 | * Important: Using c.* for fields is extremely expensive because |
613bbd7c |
491 | * we are using distinct. You almost _NEVER_ need all the fields |
492 | * in such a large SELECT |
493 | * |
494 | * @param type description |
495 | * |
613bbd7c |
496 | */ |
6315b1c8 |
497 | function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c.*", |
498 | &$totalcount, $limitfrom="", $limitnum="") { |
c7fe5c6f |
499 | |
8130b77b |
500 | global $USER, $CFG; |
5930cded |
501 | |
71dea306 |
502 | $categoryselect = ""; |
503 | if ($categoryid != "all" && is_numeric($categoryid)) { |
504 | $categoryselect = "WHERE c.category = '$categoryid'"; |
505 | } else { |
5930cded |
506 | $categoryselect = ""; |
507 | } |
508 | |
71dea306 |
509 | // pull out all course matching the cat |
12490fc2 |
510 | $visiblecourses = array(); |
5930cded |
511 | if (!($courses = get_records_sql("SELECT $fields |
512 | FROM {$CFG->prefix}course c |
71dea306 |
513 | $categoryselect |
12490fc2 |
514 | ORDER BY $sort"))) { |
515 | return $visiblecourses; |
516 | } |
71dea306 |
517 | $totalcount = 0; |
5930cded |
518 | |
71dea306 |
519 | if (!$limitnum) { |
5930cded |
520 | $limitnum = count($courses); |
71dea306 |
521 | } |
5930cded |
522 | |
285f94f5 |
523 | if (!$limitfrom) { |
5930cded |
524 | $limitfrom = 0; |
71dea306 |
525 | } |
5930cded |
526 | |
71dea306 |
527 | // iteration will have to be done inside loop to keep track of the limitfrom and limitnum |
528 | foreach ($courses as $course) { |
529 | if ($course->visible <= 0) { |
530 | // for hidden courses, require visibility check |
531 | if (has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { |
532 | $totalcount++; |
533 | if ($totalcount > $limitfrom && count($visiblecourses) < $limitnum) { |
534 | $visiblecourses [] = $course; |
535 | } |
536 | } |
537 | } else { |
538 | $totalcount++; |
539 | if ($totalcount > $limitfrom && count($visiblecourses) < $limitnum) { |
540 | $visiblecourses [] = $course; |
541 | } |
5930cded |
542 | } |
71dea306 |
543 | } |
5930cded |
544 | |
71dea306 |
545 | return $visiblecourses; |
546 | |
547 | /** |
8130b77b |
548 | |
6315b1c8 |
549 | $categoryselect = ""; |
b565bbdf |
550 | if ($categoryid != "all" && is_numeric($categoryid)) { |
6315b1c8 |
551 | $categoryselect = "c.category = '$categoryid'"; |
8130b77b |
552 | } |
553 | |
6315b1c8 |
554 | $teachertable = ""; |
555 | $visiblecourses = ""; |
556 | $sqland = ""; |
557 | if (!empty($categoryselect)) { |
558 | $sqland = "AND "; |
c7fe5c6f |
559 | } |
2d2da684 |
560 | if (!empty($USER) and !empty($USER->id)) { // May need to check they are a teacher |
ae9e4c06 |
561 | if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { |
6315b1c8 |
562 | $visiblecourses = "$sqland ((c.visible > 0) OR t.userid = '$USER->id')"; |
563 | $teachertable = "LEFT JOIN {$CFG->prefix}user_teachers t ON t.course=c.id"; |
564 | } |
8130b77b |
565 | } else { |
6315b1c8 |
566 | $visiblecourses = "$sqland c.visible > 0"; |
8130b77b |
567 | } |
568 | |
6315b1c8 |
569 | if ($limitfrom !== "") { |
29daf3a0 |
570 | $limit = sql_paging_limit($limitfrom, $limitnum); |
6315b1c8 |
571 | } else { |
572 | $limit = ""; |
02ebf404 |
573 | } |
8ef9cb56 |
574 | |
6315b1c8 |
575 | $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $visiblecourses"; |
8ef9cb56 |
576 | |
6315b1c8 |
577 | $totalcount = count_records_sql("SELECT COUNT(DISTINCT c.id) FROM $selectsql"); |
8ef9cb56 |
578 | |
2338ad32 |
579 | return get_records_sql("SELECT $fields FROM $selectsql ".((!empty($sort)) ? "ORDER BY $sort" : "")." $limit"); |
71dea306 |
580 | */ |
02ebf404 |
581 | } |
582 | |
583 | |
18a97fd8 |
584 | /** |
f8e1c7af |
585 | * List of courses that a user has access to view. Note that for admins, |
586 | * this usually includes every course on the system. |
fbc21ae8 |
587 | * |
573674bf |
588 | * Notes (inherited from get_user_courses_bycap()): |
e1d5e5c1 |
589 | * |
590 | * - $fields is an array of fieldnames to ADD |
591 | * so name the fields you really need, which will |
592 | * be added and uniq'd |
593 | * |
594 | * - the course records have $c->context which is a fully |
595 | * valid context object. Saves you a query per course! |
596 | * |
597 | * @uses $USER |
7290c7fa |
598 | * @param int $userid The user of interest |
33f85740 |
599 | * @param string $sort the sortorder in the course table |
e1d5e5c1 |
600 | * @param array $fields - names of _additional_ fields to return (also accepts a string) |
f8e1c7af |
601 | * @param bool $doanything True if using the doanything flag |
602 | * @param int $limit Maximum number of records to return, or 0 for unlimited |
33f85740 |
603 | * @return array {@link $COURSE} of course objects |
fbc21ae8 |
604 | */ |
e1d5e5c1 |
605 | function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NULL, $doanything=false,$limit=0) { |
bdf3bbd1 |
606 | |
e1d5e5c1 |
607 | global $USER; |
5930cded |
608 | |
4dbca99e |
609 | // Guest's do not have any courses |
e1d5e5c1 |
610 | $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); |
61f774e8 |
611 | if (has_capability('moodle/legacy:guest',$sitecontext,$userid,false)) { |
4dbca99e |
612 | return(array()); |
613 | } |
601edb90 |
614 | |
e1d5e5c1 |
615 | if (!is_null($fields) && is_string($fields)) { |
616 | if (empty($fields)) { |
617 | $fields = NULL; |
618 | } else { |
619 | // turn the fields from a string to an array that |
573674bf |
620 | // get_user_courses_bycap() will like... |
e1d5e5c1 |
621 | $fields = explode(',',$fields); |
622 | $fields = array_map('trim', $fields); |
2f3499b7 |
623 | } |
624 | } |
152a9060 |
625 | |
e1d5e5c1 |
626 | if ($userid === $USER->id && isset($USER->access)) { |
aeb3916b |
627 | $accessinfo = $USER->access; |
bdf3bbd1 |
628 | } else { |
e1d5e5c1 |
629 | $accessinfo = get_user_access_sitewide($userid); |
aeb3916b |
630 | } |
573674bf |
631 | $courses = get_user_courses_bycap($userid, 'moodle/course:view', $accessinfo, |
632 | $doanything, $sort, $fields, |
633 | $limit); |
aeb3916b |
634 | // strangely, get_my_courses() is expected to return the |
635 | // array keyed on id, which messes up the sorting |
636 | $kcourses = array(); |
637 | $cc = count($courses); |
638 | for ($n=0; $n<$cc; $n++) { |
639 | $kcourses[$courses[$n]->id] = $courses[$n]; |
640 | } |
641 | return $kcourses; |
02ebf404 |
642 | } |
643 | |
18a97fd8 |
644 | /** |
7290c7fa |
645 | * A list of courses that match a search |
fbc21ae8 |
646 | * |
647 | * @uses $CFG |
648 | * @param array $searchterms ? |
649 | * @param string $sort ? |
650 | * @param int $page ? |
651 | * @param int $recordsperpage ? |
652 | * @param int $totalcount Passed in by reference. ? |
7290c7fa |
653 | * @return object {@link $COURSE} records |
fbc21ae8 |
654 | */ |
d4419d55 |
655 | function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $recordsperpage=50, &$totalcount) { |
02ebf404 |
656 | |
657 | global $CFG; |
658 | |
18a97fd8 |
659 | //to allow case-insensitive search for postgesql |
48505662 |
660 | if ($CFG->dbfamily == 'postgres') { |
d4419d55 |
661 | $LIKE = 'ILIKE'; |
662 | $NOTLIKE = 'NOT ILIKE'; // case-insensitive |
663 | $REGEXP = '~*'; |
664 | $NOTREGEXP = '!~*'; |
02ebf404 |
665 | } else { |
d4419d55 |
666 | $LIKE = 'LIKE'; |
667 | $NOTLIKE = 'NOT LIKE'; |
668 | $REGEXP = 'REGEXP'; |
669 | $NOTREGEXP = 'NOT REGEXP'; |
02ebf404 |
670 | } |
671 | |
d4419d55 |
672 | $fullnamesearch = ''; |
673 | $summarysearch = ''; |
02ebf404 |
674 | |
02ebf404 |
675 | foreach ($searchterms as $searchterm) { |
6bb0f67f |
676 | |
677 | /// Under Oracle and MSSQL, trim the + and - operators and perform |
678 | /// simpler LIKE search |
48505662 |
679 | if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') { |
6bb0f67f |
680 | $searchterm = trim($searchterm, '+-'); |
681 | } |
682 | |
02ebf404 |
683 | if ($fullnamesearch) { |
d4419d55 |
684 | $fullnamesearch .= ' AND '; |
02ebf404 |
685 | } |
02ebf404 |
686 | if ($summarysearch) { |
d4419d55 |
687 | $summarysearch .= ' AND '; |
02ebf404 |
688 | } |
a8b56716 |
689 | |
d4419d55 |
690 | if (substr($searchterm,0,1) == '+') { |
a8b56716 |
691 | $searchterm = substr($searchterm,1); |
692 | $summarysearch .= " summary $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
693 | $fullnamesearch .= " fullname $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
694 | } else if (substr($searchterm,0,1) == "-") { |
695 | $searchterm = substr($searchterm,1); |
696 | $summarysearch .= " summary $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
697 | $fullnamesearch .= " fullname $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
698 | } else { |
5930cded |
699 | $summarysearch .= ' summary '. $LIKE .' \'%'. $searchterm .'%\' '; |
700 | $fullnamesearch .= ' fullname '. $LIKE .' \'%'. $searchterm .'%\' '; |
a8b56716 |
701 | } |
702 | |
02ebf404 |
703 | } |
704 | |
d4419d55 |
705 | $selectsql = $CFG->prefix .'course WHERE ('. $fullnamesearch .' OR '. $summarysearch .') AND category > \'0\''; |
a8b56716 |
706 | |
d4419d55 |
707 | $totalcount = count_records_sql('SELECT COUNT(*) FROM '. $selectsql); |
02ebf404 |
708 | |
422770d8 |
709 | $courses = get_records_sql('SELECT * FROM '. $selectsql .' ORDER BY '. $sort, $page, $recordsperpage); |
02ebf404 |
710 | |
711 | if ($courses) { /// Remove unavailable courses from the list |
712 | foreach ($courses as $key => $course) { |
152a9060 |
713 | if (!$course->visible) { |
1c45e42e |
714 | if (!has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { |
02ebf404 |
715 | unset($courses[$key]); |
a8b56716 |
716 | $totalcount--; |
02ebf404 |
717 | } |
718 | } |
719 | } |
720 | } |
721 | |
722 | return $courses; |
723 | } |
724 | |
725 | |
18a97fd8 |
726 | /** |
fbc21ae8 |
727 | * Returns a sorted list of categories |
728 | * |
613bbd7c |
729 | * @param string $parent The parent category if any |
730 | * @param string $sort the sortorder |
731 | * @return array of categories |
fbc21ae8 |
732 | */ |
d4419d55 |
733 | function get_categories($parent='none', $sort='sortorder ASC') { |
02ebf404 |
734 | |
814748c9 |
735 | if ($parent === 'none') { |
d4419d55 |
736 | $categories = get_records('course_categories', '', '', $sort); |
02ebf404 |
737 | } else { |
d4419d55 |
738 | $categories = get_records('course_categories', 'parent', $parent, $sort); |
02ebf404 |
739 | } |
740 | if ($categories) { /// Remove unavailable categories from the list |
02ebf404 |
741 | foreach ($categories as $key => $category) { |
152a9060 |
742 | if (!$category->visible) { |
115a622d |
743 | if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id))) { |
02ebf404 |
744 | unset($categories[$key]); |
745 | } |
746 | } |
747 | } |
748 | } |
749 | return $categories; |
750 | } |
751 | |
752 | |
2327b9df |
753 | /** |
754 | * Returns an array of category ids of all the subcategories for a given |
755 | * category. |
756 | * @param $catid - The id of the category whose subcategories we want to find. |
757 | * @return array of category ids. |
758 | */ |
759 | function get_all_subcategories($catid) { |
760 | |
761 | $subcats = array(); |
762 | |
763 | if ($categories = get_records('course_categories', 'parent', $catid)) { |
764 | foreach ($categories as $cat) { |
765 | array_push($subcats, $cat->id); |
766 | $subcats = array_merge($subcats, get_all_subcategories($cat->id)); |
767 | } |
768 | } |
769 | return $subcats; |
770 | } |
771 | |
772 | |
18a97fd8 |
773 | /** |
ba87a4da |
774 | * This recursive function makes sure that the courseorder is consecutive |
775 | * |
776 | * @param type description |
777 | * |
778 | * $n is the starting point, offered only for compatilibity -- will be ignored! |
779 | * $safe (bool) prevents it from assuming category-sortorder is unique, used to upgrade |
780 | * safely from 1.4 to 1.5 |
781 | */ |
f41ef63e |
782 | function fix_course_sortorder($categoryid=0, $n=0, $safe=0, $depth=0, $path='') { |
5930cded |
783 | |
ba87a4da |
784 | global $CFG; |
8f0cd6ef |
785 | |
02ebf404 |
786 | $count = 0; |
5930cded |
787 | |
f41ef63e |
788 | $catgap = 1000; // "standard" category gap |
789 | $tolerance = 200; // how "close" categories can get |
5930cded |
790 | |
f41ef63e |
791 | if ($categoryid > 0){ |
792 | // update depth and path |
793 | $cat = get_record('course_categories', 'id', $categoryid); |
794 | if ($cat->parent == 0) { |
795 | $depth = 0; |
796 | $path = ''; |
797 | } else if ($depth == 0 ) { // doesn't make sense; get from DB |
798 | // this is only called if the $depth parameter looks dodgy |
799 | $parent = get_record('course_categories', 'id', $cat->parent); |
800 | $path = $parent->path; |
801 | $depth = $parent->depth; |
802 | } |
803 | $path = $path . '/' . $categoryid; |
804 | $depth = $depth + 1; |
ba87a4da |
805 | |
5930cded |
806 | set_field('course_categories', 'path', addslashes($path), 'id', $categoryid); |
807 | set_field('course_categories', 'depth', $depth, 'id', $categoryid); |
f41ef63e |
808 | } |
39f65595 |
809 | |
810 | // get some basic info about courses in the category |
5930cded |
811 | $info = get_record_sql('SELECT MIN(sortorder) AS min, |
ba87a4da |
812 | MAX(sortorder) AS max, |
5930cded |
813 | COUNT(sortorder) AS count |
814 | FROM ' . $CFG->prefix . 'course |
ba87a4da |
815 | WHERE category=' . $categoryid); |
816 | if (is_object($info)) { // no courses? |
817 | $max = $info->max; |
818 | $count = $info->count; |
819 | $min = $info->min; |
820 | unset($info); |
821 | } |
822 | |
814748c9 |
823 | if ($categoryid > 0 && $n==0) { // only passed category so don't shift it |
824 | $n = $min; |
825 | } |
826 | |
39f65595 |
827 | // $hasgap flag indicates whether there's a gap in the sequence |
5930cded |
828 | $hasgap = false; |
39f65595 |
829 | if ($max-$min+1 != $count) { |
830 | $hasgap = true; |
831 | } |
5930cded |
832 | |
39f65595 |
833 | // $mustshift indicates whether the sequence must be shifted to |
834 | // meet its range |
835 | $mustshift = false; |
836 | if ($min < $n+$tolerance || $min > $n+$tolerance+$catgap ) { |
837 | $mustshift = true; |
838 | } |
839 | |
ba87a4da |
840 | // actually sort only if there are courses, |
841 | // and we meet one ofthe triggers: |
842 | // - safe flag |
843 | // - they are not in a continuos block |
844 | // - they are too close to the 'bottom' |
39f65595 |
845 | if ($count && ( $safe || $hasgap || $mustshift ) ) { |
846 | // special, optimized case where all we need is to shift |
847 | if ( $mustshift && !$safe && !$hasgap) { |
848 | $shift = $n + $catgap - $min; |
f8ea6077 |
849 | if ($shift < $count) { |
850 | $shift = $count + $catgap; |
851 | } |
39f65595 |
852 | // UPDATE course SET sortorder=sortorder+$shift |
5930cded |
853 | execute_sql("UPDATE {$CFG->prefix}course |
854 | SET sortorder=sortorder+$shift |
39f65595 |
855 | WHERE category=$categoryid", 0); |
5930cded |
856 | $n = $n + $catgap + $count; |
857 | |
39f65595 |
858 | } else { // do it slowly |
5930cded |
859 | $n = $n + $catgap; |
39f65595 |
860 | // if the new sequence overlaps the current sequence, lack of transactions |
861 | // will stop us -- shift things aside for a moment... |
48505662 |
862 | if ($safe || ($n >= $min && $n+$count+1 < $min && $CFG->dbfamily==='mysql')) { |
d6a49dab |
863 | $shift = $max + $n + 1000; |
5930cded |
864 | execute_sql("UPDATE {$CFG->prefix}course |
865 | SET sortorder=sortorder+$shift |
39f65595 |
866 | WHERE category=$categoryid", 0); |
ba87a4da |
867 | } |
868 | |
39f65595 |
869 | $courses = get_courses($categoryid, 'c.sortorder ASC', 'c.id,c.sortorder'); |
870 | begin_sql(); |
f8ea6077 |
871 | $tx = true; // transaction sanity |
5930cded |
872 | foreach ($courses as $course) { |
f8ea6077 |
873 | if ($tx && $course->sortorder != $n ) { // save db traffic |
874 | $tx = $tx && set_field('course', 'sortorder', $n, |
875 | 'id', $course->id); |
ba87a4da |
876 | } |
877 | $n++; |
878 | } |
f8ea6077 |
879 | if ($tx) { |
880 | commit_sql(); |
881 | } else { |
882 | rollback_sql(); |
883 | if (!$safe) { |
884 | // if we failed when called with !safe, try |
885 | // to recover calling self with safe=true |
886 | return fix_course_sortorder($categoryid, $n, true, $depth, $path); |
887 | } |
888 | } |
5930cded |
889 | } |
02ebf404 |
890 | } |
d4419d55 |
891 | set_field('course_categories', 'coursecount', $count, 'id', $categoryid); |
8f0cd6ef |
892 | |
5930cded |
893 | // $n could need updating |
814748c9 |
894 | $max = get_field_sql("SELECT MAX(sortorder) from {$CFG->prefix}course WHERE category=$categoryid"); |
895 | if ($max > $n) { |
896 | $n = $max; |
897 | } |
758b9a4d |
898 | |
6bc502cc |
899 | if ($categories = get_categories($categoryid)) { |
900 | foreach ($categories as $category) { |
f41ef63e |
901 | $n = fix_course_sortorder($category->id, $n, $safe, $depth, $path); |
6bc502cc |
902 | } |
903 | } |
8f0cd6ef |
904 | |
39f65595 |
905 | return $n+1; |
02ebf404 |
906 | } |
907 | |
d8634192 |
908 | /** |
909 | * Ensure all courses have a valid course category |
910 | * useful if a category has been removed manually |
911 | **/ |
912 | function fix_coursecategory_orphans() { |
913 | |
914 | global $CFG; |
915 | |
916 | // Note: the handling of sortorder here is arguably |
917 | // open to race conditions. Hard to fix here, unlikely |
918 | // to hit anyone in production. |
919 | |
920 | $sql = "SELECT c.id, c.category, c.shortname |
921 | FROM {$CFG->prefix}course c |
922 | LEFT OUTER JOIN {$CFG->prefix}course_categories cc ON c.category=cc.id |
923 | WHERE cc.id IS NULL AND c.id != " . SITEID; |
924 | |
925 | $rs = get_recordset_sql($sql); |
926 | |
927 | if ($rs->RecordCount()){ // we have some orphans |
928 | |
929 | // the "default" category is the lowest numbered... |
930 | $default = get_field_sql("SELECT MIN(id) |
931 | FROM {$CFG->prefix}course_categories"); |
932 | $sortorder = get_field_sql("SELECT MAX(sortorder) |
933 | FROM {$CFG->prefix}course |
934 | WHERE category=$default"); |
935 | |
936 | |
937 | begin_sql(); |
938 | $tx = true; |
939 | while ($tx && $course = rs_fetch_next_record($rs)) { |
940 | $tx = $tx && set_field('course', 'category', $default, 'id', $course->id); |
941 | $tx = $tx && set_field('course', 'sortorder', ++$sortorder, 'id', $course->id); |
942 | } |
943 | if ($tx) { |
944 | commit_sql(); |
945 | } else { |
946 | rollback_sql(); |
947 | } |
948 | } |
949 | } |
950 | |
db4b12eb |
951 | /** |
952 | * List of remote courses that a user has access to via MNET. |
953 | * Works only on the IDP |
954 | * |
955 | * @uses $CFG, $USER |
956 | * @return array {@link $COURSE} of course objects |
957 | */ |
958 | function get_my_remotecourses($userid=0) { |
959 | global $CFG, $USER; |
960 | |
961 | if (empty($userid)) { |
962 | $userid = $USER->id; |
963 | } |
964 | |
5930cded |
965 | $sql = "SELECT c.remoteid, c.shortname, c.fullname, |
86dd62a7 |
966 | c.hostid, c.summary, c.cat_name, |
967 | h.name AS hostname |
db4b12eb |
968 | FROM {$CFG->prefix}mnet_enrol_course c |
969 | JOIN {$CFG->prefix}mnet_enrol_assignments a ON c.id=a.courseid |
86dd62a7 |
970 | JOIN {$CFG->prefix}mnet_host h ON c.hostid=h.id |
db4b12eb |
971 | WHERE a.userid={$userid}"; |
972 | |
973 | return get_records_sql($sql); |
974 | } |
975 | |
976 | /** |
977 | * List of remote hosts that a user has access to via MNET. |
978 | * Works on the SP |
979 | * |
980 | * @uses $CFG, $USER |
981 | * @return array of host objects |
982 | */ |
983 | function get_my_remotehosts() { |
984 | global $CFG, $USER; |
985 | |
986 | if ($USER->mnethostid == $CFG->mnet_localhost_id) { |
987 | return false; // Return nothing on the IDP |
988 | } |
989 | if (!empty($USER->mnet_foreign_host_array) && is_array($USER->mnet_foreign_host_array)) { |
990 | return $USER->mnet_foreign_host_array; |
991 | } |
992 | return false; |
993 | } |
fbc21ae8 |
994 | |
18a97fd8 |
995 | /** |
fbc21ae8 |
996 | * This function creates a default separated/connected scale |
997 | * |
998 | * This function creates a default separated/connected scale |
999 | * so there's something in the database. The locations of |
1000 | * strings and files is a bit odd, but this is because we |
1001 | * need to maintain backward compatibility with many different |
1002 | * existing language translations and older sites. |
1003 | * |
1004 | * @uses $CFG |
1005 | */ |
02ebf404 |
1006 | function make_default_scale() { |
02ebf404 |
1007 | |
1008 | global $CFG; |
1009 | |
1010 | $defaultscale = NULL; |
1011 | $defaultscale->courseid = 0; |
1012 | $defaultscale->userid = 0; |
d4419d55 |
1013 | $defaultscale->name = get_string('separateandconnected'); |
1014 | $defaultscale->scale = get_string('postrating1', 'forum').','. |
1015 | get_string('postrating2', 'forum').','. |
1016 | get_string('postrating3', 'forum'); |
02ebf404 |
1017 | $defaultscale->timemodified = time(); |
1018 | |
8f0cd6ef |
1019 | /// Read in the big description from the file. Note this is not |
02ebf404 |
1020 | /// HTML (despite the file extension) but Moodle format text. |
d4419d55 |
1021 | $parentlang = get_string('parentlang'); |
ee6e91d4 |
1022 | if (is_readable($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) { |
1023 | $file = file($CFG->dataroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html'); |
1024 | } else if (is_readable($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html')) { |
d4419d55 |
1025 | $file = file($CFG->dirroot .'/lang/'. $CFG->lang .'/help/forum/ratings.html'); |
ee6e91d4 |
1026 | } else if ($parentlang and is_readable($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) { |
1027 | $file = file($CFG->dataroot .'/lang/'. $parentlang .'/help/forum/ratings.html'); |
d4419d55 |
1028 | } else if ($parentlang and is_readable($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html')) { |
1029 | $file = file($CFG->dirroot .'/lang/'. $parentlang .'/help/forum/ratings.html'); |
ee6e91d4 |
1030 | } else if (is_readable($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html')) { |
1031 | $file = file($CFG->dirroot .'/lang/en_utf8/help/forum/ratings.html'); |
02ebf404 |
1032 | } else { |
d4419d55 |
1033 | $file = ''; |
02ebf404 |
1034 | } |
1035 | |
d4419d55 |
1036 | $defaultscale->description = addslashes(implode('', $file)); |
02ebf404 |
1037 | |
d4419d55 |
1038 | if ($defaultscale->id = insert_record('scale', $defaultscale)) { |
1039 | execute_sql('UPDATE '. $CFG->prefix .'forum SET scale = \''. $defaultscale->id .'\'', false); |
02ebf404 |
1040 | } |
1041 | } |
1042 | |
fbc21ae8 |
1043 | |
18a97fd8 |
1044 | /** |
fbc21ae8 |
1045 | * Returns a menu of all available scales from the site as well as the given course |
1046 | * |
1047 | * @uses $CFG |
1048 | * @param int $courseid The id of the course as found in the 'course' table. |
7290c7fa |
1049 | * @return object |
fbc21ae8 |
1050 | */ |
02ebf404 |
1051 | function get_scales_menu($courseid=0) { |
02ebf404 |
1052 | |
1053 | global $CFG; |
8f0cd6ef |
1054 | |
1055 | $sql = "SELECT id, name FROM {$CFG->prefix}scale |
1056 | WHERE courseid = '0' or courseid = '$courseid' |
02ebf404 |
1057 | ORDER BY courseid ASC, name ASC"; |
1058 | |
d4419d55 |
1059 | if ($scales = get_records_sql_menu($sql)) { |
02ebf404 |
1060 | return $scales; |
1061 | } |
1062 | |
1063 | make_default_scale(); |
1064 | |
d4419d55 |
1065 | return get_records_sql_menu($sql); |
02ebf404 |
1066 | } |
1067 | |
5baa0ad6 |
1068 | |
1069 | |
1070 | /** |
1071 | * Given a set of timezone records, put them in the database, replacing what is there |
1072 | * |
1073 | * @uses $CFG |
1074 | * @param array $timezones An array of timezone records |
1075 | */ |
1076 | function update_timezone_records($timezones) { |
1077 | /// Given a set of timezone records, put them in the database |
1078 | |
1079 | global $CFG; |
1080 | |
1081 | /// Clear out all the old stuff |
1082 | execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone', false); |
1083 | |
1084 | /// Insert all the new stuff |
1085 | foreach ($timezones as $timezone) { |
1086 | insert_record('timezone', $timezone); |
1087 | } |
1088 | } |
1089 | |
1090 | |
df28d6c5 |
1091 | /// MODULE FUNCTIONS ///////////////////////////////////////////////// |
1092 | |
18a97fd8 |
1093 | /** |
fbc21ae8 |
1094 | * Just gets a raw list of all modules in a course |
1095 | * |
1096 | * @uses $CFG |
1097 | * @param int $courseid The id of the course as found in the 'course' table. |
7290c7fa |
1098 | * @return object |
fbc21ae8 |
1099 | */ |
9fa49e22 |
1100 | function get_course_mods($courseid) { |
9fa49e22 |
1101 | global $CFG; |
1102 | |
3a11c548 |
1103 | if (empty($courseid)) { |
1104 | return false; // avoid warnings |
1105 | } |
1106 | |
7acaa63d |
1107 | return get_records_sql("SELECT cm.*, m.name as modname |
8f0cd6ef |
1108 | FROM {$CFG->prefix}modules m, |
7acaa63d |
1109 | {$CFG->prefix}course_modules cm |
8f0cd6ef |
1110 | WHERE cm.course = '$courseid' |
9fa49e22 |
1111 | AND cm.module = m.id "); |
1112 | } |
1113 | |
fbc21ae8 |
1114 | |
18a97fd8 |
1115 | /** |
f9d5371b |
1116 | * Given an id of a course module, finds the coursemodule description |
fbc21ae8 |
1117 | * |
f9d5371b |
1118 | * @param string $modulename name of module type, eg. resource, assignment,... |
1119 | * @param int $cmid course module id (id in course_modules table) |
1120 | * @param int $courseid optional course id for extra validation |
1121 | * @return object course module instance with instance and module name |
1122 | */ |
1123 | function get_coursemodule_from_id($modulename, $cmid, $courseid=0) { |
1124 | |
1125 | global $CFG; |
1126 | |
1127 | $courseselect = ($courseid) ? "cm.course = '$courseid' AND " : ''; |
1128 | |
1129 | return get_record_sql("SELECT cm.*, m.name, md.name as modname |
1130 | FROM {$CFG->prefix}course_modules cm, |
1131 | {$CFG->prefix}modules md, |
1132 | {$CFG->prefix}$modulename m |
1133 | WHERE $courseselect |
1134 | cm.id = '$cmid' AND |
1135 | cm.instance = m.id AND |
1136 | md.name = '$modulename' AND |
1137 | md.id = cm.module"); |
1138 | } |
1139 | |
1140 | /** |
1141 | * Given an instance number of a module, finds the coursemodule description |
1142 | * |
1143 | * @param string $modulename name of module type, eg. resource, assignment,... |
1144 | * @param int $instance module instance number (id in resource, assignment etc. table) |
1145 | * @param int $courseid optional course id for extra validation |
1146 | * @return object course module instance with instance and module name |
fbc21ae8 |
1147 | */ |
b63c0ee5 |
1148 | function get_coursemodule_from_instance($modulename, $instance, $courseid=0) { |
df28d6c5 |
1149 | |
1150 | global $CFG; |
f9d5371b |
1151 | |
b63c0ee5 |
1152 | $courseselect = ($courseid) ? "cm.course = '$courseid' AND " : ''; |
df28d6c5 |
1153 | |
f9d5371b |
1154 | return get_record_sql("SELECT cm.*, m.name, md.name as modname |
8f0cd6ef |
1155 | FROM {$CFG->prefix}course_modules cm, |
1156 | {$CFG->prefix}modules md, |
1157 | {$CFG->prefix}$modulename m |
b63c0ee5 |
1158 | WHERE $courseselect |
8f0cd6ef |
1159 | cm.instance = m.id AND |
1160 | md.name = '$modulename' AND |
df28d6c5 |
1161 | md.id = cm.module AND |
1162 | m.id = '$instance'"); |
1163 | |
1164 | } |
1165 | |
185cfb09 |
1166 | /** |
1167 | * Returns an array of all the active instances of a particular module in given courses, sorted in the order they are defined |
1168 | * |
1169 | * Returns an array of all the active instances of a particular |
1170 | * module in given courses, sorted in the order they are defined |
1171 | * in the course. Returns false on any errors. |
1172 | * |
1173 | * @uses $CFG |
1174 | * @param string $modulename The name of the module to get instances for |
613bbd7c |
1175 | * @param array $courses This depends on an accurate $course->modinfo |
1176 | * @return array of instances |
185cfb09 |
1177 | */ |
00e12c73 |
1178 | function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $includeinvisible=false) { |
185cfb09 |
1179 | global $CFG; |
1180 | if (empty($courses) || !is_array($courses) || count($courses) == 0) { |
1181 | return array(); |
1182 | } |
1183 | if (!$rawmods = get_records_sql("SELECT cm.id as coursemodule, m.*,cw.section,cm.visible as visible,cm.groupmode, cm.course |
1184 | FROM {$CFG->prefix}course_modules cm, |
1185 | {$CFG->prefix}course_sections cw, |
1186 | {$CFG->prefix}modules md, |
1187 | {$CFG->prefix}$modulename m |
1188 | WHERE cm.course IN (".implode(',',array_keys($courses)).") AND |
1189 | cm.instance = m.id AND |
1190 | cm.section = cw.id AND |
1191 | md.name = '$modulename' AND |
1192 | md.id = cm.module")) { |
1193 | return array(); |
1194 | } |
1195 | |
1196 | $outputarray = array(); |
1197 | |
1198 | foreach ($courses as $course) { |
00e12c73 |
1199 | if ($includeinvisible) { |
1200 | $invisible = -1; |
1201 | } else if (has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id), $userid)) { |
1202 | // Usually hide non-visible instances from students |
185cfb09 |
1203 | $invisible = -1; |
1204 | } else { |
1205 | $invisible = 0; |
1206 | } |
fea43a7f |
1207 | |
1208 | /// Casting $course->modinfo to string prevents one notice when the field is null |
1209 | if (!$modinfo = unserialize((string)$course->modinfo)) { |
185cfb09 |
1210 | continue; |
1211 | } |
1212 | foreach ($modinfo as $mod) { |
1213 | if ($mod->mod == $modulename and $mod->visible > $invisible) { |
1214 | $instance = $rawmods[$mod->cm]; |
1215 | if (!empty($mod->extra)) { |
1216 | $instance->extra = $mod->extra; |
1217 | } |
1218 | $outputarray[] = $instance; |
1219 | } |
1220 | } |
1221 | } |
1222 | |
1223 | return $outputarray; |
1224 | |
1225 | } |
fbc21ae8 |
1226 | |
18a97fd8 |
1227 | /** |
fbc21ae8 |
1228 | * Returns an array of all the active instances of a particular module in a given course, sorted in the order they are defined |
1229 | * |
1230 | * Returns an array of all the active instances of a particular |
1231 | * module in a given course, sorted in the order they are defined |
1232 | * in the course. Returns false on any errors. |
1233 | * |
1234 | * @uses $CFG |
1235 | * @param string $modulename The name of the module to get instances for |
1236 | * @param object(course) $course This depends on an accurate $course->modinfo |
fbc21ae8 |
1237 | */ |
00e12c73 |
1238 | function get_all_instances_in_course($modulename, $course, $userid=NULL, $includeinvisible=false) { |
df28d6c5 |
1239 | |
1240 | global $CFG; |
1241 | |
3cc8b355 |
1242 | if (empty($course->modinfo)) { |
1243 | return array(); |
1244 | } |
1245 | |
fea43a7f |
1246 | if (!$modinfo = unserialize((string)$course->modinfo)) { |
cccb016a |
1247 | return array(); |
1acfbce5 |
1248 | } |
1249 | |
d8c9d8a1 |
1250 | if (!$rawmods = get_records_sql("SELECT cm.id as coursemodule, m.*,cw.section,cm.visible as visible,cm.groupmode,cm.groupingid |
8f0cd6ef |
1251 | FROM {$CFG->prefix}course_modules cm, |
1252 | {$CFG->prefix}course_sections cw, |
1253 | {$CFG->prefix}modules md, |
1254 | {$CFG->prefix}$modulename m |
1255 | WHERE cm.course = '$course->id' AND |
1256 | cm.instance = m.id AND |
8f0cd6ef |
1257 | cm.section = cw.id AND |
1258 | md.name = '$modulename' AND |
cccb016a |
1259 | md.id = cm.module")) { |
1260 | return array(); |
1261 | } |
1262 | |
00e12c73 |
1263 | if ($includeinvisible) { |
1264 | $invisible = -1; |
1265 | } else if (has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id), $userid)) { |
1266 | // Usually hide non-visible instances from students |
cccb016a |
1267 | $invisible = -1; |
1268 | } else { |
1269 | $invisible = 0; |
1270 | } |
1271 | |
78d4711e |
1272 | $outputarray = array(); |
1273 | |
cccb016a |
1274 | foreach ($modinfo as $mod) { |
8a67b03f |
1275 | $mod->id = $mod->cm; |
e6839677 |
1276 | $mod->course = $course->id; |
8a67b03f |
1277 | if (!groups_course_module_visible($mod)) { |
1278 | continue; |
1279 | } |
cccb016a |
1280 | if ($mod->mod == $modulename and $mod->visible > $invisible) { |
7f12f9cd |
1281 | $instance = $rawmods[$mod->cm]; |
1282 | if (!empty($mod->extra)) { |
1283 | $instance->extra = $mod->extra; |
1284 | } |
1285 | $outputarray[] = $instance; |
cccb016a |
1286 | } |
1287 | } |
1288 | |
1289 | return $outputarray; |
df28d6c5 |
1290 | |
1291 | } |
1292 | |
9fa49e22 |
1293 | |
18a97fd8 |
1294 | /** |
fbc21ae8 |
1295 | * Determine whether a module instance is visible within a course |
1296 | * |
1297 | * Given a valid module object with info about the id and course, |
1298 | * and the module's type (eg "forum") returns whether the object |
1299 | * is visible or not |
1300 | * |
1301 | * @uses $CFG |
613bbd7c |
1302 | * @param $moduletype Name of the module eg 'forum' |
1303 | * @param $module Object which is the instance of the module |
7290c7fa |
1304 | * @return bool |
fbc21ae8 |
1305 | */ |
580f2fbc |
1306 | function instance_is_visible($moduletype, $module) { |
580f2fbc |
1307 | |
1308 | global $CFG; |
1309 | |
2b49ae96 |
1310 | if (!empty($module->id)) { |
e6839677 |
1311 | if ($records = get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course |
2b49ae96 |
1312 | FROM {$CFG->prefix}course_modules cm, |
1313 | {$CFG->prefix}modules m |
1314 | WHERE cm.course = '$module->course' AND |
1315 | cm.module = m.id AND |
1316 | m.name = '$moduletype' AND |
1317 | cm.instance = '$module->id'")) { |
5930cded |
1318 | |
2b49ae96 |
1319 | foreach ($records as $record) { // there should only be one - use the first one |
13534ef7 |
1320 | return $record->visible && groups_course_module_visible($record); |
2b49ae96 |
1321 | } |
580f2fbc |
1322 | } |
1323 | } |
580f2fbc |
1324 | return true; // visible by default! |
1325 | } |
1326 | |
a3fb1c45 |
1327 | |
1328 | |
1329 | |
9fa49e22 |
1330 | /// LOG FUNCTIONS ///////////////////////////////////////////////////// |
1331 | |
1332 | |
18a97fd8 |
1333 | /** |
fbc21ae8 |
1334 | * Add an entry to the log table. |
1335 | * |
1336 | * Add an entry to the log table. These are "action" focussed rather |
1337 | * than web server hits, and provide a way to easily reconstruct what |
1338 | * any particular student has been doing. |
1339 | * |
1340 | * @uses $CFG |
1341 | * @uses $USER |
1342 | * @uses $db |
1343 | * @uses $REMOTE_ADDR |
1344 | * @uses SITEID |
89dcb99d |
1345 | * @param int $courseid The course id |
fbc21ae8 |
1346 | * @param string $module The module name - e.g. forum, journal, resource, course, user etc |
f7664880 |
1347 | * @param string $action 'view', 'update', 'add' or 'delete', possibly followed by another word to clarify. |
fbc21ae8 |
1348 | * @param string $url The file and parameters used to see the results of the action |
1349 | * @param string $info Additional description information |
1350 | * @param string $cm The course_module->id if there is one |
1351 | * @param string $user If log regards $user other than $USER |
1352 | */ |
d4419d55 |
1353 | function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) { |
e8395a09 |
1354 | // Note that this function intentionally does not follow the normal Moodle DB access idioms. |
1355 | // This is for a good reason: it is the most frequently used DB update function, |
1356 | // so it has been optimised for speed. |
fcaff7ff |
1357 | global $db, $CFG, $USER; |
9fa49e22 |
1358 | |
7a5b1fc5 |
1359 | if ($cm === '' || is_null($cm)) { // postgres won't translate empty string to its default |
f78b3c34 |
1360 | $cm = 0; |
1361 | } |
1362 | |
3d94772d |
1363 | if ($user) { |
1364 | $userid = $user; |
1365 | } else { |
cb80265b |
1366 | if (!empty($USER->realuser)) { // Don't log |
3d94772d |
1367 | return; |
1368 | } |
d4419d55 |
1369 | $userid = empty($USER->id) ? '0' : $USER->id; |
9fa49e22 |
1370 | } |
1371 | |
fcaff7ff |
1372 | $REMOTE_ADDR = getremoteaddr(); |
1373 | |
9fa49e22 |
1374 | $timenow = time(); |
1375 | $info = addslashes($info); |
10a760b9 |
1376 | if (!empty($url)) { // could break doing html_entity_decode on an empty var. |
1377 | $url = html_entity_decode($url); // for php < 4.3.0 this is defined in moodlelib.php |
1378 | } |
853df85e |
1379 | |
1380 | if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; $PERF->logwrites++;}; |
1381 | |
8b497bbc |
1382 | if ($CFG->type = 'oci8po') { |
1383 | if (empty($info)) { |
1384 | $info = ' '; |
1385 | } |
1386 | } |
1387 | |
d4419d55 |
1388 | $result = $db->Execute('INSERT INTO '. $CFG->prefix .'log (time, userid, course, ip, module, cmid, action, url, info) |
1389 | VALUES (' . "'$timenow', '$userid', '$courseid', '$REMOTE_ADDR', '$module', '$cm', '$action', '$url', '$info')"); |
ebc3bd2b |
1390 | |
ea82d6b6 |
1391 | if (!$result and debugging()) { |
d4419d55 |
1392 | echo '<p>Error: Could not insert a new entry to the Moodle log</p>'; // Don't throw an error |
8f0cd6ef |
1393 | } |
cb80265b |
1394 | |
7c3dab9f |
1395 | /// Store lastaccess times for the current user, do not use in cron and other commandline scripts |
a9fcd13a |
1396 | /// only update the lastaccess/timeaccess fields only once every 60s |
7c3dab9f |
1397 | if (!empty($USER->id) && ($userid == $USER->id) && !defined('FULLME')) { |
5930cded |
1398 | $db->Execute('UPDATE '. $CFG->prefix .'user |
cb80265b |
1399 | SET lastip=\''. $REMOTE_ADDR .'\', lastaccess=\''. $timenow .'\' |
a9fcd13a |
1400 | WHERE id = \''. $userid .'\' AND '.$timenow.' - lastaccess > 60'); |
cb80265b |
1401 | if ($courseid != SITEID && !empty($courseid)) { |
853df85e |
1402 | if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++;}; |
5930cded |
1403 | |
a9fcd13a |
1404 | if ($ulid = get_field('user_lastaccess', 'id', 'userid', $userid, 'courseid', $courseid)) { |
1405 | $db->Execute("UPDATE {$CFG->prefix}user_lastaccess |
1406 | SET timeaccess=$timenow |
1407 | WHERE id = $ulid AND $timenow - timeaccess > 60"); |
cb80265b |
1408 | } else { |
a9fcd13a |
1409 | $db->Execute("INSERT INTO {$CFG->prefix}user_lastaccess |
1410 | ('userid', 'courseid', 'timeaccess') |
1411 | VALUES ($userid, $courseid, $timenow)"); |
114176a2 |
1412 | } |
a9fcd13a |
1413 | if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++;}; |
3d94772d |
1414 | } |
8f0cd6ef |
1415 | } |
9fa49e22 |
1416 | } |
1417 | |
1418 | |
18a97fd8 |
1419 | /** |
fbc21ae8 |
1420 | * Select all log records based on SQL criteria |
1421 | * |
1422 | * @uses $CFG |
1423 | * @param string $select SQL select criteria |
1424 | * @param string $order SQL order by clause to sort the records returned |
1425 | * @param string $limitfrom ? |
1426 | * @param int $limitnum ? |
1427 | * @param int $totalcount Passed in by reference. |
7290c7fa |
1428 | * @return object |
fbc21ae8 |
1429 | * @todo Finish documenting this function |
1430 | */ |
d4419d55 |
1431 | function get_logs($select, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount) { |
9fa49e22 |
1432 | global $CFG; |
1433 | |
519d369f |
1434 | if ($order) { |
d4419d55 |
1435 | $order = 'ORDER BY '. $order; |
519d369f |
1436 | } |
1437 | |
fbc21ae8 |
1438 | $selectsql = $CFG->prefix .'log l LEFT JOIN '. $CFG->prefix .'user u ON l.userid = u.id '. ((strlen($select) > 0) ? 'WHERE '. $select : ''); |
a2ddd957 |
1439 | $countsql = $CFG->prefix.'log l '.((strlen($select) > 0) ? ' WHERE '. $select : ''); |
1440 | |
1441 | $totalcount = count_records_sql("SELECT COUNT(*) FROM $countsql"); |
519d369f |
1442 | |
d4419d55 |
1443 | return get_records_sql('SELECT l.*, u.firstname, u.lastname, u.picture |
93a89227 |
1444 | FROM '. $selectsql .' '. $order, $limitfrom, $limitnum) ; |
9fa49e22 |
1445 | } |
1446 | |
519d369f |
1447 | |
18a97fd8 |
1448 | /** |
fbc21ae8 |
1449 | * Select all log records for a given course and user |
1450 | * |
1451 | * @uses $CFG |
2f87145b |
1452 | * @uses DAYSECS |
fbc21ae8 |
1453 | * @param int $userid The id of the user as found in the 'user' table. |
1454 | * @param int $courseid The id of the course as found in the 'course' table. |
1455 | * @param string $coursestart ? |
1456 | * @todo Finish documenting this function |
1457 | */ |
9fa49e22 |
1458 | function get_logs_usercourse($userid, $courseid, $coursestart) { |
1459 | global $CFG; |
1460 | |
da0c90c3 |
1461 | if ($courseid) { |
d4419d55 |
1462 | $courseselect = ' AND course = \''. $courseid .'\' '; |
2700d113 |
1463 | } else { |
1464 | $courseselect = ''; |
da0c90c3 |
1465 | } |
1466 | |
1604a0fc |
1467 | return get_records_sql("SELECT floor((time - $coursestart)/". DAYSECS .") as day, count(*) as num |
8f0cd6ef |
1468 | FROM {$CFG->prefix}log |
1469 | WHERE userid = '$userid' |
1604a0fc |
1470 | AND time > '$coursestart' $courseselect |
9fa49e22 |
1471 | GROUP BY day "); |
1472 | } |
1473 | |
18a97fd8 |
1474 | /** |
fbc21ae8 |
1475 | * Select all log records for a given course, user, and day |
1476 | * |
1477 | * @uses $CFG |
2f87145b |
1478 | * @uses HOURSECS |
fbc21ae8 |
1479 | * @param int $userid The id of the user as found in the 'user' table. |
1480 | * @param int $courseid The id of the course as found in the 'course' table. |
1481 | * @param string $daystart ? |
7290c7fa |
1482 | * @return object |
fbc21ae8 |
1483 | * @todo Finish documenting this function |
1484 | */ |
9fa49e22 |
1485 | function get_logs_userday($userid, $courseid, $daystart) { |
1486 | global $CFG; |
1487 | |
7e4a6488 |
1488 | if ($courseid) { |
d4419d55 |
1489 | $courseselect = ' AND course = \''. $courseid .'\' '; |
2700d113 |
1490 | } else { |
1491 | $courseselect = ''; |
7e4a6488 |
1492 | } |
1493 | |
1604a0fc |
1494 | return get_records_sql("SELECT floor((time - $daystart)/". HOURSECS .") as hour, count(*) as num |
9fa49e22 |
1495 | FROM {$CFG->prefix}log |
8f0cd6ef |
1496 | WHERE userid = '$userid' |
1604a0fc |
1497 | AND time > '$daystart' $courseselect |
9fa49e22 |
1498 | GROUP BY hour "); |
1499 | } |
1500 | |
b4bac9b6 |
1501 | /** |
1502 | * Returns an object with counts of failed login attempts |
1503 | * |
8f0cd6ef |
1504 | * Returns information about failed login attempts. If the current user is |
1505 | * an admin, then two numbers are returned: the number of attempts and the |
b4bac9b6 |
1506 | * number of accounts. For non-admins, only the attempts on the given user |
1507 | * are shown. |
1508 | * |
fbc21ae8 |
1509 | * @param string $mode Either 'admin', 'teacher' or 'everybody' |
1510 | * @param string $username The username we are searching for |
1511 | * @param string $lastlogin The date from which we are searching |
1512 | * @return int |
b4bac9b6 |
1513 | */ |
b4bac9b6 |
1514 | function count_login_failures($mode, $username, $lastlogin) { |
1515 | |
d4419d55 |
1516 | $select = 'module=\'login\' AND action=\'error\' AND time > '. $lastlogin; |
b4bac9b6 |
1517 | |
51792df0 |
1518 | if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Return information about all accounts |
b4bac9b6 |
1519 | if ($count->attempts = count_records_select('log', $select)) { |
1520 | $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)'); |
1521 | return $count; |
1522 | } |
9407d456 |
1523 | } else if ($mode == 'everybody' or ($mode == 'teacher' and isteacherinanycourse())) { |
d4419d55 |
1524 | if ($count->attempts = count_records_select('log', $select .' AND info = \''. $username .'\'')) { |
b4bac9b6 |
1525 | return $count; |
1526 | } |
1527 | } |
1528 | return NULL; |
1529 | } |
1530 | |
1531 | |
a3fb1c45 |
1532 | /// GENERAL HELPFUL THINGS /////////////////////////////////// |
1533 | |
18a97fd8 |
1534 | /** |
fbc21ae8 |
1535 | * Dump a given object's information in a PRE block. |
1536 | * |
1537 | * Mostly just used for debugging. |
1538 | * |
1539 | * @param mixed $object The data to be printed |
fbc21ae8 |
1540 | */ |
a3fb1c45 |
1541 | function print_object($object) { |
1aa7b31d |
1542 | echo '<pre class="notifytiny">' . htmlspecialchars(print_r($object,true)) . '</pre>'; |
a3fb1c45 |
1543 | } |
1544 | |
0986271b |
1545 | function course_parent_visible($course = null) { |
fa145ae1 |
1546 | global $CFG; |
1547 | |
418b4e5a |
1548 | if (empty($course)) { |
1549 | return true; |
1550 | } |
1551 | if (!empty($CFG->allowvisiblecoursesinhiddencategories)) { |
1552 | return true; |
1553 | } |
0986271b |
1554 | return category_parent_visible($course->category); |
1555 | } |
1556 | |
1557 | function category_parent_visible($parent = 0) { |
5930cded |
1558 | |
824f1c40 |
1559 | static $visible; |
1560 | |
0986271b |
1561 | if (!$parent) { |
1562 | return true; |
1563 | } |
5930cded |
1564 | |
824f1c40 |
1565 | if (empty($visible)) { |
1566 | $visible = array(); // initialize |
1567 | } |
1568 | |
1569 | if (array_key_exists($parent,$visible)) { |
1570 | return $visible[$parent]; |
1571 | } |
5930cded |
1572 | |
0986271b |
1573 | $category = get_record('course_categories', 'id', $parent); |
1574 | $list = explode('/', preg_replace('/^\/(.*)$/', '$1', $category->path)); |
1575 | $list[] = $parent; |
1576 | $parents = get_records_list('course_categories', 'id', implode(',', $list), 'depth DESC'); |
824f1c40 |
1577 | $v = true; |
1578 | foreach ($parents as $p) { |
1579 | if (!$p->visible) { |
1580 | $v = false; |
0986271b |
1581 | } |
1582 | } |
824f1c40 |
1583 | $visible[$parent] = $v; // now cache it |
1584 | return $v; |
0986271b |
1585 | } |
1586 | |
62d4e774 |
1587 | /** |
5930cded |
1588 | * This function is the official hook inside XMLDB stuff to delegate its debug to one |
62d4e774 |
1589 | * external function. |
1590 | * |
1591 | * Any script can avoid calls to this function by defining XMLDB_SKIP_DEBUG_HOOK before |
1592 | * using XMLDB classes. Obviously, also, if this function doesn't exist, it isn't invoked ;-) |
1593 | * |
1594 | * @param $message string contains the error message |
1595 | * @param $object object XMLDB object that fired the debug |
1596 | */ |
1597 | function xmldb_debug($message, $object) { |
1598 | |
92b564f4 |
1599 | debugging($message, DEBUG_DEVELOPER); |
62d4e774 |
1600 | } |
1601 | |
49860445 |
1602 | /** |
1603 | * Get the lists of courses the current user has $cap capability in |
5930cded |
1604 | * I am not sure if this is needed, it loops through all courses so |
1605 | * could cause performance problems. |
1606 | * If it's not used, we can use a faster function to detect |
49860445 |
1607 | * capability in restorelib.php |
1608 | * @param string $cap |
1609 | * @return array |
1610 | */ |
1611 | function get_capability_courses($cap) { |
1612 | global $USER; |
5930cded |
1613 | |
49860445 |
1614 | $mycourses = array(); |
1615 | if ($courses = get_records('course')) { |
1616 | foreach ($courses as $course) { |
1617 | if (has_capability($cap, get_context_instance(CONTEXT_COURSE, $course->id))) { |
5930cded |
1618 | $mycourses[] = $course->id; |
49860445 |
1619 | } |
1620 | } |
1621 | } |
5930cded |
1622 | |
49860445 |
1623 | return $mycourses; |
5930cded |
1624 | } |
1625 | |
49860445 |
1626 | /** |
1627 | * true or false function to see if user can create any courses at all |
1628 | * @return bool |
1629 | */ |
1630 | function user_can_create_courses() { |
1631 | global $USER; |
1632 | // if user has course creation capability at any site or course cat, then return true; |
5930cded |
1633 | |
49860445 |
1634 | if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { |
5930cded |
1635 | return true; |
49860445 |
1636 | } else { |
5930cded |
1637 | return (bool) count(get_creatable_categories()); |
49860445 |
1638 | } |
5930cded |
1639 | |
49860445 |
1640 | } |
1641 | |
1642 | /** |
1643 | * get the list of categories the current user can create courses in |
1644 | * @return array |
1645 | */ |
1646 | function get_creatable_categories() { |
5930cded |
1647 | |
49860445 |
1648 | $creatablecats = array(); |
1649 | if ($cats = get_records('course_categories')) { |
1650 | foreach ($cats as $cat) { |
1651 | if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) { |
1652 | $creatablecats[$cat->id] = $cat->name; |
1653 | } |
1654 | } |
1655 | } |
1656 | return $creatablecats; |
1657 | } |
1658 | |
41883f79 |
1659 | /** |
1660 | * Turn an array of ints into a string usable in an IN sql clause... |
1661 | * |
1662 | **/ |
1663 | function sql_intarray_to_in($array) { |
1664 | |
1665 | $na = array(); |
1666 | $c = count($array); |
1667 | for ($n=0;$n<$c;$n++) { |
1668 | if (isset($array[$n]) && is_int($array[$n])) { |
1669 | $na[] = $array[$n]; |
1670 | } |
1671 | } |
1672 | return join(',',$array); |
1673 | } |
1674 | |
9d5b689c |
1675 | // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: |
03517306 |
1676 | ?> |