skills_import.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * Validate the imported data.
  14. *
  15. * @param $skills
  16. *
  17. * @return array
  18. */
  19. function validate_data($skills)
  20. {
  21. $errors = [];
  22. // 1. Check if mandatory fields are set.
  23. $mandatory_fields = ['id', 'parent_id', 'name'];
  24. foreach ($skills as $index => $skill) {
  25. foreach ($mandatory_fields as $field) {
  26. if (empty($skill[$field])) {
  27. $skill['error'] = get_lang(ucfirst($field).'Mandatory');
  28. $errors[] = $skill;
  29. }
  30. }
  31. // 2. Check skill ID is not empty
  32. if (!isset($skill['id']) || empty($skill['id'])) {
  33. $skill['error'] = get_lang('SkillImportNoID');
  34. $errors[] = $skill;
  35. }
  36. // 3. Check skill Parent
  37. if (!isset($skill['parent_id'])) {
  38. $skill['error'] = get_lang('SkillImportNoParent');
  39. $errors[] = $skill;
  40. }
  41. // 4. Check skill Name
  42. if (!isset($skill['name'])) {
  43. $skill['error'] = get_lang('SkillImportNoName');
  44. $errors[] = $skill;
  45. }
  46. }
  47. return $errors;
  48. }
  49. /**
  50. * Save the imported data.
  51. *
  52. * @param array List of users
  53. *
  54. * @uses \global variable $inserted_in_course,
  55. * which returns the list of courses the user was inserted in
  56. */
  57. function save_data($skills)
  58. {
  59. if (is_array($skills)) {
  60. $parents = [];
  61. $urlId = api_get_current_access_url_id();
  62. foreach ($skills as $index => $skill) {
  63. if (isset($parents[$skill['parent_id']])) {
  64. $skill['parent_id'] = $parents[$skill['parent_id']];
  65. } else {
  66. $skill['parent_id'] = 1;
  67. }
  68. if (empty($skill['access_url_id'])) {
  69. $skill['access_url_id'] = $urlId;
  70. }
  71. $skill['a'] = 'add';
  72. $saved_id = $skill['id'];
  73. $skill['id'] = null;
  74. $oskill = new Skill();
  75. $skill_id = $oskill->add($skill);
  76. $parents[$saved_id] = $skill_id;
  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. return $skills;
  94. }
  95. $this_section = SECTION_PLATFORM_ADMIN;
  96. api_protect_admin_script(true);
  97. $tool_name = get_lang('ImportSkillsListCSV');
  98. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('PlatformAdmin')];
  99. set_time_limit(0);
  100. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', true);
  101. $user_id_error = [];
  102. $error_message = '';
  103. if (!empty($_POST['formSent']) && $_FILES['import_file']['size'] !== 0) {
  104. $file_type = $_POST['file_type'];
  105. Security::clear_token();
  106. $tok = Security::get_token();
  107. $allowed_file_mimetype = ['csv'];
  108. $error_kind_file = false;
  109. $error_message = '';
  110. $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
  111. if (in_array($ext_import_file, $allowed_file_mimetype)) {
  112. if (strcmp($file_type, 'csv') === 0 && $ext_import_file == $allowed_file_mimetype[0]) {
  113. $skills = parse_csv_data($_FILES['import_file']['tmp_name']);
  114. $errors = validate_data($skills);
  115. $error_kind_file = false;
  116. } else {
  117. $error_kind_file = true;
  118. }
  119. } else {
  120. $error_kind_file = true;
  121. }
  122. // List skill id with error.
  123. $skills_to_insert = $skill_id_error = [];
  124. if (is_array($errors)) {
  125. foreach ($errors as $my_errors) {
  126. $skill_id_error[] = $my_errors['SkillName'];
  127. }
  128. }
  129. if (is_array($skills)) {
  130. foreach ($skills as $my_skill) {
  131. if (isset($my_skill['name']) && !in_array($my_skill['name'], $skill_id_error)) {
  132. $skills_to_insert[] = $my_skill;
  133. }
  134. }
  135. }
  136. if (strcmp($file_type, 'csv') === 0) {
  137. save_data($skills_to_insert);
  138. } else {
  139. $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption');
  140. }
  141. if (count($errors) > 0) {
  142. $see_message_import = get_lang('FileImportedJustSkillsThatAreNotRegistered');
  143. } else {
  144. $see_message_import = get_lang('FileImported');
  145. }
  146. if (count($errors) != 0) {
  147. $warning_message = '<ul>';
  148. foreach ($errors as $index => $error_skill) {
  149. $warning_message .= '<li><b>'.$error_skill['error'].'</b>: ';
  150. $warning_message .= '<strong>'.$error_skill['SkillName'].'</strong>&nbsp;('.$error_skill['SkillName'].')';
  151. $warning_message .= '</li>';
  152. }
  153. $warning_message .= '</ul>';
  154. }
  155. if ($error_kind_file) {
  156. $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption');
  157. }
  158. }
  159. $interbreadcrumb[] = ["url" => 'skill_list.php', "name" => get_lang('ManageSkills')];
  160. Display :: display_header($tool_name);
  161. if (!empty($error_message)) {
  162. echo Display::return_message($error_message, 'error');
  163. }
  164. if (!empty($see_message_import)) {
  165. echo Display::return_message($see_message_import, 'normal');
  166. }
  167. $objSkill = new Skill();
  168. echo $objSkill->getToolBar();
  169. $form = new FormValidator('user_import', 'post', 'skills_import.php');
  170. $form->addElement('header', '', $tool_name);
  171. $form->addElement('hidden', 'formSent');
  172. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  173. $group = [];
  174. $group[] = $form->createElement(
  175. 'radio',
  176. 'file_type',
  177. '',
  178. 'CSV (<a href="skill_example.csv" target="_blank" download>'.get_lang('ExampleCSVFile').'</a>)',
  179. 'csv'
  180. );
  181. $form->addGroup($group, '', get_lang('FileType'));
  182. $form->addButtonImport(get_lang('Import'));
  183. $defaults['formSent'] = 1;
  184. $defaults['sendMail'] = 0;
  185. $defaults['file_type'] = 'csv';
  186. $form->setDefaults($defaults);
  187. $form->display();
  188. ?>
  189. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  190. <pre>
  191. <b>id</b>;<b>parent_id</b>;<b>name</b>;<b>description</b>
  192. <b>2</b>;<b>1</b>;<b>Chamilo Expert</b>;Chamilo is an open source LMS;<br />
  193. </pre>
  194. <?php
  195. Display :: display_footer();