df28d6c5 |
1 | <?PHP // $Id$ |
2 | |
3 | /// FUNCTIONS FOR DATABASE HANDLING //////////////////////////////// |
4 | |
5 | function execute_sql($command, $feedback=true) { |
6 | /// Completely general function - it just runs some SQL and reports success. |
7 | |
8 | global $db; |
9 | |
10 | $result = $db->Execute("$command"); |
11 | |
12 | if ($result) { |
13 | if ($feedback) { |
14 | echo "<P><FONT COLOR=green><B>".get_string("success")."</B></FONT></P>"; |
15 | } |
16 | return true; |
17 | } else { |
18 | if ($feedback) { |
19 | echo "<P><FONT COLOR=red><B>".get_string("error")."</B></FONT></P>"; |
20 | } |
21 | return false; |
22 | } |
23 | } |
24 | |
25 | function modify_database($sqlfile) { |
26 | /// Assumes that the input text file consists of a number |
27 | /// of SQL statements ENDING WITH SEMICOLONS. The semicolons |
28 | /// MUST be the last character in a line. |
29 | /// Lines that are blank or that start with "#" are ignored. |
30 | /// Only tested with mysql dump files (mysqldump -p -d moodle) |
31 | |
32 | global $CFG; |
33 | |
34 | if (file_exists($sqlfile)) { |
35 | $success = true; |
36 | $lines = file($sqlfile); |
37 | $command = ""; |
38 | |
39 | while ( list($i, $line) = each($lines) ) { |
40 | $line = chop($line); |
41 | $length = strlen($line); |
42 | |
43 | if ($length && substr($line, 0, 1) <> "#") { |
44 | if (substr($line, $length-1, 1) == ";") { |
45 | $line = substr($line, 0, $length-1); // strip ; |
46 | $command .= $line; |
47 | $command = str_replace("prefix_", $CFG->prefix, $command); // Table prefixes |
48 | if (! execute_sql($command)) { |
49 | $success = false; |
50 | } |
51 | $command = ""; |
52 | } else { |
53 | $command .= $line; |
54 | } |
55 | } |
56 | } |
57 | |
58 | } else { |
59 | $success = false; |
60 | echo "<P>Tried to modify database, but \"$sqlfile\" doesn't exist!</P>"; |
61 | } |
62 | |
63 | return $success; |
64 | } |
65 | |
66 | |
5c63e0c4 |
67 | function record_exists($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") { |
df28d6c5 |
68 | /// Returns true or false depending on whether the specified record exists |
69 | |
70 | global $CFG; |
71 | |
5c63e0c4 |
72 | if ($field1) { |
73 | $select = "WHERE $field1 = '$value1'"; |
9fa49e22 |
74 | if ($field2) { |
df28d6c5 |
75 | $select .= " AND $field2 = '$value2'"; |
9fa49e22 |
76 | if ($field3) { |
df28d6c5 |
77 | $select .= " AND $field3 = '$value3'"; |
78 | } |
79 | } |
5c63e0c4 |
80 | } else { |
81 | $select = ""; |
df28d6c5 |
82 | } |
83 | |
84 | return record_exists_sql("SELECT * FROM $CFG->prefix$table $select LIMIT 1"); |
85 | } |
86 | |
87 | |
88 | function record_exists_sql($sql) { |
89 | /// Returns true or false depending on whether the specified record exists |
90 | /// The sql statement is provided as a string. |
91 | |
92 | global $db; |
93 | |
94 | $rs = $db->Execute($sql); |
e53b0823 |
95 | if (empty($rs)) return false; |
df28d6c5 |
96 | |
97 | if ( $rs->RecordCount() ) { |
98 | return true; |
99 | } else { |
100 | return false; |
101 | } |
102 | } |
103 | |
104 | |
5c63e0c4 |
105 | function count_records($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") { |
df28d6c5 |
106 | /// Get all the records and count them |
107 | |
108 | global $CFG; |
109 | |
5c63e0c4 |
110 | if ($field1) { |
111 | $select = "WHERE $field1 = '$value1'"; |
9fa49e22 |
112 | if ($field2) { |
df28d6c5 |
113 | $select .= " AND $field2 = '$value2'"; |
9fa49e22 |
114 | if ($field3) { |
df28d6c5 |
115 | $select .= " AND $field3 = '$value3'"; |
116 | } |
117 | } |
5c63e0c4 |
118 | } else { |
119 | $select = ""; |
df28d6c5 |
120 | } |
121 | |
122 | return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select"); |
123 | } |
124 | |
9fa49e22 |
125 | function count_records_select($table, $select="") { |
126 | /// Get all the records and count them |
127 | |
128 | global $CFG; |
129 | |
d26d7ed0 |
130 | if ($select) { |
131 | $select = "WHERE $select"; |
132 | } |
133 | |
9fa49e22 |
134 | return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select"); |
135 | } |
136 | |
137 | |
df28d6c5 |
138 | function count_records_sql($sql) { |
139 | /// Get all the records and count them |
140 | /// The sql statement is provided as a string. |
141 | |
142 | global $db; |
143 | |
144 | $rs = $db->Execute("$sql"); |
e53b0823 |
145 | if (empty($rs)) return 0; |
df28d6c5 |
146 | |
147 | return $rs->fields[0]; |
148 | } |
149 | |
5c63e0c4 |
150 | function get_record($table, $field1, $value1, $field2="", $value2="", $field3="", $value3="") { |
df28d6c5 |
151 | /// Get a single record as an object |
152 | |
153 | global $CFG; |
154 | |
5c63e0c4 |
155 | $select = "WHERE $field1 = '$value1'"; |
df28d6c5 |
156 | |
9fa49e22 |
157 | if ($field2) { |
df28d6c5 |
158 | $select .= " AND $field2 = '$value2'"; |
9fa49e22 |
159 | if ($field3) { |
df28d6c5 |
160 | $select .= " AND $field3 = '$value3'"; |
161 | } |
162 | } |
163 | |
164 | return get_record_sql("SELECT * FROM $CFG->prefix$table $select"); |
165 | } |
166 | |
167 | function get_record_sql($sql) { |
168 | /// Get a single record as an object |
169 | /// The sql statement is provided as a string. |
170 | |
171 | global $db; |
172 | |
173 | $rs = $db->Execute("$sql"); |
e53b0823 |
174 | if (empty($rs)) return false; |
df28d6c5 |
175 | |
176 | if ( $rs->RecordCount() == 1 ) { |
177 | return (object)$rs->fields; |
178 | } else { |
179 | return false; |
180 | } |
181 | } |
182 | |
18496c59 |
183 | function get_record_select($table, $select="", $fields="*") { |
184 | /// Gets one record from a table, as an object |
185 | /// "select" is a fragment of SQL to define the selection criteria |
186 | |
187 | global $CFG; |
188 | |
189 | if ($select) { |
190 | $select = "WHERE $select"; |
191 | } |
192 | |
193 | return get_record_sql("SELECT $fields FROM $CFG->prefix$table $select"); |
194 | } |
195 | |
196 | |
df28d6c5 |
197 | function get_records($table, $field="", $value="", $sort="", $fields="*") { |
198 | /// Get a number of records as an array of objects |
199 | /// Can optionally be sorted eg "time ASC" or "time DESC" |
200 | /// If "fields" is specified, only those fields are returned |
201 | /// The "key" is the first column returned, eg usually "id" |
202 | |
203 | global $CFG; |
204 | |
9fa49e22 |
205 | if ($field) { |
df28d6c5 |
206 | $select = "WHERE $field = '$value'"; |
5c63e0c4 |
207 | } else { |
208 | $select = ""; |
df28d6c5 |
209 | } |
5c63e0c4 |
210 | |
df28d6c5 |
211 | if ($sort) { |
5c63e0c4 |
212 | $sort = "ORDER BY $sort"; |
df28d6c5 |
213 | } |
214 | |
5c63e0c4 |
215 | return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort"); |
df28d6c5 |
216 | } |
217 | |
9fa49e22 |
218 | function get_records_select($table, $select="", $sort="", $fields="*") { |
219 | /// Get a number of records as an array of objects |
220 | /// Can optionally be sorted eg "time ASC" or "time DESC" |
221 | /// "select" is a fragment of SQL to define the selection criteria |
222 | /// The "key" is the first column returned, eg usually "id" |
223 | |
224 | global $CFG; |
225 | |
d26d7ed0 |
226 | if ($select) { |
227 | $select = "WHERE $select"; |
5c63e0c4 |
228 | } |
229 | |
230 | if ($sort) { |
231 | $sort = "ORDER BY $sort"; |
d26d7ed0 |
232 | } |
233 | |
5c63e0c4 |
234 | return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort"); |
9fa49e22 |
235 | } |
236 | |
df28d6c5 |
237 | |
238 | function get_records_list($table, $field="", $values="", $sort="", $fields="*") { |
9fa49e22 |
239 | /// Get a number of records as an array of objects |
240 | /// Differs from get_records() in that the values variable |
241 | /// can be a comma-separated list of values eg "4,5,6,10" |
242 | /// Can optionally be sorted eg "time ASC" or "time DESC" |
243 | /// The "key" is the first column returned, eg usually "id" |
df28d6c5 |
244 | |
245 | global $CFG; |
246 | |
9fa49e22 |
247 | if ($field) { |
df28d6c5 |
248 | $select = "WHERE $field in ($values)"; |
5c63e0c4 |
249 | } else { |
250 | $select = ""; |
df28d6c5 |
251 | } |
5c63e0c4 |
252 | |
df28d6c5 |
253 | if ($sort) { |
5c63e0c4 |
254 | $sort = "ORDER BY $sort"; |
df28d6c5 |
255 | } |
256 | |
5c63e0c4 |
257 | return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort"); |
df28d6c5 |
258 | } |
259 | |
260 | |
9fa49e22 |
261 | |
df28d6c5 |
262 | function get_records_sql($sql) { |
9fa49e22 |
263 | /// Get a number of records as an array of objects |
264 | /// The "key" is the first column returned, eg usually "id" |
265 | /// The sql statement is provided as a string. |
df28d6c5 |
266 | |
267 | global $db; |
268 | |
269 | $rs = $db->Execute("$sql"); |
e53b0823 |
270 | if (empty($rs)) return false; |
df28d6c5 |
271 | |
272 | if ( $rs->RecordCount() > 0 ) { |
273 | if ($records = $rs->GetAssoc(true)) { |
274 | foreach ($records as $key => $record) { |
275 | $objects[$key] = (object) $record; |
276 | } |
277 | return $objects; |
278 | } else { |
279 | return false; |
280 | } |
281 | } else { |
282 | return false; |
283 | } |
284 | } |
285 | |
9fa49e22 |
286 | function get_records_menu($table, $field="", $value="", $sort="", $fields="*") { |
287 | /// Get a number of records as an array of objects |
288 | /// Can optionally be sorted eg "time ASC" or "time DESC" |
289 | /// If "fields" is specified, only those fields are returned |
290 | /// The "key" is the first column returned, eg usually "id" |
291 | |
292 | global $CFG; |
293 | |
294 | if ($field) { |
295 | $select = "WHERE $field = '$value'"; |
5c63e0c4 |
296 | } else { |
297 | $select = ""; |
9fa49e22 |
298 | } |
5c63e0c4 |
299 | |
9fa49e22 |
300 | if ($sort) { |
5c63e0c4 |
301 | $sort = "ORDER BY $sort"; |
9fa49e22 |
302 | } |
303 | |
5c63e0c4 |
304 | return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sort"); |
9fa49e22 |
305 | } |
306 | |
307 | function get_records_select_menu($table, $select="", $sort="", $fields="*") { |
308 | /// Get a number of records as an array of objects |
309 | /// Can optionally be sorted eg "time ASC" or "time DESC" |
310 | /// "select" is a fragment of SQL to define the selection criteria |
311 | /// Returns associative array of first two fields |
312 | |
313 | global $CFG; |
314 | |
d26d7ed0 |
315 | if ($select) { |
316 | $select = "WHERE $select"; |
317 | } |
318 | |
5c63e0c4 |
319 | if ($sort) { |
320 | $sort = "ORDER BY $sort"; |
321 | } |
322 | |
323 | return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sort"); |
9fa49e22 |
324 | } |
325 | |
326 | |
df28d6c5 |
327 | function get_records_sql_menu($sql) { |
9fa49e22 |
328 | /// Given an SQL select, this function returns an associative |
329 | /// array of the first two columns. This is most useful in |
330 | /// combination with the choose_from_menu function to create |
331 | /// a form menu. |
df28d6c5 |
332 | |
333 | global $db; |
334 | |
335 | $rs = $db->Execute("$sql"); |
e53b0823 |
336 | if (empty($rs)) return false; |
df28d6c5 |
337 | |
338 | if ( $rs->RecordCount() > 0 ) { |
339 | while (!$rs->EOF) { |
340 | $menu[$rs->fields[0]] = $rs->fields[1]; |
341 | $rs->MoveNext(); |
342 | } |
343 | return $menu; |
344 | |
345 | } else { |
346 | return false; |
347 | } |
348 | } |
349 | |
350 | function get_field($table, $return, $field, $value) { |
351 | /// Get a single field from a database record |
352 | |
353 | global $db, $CFG; |
354 | |
355 | $rs = $db->Execute("SELECT $return FROM $CFG->prefix$table WHERE $field = '$value'"); |
e53b0823 |
356 | if (empty($rs)) return false; |
df28d6c5 |
357 | |
358 | if ( $rs->RecordCount() == 1 ) { |
359 | return $rs->fields["$return"]; |
360 | } else { |
361 | return false; |
362 | } |
363 | } |
364 | |
365 | function set_field($table, $newfield, $newvalue, $field, $value) { |
366 | /// Set a single field in a database record |
367 | |
368 | global $db, $CFG; |
369 | |
370 | return $db->Execute("UPDATE $CFG->prefix$table SET $newfield = '$newvalue' WHERE $field = '$value'"); |
371 | } |
372 | |
373 | |
5c63e0c4 |
374 | function delete_records($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") { |
df28d6c5 |
375 | /// Delete one or more records from a table |
376 | |
377 | global $db, $CFG; |
378 | |
5c63e0c4 |
379 | if ($field1) { |
380 | $select = "WHERE $field1 = '$value1'"; |
9fa49e22 |
381 | if ($field2) { |
df28d6c5 |
382 | $select .= " AND $field2 = '$value2'"; |
9fa49e22 |
383 | if ($field3) { |
df28d6c5 |
384 | $select .= " AND $field3 = '$value3'"; |
385 | } |
386 | } |
5c63e0c4 |
387 | } else { |
388 | $select = ""; |
df28d6c5 |
389 | } |
390 | |
391 | return $db->Execute("DELETE FROM $CFG->prefix$table $select"); |
392 | } |
393 | |
394 | |
395 | function insert_record($table, $dataobject, $returnid=true) { |
396 | /// Insert a record into a table and return the "id" field if required |
397 | /// If the return ID isn't required, then this just reports success as true/false. |
398 | /// $dataobject is an object containing needed data |
399 | |
400 | global $db, $CFG; |
401 | |
402 | // Determine all the fields needed |
403 | if (! $columns = $db->MetaColumns("$CFG->prefix$table")) { |
404 | return false; |
405 | } |
406 | |
407 | $data = (array)$dataobject; |
408 | |
b3fa6684 |
409 | // Pull out data from the dataobject that matches the fields in the table. |
410 | // If fields are missing or empty, then try to set the defaults explicitly |
411 | // because some databases (eg PostgreSQL) don't always set them properly |
df28d6c5 |
412 | foreach ($columns as $column) { |
b3fa6684 |
413 | if ($column->name <> "id") { |
414 | if (isset($data[$column->name])) { |
415 | if ($data[$column->name] == "" and isset($column->has_default)) { |
416 | $ddd[$column->name] = $column->default_value; |
417 | } else { |
418 | $ddd[$column->name] = $data[$column->name]; |
419 | } |
420 | } else { |
421 | if (isset($column->has_default)) { |
422 | $ddd[$column->name] = $column->default_value; |
423 | } |
424 | } |
df28d6c5 |
425 | } |
426 | } |
427 | |
b3fa6684 |
428 | |
df28d6c5 |
429 | // Construct SQL queries |
430 | if (! $numddd = count($ddd)) { |
431 | return false; |
432 | } |
433 | |
434 | $count = 0; |
435 | $inscolumns = ""; |
436 | $insvalues = ""; |
437 | $select = ""; |
438 | |
439 | foreach ($ddd as $key => $value) { |
440 | $count++; |
441 | $inscolumns .= "$key"; |
442 | $insvalues .= "'$value'"; |
443 | $select .= "$key = '$value'"; |
444 | if ($count < $numddd) { |
445 | $inscolumns .= ", "; |
446 | $insvalues .= ", "; |
447 | $select .= " AND "; |
448 | } |
449 | } |
450 | |
451 | if (! $rs = $db->Execute("INSERT INTO $CFG->prefix$table ($inscolumns) VALUES ($insvalues)")) { |
452 | return false; |
453 | } |
454 | |
455 | if ($returnid) { |
99988d1a |
456 | if ($CFG->dbtype == "mysql") { |
457 | return $db->Insert_ID(); // ADOdb has stored the ID for us, but it isn't reliable |
458 | } |
1523be78 |
459 | |
460 | // Try to pull the record out again to find the id. This is the most cross-platform method. |
df28d6c5 |
461 | if ($rs = $db->Execute("SELECT id FROM $CFG->prefix$table WHERE $select")) { |
1523be78 |
462 | if ($rs->RecordCount() == 1) { |
463 | return $rs->fields[0]; |
464 | } |
df28d6c5 |
465 | } |
1523be78 |
466 | |
467 | return false; |
468 | |
df28d6c5 |
469 | } else { |
470 | return true; |
471 | } |
472 | } |
473 | |
474 | |
475 | function update_record($table, $dataobject) { |
476 | /// Update a record in a table |
477 | /// $dataobject is an object containing needed data |
ebc3bd2b |
478 | /// Relies on $dataobject having a variable "id" to |
479 | /// specify the record to update |
df28d6c5 |
480 | |
481 | global $db, $CFG; |
482 | |
483 | if (! isset($dataobject->id) ) { |
484 | return false; |
485 | } |
486 | |
487 | // Determine all the fields in the table |
488 | if (!$columns = $db->MetaColumns("$CFG->prefix$table")) { |
489 | return false; |
490 | } |
491 | $data = (array)$dataobject; |
492 | |
493 | // Pull out data matching these fields |
494 | foreach ($columns as $column) { |
495 | if ($column->name <> "id" && isset($data[$column->name]) ) { |
496 | $ddd[$column->name] = $data[$column->name]; |
497 | } |
498 | } |
499 | |
500 | // Construct SQL queries |
501 | $numddd = count($ddd); |
502 | $count = 0; |
503 | $update = ""; |
504 | |
505 | foreach ($ddd as $key => $value) { |
506 | $count++; |
507 | $update .= "$key = '$value'"; |
508 | if ($count < $numddd) { |
509 | $update .= ", "; |
510 | } |
511 | } |
512 | |
513 | if ($rs = $db->Execute("UPDATE $CFG->prefix$table SET $update WHERE id = '$dataobject->id'")) { |
514 | return true; |
515 | } else { |
516 | return false; |
517 | } |
518 | } |
519 | |
520 | |
521 | function print_object($object) { |
522 | /// Mostly just for debugging |
523 | |
524 | $array = (array)$object; |
525 | foreach ($array as $key => $item) { |
526 | echo "$key -> $item <BR>"; |
527 | } |
528 | } |
529 | |
530 | |
531 | |
532 | |
533 | /// USER DATABASE //////////////////////////////////////////////// |
534 | |
535 | function get_user_info_from_db($field, $value) { |
536 | /// Get a complete user record, which includes all the info |
537 | /// in the user record, as well as membership information |
538 | /// Suitable for setting as $USER session cookie. |
539 | |
18496c59 |
540 | if (!$field or !$value) { |
df28d6c5 |
541 | return false; |
df28d6c5 |
542 | } |
543 | |
18496c59 |
544 | if (! $user = get_record_select("user", "$field = '$value' AND deleted <> '1'")) { |
545 | return false; |
546 | } |
df28d6c5 |
547 | |
18496c59 |
548 | // Add membership information |
df28d6c5 |
549 | |
18496c59 |
550 | if ($site = get_site()) { // Everyone is always a member of the top course |
551 | $user->student[$site->id] = true; |
552 | } |
df28d6c5 |
553 | |
f53af941 |
554 | if ($students = get_records("user_students", "userid", $user->id)) { |
555 | foreach ($students as $student) { |
556 | $user->student[$student->course] = true; |
557 | } |
18496c59 |
558 | } |
df28d6c5 |
559 | |
f53af941 |
560 | if ($teachers = get_records("user_teachers", "userid", $user->id)) { |
561 | foreach ($teachers as $teacher) { |
562 | $user->teacher[$teacher->course] = true; |
563 | } |
18496c59 |
564 | } |
df28d6c5 |
565 | |
f53af941 |
566 | if ($admins = get_records("user_admins", "userid", $user->id)) { |
567 | foreach ($admins as $admin) { |
568 | $user->admin = true; |
569 | break; |
570 | } |
df28d6c5 |
571 | } |
18496c59 |
572 | |
573 | return $user; |
df28d6c5 |
574 | } |
575 | |
576 | function update_user_in_db() { |
577 | /// Updates user record to record their last access |
578 | |
579 | global $db, $USER, $REMOTE_ADDR, $CFG; |
580 | |
581 | if (!isset($USER->id)) |
582 | return false; |
583 | |
584 | $timenow = time(); |
585 | if ($db->Execute("UPDATE {$CFG->prefix}user SET lastIP='$REMOTE_ADDR', lastaccess='$timenow' |
586 | WHERE id = '$USER->id' ")) { |
587 | return true; |
588 | } else { |
589 | return false; |
590 | } |
591 | } |
592 | |
593 | |
594 | function adminlogin($username, $md5password) { |
595 | /// Does this username and password specify a valid admin user? |
596 | |
597 | global $CFG; |
598 | |
9fa49e22 |
599 | return record_exists_sql("SELECT u.id |
600 | FROM {$CFG->prefix}user u, |
601 | {$CFG->prefix}user_admins a |
ebc3bd2b |
602 | WHERE u.id = a.userid |
df28d6c5 |
603 | AND u.username = '$username' |
604 | AND u.password = '$md5password'"); |
605 | } |
606 | |
607 | |
608 | function get_site () { |
609 | /// Returns $course object of the top-level site. |
610 | |
611 | if ( $course = get_record("course", "category", 0)) { |
612 | return $course; |
613 | } else { |
614 | return false; |
615 | } |
616 | } |
617 | |
9fa49e22 |
618 | |
619 | function get_courses($category=0, $sort="fullname ASC") { |
620 | /// Returns list of courses |
621 | |
622 | if ($category > 0) { // Return all courses in one category |
623 | return get_records("course", "category", $category, $sort); |
624 | |
625 | } else if ($category < 0) { // Return all courses, even the site |
626 | return get_records("course", "", "", $sort); |
627 | |
628 | } else { // Return all courses, except site |
629 | return get_records_select("course", "category > 0", $sort); |
630 | } |
631 | } |
632 | |
633 | function get_categories() { |
634 | return get_records("course_categories", "", "", "name"); |
635 | } |
636 | |
637 | |
638 | function get_guest() { |
639 | return get_user_info_from_db("username", "guest"); |
640 | } |
641 | |
642 | |
df28d6c5 |
643 | function get_admin () { |
644 | /// Returns $user object of the main admin user |
645 | |
646 | global $CFG; |
647 | |
648 | if ( $admins = get_admins() ) { |
649 | foreach ($admins as $admin) { |
650 | return $admin; // ie the first one |
651 | } |
652 | } else { |
653 | return false; |
654 | } |
655 | } |
656 | |
657 | function get_admins() { |
658 | /// Returns list of all admins |
659 | |
660 | global $CFG; |
661 | |
ebc3bd2b |
662 | return get_records_sql("SELECT u.* |
663 | FROM {$CFG->prefix}user u, |
664 | {$CFG->prefix}user_admins a |
665 | WHERE a.userid = u.id |
666 | ORDER BY u.id ASC"); |
df28d6c5 |
667 | } |
668 | |
669 | |
670 | function get_teacher($courseid) { |
671 | /// Returns $user object of the main teacher for a course |
672 | |
673 | global $CFG; |
674 | |
675 | if ( $teachers = get_course_teachers($courseid, "t.authority ASC")) { |
676 | foreach ($teachers as $teacher) { |
677 | if ($teacher->authority) { |
678 | return $teacher; // the highest authority teacher |
679 | } |
680 | } |
681 | } else { |
682 | return false; |
683 | } |
684 | } |
685 | |
686 | function get_course_students($courseid, $sort="u.lastaccess DESC") { |
687 | /// Returns list of all students in this course |
688 | |
689 | global $CFG; |
690 | |
691 | return get_records_sql("SELECT u.* FROM {$CFG->prefix}user u, {$CFG->prefix}user_students s |
ebc3bd2b |
692 | WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0' |
df28d6c5 |
693 | ORDER BY $sort"); |
694 | } |
695 | |
696 | function get_course_teachers($courseid, $sort="t.authority ASC") { |
697 | /// Returns list of all teachers in this course |
698 | |
699 | global $CFG; |
700 | |
701 | return get_records_sql("SELECT u.*,t.authority,t.role FROM {$CFG->prefix}user u, {$CFG->prefix}user_teachers t |
ebc3bd2b |
702 | WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0' |
df28d6c5 |
703 | ORDER BY $sort"); |
704 | } |
705 | |
706 | function get_course_users($courseid, $sort="u.lastaccess DESC") { |
707 | /// Using this method because the direct SQL just would not always work! |
708 | |
709 | $teachers = get_course_teachers($courseid, $sort); |
710 | $students = get_course_students($courseid, $sort); |
711 | |
712 | if ($teachers and $students) { |
713 | return array_merge($teachers, $students); |
714 | } else if ($teachers) { |
715 | return $teachers; |
716 | } else { |
717 | return $students; |
718 | } |
719 | |
720 | /// return get_records_sql("SELECT u.* FROM user u, user_students s, user_teachers t |
ebc3bd2b |
721 | /// WHERE (s.course = '$courseid' AND s.userid = u.id) OR |
722 | /// (t.course = '$courseid' AND t.userid = u.id) |
df28d6c5 |
723 | /// ORDER BY $sort"); |
724 | } |
725 | |
726 | |
9fa49e22 |
727 | function get_users_search($search, $sort="u.firstname ASC") { |
728 | global $CFG; |
729 | |
730 | return get_records_sql("SELECT * from {$CFG->prefix}user |
731 | WHERE confirmed = 1 |
732 | AND deleted = 0 |
733 | AND (firstname LIKE '%$search%' OR |
734 | lastname LIKE '%$search%' OR |
735 | email LIKE '%$search%') |
736 | AND username <> 'guest' |
737 | AND username <> 'changeme'"); |
738 | } |
739 | |
740 | |
741 | function get_users_count() { |
31fefa63 |
742 | return count_records_select("user", "username <> 'guest' AND deleted <> 1"); |
9fa49e22 |
743 | } |
744 | |
745 | function get_users_listing($sort, $dir="ASC", $page=1, $recordsperpage=20) { |
746 | global $CFG; |
31fefa63 |
747 | |
748 | if ($CFG->dbtype == "mysql") { |
749 | $limit = "LIMIT $page,$recordsperpage"; |
750 | } else { |
751 | $limit = "LIMIT $recordsperpage,$page"; |
752 | } |
9fa49e22 |
753 | return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess |
754 | FROM {$CFG->prefix}user |
755 | WHERE username <> 'guest' |
756 | AND deleted <> '1' |
31fefa63 |
757 | ORDER BY $sort $dir $limit"); |
9fa49e22 |
758 | |
759 | } |
760 | |
761 | function get_users_confirmed() { |
762 | global $CFG; |
763 | return get_records_sql("SELECT * |
764 | FROM {$CFG->prefix}user |
765 | WHERE confirmed = 1 |
766 | AND deleted = 0 |
767 | AND username <> 'guest' |
768 | AND username <> 'changeme'"); |
769 | } |
770 | |
771 | |
99988d1a |
772 | function get_users_unconfirmed($cutofftime=2000000000) { |
9fa49e22 |
773 | global $CFG; |
774 | return get_records_sql("SELECT * |
775 | FROM {$CFG->prefix}user |
776 | WHERE confirmed = 0 |
777 | AND firstaccess > 0 |
778 | AND firstaccess < '$cutofftime'"); |
779 | } |
780 | |
781 | |
782 | function get_users_longtimenosee($cutofftime) { |
783 | global $CFG; |
31fefa63 |
784 | |
785 | $db->debug = true; |
9fa49e22 |
786 | return get_records_sql("SELECT u.* |
787 | FROM {$CFG->prefix}user u, |
788 | {$CFG->prefix}user_students s |
31fefa63 |
789 | WHERE u.lastaccess > '0' |
790 | AND u.lastaccess < '$cutofftime' |
97485d07 |
791 | AND u.id = s.userid |
792 | GROUP BY u.id"); |
9fa49e22 |
793 | } |
794 | |
795 | |
df28d6c5 |
796 | |
797 | /// MODULE FUNCTIONS ///////////////////////////////////////////////// |
798 | |
9fa49e22 |
799 | function get_course_mods($courseid) { |
800 | /// Just gets a raw list of all modules in a course |
801 | global $CFG; |
802 | |
803 | return get_records_sql("SELECT cm.*, m.name as modname |
804 | FROM {$CFG->prefix}modules m, {$CFG->prefix}course_modules cm |
805 | WHERE cm.course = '$courseid' |
806 | AND cm.deleted = '0' |
807 | AND cm.module = m.id "); |
808 | } |
809 | |
df28d6c5 |
810 | function get_coursemodule_from_instance($modulename, $instance, $courseid) { |
811 | /// Given an instance of a module, finds the coursemodule description |
812 | |
813 | global $CFG; |
814 | |
815 | return get_record_sql("SELECT cm.*, m.name |
816 | FROM {$CFG->prefix}course_modules cm, {$CFG->prefix}modules md, {$CFG->prefix}$modulename m |
817 | WHERE cm.course = '$courseid' AND |
818 | cm.deleted = '0' AND |
819 | cm.instance = m.id AND |
820 | md.name = '$modulename' AND |
821 | md.id = cm.module AND |
822 | m.id = '$instance'"); |
823 | |
824 | } |
825 | |
826 | function get_all_instances_in_course($modulename, $courseid, $sort="cw.section") { |
827 | /// Returns an array of all the active instances of a particular |
828 | /// module in a given course. Returns false on any errors. |
829 | |
830 | global $CFG; |
831 | |
832 | return get_records_sql("SELECT m.*,cw.section,cm.id as coursemodule |
833 | FROM {$CFG->prefix}course_modules cm, {$CFG->prefix}course_sections cw, |
834 | {$CFG->prefix}modules md, {$CFG->prefix}$modulename m |
835 | WHERE cm.course = '$courseid' AND |
836 | cm.instance = m.id AND |
837 | cm.deleted = '0' AND |
838 | cm.section = cw.id AND |
839 | md.name = '$modulename' AND |
840 | md.id = cm.module |
841 | ORDER BY $sort"); |
842 | |
843 | } |
844 | |
9fa49e22 |
845 | |
846 | /// LOG FUNCTIONS ///////////////////////////////////////////////////// |
847 | |
848 | |
849 | function add_to_log($course, $module, $action, $url="", $info="") { |
850 | /// Add an entry to the log table. These are "action" focussed rather |
851 | /// than web server hits, and provide a way to easily reconstruct what |
852 | /// any particular student has been doing. |
853 | /// |
854 | /// course = the course id |
855 | /// module = forum, journal, resource, course, user etc |
856 | /// action = view, edit, post (often but not always the same as the file.php) |
857 | /// url = the file and parameters used to see the results of the action |
858 | /// info = additional description information |
859 | |
31fefa63 |
860 | global $db, $CFG, $USER, $REMOTE_ADDR; |
9fa49e22 |
861 | |
862 | if (isset($USER->realuser)) { // Don't log |
863 | return; |
864 | } |
865 | |
61e96406 |
866 | $userid = empty($USER->id) ? "" : $USER->id; |
867 | |
9fa49e22 |
868 | $timenow = time(); |
869 | $info = addslashes($info); |
870 | |
31fefa63 |
871 | $result = $db->Execute("INSERT INTO {$CFG->prefix}log (time, |
ebc3bd2b |
872 | userid, |
873 | course, |
874 | ip, |
875 | module, |
876 | action, |
877 | url, |
878 | info) |
879 | VALUES ('$timenow', |
61e96406 |
880 | '$userid', |
ebc3bd2b |
881 | '$course', |
882 | '$REMOTE_ADDR', |
883 | '$module', |
884 | '$action', |
885 | '$url', |
886 | '$info')"); |
887 | |
9fa49e22 |
888 | if (!$result) { |
889 | echo "<P>Error: Could not insert a new entry to the Moodle log</P>"; // Don't throw an error |
890 | } |
891 | } |
892 | |
893 | |
894 | function get_logs($select, $order) { |
895 | global $CFG; |
896 | |
897 | return get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture |
898 | FROM {$CFG->prefix}log l, |
899 | {$CFG->prefix}user u |
900 | $select $order"); |
901 | } |
902 | |
903 | function get_logs_usercourse($userid, $courseid, $coursestart) { |
904 | global $CFG; |
905 | |
906 | return get_records_sql("SELECT floor((`time` - $coursestart)/86400) as day, count(*) as num |
907 | FROM {$CFG->prefix}log |
ebc3bd2b |
908 | WHERE userid = '$userid' |
9fa49e22 |
909 | AND course = '$courseid' |
910 | AND `time` > '$coursestart' |
911 | GROUP BY day "); |
912 | } |
913 | |
914 | function get_logs_userday($userid, $courseid, $daystart) { |
915 | global $CFG; |
916 | |
917 | return get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour, count(*) as num |
918 | FROM {$CFG->prefix}log |
ebc3bd2b |
919 | WHERE userid = '$userid' |
9fa49e22 |
920 | AND course = '$courseid' |
921 | AND `time` > '$daystart' |
922 | GROUP BY hour "); |
923 | } |
924 | |
925 | |
df28d6c5 |
926 | ?> |