Permalink
Browse files

New get_record_select function (for completeness) and a completely

revamped update_user_in_db() function
  • Loading branch information...
1 parent cae7436 commit 18496c590bf3675875b3f2d5e14c33502da8584f moodler committed Dec 29, 2002
Showing with 36 additions and 33 deletions.
  1. +36 −33 lib/datalib.php
View
@@ -180,6 +180,20 @@ function get_record_sql($sql) {
}
}
+function get_record_select($table, $select="", $fields="*") {
+/// Gets one record from a table, as an object
+/// "select" is a fragment of SQL to define the selection criteria
+
+ global $CFG;
+
+ if ($select) {
+ $select = "WHERE $select";
+ }
+
+ return get_record_sql("SELECT $fields FROM $CFG->prefix$table $select");
+}
+
+
function get_records($table, $field="", $value="", $sort="", $fields="*") {
/// Get a number of records as an array of objects
/// Can optionally be sorted eg "time ASC" or "time DESC"
@@ -510,48 +524,37 @@ function get_user_info_from_db($field, $value) {
/// in the user record, as well as membership information
/// Suitable for setting as $USER session cookie.
- global $db, $CFG;
-
- if (!$field || !$value)
+ if (!$field or !$value) {
return false;
-
- if (! $result = $db->Execute("SELECT * FROM {$CFG->prefix}user WHERE $field = '$value' AND deleted <> '1'")) {
- error("Could not find any active users!");
}
- if ( $result->RecordCount() == 1 ) {
- $user = (object)$result->fields;
-
- $rs = $db->Execute("SELECT course FROM {$CFG->prefix}user_students WHERE userid = '$user->id' ");
- while (!$rs->EOF) {
- $course = $rs->fields["course"];
- $user->student["$course"] = true;
- $rs->MoveNext();
- }
+ if (! $user = get_record_select("user", "$field = '$value' AND deleted <> '1'")) {
+ return false;
+ }
- $rs = $db->Execute("SELECT course FROM {$CFG->prefix}user_teachers WHERE userid = '$user->id' ");
- while (!$rs->EOF) {
- $course = $rs->fields["course"];
- $user->teacher["$course"] = true;
- $rs->MoveNext();
- }
+ // Add membership information
- $rs = $db->Execute("SELECT * FROM {$CFG->prefix}user_admins WHERE userid = '$user->id' ");
- while (!$rs->EOF) {
- $user->admin = true;
- $rs->MoveNext();
- }
+ if ($site = get_site()) { // Everyone is always a member of the top course
+ $user->student[$site->id] = true;
+ }
- if ($course = get_site()) {
- // Everyone is always a member of the top course
- $user->student["$course->id"] = true;
- }
+ $students = get_records("user_students", "userid", $user->id);
+ foreach ($students as $student) {
+ $user->student[$student->course] = true;
+ }
- return $user;
+ $teachers = get_records("user_teachers", "userid", $user->id);
+ foreach ($teachers as $teacher) {
+ $user->teacher[$teacher->course] = true;
+ }
- } else {
- return false;
+ $admins = get_records("user_admins", "userid", $user->id);
+ foreach ($admins as $admin) {
+ $user->admin = true;
+ break;
}
+
+ return $user;
}
function update_user_in_db() {

0 comments on commit 18496c5

Please sign in to comment.