system_announcements.lib.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. define('VISIBLE_GUEST', 1);
  4. define('VISIBLE_STUDENT', 2);
  5. define('VISIBLE_TEACHER', 3);
  6. /**
  7. * This is the system announcements library for Dokeos.
  8. *
  9. * @package chamilo.library
  10. */
  11. class SystemAnnouncementManager
  12. {
  13. /**
  14. * Displays all announcements
  15. * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  16. * @param int $id The identifier of the announcement to display
  17. */
  18. public static function display_announcements($visible, $id = -1) {
  19. $user_selected_language = api_get_interface_language();
  20. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  21. $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y %h:%i:%s') AS display_date FROM ".$db_table." WHERE (lang='$user_selected_language' OR lang IS NULL) AND ((NOW() BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
  22. switch ($visible) {
  23. case VISIBLE_GUEST :
  24. $sql .= " AND visible_guest = 1 ";
  25. break;
  26. case VISIBLE_STUDENT :
  27. $sql .= " AND visible_student = 1 ";
  28. break;
  29. case VISIBLE_TEACHER :
  30. $sql .= " AND visible_teacher = 1 ";
  31. break;
  32. }
  33. $sql .= " AND access_url_id = ".api_get_current_access_url_id()." ";
  34. $sql .= " ORDER BY date_start DESC LIMIT 0,7";
  35. $announcements = Database::query($sql);
  36. if (Database::num_rows($announcements) > 0) {
  37. $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  38. $query_string = ereg_replace('&$', '', $query_string);
  39. $url = api_get_self();
  40. echo '<div class="system_announcements">';
  41. echo '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  42. echo '<div style="margin:10px;text-align:right;"><a href="news_list.php">'.get_lang('More').'</a></div>';
  43. while ($announcement = Database::fetch_object($announcements)) {
  44. if ($id != $announcement->id) {
  45. if (strlen($query_string) > 0) {
  46. $show_url = 'news_list.php#'.$announcement->id;
  47. } else {
  48. $show_url = 'news_list.php#'.$announcement->id;
  49. }
  50. $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG, date_default_timezone_get());
  51. echo '<a name="'.$announcement->id.'"></a>
  52. <div class="system_announcement">
  53. <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>
  54. </div>';
  55. } else {
  56. echo '<div class="system_announcement">
  57. <div class="system_announcement_title">'
  58. .$announcement->display_date.'
  59. <a name="ann'.$announcement->id.'" href="'.$url.'?'.$query_string.'#ann'.$announcement->id.'">'.$announcement->title.'</a>
  60. </div>';
  61. }
  62. echo '<br />';
  63. }
  64. echo '</div>';
  65. }
  66. return;
  67. }
  68. public static function display_all_announcements($visible, $id = -1,$start = 0,$user_id='') {
  69. $user_selected_language = api_get_interface_language();
  70. $start = intval($start);
  71. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  72. $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y %h:%i:%s') AS display_date FROM ".$db_table."
  73. WHERE (lang='$user_selected_language' OR lang IS NULL) AND ((NOW() BETWEEN date_start AND date_end) OR date_end='0000-00-00')";
  74. switch ($visible) {
  75. case VISIBLE_GUEST :
  76. $sql .= " AND visible_guest = 1 ";
  77. break;
  78. case VISIBLE_STUDENT :
  79. $sql .= " AND visible_student = 1 ";
  80. break;
  81. case VISIBLE_TEACHER :
  82. $sql .= " AND visible_teacher = 1 ";
  83. break;
  84. }
  85. global $_configuration;
  86. $current_access_url_id = 1;
  87. if ($_configuration['multiple_access_urls']) {
  88. $current_access_url_id = api_get_current_access_url_id();
  89. }
  90. $sql .= " AND access_url_id = '$current_access_url_id' ";
  91. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  92. $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20";
  93. } else {
  94. $sql .= " ORDER BY date_start DESC LIMIT ".($start+1).",20";
  95. }
  96. $announcements = Database::query($sql);
  97. if (Database::num_rows($announcements) > 0) {
  98. $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  99. $query_string = ereg_replace('&$', '', $query_string);
  100. $url = api_get_self();
  101. echo '<div class="system_announcements">';
  102. echo '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  103. echo '<table align="center">';
  104. echo '<tr>';
  105. echo '<td>';
  106. SystemAnnouncementManager :: display_arrow($user_id);
  107. echo '</td>';
  108. echo '</tr>';
  109. echo '</table>';
  110. echo '<table align="center" border="0" width="900px">';
  111. while ($announcement = Database::fetch_object($announcements)) {
  112. $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
  113. echo '<tr><td>';
  114. echo '<a name="'.$announcement->id.'"></a>
  115. <div class="system_announcement">
  116. <div class="system_announcement_title">'.$announcement->title.'</div><div class="system_announcement_date">'.$display_date.'</div>
  117. <br />
  118. <div class="system_announcement_content">'
  119. .$announcement->content.'
  120. </div>
  121. </div><br />';
  122. echo '</tr></td>';
  123. }
  124. echo '</table>';
  125. echo '<table align="center">';
  126. echo '<tr>';
  127. echo '<td>';
  128. SystemAnnouncementManager :: display_arrow($user_id);
  129. echo '</td>';
  130. echo '</tr>';
  131. echo '</table>';
  132. echo '</div>';
  133. }
  134. }
  135. public static function display_arrow($user_id) {
  136. $start = (int)$_GET['start'];
  137. $nb_announcement = SystemAnnouncementManager :: count_nb_announcement($start,$user_id);
  138. $next = ((int)$_GET['start']+19);
  139. $prev = ((int)$_GET['start']-19);
  140. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  141. if($nb_announcement > 20) {
  142. echo '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  143. }
  144. } else {
  145. echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
  146. if ($nb_announcement > 20) {
  147. echo '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  148. }
  149. }
  150. }
  151. public static function count_nb_announcement($start = 0, $user_id = '') {
  152. $start = intval($start);
  153. $visibility = api_is_allowed_to_create_course() ? VISIBLE_TEACHER : VISIBLE_STUDENT;
  154. $user_selected_language = api_get_interface_language();
  155. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  156. $sql = 'SELECT id FROM '.$db_table.' WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
  157. if (isset($user_id)) {
  158. switch ($visibility) {
  159. case VISIBLE_GUEST :
  160. $sql .= " AND visible_guest = 1 ";
  161. break;
  162. case VISIBLE_STUDENT :
  163. $sql .= " AND visible_student = 1 ";
  164. break;
  165. case VISIBLE_TEACHER :
  166. $sql .= " AND visible_teacher = 1 ";
  167. break;
  168. }
  169. }
  170. global $_configuration;
  171. $current_access_url_id = 1;
  172. if ($_configuration['multiple_access_urls']) {
  173. $current_access_url_id = api_get_current_access_url_id();
  174. }
  175. $sql .= " AND access_url_id = '$current_access_url_id' ";
  176. $sql .= 'LIMIT '.$start.',21';
  177. $announcements = Database::query($sql);
  178. $i = 0;
  179. while($rows = Database::fetch_array($announcements)) {
  180. $i++;
  181. }
  182. return $i;
  183. }
  184. /**
  185. * Get all announcements
  186. * @return array An array with all available system announcements (as php
  187. * objects)
  188. */
  189. public static function get_all_announcements()
  190. {
  191. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  192. $sql = "SELECT *, IF( NOW() BETWEEN date_start AND date_end, '1', '0') AS visible FROM ".$db_table." ";
  193. global $_configuration;
  194. $current_access_url_id = 1;
  195. if ($_configuration['multiple_access_urls']) {
  196. $current_access_url_id = api_get_current_access_url_id();
  197. }
  198. $sql .= " WHERE access_url_id = '$current_access_url_id' ";
  199. $sql .= "ORDER BY date_start ASC";
  200. $announcements = Database::query($sql);
  201. $all_announcements = array();
  202. while ($announcement = Database::fetch_object($announcements)) {
  203. $all_announcements[] = $announcement;
  204. }
  205. return $all_announcements;
  206. }
  207. /**
  208. * Adds an announcement to the database
  209. * @param string Title of the announcement
  210. * @param string Content of the announcement
  211. * @param string Start date (YYYY-MM-DD HH:II: SS)
  212. * @param string End date (YYYY-MM-DD HH:II: SS)
  213. * @param int Whether the announcement should be visible to teachers (1) or not (0)
  214. * @param int Whether the announcement should be visible to students (1) or not (0)
  215. * @param int Whether the announcement should be visible to anonymous users (1) or not (0)
  216. * @param string The language for which the announvement should be shown. Leave null for all langages
  217. * @param int Whether to send an e-mail to all users (1) or not (0)
  218. * @return bool True on success, false on failure
  219. */
  220. 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 ) {
  221. $a_dateS = explode(' ',$date_start);
  222. $a_arraySD = explode('-',$a_dateS[0]);
  223. $a_arraySH = explode(':',$a_dateS[1]);
  224. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  225. $a_dateE = explode(' ',$date_end);
  226. $a_arrayED = explode('-',$a_dateE[0]);
  227. $a_arrayEH = explode(':',$a_dateE[1]);
  228. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  229. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  230. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  231. Display :: display_normal_message(get_lang('InvalidStartDate'));
  232. return false;
  233. }
  234. 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])) {
  235. Display :: display_normal_message(get_lang('InvalidEndDate'));
  236. return false;
  237. }
  238. if( strlen(trim($title)) == 0) {
  239. Display::display_normal_message(get_lang('InvalidTitle'));
  240. return false;
  241. }
  242. $start = api_get_utc_datetime($date_start);
  243. $end = api_get_utc_datetime($date_end);
  244. $title = Database::escape_string($title);
  245. $content = Database::escape_string($content);
  246. //Fixing urls that are sent by email
  247. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  248. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  249. $langsql = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
  250. global $_configuration;
  251. $current_access_url_id = 1;
  252. if ($_configuration['multiple_access_urls']) {
  253. $current_access_url_id = api_get_current_access_url_id();
  254. }
  255. $sql = "INSERT INTO ".$db_table." (title,content,date_start,date_end,visible_teacher,visible_student,visible_guest, lang, access_url_id)
  256. VALUES ('".$title."','".$content."','".$start."','".$end."','".$visible_teacher."','".$visible_student."','".$visible_guest."',".$langsql.", ".$current_access_url_id.")";
  257. if ($send_mail==1) {
  258. SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student, $lang);
  259. }
  260. $res = Database::query($sql);
  261. if ($res === false) {
  262. Debug::log_s(mysql_error());
  263. return false;
  264. }
  265. if ($add_to_calendar) {
  266. require_once 'calendar.lib.php';
  267. $agenda_id = agenda_add_item($title, $content, $date_start, $date_end);
  268. }
  269. return true;
  270. }
  271. /**
  272. * Updates an announcement to the database
  273. * @param integer $id : id of the announcement
  274. * @param string $title : title of the announcement
  275. * @param string $content : content of the announcement
  276. * @param array $date_start: start date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  277. * @param array $date_end : end date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  278. * @return bool True on success, false on failure
  279. */
  280. 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) {
  281. $a_dateS = explode(' ',$date_start);
  282. $a_arraySD = explode('-',$a_dateS[0]);
  283. $a_arraySH = explode(':',$a_dateS[1]);
  284. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  285. $a_dateE = explode(' ',$date_end);
  286. $a_arrayED = explode('-',$a_dateE[0]);
  287. $a_arrayEH = explode(':',$a_dateE[1]);
  288. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  289. $langsql = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
  290. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  291. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  292. Display :: display_normal_message(get_lang('InvalidStartDate'));
  293. return false;
  294. }
  295. 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])) {
  296. Display :: display_normal_message(get_lang('InvalidEndDate'));
  297. return false;
  298. }
  299. if( strlen(trim($title)) == 0) {
  300. Display::display_normal_message(get_lang('InvalidTitle'));
  301. return false;
  302. }
  303. $start = api_get_utc_datetime($date_start);
  304. $end = api_get_utc_datetime($date_end);
  305. $title = Database::escape_string($title);
  306. $content = Database::escape_string($content);
  307. //Fixing urls that are sent by email
  308. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  309. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  310. $id = intval($id);
  311. $sql = "UPDATE ".$db_table." SET lang=$langsql,title='".$title."',content='".$content."',date_start='".$start."',date_end='".$end."', ";
  312. $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;
  313. if ($send_mail==1) {
  314. SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student, $lang);
  315. }
  316. $res = Database::query($sql);
  317. if ($res === false) {
  318. Debug::log_s(mysql_error());
  319. return false;
  320. }
  321. return true;
  322. }
  323. /**
  324. * Deletes an announcement
  325. * @param int $id The identifier of the announcement that should be
  326. * @return bool True on success, false on failure
  327. */
  328. public static function delete_announcement($id) {
  329. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  330. $id = intval($id);
  331. $sql = "DELETE FROM ".$db_table." WHERE id =".$id;
  332. $res = Database::query($sql);
  333. if ($res === false) {
  334. Debug::log_s(mysql_error());
  335. return false;
  336. }
  337. return true;
  338. }
  339. /**
  340. * Gets an announcement
  341. * @param int $id The identifier of the announcement that should be
  342. * @return object Object of class StdClass or the required class, containing the query result row
  343. */
  344. public static function get_announcement($id) {
  345. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  346. $id = intval($id);
  347. $sql = "SELECT * FROM ".$db_table." WHERE id = ".$id;
  348. $announcement = Database::fetch_object(Database::query($sql));
  349. return $announcement;
  350. }
  351. /**
  352. * Change the visibility of an announcement
  353. * @param int $announcement_id
  354. * @param int $user For who should the visibility be changed (possible values are VISIBLE_TEACHER, VISIBLE_STUDENT, VISIBLE_GUEST)
  355. * @return bool True on success, false on failure
  356. */
  357. public static function set_visibility($announcement_id, $user, $visible) {
  358. $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  359. $visible = intval($visible);
  360. $announcement_id = intval($announcement_id);
  361. $field = ($user == VISIBLE_TEACHER ? 'visible_teacher' : ($user == VISIBLE_STUDENT ? 'visible_student' : 'visible_guest'));
  362. $sql = "UPDATE ".$db_table." SET ".$field." = '".$visible."' WHERE id='".$announcement_id."'";
  363. $res = Database::query($sql);
  364. if ($res === false) {
  365. Debug::log_s(mysql_error());
  366. return false;
  367. }
  368. return true;
  369. }
  370. /**
  371. * Send a system announcement by e-mail to all teachers/students depending on parameters
  372. * @param string Title
  373. * @param string Content
  374. * @param int Whether to send to all teachers (1) or not (0)
  375. * @param int Whether to send to all students (1) or not (0)
  376. * @param string Language (optional, considered for all languages if left empty)
  377. * @return bool True if the message was sent or there was no destination matching. False on database or e-mail sending error.
  378. */
  379. public static function send_system_announcement_by_email($title, $content, $teacher, $student, $language=null) {
  380. global $_user;
  381. global $_setting;
  382. global $charset;
  383. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  384. if ($teacher <> 0 AND $student == 0) {
  385. $sql = "SELECT firstname, lastname, email, status FROM $user_table WHERE email<>'' AND status = '1' AND active = 1";
  386. }
  387. if ($teacher == 0 AND $student <> 0) {
  388. $sql = "SELECT firstname, lastname, email, status FROM $user_table WHERE email<>'' AND status = '5' AND active = 1 ";
  389. }
  390. if ($teacher<> 0 AND $student <> 0) {
  391. $sql = "SELECT firstname, lastname, email FROM $user_table WHERE email<>'' AND active = 1 ";
  392. }
  393. if (!empty($language)) { //special condition because language was already treated for SQL insert before
  394. $sql .= " AND language = '".Database::escape_string($language)."' ";
  395. }
  396. global $_configuration;
  397. $current_access_url_id = 1;
  398. if ($_configuration['multiple_access_urls']) {
  399. $current_access_url_id = api_get_current_access_url_id();
  400. }
  401. $sql .= " AND access_url_id = '".$current_access_url_id."' ";
  402. if ((empty($teacher) or $teacher == '0') AND (empty($student) or $student == '0')) {
  403. return true;
  404. }
  405. $result = Database::query($sql);
  406. if ($result === false) {
  407. return false;
  408. }
  409. while($row = Database::fetch_array($result,'ASSOC')) {
  410. $res = @api_mail_html(api_get_person_name($row['firstname'], $row['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $row['email'], api_html_entity_decode(stripslashes($title), ENT_QUOTES, $charset), api_html_entity_decode(stripslashes(str_replace(array('\r\n', '\n', '\r'),'',$content)), ENT_QUOTES, $charset), api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS), api_get_setting('emailAdministrator')) || $res;
  411. }
  412. return $res; //true if at least one e-mail was sent
  413. }
  414. }