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