add_users_to_session.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 University of Ghent (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. // name of the language file that needs to be included
  25. $language_file='admin';
  26. // resetting the course id
  27. $cidReset=true;
  28. // including some necessary dokeos files
  29. require('../inc/global.inc.php');
  30. // setting the section (for the tabs)
  31. $this_section = SECTION_PLATFORM_ADMIN;
  32. // Access restrictions
  33. api_protect_admin_script();
  34. // setting breadcrumbs
  35. $interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  36. $interbreadcrumb[]=array('url' => "session_list.php","name" => "Liste des sessions");
  37. // Database Table Definitions
  38. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  39. $tbl_session_rel_class = Database::get_main_table(TABLE_MAIN_SESSION_CLASS);
  40. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  41. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  42. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  43. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  44. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  45. $tbl_class = Database::get_main_table(TABLE_MAIN_CLASS);
  46. $tbl_class_user = Database::get_main_table(TABLE_MAIN_CLASS_USER);
  47. // setting the name of the tool
  48. $tool_name=get_lang('SubscribeUsersToSession');
  49. $id_session=intval($_GET['id_session']);
  50. $formSent=0;
  51. $errorMsg=$firstLetterUser=$firstLetterSession='';
  52. $UserList=$SessionList=array();
  53. $users=$sessions=array();
  54. $noPHP_SELF=true;
  55. if($_POST['formSent'])
  56. {
  57. $formSent=$_POST['formSent'];
  58. $firstLetterUser=$_POST['firstLetterUser'];
  59. $firstLetterSession=$_POST['firstLetterSession'];
  60. $UserList=$_POST['sessionUsersList'];
  61. $ClassList=$_POST['sessionClassesList'];
  62. if(!is_array($UserList))
  63. {
  64. $UserList=array();
  65. }
  66. if($formSent == 1)
  67. {
  68. $result = api_sql_query("SELECT id_user FROM $tbl_session_rel_user WHERE id_session='$id_session'");
  69. $existingUsers = array();
  70. while($row = mysql_fetch_array($result)){
  71. $existingUsers[] = $row['id_user'];
  72. }
  73. $result=api_sql_query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'",__FILE__,__LINE__);
  74. $CourseList=array();
  75. while($row=mysql_fetch_array($result))
  76. {
  77. $CourseList[]=$row['course_code'];
  78. }
  79. foreach($CourseList as $enreg_course)
  80. {
  81. $nbr_users=0;
  82. foreach($UserList as $enreg_user)
  83. {
  84. if(!in_array($enreg_user, $existingUsers)){
  85. api_sql_query("INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$enreg_user')",__FILE__,__LINE__);
  86. if(mysql_affected_rows())
  87. {
  88. $nbr_users++;
  89. }
  90. }
  91. }
  92. foreach($existingUsers as $existing_user){
  93. if(!in_array($existing_user, $UserList)){
  94. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course' AND id_user='$existing_user'";
  95. api_sql_query($sql);
  96. if(mysql_affected_rows())
  97. {
  98. $nbr_users--;
  99. }
  100. }
  101. }
  102. $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course'";
  103. $rs = api_sql_query($sql, __FILE__, __LINE__);
  104. list($nbr_users) = mysql_fetch_array($rs);
  105. api_sql_query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
  106. }
  107. api_sql_query("DELETE FROM $tbl_session_rel_user WHERE id_session = $id_session");
  108. $nbr_users = 0;
  109. foreach($UserList as $enreg_user){
  110. $nbr_users++;
  111. api_sql_query("INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$enreg_user')",__FILE__,__LINE__);
  112. }
  113. $nbr_users = count($UserList);
  114. api_sql_query("UPDATE $tbl_session SET nbr_users= $nbr_users WHERE id='$id_session' ",__FILE__,__LINE__);
  115. //if(empty($_GET['add']))
  116. //header('Location: '.$_GET['page'].'?id_session='.$id_session);
  117. //else
  118. header('Location: resume_session.php?id_session='.$id_session);
  119. }
  120. }
  121. Display::display_header($tool_name);
  122. api_display_tool_title($tool_name);
  123. $nosessionUsersList = $sessionUsersList = $nosessionClassesList = $sessionClassesList = array();
  124. //classes
  125. $sql="SELECT id, name FROM $tbl_class ORDER BY name";
  126. $result=api_sql_query($sql,__FILE__,__LINE__);
  127. $Classes=api_store_result($result);
  128. foreach($Classes as $classe)
  129. if($classe['id_session'] == $id_session)
  130. $sessionClassesList[$classe['id']] = $classe ;
  131. foreach($Classes as $classe)
  132. if(empty($sessionClassesList[$classe['user_id']]) && empty($nosessionClassesList[$classe['user_id']]))
  133. $nosessionClassesList[$classe['id']] = $classe ;
  134. //users
  135. $sql="SELECT user_id, lastname, firstname, username, id_session
  136. FROM $tbl_user
  137. LEFT JOIN $tbl_session_rel_user
  138. ON $tbl_session_rel_user.id_user = $tbl_user.user_id
  139. ORDER BY lastname,firstname,username";
  140. $result=api_sql_query($sql,__FILE__,__LINE__);
  141. $Users=api_store_result($result);
  142. foreach($Users as $user)
  143. if($user['id_session'] == $id_session)
  144. $sessionUsersList[$user['user_id']] = $user ;
  145. foreach($Users as $user)
  146. if(empty($sessionUsersList[$user['user_id']]) && empty($nosessionUsersList[$user['user_id']]))
  147. $nosessionUsersList[$user['user_id']] = $user ;
  148. ?>
  149. <form name="formulaire" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo $_GET['page'] ?>&id_session=<?php echo $id_session; ?><?php if(!empty($_GET['add'])) echo '&add=true' ; ?>" style="margin:0px;">
  150. <input type="hidden" name="formSent" value="1" />
  151. <?php
  152. if(!empty($errorMsg))
  153. {
  154. Display::display_normal_message($errorMsg); //main API
  155. }
  156. ?>
  157. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  158. <!-- Users -->
  159. <tr>
  160. <td align="center"><b><?php echo get_lang('UserListInPlatform') ?> :</b>
  161. </td>
  162. <td></td>
  163. <td align="center"><b><?php echo get_lang('UserListInSession') ?> :</b></td>
  164. </tr>
  165. <tr>
  166. <td align="center">
  167. <div id="content_source">
  168. <select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15" style="width:300px;">
  169. <?php
  170. foreach($nosessionUsersList as $enreg)
  171. {
  172. ?>
  173. <option value="<?php echo $enreg['user_id']; ?>"><?php echo $enreg['lastname'].' '.$enreg['firstname'].' ('.$enreg['username'].')'; ?></option>
  174. <?php
  175. }
  176. unset($nosessionUsersList);
  177. ?>
  178. </select>
  179. </div>
  180. </td>
  181. <td width="10%" valign="middle" align="center">
  182. <input type="button" onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))" value=">>" />
  183. <br /><br />
  184. <input type="button" onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))" value="<<" />
  185. <br /><br /><br /><br /><br /><br />
  186. </td>
  187. <td align="center">
  188. <select id="destination_users" name="sessionUsersList[]" multiple="multiple" size="15" style="width:300px;">
  189. <?php
  190. foreach($sessionUsersList as $enreg)
  191. {
  192. ?>
  193. <option value="<?php echo $enreg['user_id']; ?>"><?php echo $enreg['lastname'].' '.$enreg['firstname'].' ('.$enreg['username'].')'; ?></option>
  194. <?php
  195. }
  196. unset($sessionUsersList);
  197. ?>
  198. </select></td>
  199. </tr>
  200. <tr>
  201. <td colspan="3" align="center">
  202. <br />
  203. <?php
  204. if(isset($_GET['add']))
  205. echo '<input type="button" value="'.get_lang("FinishSessionCreation").'" onclick="valide()" />';
  206. else
  207. echo '<input type="button" value="'.get_lang('Ok').'" onclick="valide()" />';
  208. ?>
  209. </td>
  210. </tr>
  211. </table>
  212. </form>
  213. <script type="text/javascript">
  214. <!--
  215. function moveItem(origin , destination){
  216. for(var i = 0 ; i<origin.options.length ; i++) {
  217. if(origin.options[i].selected) {
  218. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  219. origin.options[i]=null;
  220. i = i-1;
  221. }
  222. }
  223. destination.selectedIndex = -1;
  224. sortOptions(destination.options);
  225. }
  226. function sortOptions(options) {
  227. newOptions = new Array();
  228. for (i = 0 ; i<options.length ; i++)
  229. newOptions[i] = options[i];
  230. newOptions = newOptions.sort(mysort);
  231. options.length = 0;
  232. for(i = 0 ; i < newOptions.length ; i++)
  233. options[i] = newOptions[i];
  234. }
  235. function mysort(a, b){
  236. if(a.text.toLowerCase() > b.text.toLowerCase()){
  237. return 1;
  238. }
  239. if(a.text.toLowerCase() < b.text.toLowerCase()){
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. function valide(){
  245. var options = document.getElementById('destination_users').options;
  246. for (i = 0 ; i<options.length ; i++)
  247. options[i].selected = true;
  248. /*
  249. var options = document.getElementById('destination_classes').options;
  250. for (i = 0 ; i<options.length ; i++)
  251. options[i].selected = true;
  252. */
  253. document.forms.formulaire.submit();
  254. }
  255. function loadUsersInSelect(select){
  256. var xhr_object = null;
  257. if(window.XMLHttpRequest) // Firefox
  258. xhr_object = new XMLHttpRequest();
  259. else if(window.ActiveXObject) // Internet Explorer
  260. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  261. else // XMLHttpRequest non supporté par le navigateur
  262. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  263. //xhr_object.open("GET", "loadUsersInSelect.ajax.php?id_session=<?php echo $id_session ?>&letter="+select.options[select.selectedIndex].text, false);
  264. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  265. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  266. nosessionUsers = makepost(document.getElementById('origin_users'));
  267. sessionUsers = makepost(document.getElementById('destination_users'));
  268. nosessionClasses = makepost(document.getElementById('origin_classes'));
  269. sessionClasses = makepost(document.getElementById('destination_classes'));
  270. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  271. xhr_object.onreadystatechange = function() {
  272. if(xhr_object.readyState == 4) {
  273. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  274. //alert(xhr_object.responseText);
  275. }
  276. }
  277. }
  278. function makepost(select){
  279. var options = select.options;
  280. var ret = "";
  281. for (i = 0 ; i<options.length ; i++)
  282. ret = ret + options[i].value +'::'+options[i].text+";;";
  283. return ret;
  284. }
  285. -->
  286. </script>
  287. <?php
  288. /*
  289. ==============================================================================
  290. FOOTER
  291. ==============================================================================
  292. */
  293. Display::display_footer();
  294. ?>