ldap_import_students.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script to import students from LDAP.
  5. *
  6. * @package chamilo.admin
  7. * Copyright (c) 2007 Mustapha Alouani (supervised by Michel Moreau-Belliard)
  8. */
  9. // resetting the course id
  10. $cidReset = true;
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. // setting the section (for the tabs)
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. // Access restrictions
  15. api_protect_admin_script();
  16. require '../auth/ldap/authldap.php';
  17. $annee_base = date('Y');
  18. $tool_name = get_lang('LDAPImport');
  19. // setting breadcrumbs
  20. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  21. $htmlHeadXtra[] = '<script>
  22. var buttoncheck = 1;
  23. function checkAll() {
  24. //var boxes = document.form.elements[\'checkboxes[]\'];
  25. var boxes = document.getElementsByName(\'checkboxes[]\');
  26. if (buttoncheck == 0) {
  27. for (i = 0; i < boxes.length; i++) {
  28. boxes[i].checked = true;
  29. }
  30. buttoncheck = 1;
  31. return "'.get_lang('None').'";
  32. }
  33. else {
  34. for (i = 0; i < boxes.length; i++) {
  35. boxes[i].checked = false;
  36. }
  37. buttoncheck = 0;
  38. return " '.get_lang('All').' ";
  39. }
  40. }
  41. </script>';
  42. $annee = $_GET['annee'];
  43. $composante = $_GET['composante'];
  44. $etape = $_GET['etape'];
  45. $course = $_POST['course'];
  46. // form1 annee = 0; composante= 0 etape = 0
  47. //if ($annee == "" && $composante == "" && $etape == "") {
  48. if (empty($annee) && empty($course)) {
  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. } elseif (!empty($annee) && empty($course)) {
  62. Display::display_header($tool_name);
  63. echo '<div style="align:center">';
  64. echo Display::return_icon('course.png', get_lang('SelectCourseToImportUsersTo')).' '.get_lang('SelectCourseToImportUsersTo').'<br />';
  65. echo '<form method="post" action="'.api_get_self().'?annee='.Security::remove_XSS($annee).'"><br />';
  66. echo '<select name="course">';
  67. $courses = CourseManager::get_courses_list();
  68. foreach ($courses as $row) {
  69. echo '<option value="'.$row['code'].'">'.api_htmlentities($row['title']).'</option>';
  70. }
  71. echo '</select>';
  72. echo '<input type="submit" value="'.get_lang('Submit').'">';
  73. echo '</form>';
  74. echo '</div>';
  75. } elseif (!empty($annee) && !empty($course) && empty($_POST['confirmed'])) {
  76. // form4 annee != 0; composante != 0 etape != 0
  77. //elseif ($annee <> "" && $composante <> "" && $etape <> "" && $listeok != 'yes') {
  78. Display::display_header($tool_name);
  79. echo '<div style="align: center;">';
  80. echo '<br />';
  81. echo '<br />';
  82. echo '<h3>'.Display::return_icon('group.gif', get_lang('SelectStudents')).' '.get_lang('SelectStudents').'</h3>';
  83. //echo "Connection ...";
  84. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  85. ldap_set_version($ds);
  86. if ($ds) {
  87. $r = false;
  88. $res = ldap_handle_bind($ds, $r);
  89. //$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))");
  90. //echo "(ou=*$annee,ou=$composante)";
  91. $sr = @ldap_search($ds, $ldap_basedn, "(ou=*$annee)");
  92. $info = ldap_get_entries($ds, $sr);
  93. for ($key = 0; $key < $info["count"]; $key++) {
  94. $nom_form[] = $info[$key]["sn"][0];
  95. $prenom_form[] = $info[$key]["givenname"][0];
  96. $email_form[] = $info[$key]["mail"][0];
  97. // Get uid from dn
  98. //$dn_array=ldap_explode_dn($info[$key]["dn"],1);
  99. //$username_form[] = $dn_array[0]; // uid is first key
  100. $username_form[] = $info[$key]['uid'][0];
  101. $outab[] = $info[$key]["eduPersonPrimaryAffiliation"][0]; // Ici "student"
  102. //$val = ldap_get_values_len($ds, $entry, "userPassword");
  103. //$password_form[] = $val[0];
  104. $password_form[] = $info[$key]['userPassword'][0];
  105. }
  106. ldap_unbind($ds);
  107. asort($nom_form);
  108. reset($nom_form);
  109. $statut = 5;
  110. include 'ldap_form_add_users_group.php';
  111. } else {
  112. echo '<h4>'.get_lang('UnableToConnectTo').' '.$host.'</h4>';
  113. }
  114. echo '<br /><br />';
  115. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  116. echo '<br /><br />';
  117. echo '</div>';
  118. } elseif (!empty($annee) && !empty($course) && ($_POST['confirmed'] == 'yes')) {
  119. $id = $_POST['username_form'];
  120. $UserList = [];
  121. $userid_match_login = [];
  122. foreach ($id as $form_index => $user_id) {
  123. if (is_array($_POST['checkboxes']) && in_array($form_index, array_values($_POST['checkboxes']))) {
  124. $tmp = ldap_add_user($user_id);
  125. $UserList[] = $tmp;
  126. $userid_match_login[$tmp] = $user_id;
  127. }
  128. }
  129. if (!empty($_POST['course'])) {
  130. foreach ($UserList as $user_id) {
  131. CourseManager::subscribeUser($user_id, $_POST['course']);
  132. }
  133. header('Location: course_information.php?code='.Security::remove_XSS($_POST['course']));
  134. exit;
  135. } else {
  136. $message = get_lang('NoUserAdded');
  137. Display::addFlash(Display::return_message($message, 'normal', false));
  138. Display::display_header($tool_name);
  139. }
  140. echo '<br /><br />';
  141. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  142. echo '<br /><br />';
  143. }
  144. Display::display_footer();