ldap_import_students.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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>
  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. Display::display_header($tool_name);
  49. echo '<div style="align:center">';
  50. Display::display_icon('group.gif', get_lang('LDAPSelectFilterOnUsersOU'));
  51. echo get_lang('LDAPSelectFilterOnUsersOU');
  52. //echo '<em>'.get_lang('ToDoThisYouMustEnterYearComponentAndComponentStep').'</em><br />';
  53. ///echo get_lang('FollowEachOfTheseStepsStepByStep').'<br />';
  54. echo '<form method="get" action="'.api_get_self().'"><br />';
  55. echo '<em>'.get_lang('LDAPOUAttributeFilter').' :</em> ';
  56. echo '<input type="text" name="annee" size="4" maxlength="30" value="'.$annee_base.'"><br />';
  57. echo '<input type="submit" value="'.get_lang('Submit').'">';
  58. echo '</form>';
  59. echo '</div>';
  60. } elseif (!empty($annee) && empty($course)) {
  61. Display::display_header($tool_name);
  62. echo '<div style="align:center">';
  63. echo Display::return_icon('course.png', get_lang('SelectCourseToImportUsersTo')).' '.get_lang('SelectCourseToImportUsersTo').'<br />';
  64. echo '<form method="post" action="'.api_get_self().'?annee='.Security::remove_XSS($annee).'"><br />';
  65. echo '<select name="course">';
  66. $courses = CourseManager::get_courses_list();
  67. foreach ($courses as $row) {
  68. echo '<option value="'.$row['code'].'">'.api_htmlentities($row['title']).'</option>';
  69. }
  70. echo '</select>';
  71. echo '<input type="submit" value="'.get_lang('Submit').'">';
  72. echo '</form>';
  73. echo '</div>';
  74. } elseif (!empty($annee) && !empty($course) && empty($_POST['confirmed'])) {
  75. // form4 annee != 0; composante != 0 etape != 0
  76. //elseif ($annee <> "" && $composante <> "" && $etape <> "" && $listeok != 'yes') {
  77. Display::display_header($tool_name);
  78. echo '<div style="align: center;">';
  79. echo '<br />';
  80. echo '<br />';
  81. echo '<h3>'.Display::return_icon('group.gif', get_lang('SelectStudents')).' '.get_lang('SelectStudents').'</h3>';
  82. //echo "Connection ...";
  83. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  84. ldap_set_version($ds);
  85. if ($ds) {
  86. $r = false;
  87. $res = ldap_handle_bind($ds, $r);
  88. //$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))");
  89. //echo "(ou=*$annee,ou=$composante)";
  90. $sr = @ ldap_search($ds, $ldap_basedn, "(ou=*$annee)");
  91. $info = ldap_get_entries($ds, $sr);
  92. for ($key = 0; $key < $info["count"]; $key++) {
  93. $nom_form[] = $info[$key]["sn"][0];
  94. $prenom_form[] = $info[$key]["givenname"][0];
  95. $email_form[] = $info[$key]["mail"][0];
  96. // Get uid from dn
  97. //$dn_array=ldap_explode_dn($info[$key]["dn"],1);
  98. //$username_form[] = $dn_array[0]; // uid is first key
  99. $username_form[] = $info[$key]['uid'][0];
  100. $outab[] = $info[$key]["eduPersonPrimaryAffiliation"][0]; // Ici "student"
  101. //$val = ldap_get_values_len($ds, $entry, "userPassword");
  102. //$password_form[] = $val[0];
  103. $password_form[] = $info[$key]['userPassword'][0];
  104. }
  105. ldap_unbind($ds);
  106. asort($nom_form);
  107. reset($nom_form);
  108. $statut = 5;
  109. include 'ldap_form_add_users_group.php';
  110. } else {
  111. echo '<h4>'.get_lang('UnableToConnectTo').' '.$host.'</h4>';
  112. }
  113. echo '<br /><br />';
  114. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  115. echo '<br /><br />';
  116. echo '</div>';
  117. } elseif (!empty($annee) && !empty($course) && ($_POST['confirmed'] == 'yes')) {
  118. $id = $_POST['username_form'];
  119. $UserList = array();
  120. $userid_match_login = array();
  121. foreach ($id as $form_index => $user_id) {
  122. if (is_array($_POST['checkboxes']) && in_array($form_index, array_values($_POST['checkboxes']))) {
  123. $tmp = ldap_add_user($user_id);
  124. $UserList[] = $tmp;
  125. $userid_match_login[$tmp] = $user_id;
  126. }
  127. }
  128. if (!empty($_POST['course'])) {
  129. foreach ($UserList as $user_id) {
  130. CourseManager::add_user_to_course($user_id, $_POST['course']);
  131. }
  132. header('Location: course_information.php?code='.Security::remove_XSS($_POST['course']));
  133. } else {
  134. $message = get_lang('NoUserAdded');
  135. Display::addFlash(Display::return_message($message, 'normal', false));
  136. Display::display_header($tool_name);
  137. }
  138. echo '<br /><br />';
  139. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  140. echo '<br /><br />';
  141. }
  142. Display::display_footer();