announcements.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. 'sortable' => 'false',
  222. ],
  223. ];
  224. // Autowidth
  225. $extra_params['autowidth'] = 'true';
  226. // height auto
  227. $extra_params['height'] = 'auto';
  228. $editOptions = '';
  229. if ($isTutor || api_is_allowed_to_edit()) {
  230. $extra_params['multiselect'] = true;
  231. $editOptions = '
  232. $("#announcements").jqGrid(
  233. "navGrid",
  234. "#announcements_pager",
  235. { edit: false, add: false, del: true },
  236. { height:280, reloadAfterSubmit:false }, // edit options
  237. { height:280, reloadAfterSubmit:false }, // add options
  238. { reloadAfterSubmit:false, url: "'.$deleteUrl.'" }, // del options
  239. { width:500 } // search options
  240. );
  241. ';
  242. }
  243. $content = '<script>
  244. $(function() {'.
  245. Display::grid_js(
  246. 'announcements',
  247. $url,
  248. $columns,
  249. $columnModel,
  250. $extra_params,
  251. [],
  252. '',
  253. true
  254. ).$editOptions.'
  255. });
  256. </script>';
  257. $count = AnnouncementManager::getAnnouncements(
  258. $token,
  259. $announcement_number,
  260. true
  261. );
  262. if (empty($count)) {
  263. $html = '';
  264. if (($allowToEdit || $allowStudentInGroupToSend) &&
  265. (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath')
  266. ) {
  267. $html .= '<div id="no-data-view">';
  268. $html .= '<h3>'.get_lang('Announcements').'</h3>';
  269. $html .= Display::return_icon('valves.png', '', [], 64);
  270. $html .= '<div class="controls">';
  271. $html .= Display::url(
  272. get_lang('AddAnnouncement'),
  273. api_get_self()."?".api_get_cidreq()."&action=add",
  274. ['class' => 'btn btn-primary']
  275. );
  276. $html .= '</div>';
  277. $html .= '</div>';
  278. } else {
  279. $html = Display::return_message(get_lang('NoAnnouncements'), 'warning');
  280. }
  281. $content = $html;
  282. } else {
  283. $content .= Display::grid_html('announcements');
  284. }
  285. break;
  286. case 'delete':
  287. /* Delete announcement */
  288. $id = intval($_GET['id']);
  289. if ($sessionId != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  290. api_not_allowed();
  291. }
  292. $delete = false;
  293. if (api_is_platform_admin()) {
  294. $delete = true;
  295. }
  296. if (!api_is_session_general_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  297. $delete = true;
  298. }
  299. if ($delete) {
  300. AnnouncementManager::delete_announcement($_course, $id);
  301. Display::addFlash(Display::return_message(get_lang('AnnouncementDeleted')));
  302. }
  303. header('Location: '.$homeUrl);
  304. exit;
  305. break;
  306. case 'delete_all':
  307. if (api_is_allowed_to_edit()) {
  308. $allow = api_get_configuration_value('disable_delete_all_announcements');
  309. if ($allow === false) {
  310. AnnouncementManager::delete_all_announcements($_course);
  311. Display::addFlash(Display::return_message(get_lang('AnnouncementDeletedAll')));
  312. }
  313. header('Location: '.$homeUrl);
  314. exit;
  315. }
  316. break;
  317. case 'delete_attachment':
  318. $id = (int) $_GET['id_attach'];
  319. if (api_is_allowed_to_edit()) {
  320. AnnouncementManager::delete_announcement_attachment_file($id);
  321. }
  322. header('Location: '.$homeUrl);
  323. exit;
  324. break;
  325. case 'showhide':
  326. if (!isset($_GET['isStudentView']) || $_GET['isStudentView'] != 'false') {
  327. if (isset($_GET['id']) && $_GET['id']) {
  328. if ($sessionId != 0 &&
  329. api_is_allowed_to_session_edit(false, true) == false
  330. ) {
  331. api_not_allowed();
  332. }
  333. if (!$allowToEdit) {
  334. api_not_allowed(true);
  335. }
  336. if (!api_is_session_general_coach() ||
  337. api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $_GET['id'])
  338. ) {
  339. AnnouncementManager::change_visibility_announcement(
  340. $_course,
  341. $_GET['id']
  342. );
  343. Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
  344. header('Location: '.$homeUrl);
  345. exit;
  346. }
  347. }
  348. }
  349. break;
  350. case 'add':
  351. case 'modify':
  352. if ($sessionId != 0 &&
  353. api_is_allowed_to_session_edit(false, true) == false
  354. ) {
  355. api_not_allowed(true);
  356. }
  357. if ($allowStudentInGroupToSend === false) {
  358. if (!$allowToEdit) {
  359. api_not_allowed(true);
  360. }
  361. }
  362. // DISPLAY ADD ANNOUNCEMENT COMMAND
  363. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  364. $url = api_get_self().'?action='.$action.'&id='.$id.'&'.api_get_cidreq();
  365. $form = new FormValidator(
  366. 'announcement',
  367. 'post',
  368. $url,
  369. null,
  370. ['enctype' => 'multipart/form-data']
  371. );
  372. $form_name = get_lang('ModifyAnnouncement');
  373. if (empty($id)) {
  374. $form_name = get_lang('AddAnnouncement');
  375. }
  376. $interbreadcrumb[] = [
  377. 'url' => api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(),
  378. 'name' => $nameTools,
  379. ];
  380. $nameTools = $form_name;
  381. $form->addHeader($form_name);
  382. $form->addButtonAdvancedSettings(
  383. 'choose_recipients',
  384. [get_lang('ChooseRecipients')]
  385. );
  386. $form->addHtml('<div id="choose_recipients_options" style="display:none;">');
  387. $to = [];
  388. if (empty($group_id)) {
  389. if (isset($_GET['remind_inactive'])) {
  390. $email_ann = '1';
  391. $content_to_modify = sprintf(
  392. get_lang('RemindInactiveLearnersMailContent'),
  393. api_get_setting('siteName'),
  394. 7
  395. );
  396. $title_to_modify = sprintf(
  397. get_lang('RemindInactiveLearnersMailSubject'),
  398. api_get_setting('siteName')
  399. );
  400. } elseif (isset($_GET['remindallinactives']) && $_GET['remindallinactives'] === 'true') {
  401. // we want to remind inactive users. The $_GET['since'] parameter
  402. // determines which users have to be warned (i.e the users who have been inactive for x days or more
  403. $since = isset($_GET['since']) ? (int) $_GET['since'] : 6;
  404. // Getting the users who have to be reminded
  405. $to = Tracking::getInactiveStudentsInCourse(
  406. api_get_course_int_id(),
  407. $since,
  408. $sessionId
  409. );
  410. // setting the variables for the form elements: the users who need to receive the message
  411. foreach ($to as &$user) {
  412. $user = 'USER:'.$user;
  413. }
  414. // setting the variables for the form elements: the message has to be sent by email
  415. $email_ann = '1';
  416. // setting the variables for the form elements: the title of the email
  417. $title_to_modify = sprintf(
  418. get_lang('RemindInactiveLearnersMailSubject'),
  419. api_get_setting('siteName')
  420. );
  421. // setting the variables for the form elements: the message of the email
  422. $content_to_modify = sprintf(
  423. get_lang('RemindInactiveLearnersMailContent'),
  424. api_get_setting('siteName'),
  425. $since
  426. );
  427. // when we want to remind the users who have never been active
  428. // then we have a different subject and content for the announcement
  429. if ($_GET['since'] === 'never') {
  430. $title_to_modify = sprintf(
  431. get_lang('RemindInactiveLearnersMailSubject'),
  432. api_get_setting('siteName')
  433. );
  434. $content_to_modify = get_lang(
  435. 'YourAccountIsActiveYouCanLoginAndCheckYourCourses'
  436. );
  437. }
  438. }
  439. $element = CourseManager::addUserGroupMultiSelect($form, []);
  440. } else {
  441. $element = CourseManager::addGroupMultiSelect($form, $groupProperties, []);
  442. }
  443. $form->addHtml('</div>');
  444. $form->addCheckBox('email_ann', '', get_lang('EmailOption'));
  445. if (!isset($announcement_to_modify)) {
  446. $announcement_to_modify = '';
  447. }
  448. $announcementInfo = [];
  449. if (!empty($id)) {
  450. $announcementInfo = AnnouncementManager::get_by_id($courseId, $id);
  451. }
  452. $showSubmitButton = true;
  453. if (!empty($announcementInfo)) {
  454. $to = AnnouncementManager::loadEditUsers('announcement', $id);
  455. if (!empty($group_id)) {
  456. $separated = CourseManager::separateUsersGroups($to);
  457. if (isset($separated['groups']) && count($separated['groups']) > 1) {
  458. $form->freeze();
  459. Display::addFlash(Display::return_message(get_lang('LockByTeacher')));
  460. $showSubmitButton = false;
  461. }
  462. }
  463. $defaults = [
  464. 'title' => $announcementInfo['title'],
  465. 'content' => $announcementInfo['content'],
  466. 'id' => $announcementInfo['id'],
  467. 'users' => $to,
  468. ];
  469. } else {
  470. $defaults = [];
  471. if (!empty($to)) {
  472. $defaults['users'] = $to;
  473. }
  474. }
  475. $ajaxUrl = api_get_path(WEB_AJAX_PATH).'announcement.ajax.php?'.api_get_cidreq().'&a=preview';
  476. $form->addHtml("
  477. <script>
  478. $(function () {
  479. $('#announcement_preview').on('click', function() {
  480. var users = [];
  481. $('#users_to option').each(function() {
  482. users.push($(this).val());
  483. });
  484. var form = $('#announcement').serialize();
  485. $.ajax({
  486. type: 'POST',
  487. dataType: 'json',
  488. url: '".$ajaxUrl."',
  489. data: {users : JSON.stringify(users), form: form},
  490. beforeSend: function() {
  491. $('#announcement_preview_result').html('<i class=\"fa fa-spinner\"></i>');
  492. $('#send_button').hide();
  493. },
  494. success: function(result) {
  495. var resultToString = '';
  496. $.each(result, function(index, value) {
  497. resultToString += '&nbsp;' + value;
  498. });
  499. $('#announcement_preview_result').html('' +
  500. '".addslashes(get_lang('AnnouncementWillBeSentTo'))."<br/>' + resultToString
  501. );
  502. $('#announcement_preview_result').show();
  503. $('#send_button').show();
  504. }
  505. });
  506. });
  507. });
  508. </script>
  509. ");
  510. if (isset($defaults['users'])) {
  511. foreach ($defaults['users'] as $value) {
  512. $parts = explode(':', $value);
  513. if (!isset($parts[1]) || empty($parts[1])) {
  514. continue;
  515. }
  516. $form->addHtml(
  517. "
  518. <script>
  519. $(function () {
  520. $('#choose_recipients').click();
  521. });
  522. </script>
  523. ");
  524. break;
  525. }
  526. }
  527. $defaults['email_ann'] = true;
  528. $form->addElement(
  529. 'text',
  530. 'title',
  531. get_lang('EmailTitle'),
  532. ['onkeypress' => 'return event.keyCode != 13;']
  533. );
  534. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  535. $form->addElement('hidden', 'id');
  536. $htmlTags = '';
  537. $tags = AnnouncementManager::getTags();
  538. foreach ($tags as $tag) {
  539. $htmlTags .= "<b>$tag</b><br />";
  540. }
  541. $form->addButtonAdvancedSettings('tags', get_lang('Tags'));
  542. $form->addElement('html', '<div id="tags_options" style="display:none">');
  543. $form->addLabel('', Display::return_message($htmlTags, 'normal', false));
  544. $form->addElement('html', '</div>');
  545. $form->addHtmlEditor(
  546. 'content',
  547. get_lang('Description'),
  548. true,
  549. false,
  550. ['ToolbarSet' => 'Announcements']
  551. );
  552. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  553. $form->addElement('textarea', 'file_comment', get_lang('FileComment'));
  554. $form->addHidden('sec_token', $token);
  555. if (empty($sessionId)) {
  556. $form->addCheckBox('send_to_users_in_session', null, get_lang('SendToUsersInSessions'));
  557. }
  558. $config = api_get_configuration_value('announcements_hide_send_to_hrm_users');
  559. if ($config === false) {
  560. $form->addCheckBox(
  561. 'send_to_hrm_users',
  562. null,
  563. get_lang('SendAnnouncementCopyToDRH'),
  564. ['id' => 'send_to_hrm_users']
  565. );
  566. }
  567. $form->addCheckBox('send_me_a_copy_by_email', null, get_lang('SendAnnouncementCopyToMyself'));
  568. $defaults['send_me_a_copy_by_email'] = true;
  569. if ($showSubmitButton) {
  570. $form->addLabel('',
  571. Display::url(
  572. get_lang('Preview'),
  573. 'javascript:void(0)',
  574. ['class' => 'btn btn-default', 'id' => 'announcement_preview']
  575. ).'<div id="announcement_preview_result" style="display:none"></div>'
  576. );
  577. $form->addHtml('<div id="send_button" style="display:none">');
  578. $form->addButtonSave(get_lang('ButtonPublishAnnouncement'));
  579. $form->addHtml('</div>');
  580. }
  581. $form->setDefaults($defaults);
  582. if ($form->validate()) {
  583. $data = $form->getSubmitValues();
  584. $data['users'] = isset($data['users']) ? $data['users'] : [];
  585. $sendToUsersInSession = isset($data['send_to_users_in_session']) ? true : false;
  586. $sendMeCopy = isset($data['send_me_a_copy_by_email']) ? true : false;
  587. if (isset($id) && $id) {
  588. // there is an Id => the announcement already exists => update mode
  589. if (Security::check_token('post')) {
  590. $file_comment = $_POST['file_comment'];
  591. $file = $_FILES['user_upload'];
  592. AnnouncementManager::edit_announcement(
  593. $id,
  594. $data['title'],
  595. $data['content'],
  596. $data['users'],
  597. $file,
  598. $file_comment,
  599. $sendToUsersInSession
  600. );
  601. // Send mail
  602. $messageSentTo = [];
  603. if (isset($_POST['email_ann']) && empty($_POST['onlyThoseMails'])) {
  604. $messageSentTo = AnnouncementManager::sendEmail(
  605. api_get_course_info(),
  606. api_get_session_id(),
  607. $id,
  608. $sendToUsersInSession,
  609. isset($data['send_to_hrm_users'])
  610. );
  611. }
  612. if ($sendMeCopy && !in_array(api_get_user_id(), $messageSentTo)) {
  613. $email = new AnnouncementEmail(api_get_course_info(), api_get_session_id(), $id);
  614. $email->sendAnnouncementEmailToMySelf();
  615. }
  616. Display::addFlash(
  617. Display::return_message(
  618. get_lang('AnnouncementModified'),
  619. 'success'
  620. )
  621. );
  622. Security::clear_token();
  623. header('Location: '.$homeUrl);
  624. exit;
  625. }
  626. } else {
  627. // Insert mode
  628. if (Security::check_token('post')) {
  629. $file = $_FILES['user_upload'];
  630. $file_comment = $data['file_comment'];
  631. if (empty($group_id)) {
  632. $insert_id = AnnouncementManager::add_announcement(
  633. api_get_course_info(),
  634. api_get_session_id(),
  635. $data['title'],
  636. $data['content'],
  637. $data['users'],
  638. $file,
  639. $file_comment,
  640. null,
  641. $sendToUsersInSession
  642. );
  643. } else {
  644. $insert_id = AnnouncementManager::addGroupAnnouncement(
  645. $data['title'],
  646. $data['content'],
  647. $group_id,
  648. $data['users'],
  649. $file,
  650. $file_comment,
  651. $sendToUsersInSession
  652. );
  653. }
  654. if ($insert_id) {
  655. Display::addFlash(
  656. Display::return_message(
  657. get_lang('AnnouncementAdded'),
  658. 'success'
  659. )
  660. );
  661. // Send mail
  662. $messageSentTo = [];
  663. if (isset($data['email_ann']) && $data['email_ann']) {
  664. $messageSentTo = AnnouncementManager::sendEmail(
  665. api_get_course_info(),
  666. api_get_session_id(),
  667. $insert_id,
  668. $sendToUsersInSession
  669. );
  670. }
  671. if ($sendMeCopy && !in_array(api_get_user_id(), $messageSentTo)) {
  672. $email = new AnnouncementEmail(api_get_course_info(), api_get_session_id(), $insert_id);
  673. $email->sendAnnouncementEmailToMySelf();
  674. }
  675. Security::clear_token();
  676. header('Location: '.$homeUrl);
  677. exit;
  678. }
  679. api_not_allowed(true);
  680. } // end condition token
  681. }
  682. }
  683. $content = $form->returnForm();
  684. break;
  685. }
  686. if (!empty($_GET['remind_inactive'])) {
  687. $to[] = 'USER:'.intval($_GET['remind_inactive']);
  688. }
  689. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  690. // We are not in the learning path
  691. Display::display_header($nameTools, get_lang('Announcements'));
  692. }
  693. // Tool introduction
  694. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  695. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  696. }
  697. // Actions
  698. $show_actions = false;
  699. $actionsLeft = '';
  700. if (($allowToEdit || $allowStudentInGroupToSend) && (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath')) {
  701. if (in_array($action, ['add', 'modify', 'view'])) {
  702. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."'>".
  703. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).
  704. "</a>";
  705. } else {
  706. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."&action=add'>".
  707. Display::return_icon('new_announce.png', get_lang('AddAnnouncement'), '', ICON_SIZE_MEDIUM).
  708. "</a>";
  709. }
  710. $show_actions = true;
  711. } else {
  712. if (in_array($action, ['view'])) {
  713. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."'>".
  714. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM)."</a>";
  715. }
  716. }
  717. if ($allowToEdit && api_get_group_id() == 0) {
  718. $allow = api_get_configuration_value('disable_delete_all_announcements');
  719. if ($allow === false && api_is_allowed_to_edit()) {
  720. if (!isset($_GET['action']) ||
  721. isset($_GET['action']) && $_GET['action'] == 'list'
  722. ) {
  723. $actionsLeft .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete_all\" onclick=\"javascript:if(!confirm('".get_lang("ConfirmYourChoice")."')) return false;\">".
  724. Display::return_icon(
  725. 'delete_announce.png',
  726. get_lang('AnnouncementDeleteAll'),
  727. '',
  728. ICON_SIZE_MEDIUM
  729. )."</a>";
  730. }
  731. }
  732. }
  733. if ($show_actions) {
  734. echo Display::toolbarAction('toolbar', [$actionsLeft, $searchFormToString]);
  735. }
  736. echo $content;
  737. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  738. //we are not in learnpath tool
  739. Display::display_footer();
  740. }