skills_import.php 9.2 KB

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