ldap_import_students.php 10 KB

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