system_announcements.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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('Administration'),
  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('Portal news'),
  33. ];
  34. if ($action == 'add') {
  35. $interbreadcrumb[] = [
  36. "url" => '#',
  37. "name" => get_lang('Add an announcement'),
  38. ];
  39. }
  40. if ($action == 'edit') {
  41. $interbreadcrumb[] = ["url" => '#', "name" => get_lang('Edit')];
  42. }
  43. } else {
  44. $tool_name = get_lang('Portal news');
  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('Add an announcement'), [], 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('Update successful'), 'confirmation');
  74. break;
  75. case 'delete':
  76. // Delete an announcement.
  77. SystemAnnouncementManager::delete_announcement($_GET['id']);
  78. echo Display::return_message(get_lang('Announcement has been deleted'), '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('Announcement has been deleted'), '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('Add news');
  124. $url = api_get_self();
  125. } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
  126. $form_title = get_lang('Edit News');
  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. // Add Picture Announcements
  152. try {
  153. $form->addFile(
  154. 'picture',
  155. [
  156. get_lang('Add Picture'),
  157. get_lang('The image must have a maximum dimension of 950 x 712 pixelss'),
  158. ],
  159. ['id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '4/3']
  160. );
  161. $allowed_picture_types = api_get_supported_image_extensions(false);
  162. $form->addRule(
  163. 'picture',
  164. get_lang('Only PNG, JPG or GIF images allowed').' ('.implode(',', $allowed_picture_types).')',
  165. 'filetype',
  166. $allowed_picture_types
  167. );
  168. $image = '';
  169. // Display announcements picture
  170. $store_path = api_get_path(SYS_UPLOAD_PATH).'announcements'; // course path
  171. if (file_exists($store_path.'/announcement_'.$announcement->id.'.png')) {
  172. $announcementsPath = api_get_path(WEB_UPLOAD_PATH).'announcements'; // announcement web path
  173. $announcementsImage = $announcementsPath.'/announcement_'.$announcement->id.'_100x100.png?'.rand(1, 1000); // redimensioned image 85x85
  174. $image = '<div class="row"><label class="col-md-2 control-label">'.get_lang('Image').'</label>
  175. <div class="col-md-8"><img class="img-thumbnail" src="'.$announcementsImage.'" /></div></div>';
  176. $form->addHtml($image);
  177. $form->addElement('checkbox', 'delete_picture', null, get_lang('Delete picture'));
  178. }
  179. } catch (Exception $e) {
  180. } catch (HTML_QuickForm_Error $e) {
  181. error_log($e);
  182. }
  183. $form->addDateRangePicker(
  184. 'range',
  185. get_lang('Start'),
  186. true,
  187. ['id' => 'range']
  188. );
  189. $group = [];
  190. foreach ($visibleList as $key => $name) {
  191. $group[] = $form->createElement(
  192. 'checkbox',
  193. $key,
  194. null,
  195. $name
  196. );
  197. }
  198. $form->addGroup($group, null, get_lang('Visible'));
  199. $form->addElement('hidden', 'id');
  200. $userGroup = new UserGroup();
  201. $group_list = $userGroup->get_all();
  202. if (!empty($group_list)) {
  203. $group_list = array_column($group_list, 'name', 'id');
  204. $group_list[0] = get_lang('All');
  205. $form->addElement(
  206. 'select',
  207. 'group',
  208. get_lang('Announcement for a group'),
  209. $group_list
  210. );
  211. }
  212. $values['group'] = isset($values['group']) ? $values['group'] : '0';
  213. $form->addElement('checkbox', 'send_mail', null, get_lang('Send mail'));
  214. if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'add') {
  215. $form->addElement('checkbox', 'add_to_calendar', null, get_lang('Add to calendar'));
  216. $text = get_lang('Add news');
  217. $class = 'add';
  218. $form->addElement('hidden', 'action', 'add');
  219. } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
  220. $text = get_lang('Edit News');
  221. $class = 'save';
  222. $form->addElement('hidden', 'action', 'edit');
  223. }
  224. $form->addElement('checkbox', 'send_email_test', null, get_lang('Send an email to myself for testing purposes.'));
  225. $form->addButtonSend($text, 'submit');
  226. $form->setDefaults($values);
  227. if ($form->validate()) {
  228. $values = $form->getSubmitValues();
  229. $visibilityResult = [];
  230. foreach ($visibleList as $key => $value) {
  231. if (!isset($values[$key])) {
  232. $values[$key] = false;
  233. }
  234. $visibilityResult[$key] = (int) $values[$key];
  235. }
  236. if ($values['lang'] == 'all') {
  237. $values['lang'] = null;
  238. }
  239. $sendMail = isset($values['send_mail']) ? $values['send_mail'] : null;
  240. switch ($values['action']) {
  241. case 'add':
  242. $announcement_id = SystemAnnouncementManager::add_announcement(
  243. $values['title'],
  244. $values['content'],
  245. $values['range_start'],
  246. $values['range_end'],
  247. $visibilityResult,
  248. $values['lang'],
  249. $sendMail,
  250. empty($values['add_to_calendar']) ? false : true,
  251. empty($values['send_email_test']) ? false : true
  252. );
  253. if ($announcement_id !== false) {
  254. // ADD Picture
  255. $picture = $_FILES['picture'];
  256. if (!empty($picture['name'])) {
  257. $picture_uri = SystemAnnouncementManager::update_announcements_picture(
  258. $announcement_id,
  259. $picture['tmp_name'],
  260. $values['picture_crop_result']
  261. );
  262. }
  263. if (isset($values['group'])) {
  264. SystemAnnouncementManager::announcement_for_groups(
  265. $announcement_id,
  266. [$values['group']]
  267. );
  268. echo Display::return_message(
  269. get_lang('Announcement has been added'),
  270. 'confirmation'
  271. );
  272. }
  273. } else {
  274. $show_announcement_list = false;
  275. $form->display();
  276. }
  277. break;
  278. case 'edit':
  279. $sendMailTest = isset($values['send_email_test']) ? $values['send_email_test'] : null;
  280. if (SystemAnnouncementManager::update_announcement(
  281. $values['id'],
  282. $values['title'],
  283. $values['content'],
  284. $values['range_start'],
  285. $values['range_end'],
  286. $visibilityResult,
  287. $values['lang'],
  288. $sendMail,
  289. $sendMailTest
  290. )) {
  291. $deletePicture = isset($values['delete_picture']) ? $values['delete_picture'] : '';
  292. if ($deletePicture) {
  293. SystemAnnouncementManager::deleteAnnouncementPicture($values['id']);
  294. } else {
  295. $picture = $_FILES['picture'];
  296. if (!empty($picture['name'])) {
  297. $picture_uri = SystemAnnouncementManager::update_announcements_picture(
  298. $values['id'],
  299. $picture['tmp_name'],
  300. $values['picture_crop_result']
  301. );
  302. }
  303. }
  304. if (isset($values['group'])) {
  305. SystemAnnouncementManager::announcement_for_groups(
  306. $values['id'],
  307. [$values['group']]
  308. );
  309. echo Display::return_message(
  310. get_lang('AnnouncementUpdate successful'),
  311. 'confirmation'
  312. );
  313. }
  314. } else {
  315. $show_announcement_list = false;
  316. $form->display();
  317. }
  318. break;
  319. default:
  320. break;
  321. }
  322. $show_announcement_list = true;
  323. } else {
  324. $form->display();
  325. $show_announcement_list = false;
  326. }
  327. }
  328. if ($show_announcement_list) {
  329. $announcements = SystemAnnouncementManager :: get_all_announcements();
  330. $announcement_data = [];
  331. foreach ($announcements as $index => $announcement) {
  332. $row = [];
  333. $row[] = $announcement->id;
  334. $row[] = Display::return_icon(($announcement->visible ? 'accept.png' : 'exclamation.png'), ($announcement->visible ? get_lang('The announcement is available') : get_lang('The announcement is not available')));
  335. $row[] = $announcement->title;
  336. $row[] = api_convert_and_format_date($announcement->date_start);
  337. $row[] = api_convert_and_format_date($announcement->date_end);
  338. $data = (array) $announcement;
  339. foreach ($visibleList as $key => $value) {
  340. $value = $data[$key];
  341. $action = $value ? 'make_invisible' : 'make_visible';
  342. $row[] = "<a href=\"?id=".$announcement->id."&person=".$key."&action=".$action."\">".
  343. Display::return_icon(($value ? 'eyes.png' : 'eyes-close.png'), get_lang('Show/Hide'))."</a>";
  344. }
  345. /*$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('Show/Hide'))."</a>";
  346. $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('Show/Hide'))."</a>";
  347. $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('Show/Hide'))."</a>";*/
  348. $row[] = $announcement->lang;
  349. $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."\" title=".addslashes(api_htmlentities(get_lang('Please confirm your choice')))." class='delete-swal' >".Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL)."</a>";
  350. $announcement_data[] = $row;
  351. }
  352. $table = new SortableTableFromArray($announcement_data);
  353. $table->set_header(0, '', false, 'width="20px"');
  354. $table->set_header(1, get_lang('active'));
  355. $table->set_header(2, get_lang('Title'));
  356. $table->set_header(3, get_lang('Start'));
  357. $table->set_header(4, get_lang('End'));
  358. $count = 5;
  359. foreach ($visibleList as $key => $title) {
  360. $table->set_header($count, $title);
  361. $count++;
  362. }
  363. $table->set_header($count++, get_lang('Language'));
  364. $table->set_header($count++, get_lang('Edit'), false, 'width="50px"');
  365. $form_actions = [];
  366. $form_actions['delete_selected'] = get_lang('Delete');
  367. $table->set_form_actions($form_actions);
  368. $table->display();
  369. }
  370. Display :: display_footer();