system_announcements.lib.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. {
  137. $start = (int)$_GET['start'];
  138. $nb_announcement = SystemAnnouncementManager :: count_nb_announcement($start,$user_id);
  139. $next = ((int)$_GET['start']+19);
  140. $prev = ((int)$_GET['start']-19);
  141. if(!isset($_GET['start']) || $_GET['start'] == 0) {
  142. if($nb_announcement > 20) {
  143. echo '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  144. }
  145. } else {
  146. echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
  147. if($nb_announcement > 20) {
  148. echo '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
  149. }
  150. }
  151. }
  152. public static function count_nb_announcement($start = 0,$user_id = '')
  153. {
  154. $start = intval($start);
  155. $visibility = api_is_allowed_to_create_course() ? VISIBLE_TEACHER : VISIBLE_STUDENT;
  156. $user_selected_language = api_get_interface_language();
  157. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  158. $sql = 'SELECT id
  159. FROM '.$db_table.'
  160. WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
  161. if (isset($user_id)) {
  162. switch ($visibility)
  163. {
  164. case VISIBLE_GUEST :
  165. $sql .= " AND visible_guest = 1 ";
  166. break;
  167. case VISIBLE_STUDENT :
  168. $sql .= " AND visible_student = 1 ";
  169. break;
  170. case VISIBLE_TEACHER :
  171. $sql .= " AND visible_teacher = 1 ";
  172. break;
  173. }
  174. }
  175. global $_configuration;
  176. $current_access_url_id = 1;
  177. if ($_configuration['multiple_access_urls']) {
  178. $current_access_url_id = api_get_current_access_url_id();
  179. }
  180. $sql .= " AND access_url_id = '$current_access_url_id' ";
  181. $sql .= 'LIMIT '.$start.',21';
  182. $announcements = Database::query($sql);
  183. $i = 0;
  184. while($rows = Database::fetch_array($announcements))
  185. {
  186. $i++;
  187. }
  188. return $i;
  189. }
  190. /**
  191. * Get all announcements
  192. * @return array An array with all available system announcements (as php
  193. * objects)
  194. */
  195. public static function get_all_announcements()
  196. {
  197. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  198. $sql = "SELECT *, IF( NOW() BETWEEN date_start AND date_end, '1', '0') AS visible FROM ".$db_table." ";
  199. global $_configuration;
  200. $current_access_url_id = 1;
  201. if ($_configuration['multiple_access_urls']) {
  202. $current_access_url_id = api_get_current_access_url_id();
  203. }
  204. $sql .= " WHERE access_url_id = '$current_access_url_id' ";
  205. $sql .= "ORDER BY date_start ASC";
  206. $announcements = Database::query($sql);
  207. $all_announcements = array();
  208. while ($announcement = Database::fetch_object($announcements)) {
  209. $all_announcements[] = $announcement;
  210. }
  211. return $all_announcements;
  212. }
  213. /**
  214. * Adds an announcement to the database
  215. * @param string Title of the announcement
  216. * @param string Content of the announcement
  217. * @param string Start date (YYYY-MM-DD HH:II: SS)
  218. * @param string End date (YYYY-MM-DD HH:II: SS)
  219. * @param int Whether the announcement should be visible to teachers (1) or not (0)
  220. * @param int Whether the announcement should be visible to students (1) or not (0)
  221. * @param int Whether the announcement should be visible to anonymous users (1) or not (0)
  222. * @param string The language for which the announvement should be shown. Leave null for all langages
  223. * @param int Whether to send an e-mail to all users (1) or not (0)
  224. * @return bool True on success, false on failure
  225. */
  226. 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 ) {
  227. $a_dateS = explode(' ',$date_start);
  228. $a_arraySD = explode('-',$a_dateS[0]);
  229. $a_arraySH = explode(':',$a_dateS[1]);
  230. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  231. $a_dateE = explode(' ',$date_end);
  232. $a_arrayED = explode('-',$a_dateE[0]);
  233. $a_arrayEH = explode(':',$a_dateE[1]);
  234. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  235. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  236. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  237. Display :: display_normal_message(get_lang('InvalidStartDate'));
  238. return false;
  239. }
  240. 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])) {
  241. Display :: display_normal_message(get_lang('InvalidEndDate'));
  242. return false;
  243. }
  244. if( strlen(trim($title)) == 0) {
  245. Display::display_normal_message(get_lang('InvalidTitle'));
  246. return false;
  247. }
  248. $start = api_get_utc_datetime($date_start);
  249. $end = api_get_utc_datetime($date_end);
  250. $title = Database::escape_string($title);
  251. $content = Database::escape_string($content);
  252. //Fixing urls that are sent by email
  253. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  254. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  255. $langsql = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
  256. global $_configuration;
  257. $current_access_url_id = 1;
  258. if ($_configuration['multiple_access_urls']) {
  259. $current_access_url_id = api_get_current_access_url_id();
  260. }
  261. $sql = "INSERT INTO ".$db_table." (title,content,date_start,date_end,visible_teacher,visible_student,visible_guest, lang, access_url_id)
  262. VALUES ('".$title."','".$content."','".$start."','".$end."','".$visible_teacher."','".$visible_student."','".$visible_guest."',".$langsql.", ".$current_access_url_id.")";
  263. if ($send_mail==1) {
  264. SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student, $lang);
  265. }
  266. $res = Database::query($sql);
  267. if ($res === false) {
  268. Debug::log_s(mysql_error());
  269. return false;
  270. }
  271. if ($add_to_calendar) {
  272. require_once 'calendar.lib.php';
  273. $agenda_id = agenda_add_item($title, $content, $date_start, $date_end);
  274. }
  275. return true;
  276. }
  277. /**
  278. * Updates an announcement to the database
  279. * @param integer $id : id of the announcement
  280. * @param string $title : title of the announcement
  281. * @param string $content : content of the announcement
  282. * @param array $date_start: start date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  283. * @param array $date_end : end date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
  284. * @return bool True on success, false on failure
  285. */
  286. 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) {
  287. $a_dateS = explode(' ',$date_start);
  288. $a_arraySD = explode('-',$a_dateS[0]);
  289. $a_arraySH = explode(':',$a_dateS[1]);
  290. $date_start_to_compare = array_merge($a_arraySD,$a_arraySH);
  291. $a_dateE = explode(' ',$date_end);
  292. $a_arrayED = explode('-',$a_dateE[0]);
  293. $a_arrayEH = explode(':',$a_dateE[1]);
  294. $date_end_to_compare = array_merge($a_arrayED,$a_arrayEH);
  295. $langsql = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
  296. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  297. if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
  298. Display :: display_normal_message(get_lang('InvalidStartDate'));
  299. return false;
  300. }
  301. 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])) {
  302. Display :: display_normal_message(get_lang('InvalidEndDate'));
  303. return false;
  304. }
  305. if( strlen(trim($title)) == 0) {
  306. Display::display_normal_message(get_lang('InvalidTitle'));
  307. return false;
  308. }
  309. $start = api_get_utc_datetime($date_start);
  310. $end = api_get_utc_datetime($date_end);
  311. $title = Database::escape_string($title);
  312. $content = Database::escape_string($content);
  313. //Fixing urls that are sent by email
  314. $content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
  315. $content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
  316. $id = intval($id);
  317. $sql = "UPDATE ".$db_table." SET lang=$langsql,title='".$title."',content='".$content."',date_start='".$start."',date_end='".$end."', ";
  318. $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;
  319. if ($send_mail==1) {
  320. SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student, $lang);
  321. }
  322. $res = Database::query($sql);
  323. if ($res === false) {
  324. Debug::log_s(mysql_error());
  325. return false;
  326. }
  327. return true;
  328. }
  329. /**
  330. * Deletes an announcement
  331. * @param int $id The identifier of the announcement that should be
  332. * @return bool True on success, false on failure
  333. */
  334. public static function delete_announcement($id) {
  335. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  336. $id = intval($id);
  337. $sql = "DELETE FROM ".$db_table." WHERE id =".$id;
  338. $res = Database::query($sql);
  339. if ($res === false) {
  340. Debug::log_s(mysql_error());
  341. return false;
  342. }
  343. return true;
  344. }
  345. /**
  346. * Gets an announcement
  347. * @param int $id The identifier of the announcement that should be
  348. * @return object Object of class StdClass or the required class, containing the query result row
  349. */
  350. public static function get_announcement($id) {
  351. $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  352. $id = intval($id);
  353. $sql = "SELECT * FROM ".$db_table." WHERE id = ".$id;
  354. $announcement = Database::fetch_object(Database::query($sql));
  355. return $announcement;
  356. }
  357. /**
  358. * Change the visibility of an announcement
  359. * @param int $announcement_id
  360. * @param int $user For who should the visibility be changed (possible values are VISIBLE_TEACHER, VISIBLE_STUDENT, VISIBLE_GUEST)
  361. * @return bool True on success, false on failure
  362. */
  363. public static function set_visibility($announcement_id, $user, $visible) {
  364. $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  365. $visible = intval($visible);
  366. $announcement_id = intval($announcement_id);
  367. $field = ($user == VISIBLE_TEACHER ? 'visible_teacher' : ($user == VISIBLE_STUDENT ? 'visible_student' : 'visible_guest'));
  368. $sql = "UPDATE ".$db_table." SET ".$field." = '".$visible."' WHERE id='".$announcement_id."'";
  369. $res = Database::query($sql);
  370. if ($res === false) {
  371. Debug::log_s(mysql_error());
  372. return false;
  373. }
  374. return true;
  375. }
  376. /**
  377. * Send a system announcement by e-mail to all teachers/students depending on parameters
  378. * @param string Title
  379. * @param string Content
  380. * @param int Whether to send to all teachers (1) or not (0)
  381. * @param int Whether to send to all students (1) or not (0)
  382. * @param string Language (optional, considered for all languages if left empty)
  383. * @return bool True if the message was sent or there was no destination matching. False on database or e-mail sending error.
  384. */
  385. public static function send_system_announcement_by_email($title, $content, $teacher, $student, $language=null) {
  386. global $_user;
  387. global $_setting;
  388. global $charset;
  389. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  390. if ($teacher <> 0 AND $student == 0) {
  391. $sql = "SELECT firstname, lastname, email, status FROM $user_table WHERE email<>'' AND status = '1' AND active = 1";
  392. }
  393. if ($teacher == 0 AND $student <> 0) {
  394. $sql = "SELECT firstname, lastname, email, status FROM $user_table WHERE email<>'' AND status = '5' AND active = 1 ";
  395. }
  396. if ($teacher<> 0 AND $student <> 0) {
  397. $sql = "SELECT firstname, lastname, email FROM $user_table WHERE email<>'' AND active = 1 ";
  398. }
  399. if (!empty($language)) { //special condition because language was already treated for SQL insert before
  400. $sql .= " AND language = '".Database::escape_string($language)."' ";
  401. }
  402. global $_configuration;
  403. $current_access_url_id = 1;
  404. if ($_configuration['multiple_access_urls']) {
  405. $current_access_url_id = api_get_current_access_url_id();
  406. }
  407. $sql .= " AND access_url_id = '".$current_access_url_id."' ";
  408. if ((empty($teacher) or $teacher == '0') AND (empty($student) or $student == '0')) {
  409. return true;
  410. }
  411. $result = Database::query($sql);
  412. if ($result === false) {
  413. return false;
  414. }
  415. while($row = Database::fetch_array($result,'ASSOC')) {
  416. $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;
  417. }
  418. return $res; //true if at least one e-mail was sent
  419. }
  420. }