DirectorView.php 55.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 23 24 25
    public function __construct(
        EntityTypeManagerInterface $entityTypeManager,
        LoggerChannelFactoryInterface $loggerChannel
    ) {
    
Open Source Developer's avatar
transf  
Open Source Developer committed
26
        $this->entityTypeManager = $entityTypeManager;
27
        $this->logger = $loggerChannel->get('epal-school');
Open Source Developer's avatar
transf  
Open Source Developer committed
28 29 30 31 32
    }

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

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

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

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

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

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

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

113 114
                return $response;
            }
115

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

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

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

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

Open Source Developer's avatar
Open Source Developer committed
162
    public function getStudentPerSchool(Request $request, $classId, $sector, $specialit)
Open Source Developer's avatar
trans  
Open Source Developer committed
163
    {
164
        try {
Open Source Developer's avatar
Open Source Developer committed
165
        $authToken = $request->headers->get('PHP_AUTH_USER');
166

Open Source Developer's avatar
Open Source Developer committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if ($user) 
        {
            $epalId = $user->init->value;
            $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');

                return $response;
            }

            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole)
            {
                if ($tmpRole === 'epal')
                {
                    $userRole = $tmpRole;
192
                }
Open Source Developer's avatar
Open Source Developer committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            }
            if ($userRole === '') 
            {
                return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
            }
            elseif ($userRole === 'epal')
            {
                    $selectIdNew = $selectId;
                    if ($classId == 1) 
                        $selectIdNew = -1;
                    elseif ($classId == 2)
                        $selectIdNew = $sector;
                    else
                        $selectIdNew = $specialit;
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId));
            }
            $i = 0;
            if ($studentPerSchool)
            {
                $list = array();
                foreach ($studentPerSchool as $object)
                {
                            $studentId = $object->id();
                            $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $studentId));
                            $epalStudent = reset($epalStudents);
                            if ($epalStudents) 
                            {
222 223 224
                                    $studentIdNew = $epalStudent->id();
                                    $checkstatus = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('student_id' => $studentIdNew));
                                    $checkstudentstatus = reset($checkstatus);
Open Source Developer's avatar
Open Source Developer committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                                    $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;
                                    }

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

                                        $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);
Open Source Developer's avatar
Open Source Developer committed
257
                                           
258 259 260 261 262 263 264 265
                                        } catch (\Exception $e) {
                                            $this->logger->warning(__METHOD__ . ' Decrypt error: ' . $e->getMessage());
                                            return $this->respondWithStatus([
                                                "message" => t("An unexpected error occured during DECODING data in getStudentPerSchool Method ")
                                            ], Response::HTTP_INTERNAL_SERVER_ERROR);
                                        }


Open Source Developer's avatar
Open Source Developer committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
                                    $list[] = array(
                                    'id' => $epalStudent->id(),
                                    'name' => $name_decoded,
                                    'studentsurname' => $studentsurname_decoded,
                                    'fatherfirstname' => $fatherfirstname_decoded,
                                    'motherfirstname' => $motherfirstname_decoded,
                                    'guardian_name' => $guardian_name_decoded,
                                    'guardian_surname' => $guardian_surname_decoded,
                                    'guardian_fathername' => $guardian_fathername_decoded,
                                    'guardian_mothername' => $guardian_mothername_decoded,
                                    'lastschool_schoolname' => $epalStudent->lastschool_schoolname->value,
                                    'lastschool_schoolyear' => $epalStudent->lastschool_schoolyear->value,
                                    'lastschool_class' => $epalStudent->lastschool_class->value,
                                    'currentclass' =>$epalStudent -> currentclass ->value,
                                    'currentsector' =>$sectorName,
                                    'currentcourse' =>$courseName,
                                    'regionaddress' => $regionaddress_decoded,
                                    'regiontk' => $regiontk_decoded,
                                    'regionarea' => $regionarea_decoded,
                                    'certificatetype' => $certificatetype_decoded,
                                    'graduation_year' => $epalStudent->graduation_year->value,
                                    '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)),
                                    'checkstatus' => $checkstudentstatus -> directorconfirm ->value,
                                    'created' => date('d/m/Y H:i', $epalStudent -> created ->value),
                                    );
                                }
                                ++$i;
Open Source Developer's avatar
trans  
Open Source Developer committed
296
                }
Open Source Developer's avatar
Open Source Developer committed
297 298 299 300 301 302 303
                return $this->respondWithStatus(
                     $list, Response::HTTP_OK);
            }
            else
            {
               return $this->respondWithStatus([
                    'message' => t('Students not found!'),
Open Source Developer's avatar
trans  
Open Source Developer committed
304
                ], Response::HTTP_FORBIDDEN);
305
            }
Open Source Developer's avatar
Open Source Developer committed
306 307 308
        }
         else 
        {
309
            return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
310 311 312 313 314 315 316 317
                    'message' => t('User not found!'),
                ], Response::HTTP_FORBIDDEN);
        }
    }
    catch (\Exception $e)
    {
        $this->logger->warning($e->getMessage());
        return $this->respondWithStatus([
318 319
                'message' => t('Unexpected Error'),
            ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
320
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
321
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
322

Open Source Developer's avatar
trans  
Open Source Developer committed
323 324 325
    public function ConfirmStudents(Request $request)
    {
        if (!$request->isMethod('POST')) {
326
            return $this->respondWithStatus([
327
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
trans  
Open Source Developer committed
328 329 330 331
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

332
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
333 334 335 336 337 338 339
        $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
340
                }
341 342 343
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
344 345
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
346 347 348 349 350 351 352
            } elseif ($userRole === 'epal') {
                $postData = null;

                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    $arr = $postData->students;
                    $type = $postData->type;
Open Source Developer's avatar
Open Source Developer committed
353 354
                    $valnew = intval($arr);
                    $typen = intval($type);
355 356 357
                        $studentForConfirm = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('id' => $valnew));
                        $studentConfirm = reset($studentForConfirm);
                        if ($studentConfirm) {
Open Source Developer's avatar
Open Source Developer committed
358 359 360 361 362 363 364
                            if ($typen === 1) {
                                $studentConfirm->set('directorconfirm', 1);
                                $studentConfirm->save();
                                return $this->respondWithStatus([
                                 'message' => t('saved')
                            ,
                            ], Response::HTTP_OK);
365
                            }
Open Source Developer's avatar
Open Source Developer committed
366 367 368 369 370 371 372
                            elseif ($typen === 2) {
                                $studentConfirm->set('directorconfirm', 0);
                                $studentConfirm->save();
                                return $this->respondWithStatus([
                            'message' => t('saved')
                            ,
                            ], Response::HTTP_OK);
373
                            }
Open Source Developer's avatar
Open Source Developer committed
374
                            elseif ($typen === 3) {
375
                                unset($studentConfirm->{directorconfirm});
Open Source Developer's avatar
Open Source Developer committed
376 377 378 379 380 381 382 383 384 385 386
                                $studentConfirm->save();
                                return $this->respondWithStatus([
                            'message' => t('saved')
                            ,
                            ], Response::HTTP_OK);
                            }
                            else
                                 {
                            return $this->respondWithStatus([
                            'message' => t('Bad request'),
                            ], Response::HTTP_FORBIDDEN);
387
                            }
Open Source Developer's avatar
Open Source Developer committed
388 389

                            
390
                        }
Open Source Developer's avatar
Open Source Developer committed
391 392 393 394 395 396 397
                        else
                        {
                            return $this->respondWithStatus([
                            'message' => t('Student not found'),
                        ], Response::HTTP_FORBIDDEN);
                        }
                    
398

Open Source Developer's avatar
Open Source Developer committed
399
                    
Open Source Developer's avatar
trans  
Open Source Developer committed
400 401
                }
            } else {
402 403 404 405 406
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
407
            return $this->respondWithStatus([
408
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
trans  
Open Source Developer committed
409 410 411 412
                ], Response::HTTP_FORBIDDEN);
        }
    }

413
    public function SaveCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
Open Source Developer committed
414 415
    {
        if (!$request->isMethod('POST')) {
416
            return $this->respondWithStatus([
417
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
Open Source Developer committed
418 419 420 421
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

422
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        $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
442
                }
443 444 445
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
446 447
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
448 449
            } elseif ($userRole === 'epal') {
                $postData = null;
Open Source Developer's avatar
Open Source Developer committed
450

451 452 453 454 455 456
                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
457
                        ], Response::HTTP_BAD_REQUEST);
458
                    }
Open Source Developer's avatar
Open Source Developer committed
459

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

469 470 471 472 473 474
                    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
475
                        }
476
                    }
Open Source Developer's avatar
Open Source Developer committed
477

478 479 480 481 482 483
                    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
484
                        }
485
                    }
Open Source Developer's avatar
Open Source Developer committed
486

487 488 489 490 491 492 493 494
                    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
495

496 497
                    return $this->respondWithStatus([
                            'message' => t('saved'),
Open Source Developer's avatar
Open Source Developer committed
498
                        ], Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
499 500
                }
            } else {
501 502 503 504 505
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
506
            return $this->respondWithStatus([
507
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
508 509 510 511
                ], Response::HTTP_FORBIDDEN);
        }
    }

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
518 519 520 521 522 523 524
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
525
                }
526 527 528
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
529 530
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
531 532 533 534 535 536 537
            } 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
538

539 540 541 542 543 544
                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
545 546
                                    );

547
                    ++$i;
Open Source Developer's avatar
trans  
Open Source Developer committed
548 549
                }

550 551 552 553 554
                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
555 556
                        ], Response::HTTP_FORBIDDEN);
            }
557 558 559 560 561
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
562 563
    }

Open Source Developer's avatar
trans  
Open Source Developer committed
564 565
    public function getCoursesPerSchool(Request $request, $schoolid)
    {
566
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
567 568 569

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
570 571 572 573 574 575 576
        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
577
                }
578 579 580
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
581 582
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
583 584 585 586 587
            } 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
588

589 590 591 592 593 594
            $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
595

596 597 598 599 600
            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
601
                }
602 603 604 605
                $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
606
                                    'id' => '1',
Open Source Developer's avatar
trans  
Open Source Developer committed
607
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
trans  
Open Source Developer committed
608 609
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
610 611
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
612
                                    );
613 614
                }
            }
615

616 617 618 619 620 621 622
            $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
623

624 625 626 627 628 629
                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
630 631
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
632 633
                            'classes' => 2,
                            'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
634 635 636

                          );
                }
637 638 639 640 641 642 643 644
            }
            $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
645

646 647 648
                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
649

650 651 652
                    $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
653 654
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
655 656
                            'classes' => 3,
                            'limitdown' => $limit,
657

Open Source Developer's avatar
trans  
Open Source Developer committed
658 659
                          );
                }
660
            }
661 662 663 664 665 666
            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
667 668
                        ], Response::HTTP_FORBIDDEN);
            }
669 670 671 672 673
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
674
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
675

676 677 678 679 680 681 682 683 684
    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
685

686 687 688 689 690 691 692
        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));
693

Open Source Developer's avatar
Open Source Developer committed
694
                    //foreach ($CourseA as $object) {
695 696
            if (sizeof($studentPerSchool) < $limit) {
                return false;
697
//                                exit;
698
            }
699 700 701 702 703 704 705 706 707
        }          //  }

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

709 710 711 712 713 714
            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
715
                }
716 717 718 719 720 721 722 723 724
            }
        }
        $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
725

726 727 728
            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
729

730 731 732
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
                    exit;
Open Source Developer's avatar
Open Source Developer committed
733
                }
734 735
            }
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
736

737 738 739
        return true;
//                exit;
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
740

741
    public function findCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
trans  
Open Source Developer committed
742
    {
743 744 745
        $tomeasnew = intval($tomeas);
        $specialitnew = intval($specialit);
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
746 747

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
        $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
767
                }
768 769 770
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
771 772
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
773 774
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
trans  
Open Source Developer committed
775

776 777 778
                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
779

780 781
                    if ($classcapacity) {
                        $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
782
                                'taxi' => $taxi,
783
                               'capacity' => $classcapacity->capacity_class_a->value,
Open Source Developer's avatar
Open Source Developer committed
784
                               );
785 786
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
787

788 789 790 791 792
                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
793 794
                                'taxi' => $taxi,
                                'tomeas' => $tomeasnew,
795 796 797
                                'special' => $specialitnew,
                                'capacity' => $classcapacity->capacity_class_sector->value,
                                'sector' => $tomeasnew.'lala'.$specialitnew,
Open Source Developer's avatar
Open Source Developer committed
798
                                );
799 800
                    }
                }
801

802 803 804 805 806 807
                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
808
                                'tomeas' => $tomeasnew,
809 810 811 812
                                'special' => $specialitnew,
                                'tomeas' => $classcapacity->specialty_id->value,
                                'capacity' => $classcapacity->capacity_class_specialty->value,
                                'specialty' => 'fromspeciality',
Open Source Developer's avatar
Open Source Developer committed
813
                                );
814 815
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
816

817 818 819 820 821 822
                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
823
                                'tomeas' => $tomeasnew,
824 825 826 827
                                '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
828
                                );
829 830
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
831

832 833
                return $this->respondWithStatus(
                                             $list, Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
834
            }
835
        } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
836
            return $this->respondWithStatus([
837
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
trans  
Open Source Developer committed
838 839 840 841
                ], Response::HTTP_FORBIDDEN);
        }
    }

842
    public function getSchoolID(Request $request)
Open Source Developer's avatar
trans  
Open Source Developer committed
843 844 845 846
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
847 848 849 850 851 852
        $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
853 854
                        );

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

864
    public function gettypeofschool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
865 866
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
867

Open Source Developer's avatar
Open Source Developer committed
868
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
869 870 871 872
        $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
873
                 loadByProperties(array('id' => $schid));
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
            $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
889
                }
890 891 892
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
893 894
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
895 896 897 898
            } elseif ($userRole === 'epal') {
                $list = array();
                $list[] = array(
                            'type' => $school->operation_shift->value,
Open Source Developer's avatar
Open Source Developer committed
899
                            );
Open Source Developer's avatar
trans  
Open Source Developer committed
900

901 902 903 904
                return $this->respondWithStatus(
                            $list, Response::HTTP_OK);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
905
            return $this->respondWithStatus([
906
                    'message' => t('User not found!'),
Open Source Developer's avatar
Open Source Developer committed
907 908 909
                ], Response::HTTP_FORBIDDEN);
        }
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
910

911 912
    public function getlimitsperCourse(Request $request, $classid)
    {
913
        $authToken = $request->headers->get('PHP_AUTH_USER');
914 915 916

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
917 918 919 920 921 922 923
        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());
924 925 926
                return $this->respondWithStatus([
                    'message' => "No access to this school"
                ], Response::HTTP_FORBIDDEN);
927 928 929 930 931 932
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
933
                }
934 935 936
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
937 938
                    'error_code' => 4003,
                ], Response::HTTP_FORBIDDEN);
939 940
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
941

942
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
943
                $SchoolCat = reset($SchoolCats);
944 945
                if ($SchoolCat) {
                    $categ = $SchoolCat->metathesis_region->value;
946

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

955
                        $list[] = array(
956 957 958 959 960 961
                            'id' => '1',
                            'name' => 'Α Λυκείου',
                            'categ' => $categ,
                            'classes' => 1,
                            'limitdown' => $limit,
                        );
962 963
                    }

964 965 966 967 968 969
                    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;
                        }
970

971
                        $list[] = array(
972 973 974 975 976 977 978
                            'name' => 'Β Λυκείου ',
                            'categ' => $categ,
                            'classes' => 2,
                            'limitdown' => $limit,

                          );
                    }
979

980 981 982 983 984 985
                    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;
                        }
986

987
                        $list[] = array(
988 989 990
                            'categ' => $categ,
                            'classes' => 3,
                            'limitdown' => $limit,
991

992 993
                          );
                    }
994

995
                    return $this->respondWithStatus($list, Response::HTTP_OK);
996
                }
997 998
            } else {
                return $this->respondWithStatus([
999 1000
                    'message' => t('Perfecture not found!'),
                ], Response::HTTP_FORBIDDEN);
1001
            }
1002 1003
        } else {
            return $this->respondWithStatus([
1004 1005
                'message' => t('User not found!'),
            ], Response::HTTP_FORBIDDEN);
1006
        }
1007 1008
    }

Open Source Developer's avatar
Open Source Developer committed
1009 1010
    public function FindCapacityPerSchool(Request $request)
    {
Open Source Developer's avatar
Open Source Developer committed
1011
        $i = 0;
1012
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
1013 1014

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
        $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;
            }
Open Source Developer's avatar
Open Source Developer committed
1029
            $operation_shift = $school -> operation_shift -> value;
1030 1031 1032 1033 1034
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
1035
                }
1036 1037 1038
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
1039 1040
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
1041
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
1042
                $categ = $school->metathesis_region->value;
1043 1044 1045
                $list = array();
                $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                $classcapacity = reset($CapacityPerClass);
Open Source Developer's avatar
Open Source Developer committed
1046

1047
                if ($classcapacity) {
Open Source Developer's avatar
Open Source Developer committed
1048 1049 1050 1051 1052 1053 1054
                $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));

Open Source Developer's avatar
Open Source Developer committed
1055 1056
                    $list[] = array(
                        'class' => 1,
Open Source Developer's avatar
Open Source Developer committed
1057 1058
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
1059
                        'taxi' => 'Ά Λυκείου',
1060 1061
                        'capacity' => $classcapacity->capacity_class_a->value,
                        'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
1062 1063
                        'limitdown' => $limit,
                        'size' => sizeof($studentPerSchool),
Open Source Developer's avatar
Open Source Developer committed
1064
                       );
1065 1066 1067 1068
                }
                ++$i;
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseB) {
Open Source Developer's avatar
Open Source Developer committed
1069 1070 1071 1072 1073 1074
                    $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;
                    }

1075 1076
                    foreach ($CourseB as $object) {
                        $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
1077
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
1078
                        $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
1079 1080
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
1081
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1082
                                'class' => 2,
1083
                                'newsector' => $object->sector_id->entity->id(),
Open Source Developer's avatar
Open Source Developer committed
1084
                                'newspecialit' => 0,
1085 1086 1087
                                '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
1088 1089
                                'limitdown' => $limit,
                                'size' => sizeof($studentPerSchool),
Open Source Developer's avatar
Open Source Developer committed
1090 1091
                                );
                        }
1092
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
1093
                    }
1094 1095 1096
                }
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
Open Source Developer's avatar
Open Source Developer committed
1097 1098 1099 1100 1101 1102 1103
                    $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;
                        }
                        foreach ($CourseC as $object) {
                        
1104
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
1105
                        $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
1106 1107
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);
1108 1109
                        if ($classcapacity) {
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1110
                                'class' => 3,
Open Source Developer's avatar