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