event_type.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Definition of new system event types
  5. * @package chamilo.admin.events
  6. */
  7. /**
  8. * Init and access validation
  9. */
  10. $cidReset = true;
  11. //require_once '../inc/global.inc.php';
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. api_protect_admin_script();
  14. if (api_get_setting('mail.activate_email_template') != 'true') {
  15. api_not_allowed();
  16. }
  17. $action = isset($_POST['action']) ? $_POST['action'] : null;
  18. $eventName = isset($_POST['eventList']) ? $_POST['eventList'] : null;
  19. $eventUsers = isset($_POST['eventUsers']) ? $_POST['eventUsers'] : null;
  20. $eventMessage = isset($_POST['eventMessage']) ? $_POST['eventMessage'] : null;
  21. $eventSubject = isset($_POST['eventSubject']) ? $_POST['eventSubject'] : null;
  22. $eventMessageLanguage = isset($_POST['languages']) ? $_POST['languages'] : null;
  23. $activated = isset($_POST['activated']) ? $_POST['activated'] : 0;
  24. $event_name = isset($_REQUEST['event_type_name']) ? addslashes($_REQUEST['event_type_name']) : 0;
  25. if ($action == 'modEventType') {
  26. if ($eventUsers) {
  27. $users = explode(';', $eventUsers);
  28. } else {
  29. $users = array();
  30. }
  31. if (!empty($event_name)) {
  32. $eventName = $event_name;
  33. }
  34. Event::save_event_type_message(
  35. $eventName,
  36. $users,
  37. $eventMessage,
  38. $eventSubject,
  39. $eventMessageLanguage,
  40. $activated
  41. );
  42. header('location: event_controller.php');
  43. exit;
  44. }
  45. $ets = Event::get_all_event_types();
  46. $languages = api_get_languages();
  47. $ajaxPath = api_get_path(WEB_CODE_PATH) . 'inc/ajax/events.ajax.php';
  48. $action_array = array(
  49. array(
  50. 'url' => 'event_controller.php?action=listing',
  51. 'content' => Display::return_icon(
  52. 'view_text.png',
  53. get_lang('ListView'),
  54. array(),
  55. ICON_SIZE_MEDIUM
  56. ),
  57. )
  58. );
  59. $key_lang = get_lang('YouHaveSomeUnsavedChanges');
  60. $users = UserManager::get_user_list(array(), array('firstname'));
  61. $new_user_list = array();
  62. foreach ($users as $user) {
  63. if ($user['status'] == ANONYMOUS) {
  64. continue;
  65. }
  66. $new_user_list[] = $user;
  67. }
  68. /**
  69. * Header definition
  70. */
  71. $interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration'), 'name' => get_lang('PlatformAdmin'));
  72. $interbreadcrumb[] = array('url' => 'event_controller.php', 'name' => get_lang('Events'));
  73. $tool_name = get_lang('EventMessageManagement');
  74. Display::display_header($tool_name);
  75. echo Display::actions($action_array);
  76. /**
  77. * JavaScript code
  78. * @todo move into HTML header
  79. */
  80. ?>
  81. <script>
  82. var usersList = <?php echo json_encode($new_user_list) ?>;
  83. var eventTypes = <?php echo json_encode($ets) ?>;
  84. var eventsConfig = <?php echo json_encode($event_config) ?>;
  85. var currentLanguage = <?php echo json_encode(api_get_language_isocode()) ?>;
  86. var flagContentHasChanged = false;
  87. var key_lang = "<?php echo $key_lang ?>";
  88. var event_type_name = "<?php echo $event_name ?>";
  89. $(document).ready(function() {
  90. confirmMessage("eventList");
  91. if (event_type_name != 0) {
  92. $("#event_list_group").hide();
  93. }
  94. });
  95. function ajax(params,func) {
  96. $.ajax({
  97. url: "<?php echo $ajaxPath ?>",
  98. type: "POST",
  99. data: params,
  100. success: func
  101. });
  102. }
  103. function refreshUsersList() {
  104. removeAllOption($('#usersList'));
  105. $.each(usersList, function(ind,item) {
  106. addOption($('#usersList'), item.user_id, item.firstname + ' '+item.lastname);
  107. });
  108. }
  109. function getCurrentEventTypeName() {
  110. var name = false;
  111. if (event_type_name != 0) {
  112. return event_type_name;
  113. } else {
  114. return $('#eventList option:selected').first().attr('value');
  115. }
  116. }
  117. function self_sent_lock(self_sent) {
  118. if (self_sent == true) {
  119. $(".registration_case").show();
  120. $("#usersList").attr('disabled', 'true');
  121. $("#usersSubList").attr('disabled', 'true');
  122. removeAllOption($('#usersSubList'));
  123. } else {
  124. $(".registration_case").show();
  125. $("#usersList").removeAttr('disabled');
  126. $("#usersSubList").removeAttr('disabled');
  127. }
  128. }
  129. function showEventType() {
  130. cleanInput();
  131. currentEventName = getCurrentEventTypeName();
  132. $("span#activated_checkbox").css("display", "inline"); // make checkbox visible
  133. $('input[name=activated]').attr('checked', false);
  134. var self_sent = false;
  135. if (typeof(eventsConfig[currentEventName])!='undefined') {
  136. // if registration, only sent to self_user
  137. if (eventsConfig[currentEventName].self_sent == true) {
  138. self_sent = true;
  139. }
  140. }
  141. self_sent_lock(self_sent);
  142. // List of events configuration
  143. $('#eventName').attr('value', currentEventName);
  144. $('#eventNameTitle').text('');
  145. //$('#descLangVar').text(eventsConfig[currentEventName].desc_lang_var);
  146. // Set message and subject accoding to the current interface language
  147. $.each(eventTypes,function(key,value) {
  148. if (eventTypes[key]["event_type_name"] == currentEventName) {
  149. $('#eventNameTitle').text(eventTypes[key]["nameLangVar"]);
  150. }
  151. if (eventTypes[key]["event_type_name"] == currentEventName && eventTypes[key]["activated"] == 1) {
  152. $('input[name=activated]').attr('checked', true);
  153. }
  154. if (eventTypes[key]["event_type_name"] == currentEventName && eventTypes[key]["dokeos_folder"] == currentLanguage) {
  155. $('#eventMessage').val(eventTypes[key]["message"]);
  156. $('#eventSubject').val(eventTypes[key]["subject"]);
  157. }
  158. });
  159. // Displays the available keys for the mail template (related to an event name)
  160. $('#keys').find('li').remove();
  161. if(typeof(eventsConfig[currentEventName]["available_keyvars"])!='undefined') {
  162. $.each(eventsConfig[currentEventName]["available_keyvars"],function(key,value) {
  163. $('#keys').append('<li>'+key+'</li>');
  164. });
  165. }
  166. if (self_sent == false ) {
  167. $.ajax({
  168. url: '<?php echo $ajaxPath ?>?action=get_event_users&eventName=' +currentEventName,
  169. dataType: 'json',
  170. success: function(data) {
  171. removeAllOption($('#usersSubList'));
  172. refreshUsersList();
  173. usersIds = new Array();
  174. var json = jQuery.parseJSON(data);
  175. $.each(json, function(ind,item) {
  176. addOption($('#usersSubList'),item.user_id, item.firstname + ' '+item.lastname);
  177. usersIds[ind] = item.value;
  178. removeOption($('#usersList'),item.user_id);
  179. });
  180. $('#eventUsers').attr('value',usersIds.join(';'));
  181. }
  182. });
  183. }
  184. }
  185. function submitForm() {
  186. if ($('#eventId')) {
  187. usersIds = new Array();
  188. $('#usersSubList option').each(function(ind,item) {
  189. usersIds[ind] = item.value;
  190. });
  191. $('#eventUsers').attr('value',usersIds.join(';'));
  192. return true;
  193. }
  194. return false;
  195. }
  196. function addOption(select,value,text) {
  197. select.append('<option value="'+value+'">'+text+'</option>');
  198. }
  199. function removeOption(select,value) {
  200. select.find('option[value='+value+']').remove();
  201. }
  202. function removeAllOption(select) {
  203. select.find('option').remove();
  204. }
  205. function moveUsers(src,dest) {
  206. src.find('option:selected').each(function(index,opt) {
  207. text = opt.text;
  208. val = opt.value;
  209. addOption(dest,val,text);
  210. removeOption(src,val);
  211. });
  212. }
  213. /**
  214. * Change the message of the mail according to the selected language
  215. */
  216. function changeLanguage()
  217. {
  218. cleanInput();
  219. currentEventName = getCurrentEventTypeName();
  220. $.each(eventTypes,function(key,value)
  221. {
  222. if(eventTypes[key]["event_type_name"] == currentEventName && eventTypes[key]["dokeos_folder"] == $('#languages option:selected').first().attr('value'))
  223. {
  224. $('#eventSubject').val(eventTypes[key]["subject"]);
  225. $('#eventMessage').val(eventTypes[key]["message"]);
  226. }
  227. });
  228. }
  229. /**
  230. * Set flag at true if message and/or content was changed
  231. */
  232. function contentChanged()
  233. {
  234. flagContentHasChanged = true;
  235. }
  236. /**
  237. * Asks if user want to abandon the changes he's done
  238. */
  239. function confirmMessage(sender) {
  240. if (flagContentHasChanged == true) {
  241. if (confirm(key_lang)) {
  242. flagContentHasChanged = false;
  243. if (sender == "eventList") {
  244. cleanInput();
  245. showEventType();
  246. } else if(sender == "languages") {
  247. cleanInput();
  248. changeLanguage();
  249. }
  250. }
  251. } else {
  252. if(sender == "eventList")
  253. showEventType();
  254. else if(sender == "languages")
  255. changeLanguage();
  256. }
  257. }
  258. /**
  259. * Empty the input and the textarea
  260. */
  261. function cleanInput() {
  262. $('#eventMessage').val("");
  263. $('#eventSubject').val("");
  264. }
  265. </script>
  266. <?php
  267. /**
  268. * HTML body
  269. * @todo move as template layout
  270. */
  271. ?>
  272. <div class="page-header">
  273. <h2><?php echo get_lang('EventMessageManagement') ?></h2>
  274. </div>
  275. <form method="POST" onSubmit="return submitForm(); ">
  276. <div class="row">
  277. <div class="col-md-12" id="event_list_group">
  278. <h4><?php echo get_lang('Events'); ?></h4>
  279. <select class="col-md-6" multiple="1" id="eventList" onchange="confirmMessage(this.name); return false;" name="eventList">
  280. <?php
  281. foreach ($event_config as $key => $config) {
  282. echo '<option value="' . $key . '">' . $config['name_lang_var'] . '</option>';
  283. }
  284. ?>
  285. </select>
  286. </div>
  287. <div class="col-md-4">
  288. <h4><?php echo get_lang('Users'); ?></h4>
  289. <select multiple="1" id="usersList" class="registration_case"></select>
  290. </div>
  291. <div class="col-md-4">
  292. <div class="registration_case">
  293. <button class="btn btn-default" onclick='moveUsers($("#usersList"),$("#usersSubList")); return false;'><em class="fa fa-arrow-left"></em></button>
  294. <br />
  295. <br />
  296. <button class="btn btn-default" onclick='moveUsers($("#usersSubList"),$("#usersList")); return false;'><em class="fa fa-arrow-right"></em></button>
  297. </div>
  298. </div>
  299. <div class="col-md-4">
  300. <h4><?php echo get_lang('ToBeWarnedUserList'); ?></h4>
  301. <select class="col-md-3" multiple="1" id="usersSubList" class="registration_case"></select>
  302. </div>
  303. </div>
  304. <br />
  305. <h2 id="eventNameTitle"></h2>
  306. <span id="activated_checkbox">
  307. <input type="checkbox" name="activated" value="1" />
  308. <label for="activated" style="display:inline;"><?php echo get_lang('ActivateEvent'); ?></label>
  309. </span>
  310. <br />
  311. <select id="languages" name="languages" style="margin-top:20px;" onclick='confirmMessage(this.name); return false;'>
  312. <?php foreach ($languages["name"] as $key => $value) {
  313. $english_name = $languages['folder'][$key]; ?>
  314. <option
  315. value="<?php echo $english_name; ?>" <?php echo ($english_name == api_get_language_isocode(
  316. )) ? "selected=selected" : ""; ?>><?php echo $value; ?></option>
  317. <?php } ?>
  318. </select>
  319. <input type="hidden" name="action" value="modEventType" />
  320. <input type="hidden" name="eventId" id="eventId" />
  321. <input type="hidden" name="eventUsers" id="eventUsers" />
  322. <input type="hidden" id="eventName" value="<?php echo $event_name ?>"/>
  323. <br />
  324. <br />
  325. <label for="eventSubject">
  326. <h4><?php echo get_lang('Subject'); ?></h4>
  327. </label>
  328. <input class="col-md-6" type="text" id="eventSubject" name="eventSubject" onchange="contentChanged(); return false;" />
  329. <br /><br />
  330. <table>
  331. <tr>
  332. <td>
  333. <label for="eventMessage"><h4><?php echo get_lang('Message'); ?></h4></label>
  334. </td>
  335. <td class="available_keys" style="padding-left: 30px;">
  336. <h4><?php echo get_lang('AvailableEventKeys'); ?></h4>
  337. </td>
  338. </tr>
  339. <tr>
  340. <td>
  341. <textarea class="col-md-6" rows="10" name="eventMessage" id="eventMessage" onchange="contentChanged(); return false;">
  342. </textarea>
  343. </td>
  344. <td class="available_keys">
  345. <div id="keys" style="padding-left: 50px;"><ul></ul></div>
  346. </td>
  347. </tr>
  348. </table>
  349. <br /><br />
  350. <input class="btn btn-primary" type="submit" value="<?php echo get_lang('Save'); ?>" />
  351. </form>
  352. <?php
  353. Display :: display_footer();