user_import.php 26 KB

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