DirectorView.php 38.6 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 46 47 48
                $epalId = $user ->  init -> value;

             $schools = $this->entityTypeManager->getStorage('eepal_school')->
             loadByProperties(array('id' => $epalId));
49 50 51 52 53 54 55 56 57 58 59 60
             $school = reset($schools);
             if (!$school) {
                 $this->logger->warning("no access to this school=" . $user->id());
                 $response = new Response();
                 $response->setContent('No access to this school');
                 $response->setStatusCode(Response::HTTP_FORBIDDEN);
                 $response->headers->set('Content-Type', 'application/json');
                 return $response;
             }


                $userid = $user -> id();
Open Source Developer's avatar
Open Source Developer committed
61 62
                //$epalIdNew = intval($epalId);
                $sectorPerSchool = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id'=> $epalId));
Open Source Developer's avatar
transf  
Open Source Developer committed
63 64 65 66 67 68 69 70 71 72 73 74
                $i = 0;

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

                   	 $i++;
                }
75

Open Source Developer's avatar
transf  
Open Source Developer committed
76 77 78 79 80 81 82
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
83
                ], Response::HTTP_OK);
Open Source Developer's avatar
transf  
Open Source Developer committed
84
                }
85

Open Source Developer's avatar
transf  
Open Source Developer committed
86 87 88 89 90 91 92 93


        } else {

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

Open Source Developer's avatar
transf  
Open Source Developer committed
95 96
    }

Open Source Developer's avatar
Open Source Developer committed
97
public function getSpecialPerSchool(Request $request , $sectorId)
Open Source Developer's avatar
trans  
Open Source Developer committed
98
    {
99

Open Source Developer's avatar
trans  
Open Source Developer committed
100
        $authToken = $request->headers->get('PHP_AUTH_USER');
101 102 103 104

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

            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array( 'id' => $epalId));
108 109 110 111 112 113 114 115 116 117
             $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;
             }
                $userid = $user -> id();
Open Source Developer's avatar
Open Source Developer committed
118 119
                //$epalIdNew = intval($epalId);
                $specialityPerSchool = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id'=> $epalId));
Open Source Developer's avatar
trans  
Open Source Developer committed
120 121 122 123 124 125 126 127 128 129
                $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);
130
                       if ($specialPerSec)
Open Source Developer's avatar
trans  
Open Source Developer committed
131 132 133 134 135
                        {       $list[] = array(
                                'specialty_id' => $object -> specialty_id ->entity->get('name')->value ,
                                'id' => $object -> specialty_id -> entity -> id()     );
                                $i++;
                        }
136

Open Source Developer's avatar
trans  
Open Source Developer committed
137
                }
138

Open Source Developer's avatar
trans  
Open Source Developer committed
139 140 141 142 143 144 145
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
146
                ], Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
147
                }
148

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

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

Open Source Developer's avatar
trans  
Open Source Developer committed
151 152 153 154 155 156
        } else {

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

Open Source Developer's avatar
trans  
Open Source Developer committed
158 159 160
    }


Open Source Developer's avatar
Open Source Developer committed
161
public function getStudentPerSchool(Request $request , $selectId, $classId, $limitdown, $limitup)
Open Source Developer's avatar
trans  
Open Source Developer committed
162
    {
163

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

       $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if ($user) {
Open Source Developer's avatar
Open Source Developer committed
169 170
            $epalId = $user ->  init -> value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array( 'id' => $epalId));
171 172 173 174 175 176 177 178 179 180 181
            $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;
            }

                $userid = $user -> id();
Open Source Developer's avatar
Open Source Developer committed
182
                //$epalIdNew = intval($epalId);
Open Source Developer's avatar
trans  
Open Source Developer committed
183
                $selectIdNew = intval($selectId);
Open Source Developer's avatar
trans  
Open Source Developer committed
184 185 186
                if ($classId == 1)
                {
                 $selectIdNew = -1;
Open Source Developer's avatar
Open Source Developer committed
187
                 $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
188

Open Source Developer's avatar
trans  
Open Source Developer committed
189 190
                }
                else
191
                {
Open Source Developer's avatar
Open Source Developer committed
192
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
193
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
194 195 196 197
                $i = 0;

            if ($studentPerSchool) {
                 $list = array();
Open Source Developer's avatar
paging  
Open Source Developer committed
198 199 200 201
                 $i = 0;
                 if  ($limitdown==$limitup && $limitup == 0)
                     {
                            $list=array(
Open Source Developer's avatar
Open Source Developer committed
202
                                   'id' => sizeof($studentPerSchool),
203
                                   'up' => $limitup,
Open Source Developer's avatar
Open Source Developer committed
204
                                   'down' => $limitdown
205
                                );
Open Source Developer's avatar
paging  
Open Source Developer committed
206 207
                     }

208
                  else
Open Source Developer's avatar
paging  
Open Source Developer committed
209 210 211 212 213 214
                  {
                         foreach ($studentPerSchool as $object)
                                {
                                $studentId = $object -> id() ;
                                $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $studentId));
                                $epalStudent = reset($epalStudents);
215

Open Source Developer's avatar
paging  
Open Source Developer committed
216
                                if ($epalStudents) {
Open Source Developer's avatar
trans  
Open Source Developer committed
217 218 219
                                    $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
220
                                   if ($i >= $limitdown && $i < $limitup)
221
                                   {
Open Source Developer's avatar
trans  
Open Source Developer committed
222
                                    $newstatus = $checkstudentstatus -> directorconfirm-> getValue();
223

Open Source Developer's avatar
paging  
Open Source Developer committed
224 225 226 227 228 229 230 231 232
                                   $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,
Open Source Developer's avatar
trans  
Open Source Developer committed
233
                                    'guardianfirstname' =>$epalUser -> name ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
234
                                    'guardiansurname' =>$epalUser -> surname ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
235 236 237 238 239 240
                                    'regionaddress' =>$epalStudent -> regionaddress ->value,
                                    'regiontk' =>$epalStudent -> regiontk ->value,
                                    'regionarea' =>$epalStudent -> regionarea ->value,
                                    'certificatetype' =>$epalStudent -> certificatetype ->value,
                                    'telnum' =>$epalStudent -> telnum ->value,
                                    'relationtostudent' =>$epalStudent -> relationtostudent ->value,
Open Source Developer's avatar
paging  
Open Source Developer committed
241
                                    'birthdate' =>$epalStudent -> birthdate ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
242 243
                                    'checkstatus' =>$newstatus[0][value],

Open Source Developer's avatar
paging  
Open Source Developer committed
244 245 246 247 248 249 250 251 252
                                    );
                                   }
                                   $i++;
                                }
                            }
                        }
                        return $this->respondWithStatus(
                                $list
                            , Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
253 254
                        }
             else {
255 256
                 $list = array();
                       return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
257 258 259
                }


260 261

        }
Open Source Developer's avatar
trans  
Open Source Developer committed
262 263 264 265 266 267
        else {

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

Open Source Developer's avatar
trans  
Open Source Developer committed
269
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
270 271 272



Open Source Developer's avatar
trans  
Open Source Developer committed
273 274 275 276
    public function ConfirmStudents(Request $request)
    {

        if (!$request->isMethod('POST')) {
277
            return $this->respondWithStatus([
Open Source Developer's avatar
trans  
Open Source Developer committed
278 279 280 281 282
                    "message" => t("Method Not Allowed")
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

283 284 285
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
286
//
287

Open Source Developer's avatar
trans  
Open Source Developer committed
288
            $postData = null;
Open Source Developer's avatar
trans  
Open Source Developer committed
289

Open Source Developer's avatar
trans  
Open Source Developer committed
290
            if ($content = $request->getContent())
Open Source Developer's avatar
trans  
Open Source Developer committed
291 292 293
            {
                 $postData = json_decode($content);
                 $arr = $postData->students;
Open Source Developer's avatar
trans  
Open Source Developer committed
294
                 $type = $postData ->type;
Open Source Developer's avatar
trans  
Open Source Developer committed
295 296

                foreach ($arr as $value) {
297
                    $valnew = intval($value);
Open Source Developer's avatar
trans  
Open Source Developer committed
298 299 300
                 $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
301
                    if ($type == 1)
Open Source Developer's avatar
trans  
Open Source Developer committed
302
                         $studentConfirm->set('directorconfirm', true);
Open Source Developer's avatar
trans  
Open Source Developer committed
303
                     if ($type == 2)
Open Source Developer's avatar
trans  
Open Source Developer committed
304
                        $studentConfirm->set('directorconfirm', false);
Open Source Developer's avatar
trans  
Open Source Developer committed
305 306
                    if ($type == 3)
                        unset($studentConfirm->{directorconfirm});
Open Source Developer's avatar
trans  
Open Source Developer committed
307
                         $studentConfirm->save();
308 309
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
                return $this->respondWithStatus([
                    'message' => t("saved"),
                ], Response::HTTP_OK);
            }
             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
331 332


Open Source Developer's avatar
Open Source Developer committed
333
public function SaveCapacity(Request $request,$taxi,$tomeas,$specialit)
Open Source Developer's avatar
Open Source Developer committed
334 335
    {

Open Source Developer's avatar
trans  
Open Source Developer committed
336 337


Open Source Developer's avatar
Open Source Developer committed
338
        if (!$request->isMethod('POST')) {
339
            return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
340 341 342 343 344
                    "message" => t("Method Not Allowed")
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

345 346 347
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
Open Source Developer's avatar
Open Source Developer committed
348
             $schoolid = $user ->  init -> value;
Open Source Developer's avatar
trans  
Open Source Developer committed
349
             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid));
350 351 352 353 354 355 356 357 358
             $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
359 360 361 362 363 364
            $postData = null;

            if ($content = $request->getContent())
            {
                 $postData = json_decode($content);
                 $cap = $postData->capacity;
Open Source Developer's avatar
Open Source Developer committed
365 366 367 368 369 370 371
                 if ($cap<= 0 || $cap > 99)
                 {
                    return $this->respondWithStatus([
                    'message' => t("Number out of limits!"),
                ], Response::HTTP_BAD_REQUEST);
                 }

Open Source Developer's avatar
trans  
Open Source Developer committed
372
                if (($tomeas == 0) && ($specialit == 0))
Open Source Developer's avatar
Open Source Developer committed
373 374 375 376 377 378
                {
                 $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid ));
                 $classcapacity = reset($CapacityPerClass);
                  if ($classcapacity) {
                         $classcapacity->set('capacity_class_a', $cap);
                         $classcapacity->save();
379
                    }
Open Source Developer's avatar
Open Source Developer committed
380 381 382
                }


Open Source Developer's avatar
trans  
Open Source Developer committed
383
                if (($tomeas != 0) && ($specialit == 0))
Open Source Developer's avatar
Open Source Developer committed
384 385 386 387 388 389
                {
                 $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();
390
                    }
Open Source Developer's avatar
Open Source Developer committed
391 392 393
                }


Open Source Developer's avatar
Open Source Developer committed
394
                if (($tomeas != 0) && ($specialit != 0) && ($taxi == 3))
Open Source Developer's avatar
Open Source Developer committed
395 396 397 398 399 400
                {
                 $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();
401
                    }
Open Source Developer's avatar
Open Source Developer committed
402 403 404
                }


Open Source Developer's avatar
Open Source Developer committed
405 406 407 408 409 410 411 412 413
                if (($tomeas != 0) && ($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
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435



                return $this->respondWithStatus([
                    'message' => t("saved"),
                ], Response::HTTP_OK);
            }
             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);
        }
    }


436
    public function getSchools(Request $request)
Open Source Developer's avatar
trans  
Open Source Developer committed
437 438 439 440 441 442 443 444
    {

        $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
445
                $selectionId = $user ->  init -> value;
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
                $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
463
                    $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('edu_admin_id'=> $selectionId ));
464
                }
465
                if ($schools)
Open Source Developer's avatar
trans  
Open Source Developer committed
466 467
                {
                    $list = array();
Open Source Developer's avatar
Open Source Developer committed
468

Open Source Developer's avatar
trans  
Open Source Developer committed
469
                    foreach ($schools as $object) {
470
                             $status = $this->returnstatus($object -> id());
Open Source Developer's avatar
trans  
Open Source Developer committed
471
                             $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
472 473
                                    'id' =>$object -> id(),
                                    'name' => $object -> name ->value,
Open Source Developer's avatar
Open Source Developer committed
474
                                    'status' => $status
Open Source Developer's avatar
trans  
Open Source Developer committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
                                    );

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

                }
490
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
491 492 493 494 495 496 497 498 499 500 501
            else
            {

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

    }


Open Source Developer's avatar
Open Source Developer committed
502 503 504



Open Source Developer's avatar
trans  
Open Source Developer committed
505 506 507 508 509 510 511 512
    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)
            {
Open Source Developer's avatar
Open Source Developer committed
513

Open Source Developer's avatar
trans  
Open Source Developer committed
514 515
               $list= array();

516
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid ));
Open Source Developer's avatar
trans  
Open Source Developer committed
517 518 519 520 521
                $SchoolCat = reset($SchoolCats);
                if ($SchoolCat){
                $categ = $SchoolCat-> metathesis_region -> value;
                }
                $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $schoolid ));
522 523

                if ($CourseA)
Open Source Developer's avatar
trans  
Open Source Developer committed
524
                {
Open Source Developer's avatar
Open Source Developer committed
525 526 527 528 529 530
                    $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
531
                    $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
532 533 534
                    $list = array();
                    foreach ($CourseA as $object) {
                             $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
535
                                    'id' => '1',
Open Source Developer's avatar
trans  
Open Source Developer committed
536
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
trans  
Open Source Developer committed
537 538
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
539 540
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
541
                                    );
542

Open Source Developer's avatar
trans  
Open Source Developer committed
543 544
                }            }

545

Open Source Developer's avatar
trans  
Open Source Developer committed
546 547 548
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid ));
                if ($CourseB)
                {
Open Source Developer's avatar
Open Source Developer committed
549 550 551 552 553 554 555
                    $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
556
                    foreach ($CourseB as $object) {
Open Source Developer's avatar
trans  
Open Source Developer committed
557 558
                    $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
559
                         $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
560
                            'id' => $object -> sector_id -> entity -> id(),
Open Source Developer's avatar
trans  
Open Source Developer committed
561
                            'name' => 'Β Λυκείου  '.$object -> sector_id -> entity-> get('name')->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
562 563
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
564 565
                            'classes' => 2,
                            'limitdown' => $limit,
Open Source Developer's avatar
trans  
Open Source Developer committed
566 567 568 569 570 571 572

                          );
                    }
                }
              $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid ));
                if ($CourseC)
                {
Open Source Developer's avatar
Open Source Developer committed
573 574 575 576 577 578 579
                    $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
580
                    foreach ($CourseC as $object) {
Open Source Developer's avatar
trans  
Open Source Developer committed
581 582 583
                    $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
584
                         $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
585
                            'id'=> $object -> specialty_id -> entity -> id(),
Open Source Developer's avatar
trans  
Open Source Developer committed
586
                            'name' => 'Γ Λυκείου  '.$object -> specialty_id -> entity-> get('name')->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
587 588
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
589 590
                            'classes' => 3,
                            'limitdown' => $limit,
591

Open Source Developer's avatar
trans  
Open Source Developer committed
592 593 594
                          );
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
595 596
                if ($CourseA || $CourseB || $CourseC)
                {
597

Open Source Developer's avatar
trans  
Open Source Developer committed
598 599 600 601 602 603 604 605 606 607 608
                            return $this->respondWithStatus(
                                     $list
                                   , Response::HTTP_OK);
                }
                else
                {
                       return $this->respondWithStatus([
                            'message' => t("Perfecture not found!"),
                        ], Response::HTTP_FORBIDDEN);

                }
609
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
610 611 612 613 614 615 616 617
            else
            {

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

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

Open Source Developer's avatar
Open Source Developer committed
620 621
public function returnstatus($id)
{
Open Source Developer's avatar
Open Source Developer committed
622
                $schoolid = $id;
623
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid ));
Open Source Developer's avatar
Open Source Developer committed
624 625 626 627 628
                $SchoolCat = reset($SchoolCats);
                if ($SchoolCat){
                $categ = $SchoolCat-> metathesis_region -> value;
                }
                $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $schoolid ));
629

630
                if ($CourseA)
Open Source Developer's avatar
Open Source Developer committed
631 632 633 634 635 636 637 638
                {
                    $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 ));
639

Open Source Developer's avatar
Open Source Developer committed
640 641
                    //foreach ($CourseA as $object) {
                            if (sizeof($studentPerSchool) < $limit){
642 643
                                return false;
//                                exit;
Open Source Developer's avatar
Open Source Developer committed
644
                            }
645

Open Source Developer's avatar
Open Source Developer committed
646 647
                }          //  }

648

Open Source Developer's avatar
Open Source Developer committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662
                $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){
663 664
                                return false;
                                exit;
Open Source Developer's avatar
Open Source Developer committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
                            }
                    }
                }
                $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;
684 685
                                exit;
                                }
Open Source Developer's avatar
Open Source Developer committed
686 687 688
                    }
                }
                return true;
689
//                exit;
Open Source Developer's avatar
Open Source Developer committed
690
}
Open Source Developer's avatar
trans  
Open Source Developer committed
691 692


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

Open Source Developer's avatar
Open Source Developer committed
694
public function findCapacity(Request $request,$taxi,$tomeas,$specialit)
Open Source Developer's avatar
trans  
Open Source Developer committed
695 696 697
    {

    $tomeasnew = intval($tomeas);
Open Source Developer's avatar
trans  
Open Source Developer committed
698
    $specialitnew = intval($specialit) ;
Open Source Developer's avatar
trans  
Open Source Developer committed
699 700 701 702 703
       $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
704
            $schoolid = $user ->  init -> value;
Open Source Developer's avatar
trans  
Open Source Developer committed
705
           $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid));
Open Source Developer's avatar
trans  
Open Source Developer committed
706 707 708 709 710 711 712 713 714
             $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;
             }
715

Open Source Developer's avatar
trans  
Open Source Developer committed
716 717 718 719 720 721
              $list = array();

                if (($tomeasnew == 0) && ($specialitnew == 0))
                {
                 $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid ));
                 $classcapacity = reset($CapacityPerClass);
722 723


Open Source Developer's avatar
trans  
Open Source Developer committed
724
                  if ($classcapacity) {
725 726


Open Source Developer's avatar
trans  
Open Source Developer committed
727 728 729
                    $list[] = array(
                        'taxi' => $taxi,
                       'capacity' => $classcapacity -> capacity_class_a -> value ,
730
                       );
Open Source Developer's avatar
trans  
Open Source Developer committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744
                    }
                }


                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 ,
Open Source Developer's avatar
trans  
Open Source Developer committed
745
                        'sector' =>$tomeasnew."lala".$specialitnew
Open Source Developer's avatar
trans  
Open Source Developer committed
746 747 748 749 750
                        );
                    }
                }


Open Source Developer's avatar
Open Source Developer committed
751
                if (($tomeasnew != 0) && ($specialitnew != 0) && ($taxi == 3))
Open Source Developer's avatar
trans  
Open Source Developer committed
752 753 754 755 756 757 758 759 760 761
                {
                 $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 ,
Open Source Developer's avatar
trans  
Open Source Developer committed
762
                        'specialty' =>"fromspeciality"
Open Source Developer's avatar
trans  
Open Source Developer committed
763 764 765 766
                        );
                    }
                }

Open Source Developer's avatar
Open Source Developer committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
                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"
                        );
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
782 783 784 785 786 787 788 789





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

Open Source Developer's avatar
trans  
Open Source Developer committed
791 792 793 794 795 796 797 798 799

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


Open Source Developer's avatar
trans  
Open Source Developer committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

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

Open Source Developer's avatar
trans  
Open Source Developer committed
820 821 822 823 824 825 826 827 828 829 830 831 832


            } else {

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

    }



Open Source Developer's avatar
Open Source Developer committed
833 834
public function gettypeofschool(Request $request)
    {
Open Source Developer's avatar
trans  
Open Source Developer committed
835

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

Open Source Developer's avatar
Open Source Developer committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
        $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;
                 }
                 else
                 {
                        $list = array();
                        $list[] = array(
857
                            'type' =>  $school ->  operation_shift  -> value,
Open Source Developer's avatar
Open Source Developer committed
858 859 860 861
                            );
                        return $this->respondWithStatus(
                            $list
                        , Response::HTTP_OK);
862

Open Source Developer's avatar
Open Source Developer committed
863
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
864

Open Source Developer's avatar
Open Source Developer committed
865
            } else {
Open Source Developer's avatar
trans  
Open Source Developer committed
866

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

Open Source Developer's avatar
Open Source Developer committed
872
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
873 874


875 876 877 878 879 880 881 882 883 884 885 886 887


    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;
               $list= array();

888
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id'=> $schoolid ));
889 890 891
                $SchoolCat = reset($SchoolCats);
                if ($SchoolCat){
                $categ = $SchoolCat-> metathesis_region -> value;
892

893 894 895
                 $list = array();
               if ($classid == 1)
               {
896

897 898 899 900 901 902
                    $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;
                    }
903 904 905



906 907 908 909 910 911 912 913
                             $list[] = array(
                                    'id' => '1',
                                    'name' => 'Α Λυκείου',
                                    'categ' => $categ,
                                    'classes' => 1,
                                    'limitdown' => $limit,
                                    );

914 915 916
                }


917 918 919 920 921 922 923 924
                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;
                    }

925 926


927
                         $list[] = array(
928

929
                            'name' => 'Β Λυκείου ',
930

931 932 933 934 935 936
                            'categ' => $categ,
                            'classes' => 2,
                            'limitdown' => $limit,

                          );
                    }
937 938


939 940 941 942 943 944 945 946
                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;
                    }

947 948


949
                         $list[] = array(
950 951


952 953 954
                            'categ' => $categ,
                            'classes' => 3,
                            'limitdown' => $limit,
955

956 957
                          );
                    }
958 959 960



961 962 963 964 965 966 967 968 969 970 971
                            return $this->respondWithStatus(
                                     $list
                                   , Response::HTTP_OK);
                }
                else
                {
                       return $this->respondWithStatus([
                            'message' => t("Perfecture not found!"),
                        ], Response::HTTP_FORBIDDEN);

                }
972
            }
973 974 975 976 977 978 979 980 981 982 983
            else
            {

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



Open Source Developer's avatar
transf  
Open Source Developer committed
984 985 986 987 988 989 990 991 992 993 994
   private function respondWithStatus($arr, $s) {
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
        return $res;
    }





}