diff --git a/main/inc/lib/page.lib.php b/main/inc/lib/page.lib.php index 9b8a1b87c1..6c226ed0db 100644 --- a/main/inc/lib/page.lib.php +++ b/main/inc/lib/page.lib.php @@ -384,7 +384,7 @@ class PageController * @assert () != '' * @assert (1) != '' */ - public function return_announcements($user_id = null, $show_slide = true) + public function getAnnouncements($user_id = null, $show_slide = true) { // Display System announcements $announcement = isset($_GET['announcement']) ? intval($_GET['announcement']) : null; @@ -393,18 +393,18 @@ class PageController $visibility = api_is_allowed_to_create_course( ) ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT; if ($show_slide) { - $announcements = SystemAnnouncementManager :: display_announcements_slider($visibility, $announcement); + $announcements = SystemAnnouncementManager::display_announcements_slider($visibility, $announcement); } else { - $announcements = SystemAnnouncementManager :: display_all_announcements($visibility, $announcement); + $announcements = SystemAnnouncementManager::display_all_announcements($visibility, $announcement); } } else { if ($show_slide) { - $announcements = SystemAnnouncementManager :: display_announcements_slider( + $announcements = SystemAnnouncementManager::display_announcements_slider( SystemAnnouncementManager::VISIBLE_GUEST, $announcement ); } else { - $announcements = SystemAnnouncementManager :: display_all_announcements( + $announcements = SystemAnnouncementManager::display_all_announcements( SystemAnnouncementManager::VISIBLE_GUEST, $announcement ); diff --git a/main/inc/lib/system_announcements.lib.php b/main/inc/lib/system_announcements.lib.php index 80c8390c78..7180d8b5c2 100644 --- a/main/inc/lib/system_announcements.lib.php +++ b/main/inc/lib/system_announcements.lib.php @@ -624,33 +624,41 @@ class SystemAnnouncementManager return $message_sent; //true if at least one e-mail was sent } - /** - * Displays announcements as an slideshow - * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER - * @param int $id The identifier of the announcement to display - */ - public static function display_announcements_slider($visible, $id = null) + /** + * @param $visible + * @param null $id + * @param string $type + * @param bool $getCount + * @param int $cutSize + * @return string + */ + public static function getAnnouncements($visible, $id = null, $type = 'resumed', $getCount = false, $cutSize = 800) { - $user_selected_language = Database::escape_string(api_get_interface_language()); - $table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); - - $cut_size = 800; - $now = api_get_utc_datetime(); + $user_selected_language = Database::escape_string(api_get_interface_language()); + $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); + $now = api_get_utc_datetime(); + $select = '*'; + if ($getCount) { + $select = 'count(*) as count'; + } - $sql = "SELECT * FROM ".$table." - WHERE ( lang = '$user_selected_language' OR lang IS NULL) AND ( '$now' >= date_start AND '$now' <= date_end) "; + $sql = "SELECT $select + FROM $table + WHERE + ( lang = '$user_selected_language' OR lang IS NULL) AND + ( '$now' >= date_start AND '$now' <= date_end) "; - switch ($visible) { - case self::VISIBLE_GUEST : - $sql .= " AND visible_guest = 1 "; - break; - case self::VISIBLE_STUDENT : - $sql .= " AND visible_student = 1 "; - break; - case self::VISIBLE_TEACHER : - $sql .= " AND visible_teacher = 1 "; - break; - } + switch ($visible) { + case self::VISIBLE_GUEST : + $sql .= " AND visible_guest = 1 "; + break; + case self::VISIBLE_STUDENT : + $sql .= " AND visible_student = 1 "; + break; + case self::VISIBLE_TEACHER : + $sql .= " AND visible_teacher = 1 "; + break; + } if (isset($id) && !empty($id)) { $id = intval($id); @@ -662,28 +670,73 @@ class SystemAnnouncementManager $sql .= " AND access_url_id IN ('1', '$current_url_id') "; } - $sql .= " ORDER BY date_start DESC"; - $announcements = Database::query($sql); - $html = ''; - if (Database::num_rows($announcements) > 0) { - $html .= Display::page_header(get_lang('SystemAnnouncements')); - $options = array(); - while ($announcement = Database::fetch_object($announcements)) { + $sql .= " ORDER BY date_start DESC"; + $announcements = Database::query($sql); + if ($getCount) { + $announcement = Database::fetch_array($announcements); + return $announcement['count']; + } + + $options = array(); + if (Database::num_rows($announcements) > 0) { + while ($announcement = Database::fetch_object($announcements)) { $content = $announcement->content; $url = api_get_path(WEB_PUBLIC_PATH).'news/'.$announcement->id; if (empty($id)) { - if (api_strlen(strip_tags($content)) > $cut_size) { - $content = Text::cut($announcement->content, $cut_size).' '.Display::url(get_lang('More'), $url); + if ($type == 'resumed') { + if (api_strlen(strip_tags($content)) > $cutSize) { + $content = Security::remove_XSS(Text::cut($announcement->content, $cutSize)). + ' '.Display::url(get_lang('More'), $url); + } + } else { + $content = $announcement->content; } } + $announcement->title = Text::cut($announcement->title, $cutSize); $options[]= array( 'title' => $announcement->title, 'content' => $content, ); - } - $html .= Display::getSlider('portal_news', $options); + } + } + return $options; + } + /** + * Displays announcements as an slideshow + * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER + * @param int $id The identifier of the announcement to display + * @return string + */ + public static function display_announcements_slider($visible, $id = null) + { + $announcements = self::getAnnouncements($visible, $id, 'resumed'); + $html = null; + if (!empty($announcements)) { + $html .= Display::page_header(get_lang('SystemAnnouncements')); + $html .= Display::getSlider('portal_news', $announcements); } return $html; } + + /** + * Displays announcements as an slideshow + * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER + * @param int $id The identifier of the announcement to display + * @param string $type + * @return string + */ + public static function displayAnnouncementsList($visible, $id = null, $type = 'resumed') + { + $announcements = self::getAnnouncements($visible, $id, $type); + $html = null; + if (!empty($announcements)) { + $html .= Display::page_header(get_lang('SystemAnnouncements')); + foreach($announcements as $announcement) { + $html .= Display::page_subheader2($announcement['title']); + $html .= "

".$announcement['content']."

"; + } + } + return $html; + } } diff --git a/main/inc/routes.php b/main/inc/routes.php index 268131d2ad..49acfe27a8 100644 --- a/main/inc/routes.php +++ b/main/inc/routes.php @@ -569,6 +569,10 @@ $app->match('/certificates/{id}', 'certificate.controller:indexAction', 'GET'); /** Portal news */ $app->match('/news/{id}', 'news.controller:indexAction', 'GET') + ->bind('portal_news_per_id'); + +/** Portal news */ +$app->match('/news', 'news.controller:newsAction', 'GET') ->bind('portal_news'); /** LP controller (subscribe users to a LP) */ diff --git a/src/ChamiloLMS/Controller/IndexController.php b/src/ChamiloLMS/Controller/IndexController.php index 8f8e254933..8ede37475d 100644 --- a/src/ChamiloLMS/Controller/IndexController.php +++ b/src/ChamiloLMS/Controller/IndexController.php @@ -133,7 +133,7 @@ class IndexController extends CommonController if (api_get_setting('show_hot_courses') == 'true') { $hotCourses = $pageController->returnHotCourses(); } - $announcementsBlock = $pageController->return_announcements(); + $announcementsBlock = $pageController->getAnnouncements(); } $template->assign('hot_courses', $hotCourses); diff --git a/src/ChamiloLMS/Controller/NewsController.php b/src/ChamiloLMS/Controller/NewsController.php index bd1347dedf..79384dbd78 100644 --- a/src/ChamiloLMS/Controller/NewsController.php +++ b/src/ChamiloLMS/Controller/NewsController.php @@ -29,7 +29,7 @@ class NewsController } else { $visibility = api_is_allowed_to_create_course() ? \SystemAnnouncementManager::VISIBLE_TEACHER : \SystemAnnouncementManager::VISIBLE_STUDENT; } - $content = \SystemAnnouncementManager::display_announcements_slider($visibility, $id); + $content = \SystemAnnouncementManager::displayAnnouncementsList($visibility, $id, 'full'); $app['template']->assign('content', $content); $app['template']->assign('actions', $actions); @@ -37,4 +37,22 @@ class NewsController return new Response($response, 200, array()); } + + /** + * @param Application $app + * @return Response + */ + public function newsAction(Application $app) + { + if (api_is_anonymous()) { + $visibility = \SystemAnnouncementManager::VISIBLE_GUEST; + } else { + $visibility = api_is_allowed_to_create_course() ? \SystemAnnouncementManager::VISIBLE_TEACHER : \SystemAnnouncementManager::VISIBLE_STUDENT; + } + $content = \SystemAnnouncementManager::displayAnnouncementsList($visibility, null, 'resumed'); + $app['template']->assign('content', $content); + $response = $app['template']->renderLayout('layout_1_col.tpl'); + + return new Response($response, 200, array()); + } }