skills_import.php 8.3 KB

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