ldap_import_students_to_session.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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('course.gif', get_lang('SelectSessionToImportUsersTo')).' '.get_lang('SelectSessionToImportUsersTo').'<br />';
  65. echo '<form method="post" action="'.api_get_self().'?annee='.Security::remove_XSS($annee).'"><br />';
  66. echo '<select name="id_session">';
  67. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  68. $sql = "SELECT id,name,nbr_courses,access_start_date,access_end_date " .
  69. " FROM $tbl_session ".
  70. " ORDER BY name";
  71. $result = Database::query($sql);
  72. $sessions=Database::store_result($result);
  73. $nbr_results=count($sessions);
  74. foreach($sessions as $row) {
  75. echo '<option value="'.$row['id'].'">'.api_htmlentities($row['name']).' ('.$row['access_start_date'].' - '.$row['access_end_date'].')</option>';
  76. }
  77. echo '</select>';
  78. echo '<input type="submit" value="'.get_lang('Submit').'">';
  79. echo '</form>';
  80. echo '</div>';
  81. }
  82. // form4 annee != 0; composante != 0 etape != 0
  83. //elseif ($annee <> "" && $composante <> "" && $etape <> "" && $listeok != 'yes') {
  84. elseif (!empty($annee) && !empty($id_session) && empty($_POST['confirmed']))
  85. {
  86. Display::display_header($tool_name);
  87. echo '<div style="align: center;">';
  88. echo '<br />';
  89. echo '<br />';
  90. echo '<h3>'.Display::return_icon('group.gif', get_lang('SelectStudents')).' '.get_lang('SelectStudents').'</h3>';
  91. //echo "Connection ...";
  92. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  93. ldap_set_version($ds);
  94. if ($ds) {
  95. $r = false;
  96. $res = ldap_handle_bind($ds, $r);
  97. //$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))");
  98. //echo "(ou=*$annee,ou=$composante)";
  99. $sr = @ ldap_search($ds, $ldap_basedn, "(ou=*$annee)");
  100. $info = ldap_get_entries($ds, $sr);
  101. for ($key = 0; $key < $info["count"]; $key ++) {
  102. $nom_form[] = $info[$key]["sn"][0];
  103. $prenom_form[] = $info[$key]["givenname"][0];
  104. $email_form[] = $info[$key]["mail"][0];
  105. // Get uid from dn
  106. //$dn_array=ldap_explode_dn($info[$key]["dn"],1);
  107. //$username_form[] = $dn_array[0]; // uid is first key
  108. $username_form[] = $info[$key]['uid'][0];
  109. $outab[] = $info[$key]["eduPersonPrimaryAffiliation"][0]; // Ici "student"
  110. //$val = ldap_get_values_len($ds, $entry, "userPassword");
  111. //$password_form[] = $val[0];
  112. $password_form[] = $info[$key]['userPassword'][0];
  113. }
  114. ldap_unbind($ds);
  115. /*-----------------------------------------------*/
  116. asort($nom_form);
  117. reset($nom_form);
  118. $statut=5;
  119. include ('ldap_form_add_users_group.php');
  120. } else {
  121. echo '<h4>'.get_lang('UnableToConnectTo').' '.$host.'</h4>';
  122. }
  123. echo '<br /><br />';
  124. echo '<a href="ldap_import_students.php?annee=">'.get_lang('BackToNewSearch').'</a>';
  125. echo '<br /><br />';
  126. echo '</div>';
  127. }
  128. elseif (!empty($annee) && !empty($id_session) && ($_POST['confirmed']=='yes'))
  129. {
  130. $id=$_POST['username_form'];
  131. $UserList=array();
  132. $userid_match_login = array();
  133. foreach ($id as $form_index=>$user_id)
  134. {
  135. if(is_array($_POST['checkboxes']) && in_array($form_index,array_values($_POST['checkboxes'])))
  136. {
  137. $tmp = ldap_add_user($user_id);
  138. $UserList[]= $tmp;
  139. $userid_match_login[$tmp] = $user_id;
  140. }
  141. }
  142. if (!empty($_POST['id_session'])) {
  143. $num = 0;
  144. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  145. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  146. foreach ($UserList as $user_id) {
  147. $res_user = Database::insert(
  148. $tbl_session_user,
  149. [
  150. 'session_id' => intval($id_session),
  151. 'user_id' => intval($user_id),
  152. 'registered_at' => api_get_utc_datetime()
  153. ]
  154. );
  155. if ($res_user !== false) {
  156. $num++;
  157. }
  158. }
  159. if($num>0) {
  160. $sql = 'UPDATE '.$tbl_session.' SET nbr_users = (nbr_users + '.$num.') WHERE id = '.intval($id_session);
  161. $res = Database::query($sql);
  162. }
  163. header('Location: resume_session.php?id_session='.Security::remove_XSS($_POST['id_session']));
  164. }
  165. /*
  166. else
  167. {
  168. Display :: display_header($tool_name);
  169. if(count($userid_match_login)>0)
  170. {
  171. $message=get_lang('LDAPUsersAddedOrUpdated').':<br />';
  172. foreach($userid_match_login as $user_id => $login)
  173. {
  174. $message .= '- '.$login.'<br />';
  175. }
  176. }
  177. else
  178. {
  179. $message=get_lang('NoUserAdded');
  180. }
  181. Display :: display_normal_message($message,false);
  182. }
  183. */
  184. else
  185. {
  186. Display::display_header($tool_name);
  187. $message=get_lang('NoUserAdded');
  188. Display :: display_normal_message($message,false);
  189. }
  190. echo '<br /><br />';
  191. echo '<a href="ldap_import_students.php?annee=&composante=&etape=">'.get_lang('BackToNewSearch').'</a>';
  192. echo '<br /><br />';
  193. }
  194. Display::display_footer();
  195. ?>