system_announcements.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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.'
  220. WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
  221. if (isset($user_id)) {
  222. switch ($visibility) {
  223. case self::VISIBLE_GUEST :
  224. $sql .= " AND visible_guest = 1 ";
  225. break;
  226. case self::VISIBLE_STUDENT :
  227. $sql .= " AND visible_student = 1 ";
  228. break;
  229. case self::VISIBLE_TEACHER :
  230. $sql .= " AND visible_teacher = 1 ";
  231. break;
  232. }
  233. }
  234. $current_access_url_id = 1;
  235. if (api_is_multiple_url_enabled()) {
  236. $current_access_url_id = api_get_current_access_url_id();
  237. }
  238. $sql .= " AND access_url_id = '$current_access_url_id' ";
  239. $sql .= 'LIMIT '.$start.', 21';
  240. $announcements = Database::query($sql);
  241. $i = 0;
  242. while ($rows = Database::fetch_array($announcements)) {
  243. $i++;
  244. }
  245. return $i;
  246. }
  247. /**
  248. * Get all announcements
  249. * @return array An array with all available system announcements (as php
  250. * objects)
  251. */
  252. public static function get_all_announcements()
  253. {
  254. $table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  255. $now = api_get_utc_datetime();
  256. $sql = "SELECT *, IF ( '$now' >= date_start AND '$now' <= date_end, '1', '0') AS visible
  257. FROM $table";
  258. $current_access_url_id = 1;
  259. if (api_is_multiple_url_enabled()) {
  260. $current_access_url_id = api_get_current_access_url_id();
  261. }
  262. $sql .= " WHERE access_url_id = '$current_access_url_id' ";
  263. $sql .= " ORDER BY date_start ASC";
  264. $announcements = Database::query($sql);
  265. $all_announcements = array();
  266. while ($announcement = Database::fetch_object($announcements)) {
  267. $all_announcements[] = $announcement;
  268. }
  269. return $all_announcements;
  270. }
  271. /**
  272. * Adds an announcement to the database
  273. * @param string Title of the announcement
  274. * @param string Content of the announcement
  275. * @param string Start date (YYYY-MM-DD HH:II: SS)
  276. * @param string End date (YYYY-MM-DD HH:II: SS)
  277. * @param int Whether the announcement should be visible to teachers (1) or not (0)
  278. * @param int Whether the announcement should be visible to students (1) or not (0)
  279. * @param int Whether the announcement should be visible to anonymous users (1) or not (0)
  280. * @param string The language for which the announvement should be shown. Leave null for all langages
  281. * @param int Whether to send an e-mail to all users (1) or not (0)
  282. * @return mixed insert_id on success, false on failure
  283. */
  284. public static function add_announcement(
  285. $title,
  286. $content,
  287. $date_start,
  288. $date_end,
  289. $visible_teacher = 0,
  290. $visible_student = 0,
  291. $visible_guest = 0,
  292. $lang = '',
  293. $send_mail = 0,
  294. $add_to_calendar = false,
  295. $sendEmailTest = false
  296. ) {
  297. $original_content = $content;
  298. $a_dateS = explode(' ',$date_start);
  299. $a_arraySD = explode('-',$a_dateS[0]);
  300. $a_arraySH = explode(':',$a_dateS[1]);
  301. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  302. $a_dateE = explode(' ',$date_end);
  303. $a_arrayED = explode('-',$a_dateE[0]);
  304. $a_arrayEH = explode(':',$a_dateE[1]);
  305. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  306. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  307. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  308. Display :: display_normal_message(get_lang('InvalidStartDate'));
  309. return false;
  310. }
  311. if (($date_end_to_compare[1] ||
  312. $date_end_to_compare[2] ||
  313. $date_end_to_compare[0]) &&
  314. !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
  315. ) {
  316. Display :: display_normal_message(get_lang('InvalidEndDate'));
  317. return false;
  318. }
  319. if (strlen(trim($title)) == 0) {
  320. Display::display_normal_message(get_lang('InvalidTitle'));
  321. return false;
  322. }
  323. $start = api_get_utc_datetime($date_start);
  324. $end = api_get_utc_datetime($date_end);
  325. // Fixing urls that are sent by email
  326. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  327. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  328. $lang = is_null($lang) ? '' : $lang;
  329. $current_access_url_id = 1;
  330. if (api_is_multiple_url_enabled()) {
  331. $current_access_url_id = api_get_current_access_url_id();
  332. }
  333. $params = [
  334. 'title' => $title,
  335. 'content' => $content,
  336. 'date_start' => $start,
  337. 'date_end' => $end,
  338. 'visible_teacher' => $visible_teacher,
  339. 'visible_student' => $visible_student,
  340. 'visible_guest' => $visible_guest,
  341. 'lang' => $lang,
  342. 'access_url_id' => $current_access_url_id,
  343. ];
  344. $resultId = Database::insert($db_table, $params);
  345. if ($resultId) {
  346. if ($sendEmailTest) {
  347. SystemAnnouncementManager::send_system_announcement_by_email(
  348. $title,
  349. $content,
  350. $visible_teacher,
  351. $visible_student,
  352. $lang,
  353. true
  354. );
  355. } else {
  356. if ($send_mail == 1) {
  357. SystemAnnouncementManager::send_system_announcement_by_email(
  358. $title,
  359. $content,
  360. $visible_teacher,
  361. $visible_student,
  362. $lang
  363. );
  364. }
  365. }
  366. if ($add_to_calendar) {
  367. $agenda = new Agenda();
  368. $agenda->setType('admin');
  369. $agenda->addEvent(
  370. $date_start,
  371. $date_end,
  372. false,
  373. $title,
  374. $original_content
  375. );
  376. }
  377. return $resultId;
  378. }
  379. return false;
  380. }
  381. /**
  382. * Makes the announcement id visible only for groups in groups_array
  383. * @param int announcement id
  384. * @param array array of group id
  385. **/
  386. public static function announcement_for_groups($announcement_id, $group_array)
  387. {
  388. $tbl_announcement_group = Database:: get_main_table(
  389. TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS
  390. );
  391. //first delete all group associations for this announcement
  392. $res = Database::query(
  393. "DELETE FROM $tbl_announcement_group WHERE announcement_id=".intval(
  394. $announcement_id
  395. )
  396. );
  397. if ($res === false) {
  398. return false;
  399. }
  400. foreach ($group_array as $group_id) {
  401. if (intval($group_id) != 0) {
  402. $sql = "INSERT INTO $tbl_announcement_group SET
  403. announcement_id=".intval($announcement_id).",
  404. group_id=".intval($group_id);
  405. $res = Database::query($sql);
  406. if ($res === false) {
  407. return false;
  408. }
  409. }
  410. }
  411. return true;
  412. }
  413. /**
  414. * Gets the groups of this announce
  415. * @param int announcement id
  416. * @return array array of group id
  417. **/
  418. public static function get_announcement_groups($announcement_id)
  419. {
  420. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  421. $tbl_group = Database :: get_main_table(TABLE_USERGROUP);
  422. //first delete all group associations for this announcement
  423. $sql = "SELECT
  424. g.id as group_id,
  425. g.name as group_name
  426. FROM $tbl_group g , $tbl_announcement_group ag
  427. WHERE
  428. announcement_id =".intval($announcement_id)." AND
  429. ag.group_id = g.id";
  430. $res = Database::query($sql);
  431. $groups = Database::fetch_array($res);
  432. return $groups;
  433. }
  434. /**
  435. * Updates an announcement to the database
  436. * @param integer $id : id of the announcement
  437. * @param string $title : title of the announcement
  438. * @param string $content : content of the announcement
  439. * @param array $date_start: start date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  440. * @param array $date_end : end date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  441. * @return bool True on success, false on failure
  442. */
  443. public static function update_announcement(
  444. $id,
  445. $title,
  446. $content,
  447. $date_start,
  448. $date_end,
  449. $visible_teacher = 0,
  450. $visible_student = 0,
  451. $visible_guest = 0,
  452. $lang = null,
  453. $send_mail = 0,
  454. $sendEmailTest = false
  455. ) {
  456. $em = Database::getManager();
  457. $announcement = $em->find('ChamiloCoreBundle:SysAnnouncement', $id);
  458. if (!$announcement) {
  459. return false;
  460. }
  461. $a_dateS = explode(' ',$date_start);
  462. $a_arraySD = explode('-',$a_dateS[0]);
  463. $a_arraySH = explode(':',$a_dateS[1]);
  464. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  465. $a_dateE = explode(' ',$date_end);
  466. $a_arrayED = explode('-',$a_dateE[0]);
  467. $a_arrayEH = explode(':',$a_dateE[1]);
  468. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  469. $lang = is_null($lang) ? '' : $lang;
  470. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  471. Display :: display_normal_message(get_lang('InvalidStartDate'));
  472. return false;
  473. }
  474. if (($date_end_to_compare[1] ||
  475. $date_end_to_compare[2] ||
  476. $date_end_to_compare[0]) &&
  477. !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
  478. ) {
  479. Display :: display_normal_message(get_lang('InvalidEndDate'));
  480. return false;
  481. }
  482. if (strlen(trim($title)) == 0) {
  483. Display::display_normal_message(get_lang('InvalidTitle'));
  484. return false;
  485. }
  486. $start = api_get_utc_datetime($date_start);
  487. $end = api_get_utc_datetime($date_end);
  488. // Fixing urls that are sent by email
  489. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  490. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  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. $dateStart = new DateTime($start, new DateTimeZone('UTC'));
  512. $dateEnd = new DateTime($end, new DateTimeZone('UTC'));
  513. $announcement
  514. ->setLang($lang)
  515. ->setTitle($title)
  516. ->setContent($content)
  517. ->setDateStart($dateStart)
  518. ->setDateEnd($dateEnd)
  519. ->setVisibleTeacher($visible_teacher)
  520. ->setVisibleStudent($visible_student)
  521. ->setVisibleGuest($visible_guest)
  522. ->setAccessUrlId(api_get_current_access_url_id());
  523. $em->merge($announcement);
  524. $em->flush();
  525. return true;
  526. }
  527. /**
  528. * Deletes an announcement
  529. * @param int $id The identifier of the announcement that should be
  530. * @return bool True on success, false on failure
  531. */
  532. public static function delete_announcement($id)
  533. {
  534. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  535. $id = intval($id);
  536. $sql = "DELETE FROM ".$db_table." WHERE id =".$id;
  537. $res = Database::query($sql);
  538. if ($res === false) {
  539. return false;
  540. }
  541. return true;
  542. }
  543. /**
  544. * Gets an announcement
  545. * @param int $id The identifier of the announcement that should be
  546. * @return object Object of class StdClass or the required class, containing the query result row
  547. */
  548. public static function get_announcement($id)
  549. {
  550. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  551. $id = intval($id);
  552. $sql = "SELECT * FROM ".$db_table." WHERE id = ".$id;
  553. $announcement = Database::fetch_object(Database::query($sql));
  554. return $announcement;
  555. }
  556. /**
  557. * Change the visibility of an announcement
  558. * @param int $announcement_id
  559. * @param int $user For who should the visibility be changed
  560. * (possible values are VISIBLE_TEACHER, VISIBLE_STUDENT, VISIBLE_GUEST)
  561. * @return bool True on success, false on failure
  562. */
  563. public static function set_visibility($announcement_id, $user, $visible)
  564. {
  565. $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  566. $visible = intval($visible);
  567. $announcement_id = intval($announcement_id);
  568. if (!in_array($user, array(self::VISIBLE_GUEST, self::VISIBLE_STUDENT, self::VISIBLE_TEACHER))) {
  569. return false;
  570. }
  571. $field = ($user == self::VISIBLE_TEACHER ? 'visible_teacher' : ($user == self::VISIBLE_STUDENT ? 'visible_student' : 'visible_guest'));
  572. $sql = "UPDATE ".$db_table." SET ".$field." = '".$visible."'
  573. WHERE id='".$announcement_id."'";
  574. $res = Database::query($sql);
  575. if ($res === false) {
  576. return false;
  577. }
  578. return true;
  579. }
  580. /**
  581. * Send a system announcement by e-mail to all teachers/students depending on parameters
  582. * @param string $title
  583. * @param string $content
  584. * @param int $teacher Whether to send to all teachers (1) or not (0)
  585. * @param int $student Whether to send to all students (1) or not (0)
  586. * @param string $language Language (optional, considered for all languages if left empty)
  587. * @param bool $sendEmailTest
  588. * @return bool True if the message was sent or there was no destination matching. False on database or e-mail sending error.
  589. */
  590. public static function send_system_announcement_by_email(
  591. $title,
  592. $content,
  593. $teacher,
  594. $student,
  595. $language = null,
  596. $sendEmailTest = false
  597. ) {
  598. $content = str_replace(array('\r\n', '\n', '\r'),'', $content);
  599. $now = api_get_utc_datetime();
  600. if ($sendEmailTest) {
  601. MessageManager::send_message_simple(api_get_user_id(), $title, $content);
  602. return true;
  603. }
  604. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  605. if (api_is_multiple_url_enabled()) {
  606. $current_access_url_id = api_get_current_access_url_id();
  607. $url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  608. $url_condition = " INNER JOIN $url_rel_user uu ON uu.user_id = u.user_id ";
  609. }
  610. if ($teacher <> 0 && $student == 0) {
  611. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition
  612. WHERE status = '1' ";
  613. }
  614. if ($teacher == 0 && $student <> 0) {
  615. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition
  616. WHERE status = '5' ";
  617. }
  618. if ($teacher<> 0 && $student <> 0) {
  619. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition
  620. WHERE 1 = 1 ";
  621. }
  622. if (!empty($language)) {
  623. //special condition because language was already treated for SQL insert before
  624. $sql .= " AND language = '".Database::escape_string($language)."' ";
  625. }
  626. if (api_is_multiple_url_enabled()) {
  627. $sql .= " AND access_url_id = '".$current_access_url_id."' ";
  628. }
  629. // Sent to active users.
  630. $sql .= " AND email <>'' AND active = 1 ";
  631. // Expiration date
  632. $sql .= " AND (expiration_date = '' OR expiration_date IS NULL OR expiration_date > '$now') ";
  633. if ((empty($teacher) || $teacher == '0') && (empty($student) || $student == '0')) {
  634. return true;
  635. }
  636. $result = Database::query($sql);
  637. if ($result === false) {
  638. return false;
  639. }
  640. $message_sent = false;
  641. while ($row = Database::fetch_array($result,'ASSOC')) {
  642. MessageManager::send_message_simple($row['user_id'], $title, $content);
  643. $message_sent = true;
  644. }
  645. return $message_sent; //true if at least one e-mail was sent
  646. }
  647. /**
  648. * Displays announcements as an slideshow
  649. * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  650. * @param int $id The identifier of the announcement to display
  651. */
  652. public static function display_announcements_slider($visible, $id = null)
  653. {
  654. $user_selected_language = Database::escape_string(api_get_interface_language());
  655. $table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  656. $cut_size = 500;
  657. $now = api_get_utc_datetime();
  658. $sql = "SELECT * FROM " . $table . "
  659. WHERE
  660. (lang = '$user_selected_language' OR lang = '') AND
  661. ('$now' >= date_start AND '$now' <= date_end) ";
  662. switch ($visible) {
  663. case self::VISIBLE_GUEST :
  664. $sql .= " AND visible_guest = 1 ";
  665. break;
  666. case self::VISIBLE_STUDENT :
  667. $sql .= " AND visible_student = 1 ";
  668. break;
  669. case self::VISIBLE_TEACHER :
  670. $sql .= " AND visible_teacher = 1 ";
  671. break;
  672. }
  673. if (isset($id) && !empty($id)) {
  674. $id = intval($id);
  675. $sql .= " AND id = $id ";
  676. }
  677. if (api_is_multiple_url_enabled()) {
  678. $current_url_id = api_get_current_access_url_id();
  679. $sql .= " AND access_url_id IN ('1', '$current_url_id') ";
  680. }
  681. $sql .= " ORDER BY date_start DESC";
  682. $result = Database::query($sql);
  683. $announcements = [];
  684. if (Database::num_rows($result) > 0) {
  685. while ($announcement = Database::fetch_object($result)) {
  686. $announcementData = [
  687. 'id' => $announcement->id,
  688. 'title' => $announcement->title,
  689. 'content' => $announcement->content,
  690. 'readMore' => null
  691. ];
  692. if (empty($id)) {
  693. if (api_strlen(strip_tags($announcement->content)) > $cut_size) {
  694. $announcementData['content'] = cut($announcement->content, $cut_size);
  695. $announcementData['readMore'] = true;
  696. }
  697. }
  698. $announcements[] = $announcementData;
  699. }
  700. }
  701. if (count($announcements) === 0) {
  702. return null;
  703. }
  704. $template = new Template(null, false, false);
  705. $template->assign('announcements', $announcements);
  706. return $template->fetch('default/announcement/slider.tpl');
  707. }
  708. /**
  709. * Get the HTML code for an announcement
  710. * @param int $announcementId The announcement ID
  711. * @param int $visibility The announcement visibility
  712. * @return string The HTML code
  713. */
  714. public static function displayAnnouncement($announcementId, $visibility)
  715. {
  716. $selectedUserLanguage = Database::escape_string(api_get_interface_language());
  717. $announcementTable = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  718. $now = api_get_utc_datetime();
  719. $whereConditions = [
  720. "(lang = ? OR lang IS NULL) " => $selectedUserLanguage,
  721. "AND (? >= date_start AND ? <= date_end) " => [$now, $now],
  722. "AND id = ? " => intval($announcementId)
  723. ];
  724. switch ($visibility) {
  725. case self::VISIBLE_GUEST :
  726. $whereConditions["AND visible_guest = ? "] = 1;
  727. break;
  728. case self::VISIBLE_STUDENT :
  729. $whereConditions["AND visible_student = ? "] = 1;
  730. break;
  731. case self::VISIBLE_TEACHER :
  732. $whereConditions["AND visible_teacher = ? "] = 1;
  733. break;
  734. }
  735. if (api_is_multiple_url_enabled()) {
  736. $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id();
  737. }
  738. $announcement = Database::select(
  739. "*",
  740. $announcementTable,
  741. [
  742. "where" => $whereConditions,
  743. "order" => "date_start"
  744. ], 'first'
  745. );
  746. $template = new Template(null, false, false);
  747. $template->assign('announcement', $announcement);
  748. return $template->fetch('default/announcement/view.tpl');
  749. }
  750. }