MDL-21227 - convert Feedback to new files api
[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     }
40     
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 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();
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->navbar->add($strfeedbacks, new moodle_url('/mod/feedback/index.php', array('id'=>$course->id)));
97     $PAGE->navbar->add(format_string($feedback->name));
99     $PAGE->set_title(format_string($feedback->name));
100     echo $OUTPUT->header();
102     /// Print the main part of the page
103     ///////////////////////////////////////////////////////////////////////////
104     ///////////////////////////////////////////////////////////////////////////
105     ///////////////////////////////////////////////////////////////////////////
106     echo $OUTPUT->heading(get_string('import_questions','feedback'));
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 />';
112         }
113         echo $OUTPUT->box_end();
114     }
115     
116     $mform->display();
117     
118     echo $OUTPUT->container_start('mdl-align');
119     // echo $OUTPUT->single_button(new moodle_url('edit.php', array('id'=>$id, 'do_show'=>'templates')), get_string('cancel'));
120     echo $OUTPUT->container_end();
121     echo $OUTPUT->footer();
123     function feedback_load_xml_data($xmlcontent) {
124         global $CFG;
125         require_once($CFG->dirroot.'/lib/xmlize.php');
127         if(!$xmlcontent = feedback_check_xml_utf8($xmlcontent)) return false;
129         $data = xmlize($xmlcontent, 1, 'UTF-8');
131         if(intval($data['FEEDBACK']['@']['VERSION']) != 200701) {
132             return false;
133         }
134         $data = $data['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
135         return $data;
136     }
138     function feedback_import_loaded_data(&$data, $feedbackid){
139         global $CFG, $DB;
141         feedback_load_feedback_items();
142     
143         $deleteolditems = optional_param('deleteolditems', 0, PARAM_INT);
145         $error = new object();
146         $error->stat = true;
147         $error->msg = array();
149         if(!is_array($data)) {
150             $error->msg[] = get_string('data_is_not_an_array', 'feedback');
151             $error->stat = false;
152             return $error;
153         }
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
160             $position = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
161         }
163         foreach($data as $item) {
164             $position++;
165             //check the typ
166             $typ = $item['@']['TYPE'];
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             }
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();
202             $newitem = new object();
203             $newitem->feedback = $feedbackid;
204             $newitem->template = 0;
205             $newitem->typ = $typ;
206             $newitem->name = trim($item['#']['ITEMTEXT'][0]['#']);
207             $newitem->label = trim($item['#']['ITEMLABEL'][0]['#']);
208             $newitem->presentation = trim($item['#']['PRESENTATION'][0]['#']);
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             }
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;
235             $DB->insert_record('feedback_item', $newitem);
236         }
237         return $error;
238     }
240     function feedback_check_xml_utf8($text) {
241         //find the encoding
242         $searchpattern = '/^\<\?xml.+(encoding=\"([a-z0-9-]*)\").+\?\>/is';
244         if(!preg_match($searchpattern, $text, $match)) return false; //no xml-file
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         }
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     }
259 ?>