MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / data / import.php
blobf0c78790ff61c680ce034d7e18667f28e6c6bbfb
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:uploadentries', $context);
65 if (has_capability('mod/data:managetemplates', $context)) {
66 if (!count_records('data_fields','dataid',$data->id)) { // Brand new database!
67 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
71 if ($rid){ //editting a record, do you have access to edit this?
72 if (!has_capability('mod/data:manageentries', $context) or !data_isowner($rid) or !confirm_sesskey()){
73 error (get_string('noaccess','data'));
78 /// Print the page header
79 $strdata = get_string('modulenameplural','data');
81 $navlinks = array();
82 $navlinks[] = array('name' => $strdata, 'link' => "index.php?id=$course->id", 'type' => 'activity');
83 $navlinks[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
84 $navigation = build_navigation($navlinks);
86 print_header_simple($data->name, "", $navigation, "", "", true, "", navmenu($course));
87 print_heading(format_string($data->name));
89 /// Groups needed for Add entry tab
90 $currentgroup = groups_get_activity_group($cm);
91 $groupmode = groups_get_activity_groupmode($cm);
93 /// Print the tabs
94 $currenttab = 'add';
95 include('tabs.php');
98 $um = new upload_manager('recordsfile', false, false, null, false, 0);
100 if ($um->preprocess_files() && confirm_sesskey()) {
101 $filename = $um->files['recordsfile']['tmp_name'];
103 // Large files are likely to take their time and memory. Let PHP know
104 // that we'll take longer, and that the process should be recycled soon
105 // to free up memory.
106 @set_time_limit(0);
107 @raise_memory_limit("96M");
108 if (function_exists('apache_child_terminate')) {
109 @apache_child_terminate();
112 //Fix mac/dos newlines
113 $text = my_file_get_contents($filename);
114 $text = preg_replace('!\r\n?!',"\n",$text);
115 $fp = fopen($filename, "w");
116 fwrite($fp, $text);
117 fclose($fp);
119 $recordsadded = 0;
121 if (!$records = data_get_records_csv($filename, $fielddelimiter, $fieldenclosure)) {
122 error('get_records_csv failed to read data from the uploaded file. Please check file for field name typos and formatting errors.');
123 } else {
124 //$db->debug = true;
125 $fieldnames = array_shift($records);
127 foreach ($records as $record) {
128 if ($recordid = data_add_record($data, 0)) { // add instance to data_record
129 $fields = get_records('data_fields', 'dataid', $data->id, '', 'name, id, type');
131 // do a manual round of inserting, to make sure even empty contents get stored
132 foreach ($fields as $field) {
133 $content->recordid = $recordid;
134 $content->fieldid = $field->id;
135 insert_record('data_content', $content);
137 // for each field in the add form, add it to the data_content.
138 foreach ($record as $key => $value) {
139 $name = $fieldnames[$key];
140 $field = $fields[$name];
141 require_once($CFG->dirroot.'/mod/data/field/'.$field->type.'/field.class.php');
142 $newfield = 'data_field_'.$field->type;
143 $currentfield = new $newfield($field->id);
145 $currentfield->update_content($recordid, $value, $name);
147 $recordsadded++;
149 } // End foreach
150 } // End else
151 }//sun without love motivo atillas
153 if ($recordsadded > 0) {
154 notify($recordsadded. ' '. get_string('recordssaved', 'data'));
155 } else {
156 notify(get_string('recordsnotsaved', 'data'));
158 echo '<p />';
161 /// Finish the page
162 print_footer($course);
167 function my_file_get_contents($filename, $use_include_path = 0) {
168 /// Returns the file as one big long string
170 $data = "";
171 $file = @fopen($filename, "rb", $use_include_path);
172 if ($file) {
173 while (!feof($file)) {
174 $data .= fread($file, 1024);
176 fclose($file);
178 return $data;
183 // Read the records from the given file.
184 // Perform a simple field count check for each record.
185 function data_get_records_csv($filename, $fielddelimiter=',', $fieldenclosure="\n") {
186 global $db;
188 if (empty($fielddelimiter)) {
189 $fielddelimiter = ',';
191 if (empty($fieldenclosure)) {
192 $fieldenclosure = "\n";
195 if (!$fp = fopen($filename, "r")) {
196 error('get_records_csv failed to open '.$filename);
198 $fieldnames = array();
199 $rows = array();
201 $fieldnames = fgetcsv($fp, 4096, $fielddelimiter, $fieldenclosure);
203 if (empty($fieldnames)) {
204 fclose($fp);
205 return false;
207 $rows[] = $fieldnames;
209 while (($data = fgetcsv($fp, 4096, $fielddelimiter, $fieldenclosure)) !== false) {
210 if (count($data) > count($fieldnames)) {
211 // For any given record, we can't have more data entities than the number of fields.
212 fclose($fp);
213 return false;
215 $rows[] = $data;
218 fclose($fp);
219 return $rows;