announcements.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 2013
  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. * @todo make AWACS out of the configuration settings
  13. * @todo this file is 1300+ lines without any functions -> needs to be split into
  14. * multiple functions
  15. */
  16. /*
  17. INIT SECTION
  18. */
  19. // name of the language file that needs to be included
  20. use \ChamiloSession as Session;
  21. $language_file = array('announcements', 'group', 'survey', 'document');
  22. // use anonymous mode when accessing this course tool
  23. $use_anonymous = true;
  24. // setting the global file that gets the general configuration, the databases, the languages, ...
  25. //require_once '../inc/global.inc.php';
  26. $ctok = Security::getCurrentToken();
  27. $stok = Security::get_token();
  28. $current_course_tool = TOOL_ANNOUNCEMENT;
  29. $this_section = SECTION_COURSES;
  30. $nameTools = get_lang('ToolAnnouncement');
  31. /* ACCESS RIGHTS */
  32. api_protect_course_script(true);
  33. // Configuration settings
  34. $display_announcement_list = true;
  35. $display_form = false;
  36. $display_title_list = true;
  37. // Maximum title messages to display
  38. $maximum = '12';
  39. // Database Table Definitions
  40. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  41. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  42. /* Libraries */
  43. $course_id = api_get_course_int_id();
  44. /* Tracking */
  45. Event::event_access_tool(TOOL_ANNOUNCEMENT);
  46. if (!empty($_POST['To'])) {
  47. if (api_get_session_id() != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  48. api_not_allowed(true);
  49. }
  50. $display_form = true;
  51. }
  52. $origin = empty($_GET['origin']) ? '' : Security::remove_XSS($_GET['origin']);
  53. // display the form
  54. if (((!empty($_GET['action']) && $_GET['action'] == 'add') && $_GET['origin'] == "") || (!empty($_GET['action']) && $_GET['action'] == 'edit') || !empty($_POST['To'])) {
  55. if (api_get_session_id() != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  56. api_not_allowed(true);
  57. }
  58. $display_form = true;
  59. }
  60. //$htmlHeadXtra[] = AnnouncementManager::to_javascript();
  61. $email_ann = null;
  62. $group_id = api_get_group_id();
  63. if (!empty($group_id)) {
  64. $group_properties = GroupManager::get_group_properties($group_id);
  65. $interbreadcrumb[] = array("url" => "../group/group.php", "name" => get_lang('Groups'));
  66. $interbreadcrumb[] = array(
  67. "url" => "../group/group_space.php?gidReq=".$group_id,
  68. "name" => get_lang('GroupSpace').' '.$group_properties['name']
  69. );
  70. }
  71. $announcement_id = isset($_GET['id']) ? intval($_GET['id']) : null;
  72. $message = null;
  73. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  74. // we are not in the learning path
  75. Display::display_header($nameTools, get_lang('Announcements'));
  76. }
  77. if (AnnouncementManager::user_can_edit_announcement()) {
  78. /* Change visibility of announcement */
  79. // change visibility -> studentview -> course manager view
  80. if (!isset($_GET['isStudentView']) || $_GET['isStudentView'] != 'false') {
  81. if (isset($_GET['id']) and $_GET['id'] and isset($_GET['action']) and $_GET['action'] == "showhide") {
  82. if (api_get_session_id() != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  83. api_not_allowed();
  84. }
  85. if (!api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $_GET['id'])) {
  86. if ($ctok == $_GET['sec_token']) {
  87. AnnouncementManager::change_visibility_announcement($_course, $_GET['id']);
  88. $message = get_lang('VisibilityChanged');
  89. }
  90. }
  91. }
  92. }
  93. /* Delete announcement */
  94. if (!empty($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
  95. $id = intval($_GET['id']);
  96. if (api_get_session_id() != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  97. api_not_allowed();
  98. }
  99. if (api_is_platform_admin() || !api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  100. // tooledit : visibility = 2 : only visible for platform administrator
  101. if ($ctok == $_GET['sec_token']) {
  102. AnnouncementManager::delete_announcement($_course, $id);
  103. $id = null;
  104. $emailTitle = null;
  105. $newContent = null;
  106. $message = get_lang('AnnouncementDeleted');
  107. }
  108. }
  109. }
  110. // Delete attachment file
  111. if (isset($_GET['action']) && $_GET['action'] == 'delete_attachment') {
  112. $id = $_GET['id_attach'];
  113. if ($ctok == $_GET['sec_token']) {
  114. if (api_is_allowed_to_edit()) {
  115. AnnouncementManager::delete_announcement_attachment_file($id);
  116. }
  117. }
  118. }
  119. /* Delete all announcements */
  120. if (!empty($_GET['action']) and $_GET['action'] == 'delete_all') {
  121. if (api_is_allowed_to_edit()) {
  122. AnnouncementManager::delete_all_announcements($_course);
  123. $id = null;
  124. $emailTitle = null;
  125. $newContent = null;
  126. $message = get_lang('AnnouncementDeletedAll');
  127. }
  128. }
  129. /* Edit announcement */
  130. if (!empty($_GET['action']) and $_GET['action'] == 'modify' and isset($_GET['id'])) {
  131. if (api_get_session_id() != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  132. api_not_allowed();
  133. }
  134. $display_form = true;
  135. // RETRIEVE THE CONTENT OF THE ANNOUNCEMENT TO MODIFY
  136. $id = intval($_GET['id']);
  137. if (api_is_platform_admin() || !api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  138. $announcementInfo = AnnouncementManager::get_by_id($course_id, $id);
  139. if ($announcementInfo) {
  140. $announcement_to_modify = $announcementInfo['id'];
  141. $content_to_modify = $announcementInfo['content'];
  142. $title_to_modify = $announcementInfo['title'];
  143. $to = AnnouncementManager::load_edit_users("announcement", $announcement_to_modify);
  144. $display_announcement_list = false;
  145. }
  146. }
  147. }
  148. /* Move announcement up/down */
  149. if (isset($_GET['sec_token']) && $ctok == $_GET['sec_token']) {
  150. if (!empty($_GET['down'])) {
  151. $thisAnnouncementId = intval($_GET['down']);
  152. $sortDirection = "DESC";
  153. }
  154. if (!empty($_GET['up'])) {
  155. $thisAnnouncementId = intval($_GET['up']);
  156. $sortDirection = "ASC";
  157. }
  158. }
  159. if (!empty($sortDirection)) {
  160. if (!in_array(trim(strtoupper($sortDirection)), array('ASC', 'DESC'))) {
  161. $sortDirection = 'ASC';
  162. }
  163. $my_sql = "SELECT announcement.id, announcement.display_order ".
  164. "FROM $tbl_announcement announcement, ".
  165. "$tbl_item_property itemproperty ".
  166. "WHERE
  167. announcement.c_id = $course_id AND
  168. itemproperty.c_id = $course_id AND
  169. itemproperty.ref=announcement.id ".
  170. "AND itemproperty.tool='".TOOL_ANNOUNCEMENT."' ".
  171. "AND itemproperty.visibility<>2 ".
  172. "ORDER BY display_order $sortDirection";
  173. $result = Database::query($my_sql);
  174. $thisAnnouncementOrderFound = false;
  175. $thisAnnouncementOrder = 1;
  176. while (list ($announcementId, $announcementOrder) = Database::fetch_row($result)) {
  177. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER. COMMIT ORDER SWAP ON THE DB
  178. if ($thisAnnouncementOrderFound) {
  179. $nextAnnouncementId = $announcementId;
  180. $nextAnnouncementOrder = $announcementOrder;
  181. $sql = "UPDATE $tbl_announcement SET display_order = '$nextAnnouncementOrder'
  182. WHERE c_id = $course_id AND id = '$thisAnnouncementId'";
  183. Database::query($sql);
  184. $sql = "UPDATE $tbl_announcement SET display_order = '$thisAnnouncementOrder'
  185. WHERE c_id = $course_id AND id = '$nextAnnouncementId.'";
  186. Database::query($sql);
  187. break;
  188. }
  189. // STEP 1 : FIND THE ORDER OF THE ANNOUNCEMENT
  190. if ($announcementId == $thisAnnouncementId) {
  191. $thisAnnouncementOrder = $announcementOrder;
  192. $thisAnnouncementOrderFound = true;
  193. }
  194. }
  195. // show message
  196. $message = get_lang('AnnouncementMoved');
  197. }
  198. /* Submit announcement */
  199. $submitAnnouncement = isset($_POST['submitAnnouncement']) ? $_POST['submitAnnouncement'] : 0;
  200. $id = 0;
  201. if (!empty($_POST['id'])) {
  202. $id = intval($_POST['id']);
  203. }
  204. if ($submitAnnouncement) {
  205. $selected_form = isset($_POST['users']) ? $_POST['users'] : null;
  206. $sendEmail = isset($_POST['email_ann']) ? $_POST['email_ann'] : null;
  207. if (isset($id) && $id) {
  208. // there is an Id => the announcement already exists => update mode
  209. if ($ctok == $_POST['sec_token']) {
  210. $file_comment = $_POST['file_comment'];
  211. $file = $_FILES['user_upload'];
  212. AnnouncementManager::edit_announcement(
  213. $id,
  214. $_POST['emailTitle'],
  215. $_POST['newContent'],
  216. $selected_form,
  217. $file,
  218. $file_comment,
  219. $sendEmail
  220. );
  221. $message = get_lang('AnnouncementModified');
  222. }
  223. } else {
  224. // Insert mode
  225. //if (1) {
  226. if ($ctok == $_REQUEST['sec_token']) {
  227. $file = $_FILES['user_upload'];
  228. $file_comment = $_POST['file_comment'];
  229. if (!empty($group_id)) {
  230. $insert_id = AnnouncementManager::add_group_announcement(
  231. $_POST['emailTitle'],
  232. $_POST['newContent'],
  233. array('GROUP:'.$group_id),
  234. $selected_form,
  235. $file,
  236. $file_comment,
  237. $sendEmail
  238. );
  239. } else {
  240. $insert_id = AnnouncementManager::add_announcement(
  241. $_POST['emailTitle'],
  242. $_POST['newContent'],
  243. $selected_form,
  244. $file,
  245. $file_comment,
  246. $sendEmail
  247. );
  248. }
  249. $message = get_lang('AnnouncementAdded');
  250. }
  251. }
  252. }
  253. }
  254. /* Tool introduction */
  255. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  256. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  257. }
  258. /* DISPLAY LEFT COLUMN */
  259. //condition for the session
  260. $session_id = api_get_session_id();
  261. $condition_session = api_get_session_condition($session_id, true, true);
  262. if (api_is_allowed_to_edit(false, true)) {
  263. // check teacher status
  264. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  265. if (api_get_group_id() == 0) {
  266. $group_condition = "";
  267. } else {
  268. $group_condition = " AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0)";
  269. }
  270. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  271. FROM $tbl_announcement announcement, $tbl_item_property ip
  272. WHERE announcement.c_id = $course_id AND
  273. ip.c_id = $course_id AND
  274. announcement.id = ip.ref AND
  275. ip.tool = 'announcement' AND
  276. ip.visibility <> '2'
  277. $group_condition
  278. $condition_session
  279. GROUP BY ip.ref
  280. ORDER BY display_order DESC
  281. LIMIT 0,$maximum";
  282. }
  283. } else {
  284. // students only get to see the visible announcements
  285. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  286. $group_memberships = GroupManager::get_group_ids($_course['real_id'], api_get_user_id());
  287. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  288. if (api_get_group_id() == 0) {
  289. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id(
  290. )."' OR ( ip.to_user_id='".api_get_user_id()."'".
  291. "OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))) ";
  292. } else {
  293. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."'
  294. OR ip.to_group_id IN (0, ".api_get_group_id()."))";
  295. }
  296. } else {
  297. if (api_get_group_id() == 0) {
  298. $cond_user_id = " AND ( ip.to_user_id='".api_get_user_id()."'".
  299. "OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ";
  300. } else {
  301. $cond_user_id = " AND (
  302. (ip.to_user_id='".api_get_user_id()."' AND ip.to_group_id = ".api_get_group_id().") OR
  303. ip.to_group_id IN (".api_get_group_id().") AND ip.to_user_id = 0 ) ";
  304. }
  305. }
  306. // the user is member of several groups => display personal announcements AND his group announcements AND the general announcements
  307. if (is_array($group_memberships) && count($group_memberships) > 0) {
  308. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  309. FROM $tbl_announcement announcement, $tbl_item_property ip
  310. WHERE
  311. announcement.c_id = $course_id AND
  312. ip.c_id = $course_id AND
  313. announcement.id = ip.ref AND
  314. ip.tool='announcement' AND
  315. ip.visibility='1'
  316. $cond_user_id
  317. $condition_session
  318. GROUP BY ip.ref
  319. ORDER BY display_order DESC
  320. LIMIT 0,$maximum";
  321. } else {
  322. // the user is not member of any group
  323. // this is an identified user => show the general announcements AND his personal announcements
  324. if ($_user['user_id']) {
  325. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  326. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id(
  327. )."' OR ( ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0')) ";
  328. } else {
  329. $cond_user_id = " AND ( ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0') ";
  330. }
  331. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  332. FROM $tbl_announcement announcement, $tbl_item_property ip
  333. WHERE
  334. announcement.c_id = $course_id AND
  335. ip.c_id = $course_id AND
  336. announcement.id = ip.ref
  337. AND ip.tool='announcement'
  338. AND ip.visibility='1'
  339. $cond_user_id
  340. $condition_session
  341. GROUP BY ip.ref
  342. ORDER BY display_order DESC
  343. LIMIT 0,$maximum";
  344. } else {
  345. if (api_get_course_setting('allow_user_edit_announcement')) {
  346. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0') ";
  347. } else {
  348. $cond_user_id = " AND ip.to_group_id='0' ";
  349. }
  350. // the user is not identiefied => show only the general announcements
  351. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  352. FROM $tbl_announcement announcement, $tbl_item_property ip
  353. WHERE
  354. announcement.c_id = $course_id AND
  355. ip.c_id = $course_id AND
  356. announcement.id = ip.ref
  357. AND ip.tool='announcement'
  358. AND ip.visibility='1'
  359. AND ip.to_group_id='0'
  360. $condition_session
  361. GROUP BY ip.ref
  362. ORDER BY display_order DESC
  363. LIMIT 0,$maximum";
  364. }
  365. }
  366. }
  367. }
  368. $result = Database::query($sql);
  369. $announcement_number = Database::num_rows($result);
  370. /*
  371. ADD ANNOUNCEMENT / DELETE ALL
  372. */
  373. $show_actions = false;
  374. if (AnnouncementManager::user_can_edit_announcement()) {
  375. echo '<div class="actions">';
  376. if (isset($_GET['action']) && in_array($_GET['action'], array('add', 'modify', 'view'))) {
  377. echo "<a href='".api_get_self()."?".api_get_cidreq(
  378. )."&origin=".$origin."'>".Display::return_icon(
  379. 'back.png',
  380. get_lang('Back'),
  381. '',
  382. ICON_SIZE_MEDIUM
  383. )."</a>";
  384. } else {
  385. echo "<a href='".api_get_self()."?".api_get_cidreq(
  386. )."&action=add&origin=".$origin."'>".Display::return_icon(
  387. 'new_announce.png',
  388. get_lang('AddAnnouncement'),
  389. '',
  390. ICON_SIZE_MEDIUM
  391. )."</a>";
  392. }
  393. $show_actions = true;
  394. } else {
  395. if (isset($_GET['action']) && in_array($_GET['action'], array('view'))) {
  396. echo '<div class="actions">';
  397. echo "<a href='".api_get_self()."?".api_get_cidreq(
  398. )."&origin=".$origin."'>".Display::return_icon(
  399. 'back.png',
  400. get_lang('Back'),
  401. '',
  402. ICON_SIZE_MEDIUM
  403. )."</a>";
  404. echo '</div>';
  405. }
  406. }
  407. if (api_is_allowed_to_edit() && $announcement_number > 1) {
  408. if ($group_id == 0) {
  409. if (!$show_actions) {
  410. echo '<div class="actions">';
  411. }
  412. if (!isset($_GET['action']) or !in_array($_GET['action'], array('add', 'modify', 'view'))) {
  413. echo "<a href=\"".api_get_self()."?".api_get_cidreq(
  414. )."&action=delete_all\" onclick=\"javascript:if(!confirm('".get_lang(
  415. "ConfirmYourChoice"
  416. )."')) return false;\">".Display::return_icon(
  417. 'delete_announce.png',
  418. get_lang('AnnouncementDeleteAll'),
  419. '',
  420. ICON_SIZE_MEDIUM
  421. )."</a>";
  422. }
  423. }
  424. }
  425. if ($show_actions) {
  426. echo '</div>';
  427. }
  428. // ANNOUNCEMENTS LIST
  429. if ($message) {
  430. Display::display_confirmation_message($message);
  431. $display_announcement_list = true;
  432. $display_form = false;
  433. }
  434. if (!empty($error_message)) {
  435. Display::display_error_message($error_message);
  436. $display_announcement_list = false;
  437. $display_form = true;
  438. }
  439. /* DISPLAY FORM */
  440. if ($display_form) {
  441. // DISPLAY ADD ANNOUNCEMENT COMMAND
  442. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  443. $url = api_get_self().'?id='.$id.'&'.api_get_cidreq();
  444. $form = new FormValidator('f1', 'post', $url, array('enctype' => 'multipart/form-data'));
  445. if (empty($_GET['id'])) {
  446. $form_name = get_lang('AddAnnouncement');
  447. } else {
  448. $form_name = get_lang('ModifyAnnouncement');
  449. }
  450. $form->addElement('header', $form_name);
  451. if (empty($group_id)) {
  452. if (isset($_GET['remind_inactive'])) {
  453. $email_ann = '1';
  454. $_SESSION['select_groupusers'] = "show";
  455. $content_to_modify = sprintf(get_lang('RemindInactiveLearnersMailContent'), api_get_setting('platform.site_name'), 7);
  456. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('platform.site_name'));
  457. } elseif (isset($_GET['remindallinactives']) && $_GET['remindallinactives'] == 'true') {
  458. // we want to remind inactive users. The $_GET['since'] parameter determines which users have to be warned (i.e the users who have been inactive for x days or more
  459. $since = isset($_GET['since']) ? intval($_GET['since']) : 6;
  460. // getting the users who have to be reminded
  461. $to = Tracking :: get_inactives_students_in_course(api_get_course_int_id(), $since, api_get_session_id());
  462. // setting the variables for the form elements: the users who need to receive the message
  463. foreach ($to as &$user) {
  464. $user = 'USER:'.$user;
  465. }
  466. // setting the variables for the form elements: the message has to be sent by email
  467. $email_ann = '1';
  468. // setting the variables for the form elements: the title of the email
  469. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('platform.site_name'));
  470. // setting the variables for the form elements: the message of the email
  471. $content_to_modify = sprintf(
  472. get_lang('RemindInactiveLearnersMailContent'),
  473. api_get_setting('platform.site_name'),
  474. $since
  475. );
  476. // when we want to remind the users who have never been active then we have a different subject and content for the announcement
  477. if ($_GET['since'] == 'never') {
  478. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('platform.site_name'));
  479. $content_to_modify = get_lang('YourAccountIsActiveYouCanLoginAndCheckYourCourses');
  480. }
  481. }
  482. CourseManager::addUserGroupMultiSelect($form, array());
  483. if (!isset($announcement_to_modify)) {
  484. $announcement_to_modify = '';
  485. }
  486. $form->addElement('checkbox', 'email_ann', null, get_lang('EmailOption'));
  487. } else {
  488. if (!isset($announcement_to_modify)) {
  489. $announcement_to_modify = "";
  490. }
  491. CourseManager::addGroupMultiSelect($form, $group_id, array());
  492. $form->addElement('checkbox', 'email_ann', null, get_lang('EmailOption'));
  493. }
  494. if (isset($announcementInfo) && !empty($announcementInfo)) {
  495. $defaults = array(
  496. 'emailTitle' => $title_to_modify,
  497. 'newContent' => $content_to_modify,
  498. 'id' => $announcement_to_modify,
  499. 'users' => $to
  500. );
  501. } else {
  502. $defaults = array();
  503. }
  504. $form->addElement('text', 'emailTitle', get_lang('EmailTitle'));
  505. $form->addElement('hidden', 'id');
  506. $form->add_html_editor('newContent', get_lang('Description'));
  507. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  508. $form->addElement('textarea', 'file_comment', get_lang('FileComment'));
  509. $form->addElement('hidden', 'submitAnnouncement', 'OK');
  510. $form->addElement('hidden', 'sec_token', $stok);
  511. $form->addElement('button', 'submit', get_lang('ButtonPublishAnnouncement'));
  512. $form->setDefaults($defaults);
  513. echo $form->return_form();
  514. }
  515. /*
  516. DISPLAY ANNOUNCEMENT LIST
  517. */
  518. if ($display_announcement_list) {
  519. $user_id = api_get_user_id();
  520. $group_id = api_get_group_id();
  521. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  522. if (api_is_allowed_to_edit(false, true) OR (api_get_course_setting(
  523. 'allow_user_edit_announcement'
  524. ) && !api_is_anonymous())
  525. ) {
  526. // A.1. you are a course admin with a USER filter
  527. // => see only the messages of this specific user + the messages of the group (s)he is member of.
  528. if (!empty($_SESSION['user'])) {
  529. if (is_array($group_memberships) && count($group_memberships) > 0) {
  530. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  531. FROM $tbl_announcement announcement, $tbl_item_property ip
  532. WHERE announcement.c_id = $course_id AND
  533. ip.c_id = $course_id AND
  534. announcement.id = ip.ref AND
  535. ip.tool = 'announcement' AND
  536. (ip.to_user_id = $user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  537. $condition_session
  538. ORDER BY display_order DESC";
  539. } else {
  540. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  541. FROM $tbl_announcement announcement, $tbl_item_property ip
  542. WHERE announcement.c_id = $course_id AND
  543. ip.c_id = $course_id AND
  544. announcement.id = ip.ref AND
  545. ip.tool ='announcement' AND
  546. (ip.to_user_id = $user_id OR ip.to_group_id='0') AND
  547. ip.visibility='1'
  548. $condition_session
  549. ORDER BY display_order DESC";
  550. }
  551. } elseif (api_get_group_id() != 0) {
  552. // A.2. you are a course admin with a GROUP filter
  553. // => see only the messages of this specific group
  554. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  555. FROM $tbl_announcement announcement, $tbl_item_property ip
  556. WHERE announcement.c_id = $course_id AND
  557. ip.c_id = $course_id AND
  558. announcement.id = ip.ref
  559. AND ip.tool='announcement'
  560. AND ip.visibility<>'2'
  561. AND (ip.to_group_id = $group_id OR ip.to_group_id='0')
  562. $condition_session
  563. GROUP BY ip.ref
  564. ORDER BY display_order DESC";
  565. } else {
  566. // A.3 you are a course admin without any group or user filter
  567. // A.3.a you are a course admin without user or group filter but WITH studentview
  568. // => see all the messages of all the users and groups without editing possibilities
  569. if (isset($isStudentView) and $isStudentView == "true") {
  570. $sql = "SELECT
  571. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  572. FROM $tbl_announcement announcement, $tbl_item_property ip
  573. WHERE announcement.c_id = $course_id AND
  574. ip.c_id = $course_id AND
  575. announcement.id = ip.ref
  576. AND ip.tool='announcement'
  577. AND ip.visibility='1'
  578. $condition_session
  579. GROUP BY ip.ref
  580. ORDER BY display_order DESC";
  581. } else {
  582. // A.3.a you are a course admin without user or group filter and WITHOUT studentview (= the normal course admin view)
  583. // => see all the messages of all the users and groups with editing possibilities
  584. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  585. FROM $tbl_announcement announcement, $tbl_item_property ip
  586. WHERE announcement.c_id = $course_id AND
  587. ip.c_id = $course_id AND
  588. announcement.id = ip.ref AND
  589. ip.tool='announcement' AND
  590. (ip.visibility='0' or ip.visibility='1') AND
  591. to_group_id = 0
  592. $condition_session
  593. GROUP BY ip.ref
  594. ORDER BY display_order DESC";
  595. }
  596. }
  597. } else {
  598. //STUDENT
  599. if (is_array($group_memberships) && count($group_memberships) > 0) {
  600. if (AnnouncementManager::user_can_edit_announcement()) {
  601. if (api_get_group_id() == 0) {
  602. //No group
  603. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id(
  604. )."' OR ( ip.to_user_id='".$_user['user_id']."'".
  605. " OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))) ";
  606. } else {
  607. $cond_user_id = " AND (
  608. ip.lastedit_user_id = '".api_get_user_id()."' OR
  609. ip.to_group_id IN (".api_get_group_id().")
  610. )";
  611. }
  612. } else {
  613. if (api_get_group_id() == 0) {
  614. $cond_user_id = " AND (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(
  615. ", ",
  616. $group_memberships
  617. ).")) ";
  618. } else {
  619. $cond_user_id = " AND (
  620. (ip.to_user_id = $user_id AND ip.to_group_id = ".api_get_group_id().") OR
  621. (ip.to_group_id IN (".api_get_group_id().") AND ip.to_user_id = 0 )
  622. )";
  623. }
  624. }
  625. $visibility_condition = " ip.visibility='1'";
  626. if (GroupManager::is_tutor_of_group(api_get_user_id(), $group_id)) {
  627. $visibility_condition = " ip.visibility IN ('0', '1') ";
  628. }
  629. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  630. FROM $tbl_announcement announcement, $tbl_item_property ip
  631. WHERE announcement.c_id = $course_id AND
  632. ip.c_id = $course_id AND
  633. announcement.id = ip.ref
  634. AND ip.tool='announcement'
  635. $cond_user_id
  636. $condition_session AND $visibility_condition
  637. ORDER BY display_order DESC";
  638. } else {
  639. if ($_user['user_id']) {
  640. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  641. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id(
  642. )."' OR (ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0')) ";
  643. } else {
  644. $cond_user_id = " AND (ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0') ";
  645. }
  646. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  647. FROM $tbl_announcement announcement, $tbl_item_property ip
  648. WHERE
  649. announcement.c_id = $course_id AND
  650. ip.c_id = $course_id AND
  651. announcement.id = ip.ref AND
  652. ip.tool='announcement'
  653. $cond_user_id
  654. $condition_session
  655. AND ip.visibility='1'
  656. AND announcement.session_id IN(0,".api_get_session_id().")
  657. ORDER BY display_order DESC";
  658. } else {
  659. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  660. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' ) ";
  661. } else {
  662. $cond_user_id = " AND ip.to_group_id='0' ";
  663. }
  664. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  665. FROM $tbl_announcement announcement, $tbl_item_property ip
  666. WHERE
  667. announcement.c_id = $course_id AND
  668. ip.c_id = $course_id AND
  669. announcement.id = ip.ref
  670. AND ip.tool='announcement'
  671. $cond_user_id
  672. $condition_session
  673. AND ip.visibility='1'
  674. AND announcement.session_id IN(0,".api_get_session_id().")";
  675. }
  676. }
  677. }
  678. $result = Database::query($sql);
  679. $num_rows = Database::num_rows($result);
  680. // DISPLAY: NO ITEMS
  681. if (!isset($_GET['action']) || !in_array($_GET['action'], array('add', 'modify', 'view'))) {
  682. if ($num_rows == 0) {
  683. if ((api_is_allowed_to_edit(false, true) OR (api_get_course_setting(
  684. 'allow_user_edit_announcement'
  685. ) && !api_is_anonymous())) and (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath')
  686. ) {
  687. echo '<div id="no-data-view">';
  688. echo '<h2>'.get_lang('Announcements').'</h2>';
  689. echo Display::return_icon('valves.png', '', array(), 64);
  690. echo '<div class="controls">';
  691. echo Display::url(
  692. get_lang('AddAnnouncement'),
  693. api_get_self()."?".api_get_cidreq(
  694. )."&action=add&origin=".$origin,
  695. array('class' => 'btn')
  696. );
  697. echo '</div>';
  698. echo '</div>';
  699. } else {
  700. Display::display_warning_message(get_lang('NoAnnouncements'));
  701. }
  702. } else {
  703. $iterator = 1;
  704. $bottomAnnouncement = $announcement_number;
  705. echo '<table width="100%" class="data_table">';
  706. $ths = Display::tag('th', get_lang('Title'));
  707. $ths .= Display::tag('th', get_lang('By'));
  708. $ths .= Display::tag('th', get_lang('LastUpdateDate'));
  709. if (api_is_allowed_to_edit(false, true) OR (api_is_course_coach() && api_is_element_in_the_session(
  710. TOOL_ANNOUNCEMENT,
  711. $announcementInfo['id']
  712. ))
  713. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  714. ) {
  715. $ths .= Display::tag('th', get_lang('Modify'));
  716. }
  717. echo Display::tag('tr', $ths);
  718. $displayed = array();
  719. while ($myrow = Database::fetch_array($result, 'ASSOC')) {
  720. if (!in_array($myrow['id'], $displayed)) {
  721. $sent_to_icon = '';
  722. // the email icon
  723. if ($myrow['email_sent'] == '1') {
  724. $sent_to_icon = ' '.Display::return_icon('email.gif', get_lang('AnnounceSentByEmail'));
  725. }
  726. $title = $myrow['title'].$sent_to_icon;
  727. $item_visibility = api_get_item_visibility(
  728. $_course,
  729. TOOL_ANNOUNCEMENT,
  730. $myrow['id'],
  731. $session_id
  732. );
  733. $myrow['visibility'] = $item_visibility;
  734. // the styles
  735. if ($myrow['visibility'] == '0') {
  736. $style = 'invisible';
  737. } else {
  738. $style = '';
  739. }
  740. echo "<tr>";
  741. // show attachment list
  742. $attachment_list = AnnouncementManager::get_attachment($myrow['id']);
  743. $attachment_icon = '';
  744. if (count($attachment_list) > 0) {
  745. $attachment_icon = ' '.Display::return_icon('attachment.gif', get_lang('Attachment'));
  746. }
  747. /* TITLE */
  748. $title = Display::url($title.$attachment_icon, '?action=view&id='.$myrow['id']);
  749. echo Display::tag('td', Security::remove_XSS($title), array('class' => $style));
  750. $user_info = api_get_user_info($myrow['insert_user_id']);
  751. $username = sprintf(get_lang("LoginX"), $user_info['username']);
  752. $username_span = Display::tag(
  753. 'span',
  754. api_get_person_name($user_info['firstName'], $user_info['lastName']),
  755. array('title' => $username)
  756. );
  757. echo Display::tag('td', $username_span);
  758. echo Display::tag(
  759. 'td',
  760. api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG)
  761. );
  762. // we can edit if : we are the teacher OR the element belongs to the session we are coaching OR the option to allow users to edit is on
  763. $modify_icons = '';
  764. if (api_is_allowed_to_edit(false, true) OR (api_is_course_coach() && api_is_element_in_the_session(
  765. TOOL_ANNOUNCEMENT,
  766. $myrow['id']
  767. ))
  768. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  769. ) {
  770. $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq(
  771. )."&action=modify&id=".$myrow['id']."\">".Display::return_icon(
  772. 'edit.png',
  773. get_lang('Edit'),
  774. '',
  775. ICON_SIZE_SMALL
  776. )."</a>";
  777. if ($myrow['visibility'] == 1) {
  778. $image_visibility = "visible";
  779. $alt_visibility = get_lang('Hide');
  780. } else {
  781. $image_visibility = "invisible";
  782. $alt_visibility = get_lang('Visible');
  783. }
  784. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq(
  785. )."&origin=".$origin."&action=showhide&id=".$myrow['id']."&sec_token=".$stok."\">".
  786. Display::return_icon(
  787. $image_visibility.'.png',
  788. $alt_visibility,
  789. '',
  790. ICON_SIZE_SMALL
  791. )."</a>";
  792. // DISPLAY MOVE UP COMMAND only if it is not the top announcement
  793. if ($iterator != 1) {
  794. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq(
  795. )."&up=".$myrow["id"]."&sec_token=".$stok."\">".Display::return_icon(
  796. 'up.gif',
  797. get_lang('Up')
  798. )."</a>";
  799. } else {
  800. $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up'));
  801. }
  802. if ($iterator < $bottomAnnouncement) {
  803. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq(
  804. )."&down=".$myrow["id"]."&sec_token=".$stok."\">".Display::return_icon(
  805. 'down.gif',
  806. get_lang('Down')
  807. )."</a>";
  808. } else {
  809. $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down'));
  810. }
  811. if (api_is_allowed_to_edit(false, true)) {
  812. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq(
  813. )."&action=delete&id=".$myrow['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(
  814. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)
  815. )."')) return false;\">".
  816. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).
  817. "</a>";
  818. }
  819. $iterator++;
  820. echo Display::tag('td', $modify_icons);
  821. }
  822. echo "</tr>";
  823. }
  824. $displayed[] = $myrow['id'];
  825. } // end while
  826. echo "</table>";
  827. }
  828. }
  829. }
  830. if (isset($_GET['action']) && $_GET['action'] == 'view') {
  831. AnnouncementManager::display_announcement($announcement_id);
  832. }
  833. /* FOOTER */
  834. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  835. //we are not in learnpath tool
  836. Display::display_footer();
  837. }