DirectorView.php 45.8 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;
12
use Drupal\Core\Database\Connection;
Open Source Developer's avatar
Open Source Developer committed
13

14 15
use Drupal\epal\Crypt;

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

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

    public static function create(ContainerInterface $container)
    {
        return new static(
35
            $container->get('entity_type.manager'),
36
            $container->get('database'),
37
            $container->get('logger.factory')
Open Source Developer's avatar
Open Source Developer committed
38
        );
39
    }
Open Source Developer's avatar
Open Source Developer committed
40

41
    public function getStudentPerSchool(Request $request, $classId, $sector, $specialit)
Open Source Developer's avatar
Open Source Developer committed
42
    {
43
        try {
44
            $authToken = $request->headers->get('PHP_AUTH_USER');
45

46 47 48 49 50 51 52
            $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);
            }
53
            if ($epalConfig->lock_school_students_view->value) {
54 55 56
                return $this->respondWithStatus([
                        "error_code" => 3002
                    ], Response::HTTP_FORBIDDEN);
57 58
            }

59 60 61 62 63 64 65 66 67 68 69
            $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);
70
                }
71

72 73 74 75 76 77
                $userRoles = $user->getRoles();
                $userRole = '';
                foreach ($userRoles as $tmpRole) {
                    if ($tmpRole === 'epal') {
                        $userRole = $tmpRole;
                    }
78
                }
79 80
                if ($userRole === '') {
                    return $this->respondWithStatus([
81 82
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
83 84
                } elseif ($userRole === 'epal') {
                    if ($classId == 1) {
85
                        $selectIdNew = -1;
86
                    } elseif ($classId == 2) {
87
                        $selectIdNew = $sector;
88
                    } else {
89
                        $selectIdNew = $specialit;
90
                    }
Open Source Developer's avatar
Open Source Developer committed
91
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId));
92 93 94 95
                }
                if ($studentPerSchool) {
                    $list = array();
                    foreach ($studentPerSchool as $object) {
96
                        $studentId = $object->student_id->target_id;
97 98
                        $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $studentId));
                        $epalStudent = reset($epalStudents);
99 100 101 102 103 104 105 106 107 108 109
                        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;
110
                                }
111 112 113 114 115
                            } 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;
116
                                }
117 118
                            }

119
                            $crypt = new Crypt();
120 121 122 123 124 125 126 127
                            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);
128
                                // $certificatetype_decoded = $crypt->decrypt($epalStudent->certificatetype->value); // obsolete
129 130 131 132 133 134 135 136 137 138 139 140 141
                                $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);
                            }

142
                            $list[] = array(
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                                '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,
161
                                'certificatetype' => '', // $certificatetype_decoded, // obsolete
162 163 164 165 166 167 168 169 170 171 172 173
                                '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(
174
                     $list, Response::HTTP_OK);
175 176
                } else {
                       return $this->respondWithStatus([
177
                    'message' => t('Students not found!'),
178 179 180 181 182
                        ], Response::HTTP_FORBIDDEN);
                }
            } else {
                return $this->respondWithStatus([
                'message' => t('User not found!'),
Open Source Developer's avatar
Open Source Developer committed
183
                ], Response::HTTP_FORBIDDEN);
184
            }
185 186
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
187
            return $this->respondWithStatus([
188
            'message' => t('Unexpected Error'),
189
            ], Response::HTTP_FORBIDDEN);
190
        }
191
    }
Open Source Developer's avatar
Open Source Developer committed
192

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

199
        $authToken = $request->headers->get('PHP_AUTH_USER');
200
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
201 202 203 204 205 206 207
        $user = reset($users);
        if ($user) {
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
208
                }
209
            }
210
            if ($userRole === 'epal') {
211 212 213 214
                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    $arr = $postData->students;
                    $type = $postData->type;
215 216
                    $valnew = intval($arr);
                    $typen = intval($type);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
                    $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);
234
                        }
235 236 237 238 239
                    } 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
240 241
                }
            } else {
242
                return $this->respondWithStatus(['error_code' => 4003], Response::HTTP_FORBIDDEN);
243 244
            }
        } else {
245
            return $this->respondWithStatus(['message' => t('EPAL user not found')], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
246 247 248
        }
    }

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

258 259 260 261 262 263 264 265 266 267 268 269 270
        $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);
        }

271
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        $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;
291
                }
292 293 294
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
295 296
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
297 298
            } elseif ($userRole === 'epal') {
                $postData = null;
299

300 301 302 303 304 305
                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!'),
306
                        ], Response::HTTP_BAD_REQUEST);
307
                    }
Open Source Developer's avatar
Open Source Developer committed
308

309 310 311 312 313 314
                    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();
315
                        }
316
                    }
Open Source Developer's avatar
Open Source Developer committed
317

318 319 320 321 322 323
                    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();
324
                        }
325
                    }
Open Source Developer's avatar
Open Source Developer committed
326

327 328 329 330 331 332
                    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();
333
                        }
334
                    }
335

336 337 338 339 340 341 342 343
                    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();
                        }
                    }
344

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

361
    public function getSchools(Request $request)
Open Source Developer's avatar
Open Source Developer committed
362 363 364 365 366
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
367 368 369 370 371 372 373
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
374
                }
375
            }
376

377 378
            if ($userRole === '') {
                return $this->respondWithStatus([
379
                    'error_code' => 4003,
Open Source Developer's avatar
Open Source Developer committed
380
                    "message" => t("1")
381
                ], Response::HTTP_FORBIDDEN);
382
            } elseif ($userRole === 'regioneduadmin') {
383 384 385
                $schools = $this->entityTypeManager
                    ->getStorage('eepal_school')
                    ->loadByProperties(array('region_edu_admin_id' => $selectionId));
386
            } elseif ($userRole === 'eduadmin') {
387 388 389 390 391
                $schools = $this->entityTypeManager
                    ->getStorage('eepal_school')
                    ->loadByProperties(array('edu_admin_id' => $selectionId));
            } else {
                $schools = [];
392
            }
393

394 395
            if ($schools) {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
396

397 398 399
                foreach ($schools as $object) {
                    $status = $this->returnstatus($object->id());
                    $list[] = array(
400 401 402 403
                        'id' => $object->id(),
                        'name' => $object->name->value,
                        'status' => $status,
                    );
Open Source Developer's avatar
Open Source Developer committed
404 405
                }

406
                return $this->respondWithStatus($list, Response::HTTP_OK);
407 408
            } else {
                return $this->respondWithStatus([
409 410
                    'message' => t('No schools found!'),
                ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
411
            }
412 413
        } else {
            return $this->respondWithStatus([
414 415
                'message' => t('User not found!'),
            ], Response::HTTP_FORBIDDEN);
416
        }
Open Source Developer's avatar
Open Source Developer committed
417 418
    }

Open Source Developer's avatar
Open Source Developer committed
419 420
    public function getCoursesPerSchool(Request $request, $schoolid)
    {
421
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
422 423 424

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
425 426 427 428 429 430 431
        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
432
                }
433 434 435
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
436 437
                    'error_code' => 4003,
                ], Response::HTTP_FORBIDDEN);
438
            } elseif ($userRole === 'regioneduadmin') {
439 440
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')
                    ->loadByProperties(array('id' => $schoolid, 'region_edu_admin_id' => $newid));
441
            } elseif ($userRole === 'eduadmin') {
442 443
                $SchoolCats = $this->entityTypeManager->getStorage('eepal_school')
                    ->loadByProperties(array('id' => $schoolid, 'edu_admin_id' => $newid));
444
            }
Open Source Developer's avatar
Open Source Developer committed
445

446 447 448
            $SchoolCat = reset($SchoolCats);
            if ($SchoolCat) {
                $categ = $SchoolCat->metathesis_region->value;
449
                $operation_shift = $SchoolCat->operation_shift->value;
Open Source Developer's avatar
Open Source Developer committed
450
                $capacity_class_a = ($SchoolCat -> capacity_class_a ->value) *25;
451 452 453 454
            } else {
                return $this->respondWithStatus([
                    'message' => t('No school located'),
                ], Response::HTTP_FORBIDDEN);
455
            }
Open Source Developer's avatar
Open Source Developer committed
456

457 458 459 460
            $list = array();
            $limit = -1;
            $CourseA = $this->entityTypeManager->getStorage('eepal_school')
                ->loadByProperties(array('id' => $schoolid));
461
            if ($CourseA) {
462 463
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')
                    ->loadByProperties(array('name' => 1, 'category' => $categ));
464 465 466
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
467 468
                } else {
                    $limit = -1;
Open Source Developer's avatar
Open Source Developer committed
469
                }
470 471
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')
                    ->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
472 473 474
                $list = array();
                foreach ($CourseA as $object) {
                    $list[] = array(
475 476 477 478 479 480
                        'id' => '1',
                        'name' => 'Α Λυκείου',
                        'size' => sizeof($studentPerSchool),
                        'categ' => $categ,
                        'classes' => 1,
                        'limitdown' => $limit,
Open Source Developer's avatar
Open Source Developer committed
481 482
                         'capc' => $capacity_class_a,

483
                    );
484 485
                }
            }
486

487 488
            $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')
                ->loadByProperties(array('epal_id' => $schoolid));
489
            if ($CourseB) {
490 491
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')
                    ->loadByProperties(array('name' => 2, 'category' => $categ));
492 493 494
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
495 496
                } else {
                    $limit = -1;
497
                }
Open Source Developer's avatar
Open Source Developer committed
498

499 500
                foreach ($CourseB as $object) {
                    $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
501
                    $capacity_class_b = ($object -> capacity_class_sector ->value) *25;
502 503
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')
                        ->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
504
                    $list[] = array(
505 506 507 508 509 510
                        'id' => $object->sector_id->entity->id(),
                        'name' => 'Β Λυκείου  '.$object->sector_id->entity->get('name')->value,
                        'size' => sizeof($studentPerSchool),
                        'categ' => $categ,
                        'classes' => 2,
                        'limitdown' => $limit,
Open Source Developer's avatar
Open Source Developer committed
511 512
                        'capc' => $capacity_class_b,

513
                    );
Open Source Developer's avatar
Open Source Developer committed
514
                }
515
            }
516 517
            $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')
                ->loadByProperties(array('epal_id' => $schoolid));
518
            if ($CourseC) {
519 520
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')
                    ->loadByProperties(array('name' => 3, 'category' => $categ));
521 522 523
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
524 525
                } else {
                    $limit = -1;
526
                }
Open Source Developer's avatar
Open Source Developer committed
527

528 529
                foreach ($CourseC as $object) {
                    $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
530
                    $capacity_class_c = ($object -> capacity_class_specialty ->value) *25;
531 532
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')
                        ->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
533
                    $list[] = array(
534 535 536 537 538 539
                        'id' => $object->specialty_id->entity->id(),
                        'name' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                        'size' => sizeof($studentPerSchool),
                        'categ' => $categ,
                        'classes' => 3,
                        'limitdown' => $limit,
Open Source Developer's avatar
Open Source Developer committed
540 541
                        'capc' => $capacity_class_c,

542
                    );
Open Source Developer's avatar
Open Source Developer committed
543
                }
544
            }
545
            if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
546 547
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')
                    ->loadByProperties(array('name' => 4, 'category' => $categ));
Open Source Developer's avatar
Open Source Developer committed
548 549 550
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
551 552
                } else {
                    $limit = -1;
Open Source Developer's avatar
Open Source Developer committed
553 554 555 556
                }

                foreach ($CourseC as $object) {
                    $specialityid = $object->specialty_id->entity->id();
557 558
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')
                        ->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 4));
559
                      $capacity_class_d = ($object -> capacity_class_specialty_d ->value) *25;
Open Source Developer's avatar
Open Source Developer committed
560
                    $list[] = array(
561 562 563 564 565 566
                        'id' => $object->specialty_id->entity->id(),
                        'name' => 'Δ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                        'size' => sizeof($studentPerSchool),
                        'categ' => $categ,
                        'classes' => 4,
                        'limitdown' => $limit,
Open Source Developer's avatar
Open Source Developer committed
567
                        'capc' => $capacity_class_d,
568
                    );
Open Source Developer's avatar
Open Source Developer committed
569 570 571
                }
            }

572
            if ($CourseA || $CourseB || $CourseC) {
573
                return $this->respondWithStatus($list, Response::HTTP_OK);
574 575
            } else {
                return $this->respondWithStatus([
576 577
                    'message' => t('No courses found!'),
                ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
578
            }
579 580
        } else {
            return $this->respondWithStatus([
581 582
                'message' => t('User not found!'),
            ], Response::HTTP_FORBIDDEN);
583
        }
Open Source Developer's avatar
Open Source Developer committed
584
    }
Open Source Developer's avatar
Open Source Developer committed
585

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    protected function getLimit($name, $categ)
    {
        static $limits = array();

        $key = "{$name}_{$categ}";
        if (isset($limits[$key])) {
            $limit = $limits[$key];
        } else {
            $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => $name, 'category' => $categ));
            $limitdown = reset($limit_down);
            if ($limitdown) {
                $limit = $limitdown->limit_down->value;
            } else {
                $limit = -1;
            }
            $limits[$key] = $limit;
        }

        return $limit;
    }

607 608 609 610 611 612 613
    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;
Ασπασία Κατσή's avatar
Ασπασία Κατσή committed
614
            $operation_shift = $school -> operation_shift -> value;
615 616
        } else {
            $categ = '-';
Ασπασία Κατσή's avatar
Ασπασία Κατσή committed
617
            $operation_shift ='-';
618
        }
Open Source Developer's avatar
Open Source Developer committed
619

620
        $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $schoolid));
621
        if ($CourseA) {
622
            $limit = $this->getLimit(1, $categ);
623

Open Source Developer's avatar
Open Source Developer committed
624
            $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
625

626 627 628
            if (sizeof($studentPerSchool) < $limit) {
                return false;
            }
629
        }
630

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
        $limit = $this->getLimit(2, $categ);
        $sCon = $this->connection->select('eepal_sectors_in_epal_field_data', 'eSchool');
        $sCon->leftJoin('epal_student_class', 'eStudent',
            'eStudent.epal_id = ' . $schoolid . ' ' .
            'AND eStudent.specialization_id = eSchool.sector_id ' .
            'AND eStudent.currentclass = 2');
        $sCon->fields('eSchool', array('sector_id'))
            ->fields('eStudent', array('specialization_id'))
            ->groupBy('specialization_id')
            ->groupBy('sector_id')
            ->condition('eSchool.epal_id', $schoolid, '=');
        $sCon->addExpression('count(eStudent.id)', 'eStudent_count');

        $results = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

        foreach ($results as $result) {
            if ($result->eStudent_count < $limit) {
                return false;
649 650
            }
        }
651

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
        $limit = $this->getLimit(3, $categ);
        $sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool');
        $sCon->leftJoin('epal_student_class', 'eStudent',
            'eStudent.epal_id = ' . $schoolid . ' ' .
            'AND eStudent.specialization_id = eSchool.specialty_id ' .
            'AND eStudent.currentclass = 3');
        $sCon->fields('eSchool', array('specialty_id'))
            ->fields('eStudent', array('specialization_id'))
            ->groupBy('specialization_id')
            ->groupBy('specialty_id')
            ->condition('eSchool.epal_id', $schoolid, '=');
        $sCon->addExpression('count(eStudent.id)', 'eStudent_count');

        $results = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

        foreach ($results as $result) {
            if ($result->eStudent_count < $limit) {
                return false;
670 671
            }
        }
Open Source Developer's avatar
Open Source Developer committed
672

Ασπασία Κατσή's avatar
Ασπασία Κατσή committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
        if ($operation_shift == 'ΕΣΠΕΡΙΝΟ')
        {
            $limit = $this->getLimit(4, $categ);
            $sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool');
            $sCon->leftJoin('epal_student_class', 'eStudent',
                'eStudent.epal_id = ' . $schoolid . ' ' .
                'AND eStudent.specialization_id = eSchool.specialty_id ' .
                'AND eStudent.currentclass = 4');
            $sCon->fields('eSchool', array('specialty_id'))
                ->fields('eStudent', array('specialization_id'))
                ->groupBy('specialization_id')
                ->groupBy('specialty_id')
                ->condition('eSchool.epal_id', $schoolid, '=');
            $sCon->addExpression('count(eStudent.id)', 'eStudent_count');

            $results = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

            foreach ($results as $result) {
                if ($result->eStudent_count < $limit) {
                    return false;
                }
            }
        }

697 698
        return true;
    }
Open Source Developer's avatar
Open Source Developer committed
699

700

Open Source Developer's avatar
Open Source Developer committed
701
    public function FindCapacityPerSchool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
702
    {
Open Source Developer's avatar
Open Source Developer committed
703
        $i = 0;
704
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
705 706

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
707 708 709 710 711 712 713
        $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());
714
                return $this->respondWithStatus(['message' => 'No access to this school'], Response::HTTP_FORBIDDEN);
715
            }
716 717

            $operation_shift = $school->operation_shift->value;
718 719 720 721 722
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
723
                }
724 725
            }
            if ($userRole === '') {
726
                return $this->respondWithStatus(['error_code' => 4003], Response::HTTP_FORBIDDEN);
727
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
728
                $categ = $school->metathesis_region->value;
729
                $list = array();
730

Open Source Developer's avatar
Open Source Developer committed
731 732 733 734 735 736 737 738 739 740
                $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,
741
                    );
742
                }
Open Source Developer's avatar
Open Source Developer committed
743
                ++$i;
744

Open Source Developer's avatar
Open Source Developer committed
745 746 747 748
                $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();
749

Open Source Developer's avatar
Open Source Developer committed
750 751 752 753 754 755 756 757
                        $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,
758
                                'capacity' => $classcapacity->capacity_class_sector->value,
Open Source Developer's avatar
Open Source Developer committed
759
                                'globalindex' => $i,
760
                            );
Open Source Developer's avatar
Open Source Developer committed
761 762
                        }
                        ++$i;
763 764
                    }
                }
765

Open Source Developer's avatar
Open Source Developer committed
766 767
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
768
                    foreach ($CourseC as $object) {
Open Source Developer's avatar
Open Source Developer committed
769 770 771 772 773 774
                        $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,
775 776 777 778 779
                                '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
780 781 782
                            );
                        }
                        ++$i;
783 784
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
785

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

Open Source Developer's avatar
Open Source Developer committed
790 791 792 793
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $list[] = array(
794 795 796 797 798 799 800
                                '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
801 802
                        }
                        ++$i;
803 804
                    }
                }
805

806
                return $this->respondWithStatus($list, Response::HTTP_OK);
807
            }
808
        } else {
809
            return $this->respondWithStatus(['message' => t('EPAL user not found')], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
Open Source Developer committed
810 811
        }
    }
812

Open Source Developer's avatar
Open Source Developer committed
813
    public function FindCoursesPerSchool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
814
    {
815
        $i = 0;
816
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
817 818

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
819 820 821 822 823 824 825 826 827 828 829 830 831 832
        $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;
            }
833
            $operation_shift = $school -> operation_shift -> value;
834 835 836 837 838
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
839
                }
840 841 842
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
843 844
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
845
            } elseif ($userRole === 'epal') {
846
                $categ = $school->metathesis_region->value;
847
                $list = array();
848

849
                $CourseA = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
Open Source Developer's avatar
Open Source Developer committed
850
                if ($CourseA) {
851 852 853 854 855 856
                    $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));
857

Open Source Developer's avatar
Open Source Developer committed
858 859
                    $list[] = array(
                        'class' => 1,
860 861
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
862
                        'taxi' => 'Ά Λυκείου',
863
                        'globalindex' => $i,
864 865
                        'limitdown' => $limit,
                        'size' => sizeof($studentPerSchool),
Open Source Developer's avatar
Open Source Developer committed
866
                       );
867 868
                }
                ++$i;
869

870 871
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseB) {
872 873 874 875 876 877
                    $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;
                    }

878 879
                    foreach ($CourseB as $object) {
                        $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
880
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
881 882 883 884 885 886 887 888 889
                        $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),
                            );
890

891
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
892
                    }
893
                }
894

895 896
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
897 898
                    $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
                        $limitdown = reset($limit_down);
899 900 901 902
                    if ($limitdown) {
                        $limit = $limitdown->limit_down->value;
                    }
                    foreach ($CourseC as $object) {
903
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
904
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
905
                        $list[] = array(
906
                            'class' => 3,
907 908 909 910 911 912 913
                            'newsector' => 0,
                            'newspecialit' => $object->specialty_id->entity->id(),
                            'taxi' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                            'globalindex' => $i,
                            'limitdown' => $limit,
                            'size' => sizeof($studentPerSchool),
                        );
914
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
915
                    }
916
                }
917

918 919 920
                if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
                        $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 4, 'category' => $categ));
                        $limitdown = reset($limit_down);
921 922 923
                    if ($limitdown) {
                        $limit = $limitdown->limit_down->value;
                    }
924 925
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
Open Source Developer committed
926
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 4));
927 928 929 930 931 932 933 934 935
                        $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),
                        );
936 937 938
                        ++$i;
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
939

940
                return $this->respondWithStatus($list, Response::HTTP_OK);
941 942
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
943
            return $this->respondWithStatus([
944
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
945 946 947 948
                ], Response::HTTP_FORBIDDEN);
        }
    }

949 950
    private function respondWithStatus($arr, $s)
    {
Open Source Developer's avatar
Open Source Developer committed
951 952
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
953

Open Source Developer's avatar
Open Source Developer committed
954 955 956
        return $res;
    }
}