announcements.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. * @package chamilo.announcements
  12. * multiple functions
  13. */
  14. // use anonymous mode when accessing this course tool
  15. $use_anonymous = true;
  16. // setting the global file that gets the general configuration, the databases, the languages, ...
  17. require_once '../inc/global.inc.php';
  18. /* Sessions */
  19. $ctok = Security::get_existing_token();
  20. $stok = Security::get_token();
  21. $current_course_tool = TOOL_ANNOUNCEMENT;
  22. $this_section = SECTION_COURSES;
  23. $nameTools = get_lang('ToolAnnouncement');
  24. /* ACCESS RIGHTS */
  25. api_protect_course_script(true);
  26. // Configuration settings
  27. $display_announcement_list = true;
  28. $display_form = false;
  29. $display_title_list = true;
  30. // Maximum title messages to display
  31. $maximum = '12';
  32. // Length of the titles
  33. $length = '36';
  34. // Database Table Definitions
  35. $tbl_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  36. $tbl_sessions = Database::get_main_table(TABLE_MAIN_SESSION);
  37. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  38. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  39. $course_id = api_get_course_int_id();
  40. $_course = api_get_course_info_by_id($course_id);
  41. $group_id = api_get_group_id();
  42. api_protect_course_group(GroupManager::GROUP_TOOL_ANNOUNCEMENT);
  43. /* Tracking */
  44. Event::event_access_tool(TOOL_ANNOUNCEMENT);
  45. $announcement_id = isset($_GET['id']) ? intval($_GET['id']) : null;
  46. $origin = isset($_GET['origin']) ? Security::remove_XSS($_GET['origin']) : null;
  47. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : 'list';
  48. $announcement_number = AnnouncementManager::getNumberAnnouncements();
  49. $homeUrl = api_get_self().'?action=list&'.api_get_cidreq();
  50. $content = null;
  51. switch ($action) {
  52. case 'move':
  53. /* Move announcement up/down */
  54. if (isset($_GET['sec_token']) && $ctok == $_GET['sec_token']) {
  55. if (!empty($_GET['down'])) {
  56. $thisAnnouncementId = intval($_GET['down']);
  57. $sortDirection = "DESC";
  58. }
  59. if (!empty($_GET['up'])) {
  60. $thisAnnouncementId = intval($_GET['up']);
  61. $sortDirection = "ASC";
  62. }
  63. }
  64. if (!empty($sortDirection)) {
  65. if (!in_array(trim(strtoupper($sortDirection)), array('ASC', 'DESC'))) {
  66. $sortDirection='ASC';
  67. }
  68. $announcementInfo = AnnouncementManager::get_by_id($course_id, $thisAnnouncementId);
  69. $sql = "SELECT DISTINCT announcement.id, announcement.display_order
  70. FROM $tbl_announcement announcement,
  71. $tbl_item_property itemproperty
  72. WHERE
  73. announcement.c_id = $course_id AND
  74. itemproperty.c_id = $course_id AND
  75. itemproperty.ref = announcement.id AND
  76. itemproperty.tool = '".TOOL_ANNOUNCEMENT."' AND
  77. itemproperty.visibility <> 2
  78. ORDER BY display_order $sortDirection";
  79. $result = Database::query($sql);
  80. $thisAnnouncementOrderFound = false;
  81. $thisAnnouncementOrder = null;
  82. while (list($announcementId, $announcementOrder) = Database::fetch_row($result)) {
  83. if ($thisAnnouncementOrderFound) {
  84. $nextAnnouncementId = $announcementId;
  85. $nextAnnouncementOrder = $announcementOrder;
  86. $sql = "UPDATE $tbl_announcement SET display_order = '$nextAnnouncementOrder'
  87. WHERE c_id = $course_id AND id = $thisAnnouncementId";
  88. Database::query($sql);
  89. $sql = "UPDATE $tbl_announcement SET display_order = '$thisAnnouncementOrder'
  90. WHERE c_id = $course_id AND id = $nextAnnouncementId";
  91. Database::query($sql);
  92. break;
  93. }
  94. // STEP 1 : FIND THE ORDER OF THE ANNOUNCEMENT
  95. if ($announcementId == $thisAnnouncementId) {
  96. $thisAnnouncementOrder = $announcementOrder;
  97. $thisAnnouncementOrderFound = true;
  98. }
  99. }
  100. Display::addFlash(Display::return_message(get_lang('AnnouncementMoved')));
  101. header('Location: '.$homeUrl);
  102. exit;
  103. }
  104. break;
  105. case 'view':
  106. $content = AnnouncementManager::display_announcement($announcement_id);
  107. break;
  108. case 'list':
  109. $content = AnnouncementManager::getAnnouncements($stok, $announcement_number);
  110. break;
  111. case 'delete':
  112. /* Delete announcement */
  113. $id = intval($_GET['id']);
  114. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false, true) == false) {
  115. api_not_allowed();
  116. }
  117. if (!api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  118. // tooledit : visibility = 2 : only visible for platform administrator
  119. if ($ctok == $_GET['sec_token']) {
  120. AnnouncementManager::delete_announcement($_course, $id);
  121. Display::addFlash(Display::return_message(get_lang('AnnouncementDeleted')));
  122. }
  123. }
  124. header('Location: '.$homeUrl);
  125. exit;
  126. break;
  127. case 'delete_all':
  128. if (api_is_allowed_to_edit()) {
  129. AnnouncementManager::delete_all_announcements($_course);
  130. Display::addFlash(Display::return_message(get_lang('AnnouncementDeletedAll')));
  131. header('Location: '.$homeUrl);
  132. exit;
  133. }
  134. break;
  135. case 'delete_attachment':
  136. $id = $_GET['id_attach'];
  137. if ($ctok == $_GET['sec_token']) {
  138. if (api_is_allowed_to_edit()) {
  139. AnnouncementManager::delete_announcement_attachment_file($id);
  140. }
  141. }
  142. header('Location: '.$homeUrl);
  143. exit;
  144. break;
  145. case 'showhide':
  146. if (!isset($_GET['isStudentView']) || $_GET['isStudentView'] != 'false') {
  147. if (isset($_GET['id']) AND $_GET['id']) {
  148. if (api_get_session_id() != 0 &&
  149. api_is_allowed_to_session_edit(false, true) == false) {
  150. api_not_allowed();
  151. }
  152. if (!api_is_course_coach() ||
  153. api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $_GET['id'])
  154. ) {
  155. if ($ctok == $_GET['sec_token']) {
  156. AnnouncementManager::change_visibility_announcement(
  157. $_course,
  158. $_GET['id']
  159. );
  160. Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
  161. header('Location: '.$homeUrl);
  162. exit;
  163. }
  164. }
  165. }
  166. }
  167. break;
  168. case 'add':
  169. case 'modify':
  170. if (api_get_session_id() != 0 &&
  171. api_is_allowed_to_session_edit(false, true) == false
  172. ) {
  173. api_not_allowed(true);
  174. }
  175. // DISPLAY ADD ANNOUNCEMENT COMMAND
  176. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  177. $url = api_get_self().'?action='.$action.'&id=' . $id . '&' . api_get_cidreq();
  178. $form = new FormValidator(
  179. 'f1',
  180. 'post',
  181. $url,
  182. null,
  183. array('enctype' => 'multipart/form-data')
  184. );
  185. if (empty($id)) {
  186. $form_name = get_lang('AddAnnouncement');
  187. } else {
  188. $form_name = get_lang('ModifyAnnouncement');
  189. }
  190. $form->addElement('header', $form_name);
  191. if (empty($group_id)) {
  192. if (isset($_GET['remind_inactive'])) {
  193. $email_ann = '1';
  194. $content_to_modify = sprintf(
  195. get_lang('RemindInactiveLearnersMailContent'),
  196. api_get_setting('siteName'),
  197. 7
  198. );
  199. $title_to_modify = sprintf(
  200. get_lang('RemindInactiveLearnersMailSubject'),
  201. api_get_setting('siteName')
  202. );
  203. } elseif (isset($_GET['remindallinactives']) && $_GET['remindallinactives'] == 'true') {
  204. // we want to remind inactive users. The $_GET['since'] parameter
  205. // determines which users have to be warned (i.e the users who have been inactive for x days or more
  206. $since = isset($_GET['since']) ? intval($_GET['since']) : 6;
  207. // getting the users who have to be reminded
  208. $to = Tracking:: getInactiveStudentsInCourse(
  209. api_get_course_int_id(),
  210. $since,
  211. api_get_session_id()
  212. );
  213. // setting the variables for the form elements: the users who need to receive the message
  214. foreach ($to as &$user) {
  215. $user = 'USER:' . $user;
  216. }
  217. // setting the variables for the form elements: the message has to be sent by email
  218. $email_ann = '1';
  219. // setting the variables for the form elements: the title of the email
  220. $title_to_modify = sprintf(
  221. get_lang('RemindInactiveLearnersMailSubject'),
  222. api_get_setting('siteName')
  223. );
  224. // setting the variables for the form elements: the message of the email
  225. $content_to_modify = sprintf(
  226. get_lang('RemindInactiveLearnersMailContent'),
  227. api_get_setting('siteName'),
  228. $since
  229. );
  230. // when we want to remind the users who have never been active
  231. // then we have a different subject and content for the announcement
  232. if ($_GET['since'] == 'never') {
  233. $title_to_modify = sprintf(
  234. get_lang('RemindInactiveLearnersMailSubject'),
  235. api_get_setting('siteName')
  236. );
  237. $content_to_modify = get_lang(
  238. 'YourAccountIsActiveYouCanLoginAndCheckYourCourses'
  239. );
  240. }
  241. }
  242. $element = CourseManager::addUserGroupMultiSelect($form, array());
  243. $form->setRequired($element);
  244. if (!isset($announcement_to_modify)) {
  245. $announcement_to_modify = '';
  246. }
  247. $form->addElement(
  248. 'checkbox',
  249. 'email_ann',
  250. null,
  251. get_lang('EmailOption')
  252. );
  253. } else {
  254. if (!isset($announcement_to_modify)) {
  255. $announcement_to_modify = "";
  256. }
  257. $element = CourseManager::addGroupMultiSelect($form, $group_id, array());
  258. $form->setRequired($element);
  259. $form->addElement(
  260. 'checkbox',
  261. 'email_ann',
  262. null,
  263. get_lang('EmailOption')
  264. );
  265. }
  266. $announcementInfo = AnnouncementManager::get_by_id($course_id, $id);
  267. if (isset($announcementInfo) && !empty($announcementInfo)) {
  268. $to = AnnouncementManager::load_edit_users("announcement", $id);
  269. $defaults = array(
  270. 'title' => $announcementInfo['title'],
  271. 'content' => $announcementInfo['content'],
  272. 'id' => $announcementInfo['id'],
  273. 'users' => $to
  274. );
  275. } else {
  276. $defaults = array();
  277. }
  278. $form->addElement('text', 'title', get_lang('EmailTitle'));
  279. $form->addElement('hidden', 'id');
  280. $form->addHtmlEditor(
  281. 'content',
  282. get_lang('Description'),
  283. false,
  284. false,
  285. array('ToolbarSet' => 'Announcements')
  286. );
  287. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  288. $form->addElement('textarea', 'file_comment', get_lang('FileComment'));
  289. $form->addElement('hidden', 'sec_token', $stok);
  290. if (api_get_session_id() == 0) {
  291. $form->addCheckBox('send_to_users_in_session', null, get_lang('SendToUsersInSessions'));
  292. }
  293. $form->addCheckBox('send_to_hrm_users', null, get_lang('SendAnnouncementCopyToDRH'));
  294. $form->addButtonSave(get_lang('ButtonPublishAnnouncement'));
  295. $form->setDefaults($defaults);
  296. if ($form->validate()) {
  297. $data = $form->getSubmitValues();
  298. $sendToUsersInSession = isset($data['send_to_users_in_session']) ? true : false;
  299. if (isset($id) && $id) {
  300. // there is an Id => the announcement already exists => update mode
  301. if ($ctok == $_POST['sec_token']) {
  302. $file_comment = $_POST['file_comment'];
  303. $file = $_FILES['user_upload'];
  304. AnnouncementManager::edit_announcement(
  305. $id,
  306. $data['title'],
  307. $data['content'],
  308. $data['users'],
  309. $file,
  310. $file_comment,
  311. $sendToUsersInSession
  312. );
  313. /* MAIL FUNCTION */
  314. if (isset($_POST['email_ann']) && empty($_POST['onlyThoseMails'])) {
  315. AnnouncementManager::send_email(
  316. $id,
  317. $sendToUsersInSession,
  318. isset($data['send_to_hrm_users'])
  319. );
  320. }
  321. Display::addFlash(
  322. Display::return_message(
  323. get_lang('AnnouncementModified'),
  324. 'success'
  325. )
  326. );
  327. header('Location: '.$homeUrl);
  328. exit;
  329. }
  330. } else {
  331. // Insert mode
  332. if ($ctok == $_POST['sec_token']) {
  333. $file = $_FILES['user_upload'];
  334. $file_comment = $data['file_comment'];
  335. if (empty($group_id)) {
  336. $insert_id = AnnouncementManager::add_announcement(
  337. $data['title'],
  338. $data['content'],
  339. $data['users'],
  340. $file,
  341. $file_comment,
  342. $sendToUsersInSession
  343. );
  344. } else {
  345. $insert_id = AnnouncementManager::add_group_announcement(
  346. $data['title'],
  347. $data['content'],
  348. array('GROUP:' . $group_id),
  349. $data['users'],
  350. $file,
  351. $file_comment,
  352. $sendToUsersInSession
  353. );
  354. }
  355. Display::addFlash(
  356. Display::return_message(
  357. get_lang('AnnouncementAdded'),
  358. 'success'
  359. )
  360. );
  361. /* MAIL FUNCTION */
  362. if (isset($data['email_ann']) && $data['email_ann']) {
  363. AnnouncementManager::send_email(
  364. $insert_id,
  365. $sendToUsersInSession
  366. );
  367. }
  368. header('Location: '.$homeUrl);
  369. exit;
  370. } // end condition token
  371. }
  372. }
  373. $content = $form->returnForm();
  374. break;
  375. }
  376. if (!empty($_GET['remind_inactive'])) {
  377. $to[] = 'USER:'.intval($_GET['remind_inactive']);
  378. }
  379. if (!empty($group_id)) {
  380. $group_properties = GroupManager :: get_group_properties($group_id);
  381. $interbreadcrumb[] = array("url" => "../group/group.php?".api_get_cidreq(), "name" => get_lang('Groups'));
  382. $interbreadcrumb[] = array("url"=>"../group/group_space.php?".api_get_cidreq(), "name"=> get_lang('GroupSpace').' '.$group_properties['name']);
  383. }
  384. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  385. //we are not in the learning path
  386. Display::display_header($nameTools,get_lang('Announcements'));
  387. }
  388. // Tool introduction
  389. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  390. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  391. }
  392. // Actions
  393. $show_actions = false;
  394. if ((api_is_allowed_to_edit(false,true) ||
  395. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) &&
  396. (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath')
  397. ) {
  398. echo '<div class="actions">';
  399. if (in_array($action, array('add', 'modify','view'))) {
  400. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".$origin."'>".
  401. Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM)."</a>";
  402. } else {
  403. echo "<a href='".api_get_self()."?".api_get_cidreq()."&action=add&origin=".$origin."'>".
  404. Display::return_icon('new_announce.png',get_lang('AddAnnouncement'),'',ICON_SIZE_MEDIUM)."</a>";
  405. }
  406. $show_actions = true;
  407. } else {
  408. if (in_array($action, array('view'))) {
  409. echo '<div class="actions">';
  410. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".$origin."'>".
  411. Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM)."</a>";
  412. echo '</div>';
  413. }
  414. }
  415. if (api_is_allowed_to_edit() && $announcement_number > 1) {
  416. if (api_get_group_id() == 0 ) {
  417. if (!$show_actions)
  418. echo '<div class="actions">';
  419. if (!isset($_GET['action'])) {
  420. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete_all\" onclick=\"javascript:if(!confirm('".get_lang("ConfirmYourChoice")."')) return false;\">".
  421. Display::return_icon('delete_announce.png',get_lang('AnnouncementDeleteAll'),'',ICON_SIZE_MEDIUM)."</a>";
  422. }
  423. }
  424. }
  425. if ($show_actions)
  426. echo '</div>';
  427. Display::showFlash();
  428. echo $content;
  429. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  430. //we are not in learnpath tool
  431. Display::display_footer();
  432. }