announcements.php 19 KB

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