chamilo /
chamilo-lms
| 1 | <?php |
||
| 2 | /* For licensing terms, see /license.txt */ |
||
| 3 | /** |
||
| 4 | * This tool allows platform admins to add skills by uploading a CSV or XML file. |
||
| 5 | * |
||
| 6 | * @package chamilo.admin |
||
| 7 | * @documentation Some interesting basic skills can be found in the "Skills" |
||
| 8 | * section here: http://en.wikipedia.org/wiki/Personal_knowledge_management |
||
| 9 | */ |
||
| 10 | $cidReset = true; |
||
| 11 | require_once __DIR__.'/../inc/global.inc.php'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Validate the imported data. |
||
| 15 | * |
||
| 16 | * @param $skills |
||
| 17 | * |
||
| 18 | * @return array |
||
| 19 | */ |
||
| 20 | function validate_data($skills) |
||
| 21 | { |
||
| 22 | $errors = []; |
||
| 23 | // 1. Check if mandatory fields are set. |
||
| 24 | $mandatory_fields = ['id', 'parent_id', 'name']; |
||
| 25 | foreach ($skills as $index => $skill) { |
||
| 26 | foreach ($mandatory_fields as $field) { |
||
| 27 | if (empty($skill[$field])) { |
||
| 28 | $skill['error'] = get_lang(ucfirst($field).'Mandatory'); |
||
| 29 | $errors[] = $skill; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | // 2. Check skill ID is not empty |
||
| 33 | if (!isset($skill['id']) || empty($skill['id'])) { |
||
| 34 | $skill['error'] = get_lang('SkillImportNoID'); |
||
| 35 | $errors[] = $skill; |
||
| 36 | } |
||
| 37 | // 3. Check skill Parent |
||
| 38 | if (!isset($skill['parent_id'])) { |
||
| 39 | $skill['error'] = get_lang('SkillImportNoParent'); |
||
| 40 | $errors[] = $skill; |
||
| 41 | } |
||
| 42 | // 4. Check skill Name |
||
| 43 | if (!isset($skill['name'])) { |
||
| 44 | $skill['error'] = get_lang('SkillImportNoName'); |
||
| 45 | $errors[] = $skill; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | return $errors; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Save the imported data. |
||
| 54 | * |
||
| 55 | * @param array List of users |
||
| 56 | * |
||
| 57 | * @uses \global variable $inserted_in_course, |
||
| 58 | * which returns the list of courses the user was inserted in |
||
| 59 | */ |
||
| 60 | function save_data($skills) |
||
| 61 | { |
||
| 62 | if (is_array($skills)) { |
||
| 63 | $parents = []; |
||
| 64 | foreach ($skills as $index => $skill) { |
||
| 65 | if (isset($parents[$skill['parent_id']])) { |
||
| 66 | $skill['parent_id'] = $parents[$skill['parent_id']]; |
||
| 67 | } else { |
||
| 68 | $skill['parent_id'] = 1; |
||
| 69 | } |
||
| 70 | $skill['a'] = 'add'; |
||
| 71 | $saved_id = $skill['id']; |
||
| 72 | $skill['id'] = null; |
||
| 73 | $oskill = new Skill(); |
||
| 74 | $skill_id = $oskill->add($skill); |
||
|
0 ignored issues
–
show
|
|||
| 75 | $parents[$saved_id] = $skill_id; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Read the CSV-file. |
||
| 82 | * |
||
| 83 | * @param string $file Path to the CSV-file |
||
| 84 | * |
||
| 85 | * @return array All userinformation read from the file |
||
| 86 | */ |
||
| 87 | function parse_csv_data($file) |
||
| 88 | { |
||
| 89 | $skills = Import::csvToArray($file); |
||
| 90 | foreach ($skills as $index => $skill) { |
||
| 91 | $skills[$index] = $skill; |
||
| 92 | } |
||
| 93 | |||
| 94 | return $skills; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * XML-parser: handle start of element. |
||
| 99 | */ |
||
| 100 | function element_start($parser, $data) |
||
| 101 | { |
||
| 102 | $data = api_utf8_decode($data); |
||
| 103 | global $skill; |
||
| 104 | global $current_tag; |
||
| 105 | switch ($data) { |
||
| 106 | case 'Skill': |
||
| 107 | $skill = []; |
||
| 108 | break; |
||
| 109 | default: |
||
| 110 | $current_tag = $data; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * XML-parser: handle end of element. |
||
| 116 | */ |
||
| 117 | function element_end($parser, $data) |
||
| 118 | { |
||
| 119 | $data = api_utf8_decode($data); |
||
| 120 | global $skill; |
||
| 121 | global $skills; |
||
| 122 | global $current_value; |
||
| 123 | switch ($data) { |
||
| 124 | case 'Skill': |
||
| 125 | $skills[] = $skill; |
||
| 126 | break; |
||
| 127 | default: |
||
| 128 | $skill[$data] = $current_value; |
||
| 129 | break; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * XML-parser: handle character data. |
||
| 135 | */ |
||
| 136 | function character_data($parser, $data) |
||
| 137 | { |
||
| 138 | $data = trim(api_utf8_decode($data)); |
||
| 139 | global $current_value; |
||
| 140 | $current_value = $data; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Read the XML-file. |
||
| 145 | * |
||
| 146 | * @param string $file Path to the XML-file |
||
| 147 | * |
||
| 148 | * @return array All userinformation read from the file |
||
| 149 | */ |
||
| 150 | function parse_xml_data($file) |
||
| 151 | { |
||
| 152 | global $current_tag; |
||
| 153 | global $current_value; |
||
| 154 | global $skill; |
||
| 155 | global $skills; |
||
| 156 | $skills = []; |
||
| 157 | $parser = xml_parser_create('UTF-8'); |
||
| 158 | xml_set_element_handler($parser, 'element_start', 'element_end'); |
||
| 159 | xml_set_character_data_handler($parser, 'character_data'); |
||
| 160 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); |
||
| 161 | xml_parse($parser, api_utf8_encode_xml(file_get_contents($file))); |
||
| 162 | xml_parser_free($parser); |
||
| 163 | |||
| 164 | return $skills; |
||
| 165 | } |
||
| 166 | |||
| 167 | $this_section = SECTION_PLATFORM_ADMIN; |
||
| 168 | api_protect_admin_script(true); |
||
| 169 | |||
| 170 | $tool_name = get_lang('ImportSkillsListCSV'); |
||
| 171 | $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('PlatformAdmin')]; |
||
| 172 | |||
| 173 | set_time_limit(0); |
||
| 174 | $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', true); |
||
| 175 | $user_id_error = []; |
||
| 176 | $error_message = ''; |
||
| 177 | |||
| 178 | if (!empty($_POST['formSent']) && $_FILES['import_file']['size'] !== 0) { |
||
| 179 | $file_type = $_POST['file_type']; |
||
| 180 | Security::clear_token(); |
||
| 181 | $tok = Security::get_token(); |
||
| 182 | $allowed_file_mimetype = ['csv', 'xml']; |
||
| 183 | $error_kind_file = false; |
||
| 184 | $error_message = ''; |
||
| 185 | |||
| 186 | $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1)); |
||
| 187 | |||
| 188 | if (in_array($ext_import_file, $allowed_file_mimetype)) { |
||
| 189 | if (strcmp($file_type, 'csv') === 0 && $ext_import_file == $allowed_file_mimetype[0]) { |
||
| 190 | $skills = parse_csv_data($_FILES['import_file']['tmp_name']); |
||
| 191 | $errors = validate_data($skills); |
||
| 192 | $error_kind_file = false; |
||
| 193 | } elseif (strcmp($file_type, 'xml') === 0 && $ext_import_file == $allowed_file_mimetype[1]) { |
||
| 194 | $skills = parse_xml_data($_FILES['import_file']['tmp_name']); |
||
| 195 | $errors = validate_data($skills); |
||
| 196 | $error_kind_file = false; |
||
| 197 | } else { |
||
| 198 | $error_kind_file = true; |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | $error_kind_file = true; |
||
| 202 | } |
||
| 203 | |||
| 204 | // List skill id with error. |
||
| 205 | $skills_to_insert = $skill_id_error = []; |
||
| 206 | if (is_array($errors)) { |
||
| 207 | foreach ($errors as $my_errors) { |
||
| 208 | $skill_id_error[] = $my_errors['SkillName']; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | if (is_array($skills)) { |
||
| 212 | foreach ($skills as $my_skill) { |
||
| 213 | if (isset($my_skill['name']) && !in_array($my_skill['name'], $skill_id_error)) { |
||
| 214 | $skills_to_insert[] = $my_skill; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | if (strcmp($file_type, 'csv') === 0) { |
||
| 220 | save_data($skills_to_insert); |
||
| 221 | } elseif (strcmp($file_type, 'xml') === 0) { |
||
| 222 | save_data($skills_to_insert); |
||
| 223 | } else { |
||
| 224 | $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption'); |
||
| 225 | } |
||
| 226 | |||
| 227 | if (count($errors) > 0) { |
||
| 228 | $see_message_import = get_lang('FileImportedJustSkillsThatAreNotRegistered'); |
||
| 229 | } else { |
||
| 230 | $see_message_import = get_lang('FileImported'); |
||
| 231 | } |
||
| 232 | |||
| 233 | if (count($errors) != 0) { |
||
| 234 | $warning_message = '<ul>'; |
||
| 235 | foreach ($errors as $index => $error_skill) { |
||
| 236 | $warning_message .= '<li><b>'.$error_skill['error'].'</b>: '; |
||
| 237 | $warning_message .= '<strong>'.$error_skill['SkillName'].'</strong> ('.$error_skill['SkillName'].')'; |
||
| 238 | $warning_message .= '</li>'; |
||
| 239 | } |
||
| 240 | $warning_message .= '</ul>'; |
||
| 241 | } |
||
| 242 | |||
| 243 | if ($error_kind_file) { |
||
| 244 | $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption'); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | $interbreadcrumb[] = ["url" => 'skill_list.php', "name" => get_lang('ManageSkills')]; |
||
| 249 | |||
| 250 | Display :: display_header($tool_name); |
||
| 251 | |||
| 252 | if (!empty($error_message)) { |
||
| 253 | echo Display::return_message($error_message, 'error'); |
||
| 254 | } |
||
| 255 | if (!empty($see_message_import)) { |
||
| 256 | echo Display::return_message($see_message_import, 'normal'); |
||
| 257 | } |
||
| 258 | |||
| 259 | $objSkill = new Skill(); |
||
| 260 | echo $objSkill->getToolBar(); |
||
| 261 | |||
| 262 | $form = new FormValidator('user_import', 'post', 'skills_import.php'); |
||
| 263 | $form->addElement('header', '', $tool_name); |
||
| 264 | $form->addElement('hidden', 'formSent'); |
||
| 265 | $form->addElement('file', 'import_file', get_lang('ImportFileLocation')); |
||
| 266 | $group = []; |
||
| 267 | $group[] = $form->createElement( |
||
| 268 | 'radio', |
||
| 269 | 'file_type', |
||
| 270 | '', |
||
| 271 | 'CSV (<a href="skill_example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', |
||
| 272 | 'csv' |
||
| 273 | ); |
||
| 274 | $form->addGroup($group, '', get_lang('FileType')); |
||
| 275 | $form->addButtonImport(get_lang('Import')); |
||
| 276 | $defaults['formSent'] = 1; |
||
| 277 | $defaults['sendMail'] = 0; |
||
| 278 | $defaults['file_type'] = 'csv'; |
||
| 279 | $form->setDefaults($defaults); |
||
| 280 | $form->display(); |
||
| 281 | |||
| 282 | $list = []; |
||
| 283 | $list_reponse = []; |
||
| 284 | $result_xml = ''; |
||
| 285 | $i = 0; |
||
| 286 | $count_fields = count($extra_fields); |
||
| 287 | if ($count_fields > 0) { |
||
| 288 | foreach ($extra_fields as $extra) { |
||
| 289 | $list[] = $extra[1]; |
||
| 290 | $list_reponse[] = 'xxx'; |
||
| 291 | $spaces = ' '; |
||
| 292 | $result_xml .= $spaces.'<'.$extra[1].'>xxx</'.$extra[1].'>'; |
||
| 293 | if ($i != $count_fields - 1) { |
||
| 294 | $result_xml .= '<br/>'; |
||
| 295 | } |
||
| 296 | $i++; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | ?> |
||
| 300 | <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p> |
||
| 301 | |||
| 302 | <pre> |
||
| 303 | <b>id</b>;<b>parent_id</b>;<b>name</b>;<b>description</b> |
||
| 304 | <b>2</b>;<b>1</b>;<b>Chamilo Expert</b>;Chamilo is an open source LMS;<br /> |
||
| 305 | </pre> |
||
| 306 | <?php |
||
| 307 | Display :: display_footer(); |
||
| 308 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.