skills_import.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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" section here: http://en.wikipedia.org/wiki/Personal_knowledge_management
  7. */
  8. /**
  9. * Validate the imported data.
  10. */
  11. $language_file = array ('admin', 'registration');
  12. $cidReset = true;
  13. require '../inc/global.inc.php';
  14. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  15. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  16. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  17. function validate_data($skills) {
  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, which returns the list of courses the user was inserted in
  52. */
  53. function save_data($skills) {
  54. if (is_array($skills)) {
  55. $parents = array();
  56. foreach ($skills as $index => $skill) {
  57. if (isset($parents[$skill['parent_id']])) {
  58. $skill['parent_id'] = $parents[$skill['parent_id']];
  59. } else {
  60. $skill['parent_id'] = 1;
  61. }
  62. $skill['a'] = 'add';
  63. $saved_id = $skill['id'];
  64. $skill['id'] = null;
  65. $oskill = new Skill();
  66. $skill_id = $oskill->add($skill);
  67. $parents[$saved_id] = $skill_id;
  68. }
  69. }
  70. }
  71. /**
  72. * Read the CSV-file
  73. * @param string $file Path to the CSV-file
  74. * @return array All userinformation read from the file
  75. */
  76. function parse_csv_data($file) {
  77. $skills = Import :: csv_to_array($file);
  78. foreach ($skills as $index => $skill) {
  79. $skills[$index] = $skill;
  80. }
  81. return $skills;
  82. }
  83. /**
  84. * XML-parser: handle start of element
  85. */
  86. function element_start($parser, $data) {
  87. $data = api_utf8_decode($data);
  88. global $skill;
  89. global $current_tag;
  90. switch ($data) {
  91. case 'Skill' :
  92. $skill = array ();
  93. break;
  94. default :
  95. $current_tag = $data;
  96. }
  97. }
  98. /**
  99. * XML-parser: handle end of element
  100. */
  101. function element_end($parser, $data) {
  102. $data = api_utf8_decode($data);
  103. global $skill;
  104. global $skills;
  105. global $current_value;
  106. switch ($data) {
  107. case 'Skill' :
  108. $skills[] = $skill;
  109. break;
  110. default :
  111. $skill[$data] = $current_value;
  112. break;
  113. }
  114. }
  115. /**
  116. * XML-parser: handle character data
  117. */
  118. function character_data($parser, $data) {
  119. $data = trim(api_utf8_decode($data));
  120. global $current_value;
  121. $current_value = $data;
  122. }
  123. /**
  124. * Read the XML-file
  125. * @param string $file Path to the XML-file
  126. * @return array All userinformation read from the file
  127. */
  128. function parse_xml_data($file) {
  129. global $current_tag;
  130. global $current_value;
  131. global $skill;
  132. global $skills;
  133. $skills = array();
  134. $parser = xml_parser_create('UTF-8');
  135. xml_set_element_handler($parser, 'element_start', 'element_end');
  136. xml_set_character_data_handler($parser, 'character_data');
  137. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  138. xml_parse($parser, api_utf8_encode_xml(file_get_contents($file)));
  139. xml_parser_free($parser);
  140. return $skills;
  141. }
  142. $this_section = SECTION_PLATFORM_ADMIN;
  143. api_protect_admin_script(true);
  144. $tool_name = get_lang('ImportSkillsListCSV');
  145. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  146. set_time_limit(0);
  147. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', true);
  148. $user_id_error = array();
  149. $error_message = '';
  150. if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) {
  151. $file_type = $_POST['file_type'];
  152. Security::clear_token();
  153. $tok = Security::get_token();
  154. $allowed_file_mimetype = array('csv','xml');
  155. $error_kind_file = false;
  156. $ext_import_file = substr($_FILES['import_file']['name'],(strrpos($_FILES['import_file']['name'],'.')+1));
  157. if (in_array($ext_import_file,$allowed_file_mimetype)) {
  158. if (strcmp($file_type, 'csv') === 0 && $ext_import_file == $allowed_file_mimetype[0]) {
  159. $skills = parse_csv_data($_FILES['import_file']['tmp_name']);
  160. $errors = validate_data($skills);
  161. $error_kind_file = false;
  162. } elseif (strcmp($file_type, 'xml') === 0 && $ext_import_file == $allowed_file_mimetype[1]) {
  163. $skills = parse_xml_data($_FILES['import_file']['tmp_name']);
  164. $errors = validate_data($skills);
  165. $error_kind_file = false;
  166. } else {
  167. $error_kind_file = true;
  168. }
  169. } else {
  170. $error_kind_file = true;
  171. }
  172. // List skill id whith error.
  173. $skills_to_insert = $skill_id_error = array();
  174. if (is_array($errors)) {
  175. foreach ($errors as $my_errors) {
  176. $skill_id_error[] = $my_errors['SkillName'];
  177. }
  178. }
  179. if (is_array($skills)) {
  180. foreach ($skills as $my_skill) {
  181. if (!in_array($my_skill['SkillName'], $skill_id_error)) {
  182. $skills_to_insert[] = $my_skill;
  183. }
  184. }
  185. }
  186. if (strcmp($file_type, 'csv') === 0) {
  187. save_data($skills_to_insert);
  188. } elseif (strcmp($file_type, 'xml') === 0) {
  189. save_data($skills_to_insert);
  190. } else {
  191. $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption');
  192. }
  193. if (count($errors) > 0) {
  194. $see_message_import = get_lang('FileImportedJustSkillsThatAreNotRegistered');
  195. } else {
  196. $see_message_import = get_lang('FileImported');
  197. }
  198. if (count($errors) != 0) {
  199. $warning_message = '<ul>';
  200. foreach ($errors as $index => $error_skill) {
  201. $warning_message .= '<li><b>'.$error_skill['error'].'</b>: ';
  202. $warning_message .= '<strong>'.$error_skill['SkillName'].'</strong>&nbsp;('.$error_skill['SkillName'].')';
  203. $warning_message .= '</li>';
  204. }
  205. $warning_message .= '</ul>';
  206. }
  207. // if the warning message is too long then we display the warning message trough a session
  208. if (api_strlen($warning_message) > 150) {
  209. $_SESSION['session_message_import_skills'] = $warning_message;
  210. $warning_message = 'session_message';
  211. }
  212. if ($error_kind_file) {
  213. $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption');
  214. } else {
  215. //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);
  216. //exit;
  217. }
  218. }
  219. Display :: display_header($tool_name);
  220. if (!empty($error_message)) {
  221. Display::display_error_message($error_message);
  222. }
  223. if (!empty($see_message_import)) {
  224. Display::display_normal_message($see_message_import);
  225. }
  226. $form = new FormValidator('user_import','post','skills_import.php');
  227. $form->addElement('header', '', $tool_name);
  228. $form->addElement('hidden', 'formSent');
  229. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  230. $group = array();
  231. $group[] = $form->createElement('radio', 'file_type', '', 'CSV (<a href="skill_example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', 'csv');
  232. //$group[] = $form->createElement('radio', 'file_type', null, 'XML (<a href="skill_example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)', 'xml');
  233. $form->addGroup($group, '', get_lang('FileType'), '<br/>');
  234. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  235. $defaults['formSent'] = 1;
  236. $defaults['sendMail'] = 0;
  237. $defaults['file_type'] = 'csv';
  238. $form->setDefaults($defaults);
  239. $form->display();
  240. $list = array();
  241. $list_reponse = array();
  242. $result_xml = '';
  243. $i = 0;
  244. $count_fields = count($extra_fields);
  245. if ($count_fields > 0) {
  246. foreach ($extra_fields as $extra) {
  247. $list[] = $extra[1];
  248. $list_reponse[] = 'xxx';
  249. $spaces = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  250. $result_xml .= $spaces.'&lt;'.$extra[1].'&gt;xxx&lt;/'.$extra[1].'&gt;';
  251. if ($i != $count_fields - 1) {
  252. $result_xml .= '<br/>';
  253. }
  254. $i++;
  255. }
  256. }
  257. ?>
  258. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  259. <pre>
  260. <b>id</b>;<b>parent_id</b>;<b>name</b>;<b>description</b>
  261. <b>2</b>;<b>1</b>;<b>Chamilo Expert</b>;Chamilo is an open source LMS;<br />
  262. </pre>
  263. <!--p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  264. <blockquote>
  265. <pre>
  266. &lt;?xml version=&quot;1.0&quot; encoding=&quot;<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>&quot;?&gt;
  267. &lt;Skills&gt;
  268. &lt;Skill&gt;
  269. <b>&lt;id&gt;n&lt;/id&gt;</b>
  270. <b>&lt;parent_id&gt;n&lt;/parent_id&gt;</b>
  271. <b>&lt;name&gt;xxx&lt;/name&gt;</b>
  272. &lt;description&gt;xxx&lt;/description&gt;
  273. &lt;/Skill&gt;
  274. &lt;/Skills&gt;
  275. </pre>
  276. </blockquote-->
  277. <?php
  278. Display :: display_footer();