announcements.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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('ToolAnnouncement');
  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('GroupSpace').' '.$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('AnnouncementMoved')));
  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('LastUpdateDate'),
  177. get_lang('Actions'),
  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('AddAnnouncement'),
  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('NoAnnouncements'), '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('AnnouncementDeleted')));
  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('AnnouncementDeletedAll')));
  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('VisibilityChanged')));
  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('ModifyAnnouncement');
  359. if (empty($id)) {
  360. $form_name = get_lang('AddAnnouncement');
  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('ChooseRecipients')]
  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('RemindInactiveLearnersMailContent'),
  379. api_get_setting('siteName'),
  380. 7
  381. );
  382. $title_to_modify = sprintf(
  383. get_lang('RemindInactiveLearnersMailSubject'),
  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 = isset($_GET['since']) ? (int) $_GET['since'] : 6;
  390. // Getting the users who have to be reminded
  391. $to = Tracking::getInactiveStudentsInCourse(
  392. api_get_course_int_id(),
  393. $since,
  394. $sessionId
  395. );
  396. // setting the variables for the form elements: the users who need to receive the message
  397. foreach ($to as &$user) {
  398. $user = 'USER:'.$user;
  399. }
  400. // setting the variables for the form elements: the message has to be sent by email
  401. $email_ann = '1';
  402. // setting the variables for the form elements: the title of the email
  403. $title_to_modify = sprintf(
  404. get_lang('RemindInactiveLearnersMailSubject'),
  405. api_get_setting('siteName')
  406. );
  407. // setting the variables for the form elements: the message of the email
  408. $content_to_modify = sprintf(
  409. get_lang('RemindInactiveLearnersMailContent'),
  410. api_get_setting('siteName'),
  411. $since
  412. );
  413. // when we want to remind the users who have never been active
  414. // then we have a different subject and content for the announcement
  415. if ($_GET['since'] === 'never') {
  416. $title_to_modify = sprintf(
  417. get_lang('RemindInactiveLearnersMailSubject'),
  418. api_get_setting('siteName')
  419. );
  420. $content_to_modify = get_lang(
  421. 'YourAccountIsActiveYouCanLoginAndCheckYourCourses'
  422. );
  423. }
  424. }
  425. $element = CourseManager::addUserGroupMultiSelect($form, []);
  426. } else {
  427. $element = CourseManager::addGroupMultiSelect($form, $groupProperties, []);
  428. }
  429. $form->addHtml('</div>');
  430. $form->addCheckBox('email_ann', '', get_lang('EmailOption'));
  431. if (!isset($announcement_to_modify)) {
  432. $announcement_to_modify = '';
  433. }
  434. $announcementInfo = [];
  435. if (!empty($id)) {
  436. $announcementInfo = AnnouncementManager::get_by_id($courseId, $id);
  437. }
  438. $showSubmitButton = true;
  439. if (!empty($announcementInfo)) {
  440. $to = AnnouncementManager::loadEditUsers('announcement', $id);
  441. if (!empty($group_id)) {
  442. $separated = CourseManager::separateUsersGroups($to);
  443. if (isset($separated['groups']) && count($separated['groups']) > 1) {
  444. $form->freeze();
  445. Display::addFlash(Display::return_message(get_lang('LockByTeacher')));
  446. $showSubmitButton = false;
  447. }
  448. }
  449. $defaults = [
  450. 'title' => $announcementInfo['title'],
  451. 'content' => $announcementInfo['content'],
  452. 'id' => $announcementInfo['id'],
  453. 'users' => $to,
  454. ];
  455. } else {
  456. $defaults = [];
  457. if (!empty($to)) {
  458. $defaults['users'] = $to;
  459. }
  460. }
  461. $ajaxUrl = api_get_path(WEB_AJAX_PATH).'announcement.ajax.php?'.api_get_cidreq().'&a=preview';
  462. $form->addHtml("
  463. <script>
  464. $(function () {
  465. $('#announcement_preview').on('click', function() {
  466. var users = [];
  467. $('#users_to option').each(function() {
  468. users.push($(this).val());
  469. });
  470. var form = $('#announcement').serialize();
  471. $.ajax({
  472. type: 'POST',
  473. dataType: 'json',
  474. url: '".$ajaxUrl."',
  475. data: {users : JSON.stringify(users), form: form},
  476. beforeSend: function() {
  477. $('#announcement_preview_result').html('<i class=\"fa fa-spinner\"></i>');
  478. $('#send_button').hide();
  479. },
  480. success: function(result) {
  481. var resultToString = '';
  482. $.each(result, function(index, value) {
  483. resultToString += '&nbsp;' + value;
  484. });
  485. $('#announcement_preview_result').html('' +
  486. '".addslashes(get_lang('AnnouncementWillBeSentTo'))."<br/>' + resultToString
  487. );
  488. $('#announcement_preview_result').show();
  489. $('#send_button').show();
  490. }
  491. });
  492. });
  493. });
  494. </script>
  495. ");
  496. if (isset($defaults['users'])) {
  497. foreach ($defaults['users'] as $value) {
  498. $parts = explode(':', $value);
  499. if (!isset($parts[1]) || empty($parts[1])) {
  500. continue;
  501. }
  502. $form->addHtml(
  503. "
  504. <script>
  505. $(function () {
  506. $('#choose_recipients').click();
  507. });
  508. </script>
  509. ");
  510. break;
  511. }
  512. }
  513. $defaults['email_ann'] = true;
  514. $form->addElement(
  515. 'text',
  516. 'title',
  517. get_lang('EmailTitle'),
  518. ['onkeypress' => 'return event.keyCode != 13;']
  519. );
  520. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  521. $form->addElement('hidden', 'id');
  522. $htmlTags = '';
  523. $tags = AnnouncementManager::getTags();
  524. foreach ($tags as $tag) {
  525. $htmlTags .= "<b>$tag</b><br />";
  526. }
  527. $form->addButtonAdvancedSettings('tags', get_lang('Tags'));
  528. $form->addElement('html', '<div id="tags_options" style="display:none">');
  529. $form->addLabel('', Display::return_message($htmlTags, 'normal', false));
  530. $form->addElement('html', '</div>');
  531. $form->addHtmlEditor(
  532. 'content',
  533. get_lang('Description'),
  534. true,
  535. false,
  536. ['ToolbarSet' => 'Announcements']
  537. );
  538. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  539. $form->addElement('textarea', 'file_comment', get_lang('FileComment'));
  540. $form->addHidden('sec_token', $token);
  541. if (empty($sessionId)) {
  542. $form->addCheckBox('send_to_users_in_session', null, get_lang('SendToUsersInSessions'));
  543. }
  544. $config = api_get_configuration_value('announcements_hide_send_to_hrm_users');
  545. if ($config === false) {
  546. $form->addCheckBox(
  547. 'send_to_hrm_users',
  548. null,
  549. get_lang('SendAnnouncementCopyToDRH'),
  550. ['id' => 'send_to_hrm_users']
  551. );
  552. }
  553. $form->addCheckBox('send_me_a_copy_by_email', null, get_lang('SendAnnouncementCopyToMyself'));
  554. $defaults['send_me_a_copy_by_email'] = true;
  555. if ($showSubmitButton) {
  556. $form->addLabel('',
  557. Display::url(
  558. get_lang('Preview'),
  559. 'javascript:void(0)',
  560. ['class' => 'btn btn-default', 'id' => 'announcement_preview']
  561. ).'<div id="announcement_preview_result" style="display:none"></div>'
  562. );
  563. $form->addHtml('<div id="send_button" style="display:none">');
  564. $form->addButtonSave(get_lang('ButtonPublishAnnouncement'));
  565. $form->addHtml('</div>');
  566. }
  567. $form->setDefaults($defaults);
  568. if ($form->validate()) {
  569. $data = $form->getSubmitValues();
  570. $data['users'] = isset($data['users']) ? $data['users'] : [];
  571. $sendToUsersInSession = isset($data['send_to_users_in_session']) ? true : false;
  572. $sendMeCopy = isset($data['send_me_a_copy_by_email']) ? true : false;
  573. if (isset($id) && $id) {
  574. // there is an Id => the announcement already exists => update mode
  575. if (Security::check_token('post')) {
  576. $file_comment = $_POST['file_comment'];
  577. $file = $_FILES['user_upload'];
  578. AnnouncementManager::edit_announcement(
  579. $id,
  580. $data['title'],
  581. $data['content'],
  582. $data['users'],
  583. $file,
  584. $file_comment,
  585. $sendToUsersInSession
  586. );
  587. // Send mail
  588. $messageSentTo = [];
  589. if (isset($_POST['email_ann']) && empty($_POST['onlyThoseMails'])) {
  590. $messageSentTo = AnnouncementManager::sendEmail(
  591. api_get_course_info(),
  592. api_get_session_id(),
  593. $id,
  594. $sendToUsersInSession,
  595. isset($data['send_to_hrm_users'])
  596. );
  597. }
  598. if ($sendMeCopy && !in_array(api_get_user_id(), $messageSentTo)) {
  599. $email = new AnnouncementEmail(api_get_course_info(), api_get_session_id(), $id);
  600. $email->sendAnnouncementEmailToMySelf();
  601. }
  602. Display::addFlash(
  603. Display::return_message(
  604. get_lang('AnnouncementModified'),
  605. 'success'
  606. )
  607. );
  608. Security::clear_token();
  609. header('Location: '.$homeUrl);
  610. exit;
  611. }
  612. } else {
  613. // Insert mode
  614. if (Security::check_token('post')) {
  615. $file = $_FILES['user_upload'];
  616. $file_comment = $data['file_comment'];
  617. if (empty($group_id)) {
  618. $insert_id = AnnouncementManager::add_announcement(
  619. api_get_course_info(),
  620. api_get_session_id(),
  621. $data['title'],
  622. $data['content'],
  623. $data['users'],
  624. $file,
  625. $file_comment,
  626. null,
  627. $sendToUsersInSession
  628. );
  629. } else {
  630. $insert_id = AnnouncementManager::addGroupAnnouncement(
  631. $data['title'],
  632. $data['content'],
  633. $group_id,
  634. $data['users'],
  635. $file,
  636. $file_comment,
  637. $sendToUsersInSession
  638. );
  639. }
  640. if ($insert_id) {
  641. Display::addFlash(
  642. Display::return_message(
  643. get_lang('AnnouncementAdded'),
  644. 'success'
  645. )
  646. );
  647. // Send mail
  648. $messageSentTo = [];
  649. if (isset($data['email_ann']) && $data['email_ann']) {
  650. $messageSentTo = AnnouncementManager::sendEmail(
  651. api_get_course_info(),
  652. api_get_session_id(),
  653. $insert_id,
  654. $sendToUsersInSession
  655. );
  656. }
  657. if ($sendMeCopy && !in_array(api_get_user_id(), $messageSentTo)) {
  658. $email = new AnnouncementEmail(api_get_course_info(), api_get_session_id(), $insert_id);
  659. $email->sendAnnouncementEmailToMySelf();
  660. }
  661. Security::clear_token();
  662. header('Location: '.$homeUrl);
  663. exit;
  664. }
  665. api_not_allowed(true);
  666. } // end condition token
  667. }
  668. }
  669. $content = $form->returnForm();
  670. break;
  671. }
  672. if (!empty($_GET['remind_inactive'])) {
  673. $to[] = 'USER:'.intval($_GET['remind_inactive']);
  674. }
  675. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  676. // We are not in the learning path
  677. Display::display_header($nameTools, get_lang('Announcements'));
  678. }
  679. // Tool introduction
  680. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  681. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  682. }
  683. // Actions
  684. $show_actions = false;
  685. $actionsLeft = '';
  686. if (($allowToEdit || $allowStudentInGroupToSend) && (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath')) {
  687. if (in_array($action, ['add', 'modify', 'view'])) {
  688. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."'>".
  689. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).
  690. "</a>";
  691. } else {
  692. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."&action=add'>".
  693. Display::return_icon('new_announce.png', get_lang('AddAnnouncement'), '', ICON_SIZE_MEDIUM).
  694. "</a>";
  695. }
  696. $show_actions = true;
  697. } else {
  698. if (in_array($action, ['view'])) {
  699. $actionsLeft .= "<a href='".api_get_self()."?".api_get_cidreq()."'>".
  700. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM)."</a>";
  701. }
  702. }
  703. if ($allowToEdit && api_get_group_id() == 0) {
  704. $allow = api_get_configuration_value('disable_delete_all_announcements');
  705. if ($allow === false && api_is_allowed_to_edit()) {
  706. if (!isset($_GET['action']) ||
  707. isset($_GET['action']) && $_GET['action'] == 'list'
  708. ) {
  709. $actionsLeft .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete_all\" onclick=\"javascript:if(!confirm('".get_lang("ConfirmYourChoice")."')) return false;\">".
  710. Display::return_icon(
  711. 'delete_announce.png',
  712. get_lang('AnnouncementDeleteAll'),
  713. '',
  714. ICON_SIZE_MEDIUM
  715. )."</a>";
  716. }
  717. }
  718. }
  719. if ($show_actions) {
  720. echo Display::toolbarAction('toolbar', [$actionsLeft, $searchFormToString]);
  721. }
  722. echo $content;
  723. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  724. //we are not in learnpath tool
  725. Display::display_footer();
  726. }