Merge branch 'MDL-49032' of https://github.com/stronk7/moodle
[moodle.git] / mod / feedback / import.php
blob083ba2670a9012b4a8152446cdfc9f23b93e1104
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * prints the form to import items from xml-file
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package mod_feedback
25 require_once("../../config.php");
26 require_once("lib.php");
27 require_once('import_form.php');
29 // get parameters
30 $id = required_param('id', PARAM_INT);
31 $choosefile = optional_param('choosefile', false, PARAM_PATH);
32 $action = optional_param('action', false, PARAM_ALPHA);
34 $url = new moodle_url('/mod/feedback/import.php', array('id'=>$id));
35 if ($choosefile !== false) {
36 $url->param('choosefile', $choosefile);
38 if ($action !== false) {
39 $url->param('action', $action);
41 $PAGE->set_url($url);
43 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
44 print_error('invalidcoursemodule');
47 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
48 print_error('coursemisconf');
51 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
52 print_error('invalidcoursemodule');
55 $context = context_module::instance($cm->id);
57 require_login($course, true, $cm);
59 require_capability('mod/feedback:edititems', $context);
61 $mform = new feedback_import_form();
62 $newformdata = array('id'=>$id,
63 'deleteolditems'=>'1',
64 'action'=>'choosefile',
65 'confirmadd'=>'1',
66 'do_show'=>'templates');
67 $mform->set_data($newformdata);
68 $formdata = $mform->get_data();
70 if ($mform->is_cancelled()) {
71 redirect('edit.php?id='.$id.'&do_show=templates');
74 // process if we are happy file is ok
75 if ($choosefile) {
76 $xmlcontent = $mform->get_file_content('choosefile');
78 if (!$xmldata = feedback_load_xml_data($xmlcontent)) {
79 print_error('cannotloadxml', 'feedback', 'edit.php?id='.$id);
82 $importerror = feedback_import_loaded_data($xmldata, $feedback->id);
83 if ($importerror->stat == true) {
84 $url = 'edit.php?id='.$id.'&do_show=templates';
85 redirect($url, get_string('import_successfully', 'feedback'), 3);
86 exit;
91 /// Print the page header
92 $strfeedbacks = get_string("modulenameplural", "feedback");
93 $strfeedback = get_string("modulename", "feedback");
95 $PAGE->set_heading($course->fullname);
96 $PAGE->set_title($feedback->name);
97 echo $OUTPUT->header();
98 echo $OUTPUT->heading(format_string($feedback->name));
99 /// print the tabs
100 require('tabs.php');
102 /// Print the main part of the page
103 ///////////////////////////////////////////////////////////////////////////
104 ///////////////////////////////////////////////////////////////////////////
105 ///////////////////////////////////////////////////////////////////////////
106 echo $OUTPUT->heading(get_string('import_questions', 'feedback'), 3);
108 if (isset($importerror->msg) AND is_array($importerror->msg)) {
109 echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter');
110 foreach ($importerror->msg as $msg) {
111 echo $msg.'<br />';
113 echo $OUTPUT->box_end();
116 $mform->display();
118 echo $OUTPUT->footer();
120 function feedback_load_xml_data($xmlcontent) {
121 global $CFG;
122 require_once($CFG->dirroot.'/lib/xmlize.php');
124 if (!$xmlcontent = feedback_check_xml_utf8($xmlcontent)) {
125 return false;
128 $data = xmlize($xmlcontent, 1, 'UTF-8');
130 if (intval($data['FEEDBACK']['@']['VERSION']) != 200701) {
131 return false;
133 $data = $data['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
134 return $data;
137 function feedback_import_loaded_data(&$data, $feedbackid) {
138 global $CFG, $DB;
140 feedback_load_feedback_items();
142 $deleteolditems = optional_param('deleteolditems', 0, PARAM_INT);
144 $error = new stdClass();
145 $error->stat = true;
146 $error->msg = array();
148 if (!is_array($data)) {
149 $error->msg[] = get_string('data_is_not_an_array', 'feedback');
150 $error->stat = false;
151 return $error;
154 if ($deleteolditems) {
155 feedback_delete_all_items($feedbackid);
156 $position = 0;
157 } else {
158 //items will be add to the end of the existing items
159 $position = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
162 //depend items we are storing temporary in an mapping list array(new id => dependitem)
163 //we also store a mapping of all items array(oldid => newid)
164 $dependitemsmap = array();
165 $itembackup = array();
166 foreach ($data as $item) {
167 $position++;
168 //check the typ
169 $typ = $item['@']['TYPE'];
171 //check oldtypes first
172 switch($typ) {
173 case 'radio':
174 $typ = 'multichoice';
175 $oldtyp = 'radio';
176 break;
177 case 'dropdown':
178 $typ = 'multichoice';
179 $oldtyp = 'dropdown';
180 break;
181 case 'check':
182 $typ = 'multichoice';
183 $oldtyp = 'check';
184 break;
185 case 'radiorated':
186 $typ = 'multichoicerated';
187 $oldtyp = 'radiorated';
188 break;
189 case 'dropdownrated':
190 $typ = 'multichoicerated';
191 $oldtyp = 'dropdownrated';
192 break;
193 default:
194 $oldtyp = $typ;
197 $itemclass = 'feedback_item_'.$typ;
198 if ($typ != 'pagebreak' AND !class_exists($itemclass)) {
199 $error->stat = false;
200 $error->msg[] = 'type ('.$typ.') not found';
201 continue;
203 $itemobj = new $itemclass();
205 $newitem = new stdClass();
206 $newitem->feedback = $feedbackid;
207 $newitem->template = 0;
208 $newitem->typ = $typ;
209 $newitem->name = trim($item['#']['ITEMTEXT'][0]['#']);
210 $newitem->label = trim($item['#']['ITEMLABEL'][0]['#']);
211 $newitem->options = trim($item['#']['OPTIONS'][0]['#']);
212 $newitem->presentation = trim($item['#']['PRESENTATION'][0]['#']);
213 //check old types of radio, check, and so on
214 switch($oldtyp) {
215 case 'radio':
216 $newitem->presentation = 'r>>>>>'.$newitem->presentation;
217 break;
218 case 'dropdown':
219 $newitem->presentation = 'd>>>>>'.$newitem->presentation;
220 break;
221 case 'check':
222 $newitem->presentation = 'c>>>>>'.$newitem->presentation;
223 break;
224 case 'radiorated':
225 $newitem->presentation = 'r>>>>>'.$newitem->presentation;
226 break;
227 case 'dropdownrated':
228 $newitem->presentation = 'd>>>>>'.$newitem->presentation;
229 break;
232 if (isset($item['#']['DEPENDITEM'][0]['#'])) {
233 $newitem->dependitem = intval($item['#']['DEPENDITEM'][0]['#']);
234 } else {
235 $newitem->dependitem = 0;
237 if (isset($item['#']['DEPENDVALUE'][0]['#'])) {
238 $newitem->dependvalue = trim($item['#']['DEPENDVALUE'][0]['#']);
239 } else {
240 $newitem->dependvalue = '';
242 $olditemid = intval($item['#']['ITEMID'][0]['#']);
244 if ($typ != 'pagebreak') {
245 $newitem->hasvalue = $itemobj->get_hasvalue();
246 } else {
247 $newitem->hasvalue = 0;
249 $newitem->required = intval($item['@']['REQUIRED']);
250 $newitem->position = $position;
251 $newid = $DB->insert_record('feedback_item', $newitem);
253 $itembackup[$olditemid] = $newid;
254 if ($newitem->dependitem) {
255 $dependitemsmap[$newid] = $newitem->dependitem;
259 //remapping the dependency
260 foreach ($dependitemsmap as $key => $dependitem) {
261 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
262 $newitem->dependitem = $itembackup[$newitem->dependitem];
263 $DB->update_record('feedback_item', $newitem);
266 return $error;
269 function feedback_check_xml_utf8($text) {
270 //find the encoding
271 $searchpattern = '/^\<\?xml.+(encoding=\"([a-z0-9-]*)\").+\?\>/is';
273 if (!preg_match($searchpattern, $text, $match)) {
274 return false; //no xml-file
277 //$match[0] = \<\? xml ... \?\> (without \)
278 //$match[1] = encoding="...."
279 //$match[2] = ISO-8859-1 or so on
280 if (isset($match[0]) AND !isset($match[1])) { //no encoding given. we assume utf-8
281 return $text;
284 //encoding is given in $match[2]
285 if (isset($match[0]) AND isset($match[1]) AND isset($match[2])) {
286 $enc = $match[2];
287 return core_text::convert($text, $enc);