system_announcements.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * Class SystemAnnouncementManager
  6. */
  7. class SystemAnnouncementManager
  8. {
  9. CONST VISIBLE_GUEST = 1;
  10. CONST VISIBLE_STUDENT = 2;
  11. CONST VISIBLE_TEACHER = 3;
  12. /**
  13. * Displays all announcements
  14. * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  15. * @param int $id The identifier of the announcement to display
  16. */
  17. public static function display_announcements($visible, $id = -1)
  18. {
  19. $user_selected_language = api_get_language_isocode();
  20. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  21. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  22. $userGroup = new UserGroup();
  23. $temp_user_groups = $userGroup->get_groups_by_user(api_get_user_id(),0);
  24. $groups = array();
  25. foreach ($temp_user_groups as $user_group) {
  26. $groups = array_merge($groups, array($user_group['id']));
  27. $groups = array_merge($groups, $userGroup->get_parent_groups($user_group['id']));
  28. }
  29. $groups_string = '('.implode($groups,',').')';
  30. $now = api_get_utc_datetime();
  31. $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y %h:%i:%s') AS display_date
  32. FROM $db_table
  33. WHERE
  34. (lang='$user_selected_language' OR lang IS NULL) AND
  35. (('$now' BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
  36. switch ($visible) {
  37. case self::VISIBLE_GUEST :
  38. $sql .= " AND visible_guest = 1 ";
  39. break;
  40. case self::VISIBLE_STUDENT :
  41. $sql .= " AND visible_student = 1 ";
  42. break;
  43. case self::VISIBLE_TEACHER :
  44. $sql .= " AND visible_teacher = 1 ";
  45. break;
  46. }
  47. if (count($groups) > 0) {
  48. $sql .= " OR id IN (
  49. SELECT announcement_id FROM $tbl_announcement_group
  50. WHERE group_id in $groups_string
  51. ) ";
  52. }
  53. $current_access_url_id = 1;
  54. if (api_is_multiple_url_enabled()) {
  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_language_isocode();
  102. $start = intval($start);
  103. $userGroup = new UserGroup();
  104. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  105. $temp_user_groups = $userGroup->get_groups_by_user(api_get_user_id(),0);
  106. $groups = array();
  107. foreach ($temp_user_groups as $user_group) {
  108. $groups = array_merge($groups, array($user_group['id']));
  109. $groups = array_merge($groups, $userGroup->get_parent_groups($user_group['id']));
  110. }
  111. // Checks if tables exists to not break platform not updated
  112. $groups_string = '('.implode($groups,',').')';
  113. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  114. $now = api_get_utc_datetime();
  115. $sql = "SELECT * FROM ".$db_table."
  116. WHERE
  117. (lang = '$user_selected_language' OR lang IS NULL) AND
  118. ( '$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) {
  131. $sql .= " OR id IN (
  132. SELECT announcement_id FROM $tbl_announcement_group
  133. WHERE group_id in $groups_string
  134. ) ";
  135. }
  136. if (api_is_multiple_url_enabled()) {
  137. $current_access_url_id = api_get_current_access_url_id();
  138. $sql .= " AND access_url_id IN ('1', '$current_access_url_id')";
  139. }
  140. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  141. $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20";
  142. } else {
  143. $sql .= " ORDER BY date_start DESC LIMIT ".($start+1).",20";
  144. }
  145. $announcements = Database::query($sql);
  146. $content = '';
  147. if (Database::num_rows($announcements) > 0) {
  148. $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  149. $query_string = ereg_replace('&$', '', $query_string);
  150. $url = api_get_self();
  151. $content .= '<div class="system_announcements">';
  152. $content .= '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  153. $content .= '<table align="center">';
  154. $content .= '<tr>';
  155. $content .= '<td>';
  156. $content .= SystemAnnouncementManager :: display_arrow($user_id);
  157. $content .= '</td>';
  158. $content .= '</tr>';
  159. $content .= '</table>';
  160. $content .= '<table align="center" border="0" width="900px">';
  161. while ($announcement = Database::fetch_object($announcements)) {
  162. $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
  163. $content .= '<tr><td>';
  164. $content .= '<a name="'.$announcement->id.'"></a>
  165. <div class="system_announcement">
  166. <h2>'.$announcement->title.'</h2><div class="system_announcement_date">'.$display_date.'</div>
  167. <br />
  168. <div class="system_announcement_content">'
  169. .$announcement->content.'
  170. </div>
  171. </div><br />';
  172. $content .= '</tr></td>';
  173. }
  174. $content .= '</table>';
  175. $content .= '<table align="center">';
  176. $content .= '<tr>';
  177. $content .= '<td>';
  178. $content .= SystemAnnouncementManager :: display_arrow($user_id);
  179. $content .= '</td>';
  180. $content .= '</tr>';
  181. $content .= '</table>';
  182. $content .= '</div>';
  183. }
  184. return $content;
  185. }
  186. /**
  187. * @param int $user_id
  188. * @return string
  189. */
  190. public static function display_arrow($user_id)
  191. {
  192. $start = (int)$_GET['start'];
  193. $nb_announcement = SystemAnnouncementManager :: count_nb_announcement($start,$user_id);
  194. $next = ((int)$_GET['start']+19);
  195. $prev = ((int)$_GET['start']-19);
  196. $content = '';
  197. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  198. if($nb_announcement > 20) {
  199. $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  200. }
  201. } else {
  202. echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
  203. if ($nb_announcement > 20) {
  204. $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  205. }
  206. }
  207. return $content;
  208. }
  209. /**
  210. * @param int $start
  211. * @param string $user_id
  212. * @return int
  213. */
  214. public static function count_nb_announcement($start = 0, $user_id = '')
  215. {
  216. $start = intval($start);
  217. $visibility = api_is_allowed_to_create_course() ? self::VISIBLE_TEACHER : self::VISIBLE_STUDENT;
  218. $user_selected_language = api_get_language_isocode();
  219. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  220. $sql = 'SELECT id FROM '.$db_table.'
  221. WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
  222. if (isset($user_id)) {
  223. switch ($visibility) {
  224. case self::VISIBLE_GUEST :
  225. $sql .= " AND visible_guest = 1 ";
  226. break;
  227. case self::VISIBLE_STUDENT :
  228. $sql .= " AND visible_student = 1 ";
  229. break;
  230. case self::VISIBLE_TEACHER :
  231. $sql .= " AND visible_teacher = 1 ";
  232. break;
  233. }
  234. }
  235. $current_access_url_id = 1;
  236. if (api_is_multiple_url_enabled()) {
  237. $current_access_url_id = api_get_current_access_url_id();
  238. }
  239. $sql .= " AND access_url_id = '$current_access_url_id' ";
  240. $sql .= 'LIMIT '.$start.', 21';
  241. $announcements = Database::query($sql);
  242. $i = 0;
  243. while ($rows = Database::fetch_array($announcements)) {
  244. $i++;
  245. }
  246. return $i;
  247. }
  248. /**
  249. * Get all announcements
  250. * @return array An array with all available system announcements (as php
  251. * objects)
  252. */
  253. public static function get_all_announcements()
  254. {
  255. $table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  256. $now = api_get_utc_datetime();
  257. $sql = "SELECT *, IF ( '$now' >= date_start AND '$now' <= date_end, '1', '0') AS visible
  258. FROM $table";
  259. $current_access_url_id = 1;
  260. if (api_is_multiple_url_enabled()) {
  261. $current_access_url_id = api_get_current_access_url_id();
  262. }
  263. $sql .= " WHERE access_url_id = '$current_access_url_id' ";
  264. $sql .= " ORDER BY date_start ASC";
  265. $announcements = Database::query($sql);
  266. $all_announcements = array();
  267. while ($announcement = Database::fetch_object($announcements)) {
  268. $all_announcements[] = $announcement;
  269. }
  270. return $all_announcements;
  271. }
  272. /**
  273. * Adds an announcement to the database
  274. * @param string Title of the announcement
  275. * @param string Content of the announcement
  276. * @param string Start date (YYYY-MM-DD HH:II: SS)
  277. * @param string End date (YYYY-MM-DD HH:II: SS)
  278. * @param int Whether the announcement should be visible to teachers (1) or not (0)
  279. * @param int Whether the announcement should be visible to students (1) or not (0)
  280. * @param int Whether the announcement should be visible to anonymous users (1) or not (0)
  281. * @param string The language for which the announvement should be shown. Leave null for all langages
  282. * @param int Whether to send an e-mail to all users (1) or not (0)
  283. * @return mixed insert_id on success, false on failure
  284. */
  285. public static function add_announcement(
  286. $title,
  287. $content,
  288. $date_start,
  289. $date_end,
  290. $visible_teacher = 0,
  291. $visible_student = 0,
  292. $visible_guest = 0,
  293. $lang = '',
  294. $send_mail = 0,
  295. $add_to_calendar = false,
  296. $sendEmailTest = false
  297. ) {
  298. $original_content = $content;
  299. $a_dateS = explode(' ',$date_start);
  300. $a_arraySD = explode('-',$a_dateS[0]);
  301. $a_arraySH = explode(':',$a_dateS[1]);
  302. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  303. $a_dateE = explode(' ',$date_end);
  304. $a_arrayED = explode('-',$a_dateE[0]);
  305. $a_arrayEH = explode(':',$a_dateE[1]);
  306. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  307. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  308. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  309. Display :: display_normal_message(get_lang('InvalidStartDate'));
  310. return false;
  311. }
  312. if (($date_end_to_compare[1] ||
  313. $date_end_to_compare[2] ||
  314. $date_end_to_compare[0]) &&
  315. !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
  316. ) {
  317. Display :: display_normal_message(get_lang('InvalidEndDate'));
  318. return false;
  319. }
  320. if (strlen(trim($title)) == 0) {
  321. Display::display_normal_message(get_lang('InvalidTitle'));
  322. return false;
  323. }
  324. $start = api_get_utc_datetime($date_start);
  325. $end = api_get_utc_datetime($date_end);
  326. //Fixing urls that are sent by email
  327. //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  328. //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  329. $content = str_replace('src=\"'.api_get_path(REL_HOME_PATH), 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
  330. $content = str_replace('file='.api_get_path(REL_HOME_PATH), 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
  331. $lang = is_null($lang) ? '' : $lang;
  332. $current_access_url_id = 1;
  333. if (api_is_multiple_url_enabled()) {
  334. $current_access_url_id = api_get_current_access_url_id();
  335. }
  336. $params = [
  337. 'title' => $title,
  338. 'content' => $content,
  339. 'date_start' => $start,
  340. 'date_end' => $end,
  341. 'visible_teacher' => $visible_teacher,
  342. 'visible_student' => $visible_student,
  343. 'visible_guest' => $visible_guest,
  344. 'lang' => $lang,
  345. 'access_url_id' => $current_access_url_id,
  346. ];
  347. $resultId = Database::insert($db_table, $params);
  348. if ($resultId) {
  349. if ($sendEmailTest) {
  350. SystemAnnouncementManager::send_system_announcement_by_email(
  351. $title,
  352. $content,
  353. $visible_teacher,
  354. $visible_student,
  355. $lang,
  356. true
  357. );
  358. } else {
  359. if ($send_mail == 1) {
  360. SystemAnnouncementManager::send_system_announcement_by_email(
  361. $title,
  362. $content,
  363. $visible_teacher,
  364. $visible_student,
  365. $lang
  366. );
  367. }
  368. }
  369. if ($add_to_calendar) {
  370. $agenda = new Agenda();
  371. $agenda->setType('admin');
  372. $agenda->addEvent(
  373. $date_start,
  374. $date_end,
  375. false,
  376. $title,
  377. $original_content
  378. );
  379. }
  380. return $resultId;
  381. }
  382. return false;
  383. }
  384. /**
  385. * Makes the announcement id visible only for groups in groups_array
  386. * @param int announcement id
  387. * @param array array of group id
  388. **/
  389. public static function announcement_for_groups($announcement_id, $group_array)
  390. {
  391. $tbl_announcement_group = Database:: get_main_table(
  392. TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS
  393. );
  394. //first delete all group associations for this announcement
  395. $res = Database::query(
  396. "DELETE FROM $tbl_announcement_group WHERE announcement_id=".intval(
  397. $announcement_id
  398. )
  399. );
  400. if ($res === false) {
  401. return false;
  402. }
  403. foreach ($group_array as $group_id) {
  404. if (intval($group_id) != 0) {
  405. $sql = "INSERT INTO $tbl_announcement_group SET
  406. announcement_id=".intval($announcement_id).",
  407. group_id=".intval($group_id);
  408. $res = Database::query($sql);
  409. if ($res === false) {
  410. return false;
  411. }
  412. }
  413. }
  414. return true;
  415. }
  416. /**
  417. * Gets the groups of this announce
  418. * @param int announcement id
  419. * @return array array of group id
  420. **/
  421. public static function get_announcement_groups($announcement_id)
  422. {
  423. $tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
  424. $tbl_group = Database :: get_main_table(TABLE_USERGROUP);
  425. //first delete all group associations for this announcement
  426. $sql = "SELECT
  427. g.id as group_id,
  428. g.name as group_name
  429. FROM $tbl_group g , $tbl_announcement_group ag
  430. WHERE
  431. announcement_id =".intval($announcement_id)." AND
  432. ag.group_id = g.id";
  433. $res = Database::query($sql);
  434. $groups = Database::fetch_array($res);
  435. return $groups;
  436. }
  437. /**
  438. * Updates an announcement to the database
  439. * @param integer $id : id of the announcement
  440. * @param string $title : title of the announcement
  441. * @param string $content : content of the announcement
  442. * @param array $date_start: start date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  443. * @param array $date_end : end date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  444. * @return bool True on success, false on failure
  445. */
  446. public static function update_announcement(
  447. $id,
  448. $title,
  449. $content,
  450. $date_start,
  451. $date_end,
  452. $visible_teacher = 0,
  453. $visible_student = 0,
  454. $visible_guest = 0,
  455. $lang = null,
  456. $send_mail = 0,
  457. $sendEmailTest = false
  458. ) {
  459. $em = Database::getManager();
  460. $a_dateS = explode(' ',$date_start);
  461. $a_arraySD = explode('-',$a_dateS[0]);
  462. $a_arraySH = explode(':',$a_dateS[1]);
  463. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  464. $a_dateE = explode(' ',$date_end);
  465. $a_arrayED = explode('-',$a_dateE[0]);
  466. $a_arrayEH = explode(':',$a_dateE[1]);
  467. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  468. $lang = is_null($lang) ? '' : $lang;
  469. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  470. Display :: display_normal_message(get_lang('InvalidStartDate'));
  471. return false;
  472. }
  473. if (($date_end_to_compare[1] ||
  474. $date_end_to_compare[2] ||
  475. $date_end_to_compare[0]) &&
  476. !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
  477. ) {
  478. Display :: display_normal_message(get_lang('InvalidEndDate'));
  479. return false;
  480. }
  481. if (strlen(trim($title)) == 0) {
  482. Display::display_normal_message(get_lang('InvalidTitle'));
  483. return false;
  484. }
  485. $start = api_get_utc_datetime($date_start);
  486. $end = api_get_utc_datetime($date_end);
  487. //Fixing urls that are sent by email
  488. //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  489. //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  490. $content = str_replace('src=\"'.api_get_path(REL_HOME_PATH), 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
  491. $content = str_replace('file='.api_get_path(REL_HOME_PATH), 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
  492. $id = intval($id);
  493. if ($sendEmailTest) {
  494. SystemAnnouncementManager::send_system_announcement_by_email(
  495. $title,
  496. $content,
  497. null,
  498. null,
  499. $lang,
  500. $sendEmailTest
  501. );
  502. } else {
  503. if ($send_mail==1) {
  504. SystemAnnouncementManager::send_system_announcement_by_email(
  505. $title,
  506. $content,
  507. $visible_teacher,
  508. $visible_student,
  509. $lang
  510. );
  511. }
  512. }
  513. $announcement = $em->find('ChamiloCoreBundle:SysAnnouncement', $id);
  514. if (!$announcement) {
  515. return false;
  516. }
  517. $dateStart = new DateTime($start, new DateTimeZone('UTC'));
  518. $dateEnd = new DateTime($end, new DateTimeZone('UTC'));
  519. $announcement
  520. ->setLang($lang)
  521. ->setTitle($title)
  522. ->setContent($content)
  523. ->setDateStart($dateStart)
  524. ->setDateEnd($dateEnd)
  525. ->setVisibleTeacher($visible_teacher)
  526. ->setVisibleStudent($visible_student)
  527. ->setVisibleGuest($visible_guest)
  528. ->setAccessUrlId(api_get_current_access_url_id());
  529. $em->merge($announcement);
  530. $em->flush();
  531. return true;
  532. }
  533. /**
  534. * Deletes an announcement
  535. * @param int $id The identifier of the announcement that should be
  536. * @return bool True on success, false on failure
  537. */
  538. public static function delete_announcement($id)
  539. {
  540. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  541. $id = intval($id);
  542. $sql = "DELETE FROM ".$db_table." WHERE id =".$id;
  543. $res = Database::query($sql);
  544. if ($res === false) {
  545. return false;
  546. }
  547. return true;
  548. }
  549. /**
  550. * Gets an announcement
  551. * @param int $id The identifier of the announcement that should be
  552. * @return object Object of class StdClass or the required class, containing the query result row
  553. */
  554. public static function get_announcement($id)
  555. {
  556. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  557. $id = intval($id);
  558. $sql = "SELECT * FROM ".$db_table." WHERE id = ".$id;
  559. $announcement = Database::fetch_object(Database::query($sql));
  560. return $announcement;
  561. }
  562. /**
  563. * Change the visibility of an announcement
  564. * @param int $announcement_id
  565. * @param int $user For who should the visibility be changed
  566. * (possible values are VISIBLE_TEACHER, VISIBLE_STUDENT, VISIBLE_GUEST)
  567. * @return bool True on success, false on failure
  568. */
  569. public static function set_visibility($announcement_id, $user, $visible)
  570. {
  571. $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  572. $visible = intval($visible);
  573. $announcement_id = intval($announcement_id);
  574. if (!in_array($user, array(self::VISIBLE_GUEST, self::VISIBLE_STUDENT, self::VISIBLE_TEACHER))) {
  575. return false;
  576. }
  577. $field = ($user == self::VISIBLE_TEACHER ? 'visible_teacher' : ($user == self::VISIBLE_STUDENT ? 'visible_student' : 'visible_guest'));
  578. $sql = "UPDATE ".$db_table." SET ".$field." = '".$visible."'
  579. WHERE id='".$announcement_id."'";
  580. $res = Database::query($sql);
  581. if ($res === false) {
  582. return false;
  583. }
  584. return true;
  585. }
  586. /**
  587. * Send a system announcement by e-mail to all teachers/students depending on parameters
  588. * @param string Title
  589. * @param string Content
  590. * @param int Whether to send to all teachers (1) or not (0)
  591. * @param int Whether to send to all students (1) or not (0)
  592. * @param string Language (optional, considered for all languages if left empty)
  593. * @return bool True if the message was sent or there was no destination matching. False on database or e-mail sending error.
  594. */
  595. public static function send_system_announcement_by_email($title, $content, $teacher, $student, $language = null, $sendEmailTest = false)
  596. {
  597. $content = stripslashes(
  598. str_replace(array('\r\n', '\n', '\r'), '', $content)
  599. );
  600. $now = api_get_utc_datetime();
  601. if ($sendEmailTest) {
  602. MessageManager::send_message_simple(api_get_user_id(), $title, $content);
  603. return true;
  604. }
  605. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  606. if (api_is_multiple_url_enabled()) {
  607. $current_access_url_id = api_get_current_access_url_id();
  608. $url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  609. $url_condition = " INNER JOIN $url_rel_user uu ON uu.user_id = u.user_id ";
  610. }
  611. if ($teacher <> 0 AND $student == 0) {
  612. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE status = '1' ";
  613. }
  614. if ($teacher == 0 AND $student <> 0) {
  615. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE status = '5' ";
  616. }
  617. if ($teacher<> 0 AND $student <> 0) {
  618. $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition WHERE 1 = 1 ";
  619. }
  620. if (!empty($language)) { //special condition because language was already treated for SQL insert before
  621. $sql .= " AND language = '".Database::escape_string($language)."' ";
  622. }
  623. if (api_is_multiple_url_enabled()) {
  624. $sql .= " AND access_url_id = '".$current_access_url_id."' ";
  625. }
  626. // Sent to active users.
  627. $sql .= " AND email <>'' AND active = 1 ";
  628. // Expiration date
  629. $sql .= " AND (expiration_date = '' OR expiration_date IS NULL OR expiration_date > '$now') ";
  630. if ((empty($teacher) or $teacher == '0') AND (empty($student) or $student == '0')) {
  631. return true;
  632. }
  633. $result = Database::query($sql);
  634. if ($result === false) {
  635. return false;
  636. }
  637. $message_sent = false;
  638. while ($row = Database::fetch_array($result,'ASSOC')) {
  639. MessageManager::send_message_simple($row['user_id'], $title, $content);
  640. $message_sent = true;
  641. }
  642. return $message_sent; //true if at least one e-mail was sent
  643. }
  644. /**
  645. * Displays announcements as an slideshow
  646. * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  647. * @param int $id The identifier of the announcement to display
  648. */
  649. public static function display_announcements_slider($visible, $id = null)
  650. {
  651. $user_selected_language = Database::escape_string(
  652. api_get_language_isocode()
  653. );
  654. $table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  655. $cut_size = 500;
  656. $now = api_get_utc_datetime();
  657. $sql = "SELECT * FROM " . $table . "
  658. WHERE
  659. (lang = '$user_selected_language' OR lang = '') AND
  660. ('$now' >= date_start AND '$now' <= date_end) ";
  661. switch ($visible) {
  662. case self::VISIBLE_GUEST :
  663. $sql .= " AND visible_guest = 1 ";
  664. break;
  665. case self::VISIBLE_STUDENT :
  666. $sql .= " AND visible_student = 1 ";
  667. break;
  668. case self::VISIBLE_TEACHER :
  669. $sql .= " AND visible_teacher = 1 ";
  670. break;
  671. }
  672. if (isset($id) && !empty($id)) {
  673. $id = intval($id);
  674. $sql .= " AND id = $id ";
  675. }
  676. if (api_is_multiple_url_enabled()) {
  677. $current_url_id = api_get_current_access_url_id();
  678. $sql .= " AND access_url_id IN ('1', '$current_url_id') ";
  679. }
  680. $sql .= " ORDER BY date_start DESC";
  681. $result = Database::query($sql);
  682. $announcements = [];
  683. if (Database::num_rows($result) > 0) {
  684. while ($announcement = Database::fetch_object($result)) {
  685. $announcementData = [
  686. 'id' => $announcement->id,
  687. 'title' => $announcement->title,
  688. 'content' => $announcement->content,
  689. 'readMore' => null
  690. ];
  691. if (empty($id)) {
  692. if (api_strlen(strip_tags($announcement->content)) > $cut_size) {
  693. $announcementData['content'] = cut($announcement->content, $cut_size);
  694. $announcementData['readMore'] = true;
  695. }
  696. }
  697. $announcements[] = $announcementData;
  698. }
  699. }
  700. if (count($announcements) === 0) {
  701. return null;
  702. }
  703. return Container::getTemplating()->render(
  704. '@template_style/announcement/slider.html.twig',
  705. [
  706. 'announcements' => $announcements
  707. ]
  708. );
  709. }
  710. /**
  711. * Get the HTML code for an announcement
  712. * @param int $announcementId The announcement ID
  713. * @param int $visibility The announcement visibility
  714. * @return string The HTML code
  715. */
  716. public static function displayAnnouncement($announcementId, $visibility)
  717. {
  718. $selectedUserLanguage = Database::escape_string(
  719. api_get_language_isocode()
  720. );
  721. $announcementTable = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  722. $now = api_get_utc_datetime();
  723. $whereConditions = [
  724. "(lang = ? OR lang IS NULL) " => $selectedUserLanguage,
  725. "AND (? >= date_start AND ? <= date_end) " => [$now, $now],
  726. "AND id = ? " => intval($announcementId)
  727. ];
  728. switch ($visibility) {
  729. case self::VISIBLE_GUEST :
  730. $whereConditions["AND visible_guest = ? "] = 1;
  731. break;
  732. case self::VISIBLE_STUDENT :
  733. $whereConditions["AND visible_student = ? "] = 1;
  734. break;
  735. case self::VISIBLE_TEACHER :
  736. $whereConditions["AND visible_teacher = ? "] = 1;
  737. break;
  738. }
  739. if (api_is_multiple_url_enabled()) {
  740. $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id();
  741. }
  742. $announcement = Database::select(
  743. "*",
  744. $announcementTable,
  745. [
  746. "where" => $whereConditions,
  747. "order" => "date_start"
  748. ], 'first'
  749. );
  750. return Container::getTemplating()->render(
  751. '@template_style/announcement/view.html.twig',
  752. [
  753. 'announcement' => $announcement
  754. ]
  755. );
  756. }
  757. }