]> git.mjollnir.org Git - moodle.git/commitdiff
Clean up of insert_record with some better error-checking
authormoodler <moodler>
Tue, 3 Feb 2004 15:18:41 +0000 (15:18 +0000)
committermoodler <moodler>
Tue, 3 Feb 2004 15:18:41 +0000 (15:18 +0000)
lib/datalib.php

index a2c1d4ab3b806c20b0445b16f14ecc7846d58994..4d4afa8d3d15c56d19c72d991e811dc79afc1eca 100644 (file)
@@ -749,28 +749,29 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') {
                                                                                                                 
     global $db, $CFG;
 
-    // Get empty record from table
-    $infosql = "SELECT * FROM $CFG->prefix$table WHERE $primarykey ='-1'";
-
-    // Execute the query and get the empty recordset
-    $rs = $db->Execute($infosql);
-                                                                                                                
-    // Convert data to array to hold the record data to insert
-    $record = (array)$dataobject;
+/// Execute a dummy query to get an empty recordset
+    if (!$rs = $db->Execute("SELECT * FROM $CFG->prefix$table WHERE $primarykey ='-1'")) {
+        return false;
+    }
                                                                                                                 
-    //Get insertsql from adodb
-    $insertSQL = $db->GetInsertSQL($rs, $record, true);
+/// Get the correct SQL from adoDB
+    if (!$insertSQL = $db->GetInsertSQL($rs, (array)$dataobject, true)) {
+        return false;
+    }
                                                                                                                 
-    if (! $rs = $db->Execute($insertSQL)) {
+/// Run the SQL statement
+    if (!$rs = $db->Execute($insertSQL)) {
         return false;
     }
                                                                                                                 
-    if (!$returnid) {   // Return ID is not needed so just finish here.
+/// If a return ID is not needed then just return true now
+    if (!$returnid) { 
         return true;
     }
 
+/// Find the return ID of the newly inserted record
     switch ($CFG->dbtype) {
-        case "postgres7":
+        case "postgres7":             // Just loves to be special
             $oid = $db->Insert_ID();
             if ($rs = $db->Execute("SELECT $primarykey FROM $CFG->prefix$table WHERE oid = $oid")) {
                 if ($rs->RecordCount() == 1) {
@@ -780,7 +781,7 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') {
             return false;
 
         default: 
-            return $db->Insert_ID();   // Should work on most databases, but not all!
+            return $db->Insert_ID();  // Should work on most databases, but not all!
     }
 }
 
@@ -1111,7 +1112,7 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
     }
 
     return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat,
-                            u.email, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone, s.timeaccess as lastaccess
+                            u.email, u.city, u.country, u.lastlogin, u.picture, u.department, uuuuuu.lang, u.timezone, s.timeaccess as lastaccess
                             FROM {$CFG->prefix}user u, 
                                  {$CFG->prefix}user_students s $groupmembers
                             WHERE $select