ldap_import_students.php 10 KB

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