]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-14442, Import all records of a database activity that has been exported as CSV...
authordongsheng <dongsheng>
Tue, 13 May 2008 02:56:51 +0000 (02:56 +0000)
committerdongsheng <dongsheng>
Tue, 13 May 2008 02:56:51 +0000 (02:56 +0000)
mod/data/import.php

index a5ac877cab46affa325347149046ab3acd14e7ae..8e24d1bcbd6b1b65ee7d2f3c58f5565c34f7ed8e 100755 (executable)
                 if ($recordid = data_add_record($data, 0)) {  // add instance to data_record
                     $fields = get_records('data_fields', 'dataid', $data->id, '', 'name, id, type');
 
-                    // do a manual round of inserting, to make sure even empty contents get stored
+                    // Insert new data_content fields with NULL contents:
                     foreach ($fields as $field) {
+                        $content = new object();
                         $content->recordid = $recordid;
                         $content->fieldid = $field->id;
                         insert_record('data_content', $content);
                     }
-                    // for each field in the add form, add it to the data_content.
+                    // Fill data_content with the values imported from the CSV file:
                     foreach ($record as $key => $value) {
                         $name = $fieldnames[$key];
                         $field = $fields[$name];
-                        require_once($CFG->dirroot.'/mod/data/field/'.$field->type.'/field.class.php');
-                        $newfield = 'data_field_'.$field->type;
-                        $currentfield = new $newfield($field->id);
-
-                        $currentfield->update_content($recordid, $value, $name);
+                        $content = new object();
+                        $content->fieldid = $field->id;
+                        $content->recordid = $recordid;
+                        // for now, only for "latlong" and "url" fields, but that should better be looked up from
+                        // $CFG->dirroot . '/mod/data/field/' . $field->type . '/field.class.php'
+                        // once there is stored how many contents the field can have. 
+                        if (preg_match("/^(latlong|url)$/", $field->type)) {
+                            $values = explode(" ", clean_param($value, PARAM_NOTAGS), 2);
+                            $content->content  = $values[0];
+                            $content->content1 = $values[1];
+                        } else {
+                            $content->content = clean_param($value, PARAM_NOTAGS);
+                        }
+                        $oldcontent = get_record('data_content', 'fieldid', $field->id, 'recordid', $recordid);
+                        $content->id = $oldcontent->id;
+                        update_record('data_content', $content);
                     }
                     $recordsadded++;
+                    print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
                 }
-            } // End foreach
-        } // End else
-    }//sun without love motivo atillas
+            }
+        }
+    }
 
     if ($recordsadded > 0) {
         notify($recordsadded. ' '. get_string('recordssaved', 'data'));