DirectorView.php 43.9 KB
Newer Older
Open Source Developer's avatar
transf  
Open Source Developer committed
1 2 3 4 5 6 7 8 9 10
<?php

namespace Drupal\epal\Controller;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Open Source Developer's avatar
trans  
Open Source Developer committed
11
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
12
use Drupal\Core\Database\Connection;
Open Source Developer's avatar
transf  
Open Source Developer committed
13

14 15
use Drupal\epal\Crypt;

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

22 23
    public function __construct(
        EntityTypeManagerInterface $entityTypeManager,
24
        Connection $connection,
25 26
        LoggerChannelFactoryInterface $loggerChannel
    ) {
Open Source Developer's avatar
transf  
Open Source Developer committed
27
        $this->entityTypeManager = $entityTypeManager;
28
        $this->connection = $connection;
29
        $this->logger = $loggerChannel->get('epal-school');
Open Source Developer's avatar
transf  
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
transf  
Open Source Developer committed
38
        );
39
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
40

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

46 47 48 49 50 51 52 53 54 55 56
            $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);
Open Source Developer's avatar
Open Source Developer committed
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);
Open Source Developer's avatar
Open Source Developer committed
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([
Open Source Developer's avatar
Open Source Developer committed
81 82
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
83
                } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
84
                    $selectIdNew = $selectId;
85
                    if ($classId == 1) {
Open Source Developer's avatar
Open Source Developer committed
86
                        $selectIdNew = -1;
87
                    } elseif ($classId == 2) {
Open Source Developer's avatar
Open Source Developer committed
88
                        $selectIdNew = $sector;
89
                    } else {
Open Source Developer's avatar
Open Source Developer committed
90
                        $selectIdNew = $specialit;
91
                    }
Open Source Developer's avatar
bug  
Open Source Developer committed
92
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $epalId, 'specialization_id' => $selectIdNew, 'currentclass' => $classId));
93 94 95 96
                }
                if ($studentPerSchool) {
                    $list = array();
                    foreach ($studentPerSchool as $object) {
97
                        $studentId = $object->student_id->target_id;
98 99
                        $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $studentId));
                        $epalStudent = reset($epalStudents);
100 101 102 103 104 105 106 107 108 109 110
                        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;
Open Source Developer's avatar
Open Source Developer committed
111
                                }
112 113 114 115 116
                            } 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;
Open Source Developer's avatar
Open Source Developer committed
117
                                }
118 119
                            }

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

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

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

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

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

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

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

                return $response;
            }
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
292
                }
293 294 295
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
296 297
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
298 299
            } elseif ($userRole === 'epal') {
                $postData = null;
Open Source Developer's avatar
Open Source Developer committed
300

301 302 303 304 305 306
                if ($content = $request->getContent()) {
                    $postData = json_decode($content);
                    $cap = $postData->capacity;
                    if ($cap <= 0 || $cap > 99) {
                        return $this->respondWithStatus([
                            'message' => t('Number out of limits!'),
Open Source Developer's avatar
Open Source Developer committed
307
                        ], Response::HTTP_BAD_REQUEST);
308
                    }
Open Source Developer's avatar
Open Source Developer committed
309

310 311 312 313 314 315
                    if (($tomeas == 0) && ($specialit == 0)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_a', $cap);
                            $classcapacity->save();
Open Source Developer's avatar
Open Source Developer committed
316
                        }
317
                    }
Open Source Developer's avatar
Open Source Developer committed
318

319 320 321 322 323 324
                    if (($tomeas != 0) && ($specialit == 0)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'sector_id' => $tomeas));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_sector', $cap);
                            $classcapacity->save();
Open Source Developer's avatar
Open Source Developer committed
325
                        }
326
                    }
Open Source Developer's avatar
Open Source Developer committed
327

328 329 330 331 332 333
                    if (($specialit != 0) && ($taxi == 3)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialit));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_specialty', $cap);
                            $classcapacity->save();
Open Source Developer's avatar
Open Source Developer committed
334
                        }
335
                    }
Open Source Developer's avatar
Open Source Developer committed
336

337 338 339 340 341 342 343 344
                    if (($specialit != 0) && ($taxi == 4)) {
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialit));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $classcapacity->set('capacity_class_specialty_d', $cap);
                            $classcapacity->save();
                        }
                    }
Open Source Developer's avatar
Open Source Developer committed
345

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

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

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
368 369 370 371 372 373 374
        if ($user) {
            $selectionId = $user->init->value;
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if (($tmpRole === 'epal') || ($tmpRole === 'regioneduadmin') || ($tmpRole === 'eduadmin')) {
                    $userRole = $tmpRole;
375
                }
376 377 378
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
379
                    'error_code' => 4003,
Open Source Developer's avatar
ch  
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
            }
            if ($schools) {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
395

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

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

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

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

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

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

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

495 496
                foreach ($CourseB as $object) {
                    $sectorid = $object->sector_id->entity->id();
497 498
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')
                        ->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
499
                    $list[] = array(
500 501 502 503 504 505 506
                        '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
trans  
Open Source Developer committed
507
                }
508
            }
509 510
            $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')
                ->loadByProperties(array('epal_id' => $schoolid));
511
            if ($CourseC) {
512 513
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')
                    ->loadByProperties(array('name' => 3, 'category' => $categ));
514 515 516
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
517 518
                } else {
                    $limit = -1;
519
                }
Open Source Developer's avatar
Open Source Developer committed
520

521 522
                foreach ($CourseC as $object) {
                    $specialityid = $object->specialty_id->entity->id();
523 524
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')
                        ->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
525
                    $list[] = array(
526 527 528 529 530 531 532
                        '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
trans  
Open Source Developer committed
533
                }
534
            }
535
            if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
536 537
                $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')
                    ->loadByProperties(array('name' => 4, 'category' => $categ));
Open Source Developer's avatar
trans  
Open Source Developer committed
538 539 540
                $limitdown = reset($limit_down);
                if ($limitdown) {
                    $limit = $limitdown->limit_down->value;
541 542
                } else {
                    $limit = -1;
Open Source Developer's avatar
trans  
Open Source Developer committed
543 544 545 546
                }

                foreach ($CourseC as $object) {
                    $specialityid = $object->specialty_id->entity->id();
547 548
                    $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')
                        ->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 4));
Open Source Developer's avatar
trans  
Open Source Developer committed
549 550

                    $list[] = array(
551 552 553 554 555 556 557
                        '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
trans  
Open Source Developer committed
558 559 560
                }
            }

561
            if ($CourseA || $CourseB || $CourseC) {
562
                return $this->respondWithStatus($list, Response::HTTP_OK);
563 564
            } else {
                return $this->respondWithStatus([
565 566
                    'message' => t('No courses found!'),
                ], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
trans  
Open Source Developer committed
567
            }
568 569
        } else {
            return $this->respondWithStatus([
570 571
                'message' => t('User not found!'),
            ], Response::HTTP_FORBIDDEN);
572
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
573
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
574

575 576 577 578 579 580 581
    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;
582 583
        } else {
            $categ = '-';
584
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
585

586
        $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $schoolid));
587 588 589 590 591
        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;
592 593
            } else {
                $limit = -1;
594
            }
595

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

598 599 600
            if (sizeof($studentPerSchool) < $limit) {
                return false;
            }
601
        }
602

603
        $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
604 605 606 607 608
        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;
609 610
            } else {
                $limit = -1;
611
            }
612

613 614
            foreach ($CourseB as $object) {
                $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
bug  
Open Source Developer committed
615
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
616 617
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
Open Source Developer's avatar
Open Source Developer committed
618
                }
619 620
            }
        }
621

622 623 624 625 626 627
        $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;
628 629
            } else {
                $limit = -1;
630
            }
Open Source Developer's avatar
Open Source Developer committed
631

632 633
            foreach ($CourseC as $object) {
                $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
bug  
Open Source Developer committed
634
                $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
635

636 637
                if (sizeof($studentPerSchool) < $limit) {
                    return false;
Open Source Developer's avatar
Open Source Developer committed
638
                }
639 640
            }
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
641

642 643
        return true;
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
644

645

Open Source Developer's avatar
changes  
Open Source Developer committed
646
    public function FindCapacityPerSchool(Request $request)
Open Source Developer's avatar
trans  
Open Source Developer committed
647
    {
Open Source Developer's avatar
changes  
Open Source Developer committed
648
        $i = 0;
649
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
trans  
Open Source Developer committed
650 651

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
652 653 654 655 656 657 658
        $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());
659
                return $this->respondWithStatus(['message' => 'No access to this school'], Response::HTTP_FORBIDDEN);
660
            }
661 662

            $operation_shift = $school->operation_shift->value;
663 664 665 666 667
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
trans  
Open Source Developer committed
668
                }
669 670
            }
            if ($userRole === '') {
671
                return $this->respondWithStatus(['error_code' => 4003], Response::HTTP_FORBIDDEN);
672
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
changes  
Open Source Developer committed
673
                $categ = $school->metathesis_region->value;
674
                $list = array();
675

Open Source Developer's avatar
changes  
Open Source Developer committed
676 677 678 679 680 681 682 683 684 685
                $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,
686
                    );
687
                }
Open Source Developer's avatar
changes  
Open Source Developer committed
688
                ++$i;
689

Open Source Developer's avatar
changes  
Open Source Developer committed
690 691 692 693
                $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();
694

Open Source Developer's avatar
changes  
Open Source Developer committed
695 696 697 698 699 700 701 702
                        $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,
703
                                'capacity' => $classcapacity->capacity_class_sector->value,
Open Source Developer's avatar
changes  
Open Source Developer committed
704
                                'globalindex' => $i,
705
                            );
Open Source Developer's avatar
changes  
Open Source Developer committed
706 707
                        }
                        ++$i;
708 709
                    }
                }
710

Open Source Developer's avatar
changes  
Open Source Developer committed
711 712
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
713
                    foreach ($CourseC as $object) {
Open Source Developer's avatar
changes  
Open Source Developer committed
714 715 716 717 718 719
                        $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,
720 721 722 723 724
                                '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
changes  
Open Source Developer committed
725 726 727
                            );
                        }
                        ++$i;
728 729
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
730

Open Source Developer's avatar
changes  
Open Source Developer committed
731 732 733
                if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
734

Open Source Developer's avatar
changes  
Open Source Developer committed
735 736 737 738
                        $CapacityPerClass = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid, 'specialty_id' => $specialityid));
                        $classcapacity = reset($CapacityPerClass);
                        if ($classcapacity) {
                            $list[] = array(
739 740 741 742 743 744 745
                                '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
changes  
Open Source Developer committed
746 747
                        }
                        ++$i;
748 749
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
750

751
                return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
752
            }
753
        } else {
754
            return $this->respondWithStatus(['message' => t('EPAL user not found')], Response::HTTP_FORBIDDEN);
Open Source Developer's avatar
trans  
Open Source Developer committed
755 756
        }
    }
757

Open Source Developer's avatar
changes  
Open Source Developer committed
758
    public function FindCoursesPerSchool(Request $request)
Open Source Developer's avatar
Open Source Developer committed
759
    {
Open Source Developer's avatar
Open Source Developer committed
760
        $i = 0;
761
        $authToken = $request->headers->get('PHP_AUTH_USER');
Open Source Developer's avatar
Open Source Developer committed
762 763

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
764 765 766 767 768 769 770 771 772 773 774 775 776 777
        $user = reset($users);
        if ($user) {
            $schoolid = $user->init->value;
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
            $school = reset($schools);
            if (!$school) {
                $this->logger->warning('no access to this school='.$user->id());
                $response = new Response();
                $response->setContent('No access to this school');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');

                return $response;
            }
Open Source Developer's avatar
Open Source Developer committed
778
            $operation_shift = $school -> operation_shift -> value;
779 780 781 782 783
            $userRoles = $user->getRoles();
            $userRole = '';
            foreach ($userRoles as $tmpRole) {
                if ($tmpRole === 'epal') {
                    $userRole = $tmpRole;
Open Source Developer's avatar
Open Source Developer committed
784
                }
785 786 787
            }
            if ($userRole === '') {
                return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
788 789
                             'error_code' => 4003,
                         ], Response::HTTP_FORBIDDEN);
790
            } elseif ($userRole === 'epal') {
Open Source Developer's avatar
Open Source Developer committed
791
                $categ = $school->metathesis_region->value;
792
                $list = array();
793

794
                $CourseA = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $schoolid));
Open Source Developer's avatar
changes  
Open Source Developer committed
795
                if ($CourseA) {
796 797 798 799 800 801
                    $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));
Open Source Developer's avatar
Open Source Developer committed
802

Open Source Developer's avatar
Open Source Developer committed
803 804
                    $list[] = array(
                        'class' => 1,
Open Source Developer's avatar
Open Source Developer committed
805 806
                        'newsector' => 0,
                        'newspecialit' => 0,
Open Source Developer's avatar
Open Source Developer committed
807
                        'taxi' => 'Ά Λυκείου',
808
                        'globalindex' => $i,
Open Source Developer's avatar
Open Source Developer committed
809 810
                        'limitdown' => $limit,
                        'size' => sizeof($studentPerSchool),
Open Source Developer's avatar
Open Source Developer committed
811
                       );
812 813
                }
                ++$i;
814

815 816
                $CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseB) {
Open Source Developer's avatar
Open Source Developer committed
817 818 819 820 821 822
                    $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;
                    }

823 824
                    foreach ($CourseB as $object) {
                        $sectorid = $object->sector_id->entity->id();
Open Source Developer's avatar
bug  
Open Source Developer committed
825
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
826 827 828 829 830 831 832 833 834
                        $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),
                            );
835

836
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
837
                    }
838
                }
839

840 841
                $CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid));
                if ($CourseC) {
Open Source Developer's avatar
Open Source Developer committed
842 843
                    $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ));
                        $limitdown = reset($limit_down);
844 845 846 847
                    if ($limitdown) {
                        $limit = $limitdown->limit_down->value;
                    }
                    foreach ($CourseC as $object) {
848
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
bug  
Open Source Developer committed
849
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
850
                        $list[] = array(
851
                            'class' => 3,
852 853 854 855 856 857 858
                            'newsector' => 0,
                            'newspecialit' => $object->specialty_id->entity->id(),
                            'taxi' => 'Γ Λυκείου  '.$object->specialty_id->entity->get('name')->value,
                            'globalindex' => $i,
                            'limitdown' => $limit,
                            'size' => sizeof($studentPerSchool),
                        );
859
                        ++$i;
Open Source Developer's avatar
Open Source Developer committed
860
                    }
861
                }
862

Open Source Developer's avatar
Open Source Developer committed
863 864 865
                if ($CourseC && $operation_shift != 'ΗΜΕΡΗΣΙΟ') {
                        $limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 4, 'category' => $categ));
                        $limitdown = reset($limit_down);
866 867 868
                    if ($limitdown) {
                        $limit = $limitdown->limit_down->value;
                    }
869 870
                    foreach ($CourseC as $object) {
                        $specialityid = $object->specialty_id->entity->id();
Open Source Developer's avatar
bug  
Open Source Developer committed
871
                        $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 4));
872 873 874 875 876 877 878 879 880
                        $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),
                        );
881 882 883
                        ++$i;
                    }
                }
Open Source Developer's avatar
Open Source Developer committed
884

885
                return $this->respondWithStatus($list, Response::HTTP_OK);
886 887
            }
        } else {
Open Source Developer's avatar
Open Source Developer committed
888
            return $this->respondWithStatus([
889
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
890 891 892 893
                ], Response::HTTP_FORBIDDEN);
        }
    }

894 895
    private function respondWithStatus($arr, $s)
    {
Open Source Developer's avatar
transf  
Open Source Developer committed
896 897
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
898

Open Source Developer's avatar
transf  
Open Source Developer committed
899 900 901
        return $res;
    }
}