Fix grid using ajax, improve speed

pull/3215/head
Julio Montoya 5 years ago
parent a9538a4568
commit 734679fc5b
  1. 4
      src/CoreBundle/Controller/CourseHomeController.php
  2. 27
      src/CoreBundle/Controller/ResourceController.php
  3. 2
      src/CoreBundle/Entity/Resource/ResourceFile.php
  4. 2
      src/CoreBundle/Entity/Resource/ResourceNode.php
  5. 38
      src/CoreBundle/Repository/IllustrationRepository.php
  6. 19
      src/CoreBundle/Repository/ResourceRepository.php
  7. 54
      src/CourseBundle/EventListener/CourseListener.php
  8. 83
      src/ThemeBundle/EventListener/TwigListener.php
  9. 0
      src/ThemeBundle/Resources/views/Breadcrumb/breadcrumb.html.twig
  10. 83
      src/ThemeBundle/Resources/views/Resource/grid_theme.html.twig
  11. 70
      src/ThemeBundle/Resources/views/Resource/index.html.twig

@ -6,6 +6,8 @@ namespace Chamilo\CoreBundle\Controller;
use Career;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Tool;
use Chamilo\CoreBundle\Tool\AbstractTool;
use Chamilo\CoreBundle\ToolChain;
use Chamilo\CourseBundle\Controller\ToolBaseController;
use Chamilo\CourseBundle\Entity\CTool;
@ -219,7 +221,9 @@ class CourseHomeController extends ToolBaseController
throw new NotFoundHttpException($this->trans('Tool not found'));
}
/** @var AbstractTool $tool */
$tool = $toolChain->getToolFromName($tool->getTool()->getName());
$url = $tool->getLink().'?'.$this->getCourseUrlQuery();
return $this->redirect($url);

@ -75,7 +75,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
$repository = $this->getRepositoryFromRequest($request);
$settings = $repository->getResourceSettings();
$grid = $this->getGrid($request, $repository, $grid, $parentResourceNode->getId());
$grid = $this->getGrid($request, $repository, $grid, $parentResourceNode->getId(), 'chamilo_core_resource_index');
$breadcrumb = $this->getBreadCrumb();
$breadcrumb->addChild(
@ -100,7 +100,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
);
}
public function getGrid(Request $request, ResourceRepository $repository, Grid $grid, int $resourceNodeId): Grid
public function getGrid(Request $request, ResourceRepository $repository, Grid $grid, int $resourceNodeId, string $routeName): Grid
{
$class = $repository->getRepository()->getClassName();
@ -126,18 +126,13 @@ class ResourceController extends AbstractResourceController implements CourseCon
$source->initQueryBuilder($qb);
$grid->setSource($source);
/*$result = $qb->getQuery()->getResult();
foreach ($result as $data) {
dump(get_class($data));
}*/
$resourceParams = $this->getResourceParams($request);
if (0 === $resourceParams['id']) {
$resourceParams['id'] = $resourceNodeId;
}
$grid->setRouteUrl($this->generateUrl('chamilo_core_resource_list', $resourceParams));
$grid->setRouteUrl($this->generateUrl($routeName, $resourceParams));
//$grid->hideFilters();
//$grid->setLimits(20);
@ -168,6 +163,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
unset($myParams[0]);
$icon = $resourceNode->getIcon().'  ';
if ($resourceNode->hasResourceFile()) {
// Process node that contains a file, process previews.
if ($resourceNode->isResourceFileAnImage()) {
@ -270,7 +266,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
$myRowAction->addManipulateRender($setNodeParameters);
$grid->addRowAction($myRowAction);
// Download action
// Download action.
$myRowAction = new RowAction(
$this->trans('Download'),
'chamilo_core_resource_download',
@ -383,7 +379,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
}
// More action.
/*$myRowAction = new RowAction(
$myRowAction = new RowAction(
$this->trans('More'),
'chamilo_core_resource_preview',
false,
@ -392,7 +388,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
);
$myRowAction->addManipulateRender($setNodeParameters);
$grid->addRowAction($myRowAction);*/
$grid->addRowAction($myRowAction);
// Delete action.
$myRowAction = new RowAction(
@ -434,7 +430,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
$repository = $this->getRepositoryFromRequest($request);
$settings = $repository->getResourceSettings();
$grid = $this->getGrid($request, $repository, $grid, $resourceNodeId);
$grid = $this->getGrid($request, $repository, $grid, $resourceNodeId, 'chamilo_core_resource_list');
$this->setBreadCrumb($request);
$parentResourceNode = $this->getParentResourceNode($request);
@ -655,7 +651,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
$tool = $request->get('tool');
$type = $request->get('type');
$illustration = $illustrationRepository->getIllustrationUrlFromNode($resource->getResourceNode());
$illustration = $illustrationRepository->getIllustrationUrlFromNode($resourceNode);
$form = $this->createForm(ResourceCommentType::class, null);
@ -666,7 +662,6 @@ class ResourceController extends AbstractResourceController implements CourseCon
'type' => $type,
'comment_form' => $form->createView(),
];
return $this->render(
$repository->getTemplates()->getFromAction(__FUNCTION__, $request->isXmlHttpRequest()),
$params
@ -753,7 +748,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
}
/**
* @Route("/{tool}/{type}/{id}", name="chamilo_core_resource_delete")
* @Route("/{tool}/{type}/{id}/delete", name="chamilo_core_resource_delete")
*/
public function deleteAction(Request $request): Response
{
@ -789,7 +784,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
}
/**
* @Route("/{tool}/{type}/{id}", methods={"DELETE"}, name="chamilo_core_resource_delete_mass")
* @Route("/{tool}/{type}/{id}/delete_mass", methods={"DELETE"}, name="chamilo_core_resource_delete_mass")
*/
public function deleteMassAction($primaryKeys, $allPrimaryKeys, Request $request): Response
{

@ -122,7 +122,7 @@ class ResourceFile
/**
* @var ResourceNode
*
* @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", mappedBy="resourceFile")
* @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", mappedBy="resourceFile", fetch="EAGER")
*/
protected $resourceNode;

@ -82,7 +82,7 @@ class ResourceNode
* @var ResourceFile
* @Groups({"list"})
*
* @ORM\OneToOne(targetEntity="ResourceFile", inversedBy="resourceNode", orphanRemoval=true)
* @ORM\OneToOne(targetEntity="ResourceFile", inversedBy="resourceNode", orphanRemoval=true, fetch="EAGER")
* @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $resourceFile;

@ -34,28 +34,13 @@ final class IllustrationRepository extends ResourceRepository implements GridInt
->select('resource')
->from($className, 'resource')
->innerJoin(
ResourceNode::class,
'node',
Join::WITH,
'resource.resourceNode = node.id'
'resource.resourceNode',
'node'
)
//->innerJoin('node.resourceLinks', 'links')
//->where('node.resourceType = :type')
//->setParameter('type',$type)
;
/*$qb
->andWhere('links.visibility = :visibility')
->setParameter('visibility', ResourceLink::VISIBILITY_PUBLISHED)
;*/
if (null !== $parentNode) {
// $qb->andWhere('node.parent = :parentNode');
// $qb->setParameter('parentNode', $parentNode);
}
$qb->andWhere('node.creator = :creator');
$qb->setParameter('creator', $user);
//var_dump($qb->getQuery()->getSQL(), $parentNode->getId());exit;
return $qb;
}
@ -130,12 +115,19 @@ final class IllustrationRepository extends ResourceRepository implements GridInt
public function getIllustrationNodeFromParent(ResourceNode $resourceNode): ?ResourceNode
{
$nodeRepo = $this->getResourceNodeRepository();
$resourceType = $this->getResourceType();
$name = $this->getResourceTypeName();
$qb = $nodeRepo->getEntityManager()->createQueryBuilder()
->select('node')
->from(ResourceNode::class, 'node')
->innerJoin('node.resourceType', 'type')
->innerJoin('node.resourceFile', 'file')
->where('node.parent = :parent')
->andWhere('type.name = :name')
->setParameters(['parent' => $resourceNode, 'name' => $name])
;
/** @var ResourceNode $node */
return $nodeRepo->findOneBy(
['parent' => $resourceNode, 'resourceType' => $resourceType]
);
return $qb->getQuery()->getFirstResult();
}
public function deleteIllustration(AbstractResource $resource)
@ -151,7 +143,7 @@ final class IllustrationRepository extends ResourceRepository implements GridInt
/**
* @param string $filter See: services.yaml parameter "glide_media_filters" to see the list of filters.
*/
public function getIllustrationUrl(AbstractResource $resource, $filter = ''): string
public function getIllustrationUrl(AbstractResource $resource, string $filter = ''): string
{
return $this->getIllustrationUrlFromNode($resource->getResourceNode(), $filter);
}

@ -27,7 +27,6 @@ use Cocur\Slugify\SlugifyInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityRepository as BaseEntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use League\Flysystem\FilesystemInterface;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
@ -605,7 +604,6 @@ class ResourceRepository extends BaseEntityRepository
$qb->andWhere('node.creator = :creator');
$qb->setParameter('creator', $user);
//var_dump($qb->getQuery()->getSQL(), $parentNode->getId());exit;
return $qb;
}
@ -670,6 +668,23 @@ class ResourceRepository extends BaseEntityRepository
public function getResourceFromResourceNode(int $resourceNodeId): ?AbstractResource
{
/*$repo = $this->getRepository();
$className = $repo->getClassName();
$qb = $repo->getEntityManager()->createQueryBuilder()
->select('resource')
->from($className, 'resource')
->innerJoin(
'resource.resourceNode',
'node'
)
->innerJoin('node.resourceLinks', 'links')
->leftJoin('node.resourceFile', 'file')
->where('node = :id')
->setParameters(['id' => $resourceNodeId])
;
return $qb->getQuery()->getFirstResult();*/
return $this->getRepository()->findOneBy(['resourceNode' => $resourceNodeId]);
}

@ -57,19 +57,28 @@ class CourseListener
}
$sessionHandler = $request->getSession();
$container = $this->container;
$translator = $container->get('translator');
$course = null;
// Check if URL has cid value. Using Symfony request.
$courseId = $request->get('cid');
$courseId = (int) $request->get('cid');
$checker = $container->get('security.authorization_checker');
if (!empty($courseId)) {
/** @var EntityManager $em */
$em = $container->get('doctrine')->getManager();
$checker = $container->get('security.authorization_checker');
$course = $em->getRepository('ChamiloCoreBundle:Course')->find($courseId);
if ($sessionHandler->has('course')) {
/** @var Course $courseFromSession */
$courseFromSession = $sessionHandler->get('course');
if ($courseId === $courseFromSession->getId()) {
$course = $courseFromSession;
}
}
if (null === $course) {
/** @var EntityManager $em */
$em = $container->get('doctrine')->getManager();
$course = $em->getRepository('ChamiloCoreBundle:Course')->find($courseId);
}
if (null === $course) {
throw new NotFoundHttpException($translator->trans('Course does not exist'));
@ -165,27 +174,6 @@ class CourseListener
$courseParams = $this->generateCourseUrl($course, $sessionId, $groupId, $origin);
$sessionHandler->set('course_url_params', $courseParams);
$container->get('twig')->addGlobal('course_url_params', $courseParams);
/*if (!$alreadyVisited ||
isset($alreadyVisited) && $alreadyVisited != $courseCode
) {
// Course access events
$dispatcher = $this->container->get('event_dispatcher');
if (empty($sessionId)) {
$dispatcher->dispatch('chamilo_course.course.access', new CourseAccess($user, $course));
} else {
$dispatcher->dispatch(
'chamilo_course.course.access',
new SessionAccess($user, $course, $session)
);
}
$coursesAlreadyVisited[$course->getCode()] = 1;
$sessionHandler->set('course_already_visited', $courseCode);
}*/
// Container::setRequest($request);
// Container::setContainer($container);
// Container::setLegacyServices($container);
}
}
@ -237,7 +225,6 @@ class CourseListener
//|| $controllerList[0] instanceof LegacyController
)
) {
//var_dump($courseId);
if (!empty($courseId)) {
$controller = $controllerList[0];
$session = $sessionHandler->get('session');
@ -266,17 +253,6 @@ class CourseListener
//$controllerActionParts = explode(':', $controllerAction);
//$controllerNameParts = explode('.', $controllerActionParts[0]);
//$controllerName = $controllerActionParts[0];
} else {
/*$ignore = [
'fos_js_routing.controller:indexAction',
'web_profiler.controller.profiler:toolbarAction',
];
$controllerAction = $request->get('_controller');
if (!in_array($controllerAction, $ignore)) {
//error_log('remove');
//$this->removeCourseFromSession($request);
}*/
}
}

@ -134,59 +134,62 @@ class TwigListener implements EventSubscriberInterface
}
}
// Plugins - Region list
$pluginConfiguration = api_get_settings('Plugins', 'list', 1);
$pluginRegionList = [];
foreach ($pluginConfiguration as $plugin) {
if ('region' === $plugin['type']) {
$pluginRegionList[$plugin['variable']][] = $plugin['subkey'];
}
}
$appPlugin = new \AppPlugin();
$installedPlugins = $appPlugin->getInstalledPluginListName();
$pluginRegions = $appPlugin->getPluginRegions();
$courseInfo = api_get_course_info();
$regionListContent = [];
foreach ($installedPlugins as $pluginName) {
foreach ($pluginRegions as $region) {
if (!isset($pluginRegionList[$region])) {
continue;
if (!empty($installedPlugins)) {
// Plugins - Region list
$pluginConfiguration = api_get_settings('Plugins', 'list', 1);
$pluginRegionList = [];
foreach ($pluginConfiguration as $plugin) {
if ('region' === $plugin['type']) {
$pluginRegionList[$plugin['variable']][] = $plugin['subkey'];
}
}
if ('course_tool_plugin' === $region) {
$courseRegions = $appPlugin->getPluginRegions();
$pluginInfo = $appPlugin->getPluginInfo($pluginName, true);
$pluginRegions = $appPlugin->getPluginRegions();
$courseInfo = api_get_course_info();
$regionListContent = [];
if (empty($courseInfo)) {
foreach ($installedPlugins as $pluginName) {
foreach ($pluginRegions as $region) {
if (!isset($pluginRegionList[$region])) {
continue;
}
foreach ($courseRegions as $subRegion) {
if (isset($pluginInfo['obj']) && $pluginInfo['obj'] instanceof \Plugin) {
/** @var \Plugin $plugin */
$plugin = $pluginInfo['obj'];
$regionListContent[$subRegion][] = $plugin->renderRegion($subRegion);
if ('course_tool_plugin' === $region) {
$courseRegions = $appPlugin->getPluginRegions();
$pluginInfo = $appPlugin->getPluginInfo($pluginName, true);
if (empty($courseInfo)) {
continue;
}
foreach ($courseRegions as $subRegion) {
if (isset($pluginInfo['obj']) && $pluginInfo['obj'] instanceof \Plugin) {
/** @var \Plugin $plugin */
$plugin = $pluginInfo['obj'];
$regionListContent[$subRegion][] = $plugin->renderRegion($subRegion);
}
}
} else {
if (in_array($pluginName, $pluginRegionList[$region])) {
$regionListContent[$region][] = $appPlugin->loadRegion(
$pluginName,
$region,
$twig,
true //$this->force_plugin_load
);
}
}
} else {
if (in_array($pluginName, $pluginRegionList[$region])) {
$regionListContent[$region][] = $appPlugin->loadRegion(
$pluginName,
$region,
$twig,
true //$this->force_plugin_load
);
}
}
}
}
foreach ($regionListContent as $region => $contentList) {
$contentToString = '';
foreach ($contentList as $content) {
$contentToString .= $content;
foreach ($regionListContent as $region => $contentList) {
$contentToString = '';
foreach ($contentList as $content) {
$contentToString .= $content;
}
$twig->addGlobal("plugin_$region", $contentToString);
}
$twig->addGlobal("plugin_$region", $contentToString);
}
/*$userInfo = api_get_user_info();

@ -93,6 +93,84 @@
</div>
{% endblock grid_column_actions_cell %}
{% block grid_scripts %}
{{ parent }}
<script>
function clickRow(firstRow) {
var anchor = firstRow.find('a');
var url = anchor.attr('href');
url = url.replace('/view_resource?', '/info?');
url = url.replace('/view?', '/info?');
url = url.replace('/list?', '/info?');
if (url) {
$.ajax({
type: "get",
url: url,
contentType: "html",
beforeSend: function () {
},
success: function (response) {
$('#resources').attr('class', 'col-lg-8');
$('#info_bar').attr('class', 'col-lg-4');
$('#info_bar').html(response);
},
});
}
}
$(function () {
$('tbody').on('click', 'tr', function (event) {
if ($(this).is('tr:first')) {
return;
}
if (event.ctrlKey === false) {
$(this).siblings().find('input:checkbox').prop('checked', false);
$(this).siblings('tr:visible').removeClass('table-active');
}
var checkbox = $(this).find('input');
checkbox.prop('checked', !checkbox.prop('checked'));
$(this).toggleClass('table-active');
var rows = $('tbody tr.table-active').length;
if (rows == 0) {
$('.mass-actions').hide();
} else {
if ($('#info_bar').is(':visible')) {
clickRow($(this));
}
$('.mass-actions').show();
}
});
$('.resource_info_row').on('click', function (event) {
console.log('resource_info_row');
var row = $(this).parent().parent().parent();
$('#info_bar').show();
clickRow(row);
event.preventDefault();
});
$('.resource_info').on('click', function (event) {
var firstRow = $('tbody tr.table-active:first ');
var rowCount = firstRow.length;
if (rowCount) {
$('#info_bar').show();
clickRow(firstRow);
}
event.preventDefault();
});
});
</script>
{% endblock %}
{% block grid %}
<div class="box box-primary">
<div class="box-body table-responsive no-padding">
@ -106,7 +184,7 @@
</div>
<div class="grid_body">
<table class="table table-hover">
<table class="table table-hover table-sm">
{% if grid.isTitleSectionVisible %}
{{ grid_titles(grid) }}
{% endif %}
@ -189,6 +267,9 @@
{% endblock grid_column_filter_type_input %}
{# ---------------------------------------------------- grid_pager_selectpage -------------------------------------------------- #}
{#{% block grid_pager_selectpage %}#}
{# {{ 'Page'|trans }}#}

@ -2,77 +2,7 @@
{% block content %}
<script>
function clickRow(firstRow) {
var anchor = firstRow.find('a');
var url = anchor.attr('href');
url = url.replace('/view_resource?', '/info?');
url = url.replace('/view?', '/info?');
url = url.replace('/list?', '/info?');
if (url) {
$.ajax({
type: "get",
url: url,
contentType: "html",
beforeSend: function() {
},
success: function (response) {
$('#resources').attr('class', 'col-lg-8');
$('#info_bar').attr('class', 'col-lg-4');
$('#info_bar').html(response);
},
});
}
}
$(function () {
$('tbody').on('click', 'tr', function (event) {
if ($(this).is('tr:first')) {
return;
}
if (event.ctrlKey === false) {
$(this).siblings().find('input:checkbox').prop('checked', false);
$(this).siblings('tr:visible').removeClass('table-active');
}
var checkbox = $(this).find('input');
checkbox.prop('checked', !checkbox.prop('checked'));
$(this).toggleClass('table-active');
var rows = $('tbody tr.table-active').length;
if (rows == 0) {
$('.mass-actions').hide();
} else {
if ($('#info_bar').is(':visible')) {
clickRow($(this));
}
$('.mass-actions').show();
}
} );
// $('#button').click( function () {
// alert( +' row(s) selected' );
// } );
$('.resource_info_row').on('click', function (event) {
var row = $(this).parent().parent().parent();
$('#info_bar').show();
clickRow(row);
event.preventDefault();
});
$('.resource_info').on('click', function (event) {
var firstRow = $('tbody tr.table-active:first ');
var rowCount = firstRow.length;
if (rowCount) {
$('#info_bar').show();
clickRow(firstRow);
}
event.preventDefault();
});
// Preview action.
$('[data-fancybox="gallery"]').fancybox({
toolbar: "true",

Loading…
Cancel
Save