announcements.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 \ChamiloSession as Session;
  16. $language_file = array('announcements', 'group', 'survey', 'document');
  17. // use anonymous mode when accessing this course tool
  18. $use_anonymous = true;
  19. // setting the global file that gets the general configuration, the databases, the languages, ...
  20. require_once '../inc/global.inc.php';
  21. /* Sessions */
  22. $ctok = $_SESSION['sec_token'];
  23. $stok = Security::get_token();
  24. $current_course_tool = TOOL_ANNOUNCEMENT;
  25. $this_section = SECTION_COURSES;
  26. $nameTools = get_lang('ToolAnnouncement');
  27. /* ACCESS RIGHTS */
  28. api_protect_course_script(true);
  29. // Configuration settings
  30. $display_announcement_list = true;
  31. $display_form = false;
  32. $display_title_list = true;
  33. // Maximum title messages to display
  34. $maximum = '12';
  35. // Length of the titles
  36. $length = '36';
  37. // Database Table Definitions
  38. $tbl_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  39. $tbl_sessions = Database::get_main_table(TABLE_MAIN_SESSION);
  40. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  41. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  42. $course_id = api_get_course_int_id();
  43. $_course = api_get_course_info();
  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:: get_inactives_students_in_course(
  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. CourseManager::addUserGroupMultiSelect($form, array());
  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. CourseManager::addGroupMultiSelect($form, $group_id, array());
  258. $form->addElement(
  259. 'checkbox',
  260. 'email_ann',
  261. null,
  262. get_lang('EmailOption')
  263. );
  264. }
  265. $announcementInfo = AnnouncementManager::get_by_id($course_id, $id);
  266. if (isset($announcementInfo) && !empty($announcementInfo)) {
  267. $to = AnnouncementManager::load_edit_users("announcement", $id);
  268. $defaults = array(
  269. 'title' => $announcementInfo['title'],
  270. 'content' => $announcementInfo['content'],
  271. 'id' => $announcementInfo['id'],
  272. 'users' => $to
  273. );
  274. } else {
  275. $defaults = array();
  276. }
  277. $form->addElement('text', 'title', get_lang('EmailTitle'));
  278. $form->addElement('hidden', 'id');
  279. $form->addHtmlEditor(
  280. 'content',
  281. get_lang('Description'),
  282. false,
  283. false,
  284. array('ToolbarSet' => 'Announcements')
  285. );
  286. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  287. $form->addElement('textarea', 'file_comment', get_lang('FileComment'));
  288. $form->addElement('hidden', 'sec_token', $stok);
  289. if (api_get_session_id() == 0) {
  290. $form->addCheckBox('send_to_users_in_session', null, get_lang('SendToUsersInSessions'));
  291. }
  292. $form->addButton('save', get_lang('ButtonPublishAnnouncement'));
  293. $form->setDefaults($defaults);
  294. $content = $form->return_form();
  295. if ($form->validate()) {
  296. $data = $form->getSubmitValues();
  297. $sendToUsersInSession = isset($data['send_to_users_in_session']) ? true : false;
  298. if (isset($id) && $id) {
  299. // there is an Id => the announcement already exists => update mode
  300. if ($ctok == $_POST['sec_token']) {
  301. $file_comment = $_POST['file_comment'];
  302. $file = $_FILES['user_upload'];
  303. AnnouncementManager::edit_announcement(
  304. $id,
  305. $data['title'],
  306. $data['content'],
  307. $_POST['selectedform'],
  308. $file,
  309. $file_comment,
  310. $sendToUsersInSession
  311. );
  312. /* MAIL FUNCTION */
  313. if ($_POST['email_ann'] && empty($_POST['onlyThoseMails'])) {
  314. AnnouncementManager::send_email($id, $sendToUsersInSession);
  315. }
  316. Display::addFlash(Display::return_message(get_lang('AnnouncementModified'), 'success'));
  317. header('Location: '.$homeUrl);
  318. exit;
  319. }
  320. } else {
  321. // Insert mode
  322. if ($ctok == $_POST['sec_token']) {
  323. $file = $_FILES['user_upload'];
  324. $file_comment = $data['file_comment'];
  325. if (empty($group_id)) {
  326. $insert_id = AnnouncementManager::add_announcement(
  327. $data['title'],
  328. $data['content'],
  329. $data['users'],
  330. $file,
  331. $file_comment,
  332. $sendToUsersInSession
  333. );
  334. } else {
  335. $insert_id = AnnouncementManager::add_group_announcement(
  336. $data['title'],
  337. $data['content'],
  338. array('GROUP:' . $group_id),
  339. $data['users'],
  340. $file,
  341. $file_comment,
  342. $sendToUsersInSession
  343. );
  344. }
  345. Display::addFlash(
  346. Display::return_message(
  347. get_lang('AnnouncementAdded'),
  348. 'success'
  349. )
  350. );
  351. /* MAIL FUNCTION */
  352. if (isset($data['email_ann']) && $data['email_ann']) {
  353. AnnouncementManager::send_email($insert_id, $sendToUsersInSession);
  354. }
  355. header('Location: '.$homeUrl);
  356. exit;
  357. } // end condition token
  358. }
  359. }
  360. break;
  361. }
  362. if (!empty($_GET['remind_inactive'])) {
  363. $to[] = 'USER:'.intval($_GET['remind_inactive']);
  364. }
  365. /*
  366. if (api_is_allowed_to_edit(false,true) OR
  367. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  368. ) {
  369. //
  370. if (!empty($_GET['action']) and $_GET['action']=='modify' AND isset($_GET['id'])) {
  371. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  372. api_not_allowed();
  373. }
  374. // RETRIEVE THE CONTENT OF THE ANNOUNCEMENT TO MODIFY
  375. $id = intval($_GET['id']);
  376. if (!api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  377. $sql = "SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id = '$id'";
  378. $rs = Database::query($sql);
  379. $myrow = Database::fetch_array($rs);
  380. $last_id = $id;
  381. $userUpload = isset($_FILES['user_upload']) ? $_FILES['user_upload'] : null;
  382. $edit_attachment = AnnouncementManager::edit_announcement_attachment_file(
  383. $last_id,
  384. $userUpload,
  385. $file_comment
  386. );
  387. if ($myrow) {
  388. $announcement_to_modify = $myrow['id'];
  389. $content_to_modify = $myrow['content'];
  390. $title_to_modify = $myrow['title'];
  391. if ($originalresource!=="no") {
  392. $to = AnnouncementManager::load_edit_users("announcement", $announcement_to_modify);
  393. }
  394. $display_announcement_list = false;
  395. }
  396. }
  397. }
  398. }*/
  399. $htmlHeadXtra[] = AnnouncementManager::to_javascript();
  400. $group_id = api_get_group_id();
  401. if (!empty($group_id)) {
  402. $group_properties = GroupManager :: get_group_properties($group_id);
  403. $interbreadcrumb[] = array("url" => "../group/group.php?".api_get_cidreq(), "name" => get_lang('Groups'));
  404. $interbreadcrumb[] = array("url"=>"../group/group_space.php?".api_get_cidreq(), "name"=> get_lang('GroupSpace').' '.$group_properties['name']);
  405. }
  406. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  407. //we are not in the learning path
  408. Display::display_header($nameTools,get_lang('Announcements'));
  409. }
  410. // Tool introduction
  411. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  412. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  413. }
  414. // Actions
  415. $show_actions = false;
  416. if ((api_is_allowed_to_edit(false,true) OR
  417. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) and
  418. (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath')
  419. ) {
  420. echo '<div class="actions">';
  421. if (in_array($action, array('add', 'modify','view'))) {
  422. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".$origin."'>".
  423. Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM)."</a>";
  424. } else {
  425. echo "<a href='".api_get_self()."?".api_get_cidreq()."&action=add&origin=".$origin."'>".
  426. Display::return_icon('new_announce.png',get_lang('AddAnnouncement'),'',ICON_SIZE_MEDIUM)."</a>";
  427. }
  428. $show_actions = true;
  429. } else {
  430. if (in_array($action, array('view'))) {
  431. echo '<div class="actions">';
  432. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".$origin."'>".
  433. Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM)."</a>";
  434. echo '</div>';
  435. }
  436. }
  437. if (api_is_allowed_to_edit() && $announcement_number > 1) {
  438. if (api_get_group_id() == 0 ) {
  439. if (!$show_actions)
  440. echo '<div class="actions">';
  441. if (!isset($_GET['action'])) {
  442. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete_all\" onclick=\"javascript:if(!confirm('".get_lang("ConfirmYourChoice")."')) return false;\">".
  443. Display::return_icon('delete_announce.png',get_lang('AnnouncementDeleteAll'),'',ICON_SIZE_MEDIUM)."</a>";
  444. }
  445. }
  446. }
  447. if ($show_actions)
  448. echo '</div>';
  449. Display::showFlash();
  450. echo $content;
  451. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  452. //we are not in learnpath tool
  453. Display::display_footer();
  454. }