session_import.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. <?php // $Id: user_import.php,v 1.17 2005/06/22 08:00:31 bmol Exp $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * @package dokeos.admin
  22. ==============================================================================
  23. */
  24. $language_file = array('admin','registration');
  25. $cidReset=true;
  26. include('../inc/global.inc.php');
  27. if(empty($charset))
  28. {
  29. $charset = 'ISO-8859-15';
  30. }
  31. api_protect_admin_script(true);
  32. include(api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  33. include (api_get_path(LIBRARY_PATH)."/add_course.conf.php");
  34. include_once (api_get_path(LIBRARY_PATH)."/add_course.lib.inc.php");
  35. $formSent=0;
  36. $errorMsg='';
  37. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  38. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  39. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  40. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  41. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  42. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  43. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  44. $tool_name=get_lang('ImportSessionListXMLCSV');
  45. $interbreadcrumb[]=array("url" => "index.php","name" => get_lang('AdministrationTools'));
  46. set_time_limit(0);
  47. if($_POST['formSent'])
  48. {
  49. if(isset($_FILES['import_file']['tmp_name']))
  50. {
  51. $formSent=$_POST['formSent'];
  52. $file_type=$_POST['file_type'];
  53. $sendMail=$_POST['sendMail']?1:0;
  54. $sessions=array();
  55. ///////////////////////
  56. //XML/////////////////
  57. /////////////////////
  58. $countSessions = 0;
  59. if($file_type == 'xml')
  60. {
  61. $racine = simplexml_load_file($_FILES['import_file']['tmp_name']);
  62. if(is_object($racine))
  63. {
  64. foreach($racine->Users->User as $userNode)
  65. {
  66. $username = mb_convert_encoding($userNode->Username,$charset,'utf-8');
  67. $isCut = 0; // if the username given is too long
  68. if(strlen($username)>20)
  69. {
  70. $user_name_dist = $username;
  71. $username = substr($username,0,20);
  72. $isCut = 1;
  73. }
  74. $sql = "SELECT 1 FROM $tbl_user WHERE username='".addslashes($username)."'";
  75. $rs = api_sql_query($sql, __FILE__, __LINE__);
  76. if(mysql_affected_rows()==0)
  77. {
  78. if($isCut)
  79. {
  80. $errorMsg .= get_lang('UsernameTooLongWasCut').' '.get_lang('From').' '.$user_name_dist.' '.get_lang('To').' '.$username.' <br />';
  81. }
  82. $lastname = mb_convert_encoding($userNode->Lastname,$charset,'utf-8');
  83. $firstname = mb_convert_encoding($userNode->Firstname,$charset,'utf-8');
  84. $password = mb_convert_encoding($userNode->Password,$charset,'utf-8');
  85. if(empty($password))
  86. $password = base64_encode(rand(1000,10000));
  87. $email = mb_convert_encoding($userNode->Email,$charset,'utf-8');
  88. $official_code = mb_convert_encoding($userNode->OfficialCode,$charset,'utf-8');
  89. $phone = mb_convert_encoding($userNode->Phone,$charset,'utf-8');
  90. $status = mb_convert_encoding($userNode->Status,$charset,'utf-8');
  91. switch($status)
  92. {
  93. case 'student' : $status = 5; break;
  94. case 'teacher' : $status = 1; break;
  95. default : $status = 5; $errorMsg = get_lang('StudentStatusWasGivenTo').' : '.$username.'<br />';
  96. }
  97. $sql = "INSERT INTO $tbl_user SET
  98. username = '".Database::escape_string($username)."',
  99. lastname = '".Database::escape_string($lastname)."',
  100. firstname = '".Database::escape_string($firstname)."',
  101. password = '".($userPasswordCrypted==true ? md5($password) : $password)."',
  102. email = '".Database::escape_string($email)."',
  103. official_code = '".Database::escape_string($official_code)."',
  104. phone = '".Database::escape_string($phone)."',
  105. status = '".Database::escape_string($status)."'";
  106. api_sql_query($sql, __FILE__, __LINE__);
  107. if(mysql_affected_rows()>0 && $sendMail)
  108. {
  109. $emailto='"'.$firstname.' '.$lastname.'" <'.$email.'>';
  110. $emailsubject='['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  111. $emailbody="[NOTE:] ".get_lang('ThisIsAutomaticEmailNoReply').".\n\n".get_lang('langDear')." $firstname $lastname,\n\n".get_lang('langYouAreReg')." ". get_setting('siteName') ." ".get_lang('langSettings')." $username\n". get_lang('langPass')." : $password\n\n".get_lang('langAddress') ." ". get_lang('langIs') ." ". $serverAddress ."\n\n".get_lang('YouWillSoonReceiveMailFromCoach')."\n\n". get_lang('langProblem'). "\n\n". get_lang('langFormula');
  112. //#287 modifiée par Stéphane DEBIEVE - FOREM
  113. $emailheaders='From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  114. $emailheaders.='Reply-To: '.get_setting('emailAdministrator');
  115. @api_send_mail($emailto,$emailsubject,$emailbody,$emailheaders);
  116. }
  117. }
  118. else
  119. {
  120. $lastname = mb_convert_encoding($userNode->Lastname,$charset,'utf-8');
  121. $firstname = mb_convert_encoding($userNode->Firstname,$charset,'utf-8');
  122. $password = mb_convert_encoding($userNode->Password,$charset,'utf-8');
  123. $email = mb_convert_encoding($userNode->Email,$charset,'utf-8');
  124. $official_code = mb_convert_encoding($userNode->OfficialCode,$charset,'utf-8');
  125. $phone = mb_convert_encoding($userNode->Phone,$charset,'utf-8');
  126. $status = mb_convert_encoding($userNode->Status,$charset,'utf-8');
  127. switch($status)
  128. {
  129. case 'student' : $status = 5; break;
  130. case 'teacher' : $status = 1; break;
  131. default : $status = 5; $errorMsg = get_lang('StudentStatusWasGivenTo').' : '.$username.'<br />';
  132. }
  133. $sql = "UPDATE $tbl_user SET
  134. lastname = '".Database::escape_string($lastname)."',
  135. firstname = '".Database::escape_string($firstname)."',
  136. ".(empty($password) ? "" : "password = '".($userPasswordCrypted==true ? md5($password) : $password)."',")."
  137. email = '".Database::escape_string($email)."',
  138. official_code = '".Database::escape_string($official_code)."',
  139. phone = '".Database::escape_string($phone)."',
  140. status = '".Database::escape_string($status)."'
  141. WHERE username = '".Database::escape_string($username)."'";
  142. api_sql_query($sql, __FILE__, __LINE__);
  143. }
  144. }
  145. foreach($racine->Courses->Course as $courseNode)
  146. {
  147. $course_code = mb_convert_encoding($courseNode->CourseCode,$charset,'utf-8');
  148. $title = mb_convert_encoding($courseNode->CourseTitle,$charset,'utf-8');
  149. $description = mb_convert_encoding($courseNode->CourseDescription,$charset,'utf-8');
  150. $language = mb_convert_encoding($courseNode->CourseLanguage,$charset,'utf-8');
  151. $username = mb_convert_encoding($courseNode->CourseTeacher,$charset,'utf-8');
  152. $sql = "SELECT user_id, lastname, firstname FROM $tbl_user WHERE username='$username'";
  153. $rs = api_sql_query($sql, __FILE__, __LINE__);
  154. list($user_id, $lastname, $firstname) = mysql_fetch_array($rs);
  155. $keys = define_course_keys($course_code, "", $dbNamePrefix);
  156. if (sizeof($keys))
  157. {
  158. $currentCourseCode = $keys['visual_code'];
  159. $currentCourseId = $keys["currentCourseId"];
  160. if(empty($currentCourseCode))
  161. $currentCourseCode = $currentCourseId;
  162. $currentCourseDbName = $keys["currentCourseDbName"];
  163. $currentCourseRepository = $keys["currentCourseRepository"];
  164. if($currentCourseId == strtoupper($course_code))
  165. {
  166. if (empty ($title))
  167. {
  168. $title = $keys["currentCourseCode"];
  169. }
  170. prepare_course_repository($currentCourseRepository, $currentCourseId);
  171. update_Db_course($currentCourseDbName);
  172. fill_course_repository($currentCourseRepository);
  173. fill_Db_course($currentCourseDbName, $currentCourseRepository, 'french');
  174. //register_course($currentCourseId, $currentCourseCode, $currentCourseRepository, $currentCourseDbName, "$lastname $firstname", $course['unit_code'], addslashes($course['FR']['title']), $language, $user_id);
  175. $sql = "INSERT INTO ".$tbl_course." SET
  176. code = '".$currentCourseId."',
  177. db_name = '".$currentCourseDbName."',
  178. directory = '".$currentCourseRepository."',
  179. course_language = '".$language."',
  180. title = '".$title."',
  181. description = '".lang2db($description)."',
  182. category_code = '',
  183. visibility = '".$defaultVisibilityForANewCourse."',
  184. show_score = '',
  185. disk_quota = NULL,
  186. creation_date = now(),
  187. expiration_date = NULL,
  188. last_edit = now(),
  189. last_visit = NULL,
  190. tutor_name = '".$lastname." ".$firstname."',
  191. visual_code = '".$currentCourseCode."'";
  192. api_sql_query($sql, __FILE__, __LINE__);
  193. $sql = "INSERT INTO ".$tbl_course_user." SET
  194. course_code = '".$currentCourseId."',
  195. user_id = '".$user_id."',
  196. status = '1',
  197. role = '".lang2db('Professor')."',
  198. tutor_id='1',
  199. sort='". ($sort +1)."',
  200. user_course_cat='0'";
  201. api_sql_query($sql, __FILE__, __LINE__);
  202. }
  203. }
  204. }
  205. foreach ($racine->Session as $sessionNode){ // foreach session
  206. $countCourses = 0;
  207. $countUsers = 0;
  208. $SessionName = mb_convert_encoding($sessionNode->SessionName,$charset,'utf-8');
  209. $Coach = mb_convert_encoding($sessionNode->Coach,$charset,'utf-8');
  210. if(!empty($Coach)){
  211. $sqlCoach = "SELECT user_id FROM $tbl_user WHERE username='$Coach'";
  212. $rsCoach = api_sql_query($sqlCoach);
  213. list($CoachId) = (mysql_fetch_array($rsCoach));
  214. if(empty($CoachId))
  215. {
  216. $errorMsg .= get_lang('UserDoesNotExist').' : '.$Coach.'<br />';
  217. }
  218. }
  219. $DateStart = $sessionNode->DateStart;
  220. if(!empty($DateStart))
  221. {
  222. list($YearStart,$MonthStart, $DayStart) = explode('-',$DateStart);
  223. if(empty($YearStart) || empty($MonthStart) || empty($DayStart))
  224. {
  225. $errorMsg .= get_lang('WrongDate').' : '.$DateStart.'<br />';
  226. break;
  227. }
  228. else
  229. {
  230. $timeStart = mktime(0,0,0,$MonthStart,$DayStart,$YearStart);
  231. }
  232. $DateEnd = $sessionNode->DateEnd;
  233. if(!empty($DateStart))
  234. {
  235. list($YearEnd,$MonthEnd, $DayEnd) = explode('-',$DateEnd);
  236. if(empty($YearEnd) || empty($MonthEnd) || empty($DayEnd))
  237. {
  238. $errorMsg .= get_lang('WrongDate').' : '.$DateEnd.'<br />';
  239. break;
  240. }
  241. else
  242. {
  243. $timeEnd = mktime(0,0,0,$MonthEnd,$DayEnd,$YearEnd);
  244. }
  245. }
  246. if($timeEnd - $timeStart < 0)
  247. {
  248. $errorMsg .= get_lang('StartDateShouldBeBeforeEndDate').' : '.$DateEnd.'<br />';
  249. }
  250. }
  251. // verify that session doesn't exist
  252. while(!$uniqueName)
  253. {
  254. if($i>1)
  255. $suffix = ' - '.$i;
  256. $sql = 'SELECT 1 FROM '.$tbl_session.' WHERE name="'.Database::escape_string($SessionName.$suffix).'"';
  257. $rs = api_sql_query($sql, __FILE__, __LINE__);
  258. if(mysql_result($rs,0,0))
  259. {
  260. $i++;
  261. }
  262. else
  263. {
  264. $uniqueName = true;
  265. $SessionName .= $suffix;
  266. }
  267. }
  268. $sqlSession = "INSERT IGNORE INTO $tbl_session SET
  269. name = '".Database::escape_string($SessionName)."',
  270. id_coach = '$CoachId',
  271. date_start = '$DateStart',
  272. date_end = '$DateEnd',
  273. session_admin_id=".intval($_user['user_id']);
  274. $rsSession = api_sql_query($sqlSession, __FILE__, __LINE__);
  275. $session_id = mysql_insert_id();
  276. $countSessions++;
  277. foreach ($sessionNode->User as $userNode){
  278. $username = mb_convert_encoding(substr($userNode->nodeValue,0,20),$charset,'utf-8');
  279. $sqlUser = "SELECT user_id FROM $tbl_user WHERE username='".Database::escape_string($username)."'";
  280. $rsUser = api_sql_query($sqlUser);
  281. list($user_id) = (mysql_fetch_array($rsUser));
  282. if(!empty($user_id)){
  283. $sql = "INSERT INTO $tbl_session_user SET
  284. id_user='$user_id',
  285. id_session = '$session_id'";
  286. $rsUser = api_sql_query($sql,__FILE__,__LINE__);
  287. if(mysql_affected_rows()){
  288. $countUsers++;
  289. }
  290. }
  291. }
  292. foreach($sessionNode->Course as $courseNode){
  293. $CourseCode = $courseNode->CourseCode;
  294. // verify that the course pointed by the course code node exists
  295. $sql = 'SELECT 1 FROM '.$tbl_course.' WHERE code="'.mysql_escape_string($CourseCode).'"';
  296. $rs = api_sql_query($sql, __FILE__, __LINE__);
  297. if(mysql_num_rows($rs)>0)
  298. { // if the course exists we continue
  299. $Coach = substr($courseNode->Coach,0,20);
  300. if(!empty($Coach)){
  301. $sqlCoach = "SELECT user_id FROM $tbl_user WHERE username='$Coach'";
  302. $rsCoach = api_sql_query($sqlCoach,__FILE__,__LINE__);
  303. list($CoachId) = (mysql_fetch_array($rsCoach));
  304. if(empty($CoachId))
  305. {
  306. $errorMsg .= get_lang('UserDoesNotExist').' : '.$Coach.'<br />';
  307. }
  308. }
  309. else {
  310. $Coach = '';
  311. }
  312. $sqlCourse = "INSERT INTO $tbl_session_course SET
  313. course_code = '$CourseCode',
  314. id_coach='$CoachId',
  315. id_session='$session_id'";
  316. $rsCourse = api_sql_query($sqlCourse,__FILE__,__LINE__);
  317. if(mysql_affected_rows()){
  318. $countCourses++;
  319. $countUsersCourses = 0;
  320. foreach ($courseNode->User as $userNode){
  321. $username = substr($userNode,0,20);
  322. $sqlUser = "SELECT user_id FROM $tbl_user WHERE username='".$username."'";
  323. $rsUser = api_sql_query($sqlUser);
  324. list($user_id) = (mysql_fetch_array($rsUser));
  325. if(!empty($user_id))
  326. {
  327. $sql = "INSERT IGNORE INTO $tbl_session_user SET
  328. id_user='$user_id',
  329. id_session = '$session_id'";
  330. if(mysql_affected_rows())
  331. $countUsers++;
  332. $rsUser = api_sql_query($sql,__FILE__,__LINE__);
  333. $sql = "INSERT IGNORE INTO $tbl_session_course_user SET
  334. id_user='$user_id',
  335. course_code='$CourseCode',
  336. id_session = '$session_id'";
  337. $rsUsers = api_sql_query($sql,__FILE__,__LINE__);
  338. if(mysql_affected_rows())
  339. $countUsersCourses++;
  340. }
  341. else
  342. {
  343. $errorMsg .= get_lang('UserDoesNotExist').' : '.$username.'<br />';
  344. }
  345. }
  346. api_sql_query("UPDATE $tbl_session_course SET nbr_users='$countUsersCourses' WHERE course_code='$CourseCode'",__FILE__,__LINE__);
  347. }
  348. }
  349. else
  350. { // if the course does not exists
  351. $errorMsg .= get_lang('CourseDoesNotExist').' : '.$CourseCode.'<br />';
  352. }
  353. }
  354. api_sql_query("UPDATE $tbl_session SET nbr_users='$countUsers', nbr_courses='$countCourses' WHERE id='$session_id'",__FILE__,__LINE__);
  355. }
  356. }
  357. else
  358. {
  359. $errorMsg .= get_lang('XMLNotValid');
  360. }
  361. }
  362. /////////////////////
  363. // CSV /////////////
  364. ///////////////////
  365. else
  366. {
  367. $content=file($_FILES['import_file']['tmp_name']);
  368. if(!strstr($content[0],';'))
  369. {
  370. $errorMsg=get_lang('NotCSV');
  371. }
  372. else
  373. {
  374. $tag_names=array();
  375. foreach($content as $key=>$enreg)
  376. {
  377. $enreg=explode(';',trim($enreg));
  378. if($key)
  379. {
  380. foreach($tag_names as $tag_key=>$tag_name)
  381. {
  382. $sessions[$key-1][$tag_name]=$enreg[$tag_key];
  383. }
  384. }
  385. else
  386. {
  387. foreach($enreg as $tag_name)
  388. {
  389. $tag_names[]=eregi_replace('[^a-z0-9_-]','',$tag_name);
  390. }
  391. if(!in_array('SessionName',$tag_names) || !in_array('DateStart',$tag_names) || !in_array('DateEnd',$tag_names))
  392. {
  393. $errorMsg=get_lang('NoNeededData');
  394. break;
  395. }
  396. }
  397. }
  398. foreach($sessions as $enreg) {
  399. $SessionName = $enreg['SessionName'];
  400. $DateStart = $enreg['DateStart'];
  401. $DateEnd = $enreg['DateEnd'];
  402. if(!empty($enreg['Coach'])){
  403. $sqlCoach = "SELECT user_id FROM $tbl_user WHERE username='".$enreg['Coach']."'";
  404. $rsCoach = api_sql_query($sqlCoach);
  405. list($Coach) = (mysql_fetch_array($rsCoach));
  406. }
  407. else {
  408. $Coach = '';
  409. }
  410. $sqlSession = "INSERT IGNORE INTO $tbl_session SET
  411. name = '$SessionName',
  412. id_coach = '$Coach',
  413. date_start = '$DateStart',
  414. date_end = '$DateEnd'";
  415. $rsSession = api_sql_query($sqlSession, __FILE__, __LINE__);
  416. $update = false;
  417. if(!mysql_affected_rows($rsSession)){
  418. $update = true;
  419. $sqlSession = "UPDATE $tbl_session SET
  420. id_coach = '$Coach',
  421. date_start = '$DateStart',
  422. date_end = '$DateEnd'
  423. WHERE name = '$SessionName'";
  424. $rsSession = api_sql_query($sqlSession, __FILE__, __LINE__);
  425. $session_id = api_sql_query("SELECT id FROM $tbl_session WHERE name='$SessionName'",__FILE__,__LINE__);
  426. list($session_id) = mysql_fetch_array($session_id);
  427. api_sql_query("DELETE FROM $tbl_session_user WHERE id_session='$session_id'",__FILE__,__LINE__);
  428. api_sql_query("DELETE FROM $tbl_session_course WHERE id_session='$session_id'",__FILE__,__LINE__);
  429. api_sql_query("DELETE FROM $tbl_session_course_user WHERE id_session='$session_id'",__FILE__,__LINE__);
  430. }
  431. else {
  432. $session_id = mysql_insert_id($rsSession);
  433. }
  434. $countSessions++;
  435. $users = explode('|',$enreg['Users']);
  436. foreach ($users as $user){
  437. $sqlUser = "SELECT user_id FROM $tbl_user WHERE username='".$user."'";
  438. $rsUser = api_sql_query($sqlUser);
  439. list($user_id) = (mysql_fetch_array($rsUser));
  440. $sql = "INSERT INTO $tbl_session_user SET
  441. id_user='$user_id',
  442. id_session = '$session_id'";
  443. $rsUser = api_sql_query($sql,__FILE__,__LINE__);
  444. if(mysql_affected_rows()){
  445. $countUsers++;
  446. }
  447. }
  448. $courses = explode('|',$enreg['Courses']);
  449. foreach($courses as $course){
  450. $CourseCode = substr($course,0,strpos($course,'['));
  451. $Coach = strstr($course,'[');
  452. $Coach = substr($Coach,1,strpos($Coach,']')-1);
  453. if(!empty($Coach)){
  454. $sqlCoach = "SELECT user_id FROM $tbl_user WHERE username='$Coach'";
  455. $rsCoach = api_sql_query($sqlCoach,__FILE__,__LINE__);
  456. list($Coach) = (mysql_fetch_array($rsCoach));
  457. }
  458. else {
  459. $Coach = '';
  460. }
  461. $sqlCourse = "INSERT INTO $tbl_session_course SET
  462. course_code = '$CourseCode',
  463. id_coach='$Coach',
  464. id_session='$session_id'";
  465. $rsCourse = api_sql_query($sqlCourse,__FILE__,__LINE__);
  466. if(mysql_affected_rows()){
  467. $countCourses++;
  468. $users = substr($course , strpos($course,'[',1)+1 , strpos($course,']',1));
  469. $users = explode('|',$enreg['Users']);
  470. $countUsersCourses = 0;
  471. foreach ($users as $user){
  472. $sqlUser = "SELECT user_id FROM $tbl_user WHERE username='".$user."'";
  473. $rsUser = api_sql_query($sqlUser);
  474. list($user_id) = (mysql_fetch_array($rsUser));
  475. $sql = "INSERT INTO $tbl_session_course_user SET
  476. id_user='$user_id',
  477. course_code='$CourseCode',
  478. id_session = '$session_id'";
  479. $rsUsers = api_sql_query($sql,__FILE__,__LINE__);
  480. if(mysql_affected_rows())
  481. $countUsersCourses++;
  482. }
  483. api_sql_query("UPDATE $tbl_session_course SET nbr_users='$countUsersCourses' WHERE course_code='$CourseCode'",__FILE__,__LINE__);
  484. }
  485. }
  486. api_sql_query("UPDATE $tbl_session SET nbr_users='$countUsers', nbr_courses='$countCourses' WHERE id='$session_id'",__FILE__,__LINE__);
  487. }
  488. }
  489. }
  490. if(!empty($errorMsg))
  491. {
  492. $errorMsg = get_lang('ButProblemsOccured').' :<br />'.$errorMsg;
  493. }
  494. if($countSessions == 1){
  495. header('Location: resume_session.php?id_session='.$session_id);
  496. exit;
  497. }
  498. else{
  499. header('Location: session_list.php?action=show_message&message='.urlencode(get_lang('FileImported').' '.$errorMsg));
  500. exit;
  501. }
  502. }
  503. else
  504. {
  505. $errorMsg = get_lang('NoInputFile');
  506. }
  507. }
  508. Display::display_header($tool_name);
  509. api_display_tool_title($tool_name);
  510. ?>
  511. <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin:0px;">
  512. <input type="hidden" name="formSent" value="1">
  513. <table border="0" cellpadding="5" cellspacing="0">
  514. <?php
  515. if(!empty($errorMsg))
  516. {
  517. ?>
  518. <tr>
  519. <td colspan="2">
  520. <?php
  521. Display::display_normal_message($errorMsg,false); //main API
  522. ?>
  523. </td>
  524. </tr>
  525. <?php
  526. }
  527. ?>
  528. <tr>
  529. <td nowrap="nowrap"><?php echo get_lang('ImportFileLocation'); ?> :</td>
  530. <td><input type="file" name="import_file" size="30"></td>
  531. </tr>
  532. <tr>
  533. <td nowrap="nowrap" valign="top"><?php echo get_lang('FileType'); ?> :</td>
  534. <td>
  535. <input class="checkbox" type="radio" name="file_type" id="file_type_xml" value="xml" checked="checked" /> <label for="file_type_xml">XML</label> (<a href="exemple.xml" target="_blank"><?php echo get_lang('ExampleXMLFile'); ?></a>)<br>
  536. <input class="checkbox" type="radio" name="file_type" id="file_type_csv" value="csv" <?php if($formSent && $file_type == 'csv') echo 'checked="checked"'; ?>> <label for="file_type_csv">CSV</label> (<a href="exempleSession.csv" target="_blank"><?php echo get_lang('ExampleCSVFile'); ?></a>)<br>
  537. </td>
  538. </tr>
  539. <tr>
  540. <td nowrap="nowrap" valign="top"><?php echo get_lang('SendMailToUsers'); ?> :</td>
  541. <td>
  542. <input class="checkbox" type="checkbox" name="sendMail" id="sendMail" value="true" />
  543. </td>
  544. </tr>
  545. <tr>
  546. <td>&nbsp;</td>
  547. <td><input type="submit" value="<?php echo get_lang('Ok'); ?>"></td>
  548. </tr>
  549. </table>
  550. </form>
  551. <font color="gray">
  552. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  553. <blockquote>
  554. <pre>
  555. <b>SessionName</b>;Coach;<b>DateStart</b>;<b>DateEnd</b>;Users;Courses
  556. <b>xxx</b>;xxx;<b>xxx;xxx</b>;username1|username2;course1[coach1][username1,username2,...]|course2[coach1][username1,username2,...]
  557. </pre>
  558. </blockquote>
  559. <p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  560. <blockquote>
  561. <pre>
  562. &lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
  563. &lt;Sessions&gt;
  564. &lt;Users&gt;
  565. &lt;User&gt;
  566. &lt;Username&gt;<b>username1</b>&lt;/Username&gt;
  567. &lt;Lastname&gt;xxx&lt;/Lastname&gt;
  568. &lt;Firstname&gt;xxx&lt;/Firstname&gt;
  569. &lt;Password&gt;xxx&lt;/Password&gt;
  570. &lt;Email&gt;xxx@xx.xx&lt;/Email&gt;
  571. &lt;OfficialCode&gt;xxx&lt;/OfficialCode&gt;
  572. &lt;Phone&gt;xxx&lt;/Phone&gt;
  573. &lt;Status&gt;student|teacher&lt;/Status&gt;
  574. &lt;/User&gt;
  575. &lt;/Users&gt;
  576. &lt;Courses&gt;
  577. &lt;Course&gt;
  578. &lt;CourseCode&gt;<b>xxx</b>&lt;/CourseCode&gt;
  579. &lt;CourseTeacher&gt;xxx&lt;/CourseTeacher&gt;
  580. &lt;CourseLanguage&gt;xxx&lt;/CourseLanguage&gt;
  581. &lt;CourseTitle&gt;xxx&lt;/CourseTitle&gt;
  582. &lt;CourseDescription&gt;xxx&lt;/CourseDescription&gt;
  583. &lt;/Course&gt;
  584. &lt;/Courses&gt;
  585. &lt;Session&gt;
  586. <b>&lt;SessionName&gt;xxx&lt;/SessionName&gt;</b>
  587. &lt;Coach&gt;xxx&lt;/Coach&gt;
  588. <b>&lt;DateStart&gt;xxx&lt;/DateStart&gt;</b>
  589. <b>&lt;DateEnd&gt;xxx&lt;/DateEnd&gt;</b>
  590. &lt;User&gt;xxx&lt;/User&gt;
  591. &lt;User&gt;xxx&lt;/User&gt;
  592. &lt;Course&gt;
  593. &lt;CourseCode&gt;coursecode1&lt;/CourseCode&gt;
  594. &lt;Coach&gt;coach1&lt;/Coach&gt;
  595. &lt;User&gt;username1&lt;/User&gt;
  596. &lt;User&gt;username2&lt;/User&gt;
  597. &lt;/Course&gt;
  598. &lt;/Session&gt;
  599. &lt;/Sessions&gt;
  600. </pre>
  601. </blockquote>
  602. </font>
  603. <?php
  604. /*
  605. ==============================================================================
  606. FOOTER
  607. ==============================================================================
  608. */
  609. Display::display_footer();
  610. ?>