Email addresses are now well and truly obfuscated on public profile pages
[moodle.git] / lib / datalib.php
CommitLineData
df28d6c5 1<?PHP // $Id$
2
3/// FUNCTIONS FOR DATABASE HANDLING ////////////////////////////////
4
5function 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
2b051f1c 25function modify_database($sqlfile="", $sqlstring="") {
26/// Assumes that the input text (file or string consists of
27/// a number of SQL statements ENDING WITH SEMICOLONS. The
28/// semicolons MUST be the last character in a line.
df28d6c5 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
2b051f1c 34 $success = true; // Let's be optimistic :-)
35
36 if (!empty($sqlfile)) {
37 if (!is_readable($sqlfile)) {
38 $success = false;
39 echo "<P>Tried to modify database, but \"$sqlfile\" doesn't exist!</P>";
40 return $success;
41 } else {
42 $lines = file($sqlfile);
43 }
44 } else {
45 $lines[] = $sqlstring;
46 }
47
48 $command = "";
49
50 foreach ($lines as $line) {
51 $line = rtrim($line);
52 $length = strlen($line);
53
54 if ($length and $line[0] <> "#") {
55 if (substr($line, $length-1, 1) == ";") {
56 $line = substr($line, 0, $length-1); // strip ;
57 $command .= $line;
58 $command = str_replace("prefix_", $CFG->prefix, $command); // Table prefixes
59 if (! execute_sql($command)) {
60 $success = false;
df28d6c5 61 }
2b051f1c 62 $command = "";
63 } else {
64 $command .= $line;
df28d6c5 65 }
66 }
df28d6c5 67 }
68
69 return $success;
2b051f1c 70
df28d6c5 71}
72
a3fb1c45 73/// FUNCTIONS TO MODIFY TABLES ////////////////////////////////////////////
74
92230499 75function table_column($table, $oldfield, $field, $type="integer", $size="10",
76 $signed="unsigned", $default="0", $null="not null", $after="") {
a3fb1c45 77/// Add a new field to a table, or modify an existing one (if oldfield is defined).
8a230a7d 78 global $CFG, $db;
a3fb1c45 79
80 switch (strtolower($CFG->dbtype)) {
81
82 case "mysql":
83 case "mysqlt":
84
85 switch (strtolower($type)) {
a3fb1c45 86 case "integer":
92230499 87 $type = "INTEGER($size)";
a3fb1c45 88 break;
92230499 89 case "varchar":
90 $type = "VARCHAR($size)";
a3fb1c45 91 break;
92 }
93
94 if (!empty($oldfield)) {
95 $operation = "CHANGE $oldfield $field";
96 } else {
97 $operation = "ADD $field";
98 }
99
100 $default = "DEFAULT '$default'";
101
102 if (!empty($after)) {
31ce4b53 103 $after = "AFTER `$after`";
a3fb1c45 104 }
105
106 execute_sql("ALTER TABLE {$CFG->prefix}$table $operation $type $signed $default $null $after");
107 break;
108
5a4d292b 109 case "postgres7": // From Petri Asikainen
8a230a7d 110 //Check db-version
111 $dbinfo = $db->ServerInfo();
112 $dbver = substr($dbinfo[version],0,3);
113
114 $field = "$field";
5a4d292b 115 //to prevent conflicts with reserved words
5a4d292b 116 $oldfield = "\"$oldfield\"";
117
118 switch (strtolower($type)) {
119 case "integer":
120 if ($size <= 2) {
121 $type = "INT2";
122 }
123 if ($size <= 4) {
124 $type = "INT";
125 }
126 if ($size > 4) {
127 $type = "INT8";
128 }
129 break;
130 case "varchar":
131 $type = "VARCHAR($size)";
132 break;
133 }
134
8a230a7d 135 $default = "'$default'";
5a4d292b 136
137 //After is not implemented in postgesql
138 //if (!empty($after)) {
139 // $after = "AFTER '$after'";
140 //}
141
cefc7e81 142 if ($oldfield != "\"\"") {
5a4d292b 143 execute_sql("ALTER TABLE {$CFG->prefix}$table RENAME COLUMN $oldfield TO $field");
144 } else {
145 execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type");
146 }
147
8a230a7d 148 if ($dbver >= "7.3") {
149 // modifying 'not null' is posible before 7.3
150 //update default values to table
151 if ($null == "NOT NULL") {
152 execute_sql("UPDATE {$CFG->prefix}$table SET $field=$default where $field IS NULL");
153 execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $null");
154 } else {
155 execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field DROP NOT NULL");
156 }
5a4d292b 157 }
8a230a7d 158
159 execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET DEFAULT $default");
5a4d292b 160
161 break;
a3fb1c45 162
163 default:
164 switch (strtolower($type)) {
a3fb1c45 165 case "integer":
a3fb1c45 166 $type = "INTEGER";
167 break;
92230499 168 case "varchar":
169 $type = "VARCHAR";
170 break;
a3fb1c45 171 }
172
173 $default = "DEFAULT '$default'";
174
175 if (!empty($after)) {
31ce4b53 176 $after = "AFTER $after";
a3fb1c45 177 }
178
179 if (!empty($oldfield)) {
180 execute_sql("ALTER TABLE {$CFG->prefix}$table RENAME COLUMN $oldfield $field");
181 } else {
182 execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type");
183 }
184
185 execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $null");
186 execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $default");
187 break;
188
189 }
190}
191
192
193
194/// GENERIC FUNCTIONS TO CHECK AND COUNT RECORDS ////////////////////////////////////////
df28d6c5 195
5c63e0c4 196function record_exists($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") {
df28d6c5 197/// Returns true or false depending on whether the specified record exists
198
199 global $CFG;
200
5c63e0c4 201 if ($field1) {
202 $select = "WHERE $field1 = '$value1'";
9fa49e22 203 if ($field2) {
df28d6c5 204 $select .= " AND $field2 = '$value2'";
9fa49e22 205 if ($field3) {
df28d6c5 206 $select .= " AND $field3 = '$value3'";
207 }
208 }
5c63e0c4 209 } else {
210 $select = "";
df28d6c5 211 }
212
213 return record_exists_sql("SELECT * FROM $CFG->prefix$table $select LIMIT 1");
214}
215
216
217function record_exists_sql($sql) {
218/// Returns true or false depending on whether the specified record exists
219/// The sql statement is provided as a string.
220
221 global $db;
222
223 $rs = $db->Execute($sql);
e53b0823 224 if (empty($rs)) return false;
df28d6c5 225
226 if ( $rs->RecordCount() ) {
227 return true;
228 } else {
229 return false;
230 }
231}
232
233
5c63e0c4 234function count_records($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") {
df28d6c5 235/// Get all the records and count them
236
237 global $CFG;
238
5c63e0c4 239 if ($field1) {
240 $select = "WHERE $field1 = '$value1'";
9fa49e22 241 if ($field2) {
df28d6c5 242 $select .= " AND $field2 = '$value2'";
9fa49e22 243 if ($field3) {
df28d6c5 244 $select .= " AND $field3 = '$value3'";
245 }
246 }
5c63e0c4 247 } else {
248 $select = "";
df28d6c5 249 }
250
251 return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select");
252}
253
9fa49e22 254function count_records_select($table, $select="") {
255/// Get all the records and count them
256
257 global $CFG;
258
d26d7ed0 259 if ($select) {
260 $select = "WHERE $select";
261 }
262
9fa49e22 263 return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select");
264}
265
266
df28d6c5 267function count_records_sql($sql) {
268/// Get all the records and count them
269/// The sql statement is provided as a string.
270
271 global $db;
272
273 $rs = $db->Execute("$sql");
e53b0823 274 if (empty($rs)) return 0;
df28d6c5 275
276 return $rs->fields[0];
277}
278
a3fb1c45 279
280
281
282/// GENERIC FUNCTIONS TO GET, INSERT, OR UPDATE DATA ///////////////////////////////////
283
5c63e0c4 284function get_record($table, $field1, $value1, $field2="", $value2="", $field3="", $value3="") {
df28d6c5 285/// Get a single record as an object
286
287 global $CFG;
288
5c63e0c4 289 $select = "WHERE $field1 = '$value1'";
df28d6c5 290
9fa49e22 291 if ($field2) {
df28d6c5 292 $select .= " AND $field2 = '$value2'";
9fa49e22 293 if ($field3) {
df28d6c5 294 $select .= " AND $field3 = '$value3'";
295 }
296 }
297
298 return get_record_sql("SELECT * FROM $CFG->prefix$table $select");
299}
300
301function get_record_sql($sql) {
302/// Get a single record as an object
303/// The sql statement is provided as a string.
dc7e8cb5 304/// A LIMIT is added to keep limit the returned records to 1
df28d6c5 305
306 global $db;
307
dc7e8cb5 308 $rs = $db->Execute("$sql LIMIT 1");
e53b0823 309 if (empty($rs)) return false;
df28d6c5 310
311 if ( $rs->RecordCount() == 1 ) {
312 return (object)$rs->fields;
313 } else {
314 return false;
315 }
316}
317
18496c59 318function get_record_select($table, $select="", $fields="*") {
319/// Gets one record from a table, as an object
320/// "select" is a fragment of SQL to define the selection criteria
321
322 global $CFG;
323
324 if ($select) {
325 $select = "WHERE $select";
326 }
327
328 return get_record_sql("SELECT $fields FROM $CFG->prefix$table $select");
329}
330
331
df28d6c5 332function get_records($table, $field="", $value="", $sort="", $fields="*") {
333/// Get a number of records as an array of objects
334/// Can optionally be sorted eg "time ASC" or "time DESC"
335/// If "fields" is specified, only those fields are returned
336/// The "key" is the first column returned, eg usually "id"
337
338 global $CFG;
339
9fa49e22 340 if ($field) {
df28d6c5 341 $select = "WHERE $field = '$value'";
5c63e0c4 342 } else {
343 $select = "";
df28d6c5 344 }
5c63e0c4 345
df28d6c5 346 if ($sort) {
5c63e0c4 347 $sort = "ORDER BY $sort";
df28d6c5 348 }
349
5c63e0c4 350 return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort");
df28d6c5 351}
352
9fa49e22 353function get_records_select($table, $select="", $sort="", $fields="*") {
354/// Get a number of records as an array of objects
355/// Can optionally be sorted eg "time ASC" or "time DESC"
356/// "select" is a fragment of SQL to define the selection criteria
357/// The "key" is the first column returned, eg usually "id"
358
359 global $CFG;
360
d26d7ed0 361 if ($select) {
362 $select = "WHERE $select";
5c63e0c4 363 }
364
365 if ($sort) {
366 $sort = "ORDER BY $sort";
d26d7ed0 367 }
368
5c63e0c4 369 return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort");
9fa49e22 370}
371
df28d6c5 372
373function get_records_list($table, $field="", $values="", $sort="", $fields="*") {
9fa49e22 374/// Get a number of records as an array of objects
375/// Differs from get_records() in that the values variable
376/// can be a comma-separated list of values eg "4,5,6,10"
377/// Can optionally be sorted eg "time ASC" or "time DESC"
378/// The "key" is the first column returned, eg usually "id"
df28d6c5 379
380 global $CFG;
381
9fa49e22 382 if ($field) {
df28d6c5 383 $select = "WHERE $field in ($values)";
5c63e0c4 384 } else {
385 $select = "";
df28d6c5 386 }
5c63e0c4 387
df28d6c5 388 if ($sort) {
5c63e0c4 389 $sort = "ORDER BY $sort";
df28d6c5 390 }
391
5c63e0c4 392 return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort");
df28d6c5 393}
394
395
9fa49e22 396
df28d6c5 397function get_records_sql($sql) {
9fa49e22 398/// Get a number of records as an array of objects
399/// The "key" is the first column returned, eg usually "id"
400/// The sql statement is provided as a string.
df28d6c5 401
402 global $db;
403
404 $rs = $db->Execute("$sql");
e53b0823 405 if (empty($rs)) return false;
df28d6c5 406
407 if ( $rs->RecordCount() > 0 ) {
408 if ($records = $rs->GetAssoc(true)) {
409 foreach ($records as $key => $record) {
410 $objects[$key] = (object) $record;
411 }
412 return $objects;
413 } else {
414 return false;
415 }
416 } else {
417 return false;
418 }
419}
420
9fa49e22 421function get_records_menu($table, $field="", $value="", $sort="", $fields="*") {
422/// Get a number of records as an array of objects
423/// Can optionally be sorted eg "time ASC" or "time DESC"
424/// If "fields" is specified, only those fields are returned
425/// The "key" is the first column returned, eg usually "id"
426
427 global $CFG;
428
429 if ($field) {
430 $select = "WHERE $field = '$value'";
5c63e0c4 431 } else {
432 $select = "";
9fa49e22 433 }
5c63e0c4 434
9fa49e22 435 if ($sort) {
5c63e0c4 436 $sort = "ORDER BY $sort";
9fa49e22 437 }
438
5c63e0c4 439 return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sort");
9fa49e22 440}
441
442function get_records_select_menu($table, $select="", $sort="", $fields="*") {
443/// Get a number of records as an array of objects
444/// Can optionally be sorted eg "time ASC" or "time DESC"
445/// "select" is a fragment of SQL to define the selection criteria
446/// Returns associative array of first two fields
447
448 global $CFG;
449
d26d7ed0 450 if ($select) {
451 $select = "WHERE $select";
452 }
453
5c63e0c4 454 if ($sort) {
455 $sort = "ORDER BY $sort";
456 }
457
458 return get_records_sql_menu("SELECT $fields FROM $CFG->prefix$table $select $sort");
9fa49e22 459}
460
461
df28d6c5 462function get_records_sql_menu($sql) {
9fa49e22 463/// Given an SQL select, this function returns an associative
464/// array of the first two columns. This is most useful in
465/// combination with the choose_from_menu function to create
466/// a form menu.
df28d6c5 467
468 global $db;
469
470 $rs = $db->Execute("$sql");
e53b0823 471 if (empty($rs)) return false;
df28d6c5 472
473 if ( $rs->RecordCount() > 0 ) {
474 while (!$rs->EOF) {
475 $menu[$rs->fields[0]] = $rs->fields[1];
476 $rs->MoveNext();
477 }
478 return $menu;
479
480 } else {
481 return false;
482 }
483}
484
ec2a28a6 485function get_field($table, $return, $field1, $value1, $field2="", $value2="", $field3="", $value3="") {
df28d6c5 486/// Get a single field from a database record
487
488 global $db, $CFG;
489
ec2a28a6 490 $select = "WHERE $field1 = '$value1'";
491
492 if ($field2) {
493 $select .= " AND $field2 = '$value2'";
494 if ($field3) {
495 $select .= " AND $field3 = '$value3'";
496 }
497 }
498
499 $rs = $db->Execute("SELECT $return FROM $CFG->prefix$table $select");
e53b0823 500 if (empty($rs)) return false;
df28d6c5 501
502 if ( $rs->RecordCount() == 1 ) {
503 return $rs->fields["$return"];
504 } else {
505 return false;
506 }
507}
508
ec2a28a6 509function set_field($table, $newfield, $newvalue, $field1, $value1, $field2="", $value2="", $field3="", $value3="") {
df28d6c5 510/// Set a single field in a database record
511
512 global $db, $CFG;
513
ec2a28a6 514 $select = "WHERE $field1 = '$value1'";
515
516 if ($field2) {
517 $select .= " AND $field2 = '$value2'";
518 if ($field3) {
519 $select .= " AND $field3 = '$value3'";
520 }
521 }
522
523 return $db->Execute("UPDATE $CFG->prefix$table SET $newfield = '$newvalue' $select");
df28d6c5 524}
525
526
5c63e0c4 527function delete_records($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") {
df28d6c5 528/// Delete one or more records from a table
529
530 global $db, $CFG;
531
5c63e0c4 532 if ($field1) {
533 $select = "WHERE $field1 = '$value1'";
9fa49e22 534 if ($field2) {
df28d6c5 535 $select .= " AND $field2 = '$value2'";
9fa49e22 536 if ($field3) {
df28d6c5 537 $select .= " AND $field3 = '$value3'";
538 }
539 }
5c63e0c4 540 } else {
541 $select = "";
df28d6c5 542 }
543
544 return $db->Execute("DELETE FROM $CFG->prefix$table $select");
545}
546
30f89d68 547function delete_records_select($table, $select="") {
548/// Delete one or more records from a table
549/// "select" is a fragment of SQL to define the selection criteria
550
551 global $CFG, $db;
552
553 if ($select) {
554 $select = "WHERE $select";
555 }
556
557 return $db->Execute("DELETE FROM $CFG->prefix$table $select");
558}
559
df28d6c5 560
561function insert_record($table, $dataobject, $returnid=true) {
562/// Insert a record into a table and return the "id" field if required
563/// If the return ID isn't required, then this just reports success as true/false.
564/// $dataobject is an object containing needed data
565
566 global $db, $CFG;
567
568 // Determine all the fields needed
569 if (! $columns = $db->MetaColumns("$CFG->prefix$table")) {
570 return false;
571 }
df28d6c5 572 $data = (array)$dataobject;
573
b3fa6684 574 // Pull out data from the dataobject that matches the fields in the table.
575 // If fields are missing or empty, then try to set the defaults explicitly
576 // because some databases (eg PostgreSQL) don't always set them properly
df28d6c5 577 foreach ($columns as $column) {
9655b654 578 if(isset($column->primary_key) and $column->primary_key == 1) {
579 $pkey = $column->name; // take column name of primary key
580 }
b3fa6684 581 if ($column->name <> "id") {
582 if (isset($data[$column->name])) {
50913937 583 if ((string)$data[$column->name] == "" and !empty($column->has_default) and !empty($column->default_value)) {
b3fa6684 584 $ddd[$column->name] = $column->default_value;
585 } else {
dc8791f3 586 $ddd[$column->name] = $data[$column->name];
b3fa6684 587 }
588 } else {
92230499 589 if (!empty($column->has_default) and !empty($column->default_value)) {
b3fa6684 590 $ddd[$column->name] = $column->default_value;
591 }
592 }
df28d6c5 593 }
594 }
595
b3fa6684 596
df28d6c5 597 // Construct SQL queries
598 if (! $numddd = count($ddd)) {
599 return false;
600 }
601
602 $count = 0;
603 $inscolumns = "";
604 $insvalues = "";
605 $select = "";
606
607 foreach ($ddd as $key => $value) {
3c72e2f9 608 if (!is_null($value)){
609 if ($select) {
610 $inscolumns .= ", ";
611 $insvalues .= ", ";
612 $select .= " AND ";
613 }
614 $inscolumns .= "$key";
615 $insvalues .= "'$value'";
616 $select .= "$key = '$value'";
df28d6c5 617 }
618 }
619
620 if (! $rs = $db->Execute("INSERT INTO $CFG->prefix$table ($inscolumns) VALUES ($insvalues)")) {
621 return false;
622 }
623
624 if ($returnid) {
64929926 625 if ($CFG->dbtype == "mysql" ) {
99988d1a 626 return $db->Insert_ID(); // ADOdb has stored the ID for us, but it isn't reliable
627 }
1523be78 628
9655b654 629 if ($CFG->dbtype == "postgres7" and isset($pkey)){
630 $oid = $db->Insert_ID();
28ba0d55 631 if ($rs = $db->Execute("SELECT $pkey FROM $CFG->prefix$table WHERE oid = $oid")) {
9655b654 632 if ($rs->RecordCount() == 1) {
633 return $rs->fields[0];
634 } else {
635 return false;
636 }
637 }
638 }
1523be78 639 // Try to pull the record out again to find the id. This is the most cross-platform method.
df28d6c5 640 if ($rs = $db->Execute("SELECT id FROM $CFG->prefix$table WHERE $select")) {
1523be78 641 if ($rs->RecordCount() == 1) {
642 return $rs->fields[0];
643 }
df28d6c5 644 }
1523be78 645
646 return false;
647
df28d6c5 648 } else {
649 return true;
650 }
651}
652
653
654function update_record($table, $dataobject) {
655/// Update a record in a table
656/// $dataobject is an object containing needed data
ebc3bd2b 657/// Relies on $dataobject having a variable "id" to
658/// specify the record to update
df28d6c5 659
660 global $db, $CFG;
661
662 if (! isset($dataobject->id) ) {
663 return false;
664 }
665
666 // Determine all the fields in the table
667 if (!$columns = $db->MetaColumns("$CFG->prefix$table")) {
668 return false;
669 }
670 $data = (array)$dataobject;
671
672 // Pull out data matching these fields
673 foreach ($columns as $column) {
92230499 674 if ($column->name <> "id" and isset($data[$column->name]) ) {
df28d6c5 675 $ddd[$column->name] = $data[$column->name];
676 }
677 }
678
679 // Construct SQL queries
680 $numddd = count($ddd);
681 $count = 0;
682 $update = "";
683
684 foreach ($ddd as $key => $value) {
685 $count++;
686 $update .= "$key = '$value'";
687 if ($count < $numddd) {
688 $update .= ", ";
689 }
690 }
691
692 if ($rs = $db->Execute("UPDATE $CFG->prefix$table SET $update WHERE id = '$dataobject->id'")) {
693 return true;
694 } else {
695 return false;
696 }
697}
698
699
df28d6c5 700
701
702/// USER DATABASE ////////////////////////////////////////////////
703
704function get_user_info_from_db($field, $value) {
705/// Get a complete user record, which includes all the info
706/// in the user record, as well as membership information
707/// Suitable for setting as $USER session cookie.
708
18496c59 709 if (!$field or !$value) {
df28d6c5 710 return false;
df28d6c5 711 }
712
18496c59 713 if (! $user = get_record_select("user", "$field = '$value' AND deleted <> '1'")) {
714 return false;
715 }
df28d6c5 716
18496c59 717 // Add membership information
df28d6c5 718
18496c59 719 if ($site = get_site()) { // Everyone is always a member of the top course
720 $user->student[$site->id] = true;
721 }
df28d6c5 722
f53af941 723 if ($students = get_records("user_students", "userid", $user->id)) {
724 foreach ($students as $student) {
725 $user->student[$student->course] = true;
b86fc0e2 726 $user->zoom[$student->course] = $student->zoom;
f53af941 727 }
18496c59 728 }
df28d6c5 729
f53af941 730 if ($teachers = get_records("user_teachers", "userid", $user->id)) {
731 foreach ($teachers as $teacher) {
732 $user->teacher[$teacher->course] = true;
733 }
18496c59 734 }
df28d6c5 735
f53af941 736 if ($admins = get_records("user_admins", "userid", $user->id)) {
737 foreach ($admins as $admin) {
738 $user->admin = true;
739 break;
740 }
df28d6c5 741 }
18496c59 742
b86fc0e2 743 if ($displays = get_records("course_display", "userid", $user->id)) {
744 foreach ($displays as $display) {
745 $user->display[$display->course] = $display->display;
746 }
747 }
748
18496c59 749 return $user;
df28d6c5 750}
751
752function update_user_in_db() {
753/// Updates user record to record their last access
754
755 global $db, $USER, $REMOTE_ADDR, $CFG;
756
757 if (!isset($USER->id))
758 return false;
759
760 $timenow = time();
761 if ($db->Execute("UPDATE {$CFG->prefix}user SET lastIP='$REMOTE_ADDR', lastaccess='$timenow'
762 WHERE id = '$USER->id' ")) {
763 return true;
764 } else {
765 return false;
766 }
767}
768
769
770function adminlogin($username, $md5password) {
771/// Does this username and password specify a valid admin user?
772
773 global $CFG;
774
9fa49e22 775 return record_exists_sql("SELECT u.id
776 FROM {$CFG->prefix}user u,
777 {$CFG->prefix}user_admins a
ebc3bd2b 778 WHERE u.id = a.userid
df28d6c5 779 AND u.username = '$username'
780 AND u.password = '$md5password'");
781}
782
783
784function get_site () {
785/// Returns $course object of the top-level site.
786
787 if ( $course = get_record("course", "category", 0)) {
788 return $course;
789 } else {
790 return false;
791 }
792}
793
9fa49e22 794
795function get_courses($category=0, $sort="fullname ASC") {
796/// Returns list of courses
797
798 if ($category > 0) { // Return all courses in one category
799 return get_records("course", "category", $category, $sort);
800
801 } else if ($category < 0) { // Return all courses, even the site
802 return get_records("course", "", "", $sort);
803
804 } else { // Return all courses, except site
805 return get_records_select("course", "category > 0", $sort);
806 }
807}
808
809function get_categories() {
810 return get_records("course_categories", "", "", "name");
811}
812
813
814function get_guest() {
815 return get_user_info_from_db("username", "guest");
816}
817
818
df28d6c5 819function get_admin () {
820/// Returns $user object of the main admin user
821
822 global $CFG;
823
824 if ( $admins = get_admins() ) {
825 foreach ($admins as $admin) {
826 return $admin; // ie the first one
827 }
828 } else {
829 return false;
830 }
831}
832
833function get_admins() {
834/// Returns list of all admins
835
836 global $CFG;
837
ebc3bd2b 838 return get_records_sql("SELECT u.*
839 FROM {$CFG->prefix}user u,
840 {$CFG->prefix}user_admins a
841 WHERE a.userid = u.id
842 ORDER BY u.id ASC");
df28d6c5 843}
844
1924074c 845function get_creators() {
846/// Returns list of all admins
847
848 global $CFG;
849
850 return get_records_sql("SELECT u.*
851 FROM {$CFG->prefix}user u,
852 {$CFG->prefix}user_coursecreators a
853 WHERE a.userid = u.id
854 ORDER BY u.id ASC");
855}
df28d6c5 856
857function get_teacher($courseid) {
858/// Returns $user object of the main teacher for a course
859
860 global $CFG;
861
862 if ( $teachers = get_course_teachers($courseid, "t.authority ASC")) {
863 foreach ($teachers as $teacher) {
864 if ($teacher->authority) {
865 return $teacher; // the highest authority teacher
866 }
867 }
868 } else {
869 return false;
870 }
871}
872
2d0b30a0 873function get_course_students($courseid, $sort="u.lastaccess DESC") {
df28d6c5 874/// Returns list of all students in this course
adaf3928 875/// if courseid = 0 then return ALL students in all courses
df28d6c5 876
877 global $CFG;
878
9d6f5d42 879 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay,
880 u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture
881 FROM {$CFG->prefix}user u,
882 {$CFG->prefix}user_students s
2d0b30a0 883 WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0'
df28d6c5 884 ORDER BY $sort");
885}
886
2d0b30a0 887function get_course_teachers($courseid, $sort="t.authority ASC") {
df28d6c5 888/// Returns list of all teachers in this course
adaf3928 889/// if courseid = 0 then return ALL teachers in all courses
df28d6c5 890
891 global $CFG;
892
893 return get_records_sql("SELECT u.*,t.authority,t.role FROM {$CFG->prefix}user u, {$CFG->prefix}user_teachers t
2d0b30a0 894 WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0'
df28d6c5 895 ORDER BY $sort");
896}
897
898function get_course_users($courseid, $sort="u.lastaccess DESC") {
353d0338 899/// Returns all the users of a course: students and teachers
900/// If the "course" is actually the site, then return all site users.
901
902 $site = get_site();
903
904 if ($courseid == $site->id) {
905 return get_site_users($sort);
906 }
907
908 /// Using this method because the single SQL just would not always work!
df28d6c5 909
910 $teachers = get_course_teachers($courseid, $sort);
911 $students = get_course_students($courseid, $sort);
912
913 if ($teachers and $students) {
914 return array_merge($teachers, $students);
915 } else if ($teachers) {
916 return $teachers;
917 } else {
918 return $students;
919 }
920
353d0338 921 /// Why wouldn't this work?
922 /// return get_records_sql("SELECT u.* FROM user u, user_students s, user_teachers t
923 /// WHERE (s.course = '$courseid' AND s.userid = u.id) OR
924 /// (t.course = '$courseid' AND t.userid = u.id)
925 /// ORDER BY $sort");
df28d6c5 926}
927
2d0b30a0 928function get_site_users($sort="u.lastaccess DESC") {
929/// Returns a list of all active users who are enrolled
930/// or teaching in courses on this server
931
353d0338 932 global $CFG, $db;
933
934 //$db->debug = true;
2d0b30a0 935
bbf9b162 936 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay,
937 u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture
938 FROM {$CFG->prefix}user u,
939 {$CFG->prefix}user_students s,
353d0338 940 {$CFG->prefix}user_teachers t,
941 {$CFG->prefix}user_coursecreators c,
942 {$CFG->prefix}user_admins a
bbf9b162 943 WHERE s.userid = u.id
944 OR t.userid = u.id
353d0338 945 OR a.userid = u.id
946 OR c.userid = u.id
947 GROUP BY u.id
948 ORDER BY $sort ");
2d0b30a0 949}
950
9fa49e22 951
5a741655 952function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sort="firstname ASC") {
953/// Returns a subset of users,
954/// $get - if false then only a count of the records is returned
955/// $search is a simple string to search for
956/// $confirmed is a switch to allow/disallow unconfirmed users
957/// $exceptions is a list of IDs to ignore, eg 2,4,5,8,9,10
958/// $sort is a sorting criteria to use
e384fb7b 959
960 if ($search) {
961 $search = " AND (firstname LIKE '%$search%'
962 OR lastname LIKE '%$search%'
963 OR email LIKE '%$search%') ";
964 }
965
5a741655 966 if ($confirmed) {
967 $confirmed = " AND confirmed = '1' ";
968 }
969
970 if ($exceptions) {
971 $exceptions = " AND id NOT IN ($exceptions) ";
972 }
973
974 if ($sort and $get) {
975 $sort = " ORDER BY $sort ";
976 } else {
977 $sort = "";
978 }
979
980 if ($get) {
981 return get_records_select("user", "username <> 'guest' AND deleted = 0 $search $confirmed $exceptions $sort");
982 } else {
983 return count_records_select("user", "username <> 'guest' AND deleted = 0 $search $confirmed $exceptions $sort");
984 }
9fa49e22 985}
986
5a741655 987
c750592a 988function get_users_listing($sort, $dir="ASC", $page=1, $recordsperpage=20, $search="") {
9fa49e22 989 global $CFG;
31fefa63 990
c2a96d6b 991 switch ($CFG->dbtype) {
992 case "mysql":
993 $limit = "LIMIT $page,$recordsperpage";
994 break;
995 case "postgres7":
a918234e 996 $limit = "LIMIT $recordsperpage OFFSET ".($page);
c2a96d6b 997 break;
998 default:
999 $limit = "LIMIT $recordsperpage,$page";
31fefa63 1000 }
c2a96d6b 1001
c750592a 1002 if ($search) {
1003 $search = " AND (firstname LIKE '%$search%'
1004 OR lastname LIKE '%$search%'
1005 OR email LIKE '%$search%') ";
1006 }
1007
9fa49e22 1008 return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess
1009 FROM {$CFG->prefix}user
1010 WHERE username <> 'guest'
c750592a 1011 AND deleted <> '1' $search
31fefa63 1012 ORDER BY $sort $dir $limit");
9fa49e22 1013
1014}
1015
1016function get_users_confirmed() {
1017 global $CFG;
1018 return get_records_sql("SELECT *
1019 FROM {$CFG->prefix}user
1020 WHERE confirmed = 1
1021 AND deleted = 0
1022 AND username <> 'guest'
1023 AND username <> 'changeme'");
1024}
1025
1026
99988d1a 1027function get_users_unconfirmed($cutofftime=2000000000) {
9fa49e22 1028 global $CFG;
1029 return get_records_sql("SELECT *
1030 FROM {$CFG->prefix}user
1031 WHERE confirmed = 0
1032 AND firstaccess > 0
1033 AND firstaccess < '$cutofftime'");
1034}
1035
1036
1037function get_users_longtimenosee($cutofftime) {
1038 global $CFG;
31fefa63 1039
1040 $db->debug = true;
9fa49e22 1041 return get_records_sql("SELECT u.*
1042 FROM {$CFG->prefix}user u,
1043 {$CFG->prefix}user_students s
31fefa63 1044 WHERE u.lastaccess > '0'
1045 AND u.lastaccess < '$cutofftime'
97485d07 1046 AND u.id = s.userid
1047 GROUP BY u.id");
9fa49e22 1048}
1049
1050
df28d6c5 1051/// MODULE FUNCTIONS /////////////////////////////////////////////////
1052
9fa49e22 1053function get_course_mods($courseid) {
1054/// Just gets a raw list of all modules in a course
1055 global $CFG;
1056
7acaa63d 1057 return get_records_sql("SELECT cm.*, m.name as modname
1058 FROM {$CFG->prefix}modules m,
1059 {$CFG->prefix}course_modules cm
9fa49e22 1060 WHERE cm.course = '$courseid'
1061 AND cm.deleted = '0'
1062 AND cm.module = m.id ");
1063}
1064
df28d6c5 1065function get_coursemodule_from_instance($modulename, $instance, $courseid) {
1066/// Given an instance of a module, finds the coursemodule description
1067
1068 global $CFG;
1069
1070 return get_record_sql("SELECT cm.*, m.name
7acaa63d 1071 FROM {$CFG->prefix}course_modules cm,
1072 {$CFG->prefix}modules md,
1073 {$CFG->prefix}$modulename m
df28d6c5 1074 WHERE cm.course = '$courseid' AND
1075 cm.deleted = '0' AND
1076 cm.instance = m.id AND
1077 md.name = '$modulename' AND
1078 md.id = cm.module AND
1079 m.id = '$instance'");
1080
1081}
1082
1083function get_all_instances_in_course($modulename, $courseid, $sort="cw.section") {
1084/// Returns an array of all the active instances of a particular
1085/// module in a given course. Returns false on any errors.
1086
1087 global $CFG;
1088
1acfbce5 1089 // Hide non-visible instances from students
1090 if (isteacher($courseid)) {
1091 $showvisible = "";
1092 } else {
1093 $showvisible = "AND cm.visible = '1'";
1094 }
1095
7cfb8347 1096 return get_records_sql("SELECT m.*,cw.section,cm.id as coursemodule,cm.visible as visible
7acaa63d 1097 FROM {$CFG->prefix}course_modules cm,
1098 {$CFG->prefix}course_sections cw,
1099 {$CFG->prefix}modules md,
1100 {$CFG->prefix}$modulename m
df28d6c5 1101 WHERE cm.course = '$courseid' AND
1102 cm.instance = m.id AND
1103 cm.deleted = '0' AND
1104 cm.section = cw.id AND
1105 md.name = '$modulename' AND
1acfbce5 1106 md.id = cm.module $showvisible
df28d6c5 1107 ORDER BY $sort");
1108
1109}
1110
9fa49e22 1111
580f2fbc 1112function instance_is_visible($moduletype, $module) {
1113/// Given a valid module object with info about the id and course,
1114/// and the module's type (eg "forum") returns whether the object
1115/// is visible or not
1116
1117 global $CFG;
1118
86e6076b 1119 if ($records = get_records_sql("SELECT cm.instance, cm.visible
580f2fbc 1120 FROM {$CFG->prefix}course_modules cm,
580f2fbc 1121 {$CFG->prefix}modules m
1122 WHERE cm.course = '$module->course' AND
1123 cm.module = m.id AND
1124 m.name = '$moduletype' AND
86e6076b 1125 cm.instance = '$module->id'")) {
580f2fbc 1126
1127 foreach ($records as $record) { // there should only be one - use the first one
1128 return $record->visible;
1129 }
1130 }
1131
1132 return true; // visible by default!
1133}
1134
a3fb1c45 1135
1136
1137
9fa49e22 1138/// LOG FUNCTIONS /////////////////////////////////////////////////////
1139
1140
1141function add_to_log($course, $module, $action, $url="", $info="") {
1142/// Add an entry to the log table. These are "action" focussed rather
1143/// than web server hits, and provide a way to easily reconstruct what
1144/// any particular student has been doing.
1145///
1146/// course = the course id
1147/// module = forum, journal, resource, course, user etc
1148/// action = view, edit, post (often but not always the same as the file.php)
1149/// url = the file and parameters used to see the results of the action
1150/// info = additional description information
1151
31fefa63 1152 global $db, $CFG, $USER, $REMOTE_ADDR;
9fa49e22 1153
1154 if (isset($USER->realuser)) { // Don't log
1155 return;
1156 }
1157
61e96406 1158 $userid = empty($USER->id) ? "" : $USER->id;
1159
9fa49e22 1160 $timenow = time();
1161 $info = addslashes($info);
1162
31fefa63 1163 $result = $db->Execute("INSERT INTO {$CFG->prefix}log (time,
ebc3bd2b 1164 userid,
1165 course,
1166 ip,
1167 module,
1168 action,
1169 url,
1170 info)
1171 VALUES ('$timenow',
61e96406 1172 '$userid',
ebc3bd2b 1173 '$course',
1174 '$REMOTE_ADDR',
1175 '$module',
1176 '$action',
1177 '$url',
1178 '$info')");
1179
ce78926d 1180 if (!$result and ($CFG->debug > 7)) {
9fa49e22 1181 echo "<P>Error: Could not insert a new entry to the Moodle log</P>"; // Don't throw an error
1182 }
1183}
1184
1185
1186function get_logs($select, $order) {
1187 global $CFG;
1188
1189 return get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture
1190 FROM {$CFG->prefix}log l,
1191 {$CFG->prefix}user u
1192 $select $order");
1193}
1194
1195function get_logs_usercourse($userid, $courseid, $coursestart) {
1196 global $CFG;
1197
1198 return get_records_sql("SELECT floor((`time` - $coursestart)/86400) as day, count(*) as num
1199 FROM {$CFG->prefix}log
ebc3bd2b 1200 WHERE userid = '$userid'
9fa49e22 1201 AND course = '$courseid'
1202 AND `time` > '$coursestart'
1203 GROUP BY day ");
1204}
1205
1206function get_logs_userday($userid, $courseid, $daystart) {
1207 global $CFG;
1208
1209 return get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour, count(*) as num
1210 FROM {$CFG->prefix}log
ebc3bd2b 1211 WHERE userid = '$userid'
9fa49e22 1212 AND course = '$courseid'
1213 AND `time` > '$daystart'
1214 GROUP BY hour ");
1215}
1216
a3fb1c45 1217/// GENERAL HELPFUL THINGS ///////////////////////////////////
1218
1219function print_object($object) {
1220/// Mostly just for debugging
1221
2b051f1c 1222 echo "<PRE>";
1223 print_r($object);
1224 echo "</PRE>";
a3fb1c45 1225}
1226
1227
9fa49e22 1228
9d5b689c 1229// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
df28d6c5 1230?>