MDL-29190 - XHTML not well formed in show_nonrespondents.php
[moodle.git] / mod / feedback / import.php
CommitLineData
84973212 1<?php
2
c70ad9f7 3/**
84973212 4 * prints the form to import items from xml-file
5 *
6 * @author Andreas Grabs
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package feedback
9 */
c70ad9f7 10
11 require_once("../../config.php");
12 require_once("lib.php");
30ec6bc9 13 require_once('import_form.php');
d4b1d58c 14
c70ad9f7 15 // get parameters
16 $id = required_param('id', PARAM_INT);
17 $choosefile = optional_param('choosefile', false, PARAM_PATH);
18 $action = optional_param('action', false, PARAM_ALPHA);
19
a6855934 20 $url = new moodle_url('/mod/feedback/import.php', array('id'=>$id));
84973212 21 if ($choosefile !== false) {
22 $url->param('choosefile', $choosefile);
23 }
24 if ($action !== false) {
25 $url->param('action', $action);
26 }
27 $PAGE->set_url($url);
28
b6f26001
AG
29 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
30 print_error('invalidcoursemodule');
31 }
32
33 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
34 print_error('coursemisconf');
35 }
36
37 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
38 print_error('invalidcoursemodule');
c70ad9f7 39 }
39790bd8 40
c987dbad
AG
41 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
42 print_error('badcontext');
43 }
c70ad9f7 44
6178c62a 45 require_login($course->id, true, $cm);
d4b1d58c 46
53ba55ec 47 require_capability('mod/feedback:edititems', $context);
d4b1d58c 48
4f37db41 49 $mform = new feedback_import_form();
30ec6bc9
AG
50 $newformdata = array('id'=>$id,
51 'deleteolditems'=>'1',
52 'action'=>'choosefile',
53 'confirmadd'=>'1',
54 'do_show'=>'templates');
55 $mform->set_data($newformdata);
56 $formdata = $mform->get_data();
57
58 if ($mform->is_cancelled()) {
59 redirect('edit.php?id='.$id.'&do_show=templates');
60 }
c70ad9f7 61
c70ad9f7 62
30ec6bc9
AG
63 // unset($filename);
64 // if ($action == 'choosefile' AND confirm_sesskey() ) {
65
66 ////file checks out ok
67 // $fileisgood = false;
68
69 ////work out if this is an uploaded file
70 ////or one from the filesarea.
71 // if ($choosefile) {
72 // $filename = "{$CFG->dataroot}/{$course->id}/{$choosefile}";
73 // }
74 // }
d4b1d58c 75
c70ad9f7 76 // process if we are happy file is ok
30ec6bc9
AG
77 if ($choosefile) {
78 $xmlcontent = $mform->get_file_content('choosefile');
79
80 if(!$xmldata = feedback_load_xml_data($xmlcontent)) {
b149ffb4 81 print_error('cannotloadxml', 'feedback', 'edit.php?id='.$id);
c70ad9f7 82 }
d4b1d58c 83
c70ad9f7 84 $importerror = feedback_import_loaded_data($xmldata, $feedback->id);
85 if($importerror->stat == true) {
86 redirect('edit.php?id='.$id.'&do_show=templates', get_string('import_successfully', 'feedback'), 3);
87 exit;
88 }
89 }
90
91
92 /// Print the page header
93 $strfeedbacks = get_string("modulenameplural", "feedback");
94 $strfeedback = get_string("modulename", "feedback");
39790bd8 95
9d092aa9 96 $PAGE->set_heading(format_string($course->fullname));
84f797db 97 $PAGE->set_title(format_string($feedback->name));
84f797db 98 echo $OUTPUT->header();
39790bd8 99
4f37db41
AG
100 /// print the tabs
101 include('tabs.php');
c70ad9f7 102
103 /// Print the main part of the page
104 ///////////////////////////////////////////////////////////////////////////
105 ///////////////////////////////////////////////////////////////////////////
106 ///////////////////////////////////////////////////////////////////////////
53b1f5da 107 echo $OUTPUT->heading(get_string('import_questions','feedback'));
d4b1d58c 108
c70ad9f7 109 if(isset($importerror->msg) AND is_array($importerror->msg)) {
d4b1d58c 110 echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter');
c70ad9f7 111 foreach($importerror->msg as $msg) {
112 echo $msg.'<br />';
113 }
d4b1d58c 114 echo $OUTPUT->box_end();
c70ad9f7 115 }
39790bd8 116
30ec6bc9 117 $mform->display();
39790bd8 118
4f37db41 119 // echo $OUTPUT->container_start('mdl-align');
30ec6bc9 120 // echo $OUTPUT->single_button(new moodle_url('edit.php', array('id'=>$id, 'do_show'=>'templates')), get_string('cancel'));
4f37db41 121 // echo $OUTPUT->container_end();
c6ea0876 122 echo $OUTPUT->footer();
d4b1d58c 123
30ec6bc9 124 function feedback_load_xml_data($xmlcontent) {
c70ad9f7 125 global $CFG;
126 require_once($CFG->dirroot.'/lib/xmlize.php');
d4b1d58c 127
30ec6bc9 128 if(!$xmlcontent = feedback_check_xml_utf8($xmlcontent)) return false;
d4b1d58c 129
30ec6bc9 130 $data = xmlize($xmlcontent, 1, 'UTF-8');
d4b1d58c 131
c70ad9f7 132 if(intval($data['FEEDBACK']['@']['VERSION']) != 200701) {
133 return false;
134 }
135 $data = $data['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
136 return $data;
137 }
d4b1d58c 138
c70ad9f7 139 function feedback_import_loaded_data(&$data, $feedbackid){
0085fff8 140 global $CFG, $DB;
d4b1d58c 141
30ec6bc9 142 feedback_load_feedback_items();
39790bd8 143
c70ad9f7 144 $deleteolditems = optional_param('deleteolditems', 0, PARAM_INT);
d4b1d58c 145
39790bd8 146 $error = new stdClass();
c70ad9f7 147 $error->stat = true;
148 $error->msg = array();
d4b1d58c 149
c70ad9f7 150 if(!is_array($data)) {
151 $error->msg[] = get_string('data_is_not_an_array', 'feedback');
152 $error->stat = false;
153 return $error;
154 }
d4b1d58c 155
c70ad9f7 156 if($deleteolditems) {
157 feedback_delete_all_items($feedbackid);
158 $position = 0;
159 } else {
160 //items will be add to the end of the existing items
0085fff8 161 $position = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
c70ad9f7 162 }
d4b1d58c 163
73043833
AG
164 //depend items we are storing temporary in an mapping list array(new id => dependitem)
165 //we also store a mapping of all items array(oldid => newid)
166 $dependitemsmap = array();
167 $itembackup = array();
c70ad9f7 168 foreach($data as $item) {
169 $position++;
170 //check the typ
171 $typ = $item['@']['TYPE'];
d4b1d58c 172
0f22be2e 173 //check oldtypes first
174 switch($typ) {
175 case 'radio':
176 $typ = 'multichoice';
177 $oldtyp = 'radio';
178 break;
179 case 'dropdown':
180 $typ = 'multichoice';
181 $oldtyp = 'dropdown';
182 break;
183 case 'check':
184 $typ = 'multichoice';
185 $oldtyp = 'check';
186 break;
187 case 'radiorated':
188 $typ = 'multichoicerated';
189 $oldtyp = 'radiorated';
190 break;
191 case 'dropdownrated':
192 $typ = 'multichoicerated';
193 $oldtyp = 'dropdownrated';
194 break;
195 default:
196 $oldtyp = $typ;
197 }
198
c70ad9f7 199 $itemclass = 'feedback_item_'.$typ;
200 if($typ != 'pagebreak' AND !class_exists($itemclass)) {
201 $error->stat = false;
202 $error->msg[] = 'type ('.$typ.') not found';
203 continue;
204 }
205 $itemobj = new $itemclass();
d4b1d58c 206
39790bd8 207 $newitem = new stdClass();
c70ad9f7 208 $newitem->feedback = $feedbackid;
209 $newitem->template = 0;
210 $newitem->typ = $typ;
211 $newitem->name = trim($item['#']['ITEMTEXT'][0]['#']);
30ec6bc9 212 $newitem->label = trim($item['#']['ITEMLABEL'][0]['#']);
6b0b5729 213 $newitem->options = trim($item['#']['OPTIONS'][0]['#']);
c70ad9f7 214 $newitem->presentation = trim($item['#']['PRESENTATION'][0]['#']);
0f22be2e 215 //check old types of radio, check, and so on
216 switch($oldtyp) {
217 case 'radio':
218 $newitem->presentation = 'r>>>>>'.$newitem->presentation;
219 break;
220 case 'dropdown':
221 $newitem->presentation = 'd>>>>>'.$newitem->presentation;
222 break;
223 case 'check':
224 $newitem->presentation = 'c>>>>>'.$newitem->presentation;
225 break;
226 case 'radiorated':
227 $newitem->presentation = 'r>>>>>'.$newitem->presentation;
228 break;
229 case 'dropdownrated':
230 $newitem->presentation = 'd>>>>>'.$newitem->presentation;
231 break;
232 }
39790bd8 233
73043833
AG
234 if(isset($item['#']['DEPENDITEM'][0]['#'])) {
235 $newitem->dependitem = intval($item['#']['DEPENDITEM'][0]['#']);
236 }else {
237 $newitem->dependitem = 0;
238 }
239 if(isset($item['#']['DEPENDVALUE'][0]['#'])) {
240 $newitem->dependvalue = trim($item['#']['DEPENDVALUE'][0]['#']);
241 }else {
242 $newitem->dependvalue = '';
243 }
244 $olditemid = intval($item['#']['ITEMID'][0]['#']);
39790bd8 245
c70ad9f7 246 if($typ != 'pagebreak') {
247 $newitem->hasvalue = $itemobj->get_hasvalue();
248 }else {
249 $newitem->hasvalue = 0;
250 }
251 $newitem->required = intval($item['@']['REQUIRED']);
252 $newitem->position = $position;
73043833 253 $newid = $DB->insert_record('feedback_item', $newitem);
39790bd8 254
73043833
AG
255 $itembackup[$olditemid] = $newid;
256 if($newitem->dependitem) {
257 $dependitemsmap[$newid] = $newitem->dependitem;
258 }
259
c70ad9f7 260 }
73043833
AG
261 //remapping the dependency
262 foreach($dependitemsmap as $key => $dependitem) {
263 $newitem = $DB->get_record('feedback_item', array('id'=>$key));
264 $newitem->dependitem = $itembackup[$newitem->dependitem];
265 $DB->update_record('feedback_item', $newitem);
39790bd8
PS
266 }
267
c70ad9f7 268 return $error;
269 }
270
271 function feedback_check_xml_utf8($text) {
272 //find the encoding
273 $searchpattern = '/^\<\?xml.+(encoding=\"([a-z0-9-]*)\").+\?\>/is';
d4b1d58c 274
c70ad9f7 275 if(!preg_match($searchpattern, $text, $match)) return false; //no xml-file
d4b1d58c 276
c70ad9f7 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;
282 }
d4b1d58c 283
c70ad9f7 284 if(isset($match[0]) AND isset($match[1]) AND isset($match[2])) { //encoding is given in $match[2]
285 $enc = $match[2];
286 $textlib = textlib_get_instance();
287 return $textlib->convert($text, $enc);
288 }
289 }