user_import.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php // $Id: user_import.php 14792 2008-04-08 20:57:53Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos SPRL
  6. Copyright (c) 2008 Julio Montoya Armas <gugli100@gmail.com>
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. ==============================================================================
  19. * This tool allows platform admins to add users by uploading a CSV or XML file
  20. * This code is inherited from admin/user_import.php
  21. * Created on 26 julio 2008 by Julio Montoya gugli100@gmail.com
  22. ==============================================================================
  23. */
  24. /**
  25. * Checks if a username exist in the DB otherwise it create a "double"
  26. * i.e. if we look into for jmontoya but the user's name already exist we create the user jmontoya2
  27. * the return array will be array(username=>'jmontoya', sufix='2')
  28. * @param string firstname
  29. * @param string lastname
  30. * @param string username
  31. * @return array with the username, the sufix
  32. * @author Julio Montoya Armas
  33. */
  34. function make_username($firstname, $lastname, $username, $language = null, $encoding = null) {
  35. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  36. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  37. // if username exist
  38. if (!UserManager::is_username_available($username) || empty($username)) {
  39. $i = 0;
  40. while (1) {
  41. if ($i == 0) {
  42. $sufix = '';
  43. } else {
  44. $sufix = $i;
  45. }
  46. $desired_username = UserManager::create_username($firstname, $lastname, $language, $encoding);
  47. if (UserManager::is_username_available($desired_username.$sufix)) {
  48. break;
  49. } else {
  50. $i++;
  51. }
  52. }
  53. $username_array = array('username' => $desired_username , 'sufix' => $sufix);
  54. return $username_array;
  55. } else {
  56. $username_array = array('username' => $username, 'sufix' => '');
  57. return $username_array;
  58. }
  59. }
  60. /**
  61. * Checks if there are repeted users in a given array
  62. * @param array $usernames list of the usernames in the uploaded file
  63. * @param array $user_array['username'] and $user_array['sufix'] where sufix is the number part in a login i.e -> jmontoya2
  64. * @return array with the $usernames array and the $user_array array
  65. * @author Julio Montoya Armas
  66. */
  67. function check_user_in_array($usernames, $user_array) {
  68. $user_list = array_keys($usernames);
  69. $username = $user_array['username'].$user_array['sufix'];
  70. if (in_array($username, $user_list)) {
  71. $user_array['sufix'] += $usernames[$username];
  72. $usernames[$username]++;
  73. } else {
  74. $usernames[$username] = 1;
  75. }
  76. $result_array = array($usernames, $user_array);
  77. return $result_array;
  78. }
  79. /**
  80. * Checks whether a username has been already subscribed in a session.
  81. * @param string a given username
  82. * @param array the array with the course list codes
  83. * @param the session id
  84. * @return 0 if the user is not subscribed otherwise it returns the user_id of the given username
  85. * @author Julio Montoya Armas
  86. */
  87. function user_available_in_session($username, $course_list, $id_session) {
  88. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  89. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  90. foreach($course_list as $enreg_course) {
  91. $sql_select = "SELECT u.user_id FROM $tbl_session_rel_course_rel_user rel INNER JOIN $table_user u
  92. on (rel.id_user=u.user_id)
  93. WHERE rel.id_session='$id_session' AND u.status='5' AND u.username ='$username' AND rel.course_code='$enreg_course'";
  94. $rs = Database::query($sql_select, __FILE__, __LINE__);
  95. if (Database::num_rows($rs) > 0) {
  96. return Database::result($rs, 0, 0);
  97. } else {
  98. return 0;
  99. }
  100. }
  101. }
  102. /**
  103. This function checks whether some users in the uploaded file repeated and creates unique usernames if necesary.
  104. A case: Within the file there is an user repeted twice (Julio Montoya / Julio Montoya) and the username fields are empty.
  105. Then, this function would create unique usernames based on the first and the last name. Two users wiould be created - jmontoya and jmontoya2.
  106. Of course, if in the database there is a user with the name jmontoya, the newly created two users registered would be jmontoya2 and jmontoya3.
  107. @param $users list of users
  108. @author Julio Montoya Armas
  109. */
  110. function check_all_usernames($users, $course_list, $id_session) {
  111. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  112. $usernames = array();
  113. $new_users = array();
  114. foreach ($users as $index => $user) {
  115. $desired_username = array();
  116. if (empty($user['UserName'])) {
  117. $desired_username = make_username($user['FirstName'], $user['LastName'], '');
  118. $pre_username = $desired_username['username'].$desired_username['sufix'];
  119. $user['UserName'] = $pre_username;
  120. $user['create'] = '1';
  121. } else {
  122. if (UserManager::is_username_available($user['UserName'])) {
  123. $desired_username = make_username($user['FirstName'], $user['LastName'], $user['UserName']);
  124. $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  125. $user['create'] = '1';
  126. } else {
  127. $is_session_avail = user_available_in_session($user['UserName'], $course_list, $id_session);
  128. if ($is_session_avail == 0) {
  129. $user_name = $user['UserName'];
  130. $sql_select = "SELECT user_id FROM $table_user WHERE username ='$user_name' ";
  131. $rs = Database::query($sql_select, __FILE__, __LINE__);
  132. $user['create'] = Database::result($rs, 0, 0); // This should be the ID because the user exists.
  133. } else {
  134. $user['create'] = $is_session_avail;
  135. }
  136. }
  137. }
  138. // Usernames is the current list of users in the file.
  139. $result_array = check_user_in_array($usernames, $desired_username);
  140. $usernames = $result_array[0];
  141. $desired_username = $result_array[1];
  142. $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  143. $new_users[] = $user;
  144. }
  145. return $new_users;
  146. }
  147. /**
  148. * This functions checks whether there are users that are already registered in the DB by different creator than the current coach.
  149. * @param string a given username
  150. * @param array the array with the course list codes
  151. * @param the session id
  152. * @author Julio Montoya Armas
  153. */
  154. function get_user_creator($users, $course_list, $id_session) {
  155. $errors = array();
  156. foreach ($users as $index => $user) {
  157. // database table definition
  158. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  159. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  160. $username = $user['UserName'];
  161. //echo "<br>";
  162. $sql = "SELECT creator_id FROM $table_user WHERE username='$username' ";
  163. $rs = Database::query($sql, __FILE__, __LINE__);
  164. $creator_id = Database::result($rs, 0, 0);
  165. // check if we are the creators or not
  166. if ($creator_id != '') {
  167. if ($creator_id != api_get_user_id()) {
  168. $user['error'] = get_lang('UserAlreadyRegisteredByOtherCreator');
  169. $errors[] = $user;
  170. }
  171. }
  172. }
  173. return $errors;
  174. }
  175. /**
  176. * Validates imported data.
  177. * @param list of users
  178. */
  179. function validate_data($users, $id_session = null) {
  180. $errors = array();
  181. $usernames = array();
  182. $new_users = array();
  183. foreach ($users as $index => $user) {
  184. // 1. Check whether mandatory fields are set.
  185. $mandatory_fields = array('LastName', 'FirstName');
  186. if (api_get_setting('registration', 'email') == 'true') {
  187. $mandatory_fields[] = 'Email';
  188. }
  189. foreach ($mandatory_fields as $key => $field) {
  190. if (!isset ($user[$field]) || strlen($user[$field]) == 0) {
  191. $user['error'] = get_lang($field.'Mandatory');
  192. $errors[] = $user;
  193. }
  194. }
  195. // 2. Check whether the username is too long.
  196. if (UserManager::is_username_too_long($user['UserName'])) {
  197. $user['error'] = get_lang('UserNameTooLong');
  198. $errors[] = $user;
  199. }
  200. $user['UserName'] = trim($user['UserName']);
  201. if (empty($user['UserName'])) {
  202. $user['UserName'] = UserManager::create_username($user['FirstName'], $user['LastName']);
  203. }
  204. $new_users[] = $user;
  205. }
  206. $results = array('errors' => $errors, 'users' => $new_users);
  207. return $results;
  208. }
  209. /**
  210. * Adds missing user-information (which isn't required, like password, etc).
  211. */
  212. function complete_missing_data($user) {
  213. // 1. Generate a password if it is necessary.
  214. if (!isset ($user['Password']) || strlen($user['Password']) == 0) {
  215. $user['Password'] = api_generate_password();
  216. }
  217. return $user;
  218. }
  219. /**
  220. * Saves imported data.
  221. */
  222. function save_data($users, $course_list, $id_session) {
  223. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  224. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  225. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  226. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  227. $sendMail = $_POST['sendMail'] ? 1 : 0;
  228. // Adding users to the platform.
  229. $new_users = array();
  230. foreach ($users as $index => $user) {
  231. $user = complete_missing_data($user);
  232. // coach only will registered users
  233. $default_status = '5';
  234. if ($user['create'] == '1') {
  235. $user['id'] = UserManager :: create_user($user['FirstName'], $user['LastName'], $default_status, $user['Email'], $user['UserName'], $user['Password'], $user['OfficialCode'], api_get_setting('PlatformLanguage'), $user['PhoneNumber'], '');
  236. $user['added_at_platform'] = 1;
  237. } else {
  238. $user['id'] = $user['create'];
  239. $user['added_at_platform'] = 0;
  240. }
  241. $new_users[] = $user;
  242. }
  243. // Update user list.
  244. $users = $new_users;
  245. // Inserting users.
  246. $super_list = array();
  247. foreach ($course_list as $enreg_course) {
  248. $nbr_users = 0;
  249. $new_users = array();
  250. foreach ($users as $index => $user) {
  251. $userid = $user['id'];
  252. $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$userid')";
  253. $course_session = array('course' => $enreg_course, 'added' => 1);
  254. //$user['added_at_session'] = $course_session;
  255. Database::query($sql, __FILE__, __LINE__);
  256. if (Database::affected_rows()) {
  257. $nbr_users++;
  258. }
  259. $new_users[] = $user;
  260. }
  261. $super_list[] = $new_users;
  262. //update the nbr_users field
  263. $sql_select = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course'";
  264. $rs = Database::query($sql_select, __FILE__, __LINE__);
  265. list($nbr_users) = Database::fetch_array($rs);
  266. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'";
  267. Database::query($sql_update , __FILE__, __LINE__);
  268. $sql_update = "UPDATE $tbl_session SET nbr_users= '$nbr_users' WHERE id='$id_session'";
  269. Database::query($sql_update, __FILE__, __LINE__);
  270. }
  271. // We don't delete the users (thoughts while dreaming)
  272. //$sql_delete = "DELETE FROM $tbl_session_rel_user WHERE id_session = '$id_session'";
  273. //Database::query($sql_delete,__FILE__, __LINE__);
  274. $new_users = array();
  275. foreach ($users as $index => $user) {
  276. $userid = $user['id'];
  277. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$userid')";
  278. Database::query($sql_insert, __FILE__, __LINE__);
  279. $user['added_at_session'] = 1;
  280. $new_users[] = $user;
  281. }
  282. $users = $new_users;
  283. $registered_users = get_lang('FileImported').'<br /> Import file results : <br />';
  284. // Sending emails.
  285. $addedto = '';
  286. if ($sendMail) {
  287. $i = 0;
  288. foreach ($users as $index => $user) {
  289. $emailto = api_get_person_name($user['FirstName'], $user['LastName'], null, PERSON_NAME_EMAIL_ADDRESS).' <'.$user['Email'].'>';
  290. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  291. $emailbody = get_lang('Dear').' '.api_get_person_name($user['FirstName'], $user['LastName']).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('Settings')." $user[UserName]\n".get_lang('Pass')." : $user[Password]\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".api_get_path('WEB_PATH')." \n\n".get_lang('Problem')."\n\n".get_lang('Formula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator')."";
  292. $emailheaders = 'From: '.api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS).' <'.api_get_setting('emailAdministrator').">\n";
  293. $emailheaders .= 'Reply-To: '.api_get_setting('emailAdministrator');
  294. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  295. if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
  296. if ($user['added_at_platform'] == 1) {
  297. $addedto = get_lang('UserCreatedPlatform');
  298. } else {
  299. $addedto = ' ';
  300. }
  301. if ($user['added_at_session'] == 1) {
  302. $addedto .= get_lang('UserInSession');
  303. }
  304. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  305. } else {
  306. $addedto = get_lang('UserNotAdded');
  307. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  308. }
  309. }
  310. } else {
  311. $i = 0;
  312. foreach ($users as $index => $user) {
  313. if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
  314. if ($user['added_at_platform'] == 1) {
  315. $addedto = get_lang('UserCreatedPlatform');
  316. } else {
  317. $addedto = ' ';
  318. }
  319. if ($user['added_at_session'] == 1) {
  320. $addedto .= ' '.get_lang('UserInSession');
  321. }
  322. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  323. } else {
  324. $addedto = get_lang('UserNotAdded');
  325. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  326. }
  327. }
  328. }
  329. header('Location: course.php?id_session='.$id_session.'&action=show_message&message='.urlencode($registered_users));
  330. exit ();
  331. //header('Location: resume_session.php?id_session='.$id_session);
  332. }
  333. /**
  334. * Reads CSV-file.
  335. * @param string $file Path to the CSV-file
  336. * @return array All userinformation read from the file
  337. */
  338. function parse_csv_data($file) {
  339. $users = Import :: csv_to_array($file);
  340. foreach ($users as $index => $user) {
  341. if (isset ($user['Courses'])) {
  342. $user['Courses'] = explode('|', trim($user['Courses']));
  343. }
  344. $users[$index] = $user;
  345. }
  346. return $users;
  347. }
  348. /**
  349. * XML-parser: the handler at the beginning of element.
  350. */
  351. function element_start($parser, $data) {
  352. $data = api_utf8_decode($data);
  353. global $user;
  354. global $current_tag;
  355. switch ($data) {
  356. case 'Contact' :
  357. $user = array ();
  358. break;
  359. default :
  360. $current_tag = $data;
  361. }
  362. }
  363. /**
  364. * XML-parser: the handler at the end of element.
  365. */
  366. function element_end($parser, $data) {
  367. $data = api_utf8_decode($data);
  368. global $user;
  369. global $users;
  370. global $current_value;
  371. global $purification_option_for_usernames;
  372. $user[$data] = $current_value;
  373. switch ($data) {
  374. case 'Contact' :
  375. $user['UserName'] = UserManager::purify_username($user['UserName'], $purification_option_for_usernames);
  376. $users[] = $user;
  377. break;
  378. default :
  379. $user[$data] = $current_value;
  380. break;
  381. }
  382. }
  383. /**
  384. * XML-parser: the handler for character data.
  385. */
  386. function character_data($parser, $data) {
  387. $data = trim(api_utf8_decode($data));
  388. global $current_value;
  389. $current_value = $data;
  390. }
  391. /**
  392. * Reads XML-file.
  393. * @param string $file Path to the XML-file
  394. * @return array All userinformation read from the file
  395. */
  396. function parse_xml_data($file) {
  397. global $current_tag;
  398. global $current_value;
  399. global $user;
  400. global $users;
  401. $users = array ();
  402. $parser = xml_parser_create('UTF-8');
  403. xml_set_element_handler($parser, 'element_start', 'element_end');
  404. xml_set_character_data_handler($parser, "character_data");
  405. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  406. xml_parse($parser, api_utf8_encode_xml(file_get_contents($file)));
  407. xml_parser_free($parser);
  408. return $users;
  409. }
  410. /*
  411. ==============================================================================
  412. Main script
  413. ==============================================================================
  414. */
  415. $language_file = array ('admin', 'registration', 'index', 'trad4all', 'tracking');
  416. $cidReset = true;
  417. require '../inc/global.inc.php';
  418. $this_section = SECTION_PLATFORM_ADMIN; // TODO: Platform admin section?
  419. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  420. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  421. require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
  422. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  423. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  424. $tool_name = get_lang('ImportUserListXMLCSV');
  425. api_block_anonymous_users();
  426. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('MySpace'));
  427. $id_session = '';
  428. if (isset($_GET['id_session']) && $_GET['id_session'] != '') {
  429. $id_session = Security::remove_XSS($_GET['id_session']);
  430. $interbreadcrumb[] = array ('url' => 'session.php', 'name' => get_lang('Sessions'));
  431. $interbreadcrumb[] = array ('url' => 'course.php?id_session='.$_GET['id_session'].'', 'name' => get_lang('Course'));
  432. }
  433. // Set this option to true to enforce strict purification for usenames.
  434. $purification_option_for_usernames = false;
  435. /*
  436. // Checking whether the current coach is the admin coach.
  437. if (!api_is_coach()) {
  438. api_not_allowed(true);
  439. }
  440. */
  441. // Checking whether the current coach is the admin coach.
  442. if (api_get_setting('add_users_by_coach') == 'true') {
  443. if (!api_is_platform_admin()) {
  444. if (isset($_REQUEST['id_session'])) {
  445. $id_session = $_REQUEST['id_session'];
  446. $sql = 'SELECT id_coach FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_session;
  447. $rs = Database::query($sql, __FILE__, __LINE__);
  448. if (Database::result($rs, 0, 0) != $_user['user_id']) {
  449. api_not_allowed(true);
  450. }
  451. } else {
  452. api_not_allowed(true);
  453. }
  454. }
  455. } else {
  456. api_not_allowed(true);
  457. }
  458. set_time_limit(0);
  459. if ($_POST['formSent'] && $_FILES['import_file']['size'] !== 0) {
  460. $file_type = $_POST['file_type'];
  461. $id_session = $_POST['id_session'];
  462. if ($file_type == 'csv') {
  463. $users = parse_csv_data($_FILES['import_file']['tmp_name']);
  464. } else {
  465. $users = parse_xml_data($_FILES['import_file']['tmp_name']);
  466. }
  467. if (count($users) > 0) {
  468. $results = validate_data($users);
  469. $errors = $results['errors'];
  470. $users = $results['users'];
  471. if (count($errors) == 0) {
  472. if (!empty($id_session)) {
  473. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  474. // Selecting all the courses from the session id requested.
  475. $sql = "SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'";
  476. $result = Database::query($sql, __FILE__, __LINE__);
  477. $course_list = array();
  478. while ($row = Database::fetch_array($result)) {
  479. $course_list[] = $row['course_code'];
  480. }
  481. $errors = get_user_creator($users, $course_list, $id_session);
  482. $users = check_all_usernames($users, $course_list, $id_session);
  483. if (count($errors) == 0) {
  484. save_data($users, $course_list, $id_session);
  485. }
  486. } else {
  487. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoSessionId')));
  488. }
  489. }
  490. } else {
  491. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoUsersRead')));
  492. }
  493. }
  494. Display :: display_header($tool_name);
  495. if ($_FILES['import_file']['size'] == 0 && $_POST) {
  496. Display::display_error_message(get_lang('ThisFieldIsRequired'));
  497. }
  498. if (count($errors) != 0) {
  499. $error_message = '<ul>';
  500. foreach ($errors as $index => $error_user) {
  501. $error_message .= '<li><strong>'.$error_user['error'].'</strong>: ';
  502. $error_message .= api_get_person_name($error_user['FirstName'], $error_user['LastName']);
  503. $error_message .= '</li>';
  504. }
  505. $error_message .= '</ul>';
  506. Display :: display_error_message($error_message, false);
  507. }
  508. $form = new FormValidator('user_import');
  509. $form->addElement('hidden', 'formSent');
  510. $form->addElement('hidden', 'id_session',$id_session);
  511. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  512. $form->addRule('import_file', get_lang('ThisFieldIsRequired'), 'required');
  513. $allowed_file_types = array ('xml', 'csv');
  514. $form->addRule('import_file', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  515. $form->addElement('radio', 'file_type', get_lang('FileType'), 'XML (<a href="exemple.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)', 'xml');
  516. $form->addElement('radio', 'file_type', null, 'CSV (<a href="exemple.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', 'csv');
  517. $form->addElement('radio', 'sendMail', get_lang('SendMailToUsers'), get_lang('Yes'), 1);
  518. $form->addElement('radio', 'sendMail', null, get_lang('No'), 0);
  519. $form->addElement('submit', 'submit', get_lang('Ok'));
  520. $defaults['formSent'] = 1;
  521. $defaults['sendMail'] = 0;
  522. $defaults['file_type'] = 'xml';
  523. $form->setDefaults($defaults);
  524. $form->display();
  525. /*
  526. <?php echo implode('/',$defined_auth_sources); ?>
  527. &lt;AuthSource&gt;<?php echo implode('/',$defined_auth_sources); ?>&lt;/AuthSource&gt;
  528. */
  529. ?>
  530. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  531. <blockquote>
  532. <pre>
  533. <b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;OfficialCode;PhoneNumber;
  534. <b>Montoya</b>;<b>Julio</b>;<b>info@localhost</b>;jmontoya;123456789;code1;3141516
  535. <b>Doewing</b>;<b>Johny</b>;<b>info@localhost</b>;jdoewing;123456789;code2;3141516
  536. </pre>
  537. </blockquote>
  538. <p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  539. <blockquote>
  540. <pre>
  541. &lt;?xml version=&quot;1.0&quot; encoding=&quot;<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>&quot;?&gt;
  542. &lt;Contacts&gt;
  543. &lt;Contact&gt;
  544. <b>&lt;LastName&gt;Montoya&lt;/LastName&gt;</b>
  545. <b>&lt;FirstName&gt;Julio&lt;/FirstName&gt;</b>
  546. <b>&lt;Email&gt;info@localhost&lt;/Email&gt;</b>
  547. &lt;UserName&gt;jmontoya&lt;/UserName&gt;
  548. &lt;Password&gt;123456&lt;/Password&gt;
  549. &lt;OfficialCode&gt;code1&lt;/OfficialCode&gt;
  550. &lt;PhoneNumber&gt;3141516&lt;/PhoneNumber&gt;
  551. &lt;/Contact&gt;
  552. &lt;/Contacts&gt;
  553. </pre>
  554. </blockquote>
  555. <?php
  556. /*
  557. ==============================================================================
  558. FOOTER
  559. ==============================================================================
  560. */
  561. Display :: display_footer();