DirectorView.php 43.2 KB
Newer Older
Open Source Developer's avatar
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
Open Source Developer committed
11
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
Open Source Developer's avatar
Open Source Developer committed
12

13 14
use Drupal\epal\Crypt;

Open Source Developer's avatar
Open Source Developer committed
15 16 17
class DirectorView extends ControllerBase
{
    protected $entityTypeManager;
18
    protected $logger;
Open Source Developer's avatar
Open Source Developer committed
19

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

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

36
    public function getStudentPerSchool(Request $request, $classId, $sector, $specialit)
Open Source Developer's avatar
Open Source Developer committed
37
    {
38
        try {
39
            $authToken = $request->headers->get('PHP_AUTH_USER');
40

41 42 43 44 45 46 47 48 49 50 51
            $epalConfigs = $this->entityTypeManager->getStorage('epal_config')->loadByProperties(array('name' => 'epal_config'));
            $epalConfig = reset($epalConfigs);
            if (!$epalConfig) {
                return $this->respondWithStatus([
                        "error_code" => 3001
                    ], Response::HTTP_FORBIDDEN);
            }
            if ($epalConfig->lock_students->value) {
                return $this->respondWithStatus([
                        "error_code" => 3002
                    ], Response::HTTP_FORBIDDEN);
52 53
            }

54 55 56 57 58 59 60 61 62 63 64
            $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
            $user = reset($users);
            if ($user) {
                $epalId = $user->init->value;
                $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $epalId));
                $school = reset($schools);
                if (!$school) {
                    $this->logger->warning('no access to this school='.$user->id());
                    return $this->respondWithStatus([
                        "message" => "No access to this school"
                    ], Response::HTTP_FORBIDDEN);
65
                }
66

67 68 69 70 71 72
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal') {
                        $userRole = $tmpRole;
                    }
73
                }
74 75
                if ($userRole === '') {
                    return $this->respondWithStatus([
76 77
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
78
                } elseif ($userRole === 'epal') {
79
                    $selectIdNew = $selectId;
80
                    if ($classId == 1) {
81
                        $selectIdNew = -1;
82
                    } elseif ($classId == 2) {
83
                        $selectIdNew = $sector;
84
                    } else {
85
                        $selectIdNew = $specialit;
86
                    }
Open Source Developer's avatar
Open Source Developer committed
87
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId));
88 89 90 91
                }
                if ($studentPerSchool) {
                    $list = array();
                    foreach ($studentPerSchool as $object) {
92 93 94
                        $studentId = intval($object->student_id->getString());
                        $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $studentId));
                        $epalStudent = reset($epalStudents);
95 96 97 98 99 100 101 102 103 104 105
                        if ($epalStudents) {
                            $studentIdNew = $epalStudent->id();
                            $checkstatus = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('student_id' => $studentIdNew));
                            $checkstudentstatus = reset($checkstatus);
                            $sectorName = '';
                            $courseName = '';
                            if ($epalStudent->currentclass->value === '2') {
                                $sectors = $this->entityTypeManager->getStorage('epal_student_sector_field')->loadByProperties(array('student_id' => $studentIdNew));
                                $sector = reset($sectors);
                                if ($sector) {
                                    $sectorName = $this->entityTypeManager->getStorage('eepal_sectors')->load($sector->sectorfield_id->target_id)->name->value;
106
                                }
107 108 109 110 111
                            } elseif ($epalStudent->currentclass->value === '3' || $epalStudent->currentclass->value === '4') {
                                $courses = $this->entityTypeManager->getStorage('epal_student_course_field')->loadByProperties(array('student_id' => $studentIdNew));
                                $course = reset($courses);
                                if ($course) {
                                    $courseName = $this->entityTypeManager->getStorage('eepal_specialty')->load($course->coursefield_id->target_id)->name->value;
112
                                }
113 114
                            }

115
                            $crypt = new Crypt();
116 117 118 119 120 121 122 123
                            try {
                                $name_decoded = $crypt->decrypt($epalStudent->name->value);
                                $studentsurname_decoded = $crypt->decrypt($epalStudent->studentsurname->value);
                                $fatherfirstname_decoded = $crypt->decrypt($epalStudent->fatherfirstname->value);
                                $motherfirstname_decoded = $crypt->decrypt($epalStudent->motherfirstname->value);
                                $regionaddress_decoded = $crypt->decrypt($epalStudent->regionaddress->value);
                                $regiontk_decoded = $crypt->decrypt($epalStudent->regiontk->value);
                                $regionarea_decoded = $crypt->decrypt($epalStudent->regionarea->value);
124
                                // $certificatetype_decoded = $crypt->decrypt($epalStudent->certificatetype->value); // obsolete
125 126 127 128 129 130 131 132 133 134 135 136 137
                                $relationtostudent_decoded = $crypt->decrypt($epalStudent->relationtostudent->value);
                                $telnum_decoded = $crypt->decrypt($epalStudent->telnum->value);
                                $guardian_name_decoded = $crypt->decrypt($epalStudent->guardian_name->value);
                                $guardian_surname_decoded = $crypt->decrypt($epalStudent->guardian_surname->value);
                                $guardian_fathername_decoded = $crypt->decrypt($epalStudent->guardian_fathername->value);
                                $guardian_mothername_decoded = $crypt->decrypt($epalStudent->guardian_mothername->value);
                            } catch (\Exception $e) {
                                $this->logger->warning(__METHOD__ . ' Decrypt error: ' . $e->getMessage());
                                return $this->respondWithStatus([
                                "message" => t("An unexpected error occured during DECODING data in getStudentPerSchool Method ")
                                ], Response::HTTP_INTERNAL_SERVER_ERROR);
                            }

138
                            $list[] = array(
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
                                'id' => $epalStudent->id(),
                                'name' => $name_decoded,
                                'studentsurname' => $studentsurname_decoded,
                                'fatherfirstname' => $fatherfirstname_decoded,
                                'motherfirstname' => $motherfirstname_decoded,
                                'guardian_name' => $guardian_name_decoded,
                                'guardian_surname' => $guardian_surname_decoded,
                                'guardian_fathername' => $guardian_fathername_decoded,
                                'guardian_mothername' => $guardian_mothername_decoded,
                                'lastschool_schoolname' => $epalStudent->lastschool_schoolname->value,
                                'lastschool_schoolyear' => $epalStudent->lastschool_schoolyear->value,
                                'lastschool_class' => $epalStudent->lastschool_class->value,
                                'currentclass' =>$epalStudent -> currentclass ->value,
                                'currentsector' =>$sectorName,
                                'currentcourse' =>$courseName,
                                'regionaddress' => $regionaddress_decoded,
                                'regiontk' => $regiontk_decoded,
                                'regionarea' => $regionarea_decoded,
157
                                'certificatetype' => '', // $certificatetype_decoded, // obsolete
158 159 160 161 162 163 164 165 166 167 168 169
                                'graduation_year' => $epalStudent->graduation_year->value,
                                'telnum' => $telnum_decoded,
                                'relationtostudent' => $relationtostudent_decoded,
                                //'birthdate' => substr($epalStudent->birthdate->value, 8, 10) . '/' . substr($epalStudent->birthdate->value, 6, 8) . '/' . substr($epalStudent->birthdate->value, 0, 4),
                                'birthdate' => date("d-m-Y", strtotime($epalStudent->birthdate->value)),
                                'checkstatus' => $checkstudentstatus -> directorconfirm ->value,
                                'created' => date('d/m/Y H:i', $epalStudent -> created ->value),

                            );
                        }
                    }
                    return $this->respondWithStatus(
170
                     $list, Response::HTTP_OK);
171 172
                } else {
                       return $this->respondWithStatus([
173
                    'message' => t('Students not found!'),
174 175 176 177 178
                        ], Response::HTTP_FORBIDDEN);
                }
            } else {
                return $this->respondWithStatus([
                'message' => t('User not found!'),
Open Source Developer's avatar
Open Source Developer committed
179
                ], Response::HTTP_FORBIDDEN);
180
            }
181 182
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
183
            return $this->respondWithStatus([
184
            'message' => t('Unexpected Error'),
185
            ], Response::HTTP_FORBIDDEN);
186
        }
187
    }
Open Source Developer's avatar
Open Source Developer committed
188

Open Source Developer's avatar
Open Source Developer committed
189 190 191
    public function ConfirmStudents(Request $request)
    {
        if (!$request->isMethod('POST')) {
192
            return $this->respondWithStatus(['message' => t('Method Not Allowed')], Response::HTTP_METHOD_NOT_ALLOWED);
Open Source Developer's avatar
Open Source Developer committed
193 194
        }

195
        $authToken = $request->headers->get('PHP_AUTH_USER');
196
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
197 198 199 200 201 202 203
        $user = reset($users);
        if ($user) {
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
204
                }
205
            }
206
            if ($userRole === 'epal') {
207 208 209 210
                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    $arr = $postData->students;
                    $type = $postData->type;
211 212
                    $valnew = intval($arr);
                    $typen = intval($type);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                    $studentForConfirm = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(['student_id' => $valnew]);
                    $studentConfirm = reset($studentForConfirm);
                    if ($studentConfirm) {
                        if ($typen === 1) {
                            $studentConfirm->set('directorconfirm', 1);
                            $studentConfirm->save();
                            return $this->respondWithStatus(['message' => t('saved')], Response::HTTP_OK);
                        } elseif ($typen === 2) {
                            $studentConfirm->set('directorconfirm', 0);
                            $studentConfirm->save();
                            return $this->respondWithStatus(['message' => t('saved')], Response::HTTP_OK);
                        } elseif ($typen === 3) {
                            unset($studentConfirm->{directorconfirm});
                            $studentConfirm->save();
                            return $this->respondWithStatus(['message' => t('saved')], Response::HTTP_OK);
                        } else {
                            return $this->respondWithStatus(['message' => t('Bad request')], Response::HTTP_FORBIDDEN);
230
                        }
231 232 233 234 235
                    } else {
                        return $this->respondWithStatus(['message' => t('Student not found')], Response::HTTP_FORBIDDEN);
                    }
                } else {
                    return $this->respondWithStatus(['message' => t('post with no data')], Response::HTTP_BAD_REQUEST);
Open Source Developer's avatar
Open Source Developer committed
236 237
                }
            } else {
238
                return $this->respondWithStatus(['error_code' => 4003], Response::HTTP_FORBIDDEN);
239 240
            }
        } else {
241
            return $this->respondWithStatus(['message' => t('EPAL user not found')], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
242 243 244
        }
    }

245
    public function SaveCapacity(Request $request, $taxi, $tomeas, $specialit)
Open Source Developer's avatar
Open Source Developer committed
246 247
    {
        if (!$request->isMethod('POST')) {
248
            return $this->respondWithStatus([
249
                    'message' => t('Method Not Allowed'),
Open Source Developer's avatar
Open Source Developer committed
250 251 252 253
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

254 255 256 257 258 259 260 261 262 263 264 265 266
        $epalConfigs = $this->entityTypeManager->getStorage('epal_config')->loadByProperties(array('name' => 'epal_config'));
        $epalConfig = reset($epalConfigs);
        if (!$epalConfig) {
            return $this->respondWithStatus([
                    "error_code" => 3001
                ], Response::HTTP_FORBIDDEN);
        }
        if ($epalConfig->lock_capacity->value) {
            return $this->respondWithStatus([
                    "error_code" => 3002
                ], Response::HTTP_FORBIDDEN);
        }

267
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        $user = reset($users);
        if ($user) {
            $schoolid = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
287
                }
288 289 290
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
291 292
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
293 294
            } elseif ($userRole === 'epal') {
                $postData = null;
295

296 297 298 299 300 301
                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!'),
302
                        ], Response::HTTP_BAD_REQUEST);
303
                    }
Open Source Developer's avatar
Open Source Developer committed
304

305 306 307 308 309 310
                    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();
311
                        }
312
                    }
Open Source Developer's avatar
Open Source Developer committed
313

314 315 316 317 318 319
                    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();
320
                        }
321
                    }
Open Source Developer's avatar
Open Source Developer committed
322

323 324 325 326 327 328
                    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();
329
                        }
330
                    }
331

332 333 334 335 336 337 338 339
                    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();
                        }
                    }
340

341 342
                    return $this->respondWithStatus([
                            'message' => t('saved'),
343
                        ], Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
344 345
                }
            } else {
346 347 348 349 350
                return $this->respondWithStatus([
                    'message' => t('post with no data'),
                ], Response::HTTP_BAD_REQUEST);
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
351
            return $this->respondWithStatus([
352
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
353 354 355 356
                ], Response::HTTP_FORBIDDEN);
        }
    }

357
    public function getSchools(Request $request)
Open Source Developer's avatar
Open Source Developer committed
358 359 360 361 362
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
363 364 365 366 367 368 369
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
370
                }
371
            }
372
            $this->logger->warning('userRole='.$userRole);
373 374
            if ($userRole === '') {
                return $this->respondWithStatus([
375 376
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
377 378 379 380 381 382 383
            } elseif ($userRole === 'regioneduadmin') {
                $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('region_edu_admin_id' => $selectionId));
            } elseif ($userRole === 'eduadmin') {
                $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('edu_admin_id' => $selectionId));
            }
            if ($schools) {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
384

385 386 387 388 389 390
                foreach ($schools as $object) {
                    $status = $this->returnstatus($object->id());
                    $list[] = array(
                                    'id' => $object->id(),
                                    'name' => $object->name->value,
                                    'status' => $status,
Open Source Developer's avatar
Open Source Developer committed
391 392 393
                                    );
                }

394 395 396 397 398
                return $this->respondWithStatus(
                                     $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                            'message' => t('Perfecture not found!'),
Open Source Developer's avatar
Open Source Developer committed
399 400
                        ], Response::HTTP_FORBIDDEN);
            }
401 402 403 404 405
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
Open Source Developer committed
406 407
    }

Open Source Developer's avatar
Open Source Developer committed
408 409
    public function getCoursesPerSchool(Request $request, $schoolid)
    {
410
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
411 412 413

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
414 415 416 417 418 419 420
        if ($user) {
            $newid = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
421
                }
422 423 424
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
425 426
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
427 428 429 430 431
            } elseif ($userRole === 'regioneduadmin') {
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid, 'region_edu_admin_id' => $newid));
            } elseif ($userRole === 'eduadmin') {
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid, 'edu_admin_id' => $newid));
            }
Open Source Developer's avatar
Open Source Developer committed
432

433 434 435 436
            $SchoolCat = reset($SchoolCats);
            if ($SchoolCat) {
                $list = array();
                $categ = $SchoolCat->metathesis_region->value;
Open Source Developer's avatar
Open Source Developer committed
437
                $operation_shift = $SchoolCat -> operation_shift -> value;
438
            }
Open Source Developer's avatar
Open Source Developer committed
439

Open Source Developer's avatar
Open Source Developer committed
440
            $CourseA = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
441 442 443 444 445
            if ($CourseA) {
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 1, 'category' => $categ));
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
Open Source Developer's avatar
Open Source Developer committed
446
                }
Open Source Developer's avatar
Open Source Developer committed
447
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
448 449 450
                $list = array();
                foreach ($CourseA as $object) {
                    $list[] = array(
Open Source Developer's avatar
Open Source Developer committed
451
                                    'id' => '1',
Open Source Developer's avatar
Open Source Developer committed
452
                                    'name' => 'Α Λυκείου',
Open Source Developer's avatar
Open Source Developer committed
453 454
                                    'size' => sizeof($studentPerSchool),
                                    'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
455 456
                                    'classes' => 1,
                                    'limitdown' => $limit,
Open Source Developer's avatar
Open Source Developer committed
457
                                    );
458 459
                }
            }
460

461 462 463 464 465 466 467
            $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
            if ($CourseB) {
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 2, 'category' => $categ));
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
                }
Open Source Developer's avatar
Open Source Developer committed
468

469 470
                foreach ($CourseB as $object) {
                    $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
471
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
472 473 474
                    $list[] = array(
                            'id' => $object->sector_id->entity->id(),
                            'name' => 'Β Λυκείου  '.$object->sector_id->entity->get('name')->value,
Open Source Developer's avatar
Open Source Developer committed
475 476
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
477 478
                            'classes' => 2,
                            'limitdown' => $limit,
Open Source Developer's avatar
Open Source Developer committed
479 480 481

                          );
                }
482 483 484 485 486 487 488 489
            }
            $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
            if ($CourseC) {
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
                }
Open Source Developer's avatar
Open Source Developer committed
490

491 492
                foreach ($CourseC as $object) {
                    $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
493
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
Open Source Developer's avatar
Open Source Developer committed
494

495 496 497
                    $list[] = array(
                            'id' => $object->specialty_id->entity->id(),
                            'name' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
Open Source Developer's avatar
Open Source Developer committed
498 499
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
Open Source Developer's avatar
Open Source Developer committed
500 501
                            'classes' => 3,
                            'limitdown' => $limit,
502

Open Source Developer's avatar
Open Source Developer committed
503 504
                          );
                }
505
            }
506
            if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
Open Source Developer's avatar
Open Source Developer committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 4, '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('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 4));

                    $list[] = array(
                            'id' => $object->specialty_id->entity->id(),
                            'name' => 'Δ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                            'size' => sizeof($studentPerSchool),
                            'categ' => $categ,
                            'classes' => 4,
                            'limitdown' => $limit,

                          );
                }
            }

529 530 531 532 533 534
            if ($CourseA || $CourseB || $CourseC) {
                return $this->respondWithStatus(
                                     $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                            'message' => t('Perfecture not found!'),
Open Source Developer's avatar
Open Source Developer committed
535 536
                        ], Response::HTTP_FORBIDDEN);
            }
537 538 539 540 541
        } else {
            return $this->respondWithStatus([
                            'message' => t('User not found!'),
                        ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
Open Source Developer committed
542
    }
Open Source Developer's avatar
Open Source Developer committed
543

544 545 546 547 548 549 550 551
    public function returnstatus($id)
    {
        $schoolid = $id;
        $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
        $SchoolCat = reset($SchoolCats);
        if ($SchoolCat) {
            $categ = $SchoolCat->metathesis_region->value;
        }
Open Source Developer's avatar
Open Source Developer committed
552

553
        $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $schoolid));
554 555 556 557 558 559
        if ($CourseA) {
            $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 1, 'category' => $categ));
            $limitdown = reset($limit_down);
            if ($limitdown) {
                $limit = $limitdown->limit_down->value;
            }
Open Source Developer's avatar
Open Source Developer committed
560
            $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
561

562 563 564
            if (sizeof($studentPerSchool) < $limit) {
                return false;
            }
565
        }
566

567
        $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
568 569 570 571 572 573
        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;
            }
574

575 576
            foreach ($CourseB as $object) {
                $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
577
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
578 579
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
Open Source Developer's avatar
Open Source Developer committed
580
                }
581 582
            }
        }
583

584 585 586 587 588 589 590
        $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
        if ($CourseC) {
            $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
            $limitdown = reset($limit_down);
            if ($limitdown) {
                $limit = $limitdown->limit_down->value;
            }
Open Source Developer's avatar
Open Source Developer committed
591

592 593
            foreach ($CourseC as $object) {
                $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
594
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
Open Source Developer's avatar
Open Source Developer committed
595

596 597
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
Open Source Developer's avatar
Open Source Developer committed
598
                }
599 600
            }
        }
Open Source Developer's avatar
Open Source Developer committed
601

602 603
        return true;
    }
Open Source Developer's avatar
Open Source Developer committed
604

605

Open Source Developer's avatar
Open Source Developer committed
606
    public function FindCapacityPerSchool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
607
    {
Open Source Developer's avatar
Open Source Developer committed
608
        $i = 0;
609
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
610 611

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
612 613 614 615 616 617 618
        $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());
619
                return $this->respondWithStatus(['message' => 'No access to this school'], Response::HTTP_FORBIDDEN);
620
            }
621 622

            $operation_shift = $school->operation_shift->value;
623 624 625 626 627
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
628
                }
629 630
            }
            if ($userRole === '') {
631
                return $this->respondWithStatus(['error_code' => 4003], Response::HTTP_FORBIDDEN);
632
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
633
                $categ = $school->metathesis_region->value;
634
                $list = array();
635

Open Source Developer's avatar
Open Source Developer committed
636 637 638 639 640 641 642 643 644 645
                $CourseA = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                $classcapacity = reset($CourseA);
                if ($classcapacity) {
                    $list[] = array(
                        'class' => 1,
                        'newsector' => 0,
                        'newspecialit' => 0,
                        'taxi' => 'Ά Λυκείου',
                        'capacity' => $classcapacity->capacity_class_a->value,
                        'globalindex' => $i,
646
                    );
647
                }
Open Source Developer's avatar
Open Source Developer committed
648
                ++$i;
649

Open Source Developer's avatar
Open Source Developer committed
650 651 652 653
                $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();
654

Open Source Developer's avatar
Open Source Developer committed
655 656 657 658 659 660 661 662
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'sector_id' => $sectorid));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $list[] = array(
                                'class' => 2,
                                'newsector' => $object->sector_id->entity->id(),
                                'newspecialit' => 0,
                                'taxi' => 'Β Λυκείου  '.$object->sector_id->entity->get('name')->value,
663
                                'capacity' => $classcapacity->capacity_class_sector->value,
Open Source Developer's avatar
Open Source Developer committed
664
                                'globalindex' => $i,
665
                            );
Open Source Developer's avatar
Open Source Developer committed
666 667
                        }
                        ++$i;
668 669
                    }
                }
670

Open Source Developer's avatar
Open Source Developer committed
671 672
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
673
                    foreach ($CourseC as $object) {
Open Source Developer's avatar
Open Source Developer committed
674 675 676 677 678 679
                        $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,
680 681 682 683 684
                                'newsector' => 0,
                                'newspecialit' => $object->specialty_id->entity->id(),
                                'taxi' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                                'capacity' => $classcapacity->capacity_class_specialty->value,
                                'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
685 686 687
                            );
                        }
                        ++$i;
688 689
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
690

Open Source Developer's avatar
Open Source Developer committed
691 692 693
                if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
694

Open Source Developer's avatar
Open Source Developer committed
695 696 697 698
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $list[] = array(
699 700 701 702 703 704 705
                                'class' => 4,
                                'newsector' => 0,
                                'newspecialit' => $object->specialty_id->entity->id(),
                                'taxi' => 'Δ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                                'capacity' => $classcapacity->capacity_class_specialty_d->value,
                                'globalindex' => $i,
                            );
Open Source Developer's avatar
Open Source Developer committed
706 707
                        }
                        ++$i;
708 709
                    }
                }
710

711
                return $this->respondWithStatus($list, Response::HTTP_OK);
712
            }
713
        } else {
714
            return $this->respondWithStatus(['message' => t('EPAL user not found')], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
715 716
        }
    }
717

Open Source Developer's avatar
Open Source Developer committed
718
    public function FindCoursesPerSchool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
719
    {
720
        $i = 0;
721
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
722 723

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
724 725 726 727 728 729 730 731 732 733 734 735 736 737
        $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;
            }
738
            $operation_shift = $school -> operation_shift -> value;
739 740 741 742 743
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
744
                }
745 746 747
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
748 749
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
750
            } elseif ($userRole === 'epal') {
751
                $categ = $school->metathesis_region->value;
752
                $list = array();
753

754
                $CourseA = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
Open Source Developer's avatar
Open Source Developer committed
755
                if ($CourseA) {
756 757 758 759 760 761
                    $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('epal_id' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
762

Open Source Developer's avatar
Open Source Developer committed
763 764
                    $list[] = array(
                        'class' => 1,
765 766
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
767
                        'taxi' => 'Ά Λυκείου',
768
                        'globalindex' => $i,
769 770
                        'limitdown' => $limit,
                        'size' => sizeof($studentPerSchool),
Open Source Developer's avatar
Open Source Developer committed
771
                       );
772 773
                }
                ++$i;
774

775 776
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseB) {
777 778 779 780 781 782
                    $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;
                    }

783 784
                    foreach ($CourseB as $object) {
                        $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
785
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
786 787 788 789 790 791 792 793 794
                        $list[] = array(
                            'class' => 2,
                            'newsector' => $object->sector_id->entity->id(),
                            'newspecialit' => 0,
                            'taxi' => 'Β Λυκείου  '.$object->sector_id->entity->get('name')->value,
                            'globalindex' => $i,
                            'limitdown' => $limit,
                            'size' => sizeof($studentPerSchool),
                            );
795

796
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
797
                    }
798
                }
799

800 801
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
802 803
                    $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
                        $limitdown = reset($limit_down);
804 805 806 807
                    if ($limitdown) {
                        $limit = $limitdown->limit_down->value;
                    }
                    foreach ($CourseC as $object) {
808
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
809
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
810
                        $list[] = array(
811
                            'class' => 3,
812 813 814 815 816 817 818
                            'newsector' => 0,
                            'newspecialit' => $object->specialty_id->entity->id(),
                            'taxi' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                            'globalindex' => $i,
                            'limitdown' => $limit,
                            'size' => sizeof($studentPerSchool),
                        );
819
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
820
                    }
821
                }
822

823 824 825
                if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
                        $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 4, 'category' => $categ));
                        $limitdown = reset($limit_down);
826 827 828
                    if ($limitdown) {
                        $limit = $limitdown->limit_down->value;
                    }
829 830
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
831
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 4));
832 833 834 835 836 837 838 839 840
                        $list[] = array(
                                'class' => 4,
                                'newsector' => 0,
                                'newspecialit' => $object->specialty_id->entity->id(),
                                'taxi' => 'Δ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                                'globalindex' => $i,
                                'limitdown' => $limit,
                                'size' => sizeof($studentPerSchool),
                        );
841 842 843
                        ++$i;
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
844

845
                return $this->respondWithStatus($list, Response::HTTP_OK);
846 847
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
848
            return $this->respondWithStatus([
849
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
850 851 852 853
                ], Response::HTTP_FORBIDDEN);
        }
    }

854 855
    private function respondWithStatus($arr, $s)
    {
Open Source Developer's avatar
Open Source Developer committed
856 857
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
858

Open Source Developer's avatar
Open Source Developer committed
859 860 861
        return $res;
    }
}