DirectorView.php 52.1 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 12
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
Open Source Developer's avatar
transf  
Open Source Developer committed
13 14 15 16

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

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

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



Open Source Developer's avatar
Open Source Developer committed
37
public function getSectorsPerSchool(Request $request)
Open Source Developer's avatar
transf  
Open Source Developer committed
38
    {
39

Open Source Developer's avatar
transf  
Open Source Developer committed
40
        $authToken = $request->headers->get('PHP_AUTH_USER');
41 42 43 44

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
Open Source Developer's avatar
Open Source Developer committed
45
                $epalId = $user ->  init -> value;
Open Source Developer's avatar
Open Source Developer committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
                $schools = $this->entityTypeManager->getStorage('eepal_school')->
                     loadByProperties(array('id' => $epalId));
                     $school = reset($schools);
                     if (!$school) {
                         $this->logger->warning("no access to this school=" . $user->id());
                         $response = new Response();
                         $response->setContent('No access to this school');
                         $response->setStatusCode(Response::HTTP_FORBIDDEN);
                         $response->headers->set('Content-Type', 'application/json');
                         return $response;
                     }
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
transf  
Open Source Developer committed
68
                }
69

Open Source Developer's avatar
Open Source Developer committed
70 71 72 73 74 75 76 77 78 79 80 81
                else if ($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
transf  
Open Source Developer committed
82 83 84 85
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
Open Source Developer's avatar
Open Source Developer committed
86
            }
Open Source Developer's avatar
transf  
Open Source Developer committed
87 88 89
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
90
                ], Response::HTTP_OK);
Open Source Developer's avatar
transf  
Open Source Developer committed
91
                }
92

Open Source Developer's avatar
transf  
Open Source Developer committed
93 94 95 96 97 98 99 100


        } else {

            return $this->respondWithStatus([
                    'message' => t("User not found!"),
                ], Response::HTTP_FORBIDDEN);
        }
101

Open Source Developer's avatar
transf  
Open Source Developer committed
102 103
    }

Open Source Developer's avatar
Open Source Developer committed
104
public function getSpecialPerSchool(Request $request , $sectorId)
Open Source Developer's avatar
trans  
Open Source Developer committed
105
    {
106

Open Source Developer's avatar
trans  
Open Source Developer committed
107
        $authToken = $request->headers->get('PHP_AUTH_USER');
108 109 110 111

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

            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array( 'id' => $epalId));
115 116 117 118 119 120 121 122 123
             $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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }

                else if ($userRole === 'epal')
                 {

141

Open Source Developer's avatar
Open Source Developer committed
142
                $specialityPerSchool = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id'=> $epalId));
Open Source Developer's avatar
trans  
Open Source Developer committed
143 144 145 146 147 148 149 150 151 152
                $i = 0;

            if ($specialityPerSchool) {
                 $list = array();
                 $SectorIdNew = intval($sectorId);
                foreach ($specialityPerSchool as $object) {
                       $idSpecial =  $object -> specialty_id -> entity -> id() ;

                       $specialityPerSector = $this->entityTypeManager->getStorage('eepal_specialty')->loadByProperties(array('id'=> $idSpecial,'sector_id' => $SectorIdNew ));
                       $specialPerSec = reset($specialityPerSector);
153
                       if ($specialPerSec)
Open Source Developer's avatar
trans  
Open Source Developer committed
154 155 156 157 158
                        {       $list[] = array(
                                'specialty_id' => $object -> specialty_id ->entity->get('name')->value ,
                                'id' => $object -> specialty_id -> entity -> id()     );
                                $i++;
                        }
159

Open Source Developer's avatar
trans  
Open Source Developer committed
160
                }
161

Open Source Developer's avatar
trans  
Open Source Developer committed
162 163 164 165
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
Open Source Developer's avatar
Open Source Developer committed
166
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
167 168 169
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
170
                ], Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
171
                }
172

Open Source Developer's avatar
trans  
Open Source Developer committed
173

Open Source Developer's avatar
transf  
Open Source Developer committed
174

Open Source Developer's avatar
trans  
Open Source Developer committed
175 176 177 178 179 180
        } else {

            return $this->respondWithStatus([
                    'message' => t("User not found!"),
                ], Response::HTTP_FORBIDDEN);
        }
181

Open Source Developer's avatar
trans  
Open Source Developer committed
182 183 184
    }


Open Source Developer's avatar
Open Source Developer committed
185
public function getStudentPerSchool(Request $request , $selectId, $classId, $limitdown, $limitup)
Open Source Developer's avatar
trans  
Open Source Developer committed
186
    {
187

Open Source Developer's avatar
trans  
Open Source Developer committed
188
        $authToken = $request->headers->get('PHP_AUTH_USER');
189 190 191 192

       $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if ($user) {
Open Source Developer's avatar
Open Source Developer committed
193 194
            $epalId = $user ->  init -> value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array( 'id' => $epalId));
195 196 197 198 199 200 201 202 203 204
            $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
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }

                else if ($userRole === 'epal')
                 {
221 222


Open Source Developer's avatar
Open Source Developer committed
223
                $selectIdNew = $epalId;
Open Source Developer's avatar
trans  
Open Source Developer committed
224 225 226
                if ($classId == 1)
                {
                 $selectIdNew = -1;
Open Source Developer's avatar
Open Source Developer committed
227
                 $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
228

Open Source Developer's avatar
trans  
Open Source Developer committed
229 230
                }
                else
231
                {
Open Source Developer's avatar
Open Source Developer committed
232
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
233
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
234 235 236 237
                $i = 0;

            if ($studentPerSchool) {
                 $list = array();
Open Source Developer's avatar
paging  
Open Source Developer committed
238 239 240 241
                 $i = 0;
                 if  ($limitdown==$limitup && $limitup == 0)
                     {
                            $list=array(
Open Source Developer's avatar
Open Source Developer committed
242
                                   'id' => sizeof($studentPerSchool),
243
                                   'up' => $limitup,
Open Source Developer's avatar
Open Source Developer committed
244
                                   'down' => $limitdown
245
                                );
Open Source Developer's avatar
paging  
Open Source Developer committed
246 247
                     }

248
                  else
Open Source Developer's avatar
paging  
Open Source Developer committed
249 250 251 252 253 254
                  {
                         foreach ($studentPerSchool as $object)
                                {
                                $studentId = $object -> id() ;
                                $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $studentId));
                                $epalStudent = reset($epalStudents);
255

Open Source Developer's avatar
paging  
Open Source Developer committed
256
                                if ($epalStudents) {
Open Source Developer's avatar
trans  
Open Source Developer committed
257 258 259
                                    $studentIdNew = $epalStudent -> id();
                                    $checkstatus = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array( 'student_id'=> $studentIdNew));
                                    $checkstudentstatus = reset($checkstatus);
Open Source Developer's avatar
paging  
Open Source Developer committed
260
                                   if ($i >= $limitdown && $i < $limitup)
261
                                   {
262
                                    $newstatus = $checkstudentstatus -> directorconfirm-> value;
263

Open Source Developer's avatar
paging  
Open Source Developer committed
264 265 266 267 268 269 270 271 272
                                   $list[] = array(
                                    'i' => $i,
                                    'id' => $epalStudent -> id(),
                                    'name' => $epalStudent -> name ->value,
                                    'studentsurname' => $epalStudent -> studentsurname ->value,
                                    'fatherfirstname' => $epalStudent -> fatherfirstname ->value,
                                    'fathersurname' =>$epalStudent -> fathersurname ->value,
                                    'motherfirstname' => $epalStudent -> motherfirstname ->value,
                                    'mothersurname' =>$epalStudent -> mothersurname ->value,
273 274 275 276 277 278 279
                                    'guardian_name' =>$epalStudent -> guardian_name ->value,
                                    'guardian_surname' =>$epalStudent -> guardian_surname ->value,
                                    'guardian_fathername' =>$epalStudent -> guardian_fathername ->value,
                                    'guardian_mothername' =>$epalStudent -> guardian_mothername ->value,
                                    'lastschool_schoolname' =>$epalStudent -> lastschool_schoolname ->value,
                                    'lastschool_schoolyear' =>$epalStudent -> lastschool_schoolyear ->value,
                                    'lastschool_class' =>$epalStudent -> currentclass ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
280 281 282 283
                                    'regionaddress' =>$epalStudent -> regionaddress ->value,
                                    'regiontk' =>$epalStudent -> regiontk ->value,
                                    'regionarea' =>$epalStudent -> regionarea ->value,
                                    'certificatetype' =>$epalStudent -> certificatetype ->value,
284
                                    'graduation_year' =>$epalStudent -> graduation_year ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
285 286
                                    'telnum' =>$epalStudent -> telnum ->value,
                                    'relationtostudent' =>$epalStudent -> relationtostudent ->value,
Open Source Developer's avatar
paging  
Open Source Developer committed
287
                                    'birthdate' =>$epalStudent -> birthdate ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
288 289
                                    'checkstatus' =>$newstatus[0][value],

Open Source Developer's avatar
paging  
Open Source Developer committed
290 291 292 293 294 295 296 297 298
                                    );
                                   }
                                   $i++;
                                }
                            }
                        }
                        return $this->respondWithStatus(
                                $list
                            , Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
299
                        }
Open Source Developer's avatar
Open Source Developer committed
300
                    }
301

Open Source Developer's avatar
trans  
Open Source Developer committed
302
             else {
303 304
                 $list = array();
                       return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
305 306
                }

Open Source Developer's avatar
Open Source Developer committed
307
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
308

309

Open Source Developer's avatar
trans  
Open Source Developer committed
310 311 312 313 314 315
        else {

            return $this->respondWithStatus([
                    'message' => t("User not found!"),
                ], Response::HTTP_FORBIDDEN);
        }
316

Open Source Developer's avatar
trans  
Open Source Developer committed
317
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
318 319 320



Open Source Developer's avatar
trans  
Open Source Developer committed
321 322 323 324
    public function ConfirmStudents(Request $request)
    {

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

331 332 333
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
334
//
Open Source Developer's avatar
Open Source Developer committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
                 $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }

                else if ($userRole === 'epal')
                 {
350

Open Source Developer's avatar
trans  
Open Source Developer committed
351
            $postData = null;
Open Source Developer's avatar
trans  
Open Source Developer committed
352

Open Source Developer's avatar
trans  
Open Source Developer committed
353
            if ($content = $request->getContent())
Open Source Developer's avatar
trans  
Open Source Developer committed
354 355 356
            {
                 $postData = json_decode($content);
                 $arr = $postData->students;
Open Source Developer's avatar
trans  
Open Source Developer committed
357
                 $type = $postData ->type;
Open Source Developer's avatar
trans  
Open Source Developer committed
358 359

                foreach ($arr as $value) {
360
                    $valnew = intval($value);
Open Source Developer's avatar
trans  
Open Source Developer committed
361 362 363
                 $studentForConfirm = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('id' => $valnew ));
                    $studentConfirm = reset($studentForConfirm);
                  if ($studentConfirm) {
Open Source Developer's avatar
trans  
Open Source Developer committed
364
                    if ($type == 1)
Open Source Developer's avatar
trans  
Open Source Developer committed
365
                         $studentConfirm->set('directorconfirm', true);
Open Source Developer's avatar
trans  
Open Source Developer committed
366
                     if ($type == 2)
Open Source Developer's avatar
trans  
Open Source Developer committed
367
                        $studentConfirm->set('directorconfirm', false);
Open Source Developer's avatar
trans  
Open Source Developer committed
368 369
                    if ($type == 3)
                        unset($studentConfirm->{directorconfirm});
Open Source Developer's avatar
trans  
Open Source Developer committed
370
                         $studentConfirm->save();
371 372
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
373 374 375
                return $this->respondWithStatus([
                    'message' => t("saved"),
                ], Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
376
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
            }
             else
              {
                  return $this->respondWithStatus([
                    'message' => t("post with no data"),
                ], Response::HTTP_BAD_REQUEST);
                }

            } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
    }




Open Source Developer's avatar
Open Source Developer committed
395 396


Open Source Developer's avatar
Open Source Developer committed
397
public function SaveCapacity(Request $request,$taxi,$tomeas,$specialit)
Open Source Developer's avatar
Open Source Developer committed
398 399
    {

Open Source Developer's avatar
trans  
Open Source Developer committed
400 401


Open Source Developer's avatar
Open Source Developer committed
402
        if (!$request->isMethod('POST')) {
403
            return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
404 405 406 407 408
                    "message" => t("Method Not Allowed")
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

409 410 411
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
Open Source Developer's avatar
Open Source Developer committed
412
             $schoolid = $user ->  init -> value;
Open Source Developer's avatar
trans  
Open Source Developer committed
413
             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid));
414 415 416 417 418 419 420 421 422
             $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
423 424 425 426 427 428 429 430 431 432 433 434
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }
Open Source Developer's avatar
Open Source Developer committed
435

Open Source Developer's avatar
Open Source Developer committed
436
                else if ($userRole === 'epal')
Open Source Developer's avatar
Open Source Developer committed
437 438
                 {

Open Source Developer's avatar
Open Source Developer committed
439

Open Source Developer's avatar
Open Source Developer committed
440
                    $postData = null;
Open Source Developer's avatar
Open Source Developer committed
441

Open Source Developer's avatar
Open Source Developer committed
442 443 444 445 446 447 448 449 450 451
                    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!"),
                        ], Response::HTTP_BAD_REQUEST);
                         }
Open Source Developer's avatar
Open Source Developer committed
452

Open Source Developer's avatar
Open Source Developer committed
453 454 455 456 457 458 459 460 461
                        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
462 463


Open Source Developer's avatar
Open Source Developer committed
464 465 466 467 468 469 470 471 472
                        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
473 474


Open Source Developer's avatar
Open Source Developer committed
475 476 477 478 479 480 481 482 483
                        if (($specialit != 0) && ($taxi == 3))
                        {
                         $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialit));
                         $classcapacity = reset($CapacityPerClass);
                          if ($classcapacity) {
                                 $classcapacity->set('capacity_class_specialty', $cap);
                                 $classcapacity->save();
                            }
                        }
Open Source Developer's avatar
Open Source Developer committed
484 485


Open Source Developer's avatar
Open Source Developer committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
                        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();
                            }
                        }



                        return $this->respondWithStatus([
                            'message' => t("saved"),
                        ], Response::HTTP_OK);
                        }
Open Source Developer's avatar
Open Source Developer committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
            }
             else
              {
                  return $this->respondWithStatus([
                    'message' => t("post with no data"),
                ], Response::HTTP_BAD_REQUEST);
                }

            } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
    }


518
    public function getSchools(Request $request)
Open Source Developer's avatar
trans  
Open Source Developer committed
519 520 521 522 523 524 525 526
    {

        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if ($user)
            {
Open Source Developer's avatar
Open Source Developer committed
527
                $selectionId = $user ->  init -> value;
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }

                else if ($userRole === 'regioneduadmin') {
                    $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('region_edu_admin_id'=> $selectionId ));
                }
                else if ($userRole === 'eduadmin') {
Open Source Developer's avatar
Open Source Developer committed
545
                    $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('edu_admin_id'=> $selectionId ));
546
                }
547
                if ($schools)
Open Source Developer's avatar
trans  
Open Source Developer committed
548 549
                {
                    $list = array();
Open Source Developer's avatar
Open Source Developer committed
550

Open Source Developer's avatar
trans  
Open Source Developer committed
551
                    foreach ($schools as $object) {
552
                             $status = $this->returnstatus($object -> id());
Open Source Developer's avatar
trans  
Open Source Developer committed
553
                             $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
554 555
                                    'id' =>$object -> id(),
                                    'name' => $object -> name ->value,
Open Source Developer's avatar
Open Source Developer committed
556
                                    'status' => $status
Open Source Developer's avatar
trans  
Open Source Developer committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
                                    );

                                 $i++;
                            }
                            return $this->respondWithStatus(
                                     $list
                                   , Response::HTTP_OK);
                }
                else
                {
                       return $this->respondWithStatus([
                            'message' => t("Perfecture not found!"),
                        ], Response::HTTP_FORBIDDEN);

                }
572
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
573 574 575 576 577 578 579 580 581 582 583
            else
            {

                   return $this->respondWithStatus([
                            'message' => t("User not found!"),
                        ], Response::HTTP_FORBIDDEN);
            }

    }


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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if ($user)
591
            {
Open Source Developer's avatar
Open Source Developer committed
592 593 594 595 596 597 598 599 600 601 602 603 604
                $newid = $user ->  init -> value;
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if (($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }
Open Source Developer's avatar
Open Source Developer committed
605

Open Source Developer's avatar
Open Source Developer committed
606 607 608 609 610 611
                else if ($userRole === 'regioneduadmin') {
                    $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid, 'region_edu_admin_id' => $newid));
                }
                else if ($userRole === 'eduadmin') {
                    $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid , 'edu_admin_id' => $newid));
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
612

Open Source Developer's avatar
trans  
Open Source Developer committed
613 614
                $SchoolCat = reset($SchoolCats);
                if ($SchoolCat){
Open Source Developer's avatar
Open Source Developer committed
615
                $list= array();
Open Source Developer's avatar
trans  
Open Source Developer committed
616 617 618
                $categ = $SchoolCat-> metathesis_region -> value;
                }
                $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $schoolid ));
619 620

                if ($CourseA)
Open Source Developer's avatar
trans  
Open Source Developer committed
621
                {
Open Source Developer's avatar
Open Source Developer committed
622 623 624 625 626 627
                    $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
628
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal'=> $schoolid, 'specialization_id' => -1, 'currentclass' => 1 ));
Open Source Developer's avatar
trans  
Open Source Developer committed
629 630 631
                    $list = array();
                    foreach ($CourseA as $object) {
                             $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
632
                                    'id' => '1',
Open Source Developer's avatar
trans  
Open Source Developer committed
633
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
trans  
Open Source Developer committed
634 635
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
636 637
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
638
                                    );
639

Open Source Developer's avatar
trans  
Open Source Developer committed
640 641
                }            }

642

Open Source Developer's avatar
trans  
Open Source Developer committed
643 644 645
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid ));
                if ($CourseB)
                {
Open Source Developer's avatar
Open Source Developer committed
646 647 648 649 650 651 652
                    $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
trans  
Open Source Developer committed
653
                    foreach ($CourseB as $object) {
Open Source Developer's avatar
trans  
Open Source Developer committed
654 655
                    $sectorid = $object -> sector_id -> entity -> id();
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('currentepal'=> $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2 ));
Open Source Developer's avatar
trans  
Open Source Developer committed
656
                         $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
657
                            'id' => $object -> sector_id -> entity -> id(),
Open Source Developer's avatar
trans  
Open Source Developer committed
658
                            'name' => 'Β Λυκείου  '.$object -> sector_id -> entity-> get('name')->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
659 660
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
661 662
                            'classes' => 2,
                            'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
663 664 665 666 667 668 669

                          );
                    }
                }
              $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid ));
                if ($CourseC)
                {
Open Source Developer's avatar
Open Source Developer committed
670 671 672 673 674 675 676
                    $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
trans  
Open Source Developer committed
677
                    foreach ($CourseC as $object) {
Open Source Developer's avatar
trans  
Open Source Developer committed
678 679 680
                    $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
681
                         $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
682
                            'id'=> $object -> specialty_id -> entity -> id(),
Open Source Developer's avatar
trans  
Open Source Developer committed
683
                            'name' => 'Γ Λυκείου  '.$object -> specialty_id -> entity-> get('name')->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
684 685
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
686 687
                            'classes' => 3,
                            'limitdown' => $limit,
688

Open Source Developer's avatar
trans  
Open Source Developer committed
689 690 691
                          );
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
692 693
                if ($CourseA || $CourseB || $CourseC)
                {
694

Open Source Developer's avatar
trans  
Open Source Developer committed
695 696 697 698 699 700 701 702 703 704 705
                            return $this->respondWithStatus(
                                     $list
                                   , Response::HTTP_OK);
                }
                else
                {
                       return $this->respondWithStatus([
                            'message' => t("Perfecture not found!"),
                        ], Response::HTTP_FORBIDDEN);

                }
706
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
707 708 709 710 711 712 713 714
            else
            {

                   return $this->respondWithStatus([
                            'message' => t("User not found!"),
                        ], Response::HTTP_FORBIDDEN);
            }
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
715

Open Source Developer's avatar
trans  
Open Source Developer committed
716

Open Source Developer's avatar
Open Source Developer committed
717 718
public function returnstatus($id)
{
Open Source Developer's avatar
Open Source Developer committed
719
                $schoolid = $id;
720
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid ));
Open Source Developer's avatar
Open Source Developer committed
721 722 723 724 725
                $SchoolCat = reset($SchoolCats);
                if ($SchoolCat){
                $categ = $SchoolCat-> metathesis_region -> value;
                }
                $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $schoolid ));
726

727
                if ($CourseA)
Open Source Developer's avatar
Open Source Developer committed
728 729 730 731 732 733 734 735
                {
                    $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 ));
736

Open Source Developer's avatar
Open Source Developer committed
737 738
                    //foreach ($CourseA as $object) {
                            if (sizeof($studentPerSchool) < $limit){
739 740
                                return false;
//                                exit;
Open Source Developer's avatar
Open Source Developer committed
741
                            }
742

Open Source Developer's avatar
Open Source Developer committed
743 744
                }          //  }

745

Open Source Developer's avatar
Open Source Developer committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759
                $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;
                    }

                    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){
760 761
                                return false;
                                exit;
Open Source Developer's avatar
Open Source Developer committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
                            }
                    }
                }
                $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;
                    }

                    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 ));

                         if (sizeof($studentPerSchool) < $limit){
                                return false;
781 782
                                exit;
                                }
Open Source Developer's avatar
Open Source Developer committed
783 784 785
                    }
                }
                return true;
786
//                exit;
Open Source Developer's avatar
Open Source Developer committed
787
}
Open Source Developer's avatar
trans  
Open Source Developer committed
788 789


Open Source Developer's avatar
trans  
Open Source Developer committed
790

Open Source Developer's avatar
Open Source Developer committed
791
public function findCapacity(Request $request,$taxi,$tomeas,$specialit)
Open Source Developer's avatar
trans  
Open Source Developer committed
792 793 794
    {

    $tomeasnew = intval($tomeas);
Open Source Developer's avatar
trans  
Open Source Developer committed
795
    $specialitnew = intval($specialit) ;
Open Source Developer's avatar
trans  
Open Source Developer committed
796 797 798 799 800
       $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
Open Source Developer's avatar
Open Source Developer committed
801
            $schoolid = $user ->  init -> value;
Open Source Developer's avatar
trans  
Open Source Developer committed
802
           $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid));
Open Source Developer's avatar
trans  
Open Source Developer committed
803 804 805 806 807 808 809 810 811
             $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
812 813 814 815 816
              $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
Open Source Developer's avatar
trans  
Open Source Developer committed
817 818
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
819 820 821 822 823
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
824

Open Source Developer's avatar
Open Source Developer committed
825 826
                else if ($userRole === 'epal')
                 {
Open Source Developer's avatar
trans  
Open Source Developer committed
827

Open Source Developer's avatar
Open Source Developer committed
828
                      $list = array();
Open Source Developer's avatar
trans  
Open Source Developer committed
829

Open Source Developer's avatar
Open Source Developer committed
830 831 832 833
                        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
834 835


Open Source Developer's avatar
Open Source Developer committed
836
                          if ($classcapacity) {
Open Source Developer's avatar
trans  
Open Source Developer committed
837 838


Open Source Developer's avatar
Open Source Developer committed
839 840 841 842 843 844
                            $list[] = array(
                                'taxi' => $taxi,
                               'capacity' => $classcapacity -> capacity_class_a -> value ,
                               );
                            }
                        }
Open Source Developer's avatar
trans  
Open Source Developer committed
845 846


Open Source Developer's avatar
Open Source Developer committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860
                        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(
                                'taxi' => $taxi,
                                'tomeas' => $tomeasnew,
                                'special' =>$specialitnew,
                                'capacity' => $classcapacity -> capacity_class_sector -> value ,
                                'sector' =>$tomeasnew."lala".$specialitnew
                                );
                            }
                        }
Open Source Developer's avatar
trans  
Open Source Developer committed
861

862

Open Source Developer's avatar
Open Source Developer committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
                        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,
                                'tomeas' => $tomeasnew,
                                'special' =>$specialitnew,
                                'tomeas' =>  $classcapacity ->  specialty_id -> value,
                                'capacity' => $classcapacity -> capacity_class_specialty -> value ,
                                'specialty' =>"fromspeciality"
                                );
                            }
                        }
Open Source Developer's avatar
trans  
Open Source Developer committed
878

Open Source Developer's avatar
Open Source Developer committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
                        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,
                                'tomeas' => $tomeasnew,
                                'special' =>$specialitnew,
                                'tomeas' =>  $classcapacity ->  specialty_id -> value,
                                'capacity' => $classcapacity -> capacity_class_specialty_d -> value ,
                                'specialty' =>"fromspecialityd"
                                );
                            }
                        }

                             return $this->respondWithStatus(
                                             $list
                                           , Response::HTTP_OK);

                            }
            }
             else {
Open Source Developer's avatar
trans  
Open Source Developer committed
902 903 904 905 906 907 908
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
    }


Open Source Developer's avatar
trans  
Open Source Developer committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

public function getSchoolID(Request $request)
    {

        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
                    $schid = intval($user ->  init -> value );
                    $list = array();
                    $list[] = array(
                             'id' =>  $schid
                        );


                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
928

Open Source Developer's avatar
trans  
Open Source Developer committed
929 930 931 932 933 934 935 936 937 938 939 940 941


            } else {

            return $this->respondWithStatus([
                    'message' => t("User not found!"),
                ], Response::HTTP_FORBIDDEN);
        }

    }



Open Source Developer's avatar
Open Source Developer committed
942 943
public function gettypeofschool(Request $request)
    {
Open Source Developer's avatar
trans  
Open Source Developer committed
944

Open Source Developer's avatar
Open Source Developer committed
945
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
946

Open Source Developer's avatar
Open Source Developer committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
                $schid = intval($user ->  init -> value );
                $schools = $this->entityTypeManager->getStorage('eepal_school')->
                 loadByProperties(array('id' => $schid));
                 $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
962 963 964 965 966 967 968 969 970 971 972 973 974 975
                  $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }

                else if ($userRole === 'epal')
Open Source Developer's avatar
Open Source Developer committed
976
                 {
977

Open Source Developer's avatar
Open Source Developer committed
978 979
                        $list = array();
                        $list[] = array(
980
                            'type' =>  $school ->  operation_shift  -> value,
Open Source Developer's avatar
Open Source Developer committed
981 982 983 984
                            );
                        return $this->respondWithStatus(
                            $list
                        , Response::HTTP_OK);
985

Open Source Developer's avatar
Open Source Developer committed
986
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
987

Open Source Developer's avatar
Open Source Developer committed
988
            } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
989

Open Source Developer's avatar
Open Source Developer committed
990 991 992 993
            return $this->respondWithStatus([
                    'message' => t("User not found!"),
                ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
994

Open Source Developer's avatar
Open Source Developer committed
995
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
996 997


998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008


    public function getlimitsperCourse(Request $request, $classid)
    {
      $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if ($user)
            {
              $schoolid = $user ->  init -> value;
Open Source Developer's avatar
Open Source Developer committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

              $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;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }

                else if ($userRole === 'epal')
                 {

                 $list= array();
1037

1038
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid ));
1039 1040 1041
                $SchoolCat = reset($SchoolCats);
                if ($SchoolCat){
                $categ = $SchoolCat-> metathesis_region -> value;
1042

1043 1044 1045
                 $list = array();
               if ($classid == 1)
               {
1046

1047 1048 1049 1050 1051 1052
                    $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;
                    }
1053 1054 1055



1056 1057 1058 1059 1060 1061 1062 1063
                             $list[] = array(
                                    'id' => '1',
                                    'name' => 'Α Λυκείου',
                                    'categ' => $categ,
                                    'classes' => 1,
                                    'limitdown' => $limit,
                                    );

1064 1065 1066
                }


1067 1068 1069 1070 1071 1072 1073 1074
                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;
                    }

1075 1076


1077
                         $list[] = array(
1078

1079
                            'name' => 'Β Λυκείου ',
1080

1081 1082 1083 1084 1085 1086
                            'categ' => $categ,
                            'classes' => 2,
                            'limitdown' => $limit,

                          );
                    }
1087 1088


1089 1090 1091 1092 1093 1094 1095 1096
                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;
                    }

1097 1098


1099
                         $list[] = array(
1100 1101


1102 1103 1104
                            'categ' => $categ,
                            'classes' => 3,
                            'limitdown' => $limit,
1105

1106 1107
                          );
                    }
1108 1109 1110



1111 1112 1113
                            return $this->respondWithStatus(
                                     $list
                                   , Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
1114
                        }
1115 1116 1117 1118 1119 1120 1121 1122
                }
                else
                {
                       return $this->respondWithStatus([
                            'message' => t("Perfecture not found!"),
                        ], Response::HTTP_FORBIDDEN);

                }
1123
            }
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
            else
            {

                   return $this->respondWithStatus([
                            'message' => t("User not found!"),
                        ], Response::HTTP_FORBIDDEN);
            }
    }



Open Source Developer's avatar
Open Source Developer committed
1135 1136 1137
    public function FindCapacityPerSchool(Request $request)
    {

Open Source Developer's avatar
Open Source Developer committed
1138
        $i = 0;
Open Source Developer's avatar
Open Source Developer committed
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
       $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $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
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal')  {
                        $userRole = $tmpRole;
                    }
                }
                if ($userRole === '') {
                    return $this->respondWithStatus([
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
                }

                else if ($userRole === 'epal')
                 {
Open Source Developer's avatar
Open Source Developer committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

                 $list = array();
                 $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid ));
                 $classcapacity = reset($CapacityPerClass);


                  if ($classcapacity) {


                    $list[] = array(
                        'class' => 1,
Open Source Developer's avatar
Open Source Developer committed
1181 1182
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
1183 1184
                        'taxi' => 'Ά Λυκείου',
                        'capacity' => $classcapacity -> capacity_class_a -> value ,
Open Source Developer's avatar
Open Source Developer committed
1185
                        'globalindex' => $i ,
Open Source Developer's avatar
Open Source Developer committed
1186 1187
                       );
                    }
Open Source Developer's avatar
Open Source Developer committed
1188
                    $i++;
Open Source Developer's avatar
Open Source Developer committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
                    $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 ));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                          $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
1199 1200 1201
                                'class' => 2,
                                'newsector' => $object -> sector_id -> entity -> id(),
                                'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
1202 1203
                                'taxi' => 'Β Λυκείου  '.$object -> sector_id -> entity-> get('name')->value,
                                'capacity' => $classcapacity -> capacity_class_sector -> value ,
Open Source Developer's avatar
Open Source Developer committed
1204
                                'globalindex' => $i ,
Open Source Developer's avatar
Open Source Developer committed
1205 1206
                                );
                            }
Open Source Developer's avatar
Open Source Developer committed
1207
                            $i++;
Open Source Developer's avatar
Open Source Developer committed
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
                        }
                    }
                    $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() ;
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);
                          if ($classcapacity) {
                               $list[] = array(
                                'class' => 3,
Open Source Developer's avatar
Open Source Developer committed
1220 1221
                                    'newsector' => 0,
                                    'newspecialit' => $object -> specialty_id -> entity -> id(),
Open Source Developer's avatar
Open Source Developer committed
1222 1223
                                    'taxi' => 'Γ Λυκείου  '.$object -> specialty_id -> entity-> get('name')->value,
                                    'capacity' => $classcapacity -> capacity_class_specialty -> value ,
Open Source Developer's avatar
Open Source Developer committed
1224
                                    'globalindex' => $i ,
Open Source Developer's avatar
Open Source Developer committed
1225 1226
                            );
                        }
Open Source Developer's avatar
Open Source Developer committed
1227
                        $i++;
Open Source Developer's avatar
Open Source Developer committed
1228 1229
                        }
                    }
1230

Open Source Developer's avatar
Open Source Developer committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
                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(
                                    'class' => 4,
Open Source Developer's avatar
Open Source Developer committed
1241 1242
                                    'newsector' => 0,
                                    'newspecialit' => $object -> specialty_id -> entity -> id(),
Open Source Developer's avatar
Open Source Developer committed
1243 1244
                                    'taxi' => 'Δ Λυκείου  '.$object -> specialty_id -> entity-> get('name')->value,
                                    'capacity' => $classcapacity -> capacity_class_specialty_d -> value ,
Open Source Developer's avatar
Open Source Developer committed
1245
                                    'globalindex' => $i ,
1246

Open Source Developer's avatar
Open Source Developer committed
1247 1248
                                    );
                                }
Open Source Developer's avatar
Open Source Developer committed
1249
                                $i++;
Open Source Developer's avatar
Open Source Developer committed
1250 1251 1252 1253 1254 1255
                            }
                        }

                      return $this->respondWithStatus(
                                     $list
                                   , Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
1256
                  }
Open Source Developer's avatar
Open Source Developer committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

            } else {
            return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
        }
    }





Open Source Developer's avatar
transf  
Open Source Developer committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
   private function respondWithStatus($arr, $s) {
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
        return $res;
    }





}