MDL-24751 fixed default memory limit when deciding to use apache_child_terminate...
[moodle.git] / mod / data / import.php
CommitLineData
6111b2b0 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is part of the Database module for Moodle
20 *
21 * @copyright 2005 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package mod-data
24 */
25
26require_once('../../config.php');
27require_once('lib.php');
bbdfd8ce
DC
28require_once($CFG->libdir.'/csvlib.class.php');
29require_once('import_form.php');
6111b2b0 30
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
36
a6855934 37$url = new moodle_url('/mod/data/import.php');
6111b2b0 38if ($rid !== 0) {
39 $url->param('rid', $rid);
40}
41if ($fielddelimiter !== '') {
42 $url->param('fielddelimiter', $fielddelimiter);
43}
44if ($fieldenclosure !== '') {
45 $url->param('fieldenclosure', $fieldenclosure);
46}
7cbc5919 47
6111b2b0 48if ($id) {
49 $url->param('id', $id);
50 $PAGE->set_url($url);
51 if (! $cm = get_coursemodule_from_id('data', $id)) {
52 print_error('invalidcoursemodule');
53 }
54 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
55 print_error('coursemisconf');
56 }
57 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
58 print_error('invalidcoursemodule');
59 }
60
61} else {
62 $url->param('d', $d);
63 $PAGE->set_url($url);
64 if (! $data = $DB->get_record('data', array('id'=>$d))) {
65 print_error('invalidid', 'data');
66 }
67 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
68 print_error('coursemisconf');
69 }
70 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
71 print_error('coursemisconf');
7cbc5919 72 }
6111b2b0 73}
aab98aaf 74
6111b2b0 75require_login($course, false, $cm);
bb5b7224 76
6111b2b0 77$context = get_context_instance(CONTEXT_MODULE, $cm->id);
78require_capability('mod/data:manageentries', $context);
bbdfd8ce 79$form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
7cbc5919 80
81/// Print the page header
91bc072a 82$PAGE->navbar->add(get_string('add', 'data'));
6111b2b0 83$PAGE->set_title($data->name);
91bc072a 84$PAGE->set_heading($course->fullname);
6111b2b0 85echo $OUTPUT->header();
86echo $OUTPUT->heading(format_string($data->name));
7cbc5919 87
cca1547e 88/// Groups needed for Add entry tab
6111b2b0 89$currentgroup = groups_get_activity_group($cm);
90$groupmode = groups_get_activity_groupmode($cm);
cca1547e 91
bbdfd8ce
DC
92if (!$formdata = $form->get_data()) {
93 /// Upload records section. Only for teachers and the admin.
94 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
95 require_once('import_form.php');
96 echo $OUTPUT->heading(get_string('uploadrecords', 'data'), 3);
97 $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
6bdfef5d 98 $formdata = new stdClass();
bbdfd8ce
DC
99 $formdata->d = $data->id;
100 $form->set_data($formdata);
101 $form->display();
102 echo $OUTPUT->box_end();
103 echo $OUTPUT->footer();
104 die;
105} else {
6111b2b0 106 // Large files are likely to take their time and memory. Let PHP know
107 // that we'll take longer, and that the process should be recycled soon
108 // to free up memory.
109 @set_time_limit(0);
110 @raise_memory_limit("96M");
aab98aaf 111
bbdfd8ce
DC
112 $iid = csv_import_reader::get_new_iid('moddata');
113 $cir = new csv_import_reader($iid, 'moddata');
6111b2b0 114
bbdfd8ce
DC
115 $readcount = $cir->load_csv_content($form->get_file_content('recordsfile'), $formdata->encoding, $formdata->fielddelimiter);
116 if (empty($readcount)) {
6111b2b0 117 print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
118 } else {
bbdfd8ce
DC
119 if (!$fieldnames = $cir->get_columns()) {
120 print_error('cannotreadtmpfile', 'error');
121 }
6111b2b0 122 // check the fieldnames are valid
123 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
124 $errorfield = '';
125 foreach ($fieldnames as $name) {
126 if (!isset($fields[$name])) {
127 $errorfield .= "'$name' ";
f24222e2 128 }
6111b2b0 129 }
130
131 if (!empty($errorfield)) {
132 print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield);
133 }
f24222e2 134
bbdfd8ce
DC
135 $cir->init();
136 $recordsadded = 0;
137 while ($record = $cir->next()) {
6111b2b0 138 if ($recordid = data_add_record($data, 0)) { // add instance to data_record
139 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
aab98aaf 140
6111b2b0 141 // Insert new data_content fields with NULL contents:
142 foreach ($fields as $field) {
39790bd8 143 $content = new stdClass();
6111b2b0 144 $content->recordid = $recordid;
145 $content->fieldid = $field->id;
146 $DB->insert_record('data_content', $content);
147 }
148 // Fill data_content with the values imported from the CSV file:
149 foreach ($record as $key => $value) {
150 $name = $fieldnames[$key];
151 $field = $fields[$name];
39790bd8 152 $content = new stdClass();
6111b2b0 153 $content->fieldid = $field->id;
154 $content->recordid = $recordid;
155 if ($field->type == 'textarea') {
156 // the only field type where HTML is possible
157 $value = clean_param($value, PARAM_CLEANHTML);
158 } else {
159 // remove potential HTML:
160 $patterns[] = '/</';
161 $replacements[] = '&lt;';
162 $patterns[] = '/>/';
163 $replacements[] = '&gt;';
164 $value = preg_replace($patterns, $replacements, $value);
e1791b72 165 }
6111b2b0 166 // for now, only for "latlong" and "url" fields, but that should better be looked up from
167 // $CFG->dirroot . '/mod/data/field/' . $field->type . '/field.class.php'
168 // once there is stored how many contents the field can have.
169 if (preg_match("/^(latlong|url)$/", $field->type)) {
170 $values = explode(" ", $value, 2);
171 $content->content = $values[0];
172 $content->content1 = $values[1];
173 } else {
174 $content->content = $value;
e1791b72 175 }
6111b2b0 176 $oldcontent = $DB->get_record('data_content', array('fieldid'=>$field->id, 'recordid'=>$recordid));
177 $content->id = $oldcontent->id;
178 $DB->update_record('data_content', $content);
e1791b72 179 }
6111b2b0 180 $recordsadded++;
181 print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
f85c75b7 182 }
183 }
bbdfd8ce
DC
184 $cir->close();
185 $cir->cleanup(true);
f85c75b7 186 }
6111b2b0 187}
7cbc5919 188
6111b2b0 189if ($recordsadded > 0) {
bbdfd8ce 190 echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), '');
6111b2b0 191} else {
bbdfd8ce 192 echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
6111b2b0 193}
7cbc5919 194
bbdfd8ce 195echo $OUTPUT->continue_button('import.php?d='.$data->id);
7cbc5919 196
197/// Finish the page
6111b2b0 198echo $OUTPUT->footer();