skills_import.php 9.0 KB

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