system_announcements.lib.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class SystemAnnouncementManager
  5. */
  6. class SystemAnnouncementManager
  7. {
  8. CONST VISIBLE_GUEST = 1;
  9. CONST VISIBLE_STUDENT = 2;
  10. CONST VISIBLE_TEACHER = 3;
  11. /**
  12. * Displays all announcements
  13. * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  14. * @param int $id The identifier of the announcement to display
  15. */
  16. public static function display_announcements($visible, $id = -1)
  17. {
  18. $user_selected_language = api_get_interface_language();
  19. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  20. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  21. $temp_user_groups = GroupPortalManager::get_groups_by_user(api_get_user_id(),0);
  22. $groups =array();
  23. foreach ($temp_user_groups as $user_group) {
  24. $groups = array_merge($groups, array($user_group['id']));
  25. $groups = array_merge($groups, GroupPortalManager::get_parent_groups($user_group['id']));
  26. }
  27. //checks if tables exists to not break platform not updated
  28. $ann_group_db_ok =false;
  29. if( Database::num_rows(Database::query("SHOW TABLES LIKE 'announcement_rel_group'")) > 0)
  30. $ann_group_db_ok =true;
  31. $groups_string = '('.implode($groups,',').')';
  32. $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y %h:%i:%s') AS display_date"
  33. ." FROM $db_table"
  34. ." WHERE (lang='$user_selected_language'"
  35. ." OR lang IS NULL)"
  36. ." AND ((NOW() BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
  37. switch ($visible) {
  38. case self::VISIBLE_GUEST :
  39. $sql .= " AND visible_guest = 1 ";
  40. break;
  41. case self::VISIBLE_STUDENT :
  42. $sql .= " AND visible_student = 1 ";
  43. break;
  44. case self::VISIBLE_TEACHER :
  45. $sql .= " AND visible_teacher = 1 ";
  46. break;
  47. }
  48. if (count($groups) > 0 and $ann_group_db_ok ) {
  49. $sql .= " OR id IN (SELECT announcement_id FROM $tbl_announcement_group "
  50. ." WHERE group_id in $groups_string) ";
  51. }
  52. global $_configuration;
  53. $current_access_url_id = 1;
  54. if ($_configuration['multiple_access_urls']) {
  55. $current_access_url_id = api_get_current_access_url_id();
  56. }
  57. $sql .= " AND access_url_id = '$current_access_url_id' ";
  58. $sql .= " ORDER BY date_start DESC LIMIT 0,7";
  59. $announcements = Database::query($sql);
  60. if (Database::num_rows($announcements) > 0) {
  61. $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  62. $query_string = ereg_replace('&$', '', $query_string);
  63. $url = api_get_self();
  64. echo '<div class="system_announcements">';
  65. echo '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  66. echo '<div style="margin:10px;text-align:right;"><a href="news_list.php">'.get_lang('More').'</a></div>';
  67. while ($announcement = Database::fetch_object($announcements)) {
  68. if ($id != $announcement->id) {
  69. if (strlen($query_string) > 0) {
  70. $show_url = 'news_list.php#'.$announcement->id;
  71. } else {
  72. $show_url = 'news_list.php#'.$announcement->id;
  73. }
  74. $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
  75. echo '<a name="'.$announcement->id.'"></a>
  76. <div class="system_announcement">
  77. <div class="system_announcement_title"><a name="ann'.$announcement->id.'" href="'.$show_url.'">'.$announcement->title.'</a></div><div class="system_announcement_date">'.$display_date.'</div>
  78. </div>';
  79. } else {
  80. echo '<div class="system_announcement">
  81. <div class="system_announcement_title">'
  82. .$announcement->display_date.'
  83. <a name="ann'.$announcement->id.'" href="'.$url.'?'.$query_string.'#ann'.$announcement->id.'">'.$announcement->title.'</a>
  84. </div>';
  85. }
  86. echo '<br />';
  87. }
  88. echo '</div>';
  89. }
  90. return;
  91. }
  92. /**
  93. * @param $visible
  94. * @param $id
  95. * @param int $start
  96. * @param string $user_id
  97. * @return string
  98. */
  99. public static function display_all_announcements($visible, $id = -1, $start = 0,$user_id='')
  100. {
  101. $user_selected_language = api_get_interface_language();
  102. $start = intval($start);
  103. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  104. $temp_user_groups = GroupPortalManager::get_groups_by_user(api_get_user_id(),0);
  105. $groups =array();
  106. foreach ($temp_user_groups as $user_group) {
  107. $groups = array_merge($groups, array($user_group['id']));
  108. $groups = array_merge($groups, GroupPortalManager::get_parent_groups($user_group['id']));
  109. }
  110. //checks if tables exists to not break platform not updated
  111. $ann_group_db_ok =false;
  112. if( Database::num_rows(Database::query("SHOW TABLES LIKE 'announcement_rel_group'")) > 0)
  113. $ann_group_db_ok =true;
  114. $groups_string = '('.implode($groups,',').')';
  115. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  116. $now = api_get_utc_datetime();
  117. $sql = "SELECT * FROM ".$db_table."
  118. WHERE ( lang = '$user_selected_language' OR lang IS NULL) AND ( '$now' >= date_start AND '$now' <= date_end) ";
  119. switch ($visible) {
  120. case self::VISIBLE_GUEST :
  121. $sql .= " AND visible_guest = 1 ";
  122. break;
  123. case self::VISIBLE_STUDENT :
  124. $sql .= " AND visible_student = 1 ";
  125. break;
  126. case self::VISIBLE_TEACHER :
  127. $sql .= " AND visible_teacher = 1 ";
  128. break;
  129. }
  130. if (count($groups) > 0 and $ann_group_db_ok ) {
  131. $sql .= " OR id IN (SELECT announcement_id FROM $tbl_announcement_group
  132. WHERE group_id in $groups_string) ";
  133. }
  134. if (api_is_multiple_url_enabled()) {
  135. $current_access_url_id = api_get_current_access_url_id();
  136. $sql .= " AND access_url_id IN ('1', '$current_access_url_id')";
  137. }
  138. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  139. $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20";
  140. } else {
  141. $sql .= " ORDER BY date_start DESC LIMIT ".($start+1).",20";
  142. }
  143. $announcements = Database::query($sql);
  144. $content = '';
  145. if (Database::num_rows($announcements) > 0) {
  146. $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  147. $query_string = ereg_replace('&$', '', $query_string);
  148. $url = api_get_self();
  149. $content .= '<div class="system_announcements">';
  150. $content .= '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  151. $content .= '<table align="center">';
  152. $content .= '<tr>';
  153. $content .= '<td>';
  154. $content .= SystemAnnouncementManager :: display_arrow($user_id);
  155. $content .= '</td>';
  156. $content .= '</tr>';
  157. $content .= '</table>';
  158. $content .= '<table align="center" border="0" width="900px">';
  159. while ($announcement = Database::fetch_object($announcements)) {
  160. $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
  161. $content .= '<tr><td>';
  162. $content .= '<a name="'.$announcement->id.'"></a>
  163. <div class="system_announcement">
  164. <h2>'.$announcement->title.'</h2><div class="system_announcement_date">'.$display_date.'</div>
  165. <br />
  166. <div class="system_announcement_content">'
  167. .$announcement->content.'
  168. </div>
  169. </div><br />';
  170. $content .= '</tr></td>';
  171. }
  172. $content .= '</table>';
  173. $content .= '<table align="center">';
  174. $content .= '<tr>';
  175. $content .= '<td>';
  176. $content .= SystemAnnouncementManager :: display_arrow($user_id);
  177. $content .= '</td>';
  178. $content .= '</tr>';
  179. $content .= '</table>';
  180. $content .= '</div>';
  181. }
  182. return $content;
  183. }
  184. /**
  185. * @param int $user_id
  186. * @return string
  187. */
  188. public static function display_arrow($user_id)
  189. {
  190. $start = (int)$_GET['start'];
  191. $nb_announcement = SystemAnnouncementManager :: count_nb_announcement($start,$user_id);
  192. $next = ((int)$_GET['start']+19);
  193. $prev = ((int)$_GET['start']-19);
  194. $content = '';
  195. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  196. if($nb_announcement > 20) {
  197. $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  198. }
  199. } else {
  200. echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
  201. if ($nb_announcement > 20) {
  202. $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  203. }
  204. }
  205. return $content;
  206. }
  207. /**
  208. * @param int $start
  209. * @param string $user_id
  210. * @return int
  211. */
  212. public static function count_nb_announcement($start = 0, $user_id = '')
  213. {
  214. $start = intval($start);
  215. $visibility = api_is_allowed_to_create_course() ? self::VISIBLE_TEACHER : self::VISIBLE_STUDENT;
  216. $user_selected_language = api_get_interface_language();
  217. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  218. $sql = 'SELECT id FROM '.$db_table.' WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
  219. if (isset($user_id)) {
  220. switch ($visibility) {
  221. case self::VISIBLE_GUEST :
  222. $sql .= " AND visible_guest = 1 ";
  223. break;
  224. case self::VISIBLE_STUDENT :
  225. $sql .= " AND visible_student = 1 ";
  226. break;
  227. case self::VISIBLE_TEACHER :
  228. $sql .= " AND visible_teacher = 1 ";
  229. break;
  230. }
  231. }
  232. global $_configuration;
  233. $current_access_url_id = 1;
  234. if ($_configuration['multiple_access_urls']) {
  235. $current_access_url_id = api_get_current_access_url_id();
  236. }
  237. $sql .= " AND access_url_id = '$current_access_url_id' ";
  238. $sql .= 'LIMIT '.$start.',21';
  239. $announcements = Database::query($sql);
  240. $i = 0;
  241. while($rows = Database::fetch_array($announcements)) {
  242. $i++;
  243. }
  244. return $i;
  245. }
  246. /**
  247. * Get all announcements
  248. * @return array An array with all available system announcements (as php
  249. * objects)
  250. */
  251. public static function get_all_announcements()
  252. {
  253. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  254. $now = api_get_utc_datetime();
  255. $sql = "SELECT *, IF( '$now' >= date_start AND '$now' <= date_end, '1', '0') AS visible FROM $db_table";
  256. global $_configuration;
  257. $current_access_url_id = 1;
  258. if ($_configuration['multiple_access_urls']) {
  259. $current_access_url_id = api_get_current_access_url_id();
  260. }
  261. $sql .= " WHERE access_url_id = '$current_access_url_id' ";
  262. $sql .= " ORDER BY date_start ASC";
  263. $announcements = Database::query($sql);
  264. $all_announcements = array();
  265. while ($announcement = Database::fetch_object($announcements)) {
  266. $all_announcements[] = $announcement;
  267. }
  268. return $all_announcements;
  269. }
  270. /**
  271. * Adds an announcement to the database
  272. * @param string Title of the announcement
  273. * @param string Content of the announcement
  274. * @param string Start date (YYYY-MM-DD HH:II: SS)
  275. * @param string End date (YYYY-MM-DD HH:II: SS)
  276. * @param int Whether the announcement should be visible to teachers (1) or not (0)
  277. * @param int Whether the announcement should be visible to students (1) or not (0)
  278. * @param int Whether the announcement should be visible to anonymous users (1) or not (0)
  279. * @param string The language for which the announvement should be shown. Leave null for all langages
  280. * @param int Whether to send an e-mail to all users (1) or not (0)
  281. * @return mixed insert_id on success, false on failure
  282. */
  283. public static function add_announcement(
  284. $title,
  285. $content,
  286. $date_start,
  287. $date_end,
  288. $visible_teacher = 0,
  289. $visible_student = 0,
  290. $visible_guest = 0,
  291. $lang = null,
  292. $send_mail = 0,
  293. $add_to_calendar = false,
  294. $sendEmailTest = false
  295. ) {
  296. $original_content = $content;
  297. $a_dateS = explode(' ',$date_start);
  298. $a_arraySD = explode('-',$a_dateS[0]);
  299. $a_arraySH = explode(':',$a_dateS[1]);
  300. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  301. $a_dateE = explode(' ',$date_end);
  302. $a_arrayED = explode('-',$a_dateE[0]);
  303. $a_arrayEH = explode(':',$a_dateE[1]);
  304. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  305. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  306. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  307. Display :: display_normal_message(get_lang('InvalidStartDate'));
  308. return false;
  309. }
  310. if (($date_end_to_compare[1] || $date_end_to_compare[2] || $date_end_to_compare[0]) && !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])) {
  311. Display :: display_normal_message(get_lang('InvalidEndDate'));
  312. return false;
  313. }
  314. if (strlen(trim($title)) == 0) {
  315. Display::display_normal_message(get_lang('InvalidTitle'));
  316. return false;
  317. }
  318. $start = api_get_utc_datetime($date_start);
  319. $end = api_get_utc_datetime($date_end);
  320. $title = Database::escape_string($title);
  321. $content = Database::escape_string($content);
  322. //Fixing urls that are sent by email
  323. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  324. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  325. $langsql = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
  326. global $_configuration;
  327. $current_access_url_id = 1;
  328. if ($_configuration['multiple_access_urls']) {
  329. $current_access_url_id = api_get_current_access_url_id();
  330. }
  331. $sql = "INSERT INTO ".$db_table." (title,content,date_start,date_end,visible_teacher,visible_student,visible_guest, lang, access_url_id)
  332. VALUES ('".$title."','".$content."','".$start."','".$end."','".$visible_teacher."','".$visible_student."','".$visible_guest."',".$langsql.", ".$current_access_url_id.")";
  333. if ($sendEmailTest) {
  334. SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student, $lang, true);
  335. } else {
  336. if ($send_mail == 1) {
  337. SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student, $lang);
  338. }
  339. }
  340. $res = Database::query($sql);
  341. if ($res === false) {
  342. Debug::log_s(mysql_error());
  343. return false;
  344. }
  345. if ($add_to_calendar) {
  346. require_once 'calendar.lib.php';
  347. $agenda_id = agenda_add_item($title, $original_content, $date_start, $date_end);
  348. }
  349. return Database::insert_id();
  350. }
  351. /**
  352. * Makes the announcement id visible only for groups in groups_array
  353. * @param int announcement id
  354. * @param array array of group id
  355. **/
  356. public static function announcement_for_groups($announcement_id, $group_array)
  357. {
  358. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  359. //first delete all group associations for this announcement
  360. $res = Database::query("DELETE FROM $tbl_announcement_group where announcement_id=".intval($announcement_id));
  361. if ($res === false) {
  362. Debug::log_s(mysql_error());
  363. return false;
  364. }
  365. foreach ($group_array as $group_id) {
  366. if (intval($group_id) != 0 ) {
  367. $res = Database::query("INSERT into $tbl_announcement_group set announcement_id=".intval($announcement_id)
  368. .", group_id=".intval($group_id));
  369. if ($res === false) {
  370. Debug::log_s(mysql_error());
  371. return false;
  372. }
  373. }
  374. }
  375. return true;
  376. }
  377. /**
  378. * Gets the groups of this announce
  379. * @param int announcement id
  380. * @return array array of group id
  381. **/
  382. public static function get_announcement_groups($announcement_id)
  383. {
  384. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  385. $tbl_group = Database :: get_main_table(TABLE_MAIN_GROUP);
  386. //first delete all group associations for this announcement
  387. $res = Database::query("SELECT g.id as group_id , g.name as group_name FROM $tbl_group g , $tbl_announcement_group ag"
  388. ." WHERE announcement_id=".intval($announcement_id)
  389. ." AND ag.group_id = g.id");
  390. $groups = Database::fetch_array($res);
  391. return $groups;
  392. }
  393. /**
  394. * Updates an announcement to the database
  395. * @param integer $id : id of the announcement
  396. * @param string $title : title of the announcement
  397. * @param string $content : content of the announcement
  398. * @param array $date_start: start date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  399. * @param array $date_end : end date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  400. * @return bool True on success, false on failure
  401. */
  402. public static function update_announcement(
  403. $id,
  404. $title,
  405. $content,
  406. $date_start,
  407. $date_end,
  408. $visible_teacher = 0,
  409. $visible_student = 0,
  410. $visible_guest = 0,
  411. $lang = null,
  412. $send_mail = 0,
  413. $sendEmailTest = false
  414. ) {
  415. $a_dateS = explode(' ',$date_start);
  416. $a_arraySD = explode('-',$a_dateS[0]);
  417. $a_arraySH = explode(':',$a_dateS[1]);
  418. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  419. $a_dateE = explode(' ',$date_end);
  420. $a_arrayED = explode('-',$a_dateE[0]);
  421. $a_arrayEH = explode(':',$a_dateE[1]);
  422. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  423. $langsql = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
  424. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  425. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  426. Display :: display_normal_message(get_lang('InvalidStartDate'));
  427. return false;
  428. }
  429. if (($date_end_to_compare[1] || $date_end_to_compare[2] || $date_end_to_compare[0]) && !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])) {
  430. Display :: display_normal_message(get_lang('InvalidEndDate'));
  431. return false;
  432. }
  433. if (strlen(trim($title)) == 0) {
  434. Display::display_normal_message(get_lang('InvalidTitle'));
  435. return false;
  436. }
  437. $start = api_get_utc_datetime($date_start);
  438. $end = api_get_utc_datetime($date_end);
  439. $title = Database::escape_string($title);
  440. $content = Database::escape_string($content);
  441. //Fixing urls that are sent by email
  442. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  443. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  444. $id = intval($id);
  445. $sql = "UPDATE ".$db_table." SET lang=$langsql,title='".$title."',content='".$content."',date_start='".$start."',date_end='".$end."', ";
  446. $sql .= " visible_teacher = '".$visible_teacher."', visible_student = '".$visible_student."', visible_guest = '".$visible_guest."' , access_url_id = '".api_get_current_access_url_id()."' WHERE id = ".$id;
  447. if ($sendEmailTest) {
  448. SystemAnnouncementManager::send_system_announcement_by_email($title, $content, null, null, $lang, $sendEmailTest);
  449. } else {
  450. if ($send_mail==1) {
  451. SystemAnnouncementManager::send_system_announcement_by_email($title, $content, $visible_teacher, $visible_student, $lang);
  452. }
  453. }
  454. $res = Database::query($sql);
  455. if ($res === false) {
  456. Debug::log_s(mysql_error());
  457. return false;
  458. }
  459. return true;
  460. }
  461. /**
  462. * Deletes an announcement
  463. * @param int $id The identifier of the announcement that should be
  464. * @return bool True on success, false on failure
  465. */
  466. public static function delete_announcement($id)
  467. {
  468. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  469. $id = intval($id);
  470. $sql = "DELETE FROM ".$db_table." WHERE id =".$id;
  471. $res = Database::query($sql);
  472. if ($res === false) {
  473. Debug::log_s(mysql_error());
  474. return false;
  475. }
  476. return true;
  477. }
  478. /**
  479. * Gets an announcement
  480. * @param int $id The identifier of the announcement that should be
  481. * @return object Object of class StdClass or the required class, containing the query result row
  482. */
  483. public static function get_announcement($id)
  484. {
  485. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  486. $id = intval($id);
  487. $sql = "SELECT * FROM ".$db_table." WHERE id = ".$id;
  488. $announcement = Database::fetch_object(Database::query($sql));
  489. return $announcement;
  490. }
  491. /**
  492. * Change the visibility of an announcement
  493. * @param int $announcement_id
  494. * @param int $user For who should the visibility be changed (possible values are VISIBLE_TEACHER, VISIBLE_STUDENT, VISIBLE_GUEST)
  495. * @return bool True on success, false on failure
  496. */
  497. public static function set_visibility($announcement_id, $user, $visible)
  498. {
  499. $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  500. $visible = intval($visible);
  501. $announcement_id = intval($announcement_id);
  502. if (!in_array($user, array(self::VISIBLE_GUEST, self::VISIBLE_STUDENT, self::VISIBLE_TEACHER))) {
  503. return false;
  504. }
  505. $field = ($user == self::VISIBLE_TEACHER ? 'visible_teacher' : ($user == self::VISIBLE_STUDENT ? 'visible_student' : 'visible_guest'));
  506. $sql = "UPDATE ".$db_table." SET ".$field." = '".$visible."' WHERE id='".$announcement_id."'";
  507. $res = Database::query($sql);
  508. if ($res === false) {
  509. Debug::log_s(mysql_error());
  510. return false;
  511. }
  512. return true;
  513. }
  514. /**
  515. * Send a system announcement by e-mail to all teachers/students depending on parameters
  516. * @param string Title
  517. * @param string Content
  518. * @param int Whether to send to all teachers (1) or not (0)
  519. * @param int Whether to send to all students (1) or not (0)
  520. * @param string Language (optional, considered for all languages if left empty)
  521. * @return bool True if the message was sent or there was no destination matching. False on database or e-mail sending error.
  522. */
  523. public static function send_system_announcement_by_email($title, $content, $teacher, $student, $language = null, $sendEmailTest = false)
  524. {
  525. global $charset;
  526. $title = api_html_entity_decode(stripslashes($title), ENT_QUOTES, $charset);
  527. $content = api_html_entity_decode(stripslashes(str_replace(array('\r\n', '\n', '\r'),'', $content)), ENT_QUOTES, $charset);
  528. if ($sendEmailTest) {
  529. MessageManager::send_message_simple(api_get_user_id(), $title, $content);
  530. return true;
  531. }
  532. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  533. $url_condition = '';
  534. $now = api_get_utc_datetime();
  535. if (api_is_multiple_url_enabled()) {
  536. $current_access_url_id = api_get_current_access_url_id();
  537. $url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  538. $url_condition = " INNER JOIN $url_rel_user uu ON uu.user_id = u.user_id ";
  539. }
  540. if ($teacher <> 0 AND $student == 0) {
  541. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE status = '1' ";
  542. }
  543. if ($teacher == 0 AND $student <> 0) {
  544. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE status = '5' ";
  545. }
  546. if ($teacher<> 0 AND $student <> 0) {
  547. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE 1 = 1 ";
  548. }
  549. if (!empty($language)) { //special condition because language was already treated for SQL insert before
  550. $sql .= " AND language = '".Database::escape_string($language)."' ";
  551. }
  552. if (api_is_multiple_url_enabled()) {
  553. $sql .= " AND access_url_id = '".$current_access_url_id."' ";
  554. }
  555. // Sent to active users.
  556. $sql .= " AND email <>'' AND active = 1 ";
  557. // Expiration date
  558. $sql .= " AND (expiration_date = '' OR expiration_date IS NULL OR expiration_date > '$now') ";
  559. if ((empty($teacher) or $teacher == '0') AND (empty($student) or $student == '0')) {
  560. return true;
  561. }
  562. $result = Database::query($sql);
  563. if ($result === false) {
  564. return false;
  565. }
  566. $message_sent = false;
  567. while ($row = Database::fetch_array($result,'ASSOC')) {
  568. MessageManager::send_message_simple($row['user_id'], $title, $content);
  569. $message_sent = true;
  570. }
  571. return $message_sent; //true if at least one e-mail was sent
  572. }
  573. /**
  574. * Displays announcements as an slideshow
  575. * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  576. * @param int $id The identifier of the announcement to display
  577. */
  578. public static function display_announcements_slider($visible, $id = null)
  579. {
  580. $user_selected_language = Database::escape_string(api_get_interface_language());
  581. $table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  582. $cut_size = 500;
  583. $now = api_get_utc_datetime();
  584. $sql = "SELECT * FROM ".$table."
  585. WHERE ( lang = '$user_selected_language' OR lang IS NULL) AND ( '$now' >= date_start AND '$now' <= date_end) ";
  586. switch ($visible) {
  587. case self::VISIBLE_GUEST :
  588. $sql .= " AND visible_guest = 1 ";
  589. break;
  590. case self::VISIBLE_STUDENT :
  591. $sql .= " AND visible_student = 1 ";
  592. break;
  593. case self::VISIBLE_TEACHER :
  594. $sql .= " AND visible_teacher = 1 ";
  595. break;
  596. }
  597. if (isset($id) && !empty($id)) {
  598. $id = intval($id);
  599. $sql .= " AND id = $id ";
  600. }
  601. if (api_is_multiple_url_enabled()) {
  602. $current_url_id = api_get_current_access_url_id();
  603. $sql .= " AND access_url_id IN ('1', '$current_url_id') ";
  604. }
  605. $sql .= " ORDER BY date_start DESC";
  606. $announcements = Database::query($sql);
  607. $html = '';
  608. if (Database::num_rows($announcements) > 0) {
  609. $html .= Display::page_header(get_lang('SystemAnnouncements'));
  610. $html .= '<div id="container-slider" class="span6"><ul id="slider">';
  611. while ($announcement = Database::fetch_object($announcements)) {
  612. $content = $announcement->content;
  613. $url = api_get_path(WEB_PATH).'news_list.php?id='.$announcement->id;
  614. if (empty($id)) {
  615. if (api_strlen(strip_tags($content)) > $cut_size) {
  616. $content = cut($announcement->content, $cut_size).' '.Display::url(get_lang('More'), $url);
  617. }
  618. }
  619. $html .= '<li><h2>'.$announcement->title.'</h2>'.$content.'</li>';
  620. }
  621. $html .= '</ul></div>';
  622. }
  623. return $html;
  624. }
  625. }