ldap_import_students_to_session.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Edition script for sessions categories.
  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 '../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('LDAP Import');
  19. // setting breadcrumbs
  20. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  21. $htmlHeadXtra[] = '<script language="JavaScript" type="text/javascript">
  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. $id_session = $_POST['id_session'];
  44. // form1 annee = 0; composante= 0 etape = 0
  45. //if ($annee == "" && $composante == "" && $etape == "") {
  46. if (empty($annee) && empty($id_session)) {
  47. Display::display_header($tool_name);
  48. echo '<div style="align:center">';
  49. echo Display::return_icon('group.gif', get_lang('Select a filter to find a matching string at the end of the OU attribute')).' '.get_lang('Select a filter to find a matching string at the end of the OU attribute');
  50. echo '<form method="get" action="'.api_get_self().'"><br />';
  51. echo '<em>'.get_lang('The OU attribute filter').' :</em> ';
  52. echo '<input type="text" name="annee" size="4" maxlength="30" value="'.$annee_base.'"> ';
  53. echo '<input type="submit" value="'.get_lang('Submit').'">';
  54. echo '</form>';
  55. echo '</div>';
  56. } elseif (!empty($annee) && empty($id_session)) {
  57. Display::display_header($tool_name);
  58. echo '<div style="align:center">';
  59. echo Display::return_icon(
  60. 'course.png',
  61. get_lang('Select the session in which you want to import these users')
  62. ).' '.get_lang('Select the session in which you want to import these users').'<br />';
  63. echo '<form method="post" action="'.api_get_self().'?annee='.Security::remove_XSS($annee).'"><br />';
  64. echo '<select name="id_session">';
  65. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  66. $sql = "SELECT id,name,nbr_courses,access_start_date,access_end_date ".
  67. " FROM $tbl_session ".
  68. " ORDER BY name";
  69. $result = Database::query($sql);
  70. $sessions = Database::store_result($result);
  71. $nbr_results = count($sessions);
  72. foreach ($sessions as $row) {
  73. echo '<option value="'.$row['id'].'">'.api_htmlentities($row['name']).' ('.$row['access_start_date'].' - '.$row['access_end_date'].')</option>';
  74. }
  75. echo '</select>';
  76. echo '<input type="submit" value="'.get_lang('Submit').'">';
  77. echo '</form>';
  78. echo '</div>';
  79. }
  80. // form4 annee != 0; composante != 0 etape != 0
  81. //elseif ($annee <> "" && $composante <> "" && $etape <> "" && $listeok != 'yes') {
  82. elseif (!empty($annee) && !empty($id_session) && empty($_POST['confirmed'])) {
  83. Display::display_header($tool_name);
  84. echo '<div style="align: center;">';
  85. echo '<br />';
  86. echo '<br />';
  87. echo '<h3>'.Display::return_icon('group.gif', get_lang('Select learners')).' '.get_lang('Select learners').'</h3>';
  88. //echo "Connection ...";
  89. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAP Connection Error'));
  90. ldap_set_version($ds);
  91. if ($ds) {
  92. $r = false;
  93. $res = ldap_handle_bind($ds, $r);
  94. //$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))");
  95. //echo "(ou=*$annee,ou=$composante)";
  96. $sr = @ldap_search($ds, $ldap_basedn, "(ou=*$annee)");
  97. $info = ldap_get_entries($ds, $sr);
  98. for ($key = 0; $key < $info["count"]; $key++) {
  99. $nom_form[] = $info[$key]["sn"][0];
  100. $prenom_form[] = $info[$key]["givenname"][0];
  101. $email_form[] = $info[$key]["mail"][0];
  102. // Get uid from dn
  103. //$dn_array=ldap_explode_dn($info[$key]["dn"],1);
  104. //$username_form[] = $dn_array[0]; // uid is first key
  105. $username_form[] = $info[$key]['uid'][0];
  106. $outab[] = $info[$key]["eduPersonPrimaryAffiliation"][0]; // Ici "student"
  107. //$val = ldap_get_values_len($ds, $entry, "userPassword");
  108. //$password_form[] = $val[0];
  109. $password_form[] = $info[$key]['userPassword'][0];
  110. }
  111. ldap_unbind($ds);
  112. asort($nom_form);
  113. reset($nom_form);
  114. $statut = 5;
  115. include 'ldap_form_add_users_group.php';
  116. } else {
  117. echo '<h4>'.get_lang('Unable to connect to').' '.$host.'</h4>';
  118. }
  119. echo '<br /><br />';
  120. echo '<a href="ldap_import_students.php?annee=">'.get_lang('Back to start new search').'</a>';
  121. echo '<br /><br />';
  122. echo '</div>';
  123. } elseif (!empty($annee) && !empty($id_session) && ($_POST['confirmed'] == 'yes')) {
  124. $id = $_POST['username_form'];
  125. $UserList = [];
  126. $userid_match_login = [];
  127. foreach ($id as $form_index => $user_id) {
  128. if (is_array($_POST['checkboxes']) && in_array($form_index, array_values($_POST['checkboxes']))) {
  129. $tmp = ldap_add_user($user_id);
  130. $UserList[] = $tmp;
  131. $userid_match_login[$tmp] = $user_id;
  132. }
  133. }
  134. if (!empty($_POST['id_session'])) {
  135. $num = 0;
  136. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  137. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  138. foreach ($UserList as $user_id) {
  139. $res_user = Database::insert(
  140. $tbl_session_user,
  141. [
  142. 'session_id' => intval($id_session),
  143. 'user_id' => intval($user_id),
  144. 'registered_at' => api_get_utc_datetime(),
  145. ]
  146. );
  147. if ($res_user !== false) {
  148. $num++;
  149. }
  150. }
  151. if ($num > 0) {
  152. $sql = 'UPDATE '.$tbl_session.' SET nbr_users = (nbr_users + '.$num.') WHERE id = '.intval($id_session);
  153. $res = Database::query($sql);
  154. }
  155. header('Location: resume_session.php?id_session='.Security::remove_XSS($_POST['id_session']));
  156. exit;
  157. } else {
  158. $message = get_lang('No user added');
  159. Display::addFlash(Display::return_message($message, 'normal', false));
  160. Display::display_header($tool_name);
  161. }
  162. echo '<br /><br />';
  163. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('Back to start new search').'</a>';
  164. echo '<br /><br />';
  165. }
  166. Display::display_footer();