user_import.php 23 KB

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