add_sessions_to_promotion.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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->registerFunction('search_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[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  18. $interbreadcrumb[] = ['url' => 'career_dashboard.php', 'name' => get_lang('Careers and promotions')];
  19. // Setting the name of the tool
  20. $tool_name = get_lang('Subscribe sessions to promotions');
  21. $add_type = 'multiple';
  22. if (isset($_REQUEST['add_type']) && $_REQUEST['add_type'] != '') {
  23. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  24. }
  25. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  26. $htmlHeadXtra[] = '<script>
  27. function add_user_to_session (code, content) {
  28. document.getElementById("user_to_add").value = "";
  29. document.getElementById("ajax_list_users_single").innerHTML = "";
  30. destination = document.getElementById("session_in_promotion");
  31. for (i=0;i<destination.length;i++) {
  32. if(destination.options[i].text == content) {
  33. return false;
  34. }
  35. }
  36. destination.options[destination.length] = new Option(content,code);
  37. destination.selectedIndex = -1;
  38. sortOptions(destination.options);
  39. }
  40. function remove_item(origin) {
  41. for(var i = 0 ; i<origin.options.length ; i++) {
  42. if(origin.options[i].selected) {
  43. origin.options[i]=null;
  44. i = i-1;
  45. }
  46. }
  47. }
  48. function validate_filter() {
  49. document.formulaire.add_type.value = \''.$add_type.'\';
  50. document.formulaire.form_sent.value=0;
  51. document.formulaire.submit();
  52. }
  53. </script>';
  54. $form_sent = 0;
  55. $errorMsg = '';
  56. $users = $sessions = [];
  57. $promotion = new Promotion();
  58. $id = intval($_GET['id']);
  59. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  60. $form_sent = $_POST['form_sent'];
  61. $session_in_promotion_posted = $_POST['session_in_promotion_name'];
  62. if (!is_array($session_in_promotion_posted)) {
  63. $session_in_promotion_posted = [$session_in_promotion_posted];
  64. }
  65. if ($form_sent == 1) {
  66. // Added a parameter to send emails when registering a user
  67. SessionManager::subscribe_sessions_to_promotion($id, $session_in_promotion_posted);
  68. header('Location: promotions.php');
  69. exit;
  70. }
  71. }
  72. $promotion_data = $promotion->get($id);
  73. $session_list = SessionManager::get_sessions_list([], ['name']);
  74. $session_not_in_promotion = $session_in_promotion = [];
  75. if (!empty($session_list)) {
  76. foreach ($session_list as $session) {
  77. $promotion_id = $session['promotion_id'];
  78. if (isset($promotion_id) && !empty($promotion_id)) {
  79. if ($promotion_id == $id) {
  80. $session_in_promotion[$session['id']] = $session['name'];
  81. } else {
  82. $session_not_in_promotion[$session['id']] = $session['name'];
  83. }
  84. } else {
  85. $session_not_in_promotion[$session['id']] = $session['name'];
  86. }
  87. }
  88. }
  89. $ajax_search = $add_type == 'unique' ? true : false;
  90. // Checking for extra field with filter on
  91. $xajax->processRequests();
  92. Display::display_header($tool_name);
  93. if ($add_type == 'multiple') {
  94. $link_add_type_unique = '<a href="'.api_get_self().'?id='.$id.'&add_type=unique">'.Display::return_icon('single.gif').get_lang('Single registration').'</a>';
  95. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('Multiple registration');
  96. } else {
  97. $link_add_type_unique = Display::return_icon('single.gif').get_lang('Single registration');
  98. $link_add_type_multiple = '<a href="'.api_get_self().'?id='.$id.'&add_type=multiple">'.Display::return_icon('multiple.gif').get_lang('Multiple registration').'</a>';
  99. }
  100. echo '<div class="actions">';
  101. echo '<a href="promotions.php">'.Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  102. echo '</div>';
  103. ?>
  104. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if (!empty($_GET['add'])) {
  105. echo '&add=true';
  106. } ?>" style="margin:0px;" <?php if ($ajax_search) {
  107. echo ' onsubmit="valide();"';
  108. }?>>
  109. <?php echo '<legend>'.$tool_name.' '.$promotion_data['name'].'</legend>';
  110. if ($add_type == 'multiple') {
  111. $extraField = new \ExtraField('session');
  112. $extra_field_list = $extraField->get_all_extra_field_by_type(ExtraField::FIELD_TYPE_SELECT);
  113. $new_field_list = [];
  114. if (is_array($extra_field_list) && (count($extra_field_list) > 0)) {
  115. echo '<h3>'.get_lang('Filter sessions').'</h3>';
  116. foreach ($extra_field_list as $new_field) {
  117. echo $new_field['name'];
  118. $varname = 'field_'.$new_field['variable'];
  119. echo '&nbsp;<select name="'.$varname.'">';
  120. echo '<option value="0">--'.get_lang('Select').'--</option>';
  121. foreach ($new_field['data'] as $option) {
  122. $checked = '';
  123. if (isset($_POST[$varname])) {
  124. if ($_POST[$varname] == $option[1]) {
  125. $checked = 'selected="true"';
  126. }
  127. }
  128. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  129. }
  130. echo '</select>';
  131. echo '&nbsp;&nbsp;';
  132. }
  133. echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
  134. echo '<br /><br />';
  135. }
  136. }
  137. echo Display::input('hidden', 'id', $id);
  138. echo Display::input('hidden', 'form_sent', '1');
  139. echo Display::input('hidden', 'add_type', null);
  140. if (!empty($errorMsg)) {
  141. echo Display::return_message($errorMsg, 'normal'); //main API
  142. }
  143. ?>
  144. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  145. <tr>
  146. <td align="center"><b><?php echo get_lang('Sessions not subscribed'); ?> :</b>
  147. </td>
  148. <td></td>
  149. <td align="center"><b><?php echo get_lang('Sessions in this promotion'); ?> :</b></td>
  150. </tr>
  151. <?php if ($add_type == 'multiple') {
  152. ?>
  153. <tr>
  154. <td align="center">
  155. <?php echo get_lang('First letter of session name'); ?> :
  156. <select name="firstLetterUser" onchange = "xajax_search_sessions(this.value,'multiple')" >
  157. <option value = "%">--</option>
  158. <?php
  159. echo Display :: get_alphabet_options(); ?>
  160. </select>
  161. </td>
  162. <td align="center">&nbsp;</td>
  163. </tr>
  164. <?php
  165. } ?>
  166. <tr>
  167. <td align="center">
  168. <div id="content_source">
  169. <?php
  170. if (!($add_type == 'multiple')) {
  171. ?>
  172. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
  173. <div id="ajax_list_users_single"></div>
  174. <?php
  175. } else {
  176. ?>
  177. <div id="ajax_list_multiple">
  178. <?php echo Display::select('session_not_in_promotion_name', $session_not_in_promotion, '', ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'session_not_in_promotion', 'size' => '15px'], false); ?>
  179. </div>
  180. <?php
  181. }
  182. ?>
  183. </div>
  184. </td>
  185. <td width="10%" valign="middle" align="center">
  186. <?php
  187. if ($ajax_search) {
  188. ?>
  189. <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('session_in_promotion'))" >
  190. <em class="fa fa-arrow-left"></em>
  191. </button>
  192. <?php
  193. } else {
  194. ?>
  195. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('session_not_in_promotion'), document.getElementById('session_in_promotion'))" onclick="moveItem(document.getElementById('session_not_in_promotion'), document.getElementById('session_in_promotion'))">
  196. <em class="fa fa-arrow-right"></em>
  197. </button>
  198. <br /><br />
  199. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('session_in_promotion'), document.getElementById('session_not_in_promotion'))" onclick="moveItem(document.getElementById('session_in_promotion'), document.getElementById('session_not_in_promotion'))">
  200. <em class="fa fa-arrow-left"></em>
  201. </button>
  202. <?php
  203. }
  204. ?>
  205. <br /><br /><br /><br /><br /><br />
  206. </td>
  207. <td align="center">
  208. <?php
  209. echo Display::select(
  210. 'session_in_promotion_name[]',
  211. $session_in_promotion,
  212. '',
  213. ['style' => 'width:360px', 'multiple' => 'multiple', 'id' => 'session_in_promotion', 'size' => '15px'],
  214. false
  215. );
  216. unset($sessionUsersList);
  217. ?>
  218. </td>
  219. </tr>
  220. <tr>
  221. <td colspan="3" align="center">
  222. <br />
  223. <?php
  224. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('Subscribe sessions to promotion').'</button>';
  225. ?>
  226. </td>
  227. </tr>
  228. </table>
  229. </form>
  230. <script>
  231. function moveItem(origin , destination) {
  232. for(var i = 0 ; i<origin.options.length ; i++) {
  233. if(origin.options[i].selected) {
  234. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  235. origin.options[i]=null;
  236. i = i-1;
  237. }
  238. }
  239. destination.selectedIndex = -1;
  240. sortOptions(destination.options);
  241. }
  242. function sortOptions(options) {
  243. newOptions = new Array();
  244. for (i = 0 ; i<options.length ; i++)
  245. newOptions[i] = options[i];
  246. newOptions = newOptions.sort(mysort);
  247. options.length = 0;
  248. for(i = 0 ; i < newOptions.length ; i++)
  249. options[i] = newOptions[i];
  250. }
  251. function mysort(a, b){
  252. if (a.text.toLowerCase() > b.text.toLowerCase()){
  253. return 1;
  254. }
  255. if (a.text.toLowerCase() < b.text.toLowerCase()){
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. function valide(){
  261. var options = document.getElementById('session_in_promotion').options;
  262. for (i = 0 ; i<options.length ; i++)
  263. options[i].selected = true;
  264. document.forms.formulaire.submit();
  265. }
  266. function loadUsersInSelect(select) {
  267. var xhr_object = null;
  268. if(window.XMLHttpRequest) // Firefox
  269. xhr_object = new XMLHttpRequest();
  270. else if(window.ActiveXObject) // Internet Explorer
  271. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  272. else // XMLHttpRequest non supporté par le navigateur
  273. alert("Your browser does not support XMLHTTPRequest...");
  274. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  275. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  276. nosessionUsers = makepost(document.getElementById('session_not_in_promotion'));
  277. sessionUsers = makepost(document.getElementById('session_in_promotion'));
  278. nosessionClasses = makepost(document.getElementById('origin_classes'));
  279. sessionClasses = makepost(document.getElementById('destination_classes'));
  280. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  281. xhr_object.onreadystatechange = function() {
  282. if(xhr_object.readyState == 4) {
  283. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  284. //alert(xhr_object.responseText);
  285. }
  286. }
  287. }
  288. function makepost(select) {
  289. var options = select.options;
  290. var ret = "";
  291. for (i = 0 ; i<options.length ; i++)
  292. ret = ret + options[i].value +'::'+options[i].text+";;";
  293. return ret;
  294. }
  295. </script>
  296. <?php
  297. Display::display_footer();
  298. function search_sessions($needle, $type)
  299. {
  300. global $session_in_promotion;
  301. $xajax_response = new xajaxResponse();
  302. $return = '';
  303. if (!empty($needle) && !empty($type)) {
  304. $session_list = SessionManager::get_sessions_list(
  305. ['s.name' => ['operator' => 'LIKE', 'value' => "$needle%"]]
  306. );
  307. $return .= '<select id="session_not_in_promotion" name="session_not_in_promotion_name[]" multiple="multiple" size="15" style="width:360px;">';
  308. foreach ($session_list as $row) {
  309. if (!in_array($row['id'], array_keys($session_in_promotion))) {
  310. $return .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
  311. }
  312. }
  313. $return .= '</select>';
  314. $xajax_response->addAssign('ajax_list_multiple', 'innerHTML', api_utf8_encode($return));
  315. }
  316. return $xajax_response;
  317. }