Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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