announcements.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 c_id = $course_id AND 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 c_id = $course_id AND id = '$thisAnnouncementId'");
  280. Database::query("UPDATE $tbl_announcement SET display_order = '$thisAnnouncementOrder' WHERE c_id = $course_id AND 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. $sql = "SELECT MAX(display_order) FROM $tbl_announcement WHERE c_id = $course_id AND (session_id=".api_get_session_id()." OR session_id=0)";
  324. $result = Database::query($sql);
  325. list($orderMax) = Database::fetch_row($result);
  326. $order = $orderMax + 1;
  327. $file = $_FILES['user_upload'];
  328. $file_comment = $_POST['file_comment'];
  329. if (!empty($_SESSION['toolgroup'])) {
  330. $insert_id = AnnouncementManager::add_group_announcement($safe_emailTitle,$safe_newContent,$order,array('GROUP:'.$_SESSION['toolgroup']),$_POST['selectedform'],$file,$file_comment);
  331. } else {
  332. $insert_id = AnnouncementManager::add_announcement($safe_emailTitle, $safe_newContent, $order, $_POST['selectedform'], $file, $file_comment);
  333. }
  334. //store_resources($_SESSION['source_type'],$insert_id);
  335. $_SESSION['select_groupusers']="hide";
  336. $message = get_lang('AnnouncementAdded');
  337. /* MAIL FUNCTION */
  338. if ($_POST['email_ann'] && empty($_POST['onlyThoseMails'])) {
  339. $sent_to = AnnouncementManager::sent_to("announcement", $insert_id);
  340. $userlist = $sent_to['users'];
  341. $grouplist = $sent_to['groups'];
  342. // groepen omzetten in users
  343. if ($grouplist) {
  344. $grouplist = "'".implode("', '",$grouplist)."'"; //protect individual elements with surrounding quotes
  345. $sql = "SELECT user_id
  346. FROM $tbl_groupUser gu
  347. WHERE c_id = $course_id AND gu.group_id IN (".$grouplist.")";
  348. $groupMemberResult = Database::query($sql);
  349. if ($groupMemberResult) {
  350. while ($u = Database::fetch_array($groupMemberResult)) {
  351. $userlist [] = $u ['user_id']; // complete the user id list ...
  352. }
  353. }
  354. }
  355. if (is_array($userlist)) {
  356. $userlist = "'".implode("', '", array_unique($userlist) )."'";
  357. // send to the created 'userlist'
  358. $sqlmail = "SELECT user_id, lastname, firstname, email
  359. FROM $tbl_user
  360. WHERE active = 1 AND user_id IN (".$userlist.")";
  361. } else if (empty($_POST['not_selected_form'])) {
  362. if(empty($_SESSION['id_session']) || api_get_setting('use_session_mode')=='false') {
  363. // send to everybody
  364. $sqlmail = "SELECT user.user_id, user.email, user.lastname, user.firstname
  365. FROM $tbl_course_user, $tbl_user
  366. WHERE active = 1 AND
  367. course_code='".Database::escape_string($_course['sysCode'])."' AND
  368. course_rel_user.user_id = user.user_id AND
  369. relation_type <>".COURSE_RELATION_TYPE_RRHH." ";
  370. } else {
  371. $sqlmail = "SELECT user.user_id, user.email, user.lastname, user.firstname
  372. FROM $tbl_user INNER JOIN $tbl_session_course_user
  373. ON $tbl_user.user_id = $tbl_session_course_user.id_user AND
  374. active = 1 AND
  375. $tbl_session_course_user.course_code = '".$_course['id']."' AND
  376. $tbl_session_course_user.id_session = ".api_get_session_id();
  377. }
  378. }
  379. if ($sqlmail != '') {
  380. $rs_mail = Database::query($sqlmail);
  381. /* Send email one by one to avoid antispam */
  382. $db_name = Database::get_course_table(TABLE_MAIN_SURVEY);
  383. while ($myrow = Database::fetch_array($rs_mail)) {
  384. $emailSubject = "[" . $_course['official_code'] . "] " . $emailTitle;
  385. // intro of the email: receiver name and subject
  386. $mail_body = api_get_person_name($myrow["lastname"], $myrow["firstname"], null, PERSON_NAME_EMAIL_ADDRESS)."<br />\n".stripslashes($emailTitle)."<br />";
  387. // Main part of the email
  388. $mail_body .= trim(stripslashes(AnnouncementManager::parse_content($newContent, api_get_course_id())));
  389. // Signature of email: sender name and course URL after -- line
  390. $mail_body .= "<br />-- <br />";
  391. $mail_body .= api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS)." \n";
  392. $mail_body .= "<br /> \n<a href=\"".api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq()."\">";
  393. $mail_body .= $_course['official_code'].' '.$_course['name'] . "</a>";
  394. $recipient_name = api_get_person_name($myrow["firstname"], $myrow["lastname"], null, PERSON_NAME_EMAIL_ADDRESS);
  395. $mailid = $myrow["email"];
  396. $sender_name = api_get_person_name($_SESSION['_user']['firstName'], $_SESSION['_user']['lastName'], null, PERSON_NAME_EMAIL_ADDRESS);
  397. $sender_email = $_SESSION['_user']['mail'];
  398. // send attachment file
  399. $data_file = array();
  400. $sql = 'SELECT path, filename FROM '.$tbl_announcement_attachment.' WHERE c_id = '.$course_id.' AND announcement_id = "'.$insert_id.'"';
  401. $rs_attach = Database::query($sql);
  402. if (Database::num_rows($rs_attach) > 0) {
  403. $row_attach = Database::fetch_array($rs_attach);
  404. $path_attach = api_get_path(SYS_COURSE_PATH).$_course['path'].'/upload/announcements/'.$row_attach['path'];
  405. $filename_attach = $row_attach['filename'];
  406. $data_file = array('path' => $path_attach,'filename' => $filename_attach);
  407. }
  408. @api_mail_html($recipient_name, $mailid, stripslashes($emailSubject), $mail_body, $sender_name, $sender_email, null, $data_file, true);
  409. //@todo who uses the $table_reminder??
  410. /*
  411. if ($_REQUEST['reminder']=="1") {
  412. $time=getdate();
  413. $time = $time['yday'];
  414. $time = $time+7;
  415. $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')";
  416. Database::query($sql);
  417. } else if ($_REQUEST['reminder']=="2") {
  418. $time=getdate();
  419. $time = $time['yday'];
  420. $time = $time+14;
  421. $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')";
  422. Database::query($sql);
  423. } else if ($_REQUEST['reminder']=="3") {
  424. $time=getdate();
  425. $time = $time['yday'];
  426. $time = $time+30;
  427. $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')";
  428. Database::query($sql);
  429. }*/
  430. }
  431. AnnouncementManager::update_mail_sent($insert_id);
  432. $message = $added_and_sent;
  433. }
  434. } // $email_ann*/
  435. } // end condition token
  436. } // isset
  437. // UNSET VARIABLES
  438. unset($form_elements);
  439. $_SESSION['formelements']=null;
  440. $newContent = null;
  441. $emailTitle = null;
  442. unset($emailTitle);
  443. unset($newContent);
  444. unset($content_to_modify);
  445. unset($title_to_modify);
  446. } // if $submit Announcement
  447. }
  448. /* Tool introduction */
  449. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  450. Display::display_introduction_section(TOOL_ANNOUNCEMENT);
  451. }
  452. /* DISPLAY LEFT COLUMN */
  453. //condition for the session
  454. $session_id = api_get_session_id();
  455. $condition_session = api_get_session_condition($session_id,true,true);
  456. if(api_is_allowed_to_edit(false,true)) {
  457. // check teacher status
  458. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  459. if (api_get_group_id() == 0) {
  460. $group_condition = "";
  461. } else {
  462. $group_condition = "AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0)";
  463. }
  464. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  465. FROM $tbl_announcement announcement, $tbl_item_property ip
  466. WHERE
  467. announcement.c_id = $course_id AND
  468. ip.c_id = $course_id AND
  469. announcement.id = ip.ref AND
  470. ip.tool='announcement' AND
  471. ip.visibility<>'2'
  472. $group_condition
  473. $condition_session
  474. GROUP BY ip.ref
  475. ORDER BY display_order DESC
  476. LIMIT 0,$maximum";
  477. }
  478. } else {
  479. // students only get to see the visible announcements
  480. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  481. $group_memberships=GroupManager::get_group_ids($_course['real_id'], $_user['user_id']);
  482. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  483. if (api_get_group_id() == 0) {
  484. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ( ip.to_user_id='".$_user['user_id']."'" .
  485. "OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))) ";
  486. } else {
  487. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."'
  488. OR ip.to_group_id IN (0, ".api_get_group_id()."))";
  489. }
  490. } else {
  491. if (api_get_group_id() == 0) {
  492. $cond_user_id = " AND ( ip.to_user_id='".$_user['user_id']."'" .
  493. "OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ";
  494. } else {
  495. $cond_user_id = " AND ( ip.to_user_id='".$_user['user_id']."'" .
  496. "OR ip.to_group_id IN (0, ".api_get_group_id().")) ";
  497. }
  498. }
  499. // the user is member of several groups => display personal announcements AND his group announcements AND the general announcements
  500. if (is_array($group_memberships) && count($group_memberships)>0) {
  501. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  502. FROM $tbl_announcement announcement, $tbl_item_property ip
  503. WHERE
  504. announcement.c_id = $course_id AND
  505. ip.c_id = $course_id AND
  506. announcement.id = ip.ref AND
  507. ip.tool='announcement'
  508. AND ip.visibility='1'
  509. $cond_user_id
  510. $condition_session
  511. GROUP BY ip.ref
  512. ORDER BY display_order DESC
  513. LIMIT 0,$maximum";
  514. } else {
  515. // the user is not member of any group
  516. // this is an identified user => show the general announcements AND his personal announcements
  517. if ($_user['user_id']) {
  518. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  519. $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')) ";
  520. } else {
  521. $cond_user_id = " AND ( ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0') ";
  522. }
  523. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  524. FROM $tbl_announcement announcement, $tbl_item_property ip
  525. WHERE
  526. announcement.c_id = $course_id AND
  527. ip.c_id = $course_id AND
  528. announcement.id = ip.ref
  529. AND ip.tool='announcement'
  530. AND ip.visibility='1'
  531. $cond_user_id
  532. $condition_session
  533. GROUP BY ip.ref
  534. ORDER BY display_order DESC
  535. LIMIT 0,$maximum";
  536. } else {
  537. if (api_get_course_setting('allow_user_edit_announcement')) {
  538. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0') ";
  539. } else {
  540. $cond_user_id = " AND ip.to_group_id='0' ";
  541. }
  542. // the user is not identiefied => show only the general announcements
  543. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  544. FROM $tbl_announcement announcement, $tbl_item_property ip
  545. WHERE
  546. announcement.c_id = $course_id AND
  547. ip.c_id = $course_id AND
  548. announcement.id = ip.ref
  549. AND ip.tool='announcement'
  550. AND ip.visibility='1'
  551. AND ip.to_group_id='0'
  552. $condition_session
  553. GROUP BY ip.ref
  554. ORDER BY display_order DESC
  555. LIMIT 0,$maximum";
  556. }
  557. }
  558. }
  559. }
  560. $result = Database::query($sql);
  561. $announcement_number = Database::num_rows($result);
  562. /*
  563. ADD ANNOUNCEMENT / DELETE ALL
  564. */
  565. $show_actions = false;
  566. 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')) {
  567. echo '<div class="actions">';
  568. if (isset($_GET['action']) && in_array($_GET['action'], array('add', 'modify','view'))) {
  569. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".(empty($_GET['origin'])?'':$_GET['origin'])."'>".Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM)."</a>";
  570. } else {
  571. 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'),'',ICON_SIZE_MEDIUM)."</a>";
  572. }
  573. $show_actions = true;
  574. } else {
  575. if (in_array($_GET['action'], array('view'))) {
  576. echo '<div class="actions">';
  577. echo "<a href='".api_get_self()."?".api_get_cidreq()."&origin=".(empty($_GET['origin'])?'':$_GET['origin'])."'>".Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM)."</a>";
  578. echo '</div>';
  579. }
  580. }
  581. if (api_is_allowed_to_edit() && $announcement_number > 1) {
  582. if (api_get_group_id() == 0 ) {
  583. if (!$show_actions)
  584. echo '<div class="actions">';
  585. if (!in_array($_GET['action'], array('add', 'modify','view')))
  586. 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'),'',ICON_SIZE_MEDIUM)."</a>";
  587. } // if announcementNumber > 1
  588. }
  589. if ($show_actions)
  590. echo '</div>';
  591. // ANNOUNCEMENTS LIST
  592. if ($message) {
  593. Display::display_confirmation_message($message);
  594. $display_announcement_list = true;
  595. $display_form = false;
  596. }
  597. if (!empty($error_message)) {
  598. Display::display_error_message($error_message);
  599. $display_announcement_list = false;
  600. $display_form = true;
  601. }
  602. /*
  603. DISPLAY FORM
  604. */
  605. if ($display_form) {
  606. $content_to_modify = stripslashes($content_to_modify);
  607. $title_to_modify = stripslashes($title_to_modify);
  608. // DISPLAY ADD ANNOUNCEMENT COMMAND
  609. //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;">';
  610. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  611. echo '<form method="post" name="f1" enctype = "multipart/form-data" action="'.api_get_self().'?id='.$id.'&'.api_get_cidreq().'" style="margin:0px;">';
  612. if (empty($_GET['id'])) {
  613. $form_name = get_lang('AddAnnouncement');
  614. } else {
  615. $form_name = get_lang('ModifyAnnouncement');
  616. }
  617. echo '<legend>'.$form_name.'</legend>';
  618. //this variable defines if the course administrator can send a message to a specific user / group or not
  619. if (empty($_SESSION['toolgroup'])) {
  620. echo ' <div class="row">
  621. <div class="label">'.
  622. Display::return_icon('group.png', get_lang('ModifyRecipientList'), array ('align' => 'absmiddle'),ICON_SIZE_SMALL).'<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>
  623. </div>
  624. <div class="formw">';
  625. if (isset($_GET['id']) && is_array($to)) {
  626. echo '&nbsp;';
  627. } elseif (isset($_GET['remind_inactive'])) {
  628. $email_ann = '1';
  629. $_SESSION['select_groupusers']="show";
  630. $content_to_modify = sprintf(get_lang('RemindInactiveLearnersMailContent'), api_get_setting('siteName'), 7);
  631. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'));
  632. } elseif (isset($_GET['remindallinactives']) && $_GET['remindallinactives']=='true') {
  633. // 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
  634. $since = isset($_GET['since']) ? intval($_GET['since']) : 6;
  635. // getting the users who have to be reminded
  636. $to = Tracking :: get_inactives_students_in_course($_course['id'],$since, api_get_session_id());
  637. // setting the variables for the form elements: the users who need to receive the message
  638. foreach($to as &$user) {
  639. $user = 'USER:'.$user;
  640. }
  641. // setting the variables for the form elements: the 'visible to' form element has to be expanded
  642. $_SESSION['select_groupusers']="show";
  643. // setting the variables for the form elements: the message has to be sent by email
  644. $email_ann = '1';
  645. // setting the variables for the form elements: the title of the email
  646. //$title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'),' > ',$_course['name']);
  647. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'));
  648. // setting the variables for the form elements: the message of the email
  649. //$content_to_modify = sprintf(get_lang('RemindInactiveLearnersMailContent'),api_get_setting('siteName'),' > ',$_course['name'],$since);
  650. $content_to_modify = sprintf(get_lang('RemindInactiveLearnersMailContent'),api_get_setting('siteName'),$since);
  651. // when we want to remind the users who have never been active then we have a different subject and content for the announcement
  652. if ($_GET['since'] == 'never') {
  653. $title_to_modify = sprintf(get_lang('RemindInactiveLearnersMailSubject'), api_get_setting('siteName'));
  654. $content_to_modify = get_lang('YourAccountIsActiveYouCanLoginAndCheckYourCourses');
  655. }
  656. } else {
  657. echo get_lang('Everybody');
  658. }
  659. AnnouncementManager::show_to_form($to);
  660. echo ' </div>
  661. </div>';
  662. if (!isset($announcement_to_modify) ) $announcement_to_modify ='';
  663. if ($announcement_to_modify=='') {
  664. ($email_ann=='1')?$checked='checked':$checked='';
  665. echo ' <div class="row">
  666. <div class="label">
  667. </div>
  668. <div class="formw">
  669. <input id="email_ann" class="checkbox" type="checkbox" value="1" name="email_ann" checked>
  670. <label for="email_ann">'.get_lang('EmailOption').'</label>
  671. </div>
  672. </div>';
  673. }
  674. } else {
  675. if (!isset($announcement_to_modify) ) {
  676. $announcement_to_modify ="";
  677. }
  678. if ($announcement_to_modify=='') {
  679. ($email_ann=='1' || !empty($surveyid))?$checked='checked':$checked='';
  680. echo '<div class="row">
  681. <div class="label">
  682. </div>
  683. <div class="formw">
  684. <input class="checkbox" type="checkbox" value="1" name="email_ann" '.$checked.'>
  685. '.get_lang('EmailOption').': '.get_lang('MyGroup').'&nbsp;&nbsp;
  686. <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>';
  687. AnnouncementManager::show_to_form_group($_SESSION['toolgroup']);
  688. echo '</div></div>';
  689. }
  690. }
  691. // the announcement title
  692. echo ' <div class="row">
  693. <div id="msg_error" style="display:none;color:red;margin-left:20%"></div>
  694. <div class="label">
  695. <span class="form_required">*</span> '.get_lang('EmailTitle').'
  696. </div>
  697. <div class="formw">
  698. <input type="text" id="emailTitle" name="emailTitle" value="'.Security::remove_XSS($title_to_modify).'" size="60">
  699. </div>
  700. </div>';
  701. unset($title_to_modify);
  702. $title_to_modify = null;
  703. if (!isset($announcement_to_modify) ) $announcement_to_modify ="";
  704. if (!isset($content_to_modify) ) $content_to_modify ="";
  705. if (!isset($title_to_modify)) $title_to_modify = "";
  706. echo '<input type="hidden" name="id" value="'.$announcement_to_modify.'" />';
  707. $oFCKeditor = new FCKeditor('newContent') ;
  708. $oFCKeditor->Width = '100%';
  709. $oFCKeditor->Height = '300';
  710. if(!api_is_allowed_to_edit()) {
  711. $oFCKeditor->ToolbarSet = "AnnouncementsStudent";
  712. } else {
  713. $oFCKeditor->ToolbarSet = "Announcements";
  714. }
  715. $oFCKeditor->Value = $content_to_modify;
  716. echo '<div class="row"><div class="formw">';
  717. echo Display::display_normal_message(get_lang('Tags').' <br /><br />'.implode('<br />', AnnouncementManager::get_tags()), false);
  718. echo $oFCKeditor->CreateHtml();
  719. echo '</div></div>';
  720. //File attachment
  721. echo ' <div class="row">
  722. <div class="label">
  723. </div>
  724. <div class="formw">
  725. <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>
  726. <br />
  727. <table id="options" style="display: none;">
  728. <tr>
  729. <td colspan="2">
  730. <label for="file_name">'.get_lang('FileName').'&nbsp;</label>
  731. <input type="file" name="user_upload"/>
  732. </td>
  733. </tr>
  734. <tr>
  735. <td colspan="2">
  736. <label for="comment">'.get_lang('FileComment').'</label><br />
  737. <textarea name="file_comment" rows ="4" cols = "34" ></textarea>
  738. </td>
  739. </tr>
  740. </table>
  741. </div>
  742. </div>';
  743. echo'<br />';
  744. echo '<div class="row"><div class="formw">';
  745. if (empty($_SESSION['toolgroup'])) {
  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[3],true)" >'.get_lang('ButtonPublishAnnouncement').'</button><br /><br />';
  749. } else {
  750. echo '<input type="hidden" name="submitAnnouncement" value="OK">';
  751. echo '<input type="hidden" name="sec_token" value="'.$stok.'" />';
  752. echo '<button class="save" type="button" value="'.' '.get_lang('Send').' '.'" onclick="selectAll(this.form.elements[4],true)" >'.get_lang('ButtonPublishAnnouncement').'</button><br /><br />';
  753. }
  754. echo '</div></div>';
  755. echo '</form><br />';
  756. if ((isset($_GET['action']) && isset($_GET['id']) && is_array($to))||isset($_GET['remindallinactives'])||isset($_GET['remind_inactive'])) {
  757. echo '<script>document.getElementById(\'recipient_list\').style.display=\'block\';</script>';
  758. }
  759. } // displayform
  760. /*
  761. DISPLAY ANNOUNCEMENT LIST
  762. */
  763. $course_id = api_get_course_int_id();
  764. //if ($display_announcement_list && !$surveyid) {
  765. if ($display_announcement_list) {
  766. // 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
  767. //$user_id=$_user['user_id'];
  768. if (isset($_SESSION['user'])) {
  769. //$user_id=$_SESSION['user'];
  770. }
  771. $user_id = api_get_user_id();
  772. if (isset($_SESSION['group'])) {
  773. //$group_id=$_SESSION['group'];
  774. }
  775. $group_id = api_get_group_id();
  776. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  777. //$is_group_member = GroupManager :: is_tutor(api_get_user_id());
  778. if (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  779. // A.1. you are a course admin with a USER filter
  780. // => see only the messages of this specific user + the messages of the group (s)he is member of.
  781. if (!empty($_SESSION['user'])) {
  782. if (is_array($group_memberships) && count($group_memberships)>0) {
  783. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  784. FROM $tbl_announcement announcement, $tbl_item_property ip
  785. WHERE announcement.c_id = $course_id AND
  786. ip.c_id = $course_id AND
  787. announcement.id = ip.ref AND
  788. ip.tool = 'announcement' AND
  789. (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  790. $condition_session
  791. ORDER BY display_order DESC";
  792. } else {
  793. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  794. FROM $tbl_announcement announcement, $tbl_item_property ip
  795. WHERE announcement.c_id = $course_id AND
  796. ip.c_id = $course_id AND
  797. announcement.id = ip.ref AND
  798. ip.tool ='announcement' AND
  799. (ip.to_user_id = $user_id OR ip.to_group_id='0') AND
  800. ip.visibility='1'
  801. $condition_session
  802. ORDER BY display_order DESC";
  803. }
  804. } elseif (api_get_group_id() != 0 ) {
  805. // A.2. you are a course admin with a GROUP filter
  806. // => see only the messages of this specific group
  807. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  808. FROM $tbl_announcement announcement, $tbl_item_property ip
  809. WHERE announcement.c_id = $course_id AND
  810. ip.c_id = $course_id AND
  811. announcement.id = ip.ref
  812. AND ip.tool='announcement'
  813. AND ip.visibility<>'2'
  814. AND (ip.to_group_id=$group_id OR ip.to_group_id='0')
  815. $condition_session
  816. GROUP BY ip.ref
  817. ORDER BY display_order DESC";
  818. } else {
  819. // A.3 you are a course admin without any group or user filter
  820. // A.3.a you are a course admin without user or group filter but WITH studentview
  821. // => see all the messages of all the users and groups without editing possibilities
  822. if (isset($isStudentView) and $isStudentView=="true") {
  823. $sql="SELECT
  824. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  825. FROM $tbl_announcement announcement, $tbl_item_property ip
  826. WHERE announcement.c_id = $course_id AND
  827. ip.c_id = $course_id AND
  828. announcement.id = ip.ref
  829. AND ip.tool='announcement'
  830. AND ip.visibility='1'
  831. $condition_session
  832. GROUP BY ip.ref
  833. ORDER BY display_order DESC";
  834. } else {
  835. // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view)
  836. // => see all the messages of all the users and groups with editing possibilities
  837. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  838. FROM $tbl_announcement announcement, $tbl_item_property ip
  839. WHERE announcement.c_id = $course_id AND
  840. ip.c_id = $course_id AND
  841. announcement.id = ip.ref
  842. AND ip.tool='announcement'
  843. AND (ip.visibility='0' or ip.visibility='1')
  844. $condition_session
  845. GROUP BY ip.ref
  846. ORDER BY display_order DESC";
  847. }
  848. }
  849. } else {
  850. //STUDENT
  851. if (is_array($group_memberships) && count($group_memberships)>0) {
  852. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  853. if (api_get_group_id() == 0) {
  854. //No group
  855. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ( ip.to_user_id='".$_user['user_id']."'" .
  856. " OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))) ";
  857. } else {
  858. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."'
  859. OR ip.to_group_id IN (0, ".api_get_group_id()."))";
  860. }
  861. //$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).") )) ";
  862. } else {
  863. if (api_get_group_id() == 0) {
  864. $cond_user_id = " AND (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ";
  865. } else {
  866. $cond_user_id = " AND (ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".api_get_group_id()."))";
  867. }
  868. }
  869. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  870. FROM $tbl_announcement announcement, $tbl_item_property ip
  871. WHERE announcement.c_id = $course_id AND
  872. ip.c_id = $course_id AND
  873. announcement.id = ip.ref
  874. AND ip.tool='announcement'
  875. $cond_user_id
  876. $condition_session
  877. AND ip.visibility='1'
  878. ORDER BY display_order DESC";
  879. } else {
  880. if ($_user['user_id']) {
  881. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  882. $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')) ";
  883. } else {
  884. $cond_user_id = " AND (ip.to_user_id='".$_user['user_id']."' OR ip.to_group_id='0') ";
  885. }
  886. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  887. FROM $tbl_announcement announcement, $tbl_item_property ip
  888. WHERE
  889. announcement.c_id = $course_id AND
  890. ip.c_id = $course_id AND
  891. announcement.id = ip.ref AND
  892. ip.tool='announcement'
  893. $cond_user_id
  894. $condition_session
  895. AND ip.visibility='1'
  896. AND announcement.session_id IN(0,".api_get_session_id().")
  897. ORDER BY display_order DESC";
  898. } else {
  899. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  900. $cond_user_id = " AND (ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' ) ";
  901. } else {
  902. $cond_user_id = " AND ip.to_group_id='0' ";
  903. }
  904. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  905. FROM $tbl_announcement announcement, $tbl_item_property ip
  906. WHERE
  907. announcement.c_id = $course_id AND
  908. ip.c_id = $course_id AND
  909. announcement.id = ip.ref
  910. AND ip.tool='announcement'
  911. $cond_user_id
  912. $condition_session
  913. AND ip.visibility='1'
  914. AND announcement.session_id IN(0,".api_get_session_id().")";
  915. }
  916. }
  917. }
  918. $result = Database::query($sql);
  919. $num_rows = Database::num_rows($result);
  920. // DISPLAY: NO ITEMS
  921. if (!isset($_GET['action']) || !in_array($_GET['action'], array('add', 'modify','view')))
  922. if ($num_rows == 0) {
  923. 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')) {
  924. echo '<div id="no-data-view">';
  925. echo '<h2>'.get_lang('Announcements').'</h2>';
  926. echo Display::return_icon('valves.png', '', array(), 64);
  927. echo '<div class="controls">';
  928. echo Display::url(get_lang('AddAnnouncement'), api_get_self()."?".api_get_cidreq()."&action=add&origin=".(empty($_GET['origin'])?'':$_GET['origin']) , array('class' => 'a_button white'));
  929. echo '</div>';
  930. echo '</div>';
  931. } else {
  932. //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'),'',ICON_SIZE_MEDIUM)."</a>";
  933. Display::display_warning_message(get_lang('NoAnnouncements'));
  934. }
  935. } else {
  936. $iterator = 1;
  937. $bottomAnnouncement = $announcement_number;
  938. echo '<table width="100%" class="data_table">';
  939. $ths = Display::tag('th', get_lang('Title'));
  940. $ths .= Display::tag('th', get_lang('By') );
  941. $ths .= Display::tag('th', get_lang('LastUpdateDate') );
  942. if (api_is_allowed_to_edit(false,true) OR (api_is_course_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT,$myrow['id']))
  943. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  944. $ths .= Display::tag('th', get_lang('Modify'));
  945. }
  946. echo Display::tag('tr', $ths);
  947. $displayed = array();
  948. while ($myrow = Database::fetch_array($result, 'ASSOC')) {
  949. if (!in_array($myrow['id'], $displayed)) {
  950. $sent_to_icon = '';
  951. // the email icon
  952. if ($myrow['email_sent'] == '1') {
  953. $sent_to_icon = ' '.Display::return_icon('email.gif', get_lang('AnnounceSentByEmail'));
  954. }
  955. $title = $myrow['title'].$sent_to_icon;
  956. /* DATE */
  957. $last_post_datetime = $myrow['end_date'];
  958. // the styles
  959. if ($myrow['visibility'] == '0') {
  960. $style='invisible';
  961. } else {
  962. $style = '';
  963. }
  964. echo "<tr>";
  965. // show attachment list
  966. $attachment_list = array();
  967. $attachment_list = AnnouncementManager::get_attachment($myrow['id']);
  968. $attachment = '';
  969. $attachment_icon = '';
  970. if (count($attachment_list)>0) {
  971. $attachment_icon = ' '.Display::return_icon('attachment.gif',get_lang('Attachment'));
  972. }
  973. /* TITLE */
  974. $title = Display::url($title.$attachment_icon, '?action=view&id='.$myrow['id']);
  975. echo Display::tag('td', Security::remove_XSS($title), array('class' => $style));
  976. $user_info = api_get_user_info($myrow['insert_user_id']);
  977. $username = sprintf(get_lang("LoginX"), $user_info['username']);
  978. $username_span = Display::tag('span', api_get_person_name($user_info['firstName'], $user_info['lastName']), array('title'=>$username));
  979. echo Display::tag('td', $username_span);
  980. echo Display::tag('td', api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG));
  981. // 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
  982. $modify_icons = '';
  983. if (api_is_allowed_to_edit(false,true) OR (api_is_course_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $myrow['id']))
  984. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  985. $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$myrow['id']."\">".Display::return_icon('edit.png', get_lang('Edit'),'',ICON_SIZE_SMALL)."</a>";
  986. if ($myrow['visibility']==1) {
  987. $image_visibility="visible";
  988. $alt_visibility=get_lang('Hide');
  989. } else {
  990. $image_visibility="invisible";
  991. $alt_visibility=get_lang('Visible');
  992. }
  993. $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."\">".
  994. Display::return_icon($image_visibility.'.png', $alt_visibility,'',ICON_SIZE_SMALL)."</a>";
  995. // DISPLAY MOVE UP COMMAND only if it is not the top announcement
  996. if ($iterator != 1) {
  997. $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>";
  998. } else {
  999. $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up'));
  1000. }
  1001. if ($iterator < $bottomAnnouncement) {
  1002. $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>";
  1003. } else {
  1004. $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down'));
  1005. }
  1006. if (api_is_allowed_to_edit(false,true)) {
  1007. $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;\">".
  1008. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).
  1009. "</a>";
  1010. }
  1011. $iterator ++;
  1012. echo Display::tag('td', $modify_icons);
  1013. }
  1014. echo "</tr>";
  1015. }
  1016. $displayed[]=$myrow['id'];
  1017. } // end while
  1018. echo "</table>";
  1019. }
  1020. } // end: if ($displayAnnoucementList)
  1021. if (isset($_GET['action']) && $_GET['action'] == 'view') {
  1022. AnnouncementManager::display_announcement($announcement_id);
  1023. }
  1024. /* FOOTER */
  1025. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  1026. //we are not in learnpath tool
  1027. Display::display_footer();
  1028. }