skills_import.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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('The skill ID was not set');
  34. $errors[] = $skill;
  35. }
  36. // 3. Check skill Parent
  37. if (!isset($skill['parent_id'])) {
  38. $skill['error'] = get_lang('The parent skill was not set');
  39. $errors[] = $skill;
  40. }
  41. // 4. Check skill Name
  42. if (!isset($skill['name'])) {
  43. $skill['error'] = get_lang('The skill had no name set');
  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. $skill['short_code'] = '';
  75. $skill['icon'] = '';
  76. $skill['updated_at'] = api_get_utc_datetime();
  77. $oskill = new Skill();
  78. $skill_id = $oskill->add($skill);
  79. $parents[$saved_id] = $skill_id;
  80. }
  81. }
  82. }
  83. /**
  84. * Read the CSV-file.
  85. *
  86. * @param string $file Path to the CSV-file
  87. *
  88. * @return array All userinformation read from the file
  89. */
  90. function parse_csv_data($file)
  91. {
  92. $skills = Import::csvToArray($file);
  93. foreach ($skills as $index => $skill) {
  94. $skills[$index] = $skill;
  95. }
  96. return $skills;
  97. }
  98. $this_section = SECTION_PLATFORM_ADMIN;
  99. api_protect_admin_script(true);
  100. $tool_name = get_lang('Import skills from a CSV file');
  101. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('Administration')];
  102. set_time_limit(0);
  103. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', true);
  104. $user_id_error = [];
  105. $error_message = '';
  106. if (!empty($_POST['formSent']) && $_FILES['import_file']['size'] !== 0) {
  107. $file_type = $_POST['file_type'];
  108. Security::clear_token();
  109. $tok = Security::get_token();
  110. $allowed_file_mimetype = ['csv'];
  111. $error_kind_file = false;
  112. $error_message = '';
  113. $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
  114. if (in_array($ext_import_file, $allowed_file_mimetype)) {
  115. if (strcmp($file_type, 'csv') === 0 && $ext_import_file == $allowed_file_mimetype[0]) {
  116. $skills = parse_csv_data($_FILES['import_file']['tmp_name']);
  117. $errors = validate_data($skills);
  118. $error_kind_file = false;
  119. } else {
  120. $error_kind_file = true;
  121. }
  122. } else {
  123. $error_kind_file = true;
  124. }
  125. // List skill id with error.
  126. $skills_to_insert = $skill_id_error = [];
  127. if (is_array($errors)) {
  128. foreach ($errors as $my_errors) {
  129. $skill_id_error[] = $my_errors['SkillName'];
  130. }
  131. }
  132. if (is_array($skills)) {
  133. foreach ($skills as $my_skill) {
  134. if (isset($my_skill['name']) && !in_array($my_skill['name'], $skill_id_error)) {
  135. $skills_to_insert[] = $my_skill;
  136. }
  137. }
  138. }
  139. if (strcmp($file_type, 'csv') === 0) {
  140. save_data($skills_to_insert);
  141. } else {
  142. $error_message = get_lang('You must import a file corresponding to the selected format');
  143. }
  144. if (count($errors) > 0) {
  145. $see_message_import = get_lang('Only skills that were not registered were imported');
  146. } else {
  147. $see_message_import = get_lang('File imported');
  148. }
  149. if (count($errors) != 0) {
  150. $warning_message = '<ul>';
  151. foreach ($errors as $index => $error_skill) {
  152. $warning_message .= '<li><b>'.$error_skill['error'].'</b>: ';
  153. $warning_message .= '<strong>'.$error_skill['SkillName'].'</strong>&nbsp;('.$error_skill['SkillName'].')';
  154. $warning_message .= '</li>';
  155. }
  156. $warning_message .= '</ul>';
  157. }
  158. if ($error_kind_file) {
  159. $error_message = get_lang('You must import a file corresponding to the selected format');
  160. }
  161. }
  162. $interbreadcrumb[] = ["url" => 'skill_list.php', "name" => get_lang('Manage skills')];
  163. Display :: display_header($tool_name);
  164. if (!empty($error_message)) {
  165. echo Display::return_message($error_message, 'error');
  166. }
  167. if (!empty($see_message_import)) {
  168. echo Display::return_message($see_message_import, 'normal');
  169. }
  170. $objSkill = new Skill();
  171. echo $objSkill->getToolBar();
  172. $form = new FormValidator('user_import', 'post', 'skills_import.php');
  173. $form->addElement('header', '', $tool_name);
  174. $form->addElement('hidden', 'formSent');
  175. $form->addElement('file', 'import_file', get_lang('Import marks in an assessment'));
  176. $group = [];
  177. $group[] = $form->createElement(
  178. 'radio',
  179. 'file_type',
  180. '',
  181. 'CSV (<a href="skill_example.csv" target="_blank" download>'.get_lang('Example CSV file').'</a>)',
  182. 'csv'
  183. );
  184. $form->addGroup($group, '', get_lang('File type'));
  185. $form->addButtonImport(get_lang('Import'));
  186. $defaults['formSent'] = 1;
  187. $defaults['sendMail'] = 0;
  188. $defaults['file_type'] = 'csv';
  189. $form->setDefaults($defaults);
  190. $form->display();
  191. ?>
  192. <p><?php echo get_lang('The CSV file must look like this').' ('.get_lang('Fields in <strong>bold</strong> are mandatory.').')'; ?> :</p>
  193. <pre>
  194. <b>id</b>;<b>parent_id</b>;<b>name</b>;<b>description</b>
  195. <b>2</b>;<b>1</b>;<b>Chamilo Expert</b>;Chamilo is an open source LMS;<br />
  196. </pre>
  197. <?php
  198. Display :: display_footer();