system_announcements.php 13 KB

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