add_sessions_to_usergroup.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 __DIR__.'/../inc/global.inc.php';
  10. $xajax = new xajax();
  11. //$xajax->debugOn();
  12. $xajax->registerFunction('search_usergroup_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. $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 == 'searchbox') {
  104. $session_list = SessionManager::get_sessions_list(
  105. array('s.name' => array('operator' => 'LIKE', 'value' => "%$needle%"))
  106. );
  107. } elseif ($type != 'single') {
  108. $session_list = SessionManager::get_sessions_list(
  109. array('s.name' => array('operator' => 'LIKE', 'value' => "$needle%"))
  110. );
  111. }
  112. if ($type != 'single') {
  113. $return .= '<select id="elements_not_in" name="elements_not_in_name[]" multiple="multiple" size="15" style="width:360px;">';
  114. foreach ($session_list as $row) {
  115. if (!in_array($row['id'], array_keys($elements_in))) {
  116. $return .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
  117. }
  118. }
  119. $return .= '</select>';
  120. $xajax_response->addAssign(
  121. 'ajax_list_multiple',
  122. 'innerHTML',
  123. api_utf8_encode($return)
  124. );
  125. }
  126. }
  127. return $xajax_response;
  128. }
  129. $xajax->processRequests();
  130. Display::display_header($tool_name);
  131. $add = (empty($_GET['add']) ? '' : Security::remove_XSS($_GET['add']));
  132. if ($add_type == 'multiple') {
  133. $link_add_type_unique = '<a href="'.api_get_self().'?add='.$add.'&add_type=unique">'.
  134. Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  135. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  136. } else {
  137. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  138. $link_add_type_multiple = '<a href="'.api_get_self().'?add='.$add.'&add_type=multiple">'.
  139. Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  140. }
  141. echo '<div class="actions">';
  142. echo '<a href="usergroups.php">'.Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  143. echo '<a href="javascript://" class="advanced_parameters" style="margin-top: 8px" onclick="display_advanced_search();"><span id="img_plus_and_minus">&nbsp;'.
  144. Display::return_icon('div_show.gif', get_lang('Show'), array('style'=>'vertical-align:middle')).' '.get_lang('AdvancedSearch').'</span></a>';
  145. echo '</div>';
  146. echo '<div id="advancedSearch" style="display: none">'.get_lang('SearchSessions'); ?> :
  147. <input name="SearchSession" onchange = "xajax_search_usergroup_sessions(this.value,'searchbox')" onkeyup="this.onchange()">
  148. </div>
  149. <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();"'; }?>>
  150. <?php
  151. echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
  152. echo Display::input('hidden', 'id', $id);
  153. echo Display::input('hidden', 'form_sent', '1');
  154. echo Display::input('hidden', 'add_type', null);
  155. if (!empty($errorMsg)) {
  156. echo Display::return_message($errorMsg, 'normal'); //main API
  157. }
  158. ?>
  159. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  160. <tr>
  161. <td align="center"><b><?php echo get_lang('SessionsInPlatform') ?> :</b>
  162. </td>
  163. <td></td>
  164. <td align="center"><b><?php echo get_lang('SessionsInGroup') ?> :</b></td>
  165. </tr>
  166. <?php if ($add_type == 'multiple') { ?>
  167. <tr>
  168. <td align="center">
  169. <?php echo get_lang('FirstLetterSessions'); ?> :
  170. <select name="firstLetterUser" onchange = "xajax_search_usergroup_sessions(this.value,'multiple')" >
  171. <option value = "%">--</option>
  172. <?php
  173. echo Display :: get_alphabet_options();
  174. ?>
  175. </select>
  176. <?php echo '<br />'; ?>
  177. </td>
  178. <td align="center">&nbsp;</td>
  179. </tr>
  180. <?php } ?>
  181. <tr>
  182. <td align="center">
  183. <div id="content_source">
  184. <?php
  185. if (!($add_type == 'multiple')) {
  186. ?>
  187. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
  188. <div id="ajax_list_users_single"></div>
  189. <?php
  190. } else {
  191. ?>
  192. <div id="ajax_list_multiple">
  193. <?php echo Display::select('elements_not_in_name', $elements_not_in, '', array('style'=>'width:360px', 'multiple'=>'multiple', 'id'=>'elements_not_in', 'size'=>'15px'), false); ?>
  194. </div>
  195. <?php
  196. }
  197. ?>
  198. </div>
  199. </td>
  200. <td width="10%" valign="middle" align="center">
  201. <?php
  202. if ($ajax_search) {
  203. ?>
  204. <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('elements_in'))" >
  205. <em class="fa fa-arrow-left"></em>
  206. </button>
  207. <?php
  208. } else {
  209. ?>
  210. <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'))">
  211. <em class="fa fa-arrow-right"></em>
  212. </button>
  213. <br /><br />
  214. <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'))">
  215. <em class="fa fa-arrow-left"></em>
  216. </button>
  217. <?php
  218. }
  219. ?>
  220. <br /><br /><br /><br /><br /><br />
  221. </td>
  222. <td align="center">
  223. <?php
  224. echo Display::select('elements_in_name[]', $elements_in, '', array('style'=>'width:360px', 'multiple'=>'multiple', 'id'=>'elements_in', 'size'=>'15px'), false);
  225. unset($sessionUsersList);
  226. ?>
  227. </td>
  228. </tr>
  229. <tr>
  230. <td colspan="3" align="center">
  231. <br />
  232. <?php
  233. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('SubscribeClassToSessions').'</button>';
  234. ?>
  235. </td>
  236. </tr>
  237. </table>
  238. </form>
  239. <script>
  240. function moveItem(origin , destination) {
  241. for(var i = 0 ; i<origin.options.length ; i++) {
  242. if(origin.options[i].selected) {
  243. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  244. origin.options[i]=null;
  245. i = i-1;
  246. }
  247. }
  248. destination.selectedIndex = -1;
  249. sortOptions(destination.options);
  250. }
  251. function sortOptions(options) {
  252. newOptions = new Array();
  253. for (i = 0 ; i<options.length ; i++)
  254. newOptions[i] = options[i];
  255. newOptions = newOptions.sort(mysort);
  256. options.length = 0;
  257. for(i = 0 ; i < newOptions.length ; i++)
  258. options[i] = newOptions[i];
  259. }
  260. function mysort(a, b){
  261. if(a.text.toLowerCase() > b.text.toLowerCase()){
  262. return 1;
  263. }
  264. if(a.text.toLowerCase() < b.text.toLowerCase()){
  265. return -1;
  266. }
  267. return 0;
  268. }
  269. function valide(){
  270. var options = document.getElementById('elements_in').options;
  271. for (i = 0 ; i<options.length ; i++)
  272. options[i].selected = true;
  273. document.forms.formulaire.submit();
  274. }
  275. function loadUsersInSelect(select) {
  276. var xhr_object = null;
  277. if(window.XMLHttpRequest) // Firefox
  278. xhr_object = new XMLHttpRequest();
  279. else if(window.ActiveXObject) // Internet Explorer
  280. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  281. else // XMLHttpRequest non supporté par le navigateur
  282. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  283. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  284. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  285. nosessionUsers = makepost(document.getElementById('elements_not_in'));
  286. sessionUsers = makepost(document.getElementById('elements_in'));
  287. nosessionClasses = makepost(document.getElementById('origin_classes'));
  288. sessionClasses = makepost(document.getElementById('destination_classes'));
  289. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  290. xhr_object.onreadystatechange = function() {
  291. if(xhr_object.readyState == 4) {
  292. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  293. //alert(xhr_object.responseText);
  294. }
  295. }
  296. }
  297. function makepost(select){
  298. var options = select.options;
  299. var ret = "";
  300. for (i = 0 ; i<options.length ; i++)
  301. ret = ret + options[i].value +'::'+options[i].text+";;";
  302. return ret;
  303. }
  304. </script>
  305. <?php
  306. Display::display_footer();