system_announcements.lib.php 25 KB

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