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
117
118
119
120
121
122
123
124
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
                }
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
            } 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
132
133
134
135
136
137
138
139
                if ($specialityPerSchool) {
                    $list = array();
                    $SectorIdNew = intval($sectorId);
                    foreach ($specialityPerSchool as $object) {
                        $idSpecial = $object->specialty_id->entity->id();

                        $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
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
190
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
191
                $selectIdNew = $epalId;
192
193
194
195
196
                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));
197
                }
Open Source Developer's avatar
trans    
Open Source Developer committed
198
199
                $i = 0;

200
201
202
203
204
                if ($studentPerSchool) {
                    $list = array();
                    $i = 0;
                    if ($limitdown == $limitup && $limitup == 0) {
                        $list = array(
Open Source Developer's avatar
Open Source Developer committed
205
                                   'id' => sizeof($studentPerSchool),
206
                                   'up' => $limitup,
207
                                   'down' => $limitdown,
208
                                );
209
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
                    } 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
238
                                    'i' => $i,
239
240
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
                                    '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
266

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

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

280
                return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
281
            }
282
        } else {
Open Source Developer's avatar
trans    
Open Source Developer committed
283
            return $this->respondWithStatus([
284
                    'message' => t('User not found!'),
Open Source Developer's avatar
trans    
Open Source Developer committed
285
286
                ], Response::HTTP_FORBIDDEN);
        }
287
288
289
290
291
292
    } 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
293
    }
Open Source Developer's avatar
transf    
Open Source Developer committed
294

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

304
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
305
306
307
308
309
310
311
        $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
312
                }
313
314
315
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
316
317
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
            } 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();
                        }
342
                    }
343
344
345

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

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

369
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
        $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
389
                }
390
391
392
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
393
394
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
395
396
            } elseif ($userRole === 'epal') {
                $postData = null;
Open Source Developer's avatar
Open Source Developer committed
397

398
399
400
401
402
403
                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
404
                        ], Response::HTTP_BAD_REQUEST);
405
                    }
Open Source Developer's avatar
Open Source Developer committed
406

407
408
409
410
411
412
                    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
413
                        }
414
                    }
Open Source Developer's avatar
Open Source Developer committed
415

416
417
418
419
420
421
                    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
422
                        }
423
                    }
Open Source Developer's avatar
Open Source Developer committed
424

425
426
427
428
429
430
                    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
431
                        }
432
                    }
Open Source Developer's avatar
Open Source Developer committed
433

434
435
436
437
438
439
440
441
                    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
442

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
465
466
467
468
469
470
471
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
472
                }
473
474
475
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
476
477
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
478
479
480
481
482
483
484
            } 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
485

486
487
488
489
490
491
                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
492
493
                                    );

494
                    ++$i;
Open Source Developer's avatar
trans    
Open Source Developer committed
495
496
                }

497
498
499
500
501
                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
502
503
                        ], Response::HTTP_FORBIDDEN);
            }
504
505
506
507
508
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
509
510
    }

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
517
518
519
520
521
522
523
        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
524
                }
525
526
527
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
528
529
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
530
531
532
533
534
            } 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
535

536
537
538
539
540
541
            $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
542

543
544
545
546
547
            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
548
                }
549
550
551
552
                $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
553
                                    'id' => '1',
Open Source Developer's avatar
trans    
Open Source Developer committed
554
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
trans    
Open Source Developer committed
555
556
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
557
558
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
trans    
Open Source Developer committed
559
                                    );
560
561
                }
            }
562

563
564
565
566
567
568
569
            $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
570

571
572
573
574
575
576
                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
577
578
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
579
580
                            'classes' => 2,
                            'limitdown' => $limit,
Open Source Developer's avatar
trans    
Open Source Developer committed
581
582
583

                          );
                }
584
585
586
587
588
589
590
591
            }
            $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
592

593
594
595
                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
596

597
598
599
                    $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
600
601
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
602
603
                            'classes' => 3,
                            'limitdown' => $limit,
604

Open Source Developer's avatar
trans    
Open Source Developer committed
605
606
                          );
                }
607
            }
608
609
610
611
612
613
            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
614
615
                        ], Response::HTTP_FORBIDDEN);
            }
616
617
618
619
620
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
621
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
622

623
624
625
626
627
628
629
630
631
    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
632

633
634
635
636
637
638
639
        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));
640

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

                $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;
            }
655

656
657
658
659
660
661
            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
662
                }
663
664
665
666
667
668
669
670
671
            }
        }
        $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
672

673
674
675
            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
676

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

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
        $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
714
                }
715
716
717
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
718
719
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
720
721
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
trans    
Open Source Developer committed
722

723
724
725
                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
726

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

735
736
737
738
739
                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
740
741
                                'taxi' => $taxi,
                                'tomeas' => $tomeasnew,
742
743
744
                                'special' => $specialitnew,
                                'capacity' => $classcapacity->capacity_class_sector->value,
                                'sector' => $tomeasnew.'lala'.$specialitnew,
Open Source Developer's avatar
Open Source Developer committed
745
                                );
746
747
                    }
                }
748

749
750
751
752
753
754
                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
755
                                'tomeas' => $tomeasnew,
756
757
758
759
                                'special' => $specialitnew,
                                'tomeas' => $classcapacity->specialty_id->value,
                                'capacity' => $classcapacity->capacity_class_specialty->value,
                                'specialty' => 'fromspeciality',
Open Source Developer's avatar
Open Source Developer committed
760
                                );
761
762
                    }
                }
Open Source Developer's avatar
trans    
Open Source Developer committed
763

764
765
766
767
768
769
                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
770
                                'tomeas' => $tomeasnew,
771
772
773
774
                                '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
775
                                );
776
777
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
778

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
794
795
796
797
798
799
        $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
800
801
                        );

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

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

Open Source Developer's avatar
Open Source Developer committed
815
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
816
817
818
819
        $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
820
                 loadByProperties(array('id' => $schid));
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
            $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
836
                }
837
838
839
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
840
841
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
842
843
844
845
            } elseif ($userRole === 'epal') {
                $list = array();
                $list[] = array(
                            'type' => $school->operation_shift->value,
Open Source Developer's avatar
Open Source Developer committed
846
                            );
Open Source Developer's avatar
trans    
Open Source Developer committed
847

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
        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
883
                }
884
885
886
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
887
888
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
889
890
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
891

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

897
898
899
900
901
902
903
                    $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;
                        }
904

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

914
915
916
917
918
919
                    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;
                        }
920

921
                        $list[] = array(
922

923
                            'name' => 'Β Λυκείου ',
924

925
926
927
928
929
930
                            'categ' => $categ,
                            'classes' => 2,
                            'limitdown' => $limit,

                          );
                    }
931

932
933
934
935
936
937
                    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;
                        }
938

939
                        $list[] = array(
940

941
942
943
                            'categ' => $categ,
                            'classes' => 3,
                            'limitdown' => $limit,
944

945
946
                          );
                    }
947

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
        $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
988
                }
989
990
991
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
992
993
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
994
995
996
997
            } 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
998

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