add_sessions_to_usergroup.php 12 KB

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