DirectorView.php 50 KB
Newer Older
Open Source Developer's avatar
transf    
Open Source Developer committed
1
2
3
4
5
6
7
8
9
10
<?php

namespace Drupal\epal\Controller;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Open Source Developer's avatar
trans    
Open Source Developer committed
11
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
Open Source Developer's avatar
transf    
Open Source Developer committed
12
13
14
15

class DirectorView extends ControllerBase
{
    protected $entityTypeManager;
16
    protected $logger;
Open Source Developer's avatar
trans    
Open Source Developer committed
17
  //  protected $testSchoolId='0640050';
Open Source Developer's avatar
transf    
Open Source Developer committed
18

19
20
    public function __construct(EntityTypeManagerInterface $entityTypeManager,
        LoggerChannelFactoryInterface $loggerChannel)
Open Source Developer's avatar
transf    
Open Source Developer committed
21
22
    {
        $this->entityTypeManager = $entityTypeManager;
23
        $this->logger = $loggerChannel->get('epal-school');
Open Source Developer's avatar
transf    
Open Source Developer committed
24
25
26
27
28
    }

    public static function create(ContainerInterface $container)
    {
        return new static(
29
30
            $container->get('entity_type.manager'),
            $container->get('logger.factory')
Open Source Developer's avatar
transf    
Open Source Developer committed
31
        );
32
    }
Open Source Developer's avatar
transf    
Open Source Developer committed
33

34
    public function getSectorsPerSchool(Request $request)
Open Source Developer's avatar
transf    
Open Source Developer committed
35
36
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
37
38

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
39
40
41
42
        $user = reset($users);
        if ($user) {
            $epalId = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->
Open Source Developer's avatar
Open Source Developer committed
43
                     loadByProperties(array('id' => $epalId));
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
59
                }
60
61
62
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
63
64
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
65
66
67
68
69
70
71
72
            } elseif ($userRole === 'epal') {
                $sectorPerSchool = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $epalId));
                if ($sectorPerSchool) {
                    $list = array();
                    foreach ($sectorPerSchool as $object) {
                        $list[] = array(
                                    'sector_id' => $object->sector_id->entity->get('name')->value,
                                    'id' => $object->sector_id->entity->id(),
Open Source Developer's avatar
Open Source Developer committed
73
                                    );
74
                    }
Open Source Developer's avatar
Open Source Developer committed
75

76
77
                    return $this->respondWithStatus(
                        $list, Response::HTTP_OK);
Open Source Developer's avatar
transf    
Open Source Developer committed
78
                }
79
80
81
            } else {
                return $this->respondWithStatus([
                    'message' => t('School not found!!!'),
82
                ], Response::HTTP_OK);
83
            }
Open Source Developer's avatar
transf    
Open Source Developer committed
84
85
        } else {
            return $this->respondWithStatus([
86
                    'message' => t('User not found!'),
Open Source Developer's avatar
transf    
Open Source Developer committed
87
88
89
90
                ], Response::HTTP_FORBIDDEN);
        }
    }

91
    public function getSpecialPerSchool(Request $request, $sectorId)
Open Source Developer's avatar
trans    
Open Source Developer committed
92
93
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
94
95

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
96
97
98
        $user = reset($users);
        if ($user) {
            $epalId = $user->init->value;
Open Source Developer's avatar
Open Source Developer committed
99

100
101
102
103
104
105
106
107
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $epalId));
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');
Open Source Developer's avatar
Open Source Developer committed
108

109
110
                return $response;
            }
111

112
113
114
115
116
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
117
                }
118
119
120
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
121
122
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
123
124
            } elseif ($userRole === 'epal') {
                $specialityPerSchool = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $epalId));
Open Source Developer's avatar
trans    
Open Source Developer committed
125
126
                $i = 0;

127
128
129
130
131
                if ($specialityPerSchool) {
                    $list = array();
                    $SectorIdNew = intval($sectorId);
                    foreach ($specialityPerSchool as $object) {
                        $idSpecial = $object->specialty_id->entity->id();
132

133
134
135
136
137
138
139
                        $specialityPerSector = $this->entityTypeManager->getStorage('eepal_specialty')->loadByProperties(array('id' => $idSpecial, 'sector_id' => $SectorIdNew));
                        $specialPerSec = reset($specialityPerSector);
                        if ($specialPerSec) {
                            $list[] = array(
                                'specialty_id' => $object->specialty_id->entity->get('name')->value,
                                'id' => $object->specialty_id->entity->id(),     );
                            ++$i;
Open Source Developer's avatar
trans    
Open Source Developer committed
140
                        }
141
                    }
142

143
144
                    return $this->respondWithStatus(
                        $list, Response::HTTP_OK);
Open Source Developer's avatar
trans    
Open Source Developer committed
145
                }
146
147
148
            } else {
                return $this->respondWithStatus([
                    'message' => t('School not found!!!'),
149
                ], Response::HTTP_OK);
150
            }
Open Source Developer's avatar
trans    
Open Source Developer committed
151
152
        } else {
            return $this->respondWithStatus([
153
                    'message' => t('User not found!'),
Open Source Developer's avatar
trans    
Open Source Developer committed
154
155
156
157
                ], Response::HTTP_FORBIDDEN);
        }
    }

158
    public function getStudentPerSchool(Request $request, $selectId, $classId, $limitdown, $limitup)
Open Source Developer's avatar
trans    
Open Source Developer committed
159
    {
160
        try {
Open Source Developer's avatar
trans    
Open Source Developer committed
161
        $authToken = $request->headers->get('PHP_AUTH_USER');
162

163
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
164
165
        $user = reset($users);
        if ($user) {
166
167
            $epalId = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $epalId));
168
169
            $school = reset($schools);
            if (!$school) {
170
                $this->logger->warning('no access to this school='.$user->id());
171
172
173
174
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');
175

176
177
178
                return $response;
            }

179
180
181
182
183
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
184
                }
185
186
187
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
188
189
190
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);

191
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
192
                $selectIdNew = $selectId;
193
194
195
196
197
                if ($classId == 1) {
                    $selectIdNew = -1;
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId));
                } else {
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId));
198
199

                }
Open Source Developer's avatar
trans    
Open Source Developer committed
200
201
                $i = 0;

202
203
204
205
206
                if ($studentPerSchool) {
                    $list = array();
                    $i = 0;
                    if ($limitdown == $limitup && $limitup == 0) {
                        $list = array(
Open Source Developer's avatar
Open Source Developer committed
207
                                   'id' => sizeof($studentPerSchool),
208
                                   'up' => $limitup,
209
                                   'down' => $limitdown,
210
                                );
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
                    } else {
                        foreach ($studentPerSchool as $object) {
                            $studentId = $object->id();
                            $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $studentId));
                            $epalStudent = reset($epalStudents);

                            if ($epalStudents) {


                                $studentIdNew = $epalStudent->id();
                                $checkstatus = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('student_id' => $studentIdNew));
                                $checkstudentstatus = reset($checkstatus);
                                if ($i >= $limitdown && $i < $limitup) {
                                    $sectorName = '';
                                    $courseName = '';
                                    if ($epalStudent->currentclass->value === '2') {
                                        $sectors = $this->entityTypeManager->getStorage('epal_student_sector_field')->loadByProperties(array('student_id' => $studentIdNew));
                                        $sector = reset($sectors);
                                        if ($sector)
                                            $sectorName = $this->entityTypeManager->getStorage('eepal_sectors')->load($sector->sectorfield_id->target_id)->name->value;
                                    }
                                    else if ($epalStudent->currentclass->value === '3' || $epalStudent->currentclass->value === '4') {
                                        $courses = $this->entityTypeManager->getStorage('epal_student_course_field')->loadByProperties(array('student_id' => $studentIdNew));
                                        $course = reset($courses);
                                        if ($course)
                                            $courseName = $this->entityTypeManager->getStorage('eepal_specialty')->load($course->coursefield_id->target_id)->name->value;
                                    }
                                    $newstatus = $checkstudentstatus->directorconfirm->value;
                                    $list[] = array(
Open Source Developer's avatar
paging    
Open Source Developer committed
240
                                    'i' => $i,
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
                                    'id' => $epalStudent->id(),
                                    'name' => $epalStudent->name->value,
                                    'studentsurname' => $epalStudent->studentsurname->value,
                                    'fatherfirstname' => $epalStudent->fatherfirstname->value,
                                    'fathersurname' => $epalStudent->fathersurname->value,
                                    'motherfirstname' => $epalStudent->motherfirstname->value,
                                    'mothersurname' => $epalStudent->mothersurname->value,
                                    'guardian_name' => $epalStudent->guardian_name->value,
                                    'guardian_surname' => $epalStudent->guardian_surname->value,
                                    'guardian_fathername' => $epalStudent->guardian_fathername->value,
                                    'guardian_mothername' => $epalStudent->guardian_mothername->value,
                                    'lastschool_schoolname' => $epalStudent->lastschool_schoolname->value,
                                    'lastschool_schoolyear' => $epalStudent->lastschool_schoolyear->value,
                                    'lastschool_class' => $epalStudent->currentclass->value,
                                    'currentclass' =>$epalStudent -> currentclass ->value,
                                    'currentsector' =>$sectorName,
                                    'currentcourse' =>$courseName,
                                    'regionaddress' => $epalStudent->regionaddress->value,
                                    'regiontk' => $epalStudent->regiontk->value,
                                    'regionarea' => $epalStudent->regionarea->value,
                                    'certificatetype' => $epalStudent->certificatetype->value,
                                    'graduation_year' => $epalStudent->graduation_year->value,
                                    'telnum' => $epalStudent->telnum->value,
                                    'relationtostudent' => $epalStudent->relationtostudent->value,
                                    'birthdate' => substr($epalStudent->birthdate->value, 8, 10) . '/' . substr($epalStudent->birthdate->value, 6, 8) . '/' . substr($epalStudent->birthdate->value, 0, 4),
                                    'checkstatus' => $newstatus[0][value],
                                    'created' => date('d/m/Y H:i', $epalStudent -> created ->value),
Open Source Developer's avatar
trans    
Open Source Developer committed
268

Open Source Developer's avatar
paging    
Open Source Developer committed
269
270
                                    );
                                }
271
                                ++$i;
Open Source Developer's avatar
paging    
Open Source Developer committed
272
273
                            }
                        }
Open Source Developer's avatar
Open Source Developer committed
274
                    }
275

276
277
                    return $this->respondWithStatus(
                                $list, Response::HTTP_OK);
Open Source Developer's avatar
trans    
Open Source Developer committed
278
                }
279
280
            } else {
                $list = array();
Open Source Developer's avatar
trans    
Open Source Developer committed
281

282
                return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
283
            }
284
        } else {
Open Source Developer's avatar
trans    
Open Source Developer committed
285
            return $this->respondWithStatus([
286
                    'message' => t('User not found!'),
Open Source Developer's avatar
trans    
Open Source Developer committed
287
288
                ], Response::HTTP_FORBIDDEN);
        }
289
290
291
292
293
294
    } catch (\Exception $e) {
        $this->logger->warning($e->getMessage());
        return $this->respondWithStatus([
                'message' => t('Unexpected Error'),
            ], Response::HTTP_FORBIDDEN);
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
295
    }
Open Source Developer's avatar
transf    
Open Source Developer committed
296

Open Source Developer's avatar
trans    
Open Source Developer committed
297
298
299
    public function ConfirmStudents(Request $request)
    {
        if (!$request->isMethod('POST')) {
300
            return $this->respondWithStatus([
301
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
trans    
Open Source Developer committed
302
303
304
305
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

306
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
307
308
309
310
311
312
313
        $user = reset($users);
        if ($user) {
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
314
                }
315
316
317
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
318
319
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
            } elseif ($userRole === 'epal') {
                $postData = null;

                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    $arr = $postData->students;
                    $type = $postData->type;

                    foreach ($arr as $value) {
                        $valnew = intval($value);
                        $studentForConfirm = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('id' => $valnew));
                        $studentConfirm = reset($studentForConfirm);
                        if ($studentConfirm) {
                            if ($type == 1) {
                                $studentConfirm->set('directorconfirm', true);
                            }
                            if ($type == 2) {
                                $studentConfirm->set('directorconfirm', false);
                            }
                            if ($type == 3) {
                                unset($studentConfirm->{directorconfirm});
                            }
                            $studentConfirm->save();
                        }
344
                    }
345
346
347

                    return $this->respondWithStatus([
                    'message' => t('saved'),
Open Source Developer's avatar
trans    
Open Source Developer committed
348
349
350
                ], Response::HTTP_OK);
                }
            } else {
351
352
353
354
355
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
trans    
Open Source Developer committed
356
            return $this->respondWithStatus([
357
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
trans    
Open Source Developer committed
358
359
360
361
                ], Response::HTTP_FORBIDDEN);
        }
    }

362
    public function SaveCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
Open Source Developer committed
363
364
    {
        if (!$request->isMethod('POST')) {
365
            return $this->respondWithStatus([
366
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
Open Source Developer committed
367
368
369
370
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

371
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
        $user = reset($users);
        if ($user) {
            $schoolid = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
391
                }
392
393
394
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
395
396
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
397
398
            } elseif ($userRole === 'epal') {
                $postData = null;
Open Source Developer's avatar
Open Source Developer committed
399

400
401
402
403
404
405
                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    $cap = $postData->capacity;
                    if ($cap <= 0 || $cap > 99) {
                        return $this->respondWithStatus([
                            'message' => t('Number out of limits!'),
Open Source Developer's avatar
Open Source Developer committed
406
                        ], Response::HTTP_BAD_REQUEST);
407
                    }
Open Source Developer's avatar
Open Source Developer committed
408

409
410
411
412
413
414
                    if (($tomeas == 0) && ($specialit == 0)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_a', $cap);
                            $classcapacity->save();
Open Source Developer's avatar
Open Source Developer committed
415
                        }
416
                    }
Open Source Developer's avatar
Open Source Developer committed
417

418
419
420
421
422
423
                    if (($tomeas != 0) && ($specialit == 0)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'sector_id' => $tomeas));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_sector', $cap);
                            $classcapacity->save();
Open Source Developer's avatar
Open Source Developer committed
424
                        }
425
                    }
Open Source Developer's avatar
Open Source Developer committed
426

427
428
429
430
431
432
                    if (($specialit != 0) && ($taxi == 3)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialit));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_specialty', $cap);
                            $classcapacity->save();
Open Source Developer's avatar
Open Source Developer committed
433
                        }
434
                    }
Open Source Developer's avatar
Open Source Developer committed
435

436
437
438
439
440
441
442
443
                    if (($specialit != 0) && ($taxi == 4)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialit));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_specialty_d', $cap);
                            $classcapacity->save();
                        }
                    }
Open Source Developer's avatar
Open Source Developer committed
444

445
446
                    return $this->respondWithStatus([
                            'message' => t('saved'),
Open Source Developer's avatar
Open Source Developer committed
447
                        ], Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
448
449
                }
            } else {
450
451
452
453
454
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
455
            return $this->respondWithStatus([
456
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
457
458
459
460
                ], Response::HTTP_FORBIDDEN);
        }
    }

461
    public function getSchools(Request $request)
Open Source Developer's avatar
trans    
Open Source Developer committed
462
463
464
465
466
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
467
468
469
470
471
472
473
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
474
                }
475
476
477
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
478
479
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
480
481
482
483
484
485
486
            } elseif ($userRole === 'regioneduadmin') {
                $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('region_edu_admin_id' => $selectionId));
            } elseif ($userRole === 'eduadmin') {
                $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('edu_admin_id' => $selectionId));
            }
            if ($schools) {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
487

488
489
490
491
492
493
                foreach ($schools as $object) {
                    $status = $this->returnstatus($object->id());
                    $list[] = array(
                                    'id' => $object->id(),
                                    'name' => $object->name->value,
                                    'status' => $status,
Open Source Developer's avatar
trans    
Open Source Developer committed
494
495
                                    );

496
                    ++$i;
Open Source Developer's avatar
trans    
Open Source Developer committed
497
498
                }

499
500
501
502
503
                return $this->respondWithStatus(
                                     $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                            'message' => t('Perfecture not found!'),
Open Source Developer's avatar
trans    
Open Source Developer committed
504
505
                        ], Response::HTTP_FORBIDDEN);
            }
506
507
508
509
510
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
511
512
    }

Open Source Developer's avatar
trans    
Open Source Developer committed
513
514
    public function getCoursesPerSchool(Request $request, $schoolid)
    {
515
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans    
Open Source Developer committed
516
517
518

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
519
520
521
522
523
524
525
        if ($user) {
            $newid = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
526
                }
527
528
529
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
530
531
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
532
533
534
535
536
            } elseif ($userRole === 'regioneduadmin') {
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid, 'region_edu_admin_id' => $newid));
            } elseif ($userRole === 'eduadmin') {
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid, 'edu_admin_id' => $newid));
            }
Open Source Developer's avatar
Open Source Developer committed
537

538
539
540
541
542
543
            $SchoolCat = reset($SchoolCats);
            if ($SchoolCat) {
                $list = array();
                $categ = $SchoolCat->metathesis_region->value;
            }
            $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $schoolid));
Open Source Developer's avatar
trans    
Open Source Developer committed
544

545
546
547
548
549
            if ($CourseA) {
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 1, 'category' => $categ));
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
Open Source Developer's avatar
trans    
Open Source Developer committed
550
                }
551
552
553
554
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
                $list = array();
                foreach ($CourseA as $object) {
                    $list[] = array(
Open Source Developer's avatar
trans    
Open Source Developer committed
555
                                    'id' => '1',
Open Source Developer's avatar
trans    
Open Source Developer committed
556
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
trans    
Open Source Developer committed
557
558
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
559
560
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
trans    
Open Source Developer committed
561
                                    );
562
563
                }
            }
564

565
566
567
568
569
570
571
            $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
            if ($CourseB) {
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 2, 'category' => $categ));
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
                }
Open Source Developer's avatar
Open Source Developer committed
572

573
574
575
576
577
578
                foreach ($CourseB as $object) {
                    $sectorid = $object->sector_id->entity->id();
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
                    $list[] = array(
                            'id' => $object->sector_id->entity->id(),
                            'name' => 'Β Λυκείου  '.$object->sector_id->entity->get('name')->value,
Open Source Developer's avatar
trans    
Open Source Developer committed
579
580
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
581
582
                            'classes' => 2,
                            'limitdown' => $limit,
Open Source Developer's avatar
trans    
Open Source Developer committed
583
584
585

                          );
                }
586
587
588
589
590
591
592
593
            }
            $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
            if ($CourseC) {
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
                }
Open Source Developer's avatar
Open Source Developer committed
594

595
596
597
                foreach ($CourseC as $object) {
                    $specialityid = $object->specialty_id->entity->id();
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
Open Source Developer's avatar
trans    
Open Source Developer committed
598

599
600
601
                    $list[] = array(
                            'id' => $object->specialty_id->entity->id(),
                            'name' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
Open Source Developer's avatar
trans    
Open Source Developer committed
602
603
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
604
605
                            'classes' => 3,
                            'limitdown' => $limit,
606

Open Source Developer's avatar
trans    
Open Source Developer committed
607
608
                          );
                }
609
            }
610
611
612
613
614
615
            if ($CourseA || $CourseB || $CourseC) {
                return $this->respondWithStatus(
                                     $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                            'message' => t('Perfecture not found!'),
Open Source Developer's avatar
trans    
Open Source Developer committed
616
617
                        ], Response::HTTP_FORBIDDEN);
            }
618
619
620
621
622
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
623
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
624

625
626
627
628
629
630
631
632
633
    public function returnstatus($id)
    {
        $schoolid = $id;
        $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
        $SchoolCat = reset($SchoolCats);
        if ($SchoolCat) {
            $categ = $SchoolCat->metathesis_region->value;
        }
        $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $schoolid));
Open Source Developer's avatar
trans    
Open Source Developer committed
634

635
636
637
638
639
640
641
        if ($CourseA) {
            $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 1, 'category' => $categ));
            $limitdown = reset($limit_down);
            if ($limitdown) {
                $limit = $limitdown->limit_down->value;
            }
            $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
642

Open Source Developer's avatar
Open Source Developer committed
643
                    //foreach ($CourseA as $object) {
644
                            if (sizeof($studentPerSchool) < $limit) {
645
646
                                return false;
//                                exit;
Open Source Developer's avatar
Open Source Developer committed
647
                            }
648
649
650
651
652
653
654
655
656
        }          //  }

                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
        if ($CourseB) {
            $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 2, 'category' => $categ));
            $limitdown = reset($limit_down);
            if ($limitdown) {
                $limit = $limitdown->limit_down->value;
            }
657

658
659
660
661
662
663
            foreach ($CourseB as $object) {
                $sectorid = $object->sector_id->entity->id();
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
                    exit;
Open Source Developer's avatar
Open Source Developer committed
664
                }
665
666
667
668
669
670
671
672
673
            }
        }
        $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
        if ($CourseC) {
            $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
            $limitdown = reset($limit_down);
            if ($limitdown) {
                $limit = $limitdown->limit_down->value;
            }
Open Source Developer's avatar
Open Source Developer committed
674

675
676
677
            foreach ($CourseC as $object) {
                $specialityid = $object->specialty_id->entity->id();
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
Open Source Developer's avatar
Open Source Developer committed
678

679
680
681
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
                    exit;
Open Source Developer's avatar
Open Source Developer committed
682
                }
683
684
            }
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
685

686
687
688
        return true;
//                exit;
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
689

690
    public function findCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
trans    
Open Source Developer committed
691
    {
692
693
694
        $tomeasnew = intval($tomeas);
        $specialitnew = intval($specialit);
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans    
Open Source Developer committed
695
696

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
        $user = reset($users);
        if ($user) {
            $schoolid = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
trans    
Open Source Developer committed
716
                }
717
718
719
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
720
721
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
722
723
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
trans    
Open Source Developer committed
724

725
726
727
                if (($tomeasnew == 0) && ($specialitnew == 0)) {
                    $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                    $classcapacity = reset($CapacityPerClass);
Open Source Developer's avatar
trans    
Open Source Developer committed
728

729
730
                    if ($classcapacity) {
                        $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
731
                                'taxi' => $taxi,
732
                               'capacity' => $classcapacity->capacity_class_a->value,
Open Source Developer's avatar
Open Source Developer committed
733
                               );
734
735
                    }
                }
Open Source Developer's avatar
trans    
Open Source Developer committed
736

737
738
739
740
741
                if (($tomeasnew != 0) && ($specialitnew == 0)) {
                    $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'sector_id' => $tomeasnew));
                    $classcapacity = reset($CapacityPerClass);
                    if ($classcapacity) {
                        $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
742
743
                                'taxi' => $taxi,
                                'tomeas' => $tomeasnew,
744
745
746
                                'special' => $specialitnew,
                                'capacity' => $classcapacity->capacity_class_sector->value,
                                'sector' => $tomeasnew.'lala'.$specialitnew,
Open Source Developer's avatar
Open Source Developer committed
747
                                );
748
749
                    }
                }
750

751
752
753
754
755
756
                if (($tomeasnew != 0) && ($specialitnew != 0) && ($taxi == 3)) {
                    $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialitnew));
                    $classcapacity = reset($CapacityPerClass);
                    if ($classcapacity) {
                        $list[] = array(
                                'taxi' => $taxi,
Open Source Developer's avatar
Open Source Developer committed
757
                                'tomeas' => $tomeasnew,
758
759
760
761
                                'special' => $specialitnew,
                                'tomeas' => $classcapacity->specialty_id->value,
                                'capacity' => $classcapacity->capacity_class_specialty->value,
                                'specialty' => 'fromspeciality',
Open Source Developer's avatar
Open Source Developer committed
762
                                );
763
764
                    }
                }
Open Source Developer's avatar
trans    
Open Source Developer committed
765

766
767
768
769
770
771
                if (($tomeasnew != 0) && ($specialitnew != 0) && ($taxi == 4)) {
                    $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialitnew));
                    $classcapacity = reset($CapacityPerClass);
                    if ($classcapacity) {
                        $list[] = array(
                                'taxi' => $taxi,
Open Source Developer's avatar
Open Source Developer committed
772
                                'tomeas' => $tomeasnew,
773
774
775
776
                                'special' => $specialitnew,
                                'tomeas' => $classcapacity->specialty_id->value,
                                'capacity' => $classcapacity->capacity_class_specialty_d->value,
                                'specialty' => 'fromspecialityd',
Open Source Developer's avatar
Open Source Developer committed
777
                                );
778
779
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
780

781
782
                return $this->respondWithStatus(
                                             $list, Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
783
            }
784
        } else {
Open Source Developer's avatar
trans    
Open Source Developer committed
785
            return $this->respondWithStatus([
786
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
trans    
Open Source Developer committed
787
788
789
790
                ], Response::HTTP_FORBIDDEN);
        }
    }

791
    public function getSchoolID(Request $request)
Open Source Developer's avatar
trans    
Open Source Developer committed
792
793
794
795
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
796
797
798
799
800
801
        $user = reset($users);
        if ($user) {
            $schid = intval($user->init->value);
            $list = array();
            $list[] = array(
                             'id' => $schid,
Open Source Developer's avatar
trans    
Open Source Developer committed
802
803
                        );

804
805
806
            return $this->respondWithStatus(
                        $list, Response::HTTP_OK);
        } else {
Open Source Developer's avatar
trans    
Open Source Developer committed
807
            return $this->respondWithStatus([
808
                    'message' => t('User not found!'),
Open Source Developer's avatar
trans    
Open Source Developer committed
809
810
811
812
                ], Response::HTTP_FORBIDDEN);
        }
    }

813
    public function gettypeofschool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
814
815
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans    
Open Source Developer committed
816

Open Source Developer's avatar
Open Source Developer committed
817
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
818
819
820
821
        $user = reset($users);
        if ($user) {
            $schid = intval($user->init->value);
            $schools = $this->entityTypeManager->getStorage('eepal_school')->
Open Source Developer's avatar
Open Source Developer committed
822
                 loadByProperties(array('id' => $schid));
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
838
                }
839
840
841
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
842
843
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
844
845
846
847
            } elseif ($userRole === 'epal') {
                $list = array();
                $list[] = array(
                            'type' => $school->operation_shift->value,
Open Source Developer's avatar
Open Source Developer committed
848
                            );
Open Source Developer's avatar
trans    
Open Source Developer committed
849

850
851
852
853
                return $this->respondWithStatus(
                            $list, Response::HTTP_OK);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
854
            return $this->respondWithStatus([
855
                    'message' => t('User not found!'),
Open Source Developer's avatar
Open Source Developer committed
856
857
858
                ], Response::HTTP_FORBIDDEN);
        }
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
859

860
861
    public function getlimitsperCourse(Request $request, $classid)
    {
862
        $authToken = $request->headers->get('PHP_AUTH_USER');
863
864
865

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
        if ($user) {
            $schoolid = $user->init->value;

            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
885
                }
886
887
888
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
889
890
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
891
892
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
893

894
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
895
                $SchoolCat = reset($SchoolCats);
896
897
                if ($SchoolCat) {
                    $categ = $SchoolCat->metathesis_region->value;
898

899
900
901
902
903
904
905
                    $list = array();
                    if ($classid == 1) {
                        $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 1, 'category' => $categ));
                        $limitdown = reset($limit_down);
                        if ($limitdown) {
                            $limit = $limitdown->limit_down->value;
                        }
906

907
                        $list[] = array(
908
909
910
911
912
913
914
915
                                    'id' => '1',
                                    'name' => 'Α Λυκείου',
                                    'categ' => $categ,
                                    'classes' => 1,
                                    'limitdown' => $limit,
                                    );
                    }

916
917
918
919
920
921
                    if ($classid == 2) {
                        $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 2, 'category' => $categ));
                        $limitdown = reset($limit_down);
                        if ($limitdown) {
                            $limit = $limitdown->limit_down->value;
                        }
922

923
                        $list[] = array(
924

925
                            'name' => 'Β Λυκείου ',
926

927
928
929
930
931
932
                            'categ' => $categ,
                            'classes' => 2,
                            'limitdown' => $limit,

                          );
                    }
933

934
935
936
937
938
939
                    if ($classid == 3) {
                        $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
                        $limitdown = reset($limit_down);
                        if ($limitdown) {
                            $limit = $limitdown->limit_down->value;
                        }
940

941
                        $list[] = array(
942

943
944
945
                            'categ' => $categ,
                            'classes' => 3,
                            'limitdown' => $limit,
946

947
948
                          );
                    }
949

950
951
                    return $this->respondWithStatus(
                                     $list, Response::HTTP_OK);
952
                }
953
954
955
            } else {
                return $this->respondWithStatus([
                            'message' => t('Perfecture not found!'),
956
                        ], Response::HTTP_FORBIDDEN);
957
            }
958
959
960
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
961
                        ], Response::HTTP_FORBIDDEN);
962
        }
963
964
    }

Open Source Developer's avatar
Open Source Developer committed
965
966
    public function FindCapacityPerSchool(Request $request)
    {
Open Source Developer's avatar
Open Source Developer committed
967
        $i = 0;
968
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
969
970

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
        $user = reset($users);
        if ($user) {
            $schoolid = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
990
                }
991
992
993
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
994
995
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
996
997
998
999
            } elseif ($userRole === 'epal') {
                $list = array();
                $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                $classcapacity = reset($CapacityPerClass);
Open Source Developer's avatar
Open Source Developer committed
1000

1001
                if ($classcapacity) {
Open Source Developer's avatar
Open Source Developer committed
1002
1003
                    $list[] = array(
                        'class' => 1,
Open Source Developer's avatar
Open Source Developer committed
1004
1005
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
1006
                        'taxi' => 'Ά Λυκείου',
1007
1008
                        'capacity' => $classcapacity->capacity_class_a->value,
                        'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
1009
                       );
1010
1011
1012
1013
1014
1015
1016
                }
                ++$i;
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseB) {
                    foreach ($CourseB as $object) {
                        $sectorid = $object->sector_id->entity->id();
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'sector_id' => $sectorid));
Open Source Developer's avatar
Open Source Developer committed
1017
1018
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
1019
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1020
                                'class' => 2,
1021
                                'newsector' => $object->sector_id->entity->id(),
Open Source Developer's avatar
Open Source Developer committed
1022
                                'newspecialit' => 0,
1023
1024
1025
                                'taxi' => 'Β Λυκείου  '.$object->sector_id->entity->get('name')->value,
                                'capacity' => $classcapacity->capacity_class_sector->value,
                                'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
1026
1027
                                );
                        }
1028
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
1029
                    }
1030
1031
1032
1033
1034
                }
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
1035
1036
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);