system_announcements.php 13 KB

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