cce13bd9102a3096f5284c812887c9c761655189
[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");
14     // get parameters
15     $id = required_param('id', PARAM_INT);
16     $choosefile = optional_param('choosefile', false, PARAM_PATH);
17     $action = optional_param('action', false, PARAM_ALPHA);
19     if(($formdata = data_submitted()) AND !confirm_sesskey()) {
20         print_error('invalidsesskey');
21     }
23     $url = new moodle_url('/mod/feedback/import.php', array('id'=>$id));
24     if ($choosefile !== false) {
25         $url->param('choosefile', $choosefile);
26     }
27     if ($action !== false) {
28         $url->param('action', $action);
29     }
30     $PAGE->set_url($url);
32     if (! $cm = get_coursemodule_from_id('feedback', $id)) {
33         print_error('invalidcoursemodule');
34     }
36     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
37         print_error('coursemisconf');
38     }
40     if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
41         print_error('invalidcoursemodule');
42     }
43     
44     if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
45         print_error('badcontext');
46     }
48     require_login($course->id, true, $cm);
50     require_capability('mod/feedback:edititems', $context);
52     unset($filename);
53     if ($action == 'choosefile' AND confirm_sesskey() ) {
55         // file checks out ok
56         $fileisgood = false;
58         // work out if this is an uploaded file
59         // or one from the filesarea.
60         if ($choosefile) {
61             $filename = "{$CFG->dataroot}/{$course->id}/{$choosefile}";
62         }
63     }
65     // process if we are happy file is ok
66     if (isset($filename)) {
67         if(!is_file($filename) OR !is_readable($filename)) {
68             print_error('filenotreadable');
69         }
70         if(!$xmldata = feedback_load_xml_data($filename)) {
71             print_error('cannotloadxml', 'feedback', 'edit.php?id='.$id);
72         }
74         $importerror = feedback_import_loaded_data($xmldata, $feedback->id);
75         if($importerror->stat == true) {
76             redirect('edit.php?id='.$id.'&do_show=templates', get_string('import_successfully', 'feedback'), 3);
77             exit;
78         }
79     }
82     /// Print the page header
83     $strfeedbacks = get_string("modulenameplural", "feedback");
84     $strfeedback  = get_string("modulename", "feedback");
86     $PAGE->navbar->add($strfeedbacks, new moodle_url('/mod/feedback/index.php', array('id'=>$course->id)));
87     $PAGE->navbar->add(format_string($feedback->name));
89     $PAGE->set_title(format_string($feedback->name));
90     echo $OUTPUT->header();
92     /// Print the main part of the page
93     ///////////////////////////////////////////////////////////////////////////
94     ///////////////////////////////////////////////////////////////////////////
95     ///////////////////////////////////////////////////////////////////////////
96     echo $OUTPUT->heading(get_string('import_questions','feedback'));
98     if(isset($importerror->msg) AND is_array($importerror->msg)) {
99         echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter');
100         foreach($importerror->msg as $msg) {
101             echo $msg.'<br />';
102         }
103         echo $OUTPUT->box_end();
104     }
106     ?>
108      <form name="form" method="post" action="import.php">
109           <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
110           <input type="hidden" name="action" value="choosefile" />
111           <input type="hidden" name="id" value="<?php p($id);?>" />
112           <input type="hidden" name="do_show" value="templates" />
113           <?php echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide'); ?>
114           <input type="radio" name="deleteolditems" value="1" checked="checked" /> <?php echo get_string('delete_old_items', 'feedback').' ('.get_string('oldvalueswillbedeleted','feedback').')';?><br />
115           <input type="radio" name="deleteolditems" value="0" /> <?php echo get_string('append_new_items', 'feedback').' ('.get_string('oldvaluespreserved','feedback').')';?><br />
116           <table cellpadding="5">
117                 <tr>
118                      <td align="right"><?php print_string('file', 'feedback'); ?>:</td>
119                      <td><input type="text" name="choosefile" size="50" /></td>
120                 </tr>
122                 <tr>
123                      <td>&nbsp;</td>
124                      <td><?php
125                         echo 'TODO: implement new file picker and file ahdnling - MDL-14493';
126                         ?>
127                           <input type="submit" name="save" value="<?php print_string('importfromthisfile', 'feedback'); ?>" />
128                     </td>
129                 </tr>
130           </table>
131           <?php
132           echo $OUTPUT->box_end(); ?>
133      </form>
135      <?php
137     echo $OUTPUT->container_start('mdl-align');
138     echo $OUTPUT->single_button(new moodle_url('edit.php', array('id'=>$id, 'do_show'=>'templates')), get_string('cancel'));
139     echo $OUTPUT->container_end();
140     echo $OUTPUT->footer();
142     function feedback_load_xml_data($filename) {
143         global $CFG;
144         require_once($CFG->dirroot.'/lib/xmlize.php');
146         $datei =  file_get_contents($filename);
148         if(!$datei = feedback_check_xml_utf8($datei)) return false;
150         $data = xmlize($datei, 1, 'UTF-8');
152         if(intval($data['FEEDBACK']['@']['VERSION']) != 200701) {
153             return false;
154         }
155         $data = $data['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
156         return $data;
157     }
159     function feedback_import_loaded_data(&$data, $feedbackid){
160         global $CFG, $DB;
162         $deleteolditems = optional_param('deleteolditems', 0, PARAM_INT);
164         $error = new object();
165         $error->stat = true;
166         $error->msg = array();
168         if(!is_array($data)) {
169             $error->msg[] = get_string('data_is_not_an_array', 'feedback');
170             $error->stat = false;
171             return $error;
172         }
174         if($deleteolditems) {
175             feedback_delete_all_items($feedbackid);
176             $position = 0;
177         } else {
178             //items will be add to the end of the existing items
179             $position = $DB->count_records('feedback_item', array('feedback'=>$feedbackid));
180         }
182         foreach($data as $item) {
183             $position++;
184             //check the typ
185             $typ = $item['@']['TYPE'];
187             //check oldtypes first
188             switch($typ) {
189                 case 'radio':
190                     $typ = 'multichoice';
191                     $oldtyp = 'radio';
192                     break;
193                 case 'dropdown':
194                     $typ = 'multichoice';
195                     $oldtyp = 'dropdown';
196                     break;
197                 case 'check':
198                     $typ = 'multichoice';
199                     $oldtyp = 'check';
200                     break;
201                 case 'radiorated':
202                     $typ = 'multichoicerated';
203                     $oldtyp = 'radiorated';
204                     break;
205                 case 'dropdownrated':
206                     $typ = 'multichoicerated';
207                     $oldtyp = 'dropdownrated';
208                     break;
209                 default:
210                     $oldtyp = $typ;
211             }
213             $itemclass = 'feedback_item_'.$typ;
214             if($typ != 'pagebreak' AND !class_exists($itemclass)) {
215                 $error->stat = false;
216                 $error->msg[] = 'type ('.$typ.') not found';
217                 continue;
218             }
219             $itemobj = new $itemclass();
221             $newitem = new object();
222             $newitem->feedback = $feedbackid;
223             $newitem->template = 0;
224             $newitem->typ = $typ;
225             $newitem->name = trim($item['#']['ITEMTEXT'][0]['#']);
226             $newitem->presentation = trim($item['#']['PRESENTATION'][0]['#']);
227             //check old types of radio, check, and so on
228             switch($oldtyp) {
229                 case 'radio':
230                     $newitem->presentation = 'r>>>>>'.$newitem->presentation;
231                     break;
232                 case 'dropdown':
233                     $newitem->presentation = 'd>>>>>'.$newitem->presentation;
234                     break;
235                 case 'check':
236                     $newitem->presentation = 'c>>>>>'.$newitem->presentation;
237                     break;
238                 case 'radiorated':
239                     $newitem->presentation = 'r>>>>>'.$newitem->presentation;
240                     break;
241                 case 'dropdownrated':
242                     $newitem->presentation = 'd>>>>>'.$newitem->presentation;
243                     break;
244             }
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             $DB->insert_record('feedback_item', $newitem);
254         }
255         return $error;
256     }
258     function feedback_check_xml_utf8($text) {
259         //find the encoding
260         $searchpattern = '/^\<\?xml.+(encoding=\"([a-z0-9-]*)\").+\?\>/is';
262         if(!preg_match($searchpattern, $text, $match)) return false; //no xml-file
264         //$match[0] = \<\? xml ... \?\> (without \)
265         //$match[1] = encoding="...."
266         //$match[2] = ISO-8859-1 or so on
267         if(isset($match[0]) AND !isset($match[1])){ //no encoding given. we assume utf-8
268             return $text;
269         }
271         if(isset($match[0]) AND isset($match[1]) AND isset($match[2])) { //encoding is given in $match[2]
272             $enc = $match[2];
273             $textlib = textlib_get_instance();
274             return $textlib->convert($text, $enc);
275         }
276     }
277 ?>