announcements.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. * @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. $language_file = array('announcements', 'group', 'survey');
  21. // use anonymous mode when accessing this course tool
  22. $use_anonymous = true;
  23. // setting the global file that gets the general configuration, the databases, the languages, ...
  24. require_once '../inc/global.inc.php';
  25. $this_section=SECTION_COURSES;
  26. $nameTools = get_lang('ToolAnnouncement');
  27. //session
  28. if(isset($_GET['id_session'])) {
  29. $_SESSION['id_session'] = intval($_GET['id_session']);
  30. }
  31. /* ACCESS RIGHTS */
  32. api_protect_course_script();
  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. // Length of the titles
  40. $length = '36';
  41. // Database Table Definitions
  42. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  43. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  44. $tbl_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  45. $tbl_sessions = Database::get_main_table(TABLE_MAIN_SESSION);
  46. $tbl_session_course_user= Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  47. $tbl_groupUser = Database::get_course_table(TABLE_GROUP_USER);
  48. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  49. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  50. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  51. /* Libraries */
  52. $lib = api_get_path(LIBRARY_PATH); //avoid useless function calls
  53. require_once $lib.'groupmanager.lib.php';
  54. require_once $lib.'mail.lib.inc.php';
  55. require_once $lib.'tracking.lib.php';
  56. require_once $lib.'fckeditor/fckeditor.php';
  57. require_once $lib.'fileUpload.lib.php';
  58. require_once 'announcements.inc.php';
  59. $course_id = api_get_course_int_id();
  60. /* Tracking */
  61. event_access_tool(TOOL_ANNOUNCEMENT);
  62. /* POST TO */
  63. $safe_emailTitle = $_POST['emailTitle'];
  64. $safe_newContent = $_POST['newContent'];
  65. $content_to_modify = $title_to_modify = '';
  66. if (!empty($_POST['To'])) {
  67. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  68. api_not_allowed();
  69. }
  70. $display_form = true;
  71. $form_elements = array ('emailTitle'=>$safe_emailTitle, 'newContent'=>$safe_newContent, 'id'=>$_POST['id'], 'emailoption'=>$_POST['email_ann']);
  72. $_SESSION['formelements'] = $form_elements;
  73. $form_elements = $_SESSION['formelements'];
  74. $title_to_modify = $form_elements["emailTitle"];
  75. $content_to_modify = $form_elements["newContent"];
  76. $announcement_to_modify = $form_elements["id"];
  77. }
  78. /*
  79. Show/hide user/group form
  80. */
  81. $setting_select_groupusers = true;
  82. if (empty($_POST['To']) and !$_SESSION['select_groupusers']) {
  83. $_SESSION['select_groupusers'] = "hide";
  84. }
  85. $select_groupusers_status=$_SESSION['select_groupusers'];
  86. if (!empty($_POST['To']) and ($select_groupusers_status=="hide")) {
  87. $_SESSION['select_groupusers'] = "show";
  88. }
  89. if (!empty($_POST['To']) and ($select_groupusers_status=="show")) {
  90. $_SESSION['select_groupusers'] = "hide";
  91. }
  92. /* Action handling */
  93. // display the form
  94. if (((!empty($_GET['action']) && $_GET['action'] == 'add') && $_GET['origin'] == "") || (!empty($_GET['action']) && $_GET['action'] == 'edit') || !empty($_POST['To'])) {
  95. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  96. api_not_allowed();
  97. }
  98. $display_form = true;
  99. }
  100. // clear all resources
  101. if ((empty($originalresource) || ($originalresource!=='no')) and (!empty($action) && $action=='add')) {
  102. $_SESSION['formelements']=null;
  103. }
  104. $htmlHeadXtra[] = AnnouncementManager::to_javascript();
  105. $htmlHeadXtra[] = '<script type="text/javascript">
  106. function setFocus(){
  107. $("#emailTitle").focus();
  108. }
  109. $(document).ready(function () {
  110. setFocus();
  111. });
  112. </script>';
  113. /* Filter user/group */
  114. if(!empty($_GET['toolgroup'])){
  115. if($_GET['toolgroup'] == strval(intval($_GET['toolgroup']))){ //check is integer
  116. $toolgroup = $_GET['toolgroup'];
  117. $_SESSION['select_groupusers'] = 'hide';
  118. } else {
  119. $toolgroup = 0;
  120. }
  121. api_session_register("toolgroup");
  122. }
  123. /* Sessions */
  124. $ctok = $_SESSION['sec_token'];
  125. $stok = Security::get_token();
  126. $to = null;
  127. $email_ann = null;
  128. if (!empty($_SESSION['formelements']) and !empty($_GET['originalresource']) and $_GET['originalresource'] == 'no') {
  129. $form_elements = $_SESSION['formelements'];
  130. $title_to_modify = $form_elements['emailTitle'];
  131. $content_to_modify = $form_elements['newContent'];
  132. $announcement_to_modify = $form_elements['id'];
  133. $to = $form_elements['to'];
  134. //load_edit_users('announcement',$announcement_to_modify);
  135. $email_ann = $form_elements['emailoption'];
  136. }
  137. if(!empty($_GET['remind_inactive'])) {
  138. $to[] = 'USER:'.intval($_GET['remind_inactive']);
  139. }
  140. if (!empty($_SESSION['toolgroup'])){
  141. $_clean_toolgroup=intval($_SESSION['toolgroup']);
  142. $group_properties = GroupManager :: get_group_properties($_clean_toolgroup);
  143. $interbreadcrumb[] = array ("url" => "../group/group.php", "name" => get_lang('Groups'));
  144. $interbreadcrumb[] = array ("url"=>"../group/group_space.php?gidReq=".$_clean_toolgroup, "name"=> get_lang('GroupSpace').' '.$group_properties['name']);
  145. }
  146. $announcement_id = intval($_GET['id']);
  147. $message = null;
  148. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  149. //we are not in the learning path
  150. Display::display_header($nameTools,get_lang('Announcements'));
  151. }
  152. if (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  153. /*
  154. Change visibility of announcement
  155. */
  156. // $_GET['isStudentView']<>"false" is added to prevent that the visibility
  157. // is changed after you do the following:
  158. // change visibility -> studentview -> course manager view
  159. if (!isset($_GET['isStudentView']) || $_GET['isStudentView']!='false') {
  160. if (isset($_GET['id']) AND $_GET['id'] AND isset($_GET['action']) AND $_GET['action']=="showhide") {
  161. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  162. api_not_allowed();
  163. }
  164. if (!api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $_GET['id'])) {
  165. if ($ctok == $_GET['sec_token']) {
  166. AnnouncementManager::change_visibility_announcement($_course, $_GET['id']);
  167. $message = get_lang('VisibilityChanged');
  168. }
  169. }
  170. }
  171. }
  172. /*
  173. Delete announcement
  174. */
  175. if (!empty($_GET['action']) && $_GET['action']=='delete' && isset($_GET['id'])) {
  176. $id=intval($_GET['id']);
  177. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  178. api_not_allowed();
  179. }
  180. if (!api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  181. // tooledit : visibility = 2 : only visible for platform administrator
  182. if ($ctok == $_GET['sec_token']) {
  183. AnnouncementManager::delete_announcement($_course, $id);
  184. //delete_added_resource("Ad_Valvas", $delete);
  185. $id = null;
  186. $emailTitle = null;
  187. $newContent = null;
  188. $message = get_lang('AnnouncementDeleted');
  189. }
  190. }
  191. }
  192. //delete attachment file
  193. if (isset($_GET['action']) && $_GET['action'] == 'delete_attachment') {
  194. $id = $_GET['id_attach'];
  195. if ($ctok == $_GET['sec_token']) {
  196. if (api_is_allowed_to_edit()) {
  197. AnnouncementManager::delete_announcement_attachment_file($id);
  198. }
  199. }
  200. }
  201. /*
  202. Delete all announcements
  203. */
  204. if (!empty($_GET['action']) and $_GET['action']=='delete_all') {
  205. if (api_is_allowed_to_edit()) {
  206. AnnouncementManager::delete_all_announcements($_course);
  207. $id = null;
  208. $emailTitle = null;
  209. $newContent = null;
  210. $message = get_lang('AnnouncementDeletedAll');
  211. }
  212. }
  213. /*
  214. Modify announcement
  215. */
  216. if (!empty($_GET['action']) and $_GET['action']=='modify' AND isset($_GET['id'])) {
  217. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  218. api_not_allowed();
  219. }
  220. $display_form = true;
  221. // RETRIEVE THE CONTENT OF THE ANNOUNCEMENT TO MODIFY
  222. $id = intval($_GET['id']);
  223. if (!api_is_course_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
  224. $sql="SELECT * FROM $tbl_announcement WHERE id = '$id'";
  225. $rs = Database::query($sql);
  226. $myrow = Database::fetch_array($rs);
  227. $last_id = $id;
  228. $edit_attachment = AnnouncementManager::edit_announcement_attachment_file($last_id, $_FILES['user_upload'], $file_comment);
  229. if ($myrow) {
  230. $announcement_to_modify = $myrow['id'];
  231. $content_to_modify = $myrow['content'];
  232. $title_to_modify = $myrow['title'];
  233. if ($originalresource!=="no") {
  234. $to=AnnouncementManager::load_edit_users("announcement", $announcement_to_modify);
  235. }
  236. $display_announcement_list = false;
  237. }
  238. if ($to=="everyone" OR !empty($_SESSION['toolgroup'])) {
  239. $_SESSION['select_groupusers']="hide";
  240. } else {
  241. $_SESSION['select_groupusers']="show";
  242. }
  243. }
  244. }
  245. /*
  246. Move announcement up/down
  247. */
  248. if (isset($_GET['sec_token']) && $ctok == $_GET['sec_token']) {
  249. if (!empty($_GET['down'])) {
  250. $thisAnnouncementId = intval($_GET['down']);
  251. $sortDirection = "DESC";
  252. }
  253. if (!empty($_GET['up'])) {
  254. $thisAnnouncementId = intval($_GET['up']);
  255. $sortDirection = "ASC";
  256. }
  257. }
  258. if (!empty($sortDirection)) {
  259. if (!in_array(trim(strtoupper($sortDirection)), array('ASC', 'DESC'))) {
  260. $sortDirection='ASC';
  261. }
  262. $my_sql = "SELECT announcement.id, announcement.display_order " .
  263. "FROM $tbl_announcement announcement, " .
  264. "$tbl_item_property itemproperty " .
  265. "WHERE
  266. announcement.c_id = $course_id AND
  267. itemproperty.c_id = $course_id AND
  268. itemproperty.ref=announcement.id " .
  269. "AND itemproperty.tool='".TOOL_ANNOUNCEMENT."' " .
  270. "AND itemproperty.visibility<>2 " .
  271. "ORDER BY display_order $sortDirection";
  272. $result = Database::query($my_sql);
  273. while (list ($announcementId, $announcementOrder) = Database::fetch_row($result)) {
  274. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER.
  275. // COMMIT ORDER SWAP ON THE DB
  276. if ($thisAnnouncementOrderFound) {
  277. $nextAnnouncementId = $announcementId;
  278. $nextAnnouncementOrder = $announcementOrder;
  279. Database::query("UPDATE $tbl_announcement SET display_order = '$nextAnnouncementOrder' WHERE id = '$thisAnnouncementId'");
  280. Database::query("UPDATE $tbl_announcement SET display_order = '$thisAnnouncementOrder' WHERE id = '$nextAnnouncementId.'");
  281. break;
  282. }
  283. // STEP 1 : FIND THE ORDER OF THE ANNOUNCEMENT
  284. if ($announcementId == $thisAnnouncementId) {
  285. $thisAnnouncementOrder = $announcementOrder;
  286. $thisAnnouncementOrderFound = true;
  287. }
  288. }
  289. // show message
  290. $message = get_lang('AnnouncementMoved');
  291. }
  292. /*
  293. Submit announcement
  294. */
  295. //if (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  296. $emailTitle=(!empty($_POST['emailTitle'])?$safe_emailTitle:'');
  297. $newContent=(!empty($_POST['newContent'])?$safe_newContent:'');
  298. $submitAnnouncement=isset($_POST['submitAnnouncement'])?$_POST['submitAnnouncement']:0;
  299. $id = 0;
  300. if (!empty($_POST['id'])) {
  301. $id=intval($_POST['id']);
  302. }
  303. if ($submitAnnouncement && empty($emailTitle)) {
  304. $error_message = get_lang('TitleIsRequired');
  305. $content_to_modify = $newContent;
  306. } else if ($submitAnnouncement) {
  307. if (isset($id) && $id) {
  308. // there is an Id => the announcement already exists => update mode
  309. if ($ctok == $_POST['sec_token']) {
  310. $file_comment = $_POST['file_comment'];
  311. $file = $_FILES['user_upload'];
  312. $edit_id = AnnouncementManager::edit_announcement($id, $emailTitle, $newContent, $_POST['selectedform'], $file, $file_comment);
  313. /*
  314. if (!$delete) {
  315. update_added_resources("Ad_Valvas", $id);
  316. }*/
  317. $message = get_lang('AnnouncementModified');
  318. }
  319. } else {
  320. //insert mode
  321. if ($ctok == $_POST['sec_token']) {
  322. //if (!$surveyid) {
  323. $result = Database::query("SELECT MAX(display_order) FROM $tbl_announcement WHERE session_id=".api_get_session_id()." OR session_id=0");
  324. list($orderMax) = Database::fetch_row($result);
  325. $order = $orderMax + 1;
  326. $file = $_FILES['user_upload'];
  327. $file_comment = $_POST['file_comment'];
  328. if (!empty($_SESSION['toolgroup'])) {
  329. $insert_id = AnnouncementManager::add_group_announcement($safe_emailTitle,$safe_newContent,$order,array('GROUP:'.$_SESSION['toolgroup']),$_POST['selectedform'],$file,$file_comment);
  330. } else {
  331. $insert_id = AnnouncementManager::add_announcement($safe_emailTitle, $safe_newContent, $order, $_POST['selectedform'], $file, $file_comment);
  332. }
  333. //store_resources($_SESSION['source_type'],$insert_id);
  334. $_SESSION['select_groupusers']="hide";
  335. $message = get_lang('AnnouncementAdded');
  336. /* MAIL FUNCTION */
  337. if ($_POST['email_ann'] && empty($_POST['onlyThoseMails'])) {
  338. $sent_to = AnnouncementManager::sent_to("announcement", $insert_id);
  339. $userlist = $sent_to['users'];
  340. $grouplist = $sent_to['groups'];
  341. // groepen omzetten in users
  342. if ($grouplist) {
  343. $grouplist = "'".implode("', '",$grouplist)."'"; //protect individual elements with surrounding quotes
  344. $sql = "SELECT user_id
  345. FROM $tbl_groupUser gu
  346. WHERE c_id = $course_id AND gu.group_id IN (".$grouplist.")";
  347. $groupMemberResult = Database::query($sql);
  348. if ($groupMemberResult) {
  349. while ($u = Database::fetch_array($groupMemberResult)) {
  350. $userlist [] = $u ['user_id']; // complete the user id list ...
  351. }
  352. }
  353. }
  354. if (is_array($userlist)) {
  355. $userlist = "'".implode("', '", array_unique($userlist) )."'";
  356. // send to the created 'userlist'
  357. $sqlmail = "SELECT user_id, lastname, firstname, email
  358. FROM $tbl_user
  359. WHERE user_id IN (".$userlist.")";
  360. } else if (empty($_POST['not_selected_form'])) {
  361. if(empty($_SESSION['id_session']) || api_get_setting('use_session_mode')=='false') {
  362. // send to everybody
  363. $sqlmail = "SELECT user.user_id, user.email, user.lastname, user.firstname
  364. FROM $tbl_course_user, $tbl_user
  365. WHERE course_code='".Database::escape_string($_course['sysCode'])."'
  366. AND course_rel_user.user_id = user.user_id AND relation_type <>".COURSE_RELATION_TYPE_RRHH." ";
  367. } else {
  368. $sqlmail = "SELECT user.user_id, user.email, user.lastname, user.firstname
  369. FROM $tbl_user
  370. INNER JOIN $tbl_session_course_user
  371. ON $tbl_user.user_id = $tbl_session_course_user.id_user
  372. AND $tbl_session_course_user.course_code = '".$_course['id']."'
  373. AND $tbl_session_course_user.id_session = ".api_get_session_id();
  374. }
  375. }
  376. if ($sqlmail != '') {
  377. $rs_mail = Database::query($sqlmail);
  378. /* Send email one by one to avoid antispam */
  379. $db_name = Database::get_course_table(TABLE_MAIN_SURVEY);
  380. while ($myrow = Database::fetch_array($rs_mail)) {
  381. $emailSubject = "[" . $_course['official_code'] . "] " . $emailTitle;
  382. // intro of the email: receiver name and subject
  383. $mail_body = api_get_person_name($myrow["lastname"], $myrow["firstname"], null, PERSON_NAME_EMAIL_ADDRESS)."<br />\n".stripslashes($emailTitle)."<br />";
  384. // Main part of the email
  385. $mail_body .= trim(stripslashes(AnnouncementManager::parse_content($newContent, api_get_course_id())));
  386. // Signature of email: sender name and course URL after -- line
  387. $mail_body .= "<br />-- <br />";
  388. $mail_body .= api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS)." \n";
  389. $mail_body .= "<br /> \n<a href=\"".api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq()."\">";
  390. $mail_body .= $_course['official_code'].' '.$_course['name'] . "</a>";
  391. $recipient_name = api_get_person_name($myrow["firstname"], $myrow["lastname"], null, PERSON_NAME_EMAIL_ADDRESS);
  392. $mailid = $myrow["email"];
  393. $sender_name = api_get_person_name($_SESSION['_user']['firstName'], $_SESSION['_user']['lastName'], null, PERSON_NAME_EMAIL_ADDRESS);
  394. $sender_email = $_SESSION['_user']['mail'];
  395. // send attachment file
  396. $data_file = array();
  397. $sql = 'SELECT path, filename FROM '.$tbl_announcement_attachment.' WHERE announcement_id = "'.$insert_id.'"';
  398. $rs_attach = Database::query($sql);
  399. if (Database::num_rows($rs_attach) > 0) {
  400. $row_attach = Database::fetch_array($rs_attach);
  401. $path_attach = api_get_path(SYS_COURSE_PATH).$_course['path'].'/upload/announcements/'.$row_attach['path'];
  402. $filename_attach = $row_attach['filename'];
  403. $data_file = array('path' => $path_attach,'filename' => $filename_attach);
  404. }
  405. @api_mail_html($recipient_name, $mailid, stripslashes($emailSubject), $mail_body, $sender_name, $sender_email, null, $data_file, true);
  406. //@todo who uses the $table_reminder??
  407. if ($_REQUEST['reminder']=="1") {
  408. $time=getdate();
  409. $time = $time['yday'];
  410. $time = $time+7;
  411. $sql="INSERT INTO $table_reminder(sid,db_name,email,subject,content,reminder_choice,reminder_time,avail_till) values('$surveyid','$db_name','$mailid','".addslashes($emailSubject)."','".addslashes($mail_body)."','1','$time','$end_date')";
  412. Database::query($sql);
  413. } else if ($_REQUEST['reminder']=="2") {
  414. $time=getdate();
  415. $time = $time['yday'];
  416. $time = $time+14;
  417. $sql="INSERT INTO $table_reminder(sid,db_name,email,subject,content,reminder_choice,reminder_time,avail_till) values('$surveyid','$db_name','$mailid','".addslashes($emailSubject)."','".addslashes($mail_body)."','1','$time','$end_date')";
  418. Database::query($sql);
  419. } else if ($_REQUEST['reminder']=="3") {
  420. $time=getdate();
  421. $time = $time['yday'];
  422. $time = $time+30;
  423. $sql="INSERT INTO $table_reminder(sid,db_name,email,subject,content,reminder_choice,reminder_time,avail_till) values('$surveyid','$db_name','$mailid','".addslashes($emailSubject)."','".addslashes($mail_body)."','1','$time','$end_date')";
  424. Database::query($sql);
  425. }
  426. }
  427. AnnouncementManager::update_mail_sent($insert_id);
  428. $message = $added_and_sent;
  429. }
  430. } // $email_ann*/
  431. } // end condition token
  432. } // isset
  433. // UNSET VARIABLES
  434. unset($form_elements);
  435. $_SESSION['formelements']=null;
  436. $newContent = null;
  437. $emailTitle = null;
  438. unset($emailTitle);
  439. unset($newContent);
  440. unset($content_to_modify);
  441. unset($title_to_modify);
  442. } // if $submit Announcement
  443. }
  444. /* Tool introduction */
  445. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  446. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  447. }
  448. /* DISPLAY LEFT COLUMN */
  449. //condition for the session
  450. $session_id = api_get_session_id();
  451. $condition_session = api_get_session_condition($session_id,true,true);
  452. if(api_is_allowed_to_edit(false,true)) {
  453. // check teacher status
  454. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  455. if (api_get_group_id() == 0) {
  456. $group_condition = "";
  457. } else {
  458. $group_condition = "AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0)";
  459. }
  460. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  461. FROM $tbl_announcement announcement, $tbl_item_property ip
  462. WHERE
  463. announcement.c_id = $course_id AND
  464. ip.c_id = $course_id AND
  465. announcement.id = ip.ref AND
  466. ip.tool='announcement' AND
  467. ip.visibility<>'2'
  468. $group_condition
  469. $condition_session
  470. GROUP BY ip.ref
  471. ORDER BY display_order DESC
  472. LIMIT 0,$maximum";
  473. }
  474. } else {
  475. // students only get to see the visible announcements
  476. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  477. $group_memberships=GroupManager::get_group_ids($_course['real_id'], $_user['user_id']);
  478. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  479. if (api_get_group_id() == 0) {
  480. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ( ip.to_user_id='".$_user['user_id']."'" .
  481. "OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))) ";
  482. } else {
  483. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."'
  484. OR ip.to_group_id IN (0, ".api_get_group_id()."))";
  485. }
  486. } else {
  487. if (api_get_group_id() == 0) {
  488. $cond_user_id = " AND ( ip.to_user_id='".$_user['user_id']."'" .
  489. "OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ";
  490. } else {
  491. $cond_user_id = " AND ( ip.to_user_id='".$_user['user_id']."'" .
  492. "OR ip.to_group_id IN (0, ".api_get_group_id().")) ";
  493. }
  494. }
  495. // the user is member of several groups => display personal announcements AND his group announcements AND the general announcements
  496. if (is_array($group_memberships) && count($group_memberships)>0) {
  497. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  498. FROM $tbl_announcement announcement, $tbl_item_property ip
  499. WHERE
  500. announcement.c_id = $course_id AND
  501. ip.c_id = $course_id AND
  502. announcement.id = ip.ref AND
  503. ip.tool='announcement'
  504. AND ip.visibility='1'
  505. $cond_user_id
  506. $condition_session
  507. GROUP BY ip.ref
  508. ORDER BY display_order DESC
  509. LIMIT 0,$maximum";
  510. } else {
  511. // the user is not member of any group
  512. // this is an identified user => show the general announcements AND his personal announcements
  513. if ($_user['user_id']) {
  514. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  515. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ( ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0')) ";
  516. } else {
  517. $cond_user_id = " AND ( ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0') ";
  518. }
  519. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  520. FROM $tbl_announcement announcement, $tbl_item_property ip
  521. WHERE
  522. announcement.c_id = $course_id AND
  523. ip.c_id = $course_id AND
  524. announcement.id = ip.ref
  525. AND ip.tool='announcement'
  526. AND ip.visibility='1'
  527. $cond_user_id
  528. $condition_session
  529. GROUP BY ip.ref
  530. ORDER BY display_order DESC
  531. LIMIT 0,$maximum";
  532. } else {
  533. if (api_get_course_setting('allow_user_edit_announcement')) {
  534. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0') ";
  535. } else {
  536. $cond_user_id = " AND ip.to_group_id='0' ";
  537. }
  538. // the user is not identiefied => show only the general announcements
  539. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  540. FROM $tbl_announcement announcement, $tbl_item_property ip
  541. WHERE
  542. announcement.c_id = $course_id AND
  543. ip.c_id = $course_id AND
  544. announcement.id = ip.ref
  545. AND ip.tool='announcement'
  546. AND ip.visibility='1'
  547. AND ip.to_group_id='0'
  548. $condition_session
  549. GROUP BY ip.ref
  550. ORDER BY display_order DESC
  551. LIMIT 0,$maximum";
  552. }
  553. }
  554. }
  555. }
  556. $result = Database::query($sql);
  557. $announcement_number = Database::num_rows($result);
  558. /*
  559. ADD ANNOUNCEMENT / DELETE ALL
  560. */
  561. $show_actions = false;
  562. if ((api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) and (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath')) {
  563. echo '<div class="actions">';
  564. if (isset($_GET['action']) && in_array($_GET['action'], array('add', 'modify','view'))) {
  565. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".(empty($_GET['origin'])?'':$_GET['origin'])."'>".Display::return_icon('back.png',get_lang('Back'),'','32')."</a>";
  566. } else {
  567. echo "<a href='".api_get_self()."?".api_get_cidreq()."&action=add&origin=".(empty($_GET['origin'])?'':$_GET['origin'])."'>".Display::return_icon('new_announce.png',get_lang('AddAnnouncement'),'','32')."</a>";
  568. }
  569. $show_actions = true;
  570. } else {
  571. if (in_array($_GET['action'], array('view'))) {
  572. echo '<div class="actions">';
  573. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".(empty($_GET['origin'])?'':$_GET['origin'])."'>".Display::return_icon('back.png',get_lang('Back'),'','32')."</a>";
  574. echo '</div>';
  575. }
  576. }
  577. if (api_is_allowed_to_edit() && $announcement_number > 1) {
  578. if (api_get_group_id() == 0 ) {
  579. if (!$show_actions)
  580. echo '<div class="actions">';
  581. if (!in_array($_GET['action'], array('add', 'modify','view')))
  582. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete_all\" onclick=\"javascript:if(!confirm('".get_lang("ConfirmYourChoice")."')) return false;\">".Display::return_icon('delete_announce.png',get_lang('AnnouncementDeleteAll'),'','32')."</a>";
  583. } // if announcementNumber > 1
  584. }
  585. if ($show_actions)
  586. echo '</div>';
  587. // ANNOUNCEMENTS LIST
  588. if ($message) {
  589. Display::display_confirmation_message($message);
  590. $display_announcement_list = true;
  591. $display_form = false;
  592. }
  593. if (!empty($error_message)) {
  594. Display::display_error_message($error_message);
  595. $display_announcement_list = false;
  596. $display_form = true;
  597. }
  598. /*
  599. DISPLAY FORM
  600. */
  601. if ($display_form) {
  602. $content_to_modify = stripslashes($content_to_modify);
  603. $title_to_modify = stripslashes($title_to_modify);
  604. // DISPLAY ADD ANNOUNCEMENT COMMAND
  605. //echo '<form method="post" name="f1" enctype = "multipart/form-data" action="'.api_get_self().'?publish_survey='.Security::remove_XSS($surveyid).'&id='.Security::remove_XSS($_GET['id']).'&db_name='.$db_name.'&cidReq='.Security::remove_XSS($_GET['cidReq']).'" style="margin:0px;">';
  606. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  607. echo '<form method="post" name="f1" enctype = "multipart/form-data" action="'.api_get_self().'?id='.$id.'&'.api_get_cidreq().'" style="margin:0px;">';
  608. if (empty($_GET['id'])) {
  609. $form_name = get_lang('AddAnnouncement');
  610. } else {
  611. $form_name = get_lang('ModifyAnnouncement');
  612. }
  613. echo '<div class="row"><div class="form_header">'.$form_name.'</div></div>';
  614. //this variable defines if the course administrator can send a message to a specific user / group or not
  615. if (empty($_SESSION['toolgroup'])) {
  616. echo ' <div class="row">
  617. <div class="label">'.
  618. Display::return_icon('group.png', get_lang('ModifyRecipientList'), array ('align' => 'absmiddle'),22).'<a href="#" onclick="if(document.getElementById(\'recipient_list\').style.display==\'none\') document.getElementById(\'recipient_list\').style.display=\'block\'; else document.getElementById(\'recipient_list\').style.display=\'none\';">'.get_lang('SentTo').'</a>
  619. </div>
  620. <div class="formw">';
  621. if (isset($_GET['id']) && is_array($to)) {
  622. echo '&nbsp;';
  623. } elseif (isset($_GET['remind_inactive'])) {
  624. $email_ann = '1';
  625. $_SESSION['select_groupusers']="show";
  626. $content_to_modify = sprintf(get_lang('RemindInactiveLearnersMailContent'), api_get_setting('siteName'), 7);
  627. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'));
  628. } elseif (isset($_GET['remindallinactives']) && $_GET['remindallinactives']=='true') {
  629. // 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
  630. $since = isset($_GET['since']) ? intval($_GET['since']) : 6;
  631. // getting the users who have to be reminded
  632. $to = Tracking :: get_inactives_students_in_course($_course['id'],$since, api_get_session_id());
  633. // setting the variables for the form elements: the users who need to receive the message
  634. foreach($to as &$user) {
  635. $user = 'USER:'.$user;
  636. }
  637. // setting the variables for the form elements: the 'visible to' form element has to be expanded
  638. $_SESSION['select_groupusers']="show";
  639. // setting the variables for the form elements: the message has to be sent by email
  640. $email_ann = '1';
  641. // setting the variables for the form elements: the title of the email
  642. //$title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'),' > ',$_course['name']);
  643. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'));
  644. // setting the variables for the form elements: the message of the email
  645. //$content_to_modify = sprintf(get_lang('RemindInactiveLearnersMailContent'),api_get_setting('siteName'),' > ',$_course['name'],$since);
  646. $content_to_modify = sprintf(get_lang('RemindInactiveLearnersMailContent'),api_get_setting('siteName'),$since);
  647. // when we want to remind the users who have never been active then we have a different subject and content for the announcement
  648. if ($_GET['since'] == 'never') {
  649. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'));
  650. $content_to_modify = get_lang('YourAccountIsActiveYouCanLoginAndCheckYourCourses');
  651. }
  652. } else {
  653. echo get_lang('Everybody');
  654. }
  655. AnnouncementManager::show_to_form($to);
  656. echo ' </div>
  657. </div>';
  658. if (!isset($announcement_to_modify) ) $announcement_to_modify ='';
  659. if ($announcement_to_modify=='') {
  660. ($email_ann=='1')?$checked='checked':$checked='';
  661. echo ' <div class="row">
  662. <div class="label">
  663. </div>
  664. <div class="formw">
  665. <input id="email_ann" class="checkbox" type="checkbox" value="1" name="email_ann" checked>
  666. <label for="email_ann">'.get_lang('EmailOption').'</label>
  667. </div>
  668. </div>';
  669. }
  670. } else {
  671. if (!isset($announcement_to_modify) ) {
  672. $announcement_to_modify ="";
  673. }
  674. if ($announcement_to_modify=='') {
  675. ($email_ann=='1' || !empty($surveyid))?$checked='checked':$checked='';
  676. echo '<div class="row">
  677. <div class="label">
  678. </div>
  679. <div class="formw">
  680. <input class="checkbox" type="checkbox" value="1" name="email_ann" '.$checked.'>
  681. '.get_lang('EmailOption').': '.get_lang('MyGroup').'&nbsp;&nbsp;
  682. <a href="#" onclick="if(document.getElementById(\'recipient_list\').style.display==\'none\') document.getElementById(\'recipient_list\').style.display=\'block\'; else document.getElementById(\'recipient_list\').style.display=\'none\';">'.get_lang('ModifyRecipientList').'</a>';
  683. AnnouncementManager::show_to_form_group($_SESSION['toolgroup']);
  684. echo '</div></div>';
  685. }
  686. }
  687. // the announcement title
  688. echo ' <div class="row">
  689. <div id="msg_error" style="display:none;color:red;margin-left:20%"></div>
  690. <div class="label">
  691. <span class="form_required">*</span> '.get_lang('EmailTitle').'
  692. </div>
  693. <div class="formw">
  694. <input type="text" id="emailTitle" name="emailTitle" value="'.Security::remove_XSS($title_to_modify).'" size="60">
  695. </div>
  696. </div>';
  697. unset($title_to_modify);
  698. $title_to_modify = null;
  699. if (!isset($announcement_to_modify) ) $announcement_to_modify ="";
  700. if (!isset($content_to_modify) ) $content_to_modify ="";
  701. if (!isset($title_to_modify)) $title_to_modify = "";
  702. echo '<input type="hidden" name="id" value="'.$announcement_to_modify.'" />';
  703. $oFCKeditor = new FCKeditor('newContent') ;
  704. $oFCKeditor->Width = '100%';
  705. $oFCKeditor->Height = '300';
  706. if(!api_is_allowed_to_edit()) {
  707. $oFCKeditor->ToolbarSet = "AnnouncementsStudent";
  708. } else {
  709. $oFCKeditor->ToolbarSet = "Announcements";
  710. }
  711. $oFCKeditor->Value = $content_to_modify;
  712. echo '<div class="row"><div class="formw">';
  713. echo Display::display_normal_message(get_lang('Tags').' <br /><br />'.implode('<br />', AnnouncementManager::get_tags()), false);
  714. echo $oFCKeditor->CreateHtml();
  715. echo '</div></div>';
  716. //File attachment
  717. echo ' <div class="row">
  718. <div class="label">
  719. </div>
  720. <div class="formw">
  721. <a href="javascript://" onclick="return plus_attachment();"><span id="plus"><img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AddAnAttachment').'</span></a>
  722. <br />
  723. <table id="options" style="display: none;">
  724. <tr>
  725. <td colspan="2">
  726. <label for="file_name">'.get_lang('FileName').'&nbsp;</label>
  727. <input type="file" name="user_upload"/>
  728. </td>
  729. </tr>
  730. <tr>
  731. <td colspan="2">
  732. <label for="comment">'.get_lang('FileComment').'</label><br />
  733. <textarea name="file_comment" rows ="4" cols = "34" ></textarea>
  734. </td>
  735. </tr>
  736. </table>
  737. </div>
  738. </div>';
  739. echo'<br />';
  740. echo '<div class="row"><div class="formw">';
  741. if (empty($_SESSION['toolgroup'])) {
  742. echo '<input type="hidden" name="submitAnnouncement" value="OK">';
  743. echo '<input type="hidden" name="sec_token" value="'.$stok.'" />';
  744. echo '<button class="save" type="button" value="'.' '.get_lang('Send').' '.'" onclick="selectAll(this.form.elements[3],true)" >'.get_lang('ButtonPublishAnnouncement').'</button><br /><br />';
  745. } else {
  746. echo '<input type="hidden" name="submitAnnouncement" value="OK">';
  747. echo '<input type="hidden" name="sec_token" value="'.$stok.'" />';
  748. echo '<button class="save" type="button" value="'.' '.get_lang('Send').' '.'" onclick="selectAll(this.form.elements[4],true)" >'.get_lang('ButtonPublishAnnouncement').'</button><br /><br />';
  749. }
  750. echo '</div></div>';
  751. echo '</form><br />';
  752. if ((isset($_GET['action']) && isset($_GET['id']) && is_array($to))||isset($_GET['remindallinactives'])||isset($_GET['remind_inactive'])) {
  753. echo '<script>document.getElementById(\'recipient_list\').style.display=\'block\';</script>';
  754. }
  755. } // displayform
  756. /*
  757. DISPLAY ANNOUNCEMENT LIST
  758. */
  759. $course_id = api_get_course_int_id();
  760. //if ($display_announcement_list && !$surveyid) {
  761. if ($display_announcement_list) {
  762. // by default we use the id of the current user. The course administrator can see the announcement of other users by using the user / group filter
  763. //$user_id=$_user['user_id'];
  764. if (isset($_SESSION['user'])) {
  765. //$user_id=$_SESSION['user'];
  766. }
  767. $user_id = api_get_user_id();
  768. if (isset($_SESSION['group'])) {
  769. //$group_id=$_SESSION['group'];
  770. }
  771. $group_id = api_get_group_id();
  772. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  773. //$is_group_member = GroupManager :: is_tutor(api_get_user_id());
  774. if (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  775. // A.1. you are a course admin with a USER filter
  776. // => see only the messages of this specific user + the messages of the group (s)he is member of.
  777. if (!empty($_SESSION['user'])) {
  778. if (is_array($group_memberships) && count($group_memberships)>0) {
  779. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  780. FROM $tbl_announcement announcement, $tbl_item_property ip
  781. WHERE announcement.c_id = $course_id AND
  782. ip.c_id = $course_id AND
  783. announcement.id = ip.ref AND
  784. ip.tool = 'announcement' AND
  785. (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  786. $condition_session
  787. ORDER BY display_order DESC";
  788. } else {
  789. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  790. FROM $tbl_announcement announcement, $tbl_item_property ip
  791. WHERE announcement.c_id = $course_id AND
  792. ip.c_id = $course_id AND
  793. announcement.id = ip.ref AND
  794. ip.tool ='announcement' AND
  795. (ip.to_user_id = $user_id OR ip.to_group_id='0') AND
  796. ip.visibility='1'
  797. $condition_session
  798. ORDER BY display_order DESC";
  799. }
  800. } elseif (api_get_group_id() != 0 ) {
  801. // A.2. you are a course admin with a GROUP filter
  802. // => see only the messages of this specific group
  803. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  804. FROM $tbl_announcement announcement, $tbl_item_property ip
  805. WHERE announcement.c_id = $course_id AND
  806. ip.c_id = $course_id AND
  807. announcement.id = ip.ref
  808. AND ip.tool='announcement'
  809. AND ip.visibility<>'2'
  810. AND (ip.to_group_id=$group_id OR ip.to_group_id='0')
  811. $condition_session
  812. GROUP BY ip.ref
  813. ORDER BY display_order DESC";
  814. } else {
  815. // A.3 you are a course admin without any group or user filter
  816. // A.3.a you are a course admin without user or group filter but WITH studentview
  817. // => see all the messages of all the users and groups without editing possibilities
  818. if (isset($isStudentView) and $isStudentView=="true") {
  819. $sql="SELECT
  820. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  821. FROM $tbl_announcement announcement, $tbl_item_property ip
  822. WHERE announcement.c_id = $course_id AND
  823. ip.c_id = $course_id AND
  824. announcement.id = ip.ref
  825. AND ip.tool='announcement'
  826. AND ip.visibility='1'
  827. $condition_session
  828. GROUP BY ip.ref
  829. ORDER BY display_order DESC";
  830. } else {
  831. // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view)
  832. // => see all the messages of all the users and groups with editing possibilities
  833. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  834. FROM $tbl_announcement announcement, $tbl_item_property ip
  835. WHERE announcement.c_id = $course_id AND
  836. ip.c_id = $course_id AND
  837. announcement.id = ip.ref
  838. AND ip.tool='announcement'
  839. AND (ip.visibility='0' or ip.visibility='1')
  840. $condition_session
  841. GROUP BY ip.ref
  842. ORDER BY display_order DESC";
  843. }
  844. }
  845. } else {
  846. //STUDENT
  847. if (is_array($group_memberships) && count($group_memberships)>0) {
  848. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  849. if (api_get_group_id() == 0) {
  850. //No group
  851. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ( ip.to_user_id='".$_user['user_id']."'" .
  852. " OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))) ";
  853. } else {
  854. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."'
  855. OR ip.to_group_id IN (0, ".api_get_group_id()."))";
  856. }
  857. //$cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") )) ";
  858. } else {
  859. if (api_get_group_id() == 0) {
  860. $cond_user_id = " AND (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ";
  861. } else {
  862. $cond_user_id = " AND (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".api_get_group_id()."))";
  863. }
  864. }
  865. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  866. FROM $tbl_announcement announcement, $tbl_item_property ip
  867. WHERE announcement.c_id = $course_id AND
  868. ip.c_id = $course_id AND
  869. announcement.id = ip.ref
  870. AND ip.tool='announcement'
  871. $cond_user_id
  872. $condition_session
  873. AND ip.visibility='1'
  874. ORDER BY display_order DESC";
  875. } else {
  876. if ($_user['user_id']) {
  877. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  878. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR (ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0')) ";
  879. } else {
  880. $cond_user_id = " AND (ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0') ";
  881. }
  882. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  883. FROM $tbl_announcement announcement, $tbl_item_property ip
  884. WHERE
  885. announcement.c_id = $course_id AND
  886. ip.c_id = $course_id AND
  887. announcement.id = ip.ref AND
  888. ip.tool='announcement'
  889. $cond_user_id
  890. $condition_session
  891. AND ip.visibility='1'
  892. AND announcement.session_id IN(0,".api_get_session_id().")
  893. ORDER BY display_order DESC";
  894. } else {
  895. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  896. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' ) ";
  897. } else {
  898. $cond_user_id = " AND ip.to_group_id='0' ";
  899. }
  900. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  901. FROM $tbl_announcement announcement, $tbl_item_property ip
  902. WHERE
  903. announcement.c_id = $course_id AND
  904. ip.c_id = $course_id AND
  905. announcement.id = ip.ref
  906. AND ip.tool='announcement'
  907. $cond_user_id
  908. $condition_session
  909. AND ip.visibility='1'
  910. AND announcement.session_id IN(0,".api_get_session_id().")";
  911. }
  912. }
  913. }
  914. $result = Database::query($sql);
  915. $num_rows = Database::num_rows($result);
  916. // DISPLAY: NO ITEMS
  917. if (!isset($_GET['action']) || !in_array($_GET['action'], array('add', 'modify','view')))
  918. if ($num_rows == 0) {
  919. Display::display_warning_message(get_lang('NoAnnouncements'));
  920. } else {
  921. $iterator = 1;
  922. $bottomAnnouncement = $announcement_number;
  923. echo '<table width="100%" class="data_table">';
  924. $ths = Display::tag('th', get_lang('Title'));
  925. $ths .= Display::tag('th', get_lang('By') );
  926. $ths .= Display::tag('th', get_lang('LastUpdateDate') );
  927. if (api_is_allowed_to_edit(false,true) OR (api_is_course_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT,$myrow['id']))
  928. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  929. $ths .= Display::tag('th', get_lang('Modify'));
  930. }
  931. echo Display::tag('tr', $ths);
  932. $displayed = array();
  933. while ($myrow = Database::fetch_array($result, 'ASSOC')) {
  934. if (!in_array($myrow['id'], $displayed)) {
  935. $sent_to_icon = '';
  936. // the email icon
  937. if ($myrow['email_sent'] == '1') {
  938. $sent_to_icon = ' '.Display::return_icon('email.gif', get_lang('AnnounceSentByEmail'));
  939. }
  940. $title = $myrow['title'].$sent_to_icon;
  941. /* DATE */
  942. $last_post_datetime = $myrow['end_date'];
  943. // the styles
  944. if ($myrow['visibility'] == '0') {
  945. $style='invisible';
  946. } else {
  947. $style = '';
  948. }
  949. echo "<tr>";
  950. // show attachment list
  951. $attachment_list = array();
  952. $attachment_list = AnnouncementManager::get_attachment($myrow['id']);
  953. $attachment = '';
  954. $attachment_icon = '';
  955. if (count($attachment_list)>0) {
  956. $attachment_icon = ' '.Display::return_icon('attachment.gif',get_lang('Attachment'));
  957. }
  958. /* TITLE */
  959. $title = Display::url($title.$attachment_icon, '?action=view&id='.$myrow['id']);
  960. echo Display::tag('td', Security::remove_XSS($title), array('class' => $style));
  961. $user_info = api_get_user_info($myrow['insert_user_id']);
  962. echo Display::tag('td', api_get_person_name($user_info['firstName'], $user_info['lastName']).' ('.$user_info['username'].')');
  963. echo Display::tag('td', api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG));
  964. // 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
  965. $modify_icons = '';
  966. if (api_is_allowed_to_edit(false,true) OR (api_is_course_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $myrow['id']))
  967. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  968. $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$myrow['id']."\">".Display::return_icon('edit.png', get_lang('Edit'),'',22)."</a>";
  969. if ($myrow['visibility']==1) {
  970. $image_visibility="visible";
  971. $alt_visibility=get_lang('Hide');
  972. } else {
  973. $image_visibility="invisible";
  974. $alt_visibility=get_lang('Visible');
  975. }
  976. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&origin=".(!empty($_GET['origin'])?Security::remove_XSS($_GET['origin']):'')."&action=showhide&id=".$myrow['id']."&sec_token=".$stok."\">".
  977. Display::return_icon($image_visibility.'.png', $alt_visibility,'',22)."</a>";
  978. // DISPLAY MOVE UP COMMAND only if it is not the top announcement
  979. if ($iterator != 1) {
  980. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&up=".$myrow["id"]."&sec_token=".$stok."\">".Display::return_icon('up.gif', get_lang('Up'))."</a>";
  981. } else {
  982. $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up'));
  983. }
  984. if ($iterator < $bottomAnnouncement) {
  985. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&down=".$myrow["id"]."&sec_token=".$stok."\">".Display::return_icon('down.gif', get_lang('Down'))."</a>";
  986. } else {
  987. $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down'));
  988. }
  989. if (api_is_allowed_to_edit(false,true)) {
  990. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete&id=".$myrow['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."')) return false;\">".
  991. Display::return_icon('delete.png', get_lang('Delete'),'',22).
  992. "</a>";
  993. }
  994. $iterator ++;
  995. echo Display::tag('td', $modify_icons);
  996. }
  997. echo "</tr>";
  998. }
  999. $displayed[]=$myrow['id'];
  1000. } // end while
  1001. echo "</table>";
  1002. }
  1003. } // end: if ($displayAnnoucementList)
  1004. if (isset($_GET['action']) && $_GET['action'] == 'view') {
  1005. AnnouncementManager::display_announcement($announcement_id);
  1006. }
  1007. /* FOOTER */
  1008. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  1009. //we are not in learnpath tool
  1010. Display::display_footer();
  1011. }