GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — user-entity ( a5fdbe...6d5c03 )
by Luis Ramón
02:54
created

GroupController::apiNewUserAction()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 3
eloc 19
nc 2
nop 1
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2016: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Controller;
22
23
use AppBundle\Entity\Agreement;
24
use AppBundle\Entity\Group;
25
use AppBundle\Entity\User;
26
use AppBundle\Entity\Workday;
27
use AppBundle\Form\Model\Calendar;
28
use Doctrine\Common\Collections\ArrayCollection;
29
use Doctrine\ORM\EntityManager;
30
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
31
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
32
use Symfony\Component\HttpFoundation\Request;
33
34
/**
35
 * @Route("/alumnado")
36
 * @Security("is_granted('ROLE_GROUP_TUTOR')")
37
 */
38
class GroupController extends BaseController
39
{
40
    /**
41
     * @Route("", name="admin_tutor_group", methods={"GET"})
42
     */
43
    public function groupIndexAction(Request $request)
44
    {
45
        /** @var EntityManager $em */
46
        $em = $this->getDoctrine()->getManager();
47
48
        /** @var User $user */
49
        $user = $this->getUser();
50
51
        $qb = $em->getRepository('AppBundle:Group')
52
            ->createQueryBuilder('g')
53
            ->innerJoin('g.training', 't')
54
            ->innerJoin('t.department', 'd')
55
            ->orderBy('d.name')
56
            ->addOrderBy('t.name')
57
            ->addOrderBy('g.name');
58
59
        if (!$user->isGlobalAdministrator()) {
60
            $qb = $qb
61
                ->where('g.id IN (:groups)')
62
                ->orWhere('d.head = :user')
63
                ->setParameter('groups', $user->getTutorizedGroups()->toArray())
64
                ->setParameter('user', $user);
65
        }
66
67
        $items = $qb->getQuery()->getResult();
68
69
        if (count($items) == 1) {
70
            return $this->groupDetailIndexAction($items[0], $request);
71
        }
72
73
        return $this->render('group/group_index.html.twig',
74
            [
75
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
76
                'title' => null,
77
                'elements' => $items
78
            ]);
79
    }
80
81
    /**
82
     * @Route("/grupo/{id}", name="admin_group_students", methods={"GET"})
83
     * @Security("is_granted('GROUP_MANAGE', group)")
84
     */
85
    public function groupDetailIndexAction(Group $group, Request $request)
86
    {
87
        /** @var EntityManager $em */
88
        $em = $this->getDoctrine()->getManager();
89
90
        $usersQuery = $em->createQuery('SELECT u FROM AppBundle:User u WHERE u.studentGroup = :group')
91
            ->setParameter('group', $group);
92
93
        $paginator = $this->get('knp_paginator');
94
        $pagination = $paginator->paginate(
95
            $usersQuery,
96
            $request->query->getInt('page', 1),
97
            $this->getParameter('page.size'),
98
            [
99
                'defaultSortFieldName' => 'u.lastName',
100
                'defaultSortDirection' => 'asc'
101
            ]
102
        );
103
104
        return $this->render('group/manage_students.html.twig', [
105
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
106
            'breadcrumb' => [
107
                ['fixed' => (string) $group],
108
            ],
109
            'title' => $group->getName(),
110
            'pagination' => $pagination
111
        ]);
112
113
    }
114
115
    /**
116
     * @Route("/alumnado/estudiante/{id}", name="student_detail", methods={"GET", "POST"})
117
     * @Security("is_granted('GROUP_MANAGE', student.getStudentGroup())")
118
     */
119
    public function studentIndexAction(User $student, Request $request)
120
    {
121
        $form = $this->createForm('AppBundle\Form\Type\StudentUserType', $student, [
122
            'admin' => $this->isGranted('ROLE_ADMIN')
123
        ]);
124
125
        $form->handleRequest($request);
126
127 View Code Duplication
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
129
            // Guardar el usuario en la base de datos
130
131
            // Probar a guardar los cambios
132
            try {
133
                $this->getDoctrine()->getManager()->flush();
134
                $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'student'));
135
                return $this->redirectToRoute('admin_group_students', ['id' => $student->getStudentGroup()->getId()]);
136
            } catch (\Exception $e) {
137
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'student'));
138
            }
139
        }
140
        return $this->render('group/form_student.html.twig',
141
            [
142
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
143
                'breadcrumb' => [
144
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
145
                    ['fixed' => (string) $student],
146
                ],
147
                'title' => (string) $student,
148
                'user' => $student,
149
                'form' => $form->createView()
150
            ]);
151
    }
152
153
    /**
154
     * @Route("/seguimiento/seleccion/{id}", name="admin_group_student_agreements", methods={"GET"})
155
     * @Security("is_granted('GROUP_MANAGE', student.getStudentGroup())")
156
     */
157
    public function studentAgreementIndexAction(User $student)
158
    {
159
        $agreements = $student->getStudentAgreements();
160
161
        return $this->render('group/calendar_agreement_select.html.twig',
162
            [
163
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
164
                'breadcrumb' => [
165
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
166
                    ['fixed' => (string) $student],
167
                ],
168
                'student' => $student,
169
                'elements' => $agreements,
170
                'route_name' => 'admin_group_student_calendar'
171
            ]);
172
    }
173
174
    /**
175
     * @Route("/seguimiento/acuerdo/{id}", name="admin_group_student_calendar", methods={"GET"})
176
     * @Security("is_granted('AGREEMENT_ACCESS', agreement)")
177
     */
178
    public function studentCalendarAgreementIndexAction(Agreement $agreement)
179
    {
180
        $student = $agreement->getStudent();
181
182
        $calendar = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->getArrayCalendar($agreement->getWorkdays());
183
        $title = (string) $agreement->getWorkcenter();
184
185
        return $this->render('group/calendar_agreement.html.twig',
186
            [
187
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
188
                'breadcrumb' => [
189
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
190
                    ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
191
                    ['fixed' => $title],
192
                ],
193
                'title' => $title,
194
                'user' => $this->getUser(),
195
                'calendar' => $calendar,
196
                'agreement' => $agreement,
197
                'route_name' => 'admin_group_student_tracking'
198
            ]);
199
    }
200
201
    public function deleteWorkdayAction(Agreement $agreement, Request $request)
202
    {
203
        $this->denyAccessUnlessGranted('AGREEMENT_MANAGE', $agreement);
204
205
        $em = $this->getDoctrine()->getManager();
206
207
        if ($request->request->has('ids')) {
208
            try {
209
                $ids = $request->request->get('ids');
210
211
                $dates = $em->getRepository('AppBundle:Workday')->createQueryBuilder('w')
212
                    ->where('w.id IN (:ids)')
213
                    ->andWhere('w.agreement = :agreement')
214
                    ->setParameter('ids', $ids)
215
                    ->setParameter('agreement', $agreement)
216
                    ->getQuery()
217
                    ->getResult();
218
219
                /** @var Workday $date */
220
                foreach ($dates as $date) {
221
                    if ($date->getTrackedHours() === 0.0) {
222
                        $em->remove($date);
223
                    }
224
                }
225
                $em->flush();
226
227
                $agreement->setFromDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealFromDate($agreement));
228
                $agreement->setToDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealToDate($agreement));
229
230
                $em->flush();
231
                $this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], 'calendar'));
232
            } catch (\Exception $e) {
233
                $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'calendar'));
234
            }
235
        }
236
        return $this->redirectToRoute('admin_group_student_calendar', ['id' => $agreement->getId()]);
237
    }
238
239
    /**
240
     * @Route("/seguimiento/acuerdo/{id}/operacion", name="admin_group_student_workday_operation", methods={"POST"})
241
     * @Security("is_granted('AGREEMENT_ACCESS', agreement)")
242
     */
243
    public function operationWorkdayAction(Agreement $agreement, Request $request)
244
    {
245
        if ($request->request->has('delete')) {
246
            return $this->deleteWorkdayAction($agreement, $request);
247
        } elseif ($request->request->has('week_lock') || ($request->request->has('week_unlock'))) {
248
            return $this->lockWeekAction($agreement, $request, $request->request->has('week_lock'), 'admin_group_student_calendar');
249
        } else {
250
            return $this->lockWorkdayAction($agreement, $request, $request->request->has('lock'), 'admin_group_student_calendar');
251
        }
252
    }
253
254
    /**
255
     * @Route("/seguimiento/acuerdo/{id}/incorporar", name="admin_group_student_workday_add", methods={"GET", "POST"})
256
     * @Security("is_granted('AGREEMENT_MANAGE', agreement)")
257
     */
258
    public function addAgreementCalendarAction(Agreement $agreement, Request $request)
259
    {
260
        $totalHours = $agreement->getStudent()->getStudentGroup()->getTraining()->getProgramHours();
261
        $agreementHours = $this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->countHours($agreement);
262
        $studentHours = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->countAgreementHours($agreement->getStudent());
263
264
        $calendar = new Calendar(max(0, $totalHours - $studentHours));
265
266
        $form = $this->createForm('AppBundle\Form\Type\CalendarType', $calendar, [
267
            'program_hours' => $totalHours
268
        ]);
269
270
        $form->handleRequest($request);
271
272
        $workdays = new ArrayCollection();
273
274
        if ($form->isValid() && $form->isSubmitted()) {
275
            $workdays = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->createCalendar($calendar, $agreement);
276
277
            if ($request->request->has('submit')) {
278
                $this->getDoctrine()->getManager()->flush();
279
                $agreement->setFromDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealFromDate($agreement));
280
                $agreement->setToDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealToDate($agreement));
281
                $this->getDoctrine()->getManager()->flush();
282
                $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'calendar'));
283
                return $this->redirectToRoute('admin_group_student_calendar', ['id' => $agreement->getId()]);
284
            }
285
        }
286
287
        $student = $agreement->getStudent();
288
289
        $calendar = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->getArrayCalendar($workdays);
290
291
        return $this->render('group/calendar_agreement_workday_add.html.twig', [
292
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
293
            'breadcrumb' => [
294
                ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
295
                ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
296
                ['fixed' => (string) $agreement->getWorkcenter()],
297
                ['fixed' => $this->get('translator')->trans('form.add', [], 'calendar')]
298
            ],
299
            'agreement' => $agreement,
300
            'total_hours' => $totalHours,
301
            'agreement_hours' => $agreementHours,
302
            'student_hours' => $studentHours,
303
            'form' => $form->createView(),
304
            'calendar' => $calendar
305
        ]);
306
    }
307
308
    /**
309
     * @Route("/seguimiento/acuerdo/jornada/{id}", name="admin_group_student_tracking", methods={"GET", "POST"})
310
     * @Security("is_granted('AGREEMENT_ACCESS', workday.getAgreement())")
311
     */
312
    public function studentWorkdayAction(Workday $workday, Request $request)
313
    {
314
        $student = $workday->getAgreement()->getStudent();
315
316
        return $this->baseWorkdayAction($workday, $request, [
317
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
318
            'breadcrumb' => [
319
                ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
320
                ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
321
                ['fixed' => (string) $workday->getAgreement()->getWorkcenter(), 'path' => 'admin_group_student_calendar', 'options' => ['id' => $workday->getAgreement()->getId()]],
322
                ['fixed' => $workday->getDate()->format('d/m/Y')]
323
            ],
324
            'back_route_name' => 'admin_group_student_calendar'
325
        ]);
326
    }
327
328
    /**
329
     * @Route("/seguimiento/acuerdo/jornada/modificar/{id}", name="admin_group_student_workday_form", methods={"GET", "POST"})
330
     * @Security("is_granted('AGREEMENT_MANAGE', workday.getAgreement())")
331
     */
332
    public function agreementCalendarFormAction(Workday $workday, Request $request)
333
    {
334
        $form = $this->createForm('AppBundle\Form\Type\WorkdayType', $workday);
335
        $form->handleRequest($request);
336
337
        if ($form->isValid() && $form->isSubmitted()) {
338
            $this->getDoctrine()->getManager()->flush();
339
            $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'calendar'));
340
            return $this->redirectToRoute('admin_group_student_calendar', ['id' => $workday->getAgreement()->getId()]);
341
        }
342
        $student = $workday->getAgreement()->getStudent();
343
344
        $dow = ((6 + (int) $workday->getDate()->format('w')) % 7);
345
        $title = $this->get('translator')->trans('dow' . $dow, [], 'calendar') . ', ' . $workday->getDate()->format('d/m/Y');
346
347
        return $this->render('group/calendar_agreement_workday_form.html.twig', [
348
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
349
            'breadcrumb' => [
350
                ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
351
                ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
352
                ['fixed' => (string) $workday->getAgreement()->getWorkcenter(), 'path' => 'admin_group_student_calendar', 'options' => ['id' => $workday->getAgreement()->getId()]],
353
                ['fixed' => $title],
354
            ],
355
            'form' => $form->createView(),
356
            'workday' => $workday
357
        ]);
358
    }
359
360
    /**
361
     * @Route("/seguimiento/acuerdo/nuevo/{id}", name="admin_group_student_agreement_new", methods={"GET", "POST"})
362
     * @Security("is_granted('GROUP_CREATE_AGREEMENT', user.getStudentGroup())")
363
     */
364
    public function agreementNewFormAction(User $user, Request $request)
365
    {
366
        $agreement = new Agreement();
367
        $agreement->setStudent($user);
368
        $this->getDoctrine()->getManager()->persist($agreement);
369
370
        return $this->agreementFormAction($agreement, $request);
371
    }
372
373
    /**
374
     * @Route("/seguimiento/acuerdo/modificar/{id}", name="admin_group_student_agreement_form", methods={"GET", "POST"})
375
     * @Security("is_granted('AGREEMENT_MANAGE', agreement)")
376
     */
377
    public function agreementFormAction(Agreement $agreement, Request $request)
378
    {
379
        $form = $this->createForm('AppBundle\Form\Type\AgreementType', $agreement);
380
381
        $form->handleRequest($request);
382
383
        $student = $agreement->getStudent();
384
385
        if ($form->isValid() && $form->isSubmitted()) {
386
            // Probar a guardar los cambios
387
            try {
388
                $this->getDoctrine()->getManager()->flush();
389
                $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'group'));
390
                return $this->redirectToRoute('admin_group_student_agreements', ['id' => $student->getId()]);
391
            } catch (\Exception $e) {
392
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'group'));
393
            }
394
        }
395
396
        $title = (null === $agreement->getId())
397
            ? $this->get('translator')->trans('form.new_agreement', [], 'group')
398
            : (string) $agreement->getWorkcenter();
399
400
        return $this->render('group/form_agreement.html.twig',
401
            [
402
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
403
                'breadcrumb' => [
404
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
405
                    ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
406
                    ['fixed' => $title]
407
                ],
408
                'form' => $form->createView(),
409
                'agreement' => $agreement
410
            ]);
411
    }
412
413
    /**
414
     * @Route("/seguimiento/acuerdo/eliminar/{id}", name="admin_group_student_agreement_delete", methods={"GET", "POST"})
415
     * @Security("is_granted('AGREEMENT_MANAGE', agreement)")
416
     */
417
    public function agreementDeleteAction(Agreement $agreement, Request $request)
418
    {
419
        $student = $agreement->getStudent();
420
421
        if ('POST' === $request->getMethod() && $request->request->has('delete')) {
422
423
            // Eliminar el acuerdo de la base de datos
424
            $this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->delete($agreement);
425
            try {
426
                $this->getDoctrine()->getManager()->flush();
427
                $this->addFlash('success', $this->get('translator')->trans('alert.agreement_deleted', [], 'group'));
428
            } catch (\Exception $e) {
429
                $this->addFlash('error', $this->get('translator')->trans('alert.agreement_not_deleted', [], 'group'));
430
            }
431
            return $this->redirectToRoute('admin_group_student_agreements', ['id' => $student->getId()]);
432
        }
433
434
        $title = (string) $agreement->getWorkcenter();
435
436
        return $this->render('group/delete_agreement.html.twig', [
437
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
438
            'breadcrumb' => [
439
                ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
440
                ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
441
                ['fixed' => $title]
442
            ],
443
            'title' => $title,
444
            'agreement' => $agreement
445
        ]);
446
    }
447
}
448