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