DirectorView.php 56.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
    public function __construct(
        EntityTypeManagerInterface $entityTypeManager,
        LoggerChannelFactoryInterface $loggerChannel
    ) {
25

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 {
165

166
            $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
167

168 169 170 171 172 173 174 175 176 177 178
            $epalConfigs = $this->entityTypeManager->getStorage('epal_config')->loadByProperties(array('name' => 'epal_config'));
            $epalConfig = reset($epalConfigs);
            if (!$epalConfig) {
                return $this->respondWithStatus([
                        "error_code" => 3001
                    ], Response::HTTP_FORBIDDEN);
            }
            if ($epalConfig->lock_students->value) {
                return $this->respondWithStatus([
                        "error_code" => 3002
                    ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
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());
                    return $this->respondWithStatus([
                        "message" => "No access to this school"
                    ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
192
                }
193

Open Source Developer's avatar
Open Source Developer committed
194

Open Source Developer's avatar
Open Source Developer committed
195 196 197 198 199 200 201
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole)
            {
                if ($tmpRole === 'epal')
                {
                    $userRole = $tmpRole;
202
                }
Open Source Developer's avatar
Open Source Developer committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            }
            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) 
                            {
232 233 234
                                    $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
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
                                    $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;
                                    }

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

                                        $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
267
                                           
268 269 270 271 272 273 274 275
                                        } 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
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                                    $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
306
                }
Open Source Developer's avatar
Open Source Developer committed
307 308 309 310 311 312 313
                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
314
                ], Response::HTTP_FORBIDDEN);
315
            }
Open Source Developer's avatar
Open Source Developer committed
316 317 318
        }
         else 
        {
319
            return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
320 321 322 323 324 325 326 327
                    'message' => t('User not found!'),
                ], Response::HTTP_FORBIDDEN);
        }
    }
    catch (\Exception $e)
    {
        $this->logger->warning($e->getMessage());
        return $this->respondWithStatus([
328 329
                'message' => t('Unexpected Error'),
            ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
330
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
331
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
332

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

342
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
343 344 345 346 347 348 349
        $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
350
                }
351 352 353
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
354 355
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
356 357 358 359 360 361 362
            } 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
363 364
                    $valnew = intval($arr);
                    $typen = intval($type);
365 366 367
                        $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
368 369 370 371 372 373 374
                            if ($typen === 1) {
                                $studentConfirm->set('directorconfirm', 1);
                                $studentConfirm->save();
                                return $this->respondWithStatus([
                                 'message' => t('saved')
                            ,
                            ], Response::HTTP_OK);
375
                            }
Open Source Developer's avatar
Open Source Developer committed
376 377 378 379 380 381 382
                            elseif ($typen === 2) {
                                $studentConfirm->set('directorconfirm', 0);
                                $studentConfirm->save();
                                return $this->respondWithStatus([
                            'message' => t('saved')
                            ,
                            ], Response::HTTP_OK);
383
                            }
Open Source Developer's avatar
Open Source Developer committed
384
                            elseif ($typen === 3) {
385
                                unset($studentConfirm->{directorconfirm});
Open Source Developer's avatar
Open Source Developer committed
386 387 388 389 390 391 392 393 394 395 396
                                $studentConfirm->save();
                                return $this->respondWithStatus([
                            'message' => t('saved')
                            ,
                            ], Response::HTTP_OK);
                            }
                            else
                                 {
                            return $this->respondWithStatus([
                            'message' => t('Bad request'),
                            ], Response::HTTP_FORBIDDEN);
397
                            }
Open Source Developer's avatar
Open Source Developer committed
398 399

                            
400
                        }
Open Source Developer's avatar
Open Source Developer committed
401 402 403 404 405 406 407
                        else
                        {
                            return $this->respondWithStatus([
                            'message' => t('Student not found'),
                        ], Response::HTTP_FORBIDDEN);
                        }
                    
408

Open Source Developer's avatar
Open Source Developer committed
409
                    
Open Source Developer's avatar
trans  
Open Source Developer committed
410 411
                }
            } else {
412 413 414 415 416
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
417
            return $this->respondWithStatus([
418
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
trans  
Open Source Developer committed
419 420 421 422
                ], Response::HTTP_FORBIDDEN);
        }
    }

423
    public function SaveCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
Open Source Developer committed
424 425
    {
        if (!$request->isMethod('POST')) {
426
            return $this->respondWithStatus([
427
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
Open Source Developer committed
428 429 430 431
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

432 433 434 435 436 437 438 439 440 441 442 443 444
        $epalConfigs = $this->entityTypeManager->getStorage('epal_config')->loadByProperties(array('name' => 'epal_config'));
        $epalConfig = reset($epalConfigs);
        if (!$epalConfig) {
            return $this->respondWithStatus([
                    "error_code" => 3001
                ], Response::HTTP_FORBIDDEN);
        }
        if ($epalConfig->lock_capacity->value) {
            return $this->respondWithStatus([
                    "error_code" => 3002
                ], Response::HTTP_FORBIDDEN);
        }

445
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        $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
465
                }
466 467 468
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
469 470
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
471 472
            } elseif ($userRole === 'epal') {
                $postData = null;
Open Source Developer's avatar
Open Source Developer committed
473

474 475 476 477 478 479
                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
480
                        ], Response::HTTP_BAD_REQUEST);
481
                    }
Open Source Developer's avatar
Open Source Developer committed
482

483 484 485 486 487 488
                    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
489
                        }
490
                    }
Open Source Developer's avatar
Open Source Developer committed
491

492 493 494 495 496 497
                    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
498
                        }
499
                    }
Open Source Developer's avatar
Open Source Developer committed
500

501 502 503 504 505 506
                    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
507
                        }
508
                    }
Open Source Developer's avatar
Open Source Developer committed
509

510 511 512 513 514 515 516 517
                    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
518

519 520
                    return $this->respondWithStatus([
                            'message' => t('saved'),
Open Source Developer's avatar
Open Source Developer committed
521
                        ], Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
522 523
                }
            } else {
524 525 526 527 528
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
529
            return $this->respondWithStatus([
530
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
531 532 533 534
                ], Response::HTTP_FORBIDDEN);
        }
    }

535
    public function getSchools(Request $request)
Open Source Developer's avatar
trans  
Open Source Developer committed
536 537 538 539 540
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
541 542 543 544 545 546 547
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
548
                }
549 550 551
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
552 553
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
554 555 556 557 558 559 560
            } 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
561

562 563 564 565 566 567
                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
568 569
                                    );

570
                    ++$i;
Open Source Developer's avatar
trans  
Open Source Developer committed
571 572
                }

573 574 575 576 577
                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
578 579
                        ], Response::HTTP_FORBIDDEN);
            }
580 581 582 583 584
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
585 586
    }

Open Source Developer's avatar
trans  
Open Source Developer committed
587 588
    public function getCoursesPerSchool(Request $request, $schoolid)
    {
589
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
590 591 592

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
593 594 595 596 597 598 599
        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
600
                }
601 602 603
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
604 605
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
606 607 608 609 610
            } 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
611

612 613 614 615 616 617
            $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
618

619 620 621 622 623
            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
624
                }
625 626 627 628
                $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
629
                                    'id' => '1',
Open Source Developer's avatar
trans  
Open Source Developer committed
630
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
trans  
Open Source Developer committed
631 632
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
633 634
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
635
                                    );
636 637
                }
            }
638

639 640 641 642 643 644 645
            $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
646

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

                          );
                }
660 661 662 663 664 665 666 667
            }
            $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
668

669 670 671
                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
672

673 674 675
                    $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
676 677
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
678 679
                            'classes' => 3,
                            'limitdown' => $limit,
680

Open Source Developer's avatar
trans  
Open Source Developer committed
681 682
                          );
                }
683
            }
684 685 686 687 688 689
            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
690 691
                        ], Response::HTTP_FORBIDDEN);
            }
692 693 694 695 696
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
697
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
698

699 700 701 702 703 704 705 706 707
    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
708

709 710 711 712 713 714 715
        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));
716

Open Source Developer's avatar
Open Source Developer committed
717
                    //foreach ($CourseA as $object) {
718 719
            if (sizeof($studentPerSchool) < $limit) {
                return false;
720
//                                exit;
721
            }
722 723 724 725 726 727 728 729 730
        }          //  }

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

732 733 734 735 736 737
            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
738
                }
739 740 741 742 743 744 745 746 747
            }
        }
        $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
748

749 750 751
            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
752

753 754 755
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
                    exit;
Open Source Developer's avatar
Open Source Developer committed
756
                }
757 758
            }
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
759

760 761 762
        return true;
//                exit;
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
763

764
    public function findCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
trans  
Open Source Developer committed
765
    {
766 767 768
        $tomeasnew = intval($tomeas);
        $specialitnew = intval($specialit);
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
769 770

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
        $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
790
                }
791 792 793
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
794 795
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
796 797
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
trans  
Open Source Developer committed
798

799 800 801
                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
802

803 804
                    if ($classcapacity) {
                        $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
805
                                'taxi' => $taxi,
806
                               'capacity' => $classcapacity->capacity_class_a->value,
Open Source Developer's avatar
Open Source Developer committed
807
                               );
808 809
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
810

811 812 813 814 815
                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
816 817
                                'taxi' => $taxi,
                                'tomeas' => $tomeasnew,
818 819 820
                                'special' => $specialitnew,
                                'capacity' => $classcapacity->capacity_class_sector->value,
                                'sector' => $tomeasnew.'lala'.$specialitnew,
Open Source Developer's avatar
Open Source Developer committed
821
                                );
822 823
                    }
                }
824

825 826 827 828 829 830
                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
831
                                'tomeas' => $tomeasnew,
832 833 834 835
                                'special' => $specialitnew,
                                'tomeas' => $classcapacity->specialty_id->value,
                                'capacity' => $classcapacity->capacity_class_specialty->value,
                                'specialty' => 'fromspeciality',
Open Source Developer's avatar
Open Source Developer committed
836
                                );
837 838
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
839

840 841 842 843 844 845
                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
846
                                'tomeas' => $tomeasnew,
847 848 849 850
                                '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
851
                                );
852 853
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
854

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
870 871 872 873 874 875
        $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
876 877
                        );

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

887
    public function gettypeofschool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
888 889
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
890

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

924 925 926 927
                return $this->respondWithStatus(
                            $list, Response::HTTP_OK);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
928
            return $this->respondWithStatus([
929
                    'message' => t('User not found!'),
Open Source Developer's avatar
Open Source Developer committed
930 931 932
                ], Response::HTTP_FORBIDDEN);
        }
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
933

934 935
    public function getlimitsperCourse(Request $request, $classid)
    {
936
        $authToken = $request->headers->get('PHP_AUTH_USER');
937 938 939

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
940 941 942 943 944 945 946
        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());
947 948 949
                return $this->respondWithStatus([
                    'message' => "No access to this school"
                ], Response::HTTP_FORBIDDEN);
950 951 952 953 954 955
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
956
                }
957 958 959
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
960 961
                    'error_code' => 4003,
                ], Response::HTTP_FORBIDDEN);
962 963
            } elseif ($userRole === 'epal') {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
964

965
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
966
                $SchoolCat = reset($SchoolCats);
967 968
                if ($SchoolCat) {
                    $categ = $SchoolCat->metathesis_region->value;
969

970 971 972 973 974 975 976
                    $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;
                        }
977

978
                        $list[] = array(
979 980 981 982 983 984
                            'id' => '1',
                            'name' => 'Α Λυκείου',
                            'categ' => $categ,
                            'classes' => 1,
                            'limitdown' => $limit,
                        );
985 986
                    }

987 988 989 990 991 992
                    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;
                        }
993

994
                        $list[] = array(
995 996 997 998 999 1000 1001
                            'name' => 'Β Λυκείου ',
                            'categ' => $categ,
                            'classes' => 2,
                            'limitdown' => $limit,

                          );
                    }
1002

1003 1004 1005 1006 1007 1008
                    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;
                        }
1009

1010
                        $list[] = array(
1011 1012 1013
                            'categ' => $categ,
                            'classes' => 3,
                            'limitdown' => $limit,
1014

1015 1016
                          );
                    }
1017

1018
                    return $this->respondWithStatus($list, Response::HTTP_OK);
1019
                }
1020 1021
            } else {
                return $this->respondWithStatus([
1022 1023
                    'message' => t('Perfecture not found!'),
                ], Response::HTTP_FORBIDDEN);
1024
            }
1025 1026
        } else {
            return $this->respondWithStatus([
1027 1028
                'message' => t('User not found!'),
            ], Response::HTTP_FORBIDDEN);
1029
        }
1030 1031
    }

Open Source Developer's avatar
Open Source Developer committed
1032 1033
    public function FindCapacityPerSchool(Request $request)
    {
Open Source Developer's avatar
Open Source Developer committed
1034
        $i = 0;
1035
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
1036 1037

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
        $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
1052
            $operation_shift = $school -> operation_shift -> value;
1053 1054 1055 1056 1057
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
1058
                }
1059 1060 1061
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
1062 1063
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
1064
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
1065
                $categ = $school->metathesis_region->value;
1066 1067 1068
                $list = array();
                $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                $classcapacity = reset($CapacityPerClass);
Open Source Developer's avatar
Open Source Developer committed
1069

1070
                if ($classcapacity) {
Open Source Developer's avatar
Open Source Developer committed
1071 1072 1073 1074 1075 1076 1077
                $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
1078 1079
                    $list[] = array(
                        'class' => 1,
Open Source Developer's avatar
Open Source Developer committed
1080 1081
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
1082
                        'taxi' => 'Ά Λυκείου',
1083 1084
                        'capacity' => $classcapacity->capacity_class_a->value,
                        'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
1085 1086
                        'limitdown' => $limit,
                        'size' => sizeof($studentPerSchool),
Open Source Developer's avatar
Open Source Developer committed
1087
                       );
1088 1089 1090 1091
                }
                ++$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
1092 1093 1094 1095 1096 1097
                    $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;
                    }

1098 1099
                    foreach ($CourseB as $object) {
                        $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
1100
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
1101
                        $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
1102 1103
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
1104
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1105
                                'class' => 2,
1106
                                'newsector' => $object->sector_id->entity->id(),
Open Source Developer's avatar
Open Source Developer committed
1107
                                'newspecialit' => 0,
1108 1109 1110
                                '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
1111 1112
                                'limitdown' => $limit,
                                'size' => sizeof($studentPerSchool),
Open Source Developer's avatar
Open Source Developer committed
1113 1114
                                );
                        }