DirectorView.php 54.4 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);
        }
    }

162
    public function getStudentPerSchool(Request $request, $selectId, $classId, $limitdown, $limitup)
Open Source Developer's avatar
trans  
Open Source Developer committed
163
    {
164
        try {
165 166 167 168 169 170 171 172 173 174 175 176 177
            $authToken = $request->headers->get('PHP_AUTH_USER');

            $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
178
                }
179

180 181 182 183 184 185
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal') {
                        $userRole = $tmpRole;
                    }
186
                }
187 188 189 190 191 192 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 296 297 298 299 300 301
                if ($userRole === '') {
                    return $this->respondWithStatus([
                        'error_code' => 4003,
                    ], Response::HTTP_FORBIDDEN);
                } elseif ($userRole === 'epal') {
                    if ($classId == 1) {
                        $selectId = -1;
                    }
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $epalId, 'specialization_id' => $selectId, 'currentclass' => $classId));

                    if ($studentPerSchool) {
                        $list = array();
                        if ($limitdown == $limitup && $limitup == 0) {
                            $list = array(
                                'id' => sizeof($studentPerSchool),
                                'up' => $limitup,
                                'down' => $limitdown,
                            );
                        } else {
                            $i = 0;
                            foreach ($studentPerSchool as $object) {
                                $studentId = $object->id();
                                $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $studentId));
                                $epalStudent = reset($epalStudents);

                                if ($epalStudent) {
                                    $studentIdNew = $epalStudent->id();
                                    $checkstatus = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('student_id' => $studentIdNew));
                                    $checkstudentstatus = reset($checkstatus);
                                    if ($i >= $limitdown && $i < $limitup) {
                                        $sectorName = '';
                                        $courseName = '';
                                        if ($epalStudent->currentclass->value === '2') {
                                            $sectors = $this->entityTypeManager->getStorage('epal_student_sector_field')->loadByProperties(array('student_id' => $studentIdNew));
                                            $sector = reset($sectors);
                                            if ($sector) {
                                                $sectorName = $this->entityTypeManager->getStorage('eepal_sectors')->load($sector->sectorfield_id->target_id)->name->value;
                                            }
                                        } elseif ($epalStudent->currentclass->value === '3' || $epalStudent->currentclass->value === '4') {
                                            $courses = $this->entityTypeManager->getStorage('epal_student_course_field')->loadByProperties(array('student_id' => $studentIdNew));
                                            $course = reset($courses);
                                            if ($course) {
                                                $courseName = $this->entityTypeManager->getStorage('eepal_specialty')->load($course->coursefield_id->target_id)->name->value;
                                            }
                                        }
                                        $newstatus = $checkstudentstatus->directorconfirm->value;

                                        $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);
                                            // $name_decoded = $epalStudent->name->value;
                                            // $studentsurname_decoded = $epalStudent->studentsurname->value;
                                            // $fatherfirstname_decoded = $epalStudent->fatherfirstname->value;
                                            // $motherfirstname_decoded = $epalStudent->motherfirstname->value;
                                            // $regionaddress_decoded = $epalStudent->regionaddress->value;
                                            // $regiontk_decoded = $epalStudent->regiontk->value;
                                            // $regionarea_decoded = $epalStudent->regionarea->value;
                                            // $certificatetype_decoded = $epalStudent->certificatetype->value;
                                            // $relationtostudent_decoded = $epalStudent->relationtostudent->value;
                                            // $telnum_decoded = $epalStudent->telnum->value;
                                            // $guardian_name_decoded = $epalStudent->guardian_name->value;
                                            // $guardian_surname_decoded = $epalStudent->guardian_surname->value;
                                            // $guardian_fathername_decoded = $epalStudent->guardian_fathername->value;
                                            // $guardian_mothername_decoded = $epalStudent->guardian_mothername->value;
                                        } 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);
                                        }

                                        $list[] = array(
                                            'i' => $i,
                                            'id' => $epalStudent->id(),
                                            'name' => $name_decoded,
                                            'studentsurname' => $studentsurname_decoded,
                                            'fatherfirstname' => $fatherfirstname_decoded,
                                            'fathersurname' => $epalStudent->fathersurname->value,
                                            'motherfirstname' => $motherfirstname_decoded,
                                            'mothersurname' => $epalStudent->mothersurname->value,
                                            '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->currentclass->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' => $newstatus[0][value],
                                            'created' => date('d/m/Y H:i', $epalStudent->created->value)
                                        );
302
                                    }
303
                                    ++$i;
Open Source Developer's avatar
paging  
Open Source Developer committed
304 305 306
                                }
                            }
                        }
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

                        return $this->respondWithStatus(
                            $list, Response::HTTP_OK);
                    } else {
                        if ($limitdown == $limitup && $limitup == 0) {
                            $list = [
                                'id' => sizeof($studentPerSchool),
                                'up' => $limitup,
                                'down' => $limitdown,
                            ];
                        } else {
                            $list = [];
                        }

                        return $this->respondWithStatus($list, Response::HTTP_OK);
                    }
                } else {
                    if ($limitdown == $limitup && $limitup == 0) {
                        $list = [
                            'id' => sizeof($studentPerSchool),
                            'up' => $limitup,
                            'down' => $limitdown,
                        ];
                    } else {
                        $list = [];
Open Source Developer's avatar
Open Source Developer committed
332
                    }
333

334
                    return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
335
                }
336
            } else {
337
                return $this->respondWithStatus([
338
                    'message' => t('User not found!'),
Open Source Developer's avatar
trans  
Open Source Developer committed
339
                ], Response::HTTP_FORBIDDEN);
340 341 342 343
            }
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
            return $this->respondWithStatus([
344 345
                'message' => t('Unexpected Error'),
            ], Response::HTTP_FORBIDDEN);
346
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
347
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
348

Open Source Developer's avatar
trans  
Open Source Developer committed
349 350 351
    public function ConfirmStudents(Request $request)
    {
        if (!$request->isMethod('POST')) {
352
            return $this->respondWithStatus([
353
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
trans  
Open Source Developer committed
354 355 356 357
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                          );
                    }
980

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

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

993 994
                          );
                    }
995

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
        $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
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 1042 1043 1044
            } elseif ($userRole === 'epal') {
                $list = array();
                $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                $classcapacity = reset($CapacityPerClass);
Open Source Developer's avatar
Open Source Developer committed
1045

1046
                if ($classcapacity) {
Open Source Developer's avatar
Open Source Developer committed
1047 1048
                    $list[] = array(
                        'class' => 1,
Open Source Developer's avatar
Open Source Developer committed
1049 1050
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
1051
                        'taxi' => 'Ά Λυκείου',
1052 1053
                        'capacity' => $classcapacity->capacity_class_a->value,
                        'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
1054
                       );
1055 1056 1057 1058 1059 1060 1061
                }
                ++$i;
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseB) {
                    foreach ($CourseB as $object) {
                        $sectorid = $object->sector_id->entity->id();
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'sector_id' => $sectorid));
Open Source Developer's avatar
Open Source Developer committed
1062 1063
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
1064
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1065
                                'class' => 2,
1066
                                'newsector' => $object->sector_id->entity->id(),
Open Source Developer's avatar
Open Source Developer committed
1067
                                'newspecialit' => 0,
1068 1069 1070
                                '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
1071 1072
                                );
                        }
1073
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
1074
                    }
1075 1076 1077 1078 1079
                }
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
1080 1081
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);
1082 1083
                        if ($classcapacity) {
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1084
                                'class' => 3,
Open Source Developer's avatar
Open Source Developer committed
1085
                                    'newsector' => 0,
1086 1087 1088 1089
                                    'newspecialit' => $object->specialty_id->entity->id(),
                                    'taxi' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                                    'capacity' => $classcapacity->capacity_class_specialty->value,
                                    'globalindex' => $i,
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 1097 1098 1099 1100 1101 1102
                if ($CourseC) {
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1103
                                    'class' => 4,
Open Source Developer's avatar
Open Source Developer committed
1104
                                    'newsector' => 0,
1105 1106 1107 1108
                                    'newspecialit' => $object->specialty_id->entity->id(),
                                    'taxi' => 'Δ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                                    'capacity' => $classcapacity->capacity_class_specialty_d->value,
                                    'globalindex' => $i,
1109

Open Source Developer's avatar
Open Source Developer committed
1110 1111
                                    );
                        }
1112 1113 1114
                        ++$i;
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
1115

1116 1117 1118 1119
                return $this->respondWithStatus(
                                     $list, Response::HTTP_OK);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
1120
            return $this->respondWithStatus([
1121
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
1122 1123 1124 1125
                ], Response::HTTP_FORBIDDEN);
        }
    }

1126 1127
    private function respondWithStatus($arr, $s)
    {
Open Source Developer's avatar
transf  
Open Source Developer committed
1128 1129
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
</