ldap_import_students_to_session.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Edition script for sessions categories
  5. * @package chamilo.admin
  6. * Copyright (c) 2007 Mustapha Alouani (supervised by Michel Moreau-Belliard)
  7. */
  8. /**
  9. * Code
  10. */
  11. // name of the language file that needs to be included
  12. $language_file[]='admin';
  13. $language_file[]='registration';
  14. // resetting the course id
  15. $cidReset=true;
  16. require_once('../inc/global.inc.php');
  17. // setting the section (for the tabs)
  18. $this_section = SECTION_PLATFORM_ADMIN;
  19. require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
  20. require_once(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  21. // Access restrictions
  22. api_protect_admin_script();
  23. require('../auth/ldap/authldap.php');
  24. $annee_base=date('Y');
  25. $tool_name = get_lang('LDAPImport');
  26. // setting breadcrumbs
  27. $interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin'));
  28. $htmlHeadXtra[] = '<script language="JavaScript" type="text/javascript">
  29. var buttoncheck = 1;
  30. function checkAll() {
  31. //var boxes = document.form.elements[\'checkboxes[]\'];
  32. var boxes = document.getElementsByName(\'checkboxes[]\');
  33. if (buttoncheck == 0) {
  34. for (i = 0; i < boxes.length; i++) {
  35. boxes[i].checked = true;
  36. }
  37. buttoncheck = 1;
  38. return "'.get_lang('None').'";
  39. }
  40. else {
  41. for (i = 0; i < boxes.length; i++) {
  42. boxes[i].checked = false;
  43. }
  44. buttoncheck = 0;
  45. return " '.get_lang('All').' ";
  46. }
  47. }
  48. </script>';
  49. $annee = $_GET['annee'];
  50. $id_session = $_POST['id_session'];
  51. // form1 annee = 0; composante= 0 etape = 0
  52. //if ($annee == "" && $composante == "" && $etape == "") {
  53. if (empty($annee) && empty($id_session))
  54. {
  55. Display::display_header($tool_name);
  56. echo '<div style="align:center">';
  57. echo Display::return_icon('group.gif', get_lang('LDAPSelectFilterOnUsersOU')).' '.get_lang('LDAPSelectFilterOnUsersOU');
  58. echo '<form method="get" action="'.api_get_self().'"><br />';
  59. echo '<em>'.get_lang('LDAPOUAttributeFilter').' :</em> ';
  60. echo '<input type="text" name="annee" size="4" maxlength="30" value="'.$annee_base.'"> ';
  61. echo '<input type="submit" value="'.get_lang('Submit').'">';
  62. echo '</form>';
  63. echo '</div>';
  64. }
  65. elseif(!empty($annee) && empty($id_session))
  66. {
  67. Display::display_header($tool_name);
  68. echo '<div style="align:center">';
  69. echo Display::return_icon('course.gif', get_lang('SelectSessionToImportUsersTo')).' '.get_lang('SelectSessionToImportUsersTo').'<br />';
  70. echo '<form method="post" action="'.api_get_self().'?annee='.Security::remove_XSS($annee).'"><br />';
  71. echo '<select name="id_session">';
  72. $sessions = SessionManager::get_sessions_list();
  73. foreach ($sessions as $row) {
  74. echo '<option value="'.$row['id'].'">'.api_htmlentities($row['name'], ENT_COMPAT, api_get_system_encoding()).' ('.api_get_local_time($row['access_start_date']).' - '.api_get_local_time($row['access_end_date']).')</option>';
  75. }
  76. echo '</select>';
  77. echo '<input type="submit" value="'.get_lang('Submit').'">';
  78. echo '</form>';
  79. echo '</div>';
  80. }
  81. // form4 annee != 0; composante != 0 etape != 0
  82. //elseif ($annee <> "" && $composante <> "" && $etape <> "" && $listeok != 'yes') {
  83. elseif (!empty($annee) && !empty($id_session) && empty($_POST['confirmed']))
  84. {
  85. Display::display_header($tool_name);
  86. echo '<div style="align: center;">';
  87. echo '<br />';
  88. echo '<br />';
  89. echo '<h3>'.Display::return_icon('group.gif', get_lang('SelectStudents')).' '.get_lang('SelectStudents').'</h3>';
  90. //echo "Connection ...";
  91. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  92. ldap_set_version($ds);
  93. if ($ds) {
  94. $r = false;
  95. $res = ldap_handle_bind($ds, $r);
  96. //$sr = @ ldap_search($ds, "ou=people,$LDAPbasedn", "(|(edupersonprimaryorgunitdn=ou=$etape,ou=$annee,ou=diploma,o=Paris1,$LDAPbasedn)(edupersonprimaryorgunitdn=ou=02PEL,ou=$annee,ou=diploma,o=Paris1,$LDAPbasedn))");
  97. //echo "(ou=*$annee,ou=$composante)";
  98. $sr = @ ldap_search($ds, $ldap_basedn, "(ou=*$annee)");
  99. $info = ldap_get_entries($ds, $sr);
  100. for ($key = 0; $key < $info["count"]; $key ++) {
  101. $nom_form[] = $info[$key]["sn"][0];//api_utf8_decode($info[$key]["sn"][0], api_get_system_encoding());
  102. $prenom_form[] = $info[$key]["givenname"][0];//api_utf8_decode($info[$key]["givenname"][0], api_get_system_encoding());
  103. $email_form[] = $info[$key]["mail"][0];
  104. // Get uid from dn
  105. //$dn_array=ldap_explode_dn($info[$key]["dn"],1);
  106. //$username_form[] = $dn_array[0]; // uid is first key
  107. $username_form[] = $info[$key]['uid'][0];
  108. $outab[] = $info[$key]["eduPersonPrimaryAffiliation"][0]; // Ici "student"
  109. //$val = ldap_get_values_len($ds, $entry, "userPassword");
  110. //$password_form[] = $val[0];
  111. $password_form[] = $info[$key]['userPassword'][0];
  112. }
  113. ldap_unbind($ds);
  114. /*-----------------------------------------------*/
  115. asort($nom_form);
  116. reset($nom_form);
  117. $statut=5;
  118. include ('ldap_form_add_users_group.php');
  119. } else {
  120. echo '<h4>'.get_lang('UnableToConnectTo').' '.$host.'</h4>';
  121. }
  122. echo '<br /><br />';
  123. echo '<a href="ldap_import_students.php?annee=">'.get_lang('BackToNewSearch').'</a>';
  124. echo '<br /><br />';
  125. echo '</div>';
  126. }
  127. elseif (!empty($annee) && !empty($id_session) && ($_POST['confirmed']=='yes'))
  128. {
  129. $id=$_POST['username_form'];
  130. $UserList=array();
  131. $userid_match_login = array();
  132. foreach ($id as $form_index=>$user_id)
  133. {
  134. if(is_array($_POST['checkboxes']) && in_array($form_index,array_values($_POST['checkboxes'])))
  135. {
  136. $tmp = ldap_add_user($user_id);
  137. $UserList[]= $tmp;
  138. $userid_match_login[$tmp] = $user_id;
  139. }
  140. }
  141. if (!empty($_POST['id_session']))
  142. {
  143. $num = 0;
  144. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  145. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  146. foreach($UserList as $user_id)
  147. {
  148. $sql = 'INSERT INTO '.$tbl_session_user.' SET
  149. id_user="'.intval($user_id).'",
  150. id_session = "'.intval($id_session).'"';
  151. $res_user = Database::query($sql);
  152. if ($res_user)
  153. {
  154. $num++;
  155. }
  156. }
  157. if($num>0)
  158. {
  159. $sql = 'UPDATE '.$tbl_session.' SET nbr_users = (nbr_users + '.$num.') WHERE id = '.intval($id_session);
  160. $res = Database::query($sql);
  161. }
  162. header('Location: resume_session.php?id_session='.Security::remove_XSS($_POST['id_session']));
  163. }
  164. /*
  165. else
  166. {
  167. Display :: display_header($tool_name);
  168. if(count($userid_match_login)>0)
  169. {
  170. $message=get_lang('LDAPUsersAddedOrUpdated').':<br />';
  171. foreach($userid_match_login as $user_id => $login)
  172. {
  173. $message .= '- '.$login.'<br />';
  174. }
  175. }
  176. else
  177. {
  178. $message=get_lang('NoUserAdded');
  179. }
  180. Display :: display_normal_message($message,false);
  181. }
  182. */
  183. else
  184. {
  185. Display::display_header($tool_name);
  186. $message=get_lang('NoUserAdded');
  187. Display :: display_normal_message($message,false);
  188. }
  189. echo '<br /><br />';
  190. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  191. echo '<br /><br />';
  192. }
  193. Display::display_footer();
  194. ?>