diff --git a/public/main/exercise/category.php b/public/main/exercise/category.php index 9e9aff2877..58eb494a8b 100644 --- a/public/main/exercise/category.php +++ b/public/main/exercise/category.php @@ -5,7 +5,7 @@ require_once __DIR__.'/../inc/global.inc.php'; if (false === api_get_configuration_value('allow_exercise_categories')) { - api_not_allowed(); + api_not_allowed(true); } api_protect_course_script(); diff --git a/public/main/inc/ajax/model.ajax.php b/public/main/inc/ajax/model.ajax.php index 9f1023fa7d..e1b195c078 100644 --- a/public/main/inc/ajax/model.ajax.php +++ b/public/main/inc/ajax/model.ajax.php @@ -2,6 +2,7 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Framework\Container; use ChamiloSession as Session; require_once __DIR__.'/../global.inc.php'; @@ -284,9 +285,11 @@ if (!$sidx) { switch ($action) { case 'get_exercise_categories': - $manager = new ExerciseCategoryManager(); $courseId = isset($_REQUEST['c_id']) ? $_REQUEST['c_id'] : 0; - $count = $manager->getCourseCount($courseId); + $repo = Container::getExerciseCategoryRepository(); + $qb = $repo->getResourcesByCourse(api_get_course_entity($courseId)); + $count = $qb->select('COUNT(resource)')->getQuery()->getSingleScalarResult(); + break; case 'get_calendar_users': $calendarPlugin = LearningCalendarPlugin::create(); @@ -933,13 +936,21 @@ switch ($action) { } $columns = ['name', 'actions']; - $manager = new ExerciseCategoryManager(); - - $result = $manager->get_all([ + $qb = $repo->getResourcesByCourse(api_get_course_entity($courseId)); + $items = $qb->getQuery()->getResult(); + /** @var \Chamilo\CourseBundle\Entity\CExerciseCategory $item */ + $result = []; + foreach ($items as $item) { + $result[] = [ + 'id' => $item->getId(), + 'name' => $item->getName() + ]; + } + /*$result = $manager->get_all([ 'where' => ['c_id = ? ' => $courseId], 'order' => "$sidx $sord", 'LIMIT' => "$start , $limit", - ]); + ]);*/ break; case 'get_calendar_users': $columns = ['firstname', 'lastname', 'exam']; diff --git a/public/main/inc/lib/ExerciseCategoryManager.php b/public/main/inc/lib/ExerciseCategoryManager.php index 98d5b8ed7b..74b64cb907 100644 --- a/public/main/inc/lib/ExerciseCategoryManager.php +++ b/public/main/inc/lib/ExerciseCategoryManager.php @@ -173,6 +173,41 @@ class ExerciseCategoryManager extends Model return $category; } + /** + * @param $token + * + * @return string + */ + public function getJqgridActionLinks($token) + { + //With this function we can add actions to the jgrid (edit, delete, etc) + $editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL); + $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL); + $confirmMessage = addslashes( + api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES) + ); + + $courseParams = api_get_cidreq(); + + $editButton = <<\ + $editIcon\ + +JAVASCRIPT; + $deleteButton = <<\ + $deleteIcon\ + +JAVASCRIPT; + + return "function action_formatter(cellvalue, options, rowObject) { + return '$editButton $deleteButton'; + }"; + } + /** * @param string $url * @param string $action @@ -245,107 +280,7 @@ class ExerciseCategoryManager extends Model $content .= ''; $content .= ''; - // 1. Set entity - $source = new Entity('ChamiloCourseBundle:CExerciseCategory'); - $repo = Container::getExerciseCategoryRepository(); - // 2. Get query builder from repo. - $qb = $repo->getResourcesByCourse($course, $session); - - // 3. Set QueryBuilder to the source. - $source->initQueryBuilder($qb); - - // 4. Get the grid builder. - $builder = Container::$container->get('apy_grid.factory'); - - // 5. Set parameters and properties. - $grid = $builder->createBuilder( - 'grid', - $source, - [ - 'persistence' => false, - 'route' => 'home', - 'filterable' => true, - 'sortable' => true, - 'max_per_page' => 10, - ] - )->add( - 'id', - 'number', - [ - 'title' => '#', - 'primary' => true, - 'visible' => false, - ] - )->add( - 'name', - 'text', - [ - 'title' => get_lang('Name'), - ] - ); - $grid = $grid->getGrid(); - - if (Container::getAuthorizationChecker()->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) { - // Add row actions - $myRowAction = new RowAction( - get_lang('Edit'), - 'legacy_main', - false, - '_self', - ['class' => 'btn btn-secondary'] - ); - $myRowAction->setRouteParameters( - ['id', 'name' => 'exercise/category.php', 'cidReq' => api_get_course_id(), 'action' => 'edit'] - ); - - $myRowAction->addManipulateRender( - function (RowAction $action, Row $row) use ($session, $repo) { - return $repo->rowCanBeEdited($action, $row, $session); - } - ); - - $grid->addRowAction($myRowAction); - - $myRowAction = new RowAction( - get_lang('Delete'), - 'legacy_main', - true, - '_self', - ['class' => 'btn btn-danger', 'form_delete' => true] - ); - $myRowAction->setRouteParameters( - ['id', 'name' => 'exercise/category.php', 'cidReq' => api_get_course_id(), 'action' => 'delete'] - ); - $myRowAction->addManipulateRender( - function (RowAction $action, Row $row) use ($session, $repo) { - return $repo->rowCanBeEdited($action, $row, $session); - } - ); - - $grid->addRowAction($myRowAction); - - if (empty($session)) { - // Add mass actions - $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( - '@ChamiloCore/Resource/grid.html.twig', - ['grid' => $grid] - ); + $content .= Display::grid_html('categories'); return $content; } diff --git a/src/CourseBundle/Entity/CExerciseCategory.php b/src/CourseBundle/Entity/CExerciseCategory.php index 5eadae844d..699f3e202a 100644 --- a/src/CourseBundle/Entity/CExerciseCategory.php +++ b/src/CourseBundle/Entity/CExerciseCategory.php @@ -32,28 +32,24 @@ class CExerciseCategory extends AbstractResource implements ResourceInterface protected $id; /** - * @var Course - * * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course") * @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false) */ - protected $course; + protected Course $course; /** - * @var string - * * @Assert\NotBlank * * @ORM\Column(name="name", type="string", length=255, nullable=false) */ - protected $name; + protected string $name; /** * @var string * * @ORM\Column(name="description", type="text", nullable=true) */ - protected $description; + protected string $description; /** * @Gedmo\SortablePosition @@ -62,16 +58,14 @@ class CExerciseCategory extends AbstractResource implements ResourceInterface */ protected $position; - /** - * Project constructor. - */ public function __construct() { + $this->description = ''; } public function __toString(): string { - return (string) $this->getName(); + return $this->getName(); } /** @@ -82,62 +76,31 @@ class CExerciseCategory extends AbstractResource implements ResourceInterface return $this->id; } - /** - * @param int $id - * - * @return CExerciseCategory - */ - public function setId($id) - { - $this->id = $id; - - return $this; - } - - /** - * @return string - */ - public function getName() + public function getName(): string { - return (string) $this->name; + return $this->name; } - /** - * @param string $name - * - * @return CExerciseCategory - */ - public function setName($name) + public function setName(string $name): self { $this->name = $name; return $this; } - /** - * @return string - */ - public function getDescription() + public function getDescription(): string { return $this->description; } - /** - * @param string $description - * - * @return CExerciseCategory - */ - public function setDescription($description) + public function setDescription(string $description): self { $this->description = $description; return $this; } - /** - * @return Course - */ - public function getCourse() + public function getCourse(): Course { return $this->course; } @@ -154,19 +117,13 @@ class CExerciseCategory extends AbstractResource implements ResourceInterface return $this->position; } - /** - * @return CExerciseCategory - */ - public function setPosition($position) + public function setPosition($position): self { $this->position = $position; return $this; } - /** - * Resource identifier. - */ public function getResourceIdentifier(): int { return $this->getId();