Completed
Push — master ( cc3037...815120 )
by Julito
29:55
created

CToolIntroController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 48
rs 9.125
c 1
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Controller;
5
6
use Chamilo\CoreBundle\Component\Editor\CkEditor\Toolbar\Introduction;
7
use Chamilo\CourseBundle\Controller\ToolBaseController;
8
use Chamilo\CourseBundle\Entity\CToolIntro;
9
use Chamilo\CourseBundle\Form\Type\CToolIntroType;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14
use Symfony\Component\HttpFoundation\Request;
15
use APY\DataGridBundle\Grid\Source\Entity;
16
use APY\DataGridBundle\Grid\Action\RowAction;
17
18
/**
19
 * Class CToolIntroController
20
 * @package Chamilo\CourseBundle\Controller
21
 * @author Julio Montoya <[email protected]>
22
 * @Route("/introduction")
23
 */
24
class CToolIntroController extends ToolBaseController
25
{
26
    /**
27
     * @Route("/")
28
     */
29
    public function indexAction()
30
    {
31
        $breadCrumb = $this->get('chamilo_core.block.breadcrumb');
32
        $breadCrumb->addChild($this->trans('Introduction'));
33
34
        // Creates a simple grid based on your entity (ORM)
35
        $source = new Entity('ChamiloCourseBundle:CToolIntro');
36
37
        // Get a Grid instance
38
        $grid = $this->get('grid');
39
40
        // Attach the source to the grid
41
        $grid->setSource($source);
42
43
        $rowUpdateAction = new RowAction(
44
            $this->trans('Update'),
45
            'chamilo_course_ctoolintro_update',
46
            false,
47
            '_self',
48
            ['class' => 'btn'],
49
            null//$role
50
        );
51
52
        $rowUpdateAction->setRouteParameters(
53
            ['iid', 'course' => $this->getCourse()->getCode()]
54
        );
55
56
        $rowAction = new RowAction(
57
            $this->trans('Delete'),
58
            'chamilo_course_ctoolintro_delete',
59
            true,
60
            '_self',
61
            ['class' => 'btn'],
62
            null//$role
63
        );
64
65
        $rowAction->setRouteParameters(
66
            ['iid', 'course' => $this->getCourse()->getCode()]
67
        );
68
69
        $grid->addRowAction($rowUpdateAction);
70
        $grid->addRowAction($rowAction);
71
72
        // Return the response of the grid to the template
73
        return $grid->getGridResponse(
74
            'ChamiloCourseBundle:CToolIntro:grid.html.twig'
75
        );
76
    }
77
78
    /**
79
     * @Route("/{tool}/create")
80
     * @Template("ChamiloCourseBundle:CToolIntro:create.html.twig")
81
     * @return array
82
     */
83
    public function createAction(Request $request, $tool)
84
    {
85
        // Breadcrumb
86
        $breadCrumb = $this->get('chamilo_core.block.breadcrumb');
87
        $breadCrumb->addChild(
88
            $this->trans('Introduction'),
89
            [
90
                'route' => 'chamilo_course_ctoolintro_index',
91
                'routeParameters' => [
92
                    'course' => $this->getCourse()->getCode(),
93
                ],
94
            ]
95
        );
96
        $breadCrumb->addChild($this->trans('Create'));
97
98
        $course = $this->getCourse();
99
        $session = $this->getSession();
100
101
        $toolIntro = new CToolIntro();
102
        $toolIntro
103
            ->setSessionId(0)
104
            ->setTool($tool)
105
            ->setCId($course->getId());
106
107
        if ($session) {
108
            $toolIntro->setSessionId($session->getId());
109
        }
110
111
        // Declared in forms.yml
112
        $formService = $this->get('chamilo_course.form.type.c_tool_intro');
113
114
        $form = $this->createForm($formService, $toolIntro);
115
        $form->handleRequest($request);
116
117 View Code Duplication
        if ($form->isValid()) {
118
            $em = $this->getDoctrine()->getManager();
119
            $em->persist($toolIntro);
120
            $em->flush();
121
            $this->addFlash('success', $this->trans('Saved'));
122
123
            return $this->redirectToRoute(
124
                'chamilo_course_ctoolintro_update',
125
                ['course' => $course->getCode(), 'iid' => $toolIntro->getId()]
126
            );
127
        }
128
129
        return ['form' => $form->createView(), 'tool' => $tool];
130
    }
131
132
    /**
133
     * @Route("/{iid}/update/")
134
     * @Method({"GET|POST"})
135
     * @Template("ChamiloCourseBundle:CToolIntro:update.html.twig")
136
     *
137
     * @return Response
138
     */
139
    public function updateAction($iid, Request $request)
140
    {
141
        // Breadcrumb
142
        $breadCrumb = $this->get('chamilo_core.block.breadcrumb');
143
        $breadCrumb->addChild(
144
            $this->trans('Introduction'),
145
            [
146
                'route' => 'chamilo_course_ctoolintro_index',
147
                'routeParameters' => [
148
                    'course' => $this->getCourse()->getCode(),
149
                ],
150
            ]
151
        );
152
        $breadCrumb->addChild($this->trans('Update'));
153
154
        $course = $this->getCourse();
155
156
        $em = $this->get('doctrine')->getManager();
157
        $criteria = [
158
            'iid' => $iid,
159
        ];
160
161
        $formService = $this->get('chamilo_course.form.type.c_tool_intro');
162
163
        $toolIntro = $em->getRepository('ChamiloCourseBundle:CToolIntro')->findOneBy($criteria);
164
        $form = $this->createForm($formService, $toolIntro);
165
        $tool = $toolIntro->getTool();
166
        $form->handleRequest($request);
167
168 View Code Duplication
        if ($form->isValid()) {
169
            $em = $this->getDoctrine()->getManager();
170
            $em->persist($toolIntro);
171
            $em->flush();
172
            $this->addFlash('success', $this->trans('Updated'));
173
174
            return $this->redirectToRoute(
175
                'chamilo_course_ctoolintro_update',
176
                ['course' => $course->getCode(), 'tool' => $tool, 'iid' => $iid]
177
            );
178
        }
179
180
        return [
181
            'tool' => $tool,
182
            'iid' => $iid,
183
            'form' => $form->createView(),
184
        ];
185
    }
186
187
    /**
188
     * @Route("/{iid}/delete")
189
     * @Method({"GET"})
190
     * @param int $iid
191
     * @param Request $request
192
     *
193
     * @return Response
194
     *
195
     */
196
    public function deleteAction($iid, Request $request)
197
    {
198
        $criteria = array(
199
            'iid' => $iid,
200
        );
201
202
        $doctrine = $this->getDoctrine();
203
        $toolIntro = $doctrine->getRepository('ChamiloCourseBundle:CToolIntro')->findOneBy($criteria);
204
        if ($toolIntro) {
205
            $doctrine->getManager()->remove($toolIntro);
206
            $doctrine->getManager()->flush();
207
            $this->addFlash('success', $this->trans("IntroductionTextDeleted"));
208
        }
209
210
        return $this->redirectCourseHome();
211
    }
212
}
213