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 ( 435b1a...e96cee )
by Luis Ramón
02:50
created

GroupController::operationWorkdayAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
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\Tracking;
26
use AppBundle\Entity\User;
27
use AppBundle\Entity\Workday;
28
use AppBundle\Form\Model\Calendar;
29
use Doctrine\Common\Collections\ArrayCollection;
30
use Doctrine\ORM\EntityManager;
31
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
32
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
33
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
34
use Symfony\Component\HttpFoundation\Request;
35
36
/**
37
 * @Route("/alumnado")
38
 * @Security("is_granted('ROLE_GROUP_TUTOR')")
39
 */
40
class GroupController extends Controller
41
{
42
    /**
43
     * @Route("", name="admin_tutor_group", methods={"GET"})
44
     */
45
    public function groupIndexAction(Request $request)
46
    {
47
        /** @var EntityManager $em */
48
        $em = $this->getDoctrine()->getManager();
49
50
        /** @var User $user */
51
        $user = $this->getUser();
52
53
        $qb = $em->getRepository('AppBundle:Group')
54
            ->createQueryBuilder('g')
55
            ->innerJoin('g.training', 't')
56
            ->innerJoin('t.department', 'd')
57
            ->orderBy('d.name')
58
            ->addOrderBy('t.name')
59
            ->addOrderBy('g.name');
60
61
        if (!$user->isGlobalAdministrator()) {
62
            $qb = $qb
63
                ->where('g.id IN (:groups)')
64
                ->orWhere('d.head = :user')
65
                ->setParameter('groups', $user->getTutorizedGroups()->toArray())
66
                ->setParameter('user', $user);
67
        }
68
69
        $items = $qb->getQuery()->getResult();
70
71
        if (count($items) == 1) {
72
            return $this->groupDetailIndexAction($items[0], $request);
73
        }
74
75
        return $this->render('group/group_index.html.twig',
76
            [
77
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
78
                'title' => null,
79
                'elements' => $items
80
            ]);
81
    }
82
83
    /**
84
     * @Route("/alumnado/{id}", name="admin_group_students", methods={"GET"})
85
     * @Security("is_granted('GROUP_MANAGE', group)")
86
     */
87 View Code Duplication
    public function groupDetailIndexAction(Group $group, Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
    {
89
        /** @var EntityManager $em */
90
        $em = $this->getDoctrine()->getManager();
91
92
        $usersQuery = $em->createQuery('SELECT u FROM AppBundle:User u WHERE u.studentGroup = :group')
93
            ->setParameter('group', $group);
94
95
        $paginator  = $this->get('knp_paginator');
96
        $pagination = $paginator->paginate(
97
            $usersQuery,
98
            $request->query->getInt('page', 1),
99
            $this->getParameter('page.size'),
100
            [
101
                'defaultSortFieldName' => 'u.lastName',
102
                'defaultSortDirection' => 'asc'
103
            ]
104
        );
105
106
        return $this->render('group/manage_students.html.twig',
107
            [
108
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
109
                'breadcrumb' => [
110
                    ['fixed' => $group->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $group->getId()]],
111
                ],
112
                'title' => $group->getName(),
113
                'pagination' => $pagination,
114
                'user' => $this->getUser()
115
            ]);
116
    }
117
118
    /**
119
     * @Route("/seguimiento/seleccion/{id}", name="admin_group_student_agreements", methods={"GET"})
120
     * @Security("is_granted('GROUP_MANAGE', student.getStudentGroup())")
121
     */
122
    public function studentAgreementIndexAction(User $student)
123
    {
124
        $agreements = $student->getStudentAgreements();
125
126
        return $this->render('group/calendar_agreement_select.html.twig',
127
            [
128
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
129
                'breadcrumb' => [
130
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
131
                    ['fixed' => (string) $student],
132
                ],
133
                'student' => $student,
134
                'elements' => $agreements,
135
                'route_name' => 'admin_group_student_calendar'
136
            ]);
137
    }
138
139
    /**
140
     * @Route("/seguimiento/acuerdo/{id}", name="admin_group_student_calendar", methods={"GET"})
141
     * @Security("is_granted('AGREEMENT_ACCESS', agreement)")
142
     */
143
    public function studentCalendarAgreementIndexAction(Agreement $agreement)
144
    {
145
        $student = $agreement->getStudent();
146
147
        $calendar = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->getArrayCalendar($agreement->getWorkdays());
148
        $title = (string) $agreement->getWorkcenter();
149
150
        return $this->render('group/calendar_agreement.html.twig',
151
            [
152
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
153
                'breadcrumb' => [
154
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
155
                    ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
156
                    ['fixed' => $title],
157
                ],
158
                'title' => $title,
159
                'user' => $this->getUser(),
160
                'calendar' => $calendar,
161
                'agreement' => $agreement,
162
                'route_name' => 'admin_group_student_tracking'
163
            ]);
164
    }
165
166
    public function lockWorkdayAction(Agreement $agreement, Request $request, $status)
167
    {
168
        $this->denyAccessUnlessGranted($status ? 'AGREEMENT_LOCK' : 'AGREEMENT_UNLOCK', $agreement);
169
170
        $em = $this->getDoctrine()->getManager();
171
172 View Code Duplication
        if ($request->request->has('ids')) {
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...
173
            try {
174
                $ids = $request->request->get('ids');
175
176
                $em->createQuery('UPDATE AppBundle:Workday w SET w.locked = :locked WHERE w.id IN (:ids) AND w.agreement = :agreement')
177
                    ->setParameter('locked', $status)
178
                    ->setParameter('ids', $ids)
179
                    ->setParameter('agreement', $agreement)
180
                    ->execute();
181
182
                $em->flush();
183
                $this->addFlash('success', $this->get('translator')->trans('alert.locked', [], 'calendar'));
184
            } catch (\Exception $e) {
185
                $this->addFlash('error', $this->get('translator')->trans('alert.locked_error', [], 'calendar'));
186
            }
187
        }
188
        return $this->redirectToRoute('admin_group_student_calendar', ['id' => $agreement->getId()]);
189
    }
190
191
    public function deleteWorkdayAction(Agreement $agreement, Request $request)
192
    {
193
        $this->denyAccessUnlessGranted('AGREEMENT_MANAGE', $agreement);
194
195
        $em = $this->getDoctrine()->getManager();
196
197 View Code Duplication
        if ($request->request->has('ids')) {
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...
198
            try {
199
                $ids = $request->request->get('ids');
200
201
                $dates = $em->getRepository('AppBundle:Workday')->createQueryBuilder('w')
202
                    ->where('w.id IN (:ids)')
203
                    ->andWhere('w.agreement = :agreement')
204
                    ->setParameter('ids', $ids)
205
                    ->setParameter('agreement', $agreement)
206
                    ->getQuery()
207
                    ->getResult();
208
209
                /** @var Workday $date */
210
                foreach ($dates as $date) {
211
                    if ($date->getTrackedHours() === 0) {
212
                        $em->remove($date);
213
                    }
214
                }
215
                $em->flush();
216
217
                $agreement->setFromDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealFromDate($agreement));
218
                $agreement->setToDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealToDate($agreement));
219
220
                $em->flush();
221
                $this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], 'calendar'));
222
            } catch (\Exception $e) {
223
                $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'calendar'));
224
            }
225
        }
226
        return $this->redirectToRoute('admin_group_student_calendar', ['id' => $agreement->getId()]);
227
    }
228
229
    /**
230
     * @Route("/seguimiento/acuerdo/{id}/operacion", name="admin_group_student_workday_operation", methods={"POST"})
231
     * @Security("is_granted('AGREEMENT_ACCESS',agreement)")
232
     */
233
    public function operationWorkdayAction(Agreement $agreement, Request $request)
234
    {
235
        if ($request->request->has('delete')) {
236
            return $this->deleteWorkdayAction($agreement, $request);
237
        } else {
238
            return $this->lockWorkdayAction($agreement, $request, $request->request->has('lock'));
239
        }
240
241
    }
242
243
    /**
244
     * @Route("/seguimiento/acuerdo/{id}/incorporar", name="admin_group_student_workday_add", methods={"GET", "POST"})
245
     * @Security("is_granted('AGREEMENT_MANAGE', agreement)")
246
     */
247
    public function addAgreementCalendarAction(Agreement $agreement, Request $request)
248
    {
249
        $totalHours = $agreement->getStudent()->getStudentGroup()->getTraining()->getProgramHours();
250
        $agreementHours = $this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->countHours($agreement);
251
        $studentHours = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->countAgreementHours($agreement->getStudent());
252
253
        $calendar = new Calendar(max(0, $totalHours - $studentHours));
254
255
        $form = $this->createForm('AppBundle\Form\Type\CalendarType', $calendar, [
256
            'program_hours' => $totalHours
257
        ]);
258
259
        $form->handleRequest($request);
260
261
        $workdays = new ArrayCollection();
262
263 View Code Duplication
        if ($form->isValid() && $form->isSubmitted()) {
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...
264
            $workdays = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->createCalendar($calendar, $agreement);
265
266
            if ($request->request->has('submit')) {
267
                $this->getDoctrine()->getManager()->flush();
268
                $agreement->setFromDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealFromDate($agreement));
269
                $agreement->setToDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealToDate($agreement));
270
                $this->getDoctrine()->getManager()->flush();
271
                $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'calendar'));
272
                return $this->redirectToRoute('admin_group_student_calendar', ['id' => $agreement->getId()]);
273
            }
274
        }
275
276
        $student = $agreement->getStudent();
277
278
        $calendar = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->getArrayCalendar($workdays);
279
280
        return $this->render('group/calendar_agreement_workday_add.html.twig', [
281
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
282
            'breadcrumb' => [
283
                ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
284
                ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
285
                ['fixed' => (string) $agreement->getWorkcenter()],
286
                ['fixed' => $this->get('translator')->trans('form.add', [], 'calendar')]
287
            ],
288
            'agreement' => $agreement,
289
            'total_hours' => $totalHours,
290
            'agreement_hours' => $agreementHours,
291
            'student_hours' => $studentHours,
292
            'form' => $form->createView(),
293
            'calendar' => $calendar
294
        ]);
295
    }
296
297
    /**
298
     * @Route("/seguimiento/acuerdo/jornada/{id}", name="admin_group_student_tracking", methods={"GET", "POST"})
299
     * @Security("is_granted('AGREEMENT_ACCESS', workday.getAgreement())")
300
     */
301
    public function studentWorkdayAction(Workday $workday, Request $request)
302
    {
303
        $student = $workday->getAgreement()->getStudent();
304
305
        $agreementHours = $this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->countHours($workday->getAgreement());
306
307
        $dow = ((6 + (int) $workday->getDate()->format('w')) % 7);
308
        $title = $this->get('translator')->trans('dow' . $dow, [], 'calendar') . ', ' . $workday->getDate()->format('d/m/Y');
309
310
        $em = $this->getDoctrine()->getManager();
311
        $em->getRepository('AppBundle:Tracking')->updateTrackingByWorkday($workday);
312
313
        $form = $this->createForm('AppBundle\Form\Type\WorkdayTrackingType', $workday);
314
        $form->handleRequest($request);
315
316 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...
317
            /** @var Tracking $tracking */
318
            foreach ($workday->getTrackingActivities() as $tracking) {
319
                if ($tracking->getHours() == 0) {
320
                    $workday->removeTrackingActivity($tracking);
321
                    $em->remove($tracking);
322
                } else {
323
                    $em->persist($tracking);
324
                }
325
            }
326
            $em->flush();
327
        }
328
329
        $activitiesStats = $em->getRepository('AppBundle:Agreement')->getActivitiesStats($workday->getAgreement());
330
        $next = $em->getRepository('AppBundle:Workday')->getNext($workday);
331
        $previous = $em->getRepository('AppBundle:Workday')->getPrevious($workday);
332
333
        return $this->render('student/tracking_form.html.twig',
334
            [
335
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
336
                'breadcrumb' => [
337
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
338
                    ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
339
                    ['fixed' => (string) $workday->getAgreement()->getWorkcenter(), 'path' => 'admin_group_student_calendar', 'options' => ['id' => $workday->getAgreement()->getId()]],
340
                    ['fixed' => $title],
341
                ],
342
                'title' => $title,
343
                'user' => $this->getUser(),
344
                'workday' => $workday,
345
                'form' => $form->createView(),
346
                'agreement_hours' => $agreementHours,
347
                'activities_stats' => $activitiesStats,
348
                'next' => $next,
349
                'previous' => $previous,
350
                'back_route_name' => 'admin_group_student_calendar'
351
            ]);
352
    }
353
354
    /**
355
     * @Route("/seguimiento/acuerdo/jornada/modificar/{id}", name="admin_group_student_workday_form", methods={"GET", "POST"})
356
     * @Security("is_granted('AGREEMENT_MANAGE', workday.getAgreement())")
357
     */
358
    public function agreementCalendarFormAction(Workday $workday, Request $request)
359
    {
360
        $form = $this->createForm('AppBundle\Form\Type\WorkdayType', $workday);
361
        $form->handleRequest($request);
362
363 View Code Duplication
        if ($form->isValid() && $form->isSubmitted()) {
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...
364
            $this->getDoctrine()->getManager()->flush();
365
            $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'calendar'));
366
            return $this->redirectToRoute('admin_group_student_calendar', ['id' => $workday->getAgreement()->getId()]);
367
        }
368
        $student = $workday->getAgreement()->getStudent();
369
370
        $dow = ((6 + (int) $workday->getDate()->format('w')) % 7);
371
        $title = $this->get('translator')->trans('dow' . $dow, [], 'calendar') . ', ' . $workday->getDate()->format('d/m/Y');
372
373
        return $this->render('group/calendar_agreement_workday_form.html.twig', [
374
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
375
            'breadcrumb' => [
376
                ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
377
                ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
378
                ['fixed' => (string) $workday->getAgreement()->getWorkcenter(), 'path' => 'admin_group_student_calendar', 'options' => ['id' => $workday->getAgreement()->getId()]],
379
                ['fixed' => $title],
380
            ],
381
            'form' => $form->createView(),
382
            'workday' => $workday
383
        ]);
384
    }
385
386
    /**
387
     * @Route("/seguimiento/acuerdo/nuevo/{id}", name="admin_group_student_agreement_new", methods={"GET", "POST"})
388
     * @Security("is_granted('GROUP_CREATE_AGREEMENT', user.getStudentGroup())")
389
     */
390
    public function agreementNewFormAction(User $user, Request $request)
391
    {
392
        $agreement = new Agreement();
393
        $agreement->setStudent($user);
394
        $this->getDoctrine()->getManager()->persist($agreement);
395
396
        return $this->agreementFormAction($agreement, $request);
397
    }
398
399
    /**
400
     * @Route("/seguimiento/acuerdo/modificar/{id}", name="admin_group_student_agreement_form", methods={"GET", "POST"})
401
     * @Security("is_granted('AGREEMENT_MANAGE', agreement)")
402
     */
403
    public function agreementFormAction(Agreement $agreement, Request $request)
404
    {
405
        $form = $this->createForm('AppBundle\Form\Type\AgreementType', $agreement);
406
407
        $form->handleRequest($request);
408
409
        $student = $agreement->getStudent();
410
411 View Code Duplication
        if ($form->isValid() && $form->isSubmitted()) {
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...
412
            // Probar a guardar los cambios
413
            try {
414
                $this->getDoctrine()->getManager()->flush();
415
                $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'group'));
416
                return $this->redirectToRoute('admin_group_student_agreements', ['id' => $student->getId()]);
417
            } catch (\Exception $e) {
418
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'group'));
419
            }
420
        }
421
422
        return $this->render('group/form_agreement.html.twig',
423
            [
424
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'),
425
                'breadcrumb' => [
426
                    ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]],
427
                    ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]],
428
                    ['fixed' => $this->get('translator')->trans('form.new_agreement', [], 'group')]
429
                ],
430
                'form' => $form->createView(),
431
                'agreement' => $agreement
432
            ]);
433
    }
434
}
435