ldap_import_students_to_session.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Edition script for sessions categories
  5. * @package chamilo.admin
  6. * Copyright (c) 2007 Mustapha Alouani (supervised by Michel Moreau-Belliard)
  7. */
  8. /**
  9. * Code
  10. */
  11. // resetting the course id
  12. $cidReset = true;
  13. require_once('../inc/global.inc.php');
  14. // setting the section (for the tabs)
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. // Access restrictions
  17. api_protect_admin_script();
  18. require('../auth/ldap/authldap.php');
  19. $annee_base = date('Y');
  20. $tool_name = get_lang('LDAPImport');
  21. // setting breadcrumbs
  22. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  23. $htmlHeadXtra[] = '<script language="JavaScript" type="text/javascript">
  24. var buttoncheck = 1;
  25. function checkAll() {
  26. //var boxes = document.form.elements[\'checkboxes[]\'];
  27. var boxes = document.getElementsByName(\'checkboxes[]\');
  28. if (buttoncheck == 0) {
  29. for (i = 0; i < boxes.length; i++) {
  30. boxes[i].checked = true;
  31. }
  32. buttoncheck = 1;
  33. return "'.get_lang('None').'";
  34. }
  35. else {
  36. for (i = 0; i < boxes.length; i++) {
  37. boxes[i].checked = false;
  38. }
  39. buttoncheck = 0;
  40. return " '.get_lang('All').' ";
  41. }
  42. }
  43. </script>';
  44. $annee = $_GET['annee'];
  45. $id_session = $_POST['id_session'];
  46. // form1 annee = 0; composante= 0 etape = 0
  47. //if ($annee == "" && $composante == "" && $etape == "") {
  48. if (empty($annee) && empty($id_session))
  49. {
  50. Display::display_header($tool_name);
  51. echo '<div style="align:center">';
  52. echo Display::return_icon('group.gif', get_lang('LDAPSelectFilterOnUsersOU')).' '.get_lang('LDAPSelectFilterOnUsersOU');
  53. echo '<form method="get" action="'.api_get_self().'"><br />';
  54. echo '<em>'.get_lang('LDAPOUAttributeFilter').' :</em> ';
  55. echo '<input type="text" name="annee" size="4" maxlength="30" value="'.$annee_base.'"> ';
  56. echo '<input type="submit" value="'.get_lang('Submit').'">';
  57. echo '</form>';
  58. echo '</div>';
  59. }
  60. elseif (!empty($annee) && empty($id_session))
  61. {
  62. Display::display_header($tool_name);
  63. echo '<div style="align:center">';
  64. echo Display::return_icon(
  65. 'course.png',
  66. get_lang('SelectSessionToImportUsersTo')).' '.get_lang('SelectSessionToImportUsersTo').'<br />';
  67. echo '<form method="post" action="'.api_get_self().'?annee='.Security::remove_XSS($annee).'"><br />';
  68. echo '<select name="id_session">';
  69. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  70. $sql = "SELECT id,name,nbr_courses,access_start_date,access_end_date ".
  71. " FROM $tbl_session ".
  72. " ORDER BY name";
  73. $result = Database::query($sql);
  74. $sessions = Database::store_result($result);
  75. $nbr_results = count($sessions);
  76. foreach ($sessions as $row) {
  77. echo '<option value="'.$row['id'].'">'.api_htmlentities($row['name']).' ('.$row['access_start_date'].' - '.$row['access_end_date'].')</option>';
  78. }
  79. echo '</select>';
  80. echo '<input type="submit" value="'.get_lang('Submit').'">';
  81. echo '</form>';
  82. echo '</div>';
  83. }
  84. // form4 annee != 0; composante != 0 etape != 0
  85. //elseif ($annee <> "" && $composante <> "" && $etape <> "" && $listeok != 'yes') {
  86. elseif (!empty($annee) && !empty($id_session) && empty($_POST['confirmed']))
  87. {
  88. Display::display_header($tool_name);
  89. echo '<div style="align: center;">';
  90. echo '<br />';
  91. echo '<br />';
  92. echo '<h3>'.Display::return_icon('group.gif', get_lang('SelectStudents')).' '.get_lang('SelectStudents').'</h3>';
  93. //echo "Connection ...";
  94. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  95. ldap_set_version($ds);
  96. if ($ds) {
  97. $r = false;
  98. $res = ldap_handle_bind($ds, $r);
  99. //$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))");
  100. //echo "(ou=*$annee,ou=$composante)";
  101. $sr = @ ldap_search($ds, $ldap_basedn, "(ou=*$annee)");
  102. $info = ldap_get_entries($ds, $sr);
  103. for ($key = 0; $key < $info["count"]; $key++) {
  104. $nom_form[] = $info[$key]["sn"][0];
  105. $prenom_form[] = $info[$key]["givenname"][0];
  106. $email_form[] = $info[$key]["mail"][0];
  107. // Get uid from dn
  108. //$dn_array=ldap_explode_dn($info[$key]["dn"],1);
  109. //$username_form[] = $dn_array[0]; // uid is first key
  110. $username_form[] = $info[$key]['uid'][0];
  111. $outab[] = $info[$key]["eduPersonPrimaryAffiliation"][0]; // Ici "student"
  112. //$val = ldap_get_values_len($ds, $entry, "userPassword");
  113. //$password_form[] = $val[0];
  114. $password_form[] = $info[$key]['userPassword'][0];
  115. }
  116. ldap_unbind($ds);
  117. /*-----------------------------------------------*/
  118. asort($nom_form);
  119. reset($nom_form);
  120. $statut = 5;
  121. include ('ldap_form_add_users_group.php');
  122. } else {
  123. echo '<h4>'.get_lang('UnableToConnectTo').' '.$host.'</h4>';
  124. }
  125. echo '<br /><br />';
  126. echo '<a href="ldap_import_students.php?annee=">'.get_lang('BackToNewSearch').'</a>';
  127. echo '<br /><br />';
  128. echo '</div>';
  129. }
  130. elseif (!empty($annee) && !empty($id_session) && ($_POST['confirmed'] == 'yes'))
  131. {
  132. $id = $_POST['username_form'];
  133. $UserList = array();
  134. $userid_match_login = array();
  135. foreach ($id as $form_index=>$user_id)
  136. {
  137. if (is_array($_POST['checkboxes']) && in_array($form_index, array_values($_POST['checkboxes'])))
  138. {
  139. $tmp = ldap_add_user($user_id);
  140. $UserList[] = $tmp;
  141. $userid_match_login[$tmp] = $user_id;
  142. }
  143. }
  144. if (!empty($_POST['id_session'])) {
  145. $num = 0;
  146. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  147. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  148. foreach ($UserList as $user_id) {
  149. $res_user = Database::insert(
  150. $tbl_session_user,
  151. [
  152. 'session_id' => intval($id_session),
  153. 'user_id' => intval($user_id),
  154. 'registered_at' => api_get_utc_datetime()
  155. ]
  156. );
  157. if ($res_user !== false) {
  158. $num++;
  159. }
  160. }
  161. if ($num > 0) {
  162. $sql = 'UPDATE '.$tbl_session.' SET nbr_users = (nbr_users + '.$num.') WHERE id = '.intval($id_session);
  163. $res = Database::query($sql);
  164. }
  165. header('Location: resume_session.php?id_session='.Security::remove_XSS($_POST['id_session']));
  166. }
  167. /*
  168. else
  169. {
  170. Display :: display_header($tool_name);
  171. if(count($userid_match_login)>0)
  172. {
  173. $message=get_lang('LDAPUsersAddedOrUpdated').':<br />';
  174. foreach($userid_match_login as $user_id => $login)
  175. {
  176. $message .= '- '.$login.'<br />';
  177. }
  178. }
  179. else
  180. {
  181. $message=get_lang('NoUserAdded');
  182. }
  183. Display::addFlash(Display::return_message($message, 'normal', false));
  184. }
  185. */
  186. else
  187. {
  188. Display::display_header($tool_name);
  189. $message = get_lang('NoUserAdded');
  190. Display::addFlash(Display::return_message($message, 'normal', false));
  191. }
  192. echo '<br /><br />';
  193. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  194. echo '<br /><br />';
  195. }
  196. Display::display_footer();
  197. ?>