system_announcements.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This page allows the administrator to manage the system announcements.
  5. * @package chamilo.admin.announcement
  6. */
  7. // Resetting the course id.
  8. $cidReset = true;
  9. // Including the global initialization file.
  10. require_once '../inc/global.inc.php';
  11. // Setting the section (for the tabs).
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. $_SESSION['this_section']=$this_section;
  14. $action = isset($_GET['action']) ? $_GET['action'] : null;
  15. $action_todo = false;
  16. // Access restrictions
  17. api_protect_admin_script(true);
  18. // Setting breadcrumbs.
  19. $interbreadcrumb[] = array(
  20. "url" => 'index.php',
  21. "name" => get_lang('PlatformAdmin'),
  22. );
  23. $tool_name = null;
  24. if (empty($_GET['lang'])) {
  25. $_GET['lang'] = isset($_SESSION['user_language_choice']) ? $_SESSION['user_language_choice'] : null;
  26. }
  27. if (!empty($action)) {
  28. $interbreadcrumb[] = array ("url" => "system_announcements.php", "name" => get_lang('SystemAnnouncements'));
  29. if ($action == 'add') {
  30. $interbreadcrumb[] = array ("url" => '#', "name" => get_lang('AddAnnouncement'));
  31. }
  32. if ($action == 'edit') {
  33. $interbreadcrumb[] = array ("url" => '#', "name" => get_lang('Edit'));
  34. }
  35. } else {
  36. $tool_name = get_lang('SystemAnnouncements');
  37. }
  38. // Displaying the header.
  39. Display :: display_header($tool_name);
  40. if ($action != 'add' && $action != 'edit') {
  41. echo '<div class="actions">';
  42. echo '<a href="?action=add">'.Display::return_icon('add.png', get_lang('AddAnnouncement'), array(), 32).'</a>';
  43. echo '</div>';
  44. }
  45. /* MAIN CODE */
  46. $show_announcement_list = true;
  47. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  48. // Form was posted?
  49. if (isset($_POST['action'])) {
  50. $action_todo = true;
  51. }
  52. // Actions
  53. switch($action) {
  54. case 'make_visible':
  55. case 'make_invisible':
  56. $status = false;
  57. if ($action == 'make_visible') {
  58. $status = true;
  59. }
  60. SystemAnnouncementManager :: set_visibility($_GET['id'], $_GET['person'], $status);
  61. break;
  62. case 'delete':
  63. // Delete an announcement.
  64. SystemAnnouncementManager :: delete_announcement($_GET['id']);
  65. Display :: display_confirmation_message(get_lang('AnnouncementDeleted'));
  66. break;
  67. case 'delete_selected':
  68. foreach($_POST['id'] as $index => $id) {
  69. SystemAnnouncementManager :: delete_announcement($id);
  70. }
  71. Display :: display_confirmation_message(get_lang('AnnouncementDeleted'));
  72. $action_todo = false;
  73. break;
  74. case 'add':
  75. // Add an announcement.
  76. $values['action'] = 'add';
  77. // Set default time window: NOW -> NEXT WEEK
  78. $values['start'] = date('Y-m-d H:i:s', api_strtotime(api_get_local_time()));
  79. $values['end'] = date('Y-m-d H:i:s', api_strtotime(api_get_local_time()) + (7 * 24 * 60 * 60));
  80. $values['range'] =
  81. substr(api_get_local_time(time()), 0, 16).' / '.
  82. substr(api_get_local_time(time() + (7 * 24 * 60 * 60)), 0, 16);
  83. $action_todo = true;
  84. break;
  85. case 'edit':
  86. // Edit an announcement.
  87. $announcement = SystemAnnouncementManager:: get_announcement($_GET['id']);
  88. $values['id'] = $announcement->id;
  89. $values['title'] = $announcement->title;
  90. $values['content'] = $announcement->content;
  91. $values['start'] = api_get_local_time($announcement->date_start);
  92. $values['end'] = api_get_local_time($announcement->date_end);
  93. $values['range'] =
  94. substr(api_get_local_time($announcement->date_start), 0, 16).' / '.
  95. substr(api_get_local_time($announcement->date_end), 0, 16);
  96. $values['visible_teacher'] = $announcement->visible_teacher;
  97. $values['visible_student'] = $announcement->visible_student;
  98. $values['visible_guest'] = $announcement->visible_guest;
  99. $values['lang'] = $announcement->lang;
  100. $values['action'] = 'edit';
  101. $groups = SystemAnnouncementManager::get_announcement_groups($announcement->id);
  102. $values['group'] = isset($groups['group_id']) ? $groups['group_id'] : 0;
  103. $action_todo = true;
  104. break;
  105. }
  106. if ($action_todo) {
  107. if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'add') {
  108. $form_title = get_lang('AddNews');
  109. $url = api_get_self();
  110. } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
  111. $form_title = get_lang('EditNews');
  112. $url = api_get_self().'?id='.intval($_GET['id']);
  113. }
  114. $form = new FormValidator('system_announcement', 'post', $url);
  115. $form->addElement('header', '', $form_title);
  116. $form->addText('title', get_lang('Title'), true);
  117. $language_list = api_get_languages();
  118. $language_list_with_keys = array();
  119. $language_list_with_keys['all'] = get_lang('All');
  120. for ($i=0; $i<count($language_list['name']) ; $i++) {
  121. $language_list_with_keys[$language_list['folder'][$i]] = $language_list['name'][$i];
  122. }
  123. $form->addElement('select', 'lang',get_lang('Language'), $language_list_with_keys);
  124. $form->addHtmlEditor(
  125. 'content',
  126. get_lang('Content'),
  127. true,
  128. false,
  129. array(
  130. 'ToolbarSet' => 'PortalNews',
  131. 'Width' => '100%',
  132. 'Height' => '300',
  133. )
  134. );
  135. $form->addDateRangePicker('range', get_lang('StartTimeWindow'), true, array('id' => 'date_range'));
  136. $group = array();
  137. $group[]= $form->createElement('checkbox', 'visible_teacher', null, get_lang('Teacher'));
  138. $group[]= $form->createElement('checkbox', 'visible_student', null, get_lang('Student'));
  139. $group[]= $form->createElement('checkbox', 'visible_guest', null, get_lang('Guest'));
  140. $form->addGroup($group, null, get_lang('Visible'), '');
  141. $form->addElement('hidden', 'id');
  142. $userGroup = new UserGroup();
  143. $group_list = $userGroup->get_all();
  144. if (!empty($group_list)) {
  145. $group_list = array_column($group_list, 'name', 'id');
  146. $group_list[0] = get_lang('All');
  147. $form->addElement(
  148. 'select',
  149. 'group',
  150. get_lang('AnnouncementForGroup'),
  151. $group_list
  152. );
  153. }
  154. $values['group'] = isset($values['group']) ? $values['group'] : '0';
  155. $form->addElement('checkbox', 'send_mail', null, get_lang('SendMail'));
  156. if (isset($_REQUEST['action']) && $_REQUEST['action']=='add') {
  157. $form->addElement('checkbox', 'add_to_calendar', null, get_lang('AddToCalendar'));
  158. $text=get_lang('AddNews');
  159. $class='add';
  160. $form->addElement('hidden', 'action', 'add');
  161. } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
  162. $text=get_lang('EditNews');
  163. $class='save';
  164. $form->addElement('hidden', 'action', 'edit');
  165. }
  166. $form->addElement('checkbox', 'send_email_test', null, get_lang('SendOnlyAnEmailToMySelfToTest'));
  167. $form->addButtonSend($text, 'submit');
  168. $form->setDefaults($values);
  169. if ($form->validate()) {
  170. $values = $form->exportValues();
  171. if (!isset($values['visible_teacher'])) {
  172. $values['visible_teacher'] = false;
  173. }
  174. if (!isset($values['visible_student'])) {
  175. $values['visible_student'] = false;
  176. }
  177. if (!isset($values['visible_guest'])) {
  178. $values['visible_guest'] = false;
  179. }
  180. if ($values['lang'] == 'all') {
  181. $values['lang'] = null;
  182. }
  183. $sendMail = isset($values['send_mail']) ? $values['send_mail'] : null;
  184. switch ($values['action']) {
  185. case 'add':
  186. $announcement_id = SystemAnnouncementManager::add_announcement(
  187. $values['title'],
  188. $values['content'],
  189. $values['range_start'],
  190. $values['range_end'],
  191. $values['visible_teacher'],
  192. $values['visible_student'],
  193. $values['visible_guest'],
  194. $values['lang'],
  195. $sendMail,
  196. empty($values['add_to_calendar']) ? false : true,
  197. empty($values['send_email_test']) ? false : true
  198. );
  199. if ($announcement_id !== false) {
  200. SystemAnnouncementManager::announcement_for_groups(
  201. $announcement_id,
  202. array($values['group'])
  203. );
  204. Display :: display_confirmation_message(get_lang('AnnouncementAdded'));
  205. } else {
  206. $show_announcement_list = false;
  207. $form->display();
  208. }
  209. break;
  210. case 'edit':
  211. $sendMailTest = isset($values['send_email_test']) ? $values['send_email_test'] : null;
  212. if (SystemAnnouncementManager::update_announcement(
  213. $values['id'],
  214. $values['title'],
  215. $values['content'],
  216. $values['range_start'],
  217. $values['range_end'],
  218. $values['visible_teacher'],
  219. $values['visible_student'],
  220. $values['visible_guest'],
  221. $values['lang'],
  222. $sendMail,
  223. $sendMailTest
  224. )
  225. ) {
  226. SystemAnnouncementManager::announcement_for_groups(
  227. $values['id'],
  228. array($values['group'])
  229. );
  230. Display :: display_confirmation_message(get_lang('AnnouncementUpdated'));
  231. } else {
  232. $show_announcement_list = false;
  233. $form->display();
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. $show_announcement_list = true;
  240. } else {
  241. $form->display();
  242. $show_announcement_list = false;
  243. }
  244. }
  245. if ($show_announcement_list) {
  246. $announcements = SystemAnnouncementManager :: get_all_announcements();
  247. $announcement_data = array ();
  248. foreach ($announcements as $index => $announcement) {
  249. $row = array();
  250. $row[] = $announcement->id;
  251. $row[] = Display::return_icon(($announcement->visible ? 'accept.png' : 'exclamation.png'), ($announcement->visible ? get_lang('AnnouncementAvailable') : get_lang('AnnouncementNotAvailable')));
  252. $row[] = $announcement->title;
  253. $row[] = api_convert_and_format_date($announcement->date_start);
  254. $row[] = api_convert_and_format_date($announcement->date_end);
  255. $row[] = "<a href=\"?id=".$announcement->id."&person=".SystemAnnouncementManager::VISIBLE_TEACHER."&action=". ($announcement->visible_teacher ? 'make_invisible' : 'make_visible')."\">".Display::return_icon(($announcement->visible_teacher ? 'visible.gif' : 'invisible.gif'), get_lang('ShowOrHide'))."</a>";
  256. $row[] = "<a href=\"?id=".$announcement->id."&person=".SystemAnnouncementManager::VISIBLE_STUDENT."&action=". ($announcement->visible_student ? 'make_invisible' : 'make_visible')."\">".Display::return_icon(($announcement->visible_student ? 'visible.gif' : 'invisible.gif'), get_lang('ShowOrHide'))."</a>";
  257. $row[] = "<a href=\"?id=".$announcement->id."&person=".SystemAnnouncementManager::VISIBLE_GUEST."&action=". ($announcement->visible_guest ? 'make_invisible' : 'make_visible')."\">".Display::return_icon(($announcement->visible_guest ? 'visible.gif' : 'invisible.gif'), get_lang('ShowOrHide'))."</a>";
  258. $row[] = $announcement->lang;
  259. $row[] = "<a href=\"?action=edit&id=".$announcement->id."\">".Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL)."</a> <a href=\"?action=delete&id=".$announcement->id."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES))."')) return false;\">".Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)."</a>";
  260. $announcement_data[] = $row;
  261. }
  262. $table = new SortableTableFromArray($announcement_data);
  263. $table->set_header(0, '', false);
  264. $table->set_header(1, get_lang('Active'));
  265. $table->set_header(2, get_lang('Title'));
  266. $table->set_header(3, get_lang('StartTimeWindow'));
  267. $table->set_header(4, get_lang('EndTimeWindow'));
  268. $table->set_header(5, get_lang('Teacher'));
  269. $table->set_header(6, get_lang('Student'));
  270. $table->set_header(7, get_lang('Guest'));
  271. $table->set_header(8, get_lang('Language'));
  272. $table->set_header(9, get_lang('Modify'), false, 'width="50px"');
  273. $form_actions = array();
  274. $form_actions['delete_selected'] = get_lang('Delete');
  275. $table->set_form_actions($form_actions);
  276. $table->display();
  277. }
  278. Display :: display_footer();