DirectorView.php 52.3 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
use Drupal\epal\Crypt;

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

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

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
41 42 43 44
        $user = reset($users);
        if ($user) {
            $epalId = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->
Open Source Developer's avatar
Open Source Developer committed
45
                     loadByProperties(array('id' => $epalId));
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
            $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
61
                }
62 63 64
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
65 66
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
67 68 69 70 71 72 73 74
            } 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
75
                                    );
76
                    }
Open Source Developer's avatar
Open Source Developer committed
77

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

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

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

102 103 104 105 106 107 108 109
            $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
110

111 112
                return $response;
            }
113

114 115 116 117 118
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
119
                }
120 121 122
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
123 124
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
125 126
            } 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
127 128
                $i = 0;

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

135 136 137 138 139 140 141
                        $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
142
                        }
143
                    }
144

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

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

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

178 179 180
                return $response;
            }

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

193
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
194
                $selectIdNew = $selectId;
195 196 197 198 199
                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));
200 201

                }
Open Source Developer's avatar
trans  
Open Source Developer committed
202 203
                $i = 0;

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

                                    $crypt = new Crypt();
                                    try  {
                                      $name_decoded = $crypt->decrypt($epalStudent->name->value);
                                      $studentsurname_decoded = $crypt->decrypt($epalStudent->studentsurname->value);
                                      $fatherfirstname_decoded = $crypt->decrypt($epalStudent->fatherfirstname->value);
                                      $motherfirstname_decoded = $crypt->decrypt($epalStudent->motherfirstname->value);
                                      $regionaddress_decoded = $crypt->decrypt($epalStudent->regionaddress->value);
                                      $regiontk_decoded = $crypt->decrypt($epalStudent->regiontk->value);
                                      $regionarea_decoded = $crypt->decrypt($epalStudent->regionarea->value);
                                      $certificatetype_decoded = $crypt->decrypt($epalStudent->certificatetype->value);
                                      $relationtostudent_decoded = $crypt->decrypt($epalStudent->relationtostudent->value);
                                      $telnum_decoded = $crypt->decrypt($epalStudent->telnum->value);
                                      $guardian_name_decoded = $crypt->decrypt($epalStudent->guardian_name->value);
                                      $guardian_surname_decoded = $crypt->decrypt($epalStudent->guardian_surname->value);
                                      $guardian_fathername_decoded = $crypt->decrypt($epalStudent->guardian_fathername->value);
                                      $guardian_mothername_decoded = $crypt->decrypt($epalStudent->guardian_mothername->value);
                                    }
                                    catch (\Exception $e) {
                                        //print_r($e->getMessage());
                                        unset($crypt);
                                        $this->logger->warning($e->getMessage());
                                        return $this->respondWithStatus([
                                            "message" => t("An unexpected error occured during DECODING data in getStudentPerSchool Method ")
                                             ], Response::HTTP_INTERNAL_SERVER_ERROR);
                                    }
                                    unset($crypt);

269
                                    $list[] = array(
Open Source Developer's avatar
paging  
Open Source Developer committed
270
                                    'i' => $i,
271
                                    'id' => $epalStudent->id(),
272 273 274
                                    'name' => $name_decoded,
                                    'studentsurname' => $studentsurname_decoded,
                                    'fatherfirstname' => $fatherfirstname_decoded,
275
                                    'fathersurname' => $epalStudent->fathersurname->value,
276
                                    'motherfirstname' => $motherfirstname_decoded,
277
                                    'mothersurname' => $epalStudent->mothersurname->value,
278 279 280 281
                                    'guardian_name' =>$guardian_name_decoded,
                                    'guardian_surname' => $guardian_surname_decoded,
                                    'guardian_fathername' =>$guardian_fathername_decoded,
                                    'guardian_mothername' =>$guardian_mothername_decoded,
282 283 284 285 286 287
                                    '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,
288 289 290 291
                                    'regionaddress' =>$regionaddress_decoded,
                                    'regiontk' =>$regiontk_decoded,
                                    'regionarea' =>$regionarea_decoded,
                                    'certificatetype' => $certificatetype_decoded,
292
                                    'graduation_year' => $epalStudent->graduation_year->value,
293 294 295 296
                                    'telnum' =>$telnum_decoded,
                                    'relationtostudent' => $relationtostudent_decoded,
                                    //'birthdate' => substr($epalStudent->birthdate->value, 8, 10) . '/' . substr($epalStudent->birthdate->value, 6, 8) . '/' . substr($epalStudent->birthdate->value, 0, 4),
                                    'birthdate' => date("d-m-Y", strtotime($epalStudent->birthdate->value)),
297 298
                                    'checkstatus' => $newstatus[0][value],
                                    'created' => date('d/m/Y H:i', $epalStudent -> created ->value),
Open Source Developer's avatar
trans  
Open Source Developer committed
299

Open Source Developer's avatar
paging  
Open Source Developer committed
300
                                    );
301

Open Source Developer's avatar
paging  
Open Source Developer committed
302
                                }
303
                                ++$i;
Open Source Developer's avatar
paging  
Open Source Developer committed
304 305
                            }
                        }
Open Source Developer's avatar
Open Source Developer committed
306
                    }
307

308 309
                    return $this->respondWithStatus(
                                $list, Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
310
                }
311 312
            } else {
                $list = array();
Open Source Developer's avatar
trans  
Open Source Developer committed
313

314
                return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
315
            }
316
        } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
317
            return $this->respondWithStatus([
318
                    'message' => t('User not found!'),
Open Source Developer's avatar
trans  
Open Source Developer committed
319 320
                ], Response::HTTP_FORBIDDEN);
        }
321 322 323 324 325 326
    } 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
327
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
328

Open Source Developer's avatar
trans  
Open Source Developer committed
329 330 331
    public function ConfirmStudents(Request $request)
    {
        if (!$request->isMethod('POST')) {
332
            return $this->respondWithStatus([
333
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
trans  
Open Source Developer committed
334 335 336 337
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

338
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
339 340 341 342 343 344 345
        $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
346
                }
347 348 349
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
350 351
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
            } 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();
                        }
376
                    }
377 378 379

                    return $this->respondWithStatus([
                    'message' => t('saved'),
Open Source Developer's avatar
trans  
Open Source Developer committed
380 381 382
                ], Response::HTTP_OK);
                }
            } else {
383 384 385 386 387
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
388
            return $this->respondWithStatus([
389
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
trans  
Open Source Developer committed
390 391 392 393
                ], Response::HTTP_FORBIDDEN);
        }
    }

394
    public function SaveCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
Open Source Developer committed
395 396
    {
        if (!$request->isMethod('POST')) {
397
            return $this->respondWithStatus([
398
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
Open Source Developer committed
399 400 401 402
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

403
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        $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
423
                }
424 425 426
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
427 428
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
429 430
            } elseif ($userRole === 'epal') {
                $postData = null;
Open Source Developer's avatar
Open Source Developer committed
431

432 433 434 435 436 437
                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
438
                        ], Response::HTTP_BAD_REQUEST);
439
                    }
Open Source Developer's avatar
Open Source Developer committed
440

441 442 443 444 445 446
                    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
447
                        }
448
                    }
Open Source Developer's avatar
Open Source Developer committed
449

450 451 452 453 454 455
                    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
456
                        }
457
                    }
Open Source Developer's avatar
Open Source Developer committed
458

459 460 461 462 463 464
                    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
465
                        }
466
                    }
Open Source Developer's avatar
Open Source Developer committed
467

468 469 470 471 472 473 474 475
                    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
476

477 478
                    return $this->respondWithStatus([
                            'message' => t('saved'),
Open Source Developer's avatar
Open Source Developer committed
479
                        ], Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
480 481
                }
            } else {
482 483 484 485 486
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
487
            return $this->respondWithStatus([
488
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
489 490 491 492
                ], Response::HTTP_FORBIDDEN);
        }
    }

493
    public function getSchools(Request $request)
Open Source Developer's avatar
trans  
Open Source Developer committed
494 495 496 497 498
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
499 500 501 502 503 504 505
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
506
                }
507 508 509
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
510 511
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
512 513 514 515 516 517 518
            } 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
519

520 521 522 523 524 525
                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
526 527
                                    );

528
                    ++$i;
Open Source Developer's avatar
trans  
Open Source Developer committed
529 530
                }

531 532 533 534 535
                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
536 537
                        ], Response::HTTP_FORBIDDEN);
            }
538 539 540 541 542
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
543 544
    }

Open Source Developer's avatar
trans  
Open Source Developer committed
545 546
    public function getCoursesPerSchool(Request $request, $schoolid)
    {
547
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
548 549 550

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
551 552 553 554 555 556 557
        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
558
                }
559 560 561
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
562 563
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
564 565 566 567 568
            } 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
569

570 571 572 573 574 575
            $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
576

577 578 579 580 581
            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
582
                }
583 584 585 586
                $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
587
                                    'id' => '1',
Open Source Developer's avatar
trans  
Open Source Developer committed
588
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
trans  
Open Source Developer committed
589 590
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
591 592
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
593
                                    );
594 595
                }
            }
596

597 598 599 600 601 602 603
            $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
604

605 606 607 608 609 610
                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
611 612
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
613 614
                            'classes' => 2,
                            'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
615 616 617

                          );
                }
618 619 620 621 622 623 624 625
            }
            $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
626

627 628 629
                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
630

631 632 633
                    $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
634 635
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
636 637
                            'classes' => 3,
                            'limitdown' => $limit,
638

Open Source Developer's avatar
trans  
Open Source Developer committed
639 640
                          );
                }
641
            }
642 643 644 645 646 647
            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
648 649
                        ], Response::HTTP_FORBIDDEN);
            }
650 651 652 653 654
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
655
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
656

657 658 659 660 661 662 663 664 665
    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
666

667 668 669 670 671 672 673
        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));
674

Open Source Developer's avatar
Open Source Developer committed
675
                    //foreach ($CourseA as $object) {
676
                            if (sizeof($studentPerSchool) < $limit) {
677 678
                                return false;
//                                exit;
Open Source Developer's avatar
Open Source Developer committed
679
                            }
680 681 682 683 684 685 686 687 688
        }          //  }

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

690 691 692 693 694 695
            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
696
                }
697 698 699 700 701 702 703 704 705
            }
        }
        $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
706

707 708 709
            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
710

711 712 713
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
                    exit;
Open Source Developer's avatar
Open Source Developer committed
714
                }
715 716
            }
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
717

718 719 720
        return true;
//                exit;
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
721

722
    public function findCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
trans  
Open Source Developer committed
723
    {
724 725 726
        $tomeasnew = intval($tomeas);
        $specialitnew = intval($specialit);
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
727 728

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
        $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
748
                }
749 750 751
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
752 753
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
754 755
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
trans  
Open Source Developer committed
756

757 758 759
                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
760

761 762
                    if ($classcapacity) {
                        $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
763
                                'taxi' => $taxi,
764
                               'capacity' => $classcapacity->capacity_class_a->value,
Open Source Developer's avatar
Open Source Developer committed
765
                               );
766 767
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
768

769 770 771 772 773
                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
774 775
                                'taxi' => $taxi,
                                'tomeas' => $tomeasnew,
776 777 778
                                'special' => $specialitnew,
                                'capacity' => $classcapacity->capacity_class_sector->value,
                                'sector' => $tomeasnew.'lala'.$specialitnew,
Open Source Developer's avatar
Open Source Developer committed
779
                                );
780 781
                    }
                }
782

783 784 785 786 787 788
                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
789
                                'tomeas' => $tomeasnew,
790 791 792 793
                                'special' => $specialitnew,
                                'tomeas' => $classcapacity->specialty_id->value,
                                'capacity' => $classcapacity->capacity_class_specialty->value,
                                'specialty' => 'fromspeciality',
Open Source Developer's avatar
Open Source Developer committed
794
                                );
795 796
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
797

798 799 800 801 802 803
                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
804
                                'tomeas' => $tomeasnew,
805 806 807 808
                                '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
809
                                );
810 811
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
812

813 814
                return $this->respondWithStatus(
                                             $list, Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
815
            }
816
        } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
817
            return $this->respondWithStatus([
818
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
trans  
Open Source Developer committed
819 820 821 822
                ], Response::HTTP_FORBIDDEN);
        }
    }

823
    public function getSchoolID(Request $request)
Open Source Developer's avatar
trans  
Open Source Developer committed
824 825 826 827
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
828 829 830 831 832 833
        $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
834 835
                        );

836 837 838
            return $this->respondWithStatus(
                        $list, Response::HTTP_OK);
        } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
839
            return $this->respondWithStatus([
840
                    'message' => t('User not found!'),
Open Source Developer's avatar
trans  
Open Source Developer committed
841 842 843 844
                ], Response::HTTP_FORBIDDEN);
        }
    }

845
    public function gettypeofschool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
846 847
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
848

Open Source Developer's avatar
Open Source Developer committed
849
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
850 851 852 853
        $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
854
                 loadByProperties(array('id' => $schid));
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
            $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
870
                }
871 872 873
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
874 875
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
876 877 878 879
            } elseif ($userRole === 'epal') {
                $list = array();
                $list[] = array(
                            'type' => $school->operation_shift->value,
Open Source Developer's avatar
Open Source Developer committed
880
                            );
Open Source Developer's avatar
trans  
Open Source Developer committed
881

882 883 884 885
                return $this->respondWithStatus(
                            $list, Response::HTTP_OK);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
886
            return $this->respondWithStatus([
887
                    'message' => t('User not found!'),
Open Source Developer's avatar
Open Source Developer committed
888 889 890
                ], Response::HTTP_FORBIDDEN);
        }
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
891

892 893
    public function getlimitsperCourse(Request $request, $classid)
    {
894
        $authToken = $request->headers->get('PHP_AUTH_USER');
895 896 897

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
        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
917
                }
918 919 920
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
921 922
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
923 924
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
925

926
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
927
                $SchoolCat = reset($SchoolCats);
928 929
                if ($SchoolCat) {
                    $categ = $SchoolCat->metathesis_region->value;
930

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

939
                        $list[] = array(
940 941 942 943 944 945 946 947
                                    'id' => '1',
                                    'name' => 'Α Λυκείου',
                                    'categ' => $categ,
                                    'classes' => 1,
                                    'limitdown' => $limit,
                                    );
                    }

948 949 950 951 952 953
                    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;
                        }
954

955
                        $list[] = array(
956

957
                            'name' => 'Β Λυκείου ',
958

959 960 961 962 963 964
                            'categ' => $categ,
                            'classes' => 2,
                            'limitdown' => $limit,

                          );
                    }
965

966 967 968 969 970 971
                    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;
                        }
972

973
                        $list[] = array(
974

975 976 977
                            'categ' => $categ,
                            'classes' => 3,
                            'limitdown' => $limit,
978

979 980
                          );
                    }
981

982 983
                    return $this->respondWithStatus(
                                     $list, Response::HTTP_OK);
984
                }
985 986 987
            } else {
                return $this->respondWithStatus([
                            'message' => t('Perfecture not found!'),
988
                        ], Response::HTTP_FORBIDDEN);
989
            }
990 991 992
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
993
                        ], Response::HTTP_FORBIDDEN);
994
        }
995 996
    }

Open Source Developer's avatar
Open Source Developer committed
997 998
    public function FindCapacityPerSchool(Request $request)
    {
Open Source Developer's avatar
Open Source Developer committed
999
        $i = 0;
1000
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
1001 1002

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        $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
1022
                }
1023 1024 1025
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
1026 1027
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
1028 1029 1030 1031
            } 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
1032

1033
                if ($classcapacity) {
Open Source Developer's avatar
Open Source Developer committed
1034 1035
                    $list[] = array(
                        'class' => 1,
Open Source Developer's avatar
Open Source Developer committed
1036 1037
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
1038
                        'taxi' => 'Ά Λυκείου',
1039 1040
                        'capacity' => $classcapacity->capacity_class_a->value,
                        'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
1041
                       );
1042 1043 1044 1045 1046 1047 1048
                }
                ++$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
1049 1050
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
1051
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1052
                                'class' => 2,
1053
                                'newsector' => $object->sector_id->entity->id(),
Open Source Developer's avatar
Open Source Developer committed
1054
                                'newspecialit' => 0,
1055 1056 1057
                                '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
1058 1059
                                );
                        }
1060
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
1061
                    }
1062 1063 1064 1065 1066
                }
                $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
1067 1068
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);