Completed
Push — 1.10.x ( a9323e...dc10cd )
by Angel Fernando Quiroz
124:05 queued 70:15
created

main/admin/class_import.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
*	@package chamilo.admin
5
*/
6
/**
7
 * Code
8
*   This     tool allows platform admins to add classes by uploading a CSV file
9
* @todo Add some langvars to DLTT
10
*/
11
12
/**
13
 * Validates imported data.
14
 */
15 View Code Duplication
function validate_data($classes) {
16
    $errors = array();
17
    foreach ($classes as $index => $class) {
18
        // 1. Check wheter ClassName is available.
19
        if (!isset($class['ClassName']) || strlen(trim($class['ClassName'])) == 0) {
20
            $class['line'] = $index + 2;
21
            $class['error'] = get_lang('MissingClassName');
22
            $errors[] = $class;
23
        }
24
        // 2. Check whether class doesn't exist yet.
25
        else {
26
            if (ClassManager::class_name_exists($class['ClassName'])) {
27
                $class['line'] = $index + 2;
28
                $class['error'] = get_lang('ClassNameExists').' <strong>'.$class['ClassName'].'</strong>';
29
                $errors[] = $class;
30
            }
31
        }
32
    }
33
    return $errors;
34
}
35
36
/**
37
 * Save imported class data to database
38
 */
39
function save_data($classes) {
40
    $number_of_added_classes = 0;
41
    foreach ($classes as $index => $class) {
42
        if (ClassManager::create_class($class['ClassName'])) {
43
            $number_of_added_classes++;
44
        }
45
    }
46
    return $number_of_added_classes;
47
}
48
49
// Resetting the course id.
50
$cidReset = true;
51
52
// Including some necessary dokeos files.
53
include '../inc/global.inc.php';
54
55
// Setting the section (for the tabs).
56
$this_section = SECTION_PLATFORM_ADMIN;
57
58
// Access restrictions.
59
api_protect_admin_script();
60
61
// setting breadcrumbs
62
$interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
63
$interbreadcrumb[] = array ('url' => 'class_list.php', 'name' => get_lang('Classes'));
64
65
// Database Table Definitions
66
67
// Setting the name of the tool.
68
$tool_name = get_lang('ImportClassListCSV');
69
70
// Displaying the header.
71
Display :: display_header($tool_name);
72
//api_display_tool_title($tool_name);
73
74
set_time_limit(0);
75
76
$form = new FormValidator('import_classes');
77
$form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
78
$form->addButtonImport(get_lang('Import'));
79
80 View Code Duplication
if ($form->validate()) {
81
    $classes = Import::csvToArray($_FILES['import_file']['tmp_name']);
0 ignored issues
show
Deprecated Code introduced by
The method Import::csvToArray() has been deprecated with message: use cvs_reader instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
    $errors = validate_data($classes);
83
    if (count($errors) == 0) {
84
        $number_of_added_classes = save_data($classes);
85
        Display::display_normal_message($number_of_added_classes.' '.get_lang('ClassesCreated'));
86
    } else {
87
        $error_message = get_lang('ErrorsWhenImportingFile');
88
        $error_message .= '<ul>';
89
        foreach ($errors as $index => $error_class) {
90
            $error_message .= '<li>'.$error_class['error'].' ('.get_lang('Line').' '.$error_class['line'].')';
91
            $error_message .= '</li>';
92
        }
93
        $error_message .= '</ul>';
94
        $error_message .= get_lang('NoClassesHaveBeenCreated');
95
        Display :: display_error_message($error_message);
96
    }
97
}
98
99
$form->display();
100
?>
101
<p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
102
103
 <pre>
104
  <b>ClassName</b>
105
  <b>1A</b>
106
  <b>1B</b>
107
  <b>2A group 1</b>
108
  <b>2A group 2</b>
109
 </pre>
110
<?php
111
112
// Displaying the footer.
113
Display :: display_footer();
114