system_announcements.lib.php 29 KB

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