Move system announcements as a controller

- Use symfony twig to render tpls
- Remove double code
pull/2715/head
Julio Montoya 6 years ago
parent 5bf2ee33b7
commit 87bc659c2a
  1. 2
      config/packages/sonata_page.yaml
  2. 3
      index.php
  3. 30
      main/inc/lib/system_announcements.lib.php
  4. 45
      main/inc/lib/userportal.lib.php
  5. 16
      main/template/default/announcement/view.html.twig
  6. 31
      news_list.php
  7. 6
      src/CoreBundle/Controller/IndexController.php
  8. 81
      src/CoreBundle/Controller/NewsController.php
  9. 49
      src/CoreBundle/Framework/PageController.php
  10. 6
      src/CoreBundle/Resources/views/Index/index.html.twig
  11. 1
      src/CoreBundle/Resources/views/Index/userportal.html.twig
  12. 8
      src/CoreBundle/Resources/views/News/index.html.twig
  13. 2
      src/CoreBundle/Resources/views/News/slider.html.twig
  14. 24
      src/CoreBundle/Resources/views/News/view.html.twig

@ -37,6 +37,8 @@ sonata_page:
- ^/js/(.*)
- ^/faq/(.*)
- ^/faq
- ^/news
- ^/news/(.*)
- ^/courses/(.*)
- ^/editor/(.*)
- ^/resource/(.*)

@ -145,14 +145,15 @@ if (!isset($_REQUEST['include'])) {
if (api_get_setting('show_hot_courses') == 'true') {
$hotCourses = $controller->return_hot_courses();
}
$announcements_block = $controller->return_announcements();
}
if (api_get_configuration_value('show_hot_sessions') === true) {
$hotSessions = SessionManager::getHotSessions();
$controller->tpl->assign('hot_sessions', $hotSessions);
}
$controller->tpl->assign('hot_courses', $hotCourses);
$controller->tpl->assign('announcements_block', $announcements_block);
if ($includeFile) {
// If we are including a static page, then home_welcome is empty
$controller->tpl->assign('home_welcome', '');

@ -820,9 +820,9 @@ class SystemAnnouncementManager
* @param string $visible see self::VISIBLE_* constants
* @param int $id The identifier of the announcement to display
*
* @return string
* @return array
*/
public static function displayAnnouncementsSlider($visible, $id = null)
public static function getAnnouncements($visible, $id = null) : array
{
$user_selected_language = Database::escape_string(api_get_interface_language());
$table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
@ -837,7 +837,7 @@ class SystemAnnouncementManager
$sql .= self::getVisibilityCondition($visible);
if (isset($id) && !empty($id)) {
$id = intval($id);
$id = (int) $id;
$sql .= " AND id = $id ";
}
@ -846,7 +846,7 @@ class SystemAnnouncementManager
$sql .= " AND access_url_id IN ('1', '$current_url_id') ";
}
$sql .= " ORDER BY date_start DESC";
$sql .= ' ORDER BY date_start DESC';
$result = Database::query($sql);
$announcements = [];
@ -871,14 +871,10 @@ class SystemAnnouncementManager
}
if (count($announcements) === 0) {
return null;
return [];
}
$template = new Template(null, false, false);
$template->assign('announcements', $announcements);
$layout = $template->get_template('announcement/slider.tpl');
return $template->fetch($layout);
return $announcements;
}
/**
@ -887,18 +883,19 @@ class SystemAnnouncementManager
* @param int $announcementId The announcement ID
* @param int $visibility The announcement visibility
*
* @return string The HTML code
* @return array
*/
public static function displayAnnouncement($announcementId, $visibility)
public static function getAnnouncement($announcementId, $visibility): array
{
$selectedUserLanguage = Database::escape_string(api_get_interface_language());
$announcementTable = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
$now = api_get_utc_datetime();
$announcementId = (int) $announcementId;
$whereConditions = [
"(lang = ? OR lang IS NULL OR lang = '') " => $selectedUserLanguage,
"AND (? >= date_start AND ? <= date_end) " => [$now, $now],
"AND id = ? " => intval($announcementId),
"AND id = ? " => $announcementId,
];
$condition = self::getVisibilityCondition($visibility);
@ -918,13 +915,10 @@ class SystemAnnouncementManager
'first'
);
$template = new Template(null, false, false);
$template->assign('announcement', $announcement);
$layout = $template->get_template('announcement/view.tpl');
return $template->fetch($layout);
return $announcement;
}
/**
* @return string
*/

@ -91,51 +91,6 @@ class IndexManager
}
}
/**
* @param bool $show_slide
*
* @return null|string
*/
public function return_announcements($show_slide = true)
{
$hideAnnouncements = api_get_setting('hide_global_announcements_when_not_connected');
$currentUserId = api_get_user_id();
if ($hideAnnouncements == 'true' && empty($currentUserId)) {
return null;
}
$announcement = isset($_GET['announcement']) ? $_GET['announcement'] : null;
$announcement = intval($announcement);
if (!api_is_anonymous() && $this->user_id) {
$visibility = SystemAnnouncementManager::getCurrentUserVisibility();
if ($show_slide) {
$announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
$visibility,
$announcement
);
} else {
$announcements = SystemAnnouncementManager::displayAllAnnouncements(
$visibility,
$announcement
);
}
} else {
if ($show_slide) {
$announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
SystemAnnouncementManager::VISIBLE_GUEST,
$announcement
);
} else {
$announcements = SystemAnnouncementManager::displayAllAnnouncements(
SystemAnnouncementManager::VISIBLE_GUEST,
$announcement
);
}
}
return $announcements;
}
/**
* Alias for the online_logout() function.
*

@ -1,16 +0,0 @@
<div class="page-header">
<h4>{{ "SystemAnnouncements" | get_lang }}</h4>
</div>
{% if not announcement is empty %}
<article id="announcement-{{ announcement.id }}}">
<div class="page-header">
<h3>{{ announcement.title }}</h3>
{{ announcement.content }}
</div>
</article>
{% else %}
<div class="alert alert-danger" role="alert">
{{ "NoResults" | get_lang }}
</div>
{% endif %}

@ -1,31 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
require_once 'main/inc/global.inc.php';
$tool_name = get_lang('SystemAnnouncements');
$visibility = SystemAnnouncementManager::getCurrentUserVisibility();
$id = isset($_GET['id']) ? $_GET['id'] : 0;
if (empty($id)) {
$content = SystemAnnouncementManager::displayAnnouncementsSlider($visibility);
} else {
$content = SystemAnnouncementManager::displayAnnouncement($id, $visibility);
}
$tpl = new Template($tool_name);
if (api_is_platform_admin()) {
$actionEdit = Display::url(
Display::return_icon('edit.png', get_lang('EditSystemAnnouncement'), [], ICON_SIZE_MEDIUM),
api_get_path(WEB_PATH).'main/admin/system_announcements.php'
);
$tpl->assign(
'actions',
Display::toolbarAction('toolbar', [$actionEdit])
);
}
$tpl->assign('content', $content);
$tpl->display_one_col_template();

@ -10,6 +10,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
/**
* Class IndexController
@ -34,13 +35,11 @@ class IndexController extends BaseController
if ($user) {
$userId = $this->getUser()->getId();
}
$announcementsBlock = $pageController->getAnnouncements($userId);
return $this->render(
'@ChamiloCore/Index/index.html.twig',
[
'content' => '',
'announcements_block' => $announcementsBlock,
'content' => ''
//'home_page_block' => $pageController->returnHomePage()
]
);
@ -50,6 +49,7 @@ class IndexController extends BaseController
* Toggle the student view action.
*
* @Route("/toggle_student_view", methods={"GET"})
*
* @Security("has_role('ROLE_TEACHER')")
*
* @param Request $request

@ -0,0 +1,81 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Controller;
use Chamilo\CoreBundle\Framework\PageController;
use Chamilo\PageBundle\Entity\Block;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
/**
* Class IndexController
* author Julio Montoya <gugli100@gmail.com>.
* @Route("/news")
*
* @package Chamilo\CoreBundle\Controller
*/
class NewsController extends BaseController
{
/**
* The Chamilo index home page.
*
* @Route("/", name="news_index", methods={"GET", "POST"}, options={"expose"=true})
*
* @return Response
*/
public function indexAction(Request $request): Response
{
$toolBar = '';
if ($this->isGranted('ROLE_ADMIN')) {
$actionEdit = \Display::url(
\Display::return_icon('edit.png', get_lang('EditSystemAnnouncement'), [], ICON_SIZE_MEDIUM),
api_get_path(WEB_PATH).'main/admin/system_announcements.php'
);
$toolBar = \Display::toolbarAction('toolbar', [$actionEdit]);
}
return $this->render(
'@ChamiloCore/News/index.html.twig',
[
'toolbar' => $toolBar
]
);
}
/**
* The Chamilo index home page.
*
* @Route("/{id}", name="news", methods={"GET", "POST"}, options={"expose"=true})
*
* @return Response
*/
public function newsAction($id = null)
{
$visibility = \SystemAnnouncementManager::getCurrentUserVisibility();
$toolBar = '';
if (empty($id)) {
$content = \SystemAnnouncementManager::getAnnouncements($visibility);
return $this->render(
'@ChamiloCore/News/slider.html.twig',
[
'announcements' => $content
]
);
} else {
$content = \SystemAnnouncementManager::getAnnouncement($id, $visibility);
return $this->render(
'@ChamiloCore/News/view.html.twig',
[
'announcement' => $content
]
);
}
}
}

@ -157,55 +157,6 @@ class PageController
return $html;
}
/**
* Returns a list of announcements.
*
* @param int User ID
* @param bool True: show the announcements as a slider. False: show them as a vertical list
*
* @return string HTML list of announcements
*/
public function getAnnouncements($user_id = null, $show_slide = true)
{
// Display System announcements
$hideAnnouncements = api_get_setting('hide_global_announcements_when_not_connected');
if ($hideAnnouncements == 'true' && empty($user_id)) {
return null;
}
$announcement = isset($_GET['announcement']) ? $_GET['announcement'] : null;
$announcement = intval($announcement);
if (!api_is_anonymous() && $user_id) {
$visibility = api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
if ($show_slide) {
$announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
$visibility,
$announcement
);
} else {
$announcements = SystemAnnouncementManager::displayAllAnnouncements(
$visibility,
$announcement
);
}
} else {
if ($show_slide) {
$announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
SystemAnnouncementManager::VISIBLE_GUEST,
$announcement
);
} else {
$announcements = SystemAnnouncementManager::displayAllAnnouncements(
SystemAnnouncementManager::VISIBLE_GUEST,
$announcement
);
}
}
return $announcements;
}
/**
* Return the homepage, including announcements.
*

@ -4,12 +4,14 @@
{% autoescape false %}
{{ render(controller('ChamiloPageBundle:Page:renderPage', {'slug': 'welcome'} )) }}
{{ announcements_block }}
{% if chamilo_settings_get('announcement.hide_global_announcements_when_not_connected') == 'false' %}
{{ render(controller('ChamiloCoreBundle:News:news')) }}
{% endif %}
{{ content }}
{% if chamilo_settings_get('display.show_hot_courses') == 'true' %}
{# See CourseBlockService.php #}
{{ sonata_block_render({'type': 'chamilo_core.block.course'}) }}
{% endif %}
{% endautoescape %}
{% endblock %}

@ -7,6 +7,5 @@
{% if content is null %}
{% include '@ChamiloCore/default/layout/welcome_to_course.html.twig' %}
{% endif %}
{% endautoescape %}
{% endblock %}

@ -0,0 +1,8 @@
{% extends "@ChamiloTheme/Layout/layout_one_col.html.twig" %}
{% block content %}
{% autoescape false %}
{{ toolbar }}
{{ render(controller('ChamiloCoreBundle:News:news')) }}
{% endautoescape %}
{% endblock %}

@ -1,3 +1,4 @@
{% autoescape false %}
<div class="page-header">
<h4>{{ "SystemAnnouncements" | get_lang }}</h4>
</div>
@ -42,3 +43,4 @@
<span class="sr-only">Next</span>
</a>
</div>
{% endautoescape %}

@ -0,0 +1,24 @@
{% extends "@ChamiloTheme/Layout/layout_one_col.html.twig" %}
{% block content %}
{% autoescape false %}
<div class="page-header">
<h4>{{ "SystemAnnouncements" | get_lang }}</h4>
</div>
{% if not announcement is empty %}
<article id="announcement-{{ announcement.id }}}">
<div class="page-header">
<h3>{{ announcement.title }}</h3>
{{ announcement.content }}
</div>
</article>
{% else %}
<div class="alert alert-danger" role="alert">
{{ "NoResults" | get_lang }}
</div>
{% endif %}
{% endautoescape %}
{% endblock %}
Loading…
Cancel
Save