skills_import.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * This tool allows platform admins to add skills by uploading a CSV or XML file
  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. /**
  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' => Container::getRouter()->generate('administration') , "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 the warning message is too long then we display the warning message trough a session
  215. if (!empty($warning_message)) {
  216. Display::addFlash(Display::return_message($warning_message, 'normal', false));
  217. }
  218. if ($error_kind_file) {
  219. Display::addFlash(
  220. Display::return_message(
  221. get_lang('YouMustImportAFileAccordingToSelectedOption'),
  222. 'error',
  223. false
  224. )
  225. );
  226. }
  227. }
  228. Display :: display_header($tool_name);
  229. if (!empty($see_message_import)) {
  230. Display::addFlash(Display::return_message($see_message_import));
  231. }
  232. $toolbar = Display::toolbarButton(
  233. get_lang('ManageSkills'),
  234. api_get_path(WEB_CODE_PATH) . 'admin/skill_list.php',
  235. 'list',
  236. 'success',
  237. ['title' => get_lang('CreateSkill')]
  238. );
  239. $toolbar .= '&nbsp;&nbsp;';
  240. $toolbar .= Display::toolbarButton(
  241. get_lang('SkillsWheel'),
  242. api_get_path(WEB_CODE_PATH) . 'admin/skills_wheel.php',
  243. 'bullseye',
  244. 'primary',
  245. ['title' => get_lang('CreateSkill')]
  246. );
  247. $toolbar .= '&nbsp;&nbsp;';
  248. $toolbar .= Display::toolbarButton(
  249. get_lang('BadgesManagement'),
  250. api_get_path(WEB_CODE_PATH) . 'admin/skill_badge_list.php',
  251. 'shield',
  252. 'warning',
  253. ['title' => get_lang('BadgesManagement')]
  254. );
  255. $toolbar .= '<br /><br />';
  256. echo $toolbar;
  257. $form = new FormValidator('user_import','post','skills_import.php');
  258. $form->addElement('header', '', $tool_name);
  259. $form->addElement('hidden', 'formSent');
  260. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  261. $group = array();
  262. $group[] = $form->createElement(
  263. 'radio',
  264. 'file_type',
  265. '',
  266. 'CSV (<a href="skill_example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)',
  267. 'csv'
  268. );
  269. $form->addGroup($group, '', get_lang('FileType'));
  270. $form->addButtonImport(get_lang('Import'));
  271. $defaults['formSent'] = 1;
  272. $defaults['sendMail'] = 0;
  273. $defaults['file_type'] = 'csv';
  274. $form->setDefaults($defaults);
  275. $form->display();
  276. $list = array();
  277. $list_reponse = array();
  278. $result_xml = '';
  279. $i = 0;
  280. $count_fields = count($extra_fields);
  281. if ($count_fields > 0) {
  282. foreach ($extra_fields as $extra) {
  283. $list[] = $extra[1];
  284. $list_reponse[] = 'xxx';
  285. $spaces = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  286. $result_xml .= $spaces.'&lt;'.$extra[1].'&gt;xxx&lt;/'.$extra[1].'&gt;';
  287. if ($i != $count_fields - 1) {
  288. $result_xml .= '<br/>';
  289. }
  290. $i++;
  291. }
  292. }
  293. ?>
  294. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  295. <pre>
  296. <b>id</b>;<b>parent_id</b>;<b>name</b>;<b>description</b>
  297. <b>2</b>;<b>1</b>;<b>Chamilo Expert</b>;Chamilo is an open source LMS;<br />
  298. </pre>
  299. <?php
  300. Display :: display_footer();