add_sessions_to_usergroup.php 12 KB

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