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