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