MDL-57455 mod_data: Import and export tags
[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
002a2a44 23 * @package mod_data
6111b2b0 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);
dd8ab1fb 51 $cm = get_coursemodule_from_id('data', $id, 0, false, MUST_EXIST);
74df2951 52 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
dd8ab1fb 53 $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
6111b2b0 54
55} else {
56 $url->param('d', $d);
57 $PAGE->set_url($url);
dd8ab1fb 58 $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
74df2951 59 $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
dd8ab1fb 60 $cm = get_coursemodule_from_instance('data', $data->id, $course->id, false, MUST_EXIST);
6111b2b0 61}
aab98aaf 62
6111b2b0 63require_login($course, false, $cm);
bb5b7224 64
0b3b8c30 65$context = context_module::instance($cm->id);
6111b2b0 66require_capability('mod/data:manageentries', $context);
bbdfd8ce 67$form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
7cbc5919 68
69/// Print the page header
91bc072a 70$PAGE->navbar->add(get_string('add', 'data'));
6111b2b0 71$PAGE->set_title($data->name);
91bc072a 72$PAGE->set_heading($course->fullname);
dd8ab1fb 73navigation_node::override_active_url(new moodle_url('/mod/data/import.php', array('d' => $data->id)));
6111b2b0 74echo $OUTPUT->header();
dd8ab1fb 75echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'uploadrecords', 'mod_data');
7cbc5919 76
cca1547e 77/// Groups needed for Add entry tab
6111b2b0 78$currentgroup = groups_get_activity_group($cm);
79$groupmode = groups_get_activity_groupmode($cm);
cca1547e 80
bbdfd8ce
DC
81if (!$formdata = $form->get_data()) {
82 /// Upload records section. Only for teachers and the admin.
83 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
84 require_once('import_form.php');
bbdfd8ce 85 $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
6bdfef5d 86 $formdata = new stdClass();
bbdfd8ce
DC
87 $formdata->d = $data->id;
88 $form->set_data($formdata);
89 $form->display();
90 echo $OUTPUT->box_end();
91 echo $OUTPUT->footer();
92 die;
93} else {
6111b2b0 94 // Large files are likely to take their time and memory. Let PHP know
95 // that we'll take longer, and that the process should be recycled soon
96 // to free up memory.
3ef7279f 97 core_php_time_limit::raise();
346c5887 98 raise_memory_limit(MEMORY_EXTRA);
aab98aaf 99
bbdfd8ce
DC
100 $iid = csv_import_reader::get_new_iid('moddata');
101 $cir = new csv_import_reader($iid, 'moddata');
6111b2b0 102
b208ded6
PS
103 $filecontent = $form->get_file_content('recordsfile');
104 $readcount = $cir->load_csv_content($filecontent, $formdata->encoding, $formdata->fielddelimiter);
105 unset($filecontent);
bbdfd8ce 106 if (empty($readcount)) {
6111b2b0 107 print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
108 } else {
bbdfd8ce
DC
109 if (!$fieldnames = $cir->get_columns()) {
110 print_error('cannotreadtmpfile', 'error');
111 }
be8c6766 112 $fieldnames = array_flip($fieldnames);
6111b2b0 113 // check the fieldnames are valid
be8c6766
AH
114 $rawfields = $DB->get_records('data_fields', array('dataid' => $data->id), '', 'name, id, type');
115 $fields = array();
6111b2b0 116 $errorfield = '';
118d5698
MG
117 $safetoskipfields = array(get_string('user'), get_string('username'), get_string('email'),
118 get_string('timeadded', 'data'), get_string('timemodified', 'data'),
04f7b079 119 get_string('approved', 'data'), get_string('tags', 'data'));
be8c6766
AH
120 foreach ($fieldnames as $name => $id) {
121 if (!isset($rawfields[$name])) {
118d5698
MG
122 if (!in_array($name, $safetoskipfields)) {
123 $errorfield .= "'$name' ";
124 }
be8c6766
AH
125 } else {
126 $field = $rawfields[$name];
127 require_once("$CFG->dirroot/mod/data/field/$field->type/field.class.php");
128 $classname = 'data_field_' . $field->type;
129 $fields[$name] = new $classname($field, $data, $cm);
f24222e2 130 }
6111b2b0 131 }
132
133 if (!empty($errorfield)) {
134 print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield);
135 }
f24222e2 136
bbdfd8ce
DC
137 $cir->init();
138 $recordsadded = 0;
139 while ($record = $cir->next()) {
6111b2b0 140 if ($recordid = data_add_record($data, 0)) { // add instance to data_record
6111b2b0 141 foreach ($fields as $field) {
be8c6766
AH
142 $fieldid = $fieldnames[$field->field->name];
143 if (isset($record[$fieldid])) {
144 $value = $record[$fieldid];
6111b2b0 145 } else {
be8c6766 146 $value = '';
e1791b72 147 }
be8c6766
AH
148
149 if (method_exists($field, 'update_content_import')) {
150 $field->update_content_import($recordid, $value, 'field_' . $field->field->id);
6111b2b0 151 } else {
be8c6766
AH
152 $content = new stdClass();
153 $content->fieldid = $field->field->id;
6111b2b0 154 $content->content = $value;
be8c6766
AH
155 $content->recordid = $recordid;
156 $DB->insert_record('data_content', $content);
e1791b72 157 }
e1791b72 158 }
04f7b079
AH
159
160 if (core_tag_tag::is_enabled('mod_data', 'data_records') &&
161 isset($fieldnames[get_string('tags', 'data')])) {
162 $columnindex = $fieldnames[get_string('tags', 'data')];
163 $rawtags = $record[$columnindex];
164 $tags = explode(',', $rawtags);
165 foreach ($tags as $tag) {
166 $tag = trim($tag);
167 if (empty($tag)) {
168 continue;
169 }
170 core_tag_tag::add_item_tag('mod_data', 'data_records', $recordid, $context, $tag);
171 }
172 }
173
6111b2b0 174 $recordsadded++;
175 print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
f85c75b7 176 }
177 }
bbdfd8ce
DC
178 $cir->close();
179 $cir->cleanup(true);
f85c75b7 180 }
6111b2b0 181}
7cbc5919 182
6111b2b0 183if ($recordsadded > 0) {
bbdfd8ce 184 echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), '');
6111b2b0 185} else {
bbdfd8ce 186 echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
6111b2b0 187}
7cbc5919 188
bbdfd8ce 189echo $OUTPUT->continue_button('import.php?d='.$data->id);
7cbc5919 190
191/// Finish the page
6111b2b0 192echo $OUTPUT->footer();