ldap_import_students.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script to import students from LDAP
  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. $composante = $_GET['composante'];
  51. $etape = $_GET['etape'];
  52. $course = $_POST['course'];
  53. // form1 annee = 0; composante= 0 etape = 0
  54. //if ($annee == "" && $composante == "" && $etape == "") {
  55. if (empty($annee) && empty($course)) {
  56. Display::display_header($tool_name);
  57. echo '<div style="align:center">';
  58. Display::display_icon('group.gif', get_lang('LDAPSelectFilterOnUsersOU'));
  59. echo get_lang('LDAPSelectFilterOnUsersOU');
  60. //echo '<em>'.get_lang('ToDoThisYouMustEnterYearComponentAndComponentStep').'</em><br />';
  61. ///echo get_lang('FollowEachOfTheseStepsStepByStep').'<br />';
  62. echo '<form method="get" action="'.api_get_self().'"><br />';
  63. echo '<em>'.get_lang('LDAPOUAttributeFilter').' :</em> ';
  64. echo '<input type="text" name="annee" size="4" maxlength="30" value="'.$annee_base.'"><br />';
  65. echo '<input type="submit" value="'.get_lang('Submit').'">';
  66. echo '</form>';
  67. echo '</div>';
  68. } /*
  69. elseif ($annee <> "" && $composante == "" && $etape == "") // form 2 annee != 0; composante= 0 etape = 0
  70. {
  71. Display::display_header($tool_name);
  72. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  73. ldap_set_version($ds);
  74. if ($ds) {
  75. $r = false;
  76. $res = ldap_handle_bind($ds, $r);
  77. //$sr = @ ldap_search($ds, "o=groups,$ldap_basedn", "(&(description=etape*)(cn=*$annee))", array ('cn'));
  78. //$sr = @ ldap_search($ds, "ou=$annee, ou=diploma, o=paris1, $ldap_basedn", "ou=02*", array ('description'));
  79. //$sr = @ ldap_search($ds, "ou=structures, o=paris1, $ldap_basedn", "businessCategory=pedagogy", array ('ou','description'));
  80. $sr = ldap_search($ds, $ldap_basedn, "(ou=*$annee)", array ('ou','description'));
  81. // "ou=2006,ou=diploma,o=Paris1,dc=univ-paris1,dc=fr
  82. $info = ldap_get_entries($ds, $sr);
  83. $composante = array();
  84. for($i = 0; $i < $info['count']; $i ++)
  85. {
  86. //presupose LDAP server is UTF-8
  87. $composante[$info[$i]['ou'][0]] = api_utf8_decode($info[$i]['description'][0], api_get_system_encoding());
  88. }
  89. $oucompotab3=$composante;
  90. echo '<div style="align: center">';
  91. echo '<br />';
  92. echo '<h3>'.Display::return_icon('group.gif', get_lang('SelectComponent')).' '.get_lang('SelectComponent').'</h3>';
  93. echo '<form method="get" action="'.api_get_self().'">';
  94. echo '<b>'.get_lang('RegistrationYear').'</b> : ';
  95. echo '<input type="text" name="annee" size="4" maxlength="30" value="'.$annee.'">';
  96. echo '<b>'.get_lang('Component').' : </b> ';
  97. echo '<select name="composante" size="1">';
  98. while (list ($key, $val) = each($oucompotab3)) {
  99. echo '<option value="'.$key.'">'.$oucompotab3[$key].'</option>';
  100. }
  101. echo '</select>';
  102. echo '<br />';
  103. echo '<br />';
  104. echo '<input type="submit" name="valider" value="'.get_lang('Submit').'">';
  105. echo '</form>';
  106. ldap_close($ds);
  107. }
  108. echo '<br />';
  109. echo '<br />';
  110. echo '<br />';
  111. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  112. echo '<br />';
  113. echo '</div>';
  114. }
  115. elseif ($annee <> "" && $composante <> "" && $etape == "") // form3 :annee!=0composante=0etape=0
  116. {
  117. Display::display_header($tool_name);
  118. echo '<div style="align: center">';
  119. echo '<h3>'.Display::return_icon('group.gif', get_lang('SearchResults')).' '.get_lang('SearchResults').'</h3>';
  120. $ds = ldap_connect($ldap_host, $ldap_port);
  121. ldap_set_version($ds);
  122. if ($ds) {
  123. $r = false;
  124. $res = ldap_handle_bind($ds, $r);
  125. // $sr = @ ldap_search($ds, "ou=groups, $LDAPbasedn", "(&(cn=*$annee*)(cn=*$composante*))");
  126. $sr = @ ldap_search($ds, $ldap_basedn, "ou=$composante", array ('ou','description'));
  127. //echo "Le nombre de resultats est : ".ldap_count_entries($ds,$sr)."<p>";
  128. $info = ldap_get_entries($ds, $sr);
  129. for ($i = 0; $i <= $info['count']; $i ++) {
  130. $description = $info[$i]['description'];
  131. $ouetapetab[$i] = $description[0];
  132. $description2 = $info[$i]['ou'];
  133. $ouetapetab2[$i] = $description2[0];
  134. }
  135. asort($ouetapetab);
  136. reset($ouetapetab);
  137. echo '<form method="get" action="'.api_get_self().'">';
  138. echo '<b>'.get_lang('RegistrationYear').':</b><input type="text" name="annee" size="4" maxlength="30" value="'.$annee.'">';
  139. echo '<br /><br />';
  140. echo '<b>'.get_lang('Component').' :</b><input type="text" name="composante" size="4" maxlength="30" value="'.$composante.'">';
  141. echo '<br />';
  142. echo '<h4>'.get_lang('SelectStepAcademicYear').'</h4>';
  143. echo '<br />';
  144. echo '<b>'.get_lang('Step').': </b>';
  145. echo '<select name="etape" size="1">';
  146. $tempcomp = "";
  147. while (list ($key, $val) = each($ouetapetab)) {
  148. if ($ouetapetab[$key] != $tempcomp) {
  149. $etape = $ouetapetab2[$key];
  150. $tempcomp = '"'.$ouetapetab[$key].'"';
  151. $tempcomp = api_utf8_decode($tempcomp, api_get_system_encoding());
  152. $annee = str_word_count($etape, 1);
  153. echo '<option value="'.$etape.'">'.$tempcomp.'</option>';
  154. }
  155. }
  156. echo '</select>';
  157. echo '<input type="hidden" name="displayname" value="'.$displayname.'">';
  158. echo '<br />';
  159. echo '<input type="submit" name="envoi" value="'.get_lang('Submit').'">';
  160. echo '</form>';
  161. ldap_close($ds);
  162. } else {
  163. // echo "<h4>Unable to connect to LDAP server</h4>";
  164. }
  165. echo '<br />';
  166. echo '<br />';
  167. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  168. echo '</div>';
  169. }
  170. */
  171. elseif (!empty($annee) && empty($course)) {
  172. Display::display_header($tool_name);
  173. echo '<div style="align:center">';
  174. echo Display::return_icon('course.gif', get_lang('SelectCourseToImportUsersTo')).' '.get_lang(
  175. 'SelectCourseToImportUsersTo'
  176. ).'<br />';
  177. echo '<form method="post" action="'.api_get_self().'?annee='.Security::remove_XSS($annee).'"><br />';
  178. echo '<select name="course">';
  179. $courses = CourseManager::get_courses_list();
  180. foreach ($courses as $row) {
  181. echo '<option value="'.$row['code'].'">'.api_htmlentities(
  182. $row['title'],
  183. ENT_COMPAT,
  184. api_get_system_encoding()
  185. ).'</option>';
  186. }
  187. echo '</select>';
  188. echo '<input type="submit" value="'.get_lang('Submit').'">';
  189. echo '</form>';
  190. echo '</div>';
  191. } // form4 annee != 0; composante != 0 etape != 0
  192. //elseif ($annee <> "" && $composante <> "" && $etape <> "" && $listeok != 'yes') {
  193. elseif (!empty($annee) && !empty($course) && empty($_POST['confirmed'])) {
  194. Display::display_header($tool_name);
  195. echo '<div style="align: center;">';
  196. echo '<br />';
  197. echo '<br />';
  198. echo '<h3>'.Display::return_icon('group.gif', get_lang('SelectStudents')).' '.get_lang('SelectStudents').'</h3>';
  199. //echo "Connection ...";
  200. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  201. ldap_set_version($ds);
  202. if ($ds) {
  203. $r = false;
  204. $res = ldap_handle_bind($ds, $r);
  205. //$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))");
  206. //echo "(ou=*$annee,ou=$composante)";
  207. $sr = @ ldap_search($ds, $ldap_basedn, "(ou=*$annee)");
  208. $info = ldap_get_entries($ds, $sr);
  209. for ($key = 0; $key < $info["count"]; $key++) {
  210. $nom_form[] = $info[$key]["sn"][0]; //api_utf8_decode($info[$key]["sn"][0], api_get_system_encoding());
  211. $prenom_form[] = $info[$key]["givenname"][0]; //api_utf8_decode($info[$key]["givenname"][0], api_get_system_encoding());
  212. $email_form[] = $info[$key]["mail"][0];
  213. // Get uid from dn
  214. //$dn_array=ldap_explode_dn($info[$key]["dn"],1);
  215. //$username_form[] = $dn_array[0]; // uid is first key
  216. $username_form[] = $info[$key]['uid'][0];
  217. $outab[] = $info[$key]["eduPersonPrimaryAffiliation"][0]; // Ici "student"
  218. //$val = ldap_get_values_len($ds, $entry, "userPassword");
  219. //$password_form[] = $val[0];
  220. $password_form[] = $info[$key]['userPassword'][0];
  221. }
  222. ldap_unbind($ds);
  223. asort($nom_form);
  224. reset($nom_form);
  225. $statut = 5;
  226. include('ldap_form_add_users_group.php');
  227. } else {
  228. echo '<h4>'.get_lang('UnableToConnectTo').' '.$host.'</h4>';
  229. }
  230. echo '<br /><br />';
  231. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  232. echo '<br /><br />';
  233. echo '</div>';
  234. } elseif (!empty($annee) && !empty($course) && ($_POST['confirmed'] == 'yes')) {
  235. $id = $_POST['username_form'];
  236. $UserList = array();
  237. $userid_match_login = array();
  238. foreach ($id as $form_index => $user_id) {
  239. if (is_array($_POST['checkboxes']) && in_array($form_index, array_values($_POST['checkboxes']))) {
  240. $tmp = ldap_add_user($user_id);
  241. $UserList[] = $tmp;
  242. $userid_match_login[$tmp] = $user_id;
  243. }
  244. }
  245. $courseInfo = api_get_course_info($_POST['course']);
  246. if (!empty($courseInfo)) {
  247. foreach ($UserList as $user_id) {
  248. CourseManager::add_user_to_course($user_id, $courseInfo['real_id']);
  249. }
  250. header('Location: course_information.php?code='.Security::remove_XSS($_POST['course']));
  251. exit;
  252. } /*
  253. else
  254. {
  255. Display :: display_header($tool_name);
  256. if(count($userid_match_login)>0)
  257. {
  258. $message=get_lang('LDAPUsersAddedOrUpdated').':<br />';
  259. foreach($userid_match_login as $user_id => $login)
  260. {
  261. $message .= '- '.$login.'<br />';
  262. }
  263. }
  264. else
  265. {
  266. $message=get_lang('NoUserAdded');
  267. }
  268. Display :: display_normal_message($message,false);
  269. }
  270. */
  271. else {
  272. Display::display_header($tool_name);
  273. $message = get_lang('NoUserAdded');
  274. Display :: display_normal_message($message, false);
  275. }
  276. echo '<br /><br />';
  277. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  278. echo '<br /><br />';
  279. }
  280. Display::display_footer();