system_announcements.php 13 KB

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