MDL-21227 - convert Feedback to new files api
[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 }
c987dbad
AG
40
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
30ec6bc9
AG
49 $mform = new mod_feedback_import_form();
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");
d4b1d58c 95
a6855934 96 $PAGE->navbar->add($strfeedbacks, new moodle_url('/mod/feedback/index.php', array('id'=>$course->id)));
84f797db 97 $PAGE->navbar->add(format_string($feedback->name));
c70ad9f7 98
84f797db 99 $PAGE->set_title(format_string($feedback->name));
84f797db 100 echo $OUTPUT->header();
c70ad9f7 101
102 /// Print the main part of the page
103 ///////////////////////////////////////////////////////////////////////////
104 ///////////////////////////////////////////////////////////////////////////
105 ///////////////////////////////////////////////////////////////////////////
53b1f5da 106 echo $OUTPUT->heading(get_string('import_questions','feedback'));
d4b1d58c 107
c70ad9f7 108 if(isset($importerror->msg) AND is_array($importerror->msg)) {
d4b1d58c 109 echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter');
c70ad9f7 110 foreach($importerror->msg as $msg) {
111 echo $msg.'<br />';
112 }
d4b1d58c 113 echo $OUTPUT->box_end();
c70ad9f7 114 }
30ec6bc9
AG
115
116 $mform->display();
117
c3aa5bd3 118 echo $OUTPUT->container_start('mdl-align');
30ec6bc9 119 // echo $OUTPUT->single_button(new moodle_url('edit.php', array('id'=>$id, 'do_show'=>'templates')), get_string('cancel'));
c3aa5bd3 120 echo $OUTPUT->container_end();
c6ea0876 121 echo $OUTPUT->footer();
d4b1d58c 122
30ec6bc9 123 function feedback_load_xml_data($xmlcontent) {
c70ad9f7 124 global $CFG;
125 require_once($CFG->dirroot.'/lib/xmlize.php');
d4b1d58c 126
30ec6bc9 127 if(!$xmlcontent = feedback_check_xml_utf8($xmlcontent)) return false;
d4b1d58c 128
30ec6bc9 129 $data = xmlize($xmlcontent, 1, 'UTF-8');
d4b1d58c 130
c70ad9f7 131 if(intval($data['FEEDBACK']['@']['VERSION']) != 200701) {
132 return false;
133 }
134 $data = $data['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
135 return $data;
136 }
d4b1d58c 137
c70ad9f7 138 function feedback_import_loaded_data(&$data, $feedbackid){
0085fff8 139 global $CFG, $DB;
d4b1d58c 140
30ec6bc9
AG
141 feedback_load_feedback_items();
142
c70ad9f7 143 $deleteolditems = optional_param('deleteolditems', 0, PARAM_INT);
d4b1d58c 144
c70ad9f7 145 $error = new object();
146 $error->stat = true;
147 $error->msg = array();
d4b1d58c 148
c70ad9f7 149 if(!is_array($data)) {
150 $error->msg[] = get_string('data_is_not_an_array', 'feedback');
151 $error->stat = false;
152 return $error;
153 }
d4b1d58c 154
c70ad9f7 155 if($deleteolditems) {
156 feedback_delete_all_items($feedbackid);
157 $position = 0;
158 } else {
159 //items will be add to the end of the existing items
0085fff8 160 $position = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
c70ad9f7 161 }
d4b1d58c 162
c70ad9f7 163 foreach($data as $item) {
164 $position++;
165 //check the typ
166 $typ = $item['@']['TYPE'];
d4b1d58c 167
0f22be2e 168 //check oldtypes first
169 switch($typ) {
170 case 'radio':
171 $typ = 'multichoice';
172 $oldtyp = 'radio';
173 break;
174 case 'dropdown':
175 $typ = 'multichoice';
176 $oldtyp = 'dropdown';
177 break;
178 case 'check':
179 $typ = 'multichoice';
180 $oldtyp = 'check';
181 break;
182 case 'radiorated':
183 $typ = 'multichoicerated';
184 $oldtyp = 'radiorated';
185 break;
186 case 'dropdownrated':
187 $typ = 'multichoicerated';
188 $oldtyp = 'dropdownrated';
189 break;
190 default:
191 $oldtyp = $typ;
192 }
193
c70ad9f7 194 $itemclass = 'feedback_item_'.$typ;
195 if($typ != 'pagebreak' AND !class_exists($itemclass)) {
196 $error->stat = false;
197 $error->msg[] = 'type ('.$typ.') not found';
198 continue;
199 }
200 $itemobj = new $itemclass();
d4b1d58c 201
c70ad9f7 202 $newitem = new object();
203 $newitem->feedback = $feedbackid;
204 $newitem->template = 0;
205 $newitem->typ = $typ;
206 $newitem->name = trim($item['#']['ITEMTEXT'][0]['#']);
30ec6bc9 207 $newitem->label = trim($item['#']['ITEMLABEL'][0]['#']);
c70ad9f7 208 $newitem->presentation = trim($item['#']['PRESENTATION'][0]['#']);
0f22be2e 209 //check old types of radio, check, and so on
210 switch($oldtyp) {
211 case 'radio':
212 $newitem->presentation = 'r>>>>>'.$newitem->presentation;
213 break;
214 case 'dropdown':
215 $newitem->presentation = 'd>>>>>'.$newitem->presentation;
216 break;
217 case 'check':
218 $newitem->presentation = 'c>>>>>'.$newitem->presentation;
219 break;
220 case 'radiorated':
221 $newitem->presentation = 'r>>>>>'.$newitem->presentation;
222 break;
223 case 'dropdownrated':
224 $newitem->presentation = 'd>>>>>'.$newitem->presentation;
225 break;
226 }
d4b1d58c 227
c70ad9f7 228 if($typ != 'pagebreak') {
229 $newitem->hasvalue = $itemobj->get_hasvalue();
230 }else {
231 $newitem->hasvalue = 0;
232 }
233 $newitem->required = intval($item['@']['REQUIRED']);
234 $newitem->position = $position;
fc29e51b 235 $DB->insert_record('feedback_item', $newitem);
c70ad9f7 236 }
237 return $error;
238 }
239
240 function feedback_check_xml_utf8($text) {
241 //find the encoding
242 $searchpattern = '/^\<\?xml.+(encoding=\"([a-z0-9-]*)\").+\?\>/is';
d4b1d58c 243
c70ad9f7 244 if(!preg_match($searchpattern, $text, $match)) return false; //no xml-file
d4b1d58c 245
c70ad9f7 246 //$match[0] = \<\? xml ... \?\> (without \)
247 //$match[1] = encoding="...."
248 //$match[2] = ISO-8859-1 or so on
249 if(isset($match[0]) AND !isset($match[1])){ //no encoding given. we assume utf-8
250 return $text;
251 }
d4b1d58c 252
c70ad9f7 253 if(isset($match[0]) AND isset($match[1]) AND isset($match[2])) { //encoding is given in $match[2]
254 $enc = $match[2];
255 $textlib = textlib_get_instance();
256 return $textlib->convert($text, $enc);
257 }
258 }
84973212 259?>