system_announcements.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. $userGroup = new UserGroup();
  22. $temp_user_groups = $userGroup->get_groups_by_user(api_get_user_id(),0);
  23. $groups = array();
  24. foreach ($temp_user_groups as $user_group) {
  25. $groups = array_merge($groups, array($user_group['id']));
  26. $groups = array_merge($groups, $userGroup->get_parent_groups($user_group['id']));
  27. }
  28. $groups_string = '('.implode($groups,',').')';
  29. $now = api_get_utc_datetime();
  30. $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y %h:%i:%s') AS display_date
  31. FROM $db_table
  32. WHERE
  33. (lang='$user_selected_language' OR lang IS NULL) AND
  34. (('$now' BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
  35. switch ($visible) {
  36. case self::VISIBLE_GUEST :
  37. $sql .= " AND visible_guest = 1 ";
  38. break;
  39. case self::VISIBLE_STUDENT :
  40. $sql .= " AND visible_student = 1 ";
  41. break;
  42. case self::VISIBLE_TEACHER :
  43. $sql .= " AND visible_teacher = 1 ";
  44. break;
  45. }
  46. if (count($groups) > 0) {
  47. $sql .= " OR id IN (
  48. SELECT announcement_id FROM $tbl_announcement_group
  49. WHERE group_id in $groups_string
  50. ) ";
  51. }
  52. $current_access_url_id = 1;
  53. if (api_is_multiple_url_enabled()) {
  54. $current_access_url_id = api_get_current_access_url_id();
  55. }
  56. $sql .= " AND access_url_id = '$current_access_url_id' ";
  57. $sql .= " ORDER BY date_start DESC LIMIT 0,7";
  58. $announcements = Database::query($sql);
  59. if (Database::num_rows($announcements) > 0) {
  60. $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  61. $query_string = ereg_replace('&$', '', $query_string);
  62. $url = api_get_self();
  63. echo '<div class="system_announcements">';
  64. echo '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  65. echo '<div style="margin:10px;text-align:right;"><a href="news_list.php">'.get_lang('More').'</a></div>';
  66. while ($announcement = Database::fetch_object($announcements)) {
  67. if ($id != $announcement->id) {
  68. if (strlen($query_string) > 0) {
  69. $show_url = 'news_list.php#'.$announcement->id;
  70. } else {
  71. $show_url = 'news_list.php#'.$announcement->id;
  72. }
  73. $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
  74. echo '<a name="'.$announcement->id.'"></a>
  75. <div class="system_announcement">
  76. <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>
  77. </div>';
  78. } else {
  79. echo '<div class="system_announcement">
  80. <div class="system_announcement_title">'
  81. .$announcement->display_date.'
  82. <a name="ann'.$announcement->id.'" href="'.$url.'?'.$query_string.'#ann'.$announcement->id.'">'.$announcement->title.'</a>
  83. </div>';
  84. }
  85. echo '<br />';
  86. }
  87. echo '</div>';
  88. }
  89. return;
  90. }
  91. /**
  92. * @param $visible
  93. * @param $id
  94. * @param int $start
  95. * @param string $user_id
  96. * @return string
  97. */
  98. public static function display_all_announcements($visible, $id = -1, $start = 0,$user_id='')
  99. {
  100. $user_selected_language = api_get_interface_language();
  101. $start = intval($start);
  102. $userGroup = new UserGroup();
  103. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  104. $temp_user_groups = $userGroup->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, $userGroup->get_parent_groups($user_group['id']));
  109. }
  110. // Checks if tables exists to not break platform not updated
  111. $groups_string = '('.implode($groups,',').')';
  112. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  113. $now = api_get_utc_datetime();
  114. $sql = "SELECT * FROM ".$db_table."
  115. WHERE
  116. (lang = '$user_selected_language' OR lang IS NULL) AND
  117. ( '$now' >= date_start AND '$now' <= date_end) ";
  118. switch ($visible) {
  119. case self::VISIBLE_GUEST :
  120. $sql .= " AND visible_guest = 1 ";
  121. break;
  122. case self::VISIBLE_STUDENT :
  123. $sql .= " AND visible_student = 1 ";
  124. break;
  125. case self::VISIBLE_TEACHER :
  126. $sql .= " AND visible_teacher = 1 ";
  127. break;
  128. }
  129. if (count($groups) > 0) {
  130. $sql .= " OR id IN (
  131. SELECT announcement_id FROM $tbl_announcement_group
  132. WHERE group_id in $groups_string
  133. ) ";
  134. }
  135. if (api_is_multiple_url_enabled()) {
  136. $current_access_url_id = api_get_current_access_url_id();
  137. $sql .= " AND access_url_id IN ('1', '$current_access_url_id')";
  138. }
  139. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  140. $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20";
  141. } else {
  142. $sql .= " ORDER BY date_start DESC LIMIT ".($start+1).",20";
  143. }
  144. $announcements = Database::query($sql);
  145. $content = '';
  146. if (Database::num_rows($announcements) > 0) {
  147. $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  148. $query_string = ereg_replace('&$', '', $query_string);
  149. $url = api_get_self();
  150. $content .= '<div class="system_announcements">';
  151. $content .= '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  152. $content .= '<table align="center">';
  153. $content .= '<tr>';
  154. $content .= '<td>';
  155. $content .= SystemAnnouncementManager :: display_arrow($user_id);
  156. $content .= '</td>';
  157. $content .= '</tr>';
  158. $content .= '</table>';
  159. $content .= '<table align="center" border="0" width="900px">';
  160. while ($announcement = Database::fetch_object($announcements)) {
  161. $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
  162. $content .= '<tr><td>';
  163. $content .= '<a name="'.$announcement->id.'"></a>
  164. <div class="system_announcement">
  165. <h2>'.$announcement->title.'</h2><div class="system_announcement_date">'.$display_date.'</div>
  166. <br />
  167. <div class="system_announcement_content">'
  168. .$announcement->content.'
  169. </div>
  170. </div><br />';
  171. $content .= '</tr></td>';
  172. }
  173. $content .= '</table>';
  174. $content .= '<table align="center">';
  175. $content .= '<tr>';
  176. $content .= '<td>';
  177. $content .= SystemAnnouncementManager :: display_arrow($user_id);
  178. $content .= '</td>';
  179. $content .= '</tr>';
  180. $content .= '</table>';
  181. $content .= '</div>';
  182. }
  183. return $content;
  184. }
  185. /**
  186. * @param int $user_id
  187. * @return string
  188. */
  189. public static function display_arrow($user_id)
  190. {
  191. $start = (int)$_GET['start'];
  192. $nb_announcement = SystemAnnouncementManager :: count_nb_announcement($start,$user_id);
  193. $next = ((int)$_GET['start']+19);
  194. $prev = ((int)$_GET['start']-19);
  195. $content = '';
  196. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  197. if($nb_announcement > 20) {
  198. $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  199. }
  200. } else {
  201. echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
  202. if ($nb_announcement > 20) {
  203. $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  204. }
  205. }
  206. return $content;
  207. }
  208. /**
  209. * @param int $start
  210. * @param string $user_id
  211. * @return int
  212. */
  213. public static function count_nb_announcement($start = 0, $user_id = '')
  214. {
  215. $start = intval($start);
  216. $visibility = api_is_allowed_to_create_course() ? self::VISIBLE_TEACHER : self::VISIBLE_STUDENT;
  217. $user_selected_language = api_get_interface_language();
  218. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  219. $sql = 'SELECT id FROM '.$db_table.' WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
  220. if (isset($user_id)) {
  221. switch ($visibility) {
  222. case self::VISIBLE_GUEST :
  223. $sql .= " AND visible_guest = 1 ";
  224. break;
  225. case self::VISIBLE_STUDENT :
  226. $sql .= " AND visible_student = 1 ";
  227. break;
  228. case self::VISIBLE_TEACHER :
  229. $sql .= " AND visible_teacher = 1 ";
  230. break;
  231. }
  232. }
  233. $current_access_url_id = 1;
  234. if (api_is_multiple_url_enabled()) {
  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. $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
  256. FROM $table";
  257. $current_access_url_id = 1;
  258. if (api_is_multiple_url_enabled()) {
  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 = '',
  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] ||
  311. $date_end_to_compare[2] ||
  312. $date_end_to_compare[0]) &&
  313. !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
  314. ) {
  315. Display :: display_normal_message(get_lang('InvalidEndDate'));
  316. return false;
  317. }
  318. if (strlen(trim($title)) == 0) {
  319. Display::display_normal_message(get_lang('InvalidTitle'));
  320. return false;
  321. }
  322. $start = api_get_utc_datetime($date_start);
  323. $end = api_get_utc_datetime($date_end);
  324. //Fixing urls that are sent by email
  325. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  326. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  327. $lang = is_null($lang) ? '' : $lang;
  328. $current_access_url_id = 1;
  329. if (api_is_multiple_url_enabled()) {
  330. $current_access_url_id = api_get_current_access_url_id();
  331. }
  332. $params = [
  333. 'title' => $title,
  334. 'content' => $content,
  335. 'date_start' => $start,
  336. 'date_end' => $end,
  337. 'visible_teacher' => $visible_teacher,
  338. 'visible_student' => $visible_student,
  339. 'visible_guest' => $visible_guest,
  340. 'lang' => $lang,
  341. 'access_url_id' => $current_access_url_id,
  342. ];
  343. $resultId = Database::insert($db_table, $params);
  344. if ($resultId) {
  345. if ($sendEmailTest) {
  346. SystemAnnouncementManager::send_system_announcement_by_email(
  347. $title,
  348. $content,
  349. $visible_teacher,
  350. $visible_student,
  351. $lang,
  352. true
  353. );
  354. } else {
  355. if ($send_mail == 1) {
  356. SystemAnnouncementManager::send_system_announcement_by_email(
  357. $title,
  358. $content,
  359. $visible_teacher,
  360. $visible_student,
  361. $lang
  362. );
  363. }
  364. }
  365. if ($add_to_calendar) {
  366. $agenda = new Agenda();
  367. $agenda->setType('admin');
  368. $agenda->addEvent(
  369. $date_start,
  370. $date_end,
  371. false,
  372. $title,
  373. $original_content
  374. );
  375. }
  376. return $resultId;
  377. }
  378. return false;
  379. }
  380. /**
  381. * Makes the announcement id visible only for groups in groups_array
  382. * @param int announcement id
  383. * @param array array of group id
  384. **/
  385. public static function announcement_for_groups($announcement_id, $group_array)
  386. {
  387. $tbl_announcement_group = Database:: get_main_table(
  388. TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS
  389. );
  390. //first delete all group associations for this announcement
  391. $res = Database::query(
  392. "DELETE FROM $tbl_announcement_group WHERE announcement_id=".intval(
  393. $announcement_id
  394. )
  395. );
  396. if ($res === false) {
  397. return false;
  398. }
  399. foreach ($group_array as $group_id) {
  400. if (intval($group_id) != 0) {
  401. $sql = "INSERT INTO $tbl_announcement_group SET
  402. announcement_id=".intval($announcement_id).",
  403. group_id=".intval($group_id);
  404. $res = Database::query($sql);
  405. if ($res === false) {
  406. return false;
  407. }
  408. }
  409. }
  410. return true;
  411. }
  412. /**
  413. * Gets the groups of this announce
  414. * @param int announcement id
  415. * @return array array of group id
  416. **/
  417. public static function get_announcement_groups($announcement_id)
  418. {
  419. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  420. $tbl_group = Database :: get_main_table(TABLE_USERGROUP);
  421. //first delete all group associations for this announcement
  422. $sql = "SELECT
  423. g.id as group_id,
  424. g.name as group_name
  425. FROM $tbl_group g , $tbl_announcement_group ag
  426. WHERE
  427. announcement_id =".intval($announcement_id)." AND
  428. ag.group_id = g.id";
  429. $res = Database::query($sql);
  430. $groups = Database::fetch_array($res);
  431. return $groups;
  432. }
  433. /**
  434. * Updates an announcement to the database
  435. * @param integer $id : id of the announcement
  436. * @param string $title : title of the announcement
  437. * @param string $content : content of the announcement
  438. * @param array $date_start: start date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  439. * @param array $date_end : end date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  440. * @return bool True on success, false on failure
  441. */
  442. public static function update_announcement(
  443. $id,
  444. $title,
  445. $content,
  446. $date_start,
  447. $date_end,
  448. $visible_teacher = 0,
  449. $visible_student = 0,
  450. $visible_guest = 0,
  451. $lang = null,
  452. $send_mail = 0,
  453. $sendEmailTest = false
  454. ) {
  455. $a_dateS = explode(' ',$date_start);
  456. $a_arraySD = explode('-',$a_dateS[0]);
  457. $a_arraySH = explode(':',$a_dateS[1]);
  458. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  459. $a_dateE = explode(' ',$date_end);
  460. $a_arrayED = explode('-',$a_dateE[0]);
  461. $a_arrayEH = explode(':',$a_dateE[1]);
  462. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  463. $langsql = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
  464. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  465. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  466. Display :: display_normal_message(get_lang('InvalidStartDate'));
  467. return false;
  468. }
  469. if (($date_end_to_compare[1] ||
  470. $date_end_to_compare[2] ||
  471. $date_end_to_compare[0]) &&
  472. !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
  473. ) {
  474. Display :: display_normal_message(get_lang('InvalidEndDate'));
  475. return false;
  476. }
  477. if (strlen(trim($title)) == 0) {
  478. Display::display_normal_message(get_lang('InvalidTitle'));
  479. return false;
  480. }
  481. $start = api_get_utc_datetime($date_start);
  482. $end = api_get_utc_datetime($date_end);
  483. $title = Database::escape_string($title);
  484. $content = Database::escape_string($content);
  485. //Fixing urls that are sent by email
  486. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  487. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  488. $id = intval($id);
  489. $sql = "UPDATE ".$db_table." SET lang=$langsql,title='".$title."',content='".$content."',date_start='".$start."',date_end='".$end."', ";
  490. $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;
  491. if ($sendEmailTest) {
  492. SystemAnnouncementManager::send_system_announcement_by_email(
  493. $title,
  494. $content,
  495. null,
  496. null,
  497. $lang,
  498. $sendEmailTest
  499. );
  500. } else {
  501. if ($send_mail==1) {
  502. SystemAnnouncementManager::send_system_announcement_by_email(
  503. $title,
  504. $content,
  505. $visible_teacher,
  506. $visible_student,
  507. $lang
  508. );
  509. }
  510. }
  511. $res = Database::query($sql);
  512. if ($res === false) {
  513. return false;
  514. }
  515. return true;
  516. }
  517. /**
  518. * Deletes an announcement
  519. * @param int $id The identifier of the announcement that should be
  520. * @return bool True on success, false on failure
  521. */
  522. public static function delete_announcement($id)
  523. {
  524. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  525. $id = intval($id);
  526. $sql = "DELETE FROM ".$db_table." WHERE id =".$id;
  527. $res = Database::query($sql);
  528. if ($res === false) {
  529. return false;
  530. }
  531. return true;
  532. }
  533. /**
  534. * Gets an announcement
  535. * @param int $id The identifier of the announcement that should be
  536. * @return object Object of class StdClass or the required class, containing the query result row
  537. */
  538. public static function get_announcement($id)
  539. {
  540. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  541. $id = intval($id);
  542. $sql = "SELECT * FROM ".$db_table." WHERE id = ".$id;
  543. $announcement = Database::fetch_object(Database::query($sql));
  544. return $announcement;
  545. }
  546. /**
  547. * Change the visibility of an announcement
  548. * @param int $announcement_id
  549. * @param int $user For who should the visibility be changed
  550. * (possible values are VISIBLE_TEACHER, VISIBLE_STUDENT, VISIBLE_GUEST)
  551. * @return bool True on success, false on failure
  552. */
  553. public static function set_visibility($announcement_id, $user, $visible)
  554. {
  555. $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  556. $visible = intval($visible);
  557. $announcement_id = intval($announcement_id);
  558. if (!in_array($user, array(self::VISIBLE_GUEST, self::VISIBLE_STUDENT, self::VISIBLE_TEACHER))) {
  559. return false;
  560. }
  561. $field = ($user == self::VISIBLE_TEACHER ? 'visible_teacher' : ($user == self::VISIBLE_STUDENT ? 'visible_student' : 'visible_guest'));
  562. $sql = "UPDATE ".$db_table." SET ".$field." = '".$visible."'
  563. WHERE id='".$announcement_id."'";
  564. $res = Database::query($sql);
  565. if ($res === false) {
  566. return false;
  567. }
  568. return true;
  569. }
  570. /**
  571. * Send a system announcement by e-mail to all teachers/students depending on parameters
  572. * @param string Title
  573. * @param string Content
  574. * @param int Whether to send to all teachers (1) or not (0)
  575. * @param int Whether to send to all students (1) or not (0)
  576. * @param string Language (optional, considered for all languages if left empty)
  577. * @return bool True if the message was sent or there was no destination matching. False on database or e-mail sending error.
  578. */
  579. public static function send_system_announcement_by_email($title, $content, $teacher, $student, $language = null, $sendEmailTest = false)
  580. {
  581. global $charset;
  582. $title = api_html_entity_decode(stripslashes($title), ENT_QUOTES, $charset);
  583. $content = api_html_entity_decode(stripslashes(str_replace(array('\r\n', '\n', '\r'),'', $content)), ENT_QUOTES, $charset);
  584. if ($sendEmailTest) {
  585. MessageManager::send_message_simple(api_get_user_id(), $title, $content);
  586. return true;
  587. }
  588. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  589. if (api_is_multiple_url_enabled()) {
  590. $current_access_url_id = api_get_current_access_url_id();
  591. $url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  592. $url_condition = " INNER JOIN $url_rel_user uu ON uu.user_id = u.user_id ";
  593. }
  594. if ($teacher <> 0 AND $student == 0) {
  595. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE status = '1' ";
  596. }
  597. if ($teacher == 0 AND $student <> 0) {
  598. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE status = '5' ";
  599. }
  600. if ($teacher<> 0 AND $student <> 0) {
  601. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE 1 = 1 ";
  602. }
  603. if (!empty($language)) { //special condition because language was already treated for SQL insert before
  604. $sql .= " AND language = '".Database::escape_string($language)."' ";
  605. }
  606. if (api_is_multiple_url_enabled()) {
  607. $sql .= " AND access_url_id = '".$current_access_url_id."' ";
  608. }
  609. // Sent to active users.
  610. $sql .= " AND email <>'' AND active = 1 ";
  611. if ((empty($teacher) or $teacher == '0') AND (empty($student) or $student == '0')) {
  612. return true;
  613. }
  614. $result = Database::query($sql);
  615. if ($result === false) {
  616. return false;
  617. }
  618. $message_sent = false;
  619. while ($row = Database::fetch_array($result,'ASSOC')) {
  620. MessageManager::send_message_simple($row['user_id'], $title, $content);
  621. $message_sent = true;
  622. }
  623. return $message_sent; //true if at least one e-mail was sent
  624. }
  625. /**
  626. * Displays announcements as an slideshow
  627. * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  628. * @param int $id The identifier of the announcement to display
  629. */
  630. public static function display_announcements_slider($visible, $id = null)
  631. {
  632. $user_selected_language = Database::escape_string(api_get_interface_language());
  633. $table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  634. $cut_size = 500;
  635. $now = api_get_utc_datetime();
  636. $sql = "SELECT * FROM " . $table . "
  637. WHERE
  638. (lang = '$user_selected_language' OR lang IS NULL) AND
  639. ('$now' >= date_start AND '$now' <= date_end) ";
  640. switch ($visible) {
  641. case self::VISIBLE_GUEST :
  642. $sql .= " AND visible_guest = 1 ";
  643. break;
  644. case self::VISIBLE_STUDENT :
  645. $sql .= " AND visible_student = 1 ";
  646. break;
  647. case self::VISIBLE_TEACHER :
  648. $sql .= " AND visible_teacher = 1 ";
  649. break;
  650. }
  651. if (isset($id) && !empty($id)) {
  652. $id = intval($id);
  653. $sql .= " AND id = $id ";
  654. }
  655. if (api_is_multiple_url_enabled()) {
  656. $current_url_id = api_get_current_access_url_id();
  657. $sql .= " AND access_url_id IN ('1', '$current_url_id') ";
  658. }
  659. $sql .= " ORDER BY date_start DESC";
  660. $result = Database::query($sql);
  661. $announcements = [];
  662. if (Database::num_rows($result) > 0) {
  663. while ($announcement = Database::fetch_object($result)) {
  664. $announcementData = [
  665. 'id' => $announcement->id,
  666. 'title' => $announcement->title,
  667. 'content' => $announcement->content,
  668. 'readMore' => null
  669. ];
  670. if (empty($id)) {
  671. if (api_strlen(strip_tags($announcement->content)) > $cut_size) {
  672. $announcementData['content'] = cut($announcement->content, $cut_size);
  673. $announcementData['readMore'] = true;
  674. }
  675. }
  676. $announcements[] = $announcementData;
  677. }
  678. }
  679. if (count($announcements) === 0) {
  680. return null;
  681. }
  682. $template = new Template(null, false, false);
  683. $template->assign('announcements', $announcements);
  684. return $template->fetch('default/announcement/slider.tpl');
  685. }
  686. /**
  687. * Get the HTML code for an announcement
  688. * @param int $announcementId The announcement ID
  689. * @param int $visibility The announcement visibility
  690. * @return string The HTML code
  691. */
  692. public static function displayAnnouncement($announcementId, $visibility)
  693. {
  694. $selectedUserLanguage = Database::escape_string(api_get_interface_language());
  695. $announcementTable = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  696. $now = api_get_utc_datetime();
  697. $whereConditions = [
  698. "(lang = ? OR lang IS NULL) " => $selectedUserLanguage,
  699. "AND (? >= date_start AND ? <= date_end) " => [$now, $now],
  700. "AND id = ? " => intval($announcementId)
  701. ];
  702. switch ($visibility) {
  703. case self::VISIBLE_GUEST :
  704. $whereConditions["AND visible_guest = ? "] = 1;
  705. break;
  706. case self::VISIBLE_STUDENT :
  707. $whereConditions["AND visible_student = ? "] = 1;
  708. break;
  709. case self::VISIBLE_TEACHER :
  710. $whereConditions["AND visible_teacher = ? "] = 1;
  711. break;
  712. }
  713. if (api_is_multiple_url_enabled()) {
  714. $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id();
  715. }
  716. $announcement = Database::select(
  717. "*",
  718. $announcementTable,
  719. [
  720. "where" => $whereConditions,
  721. "order" => "date_start"
  722. ], 'first'
  723. );
  724. $template = new Template(null, false, false);
  725. $template->assign('announcement', $announcement);
  726. return $template->fetch('default/announcement/view.tpl');
  727. }
  728. }