Add delete exercise category in grid

pull/3064/head
Julio Montoya 6 years ago
parent 76046e8ce6
commit e7dd5e003b
  1. 8
      main/exercise/category.php
  2. 86
      main/inc/lib/ExerciseCategoryManager.php
  3. 16
      src/CoreBundle/Repository/ResourceRepository.php

@ -79,14 +79,6 @@ switch ($action) {
$content .= $form->returnForm(); $content .= $form->returnForm();
} }
break; break;
case 'delete':
$res = $obj->delete($_GET['id']);
if ($res) {
Display::addFlash(Display::return_message(get_lang('Item deleted'), 'confirmation'));
}
header('Location: '.$url);
exit;
break;
default: default:
$content = $obj->display(); $content = $obj->display();
break; break;

@ -1,9 +1,11 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use APY\DataGridBundle\Grid\Action\MassAction;
use APY\DataGridBundle\Grid\Source\Entity; use APY\DataGridBundle\Grid\Source\Entity;
use Chamilo\CoreBundle\Entity\Resource\ResourceLink; use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
use Chamilo\CourseBundle\Entity\CExerciseCategory; use Chamilo\CourseBundle\Entity\CExerciseCategory;
@ -79,6 +81,30 @@ class ExerciseCategoryManager extends Model
$repo->hardDelete($category); $repo->hardDelete($category);
} }
/**
* @param $primaryKeys
* @param $allPrimaryKeys
* @param \Symfony\Component\HttpFoundation\Session\Session $session
* @param $parameters
*/
public function deleteResource(
$primaryKeys,
$allPrimaryKeys,
Symfony\Component\HttpFoundation\Session\Session $session,
$parameters
) {
$repo = Container::getExerciseCategoryRepository();
$translator = Container::getTranslator();
foreach ($primaryKeys as $id) {
$category = $repo->find($id);
$repo->hardDelete($category);
}
Display::addFlash(Display::return_message($translator->trans('Deleted')));
header('Location:'. api_get_self().'?'.api_get_cidreq());
exit;
}
/** /**
* @param array $params * @param array $params
* @param bool $showQuery * @param bool $showQuery
@ -234,13 +260,6 @@ JAVASCRIPT;
} else { } else {
$form->addButtonCreate(get_lang('Add')); $form->addButtonCreate(get_lang('Add'));
} }
/*if (!empty($defaults['created_at'])) {
$defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
}
if (!empty($defaults['updated_at'])) {
$defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
}*/
$form->setDefaults($defaults); $form->setDefaults($defaults);
// Setting the rules // Setting the rules
@ -254,7 +273,7 @@ JAVASCRIPT;
*/ */
public function display() public function display()
{ {
// action links // Action links
$content = '<div class="actions">'; $content = '<div class="actions">';
$content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'; $content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">';
$content .= Display::return_icon( $content .= Display::return_icon(
@ -273,13 +292,19 @@ JAVASCRIPT;
); );
$content .= '</a>'; $content .= '</a>';
$content .= '</div>'; $content .= '</div>';
///$grid = Container::$container->get('grid');
// 1. Set entity
$source = new Entity('ChamiloCourseBundle:CExerciseCategory'); $source = new Entity('ChamiloCourseBundle:CExerciseCategory');
// 2. Get query builder from repo.
$qb = Container::getExerciseCategoryRepository()->getResourcesByCourse(api_get_course_entity()); $qb = Container::getExerciseCategoryRepository()->getResourcesByCourse(api_get_course_entity());
// 3. Set QueryBuilder to the source.
$source-> initQueryBuilder($qb); $source-> initQueryBuilder($qb);
// 4. Get the grid builder.
$builder = Container::$container->get('apy_grid.factory'); $builder = Container::$container->get('apy_grid.factory');
// 5. Set parameters and properties.
$grid = $builder->createBuilder( $grid = $builder->createBuilder(
'grid', 'grid',
$source, $source,
@ -298,17 +323,42 @@ JAVASCRIPT;
'primary' => 'true', 'primary' => 'true',
] ]
)->add( )->add(
'name', 'name',
'text', 'text',
[ [
'title' => 'name', 'title' => 'name',
] ]
); )->add(
'description',
'text',
[
'title' => 'description',
]
);
$grid = $grid->getGrid(); $grid = $grid->getGrid();
$grid->setRouteUrl(api_get_self().'?cidReq=GEORGIA');
$grid->handleRequest(Container::getRequest()); // 7. Add actions
$content .= Container::$container->get('twig')->render('@ChamiloTheme/Resource/grid.html.twig', array('grid' => $grid)); if (Container::getAuthorizationChecker()->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) {
$deleteMassAction = new MassAction(
'Delete',
['ExerciseCategoryManager', 'deleteResource'],
true,
[]
);
$grid->addMassAction($deleteMassAction);
}
// 8. Set route and request
$grid
->setRouteUrl(api_get_self().'?'.api_get_cidreq())
->handleRequest(Container::getRequest())
;
$content .= Container::$container->get('twig')->render(
'@ChamiloTheme/Resource/grid.html.twig',
['grid' => $grid]
);
return $content; return $content;
} }

@ -18,6 +18,7 @@ use Chamilo\CourseBundle\Entity\CGroupInfo;
use Chamilo\UserBundle\Entity\User; use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use League\Flysystem\FilesystemInterface; use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager; use League\Flysystem\MountManager;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
@ -461,7 +462,7 @@ class ResourceRepository extends EntityRepository
* @param Session|null $session * @param Session|null $session
* @param CGroupInfo|null $group * @param CGroupInfo|null $group
* *
* @return array * @return QueryBuilder
*/ */
public function getResourcesByCourse(Course $course, Session $session = null, CGroupInfo $group = null) public function getResourcesByCourse(Course $course, Session $session = null, CGroupInfo $group = null)
{ {
@ -474,17 +475,18 @@ class ResourceRepository extends EntityRepository
); );
$type = $this->getResourceType(); $type = $this->getResourceType();
$qb = $this->getEntityManager()->createQueryBuilder() $qb = $repo->getEntityManager()->createQueryBuilder()
->select('resource') ->select('resource')
->from(ResourceNode::class, 'node') ->from($className, 'resource')
->innerJoin('node.resourceLinks', 'links')
->innerJoin( ->innerJoin(
$className, ResourceNode::class,
'resource', 'node',
Join::WITH, Join::WITH,
'resource.course = links.course AND resource.resourceNode = node.id' 'resource.resourceNode = node.id'
) )
->innerJoin('node.resourceLinks', 'links')
->where('node.resourceType = :type') ->where('node.resourceType = :type')
->andWhere('resource.course = links.course')
->andWhere('links.course = :course') ->andWhere('links.course = :course')
//->where('link.cId = ?', $course->getId()) //->where('link.cId = ?', $course->getId())
//->where('node.cId = 0') //->where('node.cId = 0')

Loading…
Cancel
Save