Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / mod / data / import.php
blob3c6553141df103f0c53eb6aa4edeb02bcaa7bd0a
1 <?php // $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 // //
4 // NOTICE OF COPYRIGHT //
5 // //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
8 // //
9 // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
10 // //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
15 // //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
20 // //
21 // http://www.gnu.org/copyleft/gpl.html //
22 // //
23 ///////////////////////////////////////////////////////////////////////////
25 require_once('../../config.php');
26 require_once('lib.php');
27 require_once($CFG->libdir.'/uploadlib.php');
29 require_login();
31 $id = optional_param('id', 0, PARAM_INT); // course module id
32 $d = optional_param('d', 0, PARAM_INT); // database id
33 $rid = optional_param('rid', 0, PARAM_INT); // record id
34 $fielddelimiter = optional_param('fielddelimiter', ',', PARAM_CLEANHTML); // characters used as field delimiters for csv file import
35 $fieldenclosure = optional_param('fieldenclosure', '', PARAM_CLEANHTML); // characters used as record delimiters for csv file import
37 if ($id) {
38 if (! $cm = get_coursemodule_from_id('data', $id)) {
39 error('Course Module ID was incorrect');
41 if (! $course = get_record('course', 'id', $cm->course)) {
42 error('Course is misconfigured');
44 if (! $data = get_record('data', 'id', $cm->instance)) {
45 error('Course module is incorrect');
48 } else {
49 if (! $data = get_record('data', 'id', $d)) {
50 error('Data ID is incorrect');
52 if (! $course = get_record('course', 'id', $data->course)) {
53 error('Course is misconfigured');
55 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
56 error('Course Module ID was incorrect');
60 require_login($course, false, $cm);
62 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
63 require_capability('mod/data:manageentries', $context);
65 /// Print the page header
66 $strdata = get_string('modulenameplural','data');
68 $navigation = build_navigation('', $cm);
69 print_header_simple($data->name, "", $navigation, "", "", true, "", navmenu($course));
70 print_heading(format_string($data->name));
72 /// Groups needed for Add entry tab
73 $currentgroup = groups_get_activity_group($cm);
74 $groupmode = groups_get_activity_groupmode($cm);
76 /// Print the tabs
77 $currenttab = 'add';
78 include('tabs.php');
81 $um = new upload_manager('recordsfile', false, false, null, false, 0);
83 if ($um->preprocess_files() && confirm_sesskey()) {
84 $filename = $um->files['recordsfile']['tmp_name'];
86 // Large files are likely to take their time and memory. Let PHP know
87 // that we'll take longer, and that the process should be recycled soon
88 // to free up memory.
89 @set_time_limit(0);
90 @raise_memory_limit("96M");
91 if (function_exists('apache_child_terminate')) {
92 @apache_child_terminate();
95 //Fix mac/dos newlines
96 $text = my_file_get_contents($filename);
97 $text = preg_replace('!\r\n?!',"\n",$text);
98 $fp = fopen($filename, "w");
99 fwrite($fp, $text);
100 fclose($fp);
102 $recordsadded = 0;
104 if (!$records = data_get_records_csv($filename, $fielddelimiter, $fieldenclosure)) {
105 print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
106 } else {
107 //$db->debug = true;
108 $fieldnames = array_shift($records);
110 // check the fieldnames are valid
111 $fields = get_records('data_fields', 'dataid', $data->id, '', 'name, id, type');
112 $errorfield = '';
113 foreach ($fieldnames as $name) {
114 if (!isset($fields[$name])) {
115 $errorfield .= "'$name' ";
119 if (!empty($errorfield)) {
120 print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield);
123 foreach ($records as $record) {
124 if ($recordid = data_add_record($data, 0)) { // add instance to data_record
125 $fields = get_records('data_fields', 'dataid', $data->id, '', 'name, id, type');
127 // Insert new data_content fields with NULL contents:
128 foreach ($fields as $field) {
129 $content = new object();
130 $content->recordid = $recordid;
131 $content->fieldid = $field->id;
132 if (! insert_record('data_content', $content)) {
133 print_error('cannotinsertrecord', '', '', $recordid);
136 // Fill data_content with the values imported from the CSV file:
137 foreach ($record as $key => $value) {
138 $name = $fieldnames[$key];
139 $field = $fields[$name];
140 $content = new object();
141 $content->fieldid = $field->id;
142 $content->recordid = $recordid;
143 if ($field->type == 'textarea') {
144 // the only field type where HTML is possible
145 $value = clean_param($value, PARAM_CLEANHTML);
146 } else {
147 // remove potential HTML:
148 $patterns[] = '/</';
149 $replacements[] = '&lt;';
150 $patterns[] = '/>/';
151 $replacements[] = '&gt;';
152 $value = preg_replace($patterns, $replacements, $value);
154 $value = addslashes($value);
155 // for now, only for "latlong" and "url" fields, but that should better be looked up from
156 // $CFG->dirroot . '/mod/data/field/' . $field->type . '/field.class.php'
157 // once there is stored how many contents the field can have.
158 if (preg_match("/^(latlong|url)$/", $field->type)) {
159 $values = explode(" ", $value, 2);
160 $content->content = $values[0];
161 $content->content1 = $values[1];
162 } else {
163 $content->content = $value;
165 $oldcontent = get_record('data_content', 'fieldid', $field->id, 'recordid', $recordid);
166 $content->id = $oldcontent->id;
167 if (! update_record('data_content', $content)) {
168 print_error('cannotupdaterecord', '', '', $recordid);
171 $recordsadded++;
172 print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
178 if ($recordsadded > 0) {
179 notify($recordsadded. ' '. get_string('recordssaved', 'data'));
180 } else {
181 notify(get_string('recordsnotsaved', 'data'));
183 echo '<p />';
186 /// Finish the page
187 print_footer($course);
192 function my_file_get_contents($filename, $use_include_path = 0) {
193 /// Returns the file as one big long string
195 $data = "";
196 $file = @fopen($filename, "rb", $use_include_path);
197 if ($file) {
198 while (!feof($file)) {
199 $data .= fread($file, 1024);
201 fclose($file);
203 return $data;
208 // Read the records from the given file.
209 // Perform a simple field count check for each record.
210 function data_get_records_csv($filename, $fielddelimiter=',', $fieldenclosure="\n") {
211 global $db;
214 if (empty($fielddelimiter)) {
215 $fielddelimiter = ',';
217 if (empty($fieldenclosure)) {
218 $fieldenclosure = "\n";
221 if (!$fp = fopen($filename, "r")) {
222 error('get_records_csv failed to open '.$filename);
224 $fieldnames = array();
225 $rows = array();
227 $fieldnames = fgetcsv($fp, 4096, $fielddelimiter, $fieldenclosure);
229 if (empty($fieldnames)) {
230 fclose($fp);
231 return false;
233 $rows[] = $fieldnames;
235 while (($data = fgetcsv($fp, 4096, $fielddelimiter, $fieldenclosure)) !== false) {
236 if (count($data) > count($fieldnames)) {
237 // For any given record, we can't have more data entities than the number of fields.
238 fclose($fp);
239 return false;
241 $rows[] = $data;
244 fclose($fp);
245 return $rows;