add_sessions_to_promotion.php 12 KB

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