user_import.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
  4. use ChamiloSession as Session;
  5. /**
  6. * This tool allows platform admins to add users by uploading a CSV or XML file.
  7. *
  8. * @package chamilo.admin
  9. */
  10. $cidReset = true;
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. // Set this option to true to enforce strict purification for usenames.
  13. $purification_option_for_usernames = false;
  14. $userId = api_get_user_id();
  15. /**
  16. * @param array $users
  17. * @param bool $checkUniqueEmail
  18. *
  19. * @return array
  20. */
  21. function validate_data($users, $checkUniqueEmail = false)
  22. {
  23. global $defined_auth_sources;
  24. $usernames = [];
  25. // 1. Check if mandatory fields are set.
  26. $mandatory_fields = ['LastName', 'FirstName'];
  27. if (api_get_setting('registration', 'email') == 'true' || $checkUniqueEmail) {
  28. $mandatory_fields[] = 'Email';
  29. }
  30. $classExistList = [];
  31. $usergroup = new UserGroup();
  32. foreach ($users as &$user) {
  33. $user['has_error'] = false;
  34. $user['message'] = '';
  35. foreach ($mandatory_fields as $field) {
  36. if (empty($user[$field])) {
  37. $user['message'] .= Display::return_message(get_lang($field.'Mandatory'), 'warning');
  38. $user['has_error'] = true;
  39. }
  40. }
  41. $username = isset($user['UserName']) ? $user['UserName'] : '';
  42. // 2. Check username, first, check whether it is empty.
  43. if (!UserManager::is_username_empty($username)) {
  44. // 2.1. Check whether username is too long.
  45. if (UserManager::is_username_too_long($username)) {
  46. $user['message'] .= Display::return_message(get_lang('UserNameTooLong'), 'warning');
  47. $user['has_error'] = true;
  48. }
  49. // 2.1.1
  50. $hasDash = strpos($username, '-');
  51. if ($hasDash !== false) {
  52. $user['message'] .= Display::return_message(get_lang('UserNameHasDash'), 'warning');
  53. $user['has_error'] = true;
  54. }
  55. // 2.2. Check whether the username was used twice in import file.
  56. if (isset($usernames[$username])) {
  57. $user['message'] .= Display::return_message(get_lang('UserNameUsedTwice'), 'warning');
  58. $user['has_error'] = true;
  59. }
  60. $usernames[$username] = 1;
  61. // 2.3. Check whether username is already occupied.
  62. if (!UserManager::is_username_available($username)) {
  63. $user['message'] .= Display::return_message(get_lang('UserNameNotAvailable'), 'warning');
  64. $user['has_error'] = true;
  65. }
  66. }
  67. if (isset($user['Email'])) {
  68. $result = api_valid_email($user['Email']);
  69. if ($result === false) {
  70. $user['message'] .= Display::return_message(get_lang('PleaseEnterValidEmail'), 'warning');
  71. $user['has_error'] = true;
  72. }
  73. }
  74. if ($checkUniqueEmail) {
  75. if (isset($user['Email'])) {
  76. $userFromEmail = api_get_user_info_from_email($user['Email']);
  77. if (!empty($userFromEmail)) {
  78. $user['message'] .= Display::return_message(get_lang('EmailUsedTwice'), 'warning');
  79. $user['has_error'] = true;
  80. }
  81. }
  82. }
  83. // 3. Check status.
  84. if (isset($user['Status']) && !api_status_exists($user['Status'])) {
  85. $user['message'] .= Display::return_message(get_lang('WrongStatus'), 'warning');
  86. $user['has_error'] = true;
  87. }
  88. // 4. Check ClassId
  89. if (!empty($user['ClassId'])) {
  90. $classId = explode('|', trim($user['ClassId']));
  91. foreach ($classId as $id) {
  92. if (in_array($id, $classExistList)) {
  93. continue;
  94. }
  95. $info = $usergroup->get($id);
  96. if (empty($info)) {
  97. $user['message'] .= Display::return_message(
  98. sprintf(get_lang('ClassIdDoesntExists'), $id),
  99. 'warning'
  100. );
  101. $user['has_error'] = true;
  102. } else {
  103. $classExistList[] = $info['id'];
  104. }
  105. }
  106. }
  107. // 5. Check authentication source
  108. if (!empty($user['AuthSource'])) {
  109. if (!in_array($user['AuthSource'], $defined_auth_sources)) {
  110. $user['message'] .= Display::return_message(get_lang('AuthSourceNotAvailable'), 'warning');
  111. $user['has_error'] = true;
  112. }
  113. }
  114. }
  115. return $users;
  116. }
  117. /**
  118. * Add missing user-information (which isn't required, like password, username etc).
  119. *
  120. * @param array $user
  121. */
  122. function complete_missing_data($user)
  123. {
  124. global $purification_option_for_usernames;
  125. $username = isset($user['UserName']) ? $user['UserName'] : '';
  126. // 1. Create a username if necessary.
  127. if (UserManager::is_username_empty($username)) {
  128. $user['UserName'] = UserManager::create_unique_username(
  129. $user['FirstName'],
  130. $user['LastName']
  131. );
  132. } else {
  133. $user['UserName'] = UserManager::purify_username(
  134. $user['UserName'],
  135. $purification_option_for_usernames
  136. );
  137. }
  138. // 2. Generate a password if necessary.
  139. if (empty($user['Password'])) {
  140. $user['Password'] = api_generate_password();
  141. }
  142. // 3. Set status if not allready set.
  143. if (empty($user['Status'])) {
  144. $user['Status'] = 'user';
  145. }
  146. // 4. Set authsource if not allready set.
  147. if (empty($user['AuthSource'])) {
  148. $user['AuthSource'] = PLATFORM_AUTH_SOURCE;
  149. }
  150. if (empty($user['ExpiryDate'])) {
  151. $user['ExpiryDate'] = '';
  152. }
  153. if (!isset($user['OfficialCode'])) {
  154. $user['OfficialCode'] = '';
  155. }
  156. if (!isset($user['language'])) {
  157. $user['language'] = '';
  158. }
  159. if (!isset($user['PhoneNumber'])) {
  160. $user['PhoneNumber'] = '';
  161. }
  162. if (!isset($user['OfficialCode'])) {
  163. $user['OfficialCode'] = '';
  164. }
  165. return $user;
  166. }
  167. /**
  168. * Save the imported data.
  169. *
  170. * @param array $users List of users
  171. * @param bool $sendMail
  172. *
  173. * @uses \global variable $inserted_in_course, which returns the list of
  174. * courses the user was inserted in
  175. */
  176. function save_data($users, $sendMail = false)
  177. {
  178. global $inserted_in_course, $extra_fields;
  179. // Not all scripts declare the $inserted_in_course array (although they should).
  180. if (!isset($inserted_in_course)) {
  181. $inserted_in_course = [];
  182. }
  183. $usergroup = new UserGroup();
  184. if (is_array($users)) {
  185. $efo = new ExtraFieldOption('user');
  186. $optionsByField = [];
  187. foreach ($users as &$user) {
  188. if ($user['has_error']) {
  189. continue;
  190. }
  191. $user = complete_missing_data($user);
  192. $user['Status'] = api_status_key($user['Status']);
  193. $user_id = UserManager::create_user(
  194. $user['FirstName'],
  195. $user['LastName'],
  196. $user['Status'],
  197. $user['Email'],
  198. $user['UserName'],
  199. $user['Password'],
  200. $user['OfficialCode'],
  201. $user['language'],
  202. $user['PhoneNumber'],
  203. '',
  204. $user['AuthSource'],
  205. $user['ExpiryDate'],
  206. 1,
  207. 0,
  208. null,
  209. null,
  210. $sendMail
  211. );
  212. if ($user_id) {
  213. $returnMessage = Display::return_message(get_lang('UserAdded'), 'success');
  214. if (isset($user['Courses']) && is_array($user['Courses'])) {
  215. foreach ($user['Courses'] as $course) {
  216. if (CourseManager::course_exists($course)) {
  217. $result = CourseManager::subscribeUser($user_id, $course, $user['Status']);
  218. if ($result) {
  219. $course_info = api_get_course_info($course);
  220. $inserted_in_course[$course] = $course_info['title'];
  221. }
  222. }
  223. }
  224. }
  225. if (isset($user['Sessions']) && is_array($user['Sessions'])) {
  226. foreach ($user['Sessions'] as $sessionId) {
  227. $sessionInfo = api_get_session_info($sessionId);
  228. if (!empty($sessionInfo)) {
  229. SessionManager::subscribeUsersToSession(
  230. $sessionId,
  231. [$user_id],
  232. SESSION_VISIBLE_READ_ONLY,
  233. false
  234. );
  235. }
  236. }
  237. }
  238. if (!empty($user['ClassId'])) {
  239. $classId = explode('|', trim($user['ClassId']));
  240. foreach ($classId as $id) {
  241. $usergroup->subscribe_users_to_usergroup($id, [$user_id], false);
  242. }
  243. }
  244. // We are sure that the extra field exists.
  245. foreach ($extra_fields as $extras) {
  246. if (!isset($user[$extras[1]])) {
  247. continue;
  248. }
  249. $key = $extras[1];
  250. $value = $user[$key];
  251. if (!array_key_exists($key, $optionsByField)) {
  252. $optionsByField[$key] = $efo->getOptionsByFieldVariable($key);
  253. }
  254. /** @var ExtraFieldOptions $option */
  255. foreach ($optionsByField[$key] as $option) {
  256. if ($option->getDisplayText() === $value) {
  257. $value = $option->getValue();
  258. }
  259. }
  260. UserManager::update_extra_field_value($user_id, $key, $value);
  261. }
  262. } else {
  263. $returnMessage = Display::return_message(get_lang('Error'), 'warning');
  264. }
  265. $user['message'] = $returnMessage;
  266. }
  267. }
  268. return $users;
  269. }
  270. /**
  271. * @param array $users
  272. * @param string $fileName
  273. * @param int $sendEmail
  274. * @param bool $checkUniqueEmail
  275. * @param bool $resumeImport
  276. *
  277. * @return array
  278. */
  279. function parse_csv_data($users, $fileName, $sendEmail = 0, $checkUniqueEmail = true, $resumeImport = false)
  280. {
  281. $usersFromOrigin = $users;
  282. $allowRandom = api_get_configuration_value('generate_random_login');
  283. if ($allowRandom) {
  284. $factory = new RandomLib\Factory();
  285. $generator = $factory->getLowStrengthGenerator();
  286. $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  287. }
  288. $readMax = 50;
  289. $userId = api_get_user_id();
  290. $logMessages = '';
  291. $importData = Session::read('user_import_data_'.$userId);
  292. if (!empty($importData)) {
  293. $counter = $importData['counter'];
  294. $users = $importData['complete_list'];
  295. $users = array_splice($users, $counter, $readMax);
  296. $logMessages = $importData['log_messages'];
  297. } else {
  298. $users = array_splice($users, 0, $readMax);
  299. }
  300. if ($resumeImport === false) {
  301. $users = $usersFromOrigin;
  302. }
  303. $counter = 0;
  304. foreach ($users as $index => $user) {
  305. if ($resumeImport) {
  306. if ($counter >= $readMax) {
  307. $users = array_splice($users, $counter, $readMax);
  308. break;
  309. }
  310. }
  311. $counter++;
  312. if (empty($user['UserName'])) {
  313. if ($allowRandom) {
  314. $username = $generator->generateString(10, $chars);
  315. $user['UserName'] = $username;
  316. }
  317. }
  318. if (isset($user['Courses'])) {
  319. $user['Courses'] = explode('|', trim($user['Courses']));
  320. }
  321. if (isset($user['Sessions'])) {
  322. $user['Sessions'] = explode('|', trim($user['Sessions']));
  323. }
  324. // Lastname is needed.
  325. if (!isset($user['LastName']) || (isset($user['LastName']) && empty($user['LastName']))) {
  326. unset($users[$index]);
  327. continue;
  328. }
  329. // FirstName is needed.
  330. if (!isset($user['FirstName']) || (isset($user['FirstName']) && empty($user['FirstName']))) {
  331. unset($users[$index]);
  332. continue;
  333. }
  334. $users[$index] = $user;
  335. }
  336. $globalCounter = $counter;
  337. if (!empty($importData)) {
  338. $globalCounter = $importData['counter'] + $counter;
  339. }
  340. $importData = [
  341. 'complete_list' => $usersFromOrigin,
  342. 'filename' => $fileName,
  343. 'counter' => $globalCounter,
  344. 'check_unique_email' => $checkUniqueEmail,
  345. 'send_email' => $sendEmail,
  346. 'date' => api_get_utc_datetime(),
  347. 'log_messages' => $logMessages,
  348. 'resume' => $resumeImport,
  349. ];
  350. Session::write('user_import_data_'.$userId, $importData);
  351. return $users;
  352. }
  353. /**
  354. * Read the XML-file.
  355. *
  356. * @param string $file Path to the XML-file
  357. *
  358. * @return array All user information read from the file
  359. */
  360. function parse_xml_data($file)
  361. {
  362. $crawler = new \Symfony\Component\DomCrawler\Crawler();
  363. $crawler->addXmlContent(file_get_contents($file));
  364. $crawler = $crawler->filter('Contacts > Contact ');
  365. $array = [];
  366. foreach ($crawler as $domElement) {
  367. $row = [];
  368. foreach ($domElement->childNodes as $node) {
  369. if ($node->nodeName != '#text') {
  370. $row[$node->nodeName] = $node->nodeValue;
  371. }
  372. }
  373. if (!empty($row)) {
  374. $array[] = $row;
  375. }
  376. }
  377. return $array;
  378. }
  379. /**
  380. * @param array $users
  381. * @param bool $sendMail
  382. */
  383. function processUsers(&$users, $sendMail)
  384. {
  385. $users = save_data($users, $sendMail);
  386. $warningMessage = '';
  387. if (!empty($users)) {
  388. $table = new HTML_Table(['class' => 'table table-responsive']);
  389. $headers = [
  390. get_lang('User'),
  391. get_lang('Status'),
  392. ];
  393. $row = 0;
  394. $column = 0;
  395. foreach ($headers as $header) {
  396. $table->setHeaderContents($row, $column, $header);
  397. $column++;
  398. }
  399. $row++;
  400. foreach ($users as $user) {
  401. $column = 0;
  402. $email = isset($user['Email']) ? ' - '.$user['Email'] : null;
  403. $userData =
  404. '<strong>'.$user['UserName'].'</strong> - '.
  405. api_get_person_name(
  406. $user['FirstName'],
  407. $user['LastName']
  408. ).' '.$email;
  409. $table->setCellContents($row, $column, $userData);
  410. $table->setCellContents($row, ++$column, $user['message']);
  411. $row++;
  412. }
  413. $warningMessage = $table->toHtml();
  414. }
  415. // if the warning message is too long then we display the warning message trough a session
  416. Display::addFlash(Display::return_message(get_lang('FileImported'), 'confirmation', false));
  417. $importData = Session::read('user_import_data_'.api_get_user_id());
  418. if (!empty($importData)) {
  419. if (isset($importData['log_messages'])) {
  420. $importData['log_messages'] .= $warningMessage;
  421. } else {
  422. $importData['log_messages'] = $warningMessage;
  423. }
  424. Session::write('user_import_data_'.api_get_user_id(), $importData);
  425. }
  426. }
  427. $this_section = SECTION_PLATFORM_ADMIN;
  428. api_protect_admin_script(true, null);
  429. api_protect_limit_for_session_admin();
  430. set_time_limit(0);
  431. $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
  432. if (isset($extAuthSource) && is_array($extAuthSource)) {
  433. $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
  434. }
  435. $tool_name = get_lang('ImportUserListXMLCSV');
  436. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  437. $reloadImport = (isset($_REQUEST['reload_import']) && (int) $_REQUEST['reload_import'] === 1);
  438. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', true);
  439. if (isset($_POST['formSent']) && $_POST['formSent'] && $_FILES['import_file']['size'] !== 0) {
  440. $file_type = $_POST['file_type'];
  441. Security::clear_token();
  442. $tok = Security::get_token();
  443. $allowed_file_mimetype = ['csv', 'xml'];
  444. $error_kind_file = true;
  445. $checkUniqueEmail = isset($_POST['check_unique_email']) ? $_POST['check_unique_email'] : null;
  446. $sendMail = $_POST['sendMail'] ? true : false;
  447. $resume = isset($_POST['resume_import']) ? true : false;
  448. $uploadInfo = pathinfo($_FILES['import_file']['name']);
  449. $ext_import_file = $uploadInfo['extension'];
  450. $users = [];
  451. if (in_array($ext_import_file, $allowed_file_mimetype)) {
  452. if (strcmp($file_type, 'csv') === 0 &&
  453. $ext_import_file == $allowed_file_mimetype[0]
  454. ) {
  455. Session::erase('user_import_data_'.$userId);
  456. $users = Import::csvToArray($_FILES['import_file']['tmp_name']);
  457. $users = parse_csv_data(
  458. $users,
  459. $_FILES['import_file']['name'],
  460. $sendMail,
  461. $checkUniqueEmail,
  462. $resume
  463. );
  464. $users = validate_data($users, $checkUniqueEmail);
  465. $error_kind_file = false;
  466. } elseif (strcmp($file_type, 'xml') === 0 && $ext_import_file == $allowed_file_mimetype[1]) {
  467. $users = parse_xml_data($_FILES['import_file']['tmp_name']);
  468. $users = validate_data($users, $checkUniqueEmail);
  469. $error_kind_file = false;
  470. }
  471. processUsers($users, $sendMail);
  472. if ($error_kind_file) {
  473. Display::addFlash(
  474. Display::return_message(
  475. get_lang('YouMustImportAFileAccordingToSelectedOption'),
  476. 'error',
  477. false
  478. )
  479. );
  480. } else {
  481. $reload = '';
  482. if ($resume) {
  483. $reload = '?reload_import=1';
  484. }
  485. header('Location: '.api_get_self().$reload);
  486. exit;
  487. }
  488. } else {
  489. Display::addFlash(
  490. Display::return_message(
  491. get_lang('YouMustImportAFileAccordingToSelectedOption'),
  492. 'error',
  493. false
  494. )
  495. );
  496. //header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_list.php?sec_token='.$tok);
  497. header('Location: '.api_get_self());
  498. exit;
  499. }
  500. }
  501. $importData = Session::read('user_import_data_'.$userId);
  502. $formContinue = false;
  503. $resumeStop = true;
  504. if (!empty($importData)) {
  505. $isResume = $importData['resume'];
  506. $formContinue = new FormValidator('user_import_continue', 'post', api_get_self());
  507. $label = get_lang('Results');
  508. if ($isResume) {
  509. $label = get_lang('ContinueLastImport');
  510. }
  511. $formContinue->addHeader($label);
  512. $formContinue->addLabel(get_lang('File'), $importData['filename']);
  513. $resumeStop = true;
  514. if ($isResume) {
  515. $totalUsers = isset($importData['complete_list']) ? count($importData['complete_list']) : 0;
  516. $counter = isset($importData['counter']) ? $importData['counter'] : 0;
  517. $bar = '';
  518. if (!empty($totalUsers)) {
  519. $bar = Display::bar_progress($counter / $totalUsers * 100);
  520. }
  521. $formContinue->addLabel(get_lang('Status'), $bar);
  522. $formContinue->addLabel(
  523. get_lang('UsersAdded'),
  524. $importData['counter'].' / '.count($importData['complete_list'])
  525. );
  526. } else {
  527. $formContinue->addLabel(
  528. get_lang('Users'),
  529. count($importData['complete_list'])
  530. );
  531. }
  532. $formContinue->addLabel(
  533. get_lang('CheckUniqueEmail'),
  534. $importData['check_unique_email'] ? get_lang('Yes') : get_lang('No')
  535. );
  536. $formContinue->addLabel(get_lang('SendMailToUsers'), $importData['send_email'] ? get_lang('Yes') : get_lang('No'));
  537. $formContinue->addLabel(get_lang('Date'), Display::dateToStringAgoAndLongDate($importData['date']));
  538. if ($isResume) {
  539. $resumeStop = $importData['counter'] >= count($importData['complete_list']);
  540. if ($resumeStop == false) {
  541. $formContinue->addButtonImport(get_lang('ContinueImport'), 'import_continue');
  542. }
  543. }
  544. $formContinue->addHtml(get_lang('Results').'<br />'.$importData['log_messages']);
  545. if ($formContinue->validate()) {
  546. $users = parse_csv_data(
  547. $importData['complete_list'],
  548. $importData['filename'],
  549. $importData['send_email'],
  550. $importData['check_unique_email'],
  551. true
  552. );
  553. $users = validate_data($users, $importData['check_unique_email']);
  554. processUsers($users, $importData['send_email']);
  555. $reload = '';
  556. if ($isResume && $resumeStop === false) {
  557. $reload = '?reload_import=1';
  558. }
  559. header('Location: '.api_get_self().$reload);
  560. exit;
  561. }
  562. }
  563. Display::display_header($tool_name);
  564. $form = new FormValidator('user_import', 'post', api_get_self());
  565. $form->addHeader($tool_name);
  566. $form->addElement('hidden', 'formSent');
  567. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  568. $group = [
  569. $form->createElement(
  570. 'radio',
  571. 'file_type',
  572. '',
  573. 'CSV (<a href="example.csv" target="_blank" download>'.get_lang('ExampleCSVFile').'</a>)',
  574. 'csv'
  575. ),
  576. $form->createElement(
  577. 'radio',
  578. 'file_type',
  579. null,
  580. 'XML (<a href="example.xml" target="_blank" download>'.get_lang('ExampleXMLFile').'</a>)',
  581. 'xml'
  582. ),
  583. ];
  584. $form->addGroup($group, '', get_lang('FileType'));
  585. $group = [
  586. $form->createElement('radio', 'sendMail', '', get_lang('Yes'), 1),
  587. $form->createElement('radio', 'sendMail', null, get_lang('No'), 0),
  588. ];
  589. $form->addGroup($group, '', get_lang('SendMailToUsers'));
  590. $form->addElement(
  591. 'checkbox',
  592. 'check_unique_email',
  593. '',
  594. get_lang('CheckUniqueEmail')
  595. );
  596. $form->addElement(
  597. 'checkbox',
  598. 'resume_import',
  599. '',
  600. get_lang('ResumeImport')
  601. );
  602. $form->addButtonImport(get_lang('Import'));
  603. $defaults['formSent'] = 1;
  604. $defaults['sendMail'] = 0;
  605. $defaults['file_type'] = 'csv';
  606. $extraSettings = api_get_configuration_value('user_import_settings');
  607. if (!empty($extraSettings) && isset($extraSettings['options']) &&
  608. isset($extraSettings['options']['send_mail_default_option'])
  609. ) {
  610. $defaults['sendMail'] = $extraSettings['options']['send_mail_default_option'];
  611. }
  612. $form->setDefaults($defaults);
  613. $form->display();
  614. if ($formContinue) {
  615. $formContinue->display();
  616. }
  617. if ($reloadImport) {
  618. echo '<script>
  619. $(function() {
  620. function reload() {
  621. $("#user_import_continue").submit();
  622. }
  623. setTimeout(reload, 3000);
  624. });
  625. </script>';
  626. }
  627. $list = [];
  628. $list_reponse = [];
  629. $result_xml = '';
  630. $i = 0;
  631. $count_fields = count($extra_fields);
  632. if ($count_fields > 0) {
  633. foreach ($extra_fields as $extra) {
  634. $list[] = $extra[1];
  635. $list_reponse[] = 'xxx';
  636. $spaces = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  637. $result_xml .= $spaces.'&lt;'.$extra[1].'&gt;xxx&lt;/'.$extra[1].'&gt;';
  638. if ($i != $count_fields - 1) {
  639. $result_xml .= '<br/>';
  640. }
  641. $i++;
  642. }
  643. }
  644. ?>
  645. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  646. <blockquote>
  647. <pre>
  648. <b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;AuthSource;OfficialCode;PhoneNumber;Status;ExpiryDate;<span style="color:red;"><?php if (count($list) > 0) {
  649. echo implode(';', $list).';';
  650. } ?></span>Courses;Sessions;ClassId;
  651. <b>xxx</b>;<b>xxx</b>;<b>xxx</b>;xxx;xxx;<?php echo implode('/', $defined_auth_sources); ?>;xxx;xxx;user/teacher/drh;0000-00-00 00:00:00;<span style="color:red;"><?php if (count($list_reponse) > 0) {
  652. echo implode(';', $list_reponse).';';
  653. } ?></span>xxx1|xxx2|xxx3;sessionId|sessionId|sessionId;1;<br />
  654. </pre>
  655. </blockquote>
  656. <p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  657. <blockquote>
  658. <pre>
  659. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  660. &lt;Contacts&gt;
  661. &lt;Contact&gt;
  662. <b>&lt;LastName&gt;xxx&lt;/LastName&gt;</b>
  663. <b>&lt;FirstName&gt;xxx&lt;/FirstName&gt;</b>
  664. &lt;UserName&gt;xxx&lt;/UserName&gt;
  665. &lt;Password&gt;xxx&lt;/Password&gt;
  666. &lt;AuthSource&gt;<?php echo implode('/', $defined_auth_sources); ?>&lt;/AuthSource&gt;
  667. <b>&lt;Email&gt;xxx&lt;/Email&gt;</b>
  668. &lt;OfficialCode&gt;xxx&lt;/OfficialCode&gt;
  669. &lt;PhoneNumber&gt;xxx&lt;/PhoneNumber&gt;
  670. &lt;Status&gt;user/teacher/drh<?php if ($result_xml != '') {
  671. echo '<br /><span style="color:red;">', $result_xml;
  672. echo '</span>';
  673. } ?>&lt;/Status&gt;
  674. &lt;Courses&gt;xxx1|xxx2|xxx3&lt;/Courses&gt;
  675. &lt;Sessions&gt;sessionId|sessionId|sessionId&lt;/Sessions&gt;
  676. &lt;ClassId&gt;1&lt;/ClassId&gt;
  677. &lt;/Contact&gt;
  678. &lt;/Contacts&gt;
  679. </pre>
  680. </blockquote>
  681. <?php
  682. Display::display_footer();