add_sessions_to_promotion.php 12 KB

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