ldap_import_students.php 11 KB

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