add_sessions_to_promotion.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  18. $interbreadcrumb[] = array('url' => 'career_dashboard.php', 'name' => get_lang('CareersAndPromotions'));
  19. // Setting the name of the tool
  20. $tool_name = get_lang('SubscribeSessionsToPromotions');
  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 = array();
  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 = array($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(array(), array('name'));
  74. $session_not_in_promotion = $session_in_promotion = array();
  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('SessionAddTypeUnique').'</a>';
  95. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  96. } else {
  97. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  98. $link_add_type_multiple = '<a href="'.api_get_self().'?id='.$id.'&add_type=multiple">'.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</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'])) echo '&add=true'; ?>" style="margin:0px;" <?php if ($ajax_search) {echo ' onsubmit="valide();"'; }?>>
  105. <?php echo '<legend>'.$tool_name.' '.$promotion_data['name'].'</legend>';
  106. if ($add_type == 'multiple') {
  107. $extraField = new \ExtraField('session');
  108. $extra_field_list = $extraField->get_all_extra_field_by_type(ExtraField::FIELD_TYPE_SELECT);
  109. $new_field_list = array();
  110. if (is_array($extra_field_list) && (count($extra_field_list) > 0)) {
  111. echo '<h3>'.get_lang('FilterSessions').'</h3>';
  112. foreach ($extra_field_list as $new_field) {
  113. echo $new_field['name'];
  114. $varname = 'field_'.$new_field['variable'];
  115. echo '&nbsp;<select name="'.$varname.'">';
  116. echo '<option value="0">--'.get_lang('Select').'--</option>';
  117. foreach ($new_field['data'] as $option) {
  118. $checked = '';
  119. if (isset($_POST[$varname])) {
  120. if ($_POST[$varname] == $option[1]) {
  121. $checked = 'selected="true"';
  122. }
  123. }
  124. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  125. }
  126. echo '</select>';
  127. echo '&nbsp;&nbsp;';
  128. }
  129. echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
  130. echo '<br /><br />';
  131. }
  132. }
  133. echo Display::input('hidden', 'id', $id);
  134. echo Display::input('hidden', 'form_sent', '1');
  135. echo Display::input('hidden', 'add_type', null);
  136. if (!empty($errorMsg)) {
  137. Display::addFlash(Display::return_message($errorMsg, 'normal')); //main API
  138. }
  139. ?>
  140. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  141. <tr>
  142. <td align="center"><b><?php echo get_lang('SessionsInPlatform') ?> :</b>
  143. </td>
  144. <td></td>
  145. <td align="center"><b><?php echo get_lang('SessionsInPromotion') ?> :</b></td>
  146. </tr>
  147. <?php if ($add_type == 'multiple') { ?>
  148. <tr>
  149. <td align="center">
  150. <?php echo get_lang('FirstLetterSessions'); ?> :
  151. <select name="firstLetterUser" onchange = "xajax_search_sessions(this.value,'multiple')" >
  152. <option value = "%">--</option>
  153. <?php
  154. echo Display :: get_alphabet_options();
  155. ?>
  156. </select>
  157. </td>
  158. <td align="center">&nbsp;</td>
  159. </tr>
  160. <?php } ?>
  161. <tr>
  162. <td align="center">
  163. <div id="content_source">
  164. <?php
  165. if (!($add_type == 'multiple')) {
  166. ?>
  167. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" />
  168. <div id="ajax_list_users_single"></div>
  169. <?php
  170. } else {
  171. ?>
  172. <div id="ajax_list_multiple">
  173. <?php echo Display::select('session_not_in_promotion_name', $session_not_in_promotion, '', array('style'=>'width:360px', 'multiple'=>'multiple', 'id'=>'session_not_in_promotion', 'size'=>'15px'), false); ?>
  174. </div>
  175. <?php
  176. }
  177. ?>
  178. </div>
  179. </td>
  180. <td width="10%" valign="middle" align="center">
  181. <?php
  182. if ($ajax_search) {
  183. ?>
  184. <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('session_in_promotion'))" >
  185. <em class="fa fa-arrow-left"></em>
  186. </button>
  187. <?php
  188. } else {
  189. ?>
  190. <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'))">
  191. <em class="fa fa-arrow-right"></em>
  192. </button>
  193. <br /><br />
  194. <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'))">
  195. <em class="fa fa-arrow-left"></em>
  196. </button>
  197. <?php
  198. }
  199. ?>
  200. <br /><br /><br /><br /><br /><br />
  201. </td>
  202. <td align="center">
  203. <?php
  204. echo Display::select(
  205. 'session_in_promotion_name[]',
  206. $session_in_promotion,
  207. '',
  208. array('style'=>'width:360px', 'multiple'=>'multiple', 'id'=>'session_in_promotion', 'size'=>'15px'),
  209. false
  210. );
  211. unset($sessionUsersList);
  212. ?>
  213. </td>
  214. </tr>
  215. <tr>
  216. <td colspan="3" align="center">
  217. <br />
  218. <?php
  219. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('SubscribeSessionsToPromotion').'</button>';
  220. ?>
  221. </td>
  222. </tr>
  223. </table>
  224. </form>
  225. <script>
  226. function moveItem(origin , destination) {
  227. for(var i = 0 ; i<origin.options.length ; i++) {
  228. if(origin.options[i].selected) {
  229. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  230. origin.options[i]=null;
  231. i = i-1;
  232. }
  233. }
  234. destination.selectedIndex = -1;
  235. sortOptions(destination.options);
  236. }
  237. function sortOptions(options) {
  238. newOptions = new Array();
  239. for (i = 0 ; i<options.length ; i++)
  240. newOptions[i] = options[i];
  241. newOptions = newOptions.sort(mysort);
  242. options.length = 0;
  243. for(i = 0 ; i < newOptions.length ; i++)
  244. options[i] = newOptions[i];
  245. }
  246. function mysort(a, b){
  247. if (a.text.toLowerCase() > b.text.toLowerCase()){
  248. return 1;
  249. }
  250. if (a.text.toLowerCase() < b.text.toLowerCase()){
  251. return -1;
  252. }
  253. return 0;
  254. }
  255. function valide(){
  256. var options = document.getElementById('session_in_promotion').options;
  257. for (i = 0 ; i<options.length ; i++)
  258. options[i].selected = true;
  259. document.forms.formulaire.submit();
  260. }
  261. function loadUsersInSelect(select) {
  262. var xhr_object = null;
  263. if(window.XMLHttpRequest) // Firefox
  264. xhr_object = new XMLHttpRequest();
  265. else if(window.ActiveXObject) // Internet Explorer
  266. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  267. else // XMLHttpRequest non supporté par le navigateur
  268. alert("Your browser does not support XMLHTTPRequest...");
  269. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  270. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  271. nosessionUsers = makepost(document.getElementById('session_not_in_promotion'));
  272. sessionUsers = makepost(document.getElementById('session_in_promotion'));
  273. nosessionClasses = makepost(document.getElementById('origin_classes'));
  274. sessionClasses = makepost(document.getElementById('destination_classes'));
  275. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  276. xhr_object.onreadystatechange = function() {
  277. if(xhr_object.readyState == 4) {
  278. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  279. //alert(xhr_object.responseText);
  280. }
  281. }
  282. }
  283. function makepost(select) {
  284. var options = select.options;
  285. var ret = "";
  286. for (i = 0 ; i<options.length ; i++)
  287. ret = ret + options[i].value +'::'+options[i].text+";;";
  288. return ret;
  289. }
  290. </script>
  291. <?php
  292. Display::display_footer();
  293. function search_sessions($needle, $type)
  294. {
  295. global $session_in_promotion;
  296. $xajax_response = new xajaxResponse();
  297. $return = '';
  298. if (!empty($needle) && !empty($type)) {
  299. $session_list = SessionManager::get_sessions_list(
  300. array('s.name' => array('operator' => 'LIKE', 'value' => "$needle%"))
  301. );
  302. $return .= '<select id="session_not_in_promotion" name="session_not_in_promotion_name[]" multiple="multiple" size="15" style="width:360px;">';
  303. foreach ($session_list as $row) {
  304. if (!in_array($row['id'], array_keys($session_in_promotion))) {
  305. $return .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
  306. }
  307. }
  308. $return .= '</select>';
  309. $xajax_response -> addAssign('ajax_list_multiple', 'innerHTML', api_utf8_encode($return));
  310. }
  311. return $xajax_response;
  312. }