announcements.php 31 KB

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