ldap_import_students_to_session.php 7.6 KB

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