add_sessions_to_usergroup.php 14 KB

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