add_sessions_to_usergroup.php 12 KB

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