announcements.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author Frederik Vermeire <frederik.vermeire@pandora.be>, UGent Internship
  5. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: code cleaning
  6. * @author Julio Montoya <gugli100@gmail.com>, MORE code cleaning 2011
  7. *
  8. * @abstract The task of the internship was to integrate the 'send messages to specific users' with the
  9. * Announcements tool and also add the resource linker here. The database also needed refactoring
  10. * as there was no title field (the title was merged into the content field)
  11. *
  12. * @package chamilo.announcements
  13. * multiple functions
  14. */
  15. // use anonymous mode when accessing this course tool
  16. $use_anonymous = true;
  17. // setting the global file that gets the general configuration, the databases, the languages, ...
  18. require_once __DIR__.'/../inc/global.inc.php';
  19. api_protect_course_script(true);
  20. api_protect_course_group(GroupManager::GROUP_TOOL_ANNOUNCEMENT);
  21. $token = Security::get_existing_token();
  22. $courseId = api_get_course_int_id();
  23. $_course = api_get_course_info_by_id($courseId);
  24. $group_id = api_get_group_id();
  25. $sessionId = api_get_session_id();
  26. $current_course_tool = TOOL_ANNOUNCEMENT;
  27. $this_section = SECTION_COURSES;
  28. $nameTools = get_lang('ToolAnnouncement');
  29. $allowToEdit = (
  30. api_is_allowed_to_edit(false, true) ||
  31. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  32. );
  33. $sessionId = api_get_session_id();
  34. $drhHasAccessToSessionContent = api_drh_can_access_all_session_content();
  35. if (!empty($sessionId) && $drhHasAccessToSessionContent) {
  36. $allowToEdit = $allowToEdit || api_is_drh();
  37. }
  38. // Configuration settings
  39. $display_announcement_list = true;
  40. $display_form = false;
  41. $display_title_list = true;
  42. // Maximum title messages to display
  43. $maximum = '12';
  44. // Length of the titles
  45. $length = '36';
  46. // Database Table Definitions
  47. $tbl_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  48. $tbl_sessions = Database::get_main_table(TABLE_MAIN_SESSION);
  49. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  50. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  51. $isTutor = false;
  52. if (!empty($group_id)) {
  53. $groupProperties = GroupManager:: get_group_properties($group_id);
  54. $interbreadcrumb[] = [
  55. 'url' => api_get_path(WEB_CODE_PATH)."group/group.php?".api_get_cidreq(),
  56. 'name' => get_lang('Groups'),
  57. ];
  58. $interbreadcrumb[] = [
  59. 'url' => api_get_path(WEB_CODE_PATH)."group/group_space.php?".api_get_cidreq(),
  60. 'name' => get_lang('GroupSpace').' '.$groupProperties['name'],
  61. ];
  62. if ($allowToEdit === false) {
  63. // Check if user is tutor group
  64. $isTutor = GroupManager::is_tutor_of_group(api_get_user_id(), $groupProperties, $courseId);
  65. if ($isTutor) {
  66. $allowToEdit = true;
  67. }
  68. }
  69. }
  70. /* Tracking */
  71. Event::event_access_tool(TOOL_ANNOUNCEMENT);
  72. $announcement_id = isset($_GET['id']) ? (int) $_GET['id'] : null;
  73. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : 'list';
  74. $announcement_number = AnnouncementManager::getNumberAnnouncements();
  75. $homeUrl = api_get_self().'?action=list&'.api_get_cidreq();
  76. $content = '';
  77. $searchFormToString = '';
  78. $logInfo = [
  79. 'tool' => TOOL_ANNOUNCEMENT,
  80. 'tool_id' => 0,
  81. 'tool_id_detail' => 0,
  82. 'action' => $action,
  83. ];
  84. Event::registerLog($logInfo);
  85. switch ($action) {
  86. case 'move':
  87. if (!$allowToEdit) {
  88. api_not_allowed(true);
  89. }
  90. /* Move announcement up/down */
  91. if (!empty($_GET['down'])) {
  92. $thisAnnouncementId = intval($_GET['down']);
  93. $sortDirection = "DESC";
  94. }
  95. if (!empty($_GET['up'])) {
  96. $thisAnnouncementId = intval($_GET['up']);
  97. $sortDirection = "ASC";
  98. }
  99. if (!empty($sortDirection)) {
  100. if (!in_array(trim(strtoupper($sortDirection)), ['ASC', 'DESC'])) {
  101. $sortDirection = 'ASC';
  102. }
  103. $announcementInfo = AnnouncementManager::get_by_id($courseId, $thisAnnouncementId);
  104. $sql = "SELECT DISTINCT announcement.id, announcement.display_order
  105. FROM $tbl_announcement announcement
  106. INNER JOIN $tbl_item_property itemproperty
  107. ON (announcement.c_id = itemproperty.c_id)
  108. WHERE
  109. announcement.c_id = $courseId AND
  110. itemproperty.c_id = $courseId AND
  111. itemproperty.ref = announcement.id AND
  112. itemproperty.tool = '".TOOL_ANNOUNCEMENT."' AND
  113. itemproperty.visibility <> 2
  114. ORDER BY display_order $sortDirection";
  115. $result = Database::query($sql);
  116. $thisAnnouncementOrderFound = false;
  117. $thisAnnouncementOrder = null;
  118. while (list($announcementId, $announcementOrder) = Database::fetch_row($result)) {
  119. if ($thisAnnouncementOrderFound) {
  120. $nextAnnouncementId = $announcementId;
  121. $nextAnnouncementOrder = $announcementOrder;
  122. $sql = "UPDATE $tbl_announcement SET display_order = '$nextAnnouncementOrder'
  123. WHERE c_id = $courseId AND id = $thisAnnouncementId";
  124. Database::query($sql);
  125. $sql = "UPDATE $tbl_announcement SET display_order = '$thisAnnouncementOrder'
  126. WHERE c_id = $courseId AND id = $nextAnnouncementId";
  127. Database::query($sql);
  128. break;
  129. }
  130. // STEP 1 : FIND THE ORDER OF THE ANNOUNCEMENT
  131. if ($announcementId == $thisAnnouncementId) {
  132. $thisAnnouncementOrder = $announcementOrder;
  133. $thisAnnouncementOrderFound = true;
  134. }
  135. }
  136. Display::addFlash(Display::return_message(get_lang('AnnouncementMoved')));
  137. header('Location: '.$homeUrl);
  138. exit;
  139. }
  140. break;
  141. case 'view':
  142. $interbreadcrumb[] = [
  143. 'url' => api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(),
  144. 'name' => $nameTools,
  145. ];
  146. $nameTools = get_lang('View');
  147. $content = AnnouncementManager::displayAnnouncement($announcement_id);
  148. break;
  149. case 'list':
  150. $htmlHeadXtra[] = api_get_jqgrid_js();
  151. $searchForm = new FormValidator(
  152. 'search_simple',
  153. 'post',
  154. api_get_self().'?'.api_get_cidreq(),
  155. '',
  156. [],
  157. FormValidator::LAYOUT_INLINE
  158. );
  159. $searchForm->addElement('text', 'keyword', get_lang('Title'));
  160. $users = CourseManager::get_user_list_from_course_code(api_get_course_id(), $sessionId);
  161. $userList = ['' => ''];
  162. if (!empty($users)) {
  163. foreach ($users as $user) {
  164. $userList[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  165. }
  166. }
  167. $users = [];
  168. $searchForm->addElement('select', 'user_id', get_lang('Users'), $userList);
  169. $searchForm->addButtonSearch(get_lang('Search'));
  170. $filterData = [];
  171. $keyword = '';
  172. $userIdToSearch = 0;
  173. if ($searchForm->validate()) {
  174. $filterData = $searchForm->getSubmitValues();
  175. $keyword = $filterData['keyword'];
  176. $userIdToSearch = $filterData['user_id'];
  177. }
  178. // jqgrid will use this URL to do the selects
  179. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_course_announcements&'.api_get_cidreq().'&title_to_search='.$keyword.'&user_id_to_search='.$userIdToSearch;
  180. $deleteUrl = api_get_path(WEB_AJAX_PATH).'announcement.ajax.php?a=delete_item&'.api_get_cidreq();
  181. $columns = [
  182. get_lang('Title'),
  183. get_lang('By'),
  184. get_lang('LastUpdateDate'),
  185. get_lang('Actions'),
  186. ];
  187. // Column config
  188. $columnModel = [
  189. [
  190. 'name' => 'title',
  191. 'index' => 'title',
  192. 'width' => '300',
  193. 'align' => 'left',
  194. 'sortable' => 'false',
  195. ],
  196. [
  197. 'name' => 'username',
  198. 'index' => 'username',
  199. 'width' => '100',
  200. 'align' => 'left',
  201. 'sortable' => 'false',
  202. ],
  203. [
  204. 'name' => 'lastedit_date',
  205. 'index' => 'lastedit_date',
  206. 'width' => '200',
  207. 'align' => 'left',
  208. 'sortable' => 'false',
  209. ],
  210. [
  211. 'name' => 'actions',
  212. 'index' => 'actions',
  213. 'width' => '150',
  214. 'align' => 'left',
  215. //'formatter' => 'action_formatter',
  216. 'sortable' => 'false',
  217. ],
  218. ];
  219. // Autowidth
  220. $extra_params['autowidth'] = 'true';
  221. // height auto
  222. $extra_params['height'] = 'auto';
  223. $editOptions = '';
  224. if (api_is_allowed_to_edit() || $isTutor) {
  225. $extra_params['multiselect'] = true;
  226. $editOptions = '
  227. $("#announcements").jqGrid(
  228. "navGrid",
  229. "#announcements_pager",
  230. { edit: false, add: false, del: true },
  231. { height:280, reloadAfterSubmit:false }, // edit options
  232. { height:280, reloadAfterSubmit:false }, // add options
  233. { reloadAfterSubmit:false, url: "'.$deleteUrl.'" }, // del options
  234. { width:500 } // search options
  235. );
  236. ';
  237. }
  238. $content = '<script>
  239. $(function() {'.
  240. Display::grid_js(
  241. 'announcements',
  242. $url,
  243. $columns,
  244. $columnModel,
  245. $extra_params,
  246. [],
  247. '',
  248. true
  249. ).$editOptions.'
  250. });
  251. </script>';
  252. $count = AnnouncementManager::getAnnouncements(
  253. $token,
  254. $announcement_number,
  255. true
  256. );
  257. if (empty($count)) {
  258. $html = '';
  259. if ($allowToEdit && (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath')) {
  260. $html .= '<div id="no-data-view">';
  261. $html .= '<h3>'.get_lang('Announcements').'</h3>';
  262. $html .= Display::return_icon('valves.png', '', [], 64);
  263. $html .= '<div class="controls">';
  264. $html .= Display::url(
  265. get_lang('AddAnnouncement'),
  266. api_get_self()."?".api_get_cidreq()."&action=add",
  267. ['class' => 'btn btn-primary']
  268. );
  269. $html .= '</div>';
  270. $html .= '</div>';
  271. } else {
  272. $html = Display::return_message(get_lang('NoAnnouncements'), 'warning');
  273. }
  274. $content = $html;
  275. } else {
  276. $content .= Display::grid_html('announcements');
  277. }
  278. break;
  279. case 'delete':
  280. /* Delete announcement */
  281. $id = intval($_GET['id']);
  282. if ($sessionId != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  283. api_not_allowed();
  284. }
  285. $delete = false;
  286. if (api_is_platform_admin()) {
  287. $delete = true;
  288. }
  289. if (!api_is_session_general_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  290. $delete = true;
  291. }
  292. if ($delete) {
  293. AnnouncementManager::delete_announcement($_course, $id);
  294. Display::addFlash(Display::return_message(get_lang('AnnouncementDeleted')));
  295. }
  296. header('Location: '.$homeUrl);
  297. exit;
  298. break;
  299. case 'delete_all':
  300. if (api_is_allowed_to_edit()) {
  301. $allow = api_get_configuration_value('disable_delete_all_announcements');
  302. if ($allow === false) {
  303. AnnouncementManager::delete_all_announcements($_course);
  304. Display::addFlash(Display::return_message(get_lang('AnnouncementDeletedAll')));
  305. }
  306. header('Location: '.$homeUrl);
  307. exit;
  308. }
  309. break;
  310. case 'delete_attachment':
  311. $id = (int) $_GET['id_attach'];
  312. if (api_is_allowed_to_edit()) {
  313. AnnouncementManager::delete_announcement_attachment_file($id);
  314. }
  315. header('Location: '.$homeUrl);
  316. exit;
  317. break;
  318. case 'showhide':
  319. if (!isset($_GET['isStudentView']) || $_GET['isStudentView'] != 'false') {
  320. if (isset($_GET['id']) && $_GET['id']) {
  321. if ($sessionId != 0 &&
  322. api_is_allowed_to_session_edit(false, true) == false
  323. ) {
  324. api_not_allowed();
  325. }
  326. if (!$allowToEdit) {
  327. api_not_allowed(true);
  328. }
  329. if (!api_is_session_general_coach() ||
  330. api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $_GET['id'])
  331. ) {
  332. AnnouncementManager::change_visibility_announcement(
  333. $_course,
  334. $_GET['id']
  335. );
  336. Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
  337. header('Location: '.$homeUrl);
  338. exit;
  339. }
  340. }
  341. }
  342. break;
  343. case 'add':
  344. case 'modify':
  345. if ($sessionId != 0 &&
  346. api_is_allowed_to_session_edit(false, true) == false
  347. ) {
  348. api_not_allowed(true);
  349. }
  350. if (!$allowToEdit) {
  351. api_not_allowed(true);
  352. }
  353. // DISPLAY ADD ANNOUNCEMENT COMMAND
  354. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  355. $url = api_get_self().'?action='.$action.'&id='.$id.'&'.api_get_cidreq();
  356. $form = new FormValidator(
  357. 'announcement',
  358. 'post',
  359. $url,
  360. null,
  361. ['enctype' => 'multipart/form-data']
  362. );
  363. $form_name = get_lang('ModifyAnnouncement');
  364. if (empty($id)) {
  365. $form_name = get_lang('AddAnnouncement');
  366. }
  367. $interbreadcrumb[] = [
  368. 'url' => api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(),
  369. 'name' => $nameTools,
  370. ];
  371. $nameTools = $form_name;
  372. $form->addHeader($form_name);
  373. $form->addButtonAdvancedSettings(
  374. 'choose_recipients',
  375. [get_lang('ChooseRecipients')]
  376. );
  377. $form->addHtml('<div id="choose_recipients_options" style="display:none;">');
  378. $to = [];
  379. if (empty($group_id)) {
  380. if (isset($_GET['remind_inactive'])) {
  381. $email_ann = '1';
  382. $content_to_modify = sprintf(
  383. get_lang('RemindInactiveLearnersMailContent'),
  384. api_get_setting('siteName'),
  385. 7
  386. );
  387. $title_to_modify = sprintf(
  388. get_lang('RemindInactiveLearnersMailSubject'),
  389. api_get_setting('siteName')
  390. );
  391. } elseif (isset($_GET['remindallinactives']) && $_GET['remindallinactives'] === 'true') {
  392. // we want to remind inactive users. The $_GET['since'] parameter
  393. // determines which users have to be warned (i.e the users who have been inactive for x days or more
  394. $since = isset($_GET['since']) ? (int) $_GET['since'] : 6;
  395. // Getting the users who have to be reminded
  396. $to = Tracking::getInactiveStudentsInCourse(
  397. api_get_course_int_id(),
  398. $since,
  399. $sessionId
  400. );
  401. // setting the variables for the form elements: the users who need to receive the message
  402. foreach ($to as &$user) {
  403. $user = 'USER:'.$user;
  404. }
  405. // setting the variables for the form elements: the message has to be sent by email
  406. $email_ann = '1';
  407. // setting the variables for the form elements: the title of the email
  408. $title_to_modify = sprintf(
  409. get_lang('RemindInactiveLearnersMailSubject'),
  410. api_get_setting('siteName')
  411. );
  412. // setting the variables for the form elements: the message of the email
  413. $content_to_modify = sprintf(
  414. get_lang('RemindInactiveLearnersMailContent'),
  415. api_get_setting('siteName'),
  416. $since
  417. );
  418. // when we want to remind the users who have never been active
  419. // then we have a different subject and content for the announcement
  420. if ($_GET['since'] === 'never') {
  421. $title_to_modify = sprintf(
  422. get_lang('RemindInactiveLearnersMailSubject'),
  423. api_get_setting('siteName')
  424. );
  425. $content_to_modify = get_lang(
  426. 'YourAccountIsActiveYouCanLoginAndCheckYourCourses'
  427. );
  428. }
  429. }
  430. $element = CourseManager::addUserGroupMultiSelect($form, []);
  431. } else {
  432. $element = CourseManager::addGroupMultiSelect($form, $groupProperties, []);
  433. }
  434. $form->addHtml('</div>');
  435. $form->addCheckBox('email_ann', '', get_lang('EmailOption'));
  436. if (!isset($announcement_to_modify)) {
  437. $announcement_to_modify = '';
  438. }
  439. $announcementInfo = [];
  440. if (!empty($id)) {
  441. $announcementInfo = AnnouncementManager::get_by_id($courseId, $id);
  442. }
  443. $showSubmitButton = true;
  444. if (!empty($announcementInfo)) {
  445. $to = AnnouncementManager::loadEditUsers('announcement', $id);
  446. if (!empty($group_id)) {
  447. $separated = CourseManager::separateUsersGroups($to);
  448. if (isset($separated['groups']) && count($separated['groups']) > 1) {
  449. $form->freeze();
  450. Display::addFlash(Display::return_message(get_lang('LockByTeacher')));
  451. $showSubmitButton = false;
  452. }
  453. }
  454. $defaults = [
  455. 'title' => $announcementInfo['title'],
  456. 'content' => $announcementInfo['content'],
  457. 'id' => $announcementInfo['id'],
  458. 'users' => $to,
  459. ];
  460. } else {
  461. $defaults = [];
  462. if (!empty($to)) {
  463. $defaults['users'] = $to;
  464. }
  465. }
  466. $ajaxUrl = api_get_path(WEB_AJAX_PATH).'announcement.ajax.php?'.api_get_cidreq().'&a=preview';
  467. $form->addHtml("
  468. <script>
  469. $(function () {
  470. $('#announcement_preview').on('click', function() {
  471. var users = [];
  472. $('#users_to option').each(function() {
  473. users.push($(this).val());
  474. });
  475. var form = $('#announcement').serialize();
  476. $.ajax({
  477. type: 'POST',
  478. dataType: 'json',
  479. url: '".$ajaxUrl."',
  480. data: {users : JSON.stringify(users), form: form},
  481. beforeSend: function() {
  482. $('#announcement_preview_result').html('<i class=\"fa fa-spinner\"></i>');
  483. $('#send_button').hide();
  484. },
  485. success: function(result) {
  486. var resultToString = '';
  487. $.each(result, function(index, value) {
  488. resultToString += '&nbsp;' + value;
  489. });
  490. $('#announcement_preview_result').html('' +
  491. '".addslashes(get_lang('AnnouncementWillBeSentTo'))."<br/>' + resultToString
  492. );
  493. $('#announcement_preview_result').show();
  494. $('#send_button').show();
  495. }
  496. });
  497. });
  498. });
  499. </script>
  500. ");
  501. if (isset($defaults['users'])) {
  502. foreach ($defaults['users'] as $value) {
  503. $parts = explode(':', $value);
  504. if (!isset($parts[1]) || empty($parts[1])) {
  505. continue;
  506. }
  507. $form->addHtml(
  508. "
  509. <script>
  510. $(function () {
  511. $('#choose_recipients').click();
  512. });
  513. </script>
  514. ");
  515. break;
  516. }
  517. }
  518. $defaults['email_ann'] = true;
  519. $form->addElement(
  520. 'text',
  521. 'title',
  522. get_lang('EmailTitle'),
  523. ['onkeypress' => 'return event.keyCode != 13;']
  524. );
  525. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  526. $form->addElement('hidden', 'id');
  527. $htmlTags = '';
  528. $tags = AnnouncementManager::getTags();
  529. foreach ($tags as $tag) {
  530. $htmlTags .= "<b>$tag</b><br />";
  531. }
  532. $form->addButtonAdvancedSettings('tags', get_lang('Tags'));
  533. $form->addElement('html', '<div id="tags_options" style="display:none">');
  534. $form->addLabel('', Display::return_message($htmlTags, 'normal', false));
  535. $form->addElement('html', '</div>');
  536. $form->addHtmlEditor(
  537. 'content',
  538. get_lang('Description'),
  539. true,
  540. false,
  541. ['ToolbarSet' => 'Announcements']
  542. );
  543. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  544. $form->addElement('textarea', 'file_comment', get_lang('FileComment'));
  545. $form->addHidden('sec_token', $token);
  546. if (empty($sessionId)) {
  547. $form->addCheckBox('send_to_users_in_session', null, get_lang('SendToUsersInSessions'));
  548. }
  549. $config = api_get_configuration_value('announcements_hide_send_to_hrm_users');
  550. if ($config === false) {
  551. $form->addCheckBox(
  552. 'send_to_hrm_users',
  553. null,
  554. get_lang('SendAnnouncementCopyToDRH'),
  555. ['id' => 'send_to_hrm_users']
  556. );
  557. }
  558. $form->addCheckBox('send_me_a_copy_by_email', null, get_lang('SendAnnouncementCopyToMyself'));
  559. $defaults['send_me_a_copy_by_email'] = true;
  560. if ($showSubmitButton) {
  561. $form->addLabel('',
  562. Display::url(
  563. get_lang('Preview'),
  564. 'javascript:void(0)',
  565. ['class' => 'btn btn-default', 'id' => 'announcement_preview']
  566. ).'<div id="announcement_preview_result" style="display:none"></div>'
  567. );
  568. $form->addHtml('<div id="send_button" style="display:none">');
  569. $form->addButtonSave(get_lang('ButtonPublishAnnouncement'));
  570. $form->addHtml('</div>');
  571. }
  572. $form->setDefaults($defaults);
  573. if ($form->validate()) {
  574. $data = $form->getSubmitValues();
  575. $data['users'] = isset($data['users']) ? $data['users'] : [];
  576. $sendToUsersInSession = isset($data['send_to_users_in_session']) ? true : false;
  577. $sendMeCopy = isset($data['send_me_a_copy_by_email']) ? true : false;
  578. if (isset($id) && $id) {
  579. // there is an Id => the announcement already exists => update mode
  580. if (Security::check_token('post')) {
  581. $file_comment = $_POST['file_comment'];
  582. $file = $_FILES['user_upload'];
  583. AnnouncementManager::edit_announcement(
  584. $id,
  585. $data['title'],
  586. $data['content'],
  587. $data['users'],
  588. $file,
  589. $file_comment,
  590. $sendToUsersInSession
  591. );
  592. // Send mail
  593. $messageSentTo = [];
  594. if (isset($_POST['email_ann']) && empty($_POST['onlyThoseMails'])) {
  595. $messageSentTo = AnnouncementManager::sendEmail(
  596. api_get_course_info(),
  597. api_get_session_id(),
  598. $id,
  599. $sendToUsersInSession,
  600. isset($data['send_to_hrm_users'])
  601. );
  602. }
  603. if ($sendMeCopy && !in_array(api_get_user_id(), $messageSentTo)) {
  604. $email = new AnnouncementEmail(api_get_course_info(), api_get_session_id(), $id);
  605. $email->sendAnnouncementEmailToMySelf();
  606. }
  607. Display::addFlash(
  608. Display::return_message(
  609. get_lang('AnnouncementModified'),
  610. 'success'
  611. )
  612. );
  613. Security::clear_token();
  614. header('Location: '.$homeUrl);
  615. exit;
  616. }
  617. } else {
  618. // Insert mode
  619. if (Security::check_token('post')) {
  620. $file = $_FILES['user_upload'];
  621. $file_comment = $data['file_comment'];
  622. if (empty($group_id)) {
  623. $insert_id = AnnouncementManager::add_announcement(
  624. api_get_course_info(),
  625. api_get_session_id(),
  626. $data['title'],
  627. $data['content'],
  628. $data['users'],
  629. $file,
  630. $file_comment,
  631. null,
  632. $sendToUsersInSession
  633. );
  634. } else {
  635. $insert_id = AnnouncementManager::addGroupAnnouncement(
  636. $data['title'],
  637. $data['content'],
  638. $group_id,
  639. $data['users'],
  640. $file,
  641. $file_comment,
  642. $sendToUsersInSession
  643. );
  644. }
  645. if ($insert_id) {
  646. Display::addFlash(
  647. Display::return_message(
  648. get_lang('AnnouncementAdded'),
  649. 'success'
  650. )
  651. );
  652. // Send mail
  653. $messageSentTo = [];
  654. if (isset($data['email_ann']) && $data['email_ann']) {
  655. $messageSentTo = AnnouncementManager::sendEmail(
  656. api_get_course_info(),
  657. api_get_session_id(),
  658. $insert_id,
  659. $sendToUsersInSession
  660. );
  661. }
  662. if ($sendMeCopy && !in_array(api_get_user_id(), $messageSentTo)) {
  663. $email = new AnnouncementEmail(api_get_course_info(), api_get_session_id(), $insert_id);
  664. $email->sendAnnouncementEmailToMySelf();
  665. }
  666. Security::clear_token();
  667. header('Location: '.$homeUrl);
  668. exit;
  669. }
  670. api_not_allowed(true);
  671. } // end condition token
  672. }
  673. }
  674. $content = $form->returnForm();
  675. break;
  676. }
  677. if (!empty($_GET['remind_inactive'])) {
  678. $to[] = 'USER:'.intval($_GET['remind_inactive']);
  679. }
  680. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  681. // We are not in the learning path
  682. Display::display_header($nameTools, get_lang('Announcements'));
  683. }
  684. // Tool introduction
  685. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  686. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  687. }
  688. // Actions
  689. $show_actions = false;
  690. $actionsLeft = '';
  691. if ($allowToEdit && (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath')) {
  692. if (in_array($action, ['add', 'modify', 'view'])) {
  693. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."'>".
  694. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).
  695. "</a>";
  696. } else {
  697. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."&action=add'>".
  698. Display::return_icon('new_announce.png', get_lang('AddAnnouncement'), '', ICON_SIZE_MEDIUM).
  699. "</a>";
  700. }
  701. $show_actions = true;
  702. } else {
  703. if (in_array($action, ['view'])) {
  704. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."'>".
  705. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM)."</a>";
  706. }
  707. }
  708. if ($allowToEdit && api_get_group_id() == 0) {
  709. $allow = api_get_configuration_value('disable_delete_all_announcements');
  710. if ($allow === false) {
  711. if (!isset($_GET['action']) ||
  712. isset($_GET['action']) && $_GET['action'] == 'list'
  713. ) {
  714. $actionsLeft .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete_all\" onclick=\"javascript:if(!confirm('".get_lang("ConfirmYourChoice")."')) return false;\">".
  715. Display::return_icon(
  716. 'delete_announce.png',
  717. get_lang('AnnouncementDeleteAll'),
  718. '',
  719. ICON_SIZE_MEDIUM
  720. )."</a>";
  721. }
  722. }
  723. }
  724. if ($show_actions) {
  725. echo Display::toolbarAction('toolbar', [$actionsLeft, $searchFormToString]);
  726. }
  727. echo $content;
  728. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  729. //we are not in learnpath tool
  730. Display::display_footer();
  731. }