Organize resource URLs, remove unused controller.

pull/3064/head
Julio Montoya 5 years ago
parent 8578c1f803
commit 3a76aa3574
  1. 2
      config/packages/sonata_page.yaml
  2. 51
      src/CoreBundle/Controller/ResourceController.php
  3. 142
      src/CoreBundle/Controller/ResourceDownloadController.php
  4. 1
      src/CoreBundle/Controller/ResourceUploaderController.php
  5. 2
      src/CoreBundle/Entity/Resource/AbstractResource.php
  6. 8
      src/CoreBundle/Entity/Resource/ResourceFile.php
  7. 11
      src/CoreBundle/Repository/IllustrationRepository.php
  8. 73
      src/CoreBundle/Repository/ResourceRepository.php
  9. 5
      src/CoreBundle/Resources/config/services.yml
  10. 26
      src/CoreBundle/Resources/views/Resource/info.html.twig
  11. 22
      src/CourseBundle/Controller/Home/HomeController.php
  12. 2
      src/CourseBundle/Repository/CDocumentRepository.php
  13. 4
      src/CourseBundle/Repository/CExerciseCategoryRepository.php
  14. 15
      src/CourseBundle/Resources/config/routing.yml

@ -44,7 +44,7 @@ sonata_page:
- ^/news/(.*)
- ^/courses/(.*)
- ^/editor/(.*)
- ^/resource/(.*)
- ^/resources/(.*)
- ^/front/editor
- ^/contact(.*) # sonata admin
- ^/cms/pages/latest

@ -30,6 +30,10 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Vich\UploaderBundle\Util\Transliterator;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class ResourceController.
@ -401,7 +405,6 @@ class ResourceController extends BaseController implements CourseControllerInter
* Shows a resource.
*
* @param Request $request
* @param CDocumentRepository $documentRepo
* @param Glide $glide
*
* @return Response
@ -429,29 +432,21 @@ class ResourceController extends BaseController implements CourseControllerInter
*
* @return Response
*/
public function showAction(Request $request, CDocumentRepository $documentRepo, Glide $glide): Response
public function showAction(Request $request): Response
{
$file = $request->get('file');
$type = $request->get('type');
// see list of filters in config/services.yaml
$filter = $request->get('filter');
$type = !empty($type) ? $type : 'show';
$criteria = [
'path' => "/$file",
'course' => $this->getCourse(),
];
$em = $this->getDoctrine();
$document = $documentRepo->findOneBy($criteria);
$id = $request->get('id');
$resourceNode = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($id);
if (null === $document) {
if (null === $resourceNode) {
throw new NotFoundHttpException();
}
/** @var ResourceNode $resourceNode */
$resourceNode = $document->getResourceNode();
return $this->showFile($request, $resourceNode, $glide, $type, $filter);
$params = [
'resource_node' => $resourceNode,
];
return $this->render('@ChamiloCore/Resource/info.html.twig', $params);
}
/**
@ -619,4 +614,24 @@ class ResourceController extends BaseController implements CourseControllerInter
return $response;
}
/**
* Upload form.
*
* @Route("/upload/{type}/{id}", name="resource_upload", methods={"GET", "POST"}, options={"expose"=true})
*
* @return Response
*/
public function showUploadFormAction($type, $id): Response
{
//$helper = $this->container->get('oneup_uploader.templating.uploader_helper');
//$endpoint = $helper->endpoint('courses');
return $this->render(
'@ChamiloCore/Resource/upload.html.twig',
[
'identifier' => $id,
'type' => $type,
]
);
}
}

@ -1,142 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Controller;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class ResourceController.
*
* @author Julio Montoya <gugli100@gmail.com>.
*
* @Route("/resource")
*/
class ResourceDownloadController extends BaseController
{
/**
* Upload form.
*
* @Route("/upload/{type}/{id}", name="resource_upload", methods={"GET", "POST"}, options={"expose"=true})
*
* @return Response
*/
public function showUploadFormAction($type, $id): Response
{
//$helper = $this->container->get('oneup_uploader.templating.uploader_helper');
//$endpoint = $helper->endpoint('courses');
return $this->render(
'@ChamiloCore/Resource/upload.html.twig',
[
'identifier' => $id,
'type' => $type,
]
);
}
/**
* Downloads the file courses/MATHS/document/file.jpg to the user.
*
* @Route("/download/{course}/", name="resource_download", methods={"GET"}, options={"expose"=true})
*
* @todo check permissions
*
* @param string $course
*
* @return \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function downloadFileAction(Request $request, $course)
{
try {
/** @var Filesystem $fs */
$fs = $this->container->get('oneup_flysystem.resources_filesystem');
$file = $request->get('file');
$path = $course.'/document/'.$file;
// Has folder
if (!$fs->has($course)) {
return $this->abort();
}
// Has file
if (!$fs->has($path)) {
return $this->abort();
}
/** @var Local $adapter */
$adapter = $fs->getAdapter();
$filePath = $adapter->getPathPrefix().$path;
$response = new BinaryFileResponse($filePath);
// To generate a file download, you need the mimetype of the file
$mimeTypeGuesser = new FileinfoMimeTypeGuesser();
// Set the mimetype with the guesser or manually
if ($mimeTypeGuesser->isSupported()) {
// Guess the mimetype of the file according to the extension of the file
$response->headers->set('Content-Type', $mimeTypeGuesser->guess($filePath));
} else {
// Set the mimetype of the file manually, in this case for a text file is text/plain
$response->headers->set('Content-Type', 'text/plain');
}
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
basename($filePath)
);
return $response;
} catch (\InvalidArgumentException $e) {
return $this->abort();
}
}
/**
* Gets a document in browser courses/MATHS/document/file.jpg to the user.
*
* @Route("/get/{course}/", name="resource_get", methods={"GET"}, options={"expose"=true})
*
* @todo check permissions
*
* @param string $course
*
* @return \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function getFileAction(Request $request, $course)
{
try {
/** @var Filesystem $fs */
$fs = $this->container->get('oneup_flysystem.resources_filesystem');
$file = $request->get('file');
$path = $course.'/document/'.$file;
// Has folder
if (!$fs->has($course)) {
return $this->abort();
}
// Has file
if (!$fs->has($path)) {
return $this->abort();
}
/** @var Local $adapter */
$adapter = $fs->getAdapter();
$filePath = $adapter->getPathPrefix().$path;
return $this->file($filePath, null, ResponseHeaderBag::DISPOSITION_INLINE);
} catch (\InvalidArgumentException $e) {
return $this->abort();
}
}
}

@ -21,6 +21,7 @@ class ResourceUploaderController extends BlueimpController
*/
public function upload()
{
return;
error_log('upload!!!');
$request = $this->getRequest();
$response = new EmptyResponse();

@ -26,6 +26,8 @@ abstract class AbstractResource implements ResourceInterface
abstract public function getResourceName(): string;
/**
* Updates the resource node name when updating the resource.
*
* @ORM\PostUpdate()
*
* @param LifecycleEventArgs $args

@ -205,6 +205,14 @@ class ResourceFile
$this->enabled = true;
}
/**
* @return string
*/
public function __toString(): string
{
return (string) $this->getName();
}
/**
* @return mixed
*/

@ -22,7 +22,7 @@ class IllustrationRepository extends ResourceRepository
*
* @return ResourceFile
*/
public function addIllustration(AbstractResource $resource, User $user, $uploadFile)
public function addIllustration(AbstractResource $resource, User $user, $uploadFile): ?ResourceFile
{
$illustrationNode = $this->getIllustrationNodeFromResource($resource);
$em = $this->getEntityManager();
@ -33,6 +33,7 @@ class IllustrationRepository extends ResourceRepository
$illustrationNode = $this->addResourceNode($illustration, $user, $resource);
//$this->addResourceToEveryone($illustrationNode);
}
return $this->addFile($illustrationNode, $uploadFile);
}
@ -41,7 +42,7 @@ class IllustrationRepository extends ResourceRepository
*
* @return ResourceNode
*/
public function getIllustrationNodeFromResource(AbstractResource $resource)
public function getIllustrationNodeFromResource(AbstractResource $resource): ?ResourceNode
{
$nodeRepo = $this->getResourceNodeRepository();
$resourceType = $this->getResourceType();
@ -69,7 +70,7 @@ class IllustrationRepository extends ResourceRepository
/**
* @param AbstractResource $resource
* @param string $filter
* @param string $filter See: services.yaml parameter "glide_media_filters" to see the list of filters.
*
* @return string
*/
@ -83,8 +84,8 @@ class IllustrationRepository extends ResourceRepository
$params['filter'] = $filter;
}
return $this->router->generate(
'core_tool_resource',
return $this->getRouter()->generate(
'resources_get_file',
$params
);
}

@ -67,7 +67,7 @@ class ResourceRepository extends EntityRepository
public function __construct(EntityManager $entityManager, MountManager $mountManager, RouterInterface $router, string $className)
{
$this->repository = $entityManager->getRepository($className);
// Flysystem mount name is saved in config/packages/oneup_flysystem.yaml
// Flysystem mount name is saved in config/packages/oneup_flysystem.yaml @todo add it as a service.
$this->fs = $mountManager->getFilesystem('resources_fs');
$this->router = $router;
$this->resourceNodeRepository = $entityManager->getRepository('ChamiloCoreBundle:Resource\ResourceNode');
@ -81,6 +81,14 @@ class ResourceRepository extends EntityRepository
return new $this->className();
}
/**
* @return RouterInterface
*/
public function getRouter(): RouterInterface
{
return $this->router;
}
/**
* @return EntityRepository
*/
@ -120,7 +128,7 @@ class ResourceRepository extends EntityRepository
*
* @return AbstractResource|null
*/
public function find($id, $lockMode = null, $lockVersion = null)
public function find($id, $lockMode = null, $lockVersion = null): ?AbstractResource
{
return $this->getRepository()->find($id);
}
@ -131,7 +139,7 @@ class ResourceRepository extends EntityRepository
*
* @return AbstractResource
*/
public function findOneBy(array $criteria, array $orderBy = null)
public function findOneBy(array $criteria, array $orderBy = null): ?AbstractResource
{
return $this->getRepository()->findOneBy($criteria, $orderBy);
}
@ -142,7 +150,7 @@ class ResourceRepository extends EntityRepository
*
* @return ResourceFile
*/
public function addFile(ResourceNode $resourceNode, UploadedFile $file)
public function addFile(ResourceNode $resourceNode, UploadedFile $file): ?ResourceFile
{
$resourceFile = $resourceNode->getResourceFile();
if ($resourceFile === null) {
@ -170,11 +178,8 @@ class ResourceRepository extends EntityRepository
*
* @return ResourceNode
*/
public function addResourceNode(
AbstractResource $resource,
User $creator,
AbstractResource $parent = null
): ResourceNode {
public function addResourceNode(AbstractResource $resource, User $creator, AbstractResource $parent = null): ResourceNode
{
$em = $this->getEntityManager();
$resourceType = $this->getResourceType();
@ -199,7 +204,15 @@ class ResourceRepository extends EntityRepository
return $resourceNode;
}
public function addResourceToCourse(AbstractResource $resource, $visibility, User $creator, Course $course, $session = null, $group = null)
/**
* @param AbstractResource $resource
* @param int $visibility
* @param User $creator
* @param Course $course
* @param Session $session
* @param CGroupInfo $group
*/
public function addResourceToCourse(AbstractResource $resource, int $visibility, User $creator, Course $course, Session $session = null, CGroupInfo $group = null)
{
$node = $this->addResourceNode($resource, $creator, $course);
$this->addResourceNodeToCourse($node, $visibility, $course, $session, $group);
@ -305,8 +318,6 @@ class ResourceRepository extends EntityRepository
return $resourceLink;
}
/**
* @param ResourceNode $resourceNode
* @param Course $course
@ -463,7 +474,7 @@ class ResourceRepository extends EntityRepository
);
$type = $this->getResourceType();
$query = $this->getEntityManager()->createQueryBuilder()
$qb = $this->getEntityManager()->createQueryBuilder()
->select('resource')
->from(ResourceNode::class, 'node')
->innerJoin('node.resourceLinks', 'links')
@ -486,37 +497,39 @@ class ResourceRepository extends EntityRepository
);
if ($session === null) {
$query->andWhere('links.session IS NULL');
$qb->andWhere('links.session IS NULL');
} else {
if ($loadBaseSessionContent) {
$query->andWhere('links.session = :session OR links.session IS NULL');
$query->setParameter('session', $session);
$qb->andWhere('links.session = :session OR links.session IS NULL');
$qb->setParameter('session', $session);
} else {
$query->andWhere('links.session = :session');
$query->setParameter('session', $session);
$qb->andWhere('links.session = :session');
$qb->setParameter('session', $session);
}
}
if ($group === null) {
$query->andWhere('links.group IS NULL');
$qb->andWhere('links.group IS NULL');
}
/*if ($parent !== null) {
$query->andWhere('node.parent = :parentId');
$query->setParameter('parentId', $parent->getResourceNode()->getId());
$qb->andWhere('node.parent = :parentId');
$qb->setParameter('parentId', $parent->getResourceNode()->getId());
} else {
$query->andWhere('node.parent = :parentId');
$query->setParameter('parentId', $course->getResourceNode());
$qb->andWhere('node.parent = :parentId');
$qb->setParameter('parentId', $course->getResourceNode());
}*/
/*$query->setFirstResult();
$query->setMaxResults();
$query->orderBy();*/
/*$qb->setFirstResult();
$qb->setMaxResults();
$qb->orderBy();*/
return $qb;
$query = $query->getQuery();
//var_dump($query->getSQL());
$qb = $qb->getQuery();
//var_dump($qb->getSQL());
/*$query = $this->getEntityManager()->createQueryBuilder()
/*$qb = $this->getEntityManager()->createQueryBuilder()
->select('notebook')
->from('ChamiloNotebookBundle:CNotebook', 'notebook')
->innerJoin('notebook.resourceNodes', 'node')
@ -531,7 +544,7 @@ class ResourceRepository extends EntityRepository
)
->getQuery()
;*/
return $query->getResult();
return $qb->getResult();
}
/**

@ -67,6 +67,11 @@ services:
# tags:
# - {name: twig.extension}
# twig.extension.date:
# class: Twig_Extensions_Extension_Date
# tags:
# - { name: twig.extension }
# Chamilo custom password encoder (depends on the password_encryption param)
# chamilo_user.security.encoder:
# class: Chamilo\UserBundle\Security\Encoder

@ -0,0 +1,26 @@
{% extends "@ChamiloTheme/Layout/layout_one_col.html.twig" %}
{% block content %}
{% autoescape false %}
<h3>{{ resource_node.name }} </h3>
Type: {{ resource_node.resourceType }} <br />
{% if resource_node.resourceFile %}
File:
<a href="{{ url('resources_get_file', {id: resource_node.id } ) }}">
{{ resource_node.resourceFile }}
</a>
<br />
Size: {{ resource_node.resourceFile.size }}
<br /><br />
{% endif %}
Updated at: {{ resource_node.updatedAt | date_to_time_ago }}
<br />
Created at: {{ resource_node.createdAt | date_to_time_ago }}<br />
<br />
Creator: {{ resource_node.creator }}
{% endautoescape %}
{% endblock %}

@ -3,6 +3,7 @@
namespace Chamilo\CourseBundle\Controller\Home;
use APY\DataGridBundle\Grid\Source\Entity;
use Chamilo\CourseBundle\Controller\ToolBaseController;
use Chamilo\CourseBundle\Entity\CTool;
use CourseHome;
@ -16,6 +17,7 @@ use Security;
use Event;
use Database;
use ExtraFieldValue;
use Ublaboo\DataGrid\DataGrid;
/**
* Class HomeController.
@ -106,8 +108,6 @@ class HomeController extends ToolBaseController
$logInfo = [
'tool' => 'course-main',
'tool_id' => 0,
'tool_id_detail' => 0,
'action' => $action,
];
Event::registerLog($logInfo);
@ -285,13 +285,11 @@ class HomeController extends ToolBaseController
//require 'activity.php';
// Activity start
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;
$course_id = api_get_course_int_id();
$session_id = api_get_session_id();
// Work with data post askable by admin of course
// Work with data post askable by admin of course
if (api_is_platform_admin()) {
// Show message to confirm that a tool it to be hidden from available tools
// visibility 0,1->2
@ -317,7 +315,7 @@ class HomeController extends ToolBaseController
$pluginExtra = $plugin->getTeacherLink();
}
// Start of tools for CourseAdmins (teachers/tutors)
// Start of tools for CourseAdmins (teachers/tutors)
if ($session_id === 0 && api_is_course_admin() && api_is_allowed_to_edit(null, true)) {
$content .= '<div class="alert alert-success" style="border:0px; margin-top: 0px;padding:0px;">
<div class="normal-message" id="id_normal_message" style="display:none">';
@ -340,11 +338,9 @@ class HomeController extends ToolBaseController
}
$blocks = CourseHome::getUserBlocks();
// Activity end
// Get session-career diagram
// Get session-career diagram
$diagram = '';
$allow = api_get_configuration_value('allow_career_diagram');
if ($allow === true) {
@ -395,6 +391,10 @@ class HomeController extends ToolBaseController
$content = '<div id="course_tools">'.$diagram.$content.'</div>';
$source = new Entity('ChamiloCourseBundle:CExerciseCategory');
$grid = $this->get('grid');
$grid ->setSource($source);
// Deleting the objects
Session::erase('_gid');
Session::erase('oLP');
@ -403,11 +403,15 @@ class HomeController extends ToolBaseController
\Exercise::cleanSessionVariables();
\DocumentManager::removeGeneratedAudioTempFile();
return $this->render(
'@ChamiloTheme/Course/home.html.twig',
[
'course' => $course,
'diagram' => $diagram,
'grid' => $grid,
// 'session_info' => $sessionInfo,
'icons' => $result['content'],
'blocks' => $blocks,

@ -38,7 +38,7 @@ final class CDocumentRepository extends ResourceRepository
];
return $this->getRouter()->generate(
'core_tool_document',
'resources_document_get_file',
$params
);
}

@ -22,7 +22,7 @@ final class CExerciseCategoryRepository extends ResourceRepository
{
$query = $this->getRepository()->createQueryBuilder('e');
$query->select('count(e.id)');
$query->where('e.cId = :cId');
$query->where('e.course = :cId');
$query->setParameter('cId', $courseId);
return $query->getQuery()->getSingleScalarResult();
@ -36,7 +36,7 @@ final class CExerciseCategoryRepository extends ResourceRepository
public function getCategories($courseId)
{
$query = $this->getRepository()->createQueryBuilder('e');
$query->where('e.cId = :cId');
$query->where('e.course = :cId');
$query->setParameter('cId', $courseId);
$query->orderBy('e.position');

@ -7,20 +7,27 @@ core_tool:
type: annotation
prefix: /courses/{course}/
core_tool_document:
path: /courses/{course}/document/{file}
resources_get_info:
path: /resources/{id}
defaults:
_controller: Chamilo\CoreBundle\Controller\ResourceController::showAction
requirements:
file: .+ # allow "/" in {file}
core_tool_resource:
path: /resources/{id}
resources_get_file:
path: /resources/file/{id}
defaults:
_controller: Chamilo\CoreBundle\Controller\ResourceController::getResourceFileAction
requirements:
file: .+ # allow "/" in {file}
resources_document_get_file:
path: /courses/{course}/document/{file}
defaults:
_controller: Chamilo\CoreBundle\Controller\ResourceController::showAction
requirements:
file: .+ # allow "/" in {file}
chamilo_course_tool_intro_create:
path: /courses/{course}/introduction/{tool}/create/
methods: [GET, POST]

Loading…
Cancel
Save