MDL-29190 - XHTML not well formed in show_nonrespondents.php
[moodle.git] / mod / feedback / import.php
1 <?php
3 /**
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  */
11     require_once("../../config.php");
12     require_once("lib.php");
13     require_once('import_form.php');
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);
20     $url = new moodle_url('/mod/feedback/import.php', array('id'=>$id));
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);
29     if (! $cm = get_coursemodule_from_id('feedback', $id)) {
30         print_error('invalidcoursemodule');
31     }
33     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
34         print_error('coursemisconf');
35     }
37     if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
38         print_error('invalidcoursemodule');
39     }
41     if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
42         print_error('badcontext');
43     }
45     require_login($course->id, true, $cm);
47     require_capability('mod/feedback:edititems', $context);
49     $mform = new 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();
58     if ($mform->is_cancelled()) {
59         redirect('edit.php?id='.$id.'&do_show=templates');
60     }
63     // unset($filename);
64     // if ($action == 'choosefile' AND confirm_sesskey() ) {
66         ////file checks out ok
67         // $fileisgood = false;
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     // }
76     // process if we are happy file is ok
77     if ($choosefile) {
78         $xmlcontent = $mform->get_file_content('choosefile');
80         if(!$xmldata = feedback_load_xml_data($xmlcontent)) {
81             print_error('cannotloadxml', 'feedback', 'edit.php?id='.$id);
82         }
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     }
92     /// Print the page header
93     $strfeedbacks = get_string("modulenameplural", "feedback");
94     $strfeedback  = get_string("modulename", "feedback");
96     $PAGE->set_heading(format_string($course->fullname));
97     $PAGE->set_title(format_string($feedback->name));
98     echo $OUTPUT->header();
100     /// print the tabs
101     include('tabs.php');
103     /// Print the main part of the page
104     ///////////////////////////////////////////////////////////////////////////
105     ///////////////////////////////////////////////////////////////////////////
106     ///////////////////////////////////////////////////////////////////////////
107     echo $OUTPUT->heading(get_string('import_questions','feedback'));
109     if(isset($importerror->msg) AND is_array($importerror->msg)) {
110         echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter');
111         foreach($importerror->msg as $msg) {
112             echo $msg.'<br />';
113         }
114         echo $OUTPUT->box_end();
115     }
117     $mform->display();
119     // echo $OUTPUT->container_start('mdl-align');
120     // echo $OUTPUT->single_button(new moodle_url('edit.php', array('id'=>$id, 'do_show'=>'templates')), get_string('cancel'));
121     // echo $OUTPUT->container_end();
122     echo $OUTPUT->footer();
124     function feedback_load_xml_data($xmlcontent) {
125         global $CFG;
126         require_once($CFG->dirroot.'/lib/xmlize.php');
128         if(!$xmlcontent = feedback_check_xml_utf8($xmlcontent)) return false;
130         $data = xmlize($xmlcontent, 1, 'UTF-8');
132         if(intval($data['FEEDBACK']['@']['VERSION']) != 200701) {
133             return false;
134         }
135         $data = $data['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
136         return $data;
137     }
139     function feedback_import_loaded_data(&$data, $feedbackid){
140         global $CFG, $DB;
142         feedback_load_feedback_items();
144         $deleteolditems = optional_param('deleteolditems', 0, PARAM_INT);
146         $error = new stdClass();
147         $error->stat = true;
148         $error->msg = array();
150         if(!is_array($data)) {
151             $error->msg[] = get_string('data_is_not_an_array', 'feedback');
152             $error->stat = false;
153             return $error;
154         }
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
161             $position = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
162         }
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();
168         foreach($data as $item) {
169             $position++;
170             //check the typ
171             $typ = $item['@']['TYPE'];
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             }
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();
207             $newitem = new stdClass();
208             $newitem->feedback = $feedbackid;
209             $newitem->template = 0;
210             $newitem->typ = $typ;
211             $newitem->name = trim($item['#']['ITEMTEXT'][0]['#']);
212             $newitem->label = trim($item['#']['ITEMLABEL'][0]['#']);
213             $newitem->options = trim($item['#']['OPTIONS'][0]['#']);
214             $newitem->presentation = trim($item['#']['PRESENTATION'][0]['#']);
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             }
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]['#']);
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;
253             $newid = $DB->insert_record('feedback_item', $newitem);
255             $itembackup[$olditemid] = $newid;
256             if($newitem->dependitem) {
257                 $dependitemsmap[$newid] = $newitem->dependitem;
258             }
260         }
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);
266         }
268         return $error;
269     }
271     function feedback_check_xml_utf8($text) {
272         //find the encoding
273         $searchpattern = '/^\<\?xml.+(encoding=\"([a-z0-9-]*)\").+\?\>/is';
275         if(!preg_match($searchpattern, $text, $match)) 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;
282         }
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     }