Passed
Push — master ( ca27a7...8ba864 )
by
unknown
17:12 queued 08:14
created

importFile()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
nc 14
nop 1
dl 0
loc 33
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Code for Qti2 import integration.
7
 *
8
 * @author Ronny Velasquez
9
 *
10
 * @version $Id: qti2.php  2010-03-12 12:14:25Z $
11
 */
12
13
use Chamilo\CoreBundle\Enums\ActionIcon;
14
15
require_once __DIR__.'/../inc/global.inc.php';
16
17
api_protect_course_script(true);
18
19
// section (for the tabs)
20
$this_section = SECTION_COURSES;
21
22
// access restriction: only teachers are allowed here
23
if (!api_is_allowed_to_edit(null, true)) {
24
    api_not_allowed(true);
25
}
26
27
// the breadcrumbs
28
$interbreadcrumb[] = [
29
    'url' => api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq(),
30
    'name' => get_lang('Tests'),
31
];
32
$is_allowedToEdit = api_is_allowed_to_edit(null, true);
33
34
/**
35
 * This function displays the form to import the zip file with qti2.
36
 */
37
function displayForm()
38
{
39
    $form = '<div class="actions">';
40
    $form .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?show=test&'.api_get_cidreq().'">'.
41
        Display::getMdiIcon(ActionIcon::BACK, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, sprintf(get_lang('Back to %s'), get_lang('Test list'))).'</a>';
42
    $form .= '</div>';
43
    $formValidator = new FormValidator(
44
        'qti_upload',
45
        'post',
46
        api_get_self().'?'.api_get_cidreq(),
47
        null,
48
        ['enctype' => 'multipart/form-data']
49
    );
50
    $formValidator->addHeader(get_lang('Import exercises Qti2'));
51
    $formValidator->addElement('file', 'userFile', get_lang('Download file'));
52
    $formValidator->addButtonImport(get_lang('Upload'));
53
    $form .= $formValidator->returnForm();
54
    echo $form;
55
}
56
57
/**
58
 * This function will import the zip file with the respective qti2.
59
 *
60
 * @param array $array_file ($_FILES)
61
 *
62
 * @return string|array
63
 */
64
function importFile($array_file)
65
{
66
    $unzip = 0;
67
    $process = process_uploaded_file($array_file, false);
68
69
    if (preg_match('/\.zip$/i', $array_file['name'])) {
70
        // if it's a zip, allow zip upload
71
        $unzip = 1;
72
    }
73
74
    if ($process && 1 == $unzip) {
75
        $main_path = api_get_path(SYS_CODE_PATH);
76
        require_once $main_path.'exercise/export/exercise_import.inc.php';
77
78
        // Move upload to a real temp location keeping the .zip name
79
        $permDirs = api_get_permissions_for_new_directories();
80
        $tmpBase = api_get_path(SYS_ARCHIVE_PATH).'qti2_import/'.api_get_unique_id().'/';
81
82
        if (!is_dir($tmpBase)) {
83
            mkdir($tmpBase, $permDirs, true);
84
        }
85
86
        $targetPath = $tmpBase.$array_file['name'];
87
        if (!move_uploaded_file($array_file['tmp_name'], $targetPath)) {
88
            if (!copy($array_file['tmp_name'], $targetPath)) {
89
                return 'FileError';
90
            }
91
        }
92
93
        return import_exercise($targetPath);
94
    }
95
96
    return 'FileError';
97
}
98
99
$message = null;
100
101
// import file
102
if (api_is_allowed_to_edit(null, true)) {
103
    if (isset($_POST['submit'])) {
104
        $imported = importFile($_FILES['userFile']);
105
106
        if (is_numeric($imported) && !empty($imported)) {
107
            header('Location: '.api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'&exerciseId='.$imported);
108
            exit;
109
        } else {
110
            $message = Display::return_message(get_lang($imported));
111
        }
112
    }
113
}
114
115
Display::display_header(get_lang('Import exercises Qti2'), 'Exercises');
116
117
echo $message;
118
119
// display qti form
120
displayForm();
121
122
Display::display_footer();
123