user_import.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. Makes a username i.e Julio Montoya into jmontoya
  26. we check if the lastname have < 17 characteres
  27. we give 2 variables to a possible sufix. Sufix means the last numbers of the username i.e jmontoya66
  28. @param string firstname
  29. @param string lastname
  30. @author Julio Montoya Armas
  31. */
  32. function make_login($firstname,$lastname)
  33. {
  34. $desired_username='';
  35. if (strlen($lastname)<17)
  36. {
  37. $desired_username = substr($firstname,0,1).$lastname;
  38. }
  39. else
  40. {
  41. $desired_username = substr($firstname,0,1).substr($lastname,0,16);
  42. }
  43. return strtolower($desired_username);
  44. }
  45. /**
  46. Checks if a username exist in the DB otherwise it create a "double"
  47. ie. if we look into for jmontoya but the user's name already exist we create the user jmontoya2
  48. the return array will be array(username=>'jmontoya', sufix='2')
  49. @param string firstname
  50. @param string lastname
  51. @param string username
  52. @return array with the username, the sufix
  53. @author Julio Montoya Armas
  54. */
  55. function make_username($firstname, $lastname, $username)
  56. {
  57. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  58. $tbl_session_rel_course_rel_user=Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  59. // if username exist
  60. if ( ! UserManager :: is_username_available($username) || empty($username) )
  61. {
  62. $i=0;
  63. while (1)
  64. {
  65. if ($i==0)
  66. {
  67. $sufix = '';
  68. }
  69. else
  70. {
  71. $sufix= $i;
  72. }
  73. $desired_username=make_login($firstname,$lastname);
  74. if (UserManager :: is_username_available($desired_username.$sufix))
  75. {
  76. break;
  77. }
  78. else
  79. {
  80. $i++;
  81. }
  82. }
  83. $username_array=array('username'=>$desired_username , 'sufix'=>$sufix);
  84. return $username_array;
  85. }
  86. else
  87. {
  88. $username_array=array('username'=>$username , 'sufix'=>'');
  89. return $username_array;
  90. }
  91. }
  92. /**
  93. Checks if there are repeted users in a given array
  94. @param array $usernames list of the usernames in the uploaded file
  95. @param array $user_array['username'] and $user_array['sufix'] where sufix is the number part in a login i.e -> jmontoya2
  96. @return array with the $usernames array and the $user_array array
  97. @author Julio Montoya Armas
  98. */
  99. function check_user_in_array($usernames,$user_array)
  100. {
  101. $user_list=array_keys($usernames);
  102. $username=$user_array['username'].$user_array['sufix'];
  103. if (in_array($username,$user_list))
  104. {
  105. $user_array['sufix']+= $usernames[$username];
  106. $usernames[$username]++;
  107. }
  108. else
  109. {
  110. $usernames[$username]=1;
  111. }
  112. $result_array=array($usernames,$user_array);
  113. return $result_array;
  114. }
  115. /** checks if the username is already subscribed in a session
  116. * @param string a given username
  117. * @param array the array with the course list codes
  118. * @param the session id
  119. * @return 0 if the user is not subscribed otherwise it returns the user_id of the given username
  120. * @author Julio Montoya Armas
  121. */
  122. function user_available_in_session($username, $CourseList,$id_session)
  123. {
  124. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  125. $tbl_session_rel_course_rel_user=Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  126. foreach($CourseList as $enreg_course)
  127. {
  128. $sql_select = "SELECT u.user_id FROM $tbl_session_rel_course_rel_user rel INNER JOIN $table_user u
  129. on (rel.id_user=u.user_id)
  130. WHERE rel.id_session='$id_session' AND u.status='5' AND u.username ='$username' AND rel.course_code='$enreg_course'";
  131. //echo "<br>";
  132. $rs = api_sql_query($sql_select, __FILE__, __LINE__);
  133. if (Database::num_rows($rs) > 0)
  134. {
  135. return Database::result($rs,0,0);
  136. }
  137. else
  138. return 0;
  139. }
  140. }
  141. /**
  142. This function checks if the users in the uploaded file are not repeted and creates the username if necesary
  143. i.e if in the file there are an user repeted twice (Julio Montoya / Julio Montoya) and the username fields are empty. IN this case,
  144. this function will create the usernames based in the first and last name. The users will be jmontoya and jmontoya2
  145. but if in the database there is a user with a name jmontoya the users registered will be jmontoya2 and jmontoya3.
  146. @param $users list of users
  147. @author Julio Montoya Armas
  148. */
  149. function check_all_usernames($users,$CourseList, $id_session)
  150. {
  151. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  152. $usernames = array ();
  153. $new_users=array();
  154. foreach ($users as $index => $user)
  155. {
  156. $user['UserName']=str_replace(' ', '',trim($user['UserName']));
  157. $desired_username=array();
  158. if (empty($user['UserName']))
  159. {
  160. $desired_username =make_username($user['FirstName'],$user['LastName'],'');
  161. $pre_username= $desired_username['username'].$desired_username['sufix'];
  162. $user['UserName'] = $pre_username;
  163. $user['create'] = '1';
  164. }
  165. else
  166. {
  167. if (UserManager :: is_username_available($user['UserName']))
  168. {
  169. $desired_username = make_username($user['FirstName'],$user['LastName'],$user['UserName']);
  170. $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  171. $user['create'] = '1';
  172. }
  173. else
  174. {
  175. $is_session_avail= user_available_in_session($user['UserName'], $CourseList, $id_session);
  176. if ($is_session_avail==0)
  177. {
  178. //$desired_username = make_username($user['FirstName'],$user['LastName'],$user['UserName']);
  179. //$user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  180. $user_name = $user['UserName'];
  181. //echo "<br>";
  182. $sql_select = "SELECT user_id FROM $table_user WHERE username ='$user_name' ";
  183. $rs = api_sql_query($sql_select, __FILE__, __LINE__);
  184. $user['create'] =Database::result($rs,0,0); // this should be the ID because the user exist
  185. //echo '<br>';
  186. }
  187. else
  188. {
  189. $user['create'] = $is_session_avail;
  190. }
  191. }
  192. }
  193. // usernames is the current list of users in the file
  194. $result_array=check_user_in_array($usernames,$desired_username);
  195. $usernames=$result_array[0];
  196. $desired_username=$result_array[1];
  197. $user['UserName']=$desired_username['username'].$desired_username['sufix'];
  198. $new_users[]=$user;
  199. }
  200. return $new_users;
  201. }
  202. /**
  203. * This functions checks if there are users that are already registered in the DB by other creator than the current coach.
  204. * @param string a given username
  205. * @param array the array with the course list codes
  206. * @param the session id
  207. * @author Julio Montoya Armas
  208. */
  209. function get_user_creator($users,$CourseList, $id_session)
  210. {
  211. $errors=array();
  212. foreach ($users as $index => $user)
  213. {
  214. // database table definition
  215. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  216. $tbl_session_rel_course_rel_user=Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  217. $username=$user['UserName'];
  218. //echo "<br>";
  219. $sql ="SELECT creator_id FROM $table_user WHERE username='$username' ";
  220. $rs=api_sql_query($sql,__FILE__,__LINE__);
  221. $creator_id=Database::result($rs,0,0);
  222. // check if we are the creators or not
  223. if ($creator_id!='')
  224. {
  225. if ($creator_id != api_get_user_id())
  226. {
  227. $user['error'] = get_lang('UserAlreadyRegisteredByOtherCreator');
  228. $errors[] = $user;
  229. }
  230. }
  231. }
  232. return $errors;
  233. }
  234. /**
  235. * validate the imported data
  236. * @param list of users
  237. */
  238. function validate_data($users,$id_session)
  239. {
  240. $errors = array ();
  241. $usernames = array ();
  242. $new_users=array();
  243. foreach ($users as $index => $user)
  244. {
  245. //1. check if mandatory fields are set
  246. $mandatory_fields = array ('LastName', 'FirstName');
  247. if (api_get_setting('registration', 'email') == 'true')
  248. {
  249. $mandatory_fields[] = 'Email';
  250. }
  251. foreach ($mandatory_fields as $key => $field)
  252. {
  253. if (!isset ($user[$field]) || strlen($user[$field]) == 0)
  254. {
  255. $user['error'] = get_lang($field.'Mandatory');
  256. $errors[] = $user;
  257. }
  258. }
  259. // 2. check if the username is too long
  260. if (strlen($user['UserName']) > 20)
  261. {
  262. $user['error'] = get_lang('UserNameTooLong');
  263. $errors[] = $user;
  264. }
  265. $user['UserName']=trim($user['UserName']);
  266. if (strlen($user['UserName']) == 0 || $user['UserName']=='' )
  267. {
  268. $user['UserName'] =make_login($user['FirstName'],$user['LastName']);
  269. }
  270. $new_users[]=$user;
  271. }
  272. $results=array('errors'=>$errors,'users'=>$new_users);
  273. return $results;
  274. }
  275. /**
  276. * Add missing user-information (which isn't required, like password, etc)
  277. */
  278. function complete_missing_data($user)
  279. {
  280. //1. generate a password if necessary
  281. if (!isset ($user['Password']) || strlen($user['Password']) == 0)
  282. {
  283. $user['Password'] = api_generate_password();
  284. }
  285. return $user;
  286. }
  287. /**
  288. * Save the imported data
  289. */
  290. function save_data($users, $CourseList,$id_session)
  291. {
  292. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  293. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  294. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  295. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  296. $sendMail = $_POST['sendMail'] ? 1 : 0;
  297. // adding users to the platform
  298. $new_users=array();
  299. foreach ($users as $index => $user)
  300. {
  301. $user = complete_missing_data($user);
  302. // coach only will registered users
  303. $default_status = '5';
  304. if ($user['create']=='1')
  305. {
  306. $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'], '');
  307. $user['added_at_platform']=1;
  308. }
  309. else
  310. {
  311. $user['id']=$user['create'];
  312. $user['added_at_platform']=0;
  313. }
  314. $new_users[]=$user;
  315. }
  316. //update users list
  317. $users = $new_users;
  318. // inserting users
  319. $super_list =array();
  320. foreach($CourseList as $enreg_course)
  321. {
  322. $nbr_users=0;
  323. $new_users =array();
  324. foreach ($users as $index => $user)
  325. {
  326. $userid = $user['id'];
  327. $sql= "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$userid')";
  328. //echo "<br>";
  329. $course_session=array('course'=>$enreg_course,'added'=>1);
  330. //$user['added_at_session']=$course_session;
  331. api_sql_query($sql,__FILE__,__LINE__);
  332. if(mysql_affected_rows())
  333. {
  334. $nbr_users++;
  335. }
  336. $new_users[]=$user;
  337. }
  338. $super_list[]=$new_users;
  339. //update the nbr_users field
  340. $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'";
  341. $rs = api_sql_query($sql_select, __FILE__, __LINE__);
  342. list($nbr_users) = Database::fetch_array($rs);
  343. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'";
  344. api_sql_query($sql_update ,__FILE__,__LINE__);
  345. $sql_update = "UPDATE $tbl_session SET nbr_users= '$nbr_users' WHERE id='$id_session'";
  346. api_sql_query($sql_update,__FILE__,__LINE__);
  347. }
  348. // we dont delete the users (thoughts while dreaming)
  349. //$sql_delete = "DELETE FROM $tbl_session_rel_user WHERE id_session = '$id_session'";
  350. //api_sql_query($sql_delete,__FILE__, __LINE__);
  351. $new_users=array();
  352. foreach ($users as $index => $user)
  353. {
  354. $userid = $user['id'];
  355. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$userid')";
  356. api_sql_query($sql_insert,__FILE__,__LINE__);
  357. $user['added_at_session']=1;
  358. $new_users[]=$user;
  359. }
  360. $users=$new_users;
  361. $registered_users=get_lang('FileImported').'<br /> Import file results : <br />';
  362. // sending the email
  363. $addedto='';
  364. if ($sendMail)
  365. {
  366. $i=0;
  367. foreach ($users as $index => $user)
  368. {
  369. $emailto =$user['FirstName'].' '.$user['LastName'].' <'.$user['Email'].'>';
  370. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  371. $emailbody = get_lang('Dear').' '.$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_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')."";
  372. $emailheaders = 'From: '.api_get_setting('administratorName').' '.api_get_setting('administratorSurname').' <'.api_get_setting('emailAdministrator').">\n";
  373. $emailheaders .= 'Reply-To: '.api_get_setting('emailAdministrator');
  374. @ api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  375. if ( ($user['added_at_platform']== 1 && $user['added_at_session']==1) || $user['added_at_session']==1)
  376. {
  377. if ($user['added_at_platform']==1)
  378. {
  379. $addedto=get_lang('UserCreatedPlatform');
  380. }
  381. else
  382. {
  383. $addedto=' ';
  384. }
  385. if ($user['added_at_session']==1)
  386. {
  387. $addedto.=get_lang('UserInSession');
  388. }
  389. $registered_users.= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".$user['FirstName']." ".$user['LastName']."</a> - ".$addedto.'<br />';
  390. }
  391. else
  392. {
  393. $addedto= get_lang('UserNotAdded');
  394. $registered_users.= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".$user['FirstName']." ".$user['LastName']."</a> - ".$addedto.'<br />';
  395. }
  396. }
  397. }
  398. else
  399. {
  400. $i=0;
  401. foreach ($users as $index => $user)
  402. {
  403. if ( ($user['added_at_platform']== 1 && $user['added_at_session']==1) || $user['added_at_session']==1)
  404. {
  405. if ($user['added_at_platform']==1)
  406. {
  407. $addedto=get_lang('UserCreatedPlatform');
  408. }
  409. else
  410. {
  411. $addedto=' ';
  412. }
  413. if ($user['added_at_session']==1)
  414. {
  415. $addedto.=' '.get_lang('UserInSession');
  416. }
  417. $registered_users.= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".$user['FirstName']." ".$user['LastName']."</a> - ".$addedto.'<br />';
  418. }
  419. else
  420. {
  421. $addedto= get_lang('UserNotAdded');
  422. $registered_users.= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".$user['FirstName']." ".$user['LastName']."</a> - ".$addedto.'<br />';
  423. }
  424. }
  425. }
  426. header('Location: course.php?id_session='.$id_session.'&action=show_message&message='.urlencode($registered_users));
  427. exit ();
  428. //header('Location: resume_session.php?id_session='.$id_session);
  429. }
  430. /**
  431. * Read the CSV-file
  432. * @param string $file Path to the CSV-file
  433. * @return array All userinformation read from the file
  434. */
  435. function parse_csv_data($file)
  436. {
  437. $users = Import :: csv_to_array($file);
  438. foreach ($users as $index => $user)
  439. {
  440. if (isset ($user['Courses']))
  441. {
  442. $user['Courses'] = explode('|', trim($user['Courses']));
  443. }
  444. $users[$index] = $user;
  445. }
  446. return $users;
  447. }
  448. /**
  449. * XML-parser: handle start of element
  450. */
  451. function element_start($parser, $data)
  452. {
  453. global $user;
  454. global $current_tag;
  455. switch ($data)
  456. {
  457. case 'Contact' :
  458. $user = array ();
  459. break;
  460. default :
  461. $current_tag = $data;
  462. }
  463. }
  464. /**
  465. * XML-parser: handle end of element
  466. */
  467. function element_end($parser, $data)
  468. {
  469. global $user;
  470. global $users;
  471. global $current_value;
  472. $user[$data] = $current_value;
  473. switch ($data)
  474. {
  475. case 'Contact' :
  476. $users[] = $user;
  477. break;
  478. default :
  479. $user[$data] = $current_value;
  480. break;
  481. }
  482. }
  483. /**
  484. * XML-parser: handle character data
  485. */
  486. function character_data($parser, $data)
  487. {
  488. global $current_value;
  489. $current_value = $data;
  490. }
  491. /**
  492. * Read the XML-file
  493. * @param string $file Path to the XML-file
  494. * @return array All userinformation read from the file
  495. */
  496. function parse_xml_data($file)
  497. {
  498. global $current_tag;
  499. global $current_value;
  500. global $user;
  501. global $users;
  502. $users = array ();
  503. $parser = xml_parser_create();
  504. xml_set_element_handler($parser, 'element_start', 'element_end');
  505. xml_set_character_data_handler($parser, "character_data");
  506. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  507. xml_parse($parser, file_get_contents($file));
  508. xml_parser_free($parser);
  509. return $users;
  510. }
  511. // name of the language file that needs to be included
  512. $language_file = array ('admin','registration', 'index','trad4all', 'tracking');
  513. $cidReset = true;
  514. include ('../inc/global.inc.php');
  515. $this_section = SECTION_PLATFORM_ADMIN;
  516. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  517. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  518. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  519. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  520. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  521. $formSent = 0;
  522. $errorMsg = '';
  523. $tool_name = get_lang('ImportUserListXMLCSV');
  524. api_block_anonymous_users();
  525. $interbreadcrumb[] = array ("url" => "index.php", "name" => get_lang('MySpace'));
  526. $id_session='';
  527. if(isset($_GET["id_session"]) && $_GET["id_session"]!="")
  528. {
  529. $id_session = Security::remove_XSS($_GET["id_session"]);
  530. $interbreadcrumb[] = array ("url" => "session.php", "name" => get_lang('Sessions'));
  531. $interbreadcrumb[] = array ("url" => "course.php?id_session=".$_GET["id_session"]."", "name" => get_lang('Course'));
  532. }
  533. //checking if the current coach is the admin coach
  534. /*
  535. if (!api_is_coach())
  536. {
  537. api_not_allowed(true);
  538. }
  539. */
  540. //checking if the current coach is the admin coach
  541. if (api_get_setting('add_users_by_coach')=='true')
  542. {
  543. if(!api_is_platform_admin())
  544. {
  545. if (isset($_REQUEST['id_session']))
  546. {
  547. $id_session=$_REQUEST['id_session'];
  548. $sql = 'SELECT id_coach FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_session;
  549. $rs = api_sql_query($sql,__FILE__,__LINE__);
  550. if(mysql_result($rs,0,0)!=$_user['user_id'])
  551. {
  552. api_not_allowed(true);
  553. }
  554. }
  555. else
  556. {
  557. api_not_allowed(true);
  558. }
  559. }
  560. }
  561. else
  562. {
  563. api_not_allowed(true);
  564. }
  565. set_time_limit(0);
  566. if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0)
  567. {
  568. $file_type = $_POST['file_type'];
  569. $id_session = $_POST['id_session'];
  570. if ($file_type == 'csv')
  571. {
  572. $users = parse_csv_data($_FILES['import_file']['tmp_name']);
  573. }
  574. else
  575. {
  576. $users = parse_xml_data($_FILES['import_file']['tmp_name']);
  577. }
  578. if (count($users) >0 )
  579. {
  580. $results = validate_data($users);
  581. $errors = $results['errors'];
  582. $users = $results['users'];
  583. if (count($errors) == 0)
  584. {
  585. if (!empty($id_session))
  586. {
  587. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  588. //selecting all the courses from the session id requested
  589. $sql ="SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'";
  590. $result=api_sql_query($sql,__FILE__,__LINE__);
  591. $CourseList=array();
  592. while($row=Database::fetch_array($result))
  593. {
  594. $CourseList[]=$row['course_code'];
  595. }
  596. $errors=get_user_creator($users,$CourseList, $id_session);
  597. $users=check_all_usernames($users,$CourseList, $id_session);
  598. if (count($errors) == 0)
  599. {
  600. save_data($users,$CourseList,$id_session);
  601. }
  602. }
  603. else
  604. {
  605. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoSessionId')));
  606. }
  607. }
  608. }
  609. else
  610. {
  611. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoUsersRead')));
  612. }
  613. }
  614. Display :: display_header($tool_name);
  615. if($_FILES['import_file']['size'] == 0 AND $_POST)
  616. {
  617. Display::display_error_message(get_lang('ThisFieldIsRequired'));
  618. }
  619. if (count($errors) != 0)
  620. {
  621. $error_message = '<ul>';
  622. foreach ($errors as $index => $error_user)
  623. {
  624. $error_message .= '<li><b>'.$error_user['error'].'</b>: ';
  625. $error_message .= $error_user['FirstName'].' '.$error_user['LastName'];
  626. $error_message .= '</li>';
  627. }
  628. $error_message .= '</ul>';
  629. Display :: display_error_message($error_message, false);
  630. }
  631. $form = new FormValidator('user_import');
  632. $form->addElement('hidden', 'formSent');
  633. $form->addElement('hidden', 'id_session',$id_session);
  634. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  635. $form->addRule('import_file', get_lang('ThisFieldIsRequired'), 'required');
  636. $allowed_file_types = array ('xml', 'csv');
  637. $form->addRule('file', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  638. $form->addElement('radio', 'file_type', get_lang('FileType'), 'XML (<a href="exemple.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)', 'xml');
  639. $form->addElement('radio', 'file_type', null, 'CSV (<a href="exemple.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', 'csv');
  640. $form->addElement('radio', 'sendMail', get_lang('SendMailToUsers'), get_lang('Yes'), 1);
  641. $form->addElement('radio', 'sendMail', null, get_lang('No'), 0);
  642. $form->addElement('submit', 'submit', get_lang('Ok'));
  643. $defaults['formSent'] = 1;
  644. $defaults['file_type'] = 'xml';
  645. $form->setDefaults($defaults);
  646. $form->display();
  647. /*
  648. <?php echo implode('/',$defined_auth_sources); ?>
  649. &lt;AuthSource&gt;<?php echo implode('/',$defined_auth_sources); ?>&lt;/AuthSource&gt;
  650. */
  651. ?>
  652. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  653. <blockquote>
  654. <pre>
  655. <b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;OfficialCode;PhoneNumber;
  656. <b>Montoya</b>;<b>Julio</b>;<b>info@localhost</b>;jmontoya;123456789;code1;3141516
  657. <b>Doewing</b>;<b>Johny</b>;<b>info@localhost</b>;jdoewing;123456789;code2;3141516
  658. </pre>
  659. </blockquote>
  660. <p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  661. <blockquote>
  662. <pre>
  663. &lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
  664. &lt;Contacts&gt;
  665. &lt;Contact&gt;
  666. <b>&lt;LastName&gt;Montoya&lt;/LastName&gt;</b>
  667. <b>&lt;FirstName&gt;Julio&lt;/FirstName&gt;</b>
  668. <b>&lt;Email&gt;info@localhost&lt;/Email&gt;</b>
  669. &lt;UserName&gt;jmontoya&lt;/UserName&gt;
  670. &lt;Password&gt;123456&lt;/Password&gt;
  671. &lt;OfficialCode&gt;code1&lt;/OfficialCode&gt;
  672. &lt;PhoneNumber&gt;3141516&lt;/PhoneNumber&gt;
  673. &lt;/Contact&gt;
  674. &lt;/Contacts&gt;
  675. </pre>
  676. </blockquote>
  677. <?php
  678. /*
  679. ==============================================================================
  680. FOOTER
  681. ==============================================================================
  682. */
  683. Display :: display_footer();
  684. ?>