add_sessions_to_promotion.php 12 KB

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