ldap_import_students.php 11 KB

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