add_sessions_to_usergroup.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // name of the language file that needs to be included
  7. $language_file=array('admin','registration');
  8. // resetting the course id
  9. $cidReset=true;
  10. // including some necessary files
  11. require_once '../inc/global.inc.php';
  12. require_once '../inc/lib/xajax/xajax.inc.php';
  13. require_once api_get_path(LIBRARY_PATH).'usergroup.lib.php';
  14. $xajax = new xajax();
  15. //$xajax->debugOn();
  16. $xajax->registerFunction('search_sessions');
  17. // setting the section (for the tabs)
  18. $this_section = SECTION_PLATFORM_ADMIN;
  19. // Access restrictions
  20. api_protect_admin_script(true);
  21. // setting breadcrumbs
  22. $interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin'));
  23. $interbreadcrumb[]=array('url' => 'usergroups.php','name' => get_lang('Classes'));
  24. // Database Table Definitions
  25. // setting the name of the tool
  26. $tool_name=get_lang('SubscribeClassToSessions');
  27. $add_type = 'multiple';
  28. if(isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){
  29. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  30. }
  31. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  32. $htmlHeadXtra[] = '<script>
  33. function add_user_to_session (code, content) {
  34. document.getElementById("user_to_add").value = "";
  35. document.getElementById("ajax_list_users_single").innerHTML = "";
  36. destination = document.getElementById("elements_in");
  37. for (i=0;i<destination.length;i++) {
  38. if(destination.options[i].text == content) {
  39. return false;
  40. }
  41. }
  42. destination.options[destination.length] = new Option(content,code);
  43. destination.selectedIndex = -1;
  44. sortOptions(destination.options);
  45. }
  46. function remove_item(origin) {
  47. for(var i = 0 ; i<origin.options.length ; i++) {
  48. if(origin.options[i].selected) {
  49. origin.options[i]=null;
  50. i = i-1;
  51. }
  52. }
  53. }
  54. function display_advanced_search () {
  55. if ($("#advancedSearch").css("display") == "none") {
  56. $("#advancedSearch").css("display","block");
  57. $("#img_plus_and_minus").html(\'&nbsp;'.Display::return_icon('div_hide.gif',get_lang('Hide'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedSearch').'\');
  58. } else {
  59. $("#advancedSearch").css("display","none");
  60. $("#img_plus_and_minus").html(\'&nbsp;'.Display::return_icon('div_show.gif',get_lang('Show'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedSearch').'\');
  61. }
  62. }
  63. function validate_filter() {
  64. document.formulaire.add_type.value = \''.$add_type.'\';
  65. document.formulaire.form_sent.value=0;
  66. document.formulaire.submit();
  67. }
  68. </script>';
  69. $form_sent = 0;
  70. $errorMsg = '';
  71. $sessions=array();
  72. $usergroup = new UserGroup();
  73. $id = intval($_GET['id']);
  74. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  75. $form_sent = $_POST['form_sent'];
  76. $elements_posted = $_POST['elements_in_name'];
  77. if (!is_array($elements_posted)) {
  78. $elements_posted = array();
  79. }
  80. if ($form_sent == 1) {
  81. //added a parameter to send emails when registering a user
  82. $usergroup->subscribe_sessions_to_usergroup($id, $elements_posted);
  83. header('Location: usergroups.php');
  84. exit;
  85. }
  86. }
  87. $data = $usergroup->get($id);
  88. $session_list_in = $usergroup->get_sessions_by_usergroup($id);
  89. $session_list = SessionManager::get_sessions_list(array(), array('name'));
  90. //api_display_tool_title($tool_name.' ('.$session_info['name'].')');
  91. $elements_not_in = $elements_in= array();
  92. if (!empty($session_list)) {
  93. foreach($session_list as $session) {
  94. if (in_array($session['id'], $session_list_in)) {
  95. $elements_in[$session['id']] = $session['name'];
  96. } else {
  97. $elements_not_in[$session['id']] = $session['name'];
  98. }
  99. }
  100. }
  101. $ajax_search = $add_type == 'unique' ? true : false;
  102. //checking for extra field with filter on
  103. function search_sessions($needle,$type) {
  104. global $tbl_user,$elements_in;
  105. $xajax_response = new XajaxResponse();
  106. $return = '';
  107. if (!empty($needle) && !empty($type)) {
  108. // xajax send utf8 datas... datas in db can be non-utf8 datas
  109. $charset = api_get_system_encoding();
  110. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  111. if ($type == 'single') {
  112. // search users where username or firstname or lastname begins likes $needle
  113. /* $sql = 'SELECT user.user_id, username, lastname, firstname FROM '.$tbl_user.' user
  114. WHERE (username LIKE "'.$needle.'%"
  115. OR firstname LIKE "'.$needle.'%"
  116. OR lastname LIKE "'.$needle.'%") AND user.user_id<>"'.$user_anonymous.'" AND user.status<>'.DRH.''.
  117. $order_clause.
  118. ' LIMIT 11';*/
  119. } else if ($type == 'searchbox') {
  120. $session_list = SessionManager::get_sessions_list(
  121. array('s.name' => array('operator' => 'LIKE', 'value' => "%$needle%"))
  122. );
  123. } else {
  124. $session_list = SessionManager::get_sessions_list(
  125. array('s.name' => array('operator' => 'LIKE', 'value' => "$needle%"))
  126. );
  127. }
  128. $i=0;
  129. if ($type=='single') {
  130. /*
  131. while ($user = Database :: fetch_array($rs)) {
  132. $i++;
  133. if ($i<=10) {
  134. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  135. $return .= '<a href="javascript: void(0);" onclick="javascript: add_user_to_session(\''.$user['user_id'].'\',\''.$person_name.' ('.$user['username'].')'.'\')">'.$person_name.' ('.$user['username'].')</a><br />';
  136. } else {
  137. $return .= '...<br />';
  138. }
  139. }
  140. $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));*/
  141. } else {
  142. $return .= '<select id="elements_not_in" name="elements_not_in_name[]" multiple="multiple" size="15" style="width:360px;">';
  143. foreach ($session_list as $row ) {
  144. if (!in_array($row['id'], array_keys($elements_in))) {
  145. $return .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
  146. }
  147. }
  148. $return .= '</select>';
  149. $xajax_response -> addAssign('ajax_list_multiple','innerHTML',api_utf8_encode($return));
  150. }
  151. }
  152. return $xajax_response;
  153. }
  154. $xajax -> processRequests();
  155. Display::display_header($tool_name);
  156. if ($add_type == 'multiple') {
  157. $link_add_type_unique = '<a href="'.api_get_self().'?id_session='.$id_session.'&add='.Security::remove_XSS($_GET['add']).'&add_type=unique">'.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  158. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  159. } else {
  160. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  161. $link_add_type_multiple = '<a href="'.api_get_self().'?id_session='.$id_session.'&add='.Security::remove_XSS($_GET['add']).'&add_type=multiple">'.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  162. }
  163. echo '<div class="actions">';
  164. echo '<a href="usergroups.php">'.Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM).'</a>';
  165. echo '<a href="javascript://" class="advanced_parameters" style="margin-top: 8px" onclick="display_advanced_search();"><span id="img_plus_and_minus">&nbsp;'.Display::return_icon('div_show.gif',get_lang('Show'),array('style'=>'vertical-align:middle')).' '.get_lang('AdvancedSearch').'</span></a>';
  166. echo '</div>';
  167. echo '<div id="advancedSearch" style="display: none">'. get_lang('SearchSessions'); ?> :
  168. <input name="SearchSession" onchange = "xajax_search_sessions(this.value,'searchbox')" onkeyup="this.onchange()">
  169. </div>
  170. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if(!empty($_GET['add'])) echo '&add=true' ; ?>" style="margin:0px;" <?php if($ajax_search){echo ' onsubmit="valide();"';}?>>
  171. <?php
  172. echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
  173. if ($add_type=='multiple') {
  174. if (is_array($extra_field_list)) {
  175. if (is_array($new_field_list) && count($new_field_list)>0 ) {
  176. echo '<h3>'.get_lang('FilterUsers').'</h3>';
  177. foreach ($new_field_list as $new_field) {
  178. echo $new_field['name'];
  179. $varname = 'field_'.$new_field['variable'];
  180. echo '&nbsp;<select name="'.$varname.'">';
  181. echo '<option value="0">--'.get_lang('Select').'--</option>';
  182. foreach ($new_field['data'] as $option) {
  183. $checked='';
  184. if (isset($_POST[$varname])) {
  185. if ($_POST[$varname]==$option[1]) {
  186. $checked = 'selected="true"';
  187. }
  188. }
  189. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  190. }
  191. echo '</select>';
  192. echo '&nbsp;&nbsp;';
  193. }
  194. echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
  195. echo '<br /><br />';
  196. }
  197. }
  198. }
  199. echo Display::input('hidden','id',$id);
  200. echo Display::input('hidden','form_sent','1');
  201. echo Display::input('hidden','add_type',null);
  202. if(!empty($errorMsg)) {
  203. Display::display_normal_message($errorMsg); //main API
  204. }
  205. ?>
  206. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  207. <tr>
  208. <td align="center"><b><?php echo get_lang('SessionsInPlatform') ?> :</b>
  209. </td>
  210. <td></td>
  211. <td align="center"><b><?php echo get_lang('SessionsInGroup') ?> :</b></td>
  212. </tr>
  213. <?php if ($add_type=='multiple') { ?>
  214. <tr>
  215. <td align="center">
  216. <?php echo get_lang('FirstLetterSessions'); ?> :
  217. <select name="firstLetterUser" onchange = "xajax_search_sessions(this.value,'multiple')" >
  218. <option value = "%">--</option>
  219. <?php
  220. echo Display :: get_alphabet_options();
  221. ?>
  222. </select>
  223. <?php echo '<br />'; ?>
  224. </td>
  225. <td align="center">&nbsp;</td>
  226. </tr>
  227. <?php } ?>
  228. <tr>
  229. <td align="center">
  230. <div id="content_source">
  231. <?php
  232. if (!($add_type=='multiple')) {
  233. ?>
  234. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
  235. <div id="ajax_list_users_single"></div>
  236. <?php
  237. } else {
  238. ?>
  239. <div id="ajax_list_multiple">
  240. <?php echo Display::select('elements_not_in_name',$elements_not_in, '',array('style'=>'width:360px', 'multiple'=>'multiple','id'=>'elements_not_in','size'=>'15px'),false); ?>
  241. </div>
  242. <?php
  243. }
  244. ?>
  245. </div>
  246. </td>
  247. <td width="10%" valign="middle" align="center">
  248. <?php
  249. if ($ajax_search) {
  250. ?>
  251. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('elements_in'))" ></button>
  252. <?php
  253. } else {
  254. ?>
  255. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))"></button>
  256. <br /><br />
  257. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))"></button>
  258. <?php
  259. }
  260. ?>
  261. <br /><br /><br /><br /><br /><br />
  262. </td>
  263. <td align="center">
  264. <?php
  265. echo Display::select('elements_in_name[]', $elements_in, '', array('style'=>'width:360px', 'multiple'=>'multiple','id'=>'elements_in','size'=>'15px'),false );
  266. unset($sessionUsersList);
  267. ?>
  268. </td>
  269. </tr>
  270. <tr>
  271. <td colspan="3" align="center">
  272. <br />
  273. <?php
  274. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('SubscribeClassToSessions').'</button>';
  275. ?>
  276. </td>
  277. </tr>
  278. </table>
  279. </form>
  280. <script type="text/javascript">
  281. function moveItem(origin , destination) {
  282. for(var i = 0 ; i<origin.options.length ; i++) {
  283. if(origin.options[i].selected) {
  284. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  285. origin.options[i]=null;
  286. i = i-1;
  287. }
  288. }
  289. destination.selectedIndex = -1;
  290. sortOptions(destination.options);
  291. }
  292. function sortOptions(options) {
  293. newOptions = new Array();
  294. for (i = 0 ; i<options.length ; i++)
  295. newOptions[i] = options[i];
  296. newOptions = newOptions.sort(mysort);
  297. options.length = 0;
  298. for(i = 0 ; i < newOptions.length ; i++)
  299. options[i] = newOptions[i];
  300. }
  301. function mysort(a, b){
  302. if(a.text.toLowerCase() > b.text.toLowerCase()){
  303. return 1;
  304. }
  305. if(a.text.toLowerCase() < b.text.toLowerCase()){
  306. return -1;
  307. }
  308. return 0;
  309. }
  310. function valide(){
  311. var options = document.getElementById('elements_in').options;
  312. for (i = 0 ; i<options.length ; i++)
  313. options[i].selected = true;
  314. document.forms.formulaire.submit();
  315. }
  316. function loadUsersInSelect(select) {
  317. var xhr_object = null;
  318. if(window.XMLHttpRequest) // Firefox
  319. xhr_object = new XMLHttpRequest();
  320. else if(window.ActiveXObject) // Internet Explorer
  321. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  322. else // XMLHttpRequest non supporté par le navigateur
  323. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  324. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  325. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  326. nosessionUsers = makepost(document.getElementById('elements_not_in'));
  327. sessionUsers = makepost(document.getElementById('elements_in'));
  328. nosessionClasses = makepost(document.getElementById('origin_classes'));
  329. sessionClasses = makepost(document.getElementById('destination_classes'));
  330. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  331. xhr_object.onreadystatechange = function() {
  332. if(xhr_object.readyState == 4) {
  333. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  334. //alert(xhr_object.responseText);
  335. }
  336. }
  337. }
  338. function makepost(select){
  339. var options = select.options;
  340. var ret = "";
  341. for (i = 0 ; i<options.length ; i++)
  342. ret = ret + options[i].value +'::'+options[i].text+";;";
  343. return ret;
  344. }
  345. </script>
  346. <?php
  347. Display::display_footer();