SubmitedApplications.php 32.4 KB
Newer Older
Open Source Developer's avatar
trans  
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;
11
use Drupal\Core\Database\Connection;
12 13 14
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\epal\Crypt;

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

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

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    public function deleteApplication(Request $request)
    {
        if (!$request->isMethod('POST')) {
            return $this->respondWithStatus([
                    "error_code" => 2001
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }

        $content = $request->getContent();

        $applicationId = 0;
        if (!empty($content)) {
            $postArr = json_decode($content, TRUE);
            $applicationId = $postArr['applicationId'];
        }
        else {
            return $this->respondWithStatus([
                    "error_code" => 5002
                ], Response::HTTP_BAD_REQUEST);
        }

61 62

        $authToken = $request->headers->get('PHP_AUTH_USER');
63 64
        $transaction = $this->connection->startTransaction();
        try {
65 66 67 68 69 70 71 72 73 74 75 76 77 78
            //ανάκτηση τιμής από ρυθμίσεις διαχειριστή για lock_results
            $config_storage = $this->entityTypeManager->getStorage('epal_config');
            $epalConfigs = $config_storage->loadByProperties(array('name' => 'epal_config'));
            $epalConfig = reset($epalConfigs);
            if (!$epalConfig) {
               return $this->respondWithStatus([
                       'message' => t("EpalConfig Enity not found"),
                   ], Response::HTTP_FORBIDDEN);
            }
            else if ($epalConfig->lock_application->value) {
                return $this->respondWithStatus([
                        "error_code" => 3002
                    ], Response::HTTP_FORBIDDEN);
            }
79 80 81 82
            $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
            $epalUser = reset($epalUsers);
            if ($epalUser) {
                $userid = $epalUser->id();
83

84 85 86 87 88 89 90 91 92 93 94 95
                $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid, 'id' => $applicationId));
                $epalStudent = reset($epalStudents);


                if ($epalStudent) {
                    $epalStudentClasses = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('student_id' => $applicationId));
                    $epalStudentClass = reset($epalStudentClasses);
                    if ($epalStudentClass) {
                        return $this->respondWithStatus([
                                "error_code" => 3002
                            ], Response::HTTP_FORBIDDEN);
                    }
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                    $delQuery = $this->connection->delete('epal_student_epal_chosen');
                    $delQuery->condition('student_id', $applicationId);
                    $delQuery->execute();
                    $delQuery = $this->connection->delete('epal_student_sector_field');
                    $delQuery->condition('student_id', $applicationId);
                    $delQuery->execute();
                    $delQuery = $this->connection->delete('epal_student_course_field');
                    $delQuery->condition('student_id', $applicationId);
                    $delQuery->execute();
                    $delQuery = $this->connection->delete('epal_student_class');
                    $delQuery->condition('student_id', $applicationId);
                    $delQuery->execute();
                    $epalStudent->delete();
                    return $this->respondWithStatus([
                      'error_code' => 0,
                  ], Response::HTTP_OK);

                } else {
                    return $this->respondWithStatus([
                    'message' => t('EPAL student not found'),
                ], Response::HTTP_FORBIDDEN);
                }
119 120
            } else {
                return $this->respondWithStatus([
121
                'message' => t('EPAL user not found'),
122 123
                ], Response::HTTP_FORBIDDEN);
            }
124 125 126
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
            $transaction->rollback();
127

128 129 130 131
            return $this->respondWithStatus([
                'error_code' => 5001,
            ], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
132 133
    }

Open Source Developer's avatar
trans  
Open Source Developer committed
134 135
    public function getSubmittedApplications(Request $request)
    {
Open Source Developer's avatar
Open Source Developer committed
136 137 138 139
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
140
            $userid = $epalUser->id();
141

Open Source Developer's avatar
Open Source Developer committed
142 143
            $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid));
            $i = 0;
144
            $list = array();
Open Source Developer's avatar
Open Source Developer committed
145
            if ($epalStudents) {
146 147
                $crypt = new Crypt();

148

149 150 151 152 153 154 155 156 157 158
                foreach ($epalStudents as $object) {
                    $canDelete = 0;
                    $epalStudentClasses = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('student_id' => $object->id()));
                    $epalStudentClass = reset($epalStudentClasses);
                    if ($epalStudentClass) {
                        $canDelete = 0;
                    }
                    else {
                        $canDelete = 1;
                    }
159 160 161 162
                    try {
                        $name_decoded = $crypt->decrypt($object->name->value);
                        $studentsurname_decoded = $crypt->decrypt($object->studentsurname->value);
                    } catch (\Exception $e) {
163 164
                        unset($crypt);
                        $this->logger->warning($e->getMessage());
165

166
                        return $this->respondWithStatus([
167
                          'message' => t('An unexpected error occured during DECODING data in getSubmittedApplications Method '),
168 169 170
                        ], Response::HTTP_INTERNAL_SERVER_ERROR);
                    }

Open Source Developer's avatar
Open Source Developer committed
171
                    $list[] = array(
172
                            'id' => $object->id(),
173 174 175
                            //'name' => $object -> name ->value,
                            'name' => $name_decoded,
                            //'studentsurname' => $object -> studentsurname ->value);
176 177
                            'studentsurname' => $studentsurname_decoded,
                            'candelete' => $canDelete, );
178
                    ++$i;
Open Source Developer's avatar
Open Source Developer committed
179
                }
180

181 182
                unset($crypt);

Open Source Developer's avatar
Open Source Developer committed
183
                return $this->respondWithStatus(
184 185
                        $list, Response::HTTP_OK);
            } else {
186 187
                return $this->respondWithStatus(
                        $list, Response::HTTP_OK);
188
            }
Open Source Developer's avatar
Open Source Developer committed
189 190
        } else {
            return $this->respondWithStatus([
191
                    'message' => t('User not found'),
Open Source Developer's avatar
Open Source Developer committed
192 193 194 195
                ], Response::HTTP_FORBIDDEN);
        }
    }

196
    public function getStudentApplications(Request $request, $studentId)
Open Source Developer's avatar
Open Source Developer committed
197 198 199 200 201
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
202 203 204 205
            $userid = $epalUser->id();
            $studentIdNew = intval($studentId);
            $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid, 'id' => $studentIdNew));
            $i = 0;
206

207 208
            if ($epalStudents) {
                $list = array();
209

Open Source Developer's avatar
Open Source Developer committed
210
                foreach ($epalStudents as $object) {
211 212 213 214 215
                    $sectorName = '';
                    $courseName = '';
                    if ($object->currentclass->value === '2') {
                        $sectors = $this->entityTypeManager->getStorage('epal_student_sector_field')->loadByProperties(array('student_id' => $object->id()));
                        $sector = reset($sectors);
216
                        if ($sector) {
217
                            $sectorName = $this->entityTypeManager->getStorage('eepal_sectors')->load($sector->sectorfield_id->target_id)->name->value;
218 219
                        }
                    } elseif ($object->currentclass->value === '3' || $object->currentclass->value === '4') {
220 221
                        $courses = $this->entityTypeManager->getStorage('epal_student_course_field')->loadByProperties(array('student_id' => $object->id()));
                        $course = reset($courses);
222
                        if ($course) {
223
                            $courseName = $this->entityTypeManager->getStorage('eepal_specialty')->load($course->coursefield_id->target_id)->name->value;
224
                        }
225
                    }
226 227

                    $crypt = new Crypt();
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                    try {
                        $name_decoded = $crypt->decrypt($object->name->value);
                        $studentsurname_decoded = $crypt->decrypt($object->studentsurname->value);
                        $fatherfirstname_decoded = $crypt->decrypt($object->fatherfirstname->value);
                        $motherfirstname_decoded = $crypt->decrypt($object->motherfirstname->value);
                        $regionaddress_decoded = $crypt->decrypt($object->regionaddress->value);
                        $regiontk_decoded = $crypt->decrypt($object->regiontk->value);
                        $regionarea_decoded = $crypt->decrypt($object->regionarea->value);
                        $relationtostudent_decoded = $crypt->decrypt($object->relationtostudent->value);
                        $telnum_decoded = $crypt->decrypt($object->telnum->value);
                        $guardian_name_decoded = $crypt->decrypt($object->guardian_name->value);
                        $guardian_surname_decoded = $crypt->decrypt($object->guardian_surname->value);
                        $guardian_fathername_decoded = $crypt->decrypt($object->guardian_fathername->value);
                        $guardian_mothername_decoded = $crypt->decrypt($object->guardian_mothername->value);
                    } catch (\Exception $e) {
243 244
                        //print_r($e->getMessage());
                        unset($crypt);
245 246 247 248 249
                        $this->logger->warning($e->getMessage());

                        return $this->respondWithStatus([
                            'message' => t('An unexpected error occured during DECODING data in getStudentApplications Method '),
                                   ], Response::HTTP_INTERNAL_SERVER_ERROR);
250 251 252
                    }
                    unset($crypt);

Open Source Developer's avatar
Open Source Developer committed
253
                    $list[] = array(
254
                            'applicationId' => $object->id(),
255 256 257
                            'name' => $name_decoded,
                            'studentsurname' => $studentsurname_decoded,
                            'fatherfirstname' => $fatherfirstname_decoded,
258
                            'fathersurname' => $object->fathersurname->value,
259
                            'motherfirstname' => $motherfirstname_decoded,
260 261
                            'mothersurname' => $object->mothersurname->value,
                            'guardian_name' => $guardian_name_decoded,
262
                            'guardian_surname' => $guardian_surname_decoded,
263 264 265 266 267 268 269 270 271 272 273 274
                            'guardian_fathername' => $guardian_fathername_decoded,
                            'guardian_mothername' => $guardian_mothername_decoded,
                            'lastschool_schoolname' => $object->lastschool_schoolname->value,
                            'lastschool_schoolyear' => $object->lastschool_schoolyear->value,
                            'lastschool_class' => $object->lastschool_class->value,
                            'currentclass' => $object->currentclass->value,
                            'currentsector' => $sectorName,
                            'currentcourse' => $courseName,
                            'regionaddress' => $regionaddress_decoded,
                            'regiontk' => $regiontk_decoded,
                            'regionarea' => $regionarea_decoded,
                            'telnum' => $telnum_decoded,
275
                            'relationtostudent' => $relationtostudent_decoded,
276 277
                            'birthdate' => substr($object->birthdate->value, 8, 2).'/'.substr($object->birthdate->value, 5, 2).'/'.substr($object->birthdate->value, 0, 4),
                            'created' => date('d/m/Y H:i', $object->created->value),
278

Open Source Developer's avatar
trans  
Open Source Developer committed
279
                        );
Open Source Developer's avatar
Open Source Developer committed
280

281
                    ++$i;
Open Source Developer's avatar
Open Source Developer committed
282
                }
283

Open Source Developer's avatar
Open Source Developer committed
284
                return $this->respondWithStatus(
285 286 287 288
                        $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
289
                ], Response::HTTP_FORBIDDEN);
290
            }
Open Source Developer's avatar
Open Source Developer committed
291 292
        } else {
            return $this->respondWithStatus([
293
                    'message' => t('User not found!!!!'),
Open Source Developer's avatar
Open Source Developer committed
294 295 296 297
                ], Response::HTTP_FORBIDDEN);
        }
    }

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

    public function getApplicationDetails(Request $request, $studentId)
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {

            $config_storage = $this->entityTypeManager->getStorage('epal_config');
            $epalConfigs = $config_storage->loadByProperties(array('name' => 'epal_config'));
            $epalConfig = reset($epalConfigs);
            if (!$epalConfig) {
               return $this->respondWithStatus([
                       'message' => t("EpalConfig Enity not found"),
                   ], Response::HTTP_FORBIDDEN);
            }
            else {
               $applicantsResultsDisabled = $epalConfig->lock_results->value;
316
               //$secondPeriodEnabled = $epalConfig->activate_second_period->value;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            }

            $status = "0";
            $schoolName = '';
            $schoolAddress = '';
            $schoolTel = '';

            $esQuery = $this->connection->select('epal_student', 'es')
                                    ->fields('es',
                                    array('name',
                                            'studentsurname',
                                            'fatherfirstname',
                                            'motherfirstname',
                                            'regionaddress',
                                            'regiontk',
                                            'regionarea',
                                            'relationtostudent',
                                            'telnum',
                                            'guardian_name',
                                            'guardian_surname',
                                            'guardian_fathername',
                                            'guardian_mothername',
                                            'id',
                                            'lastschool_schoolname',
                                            'lastschool_schoolyear',
                                            'lastschool_class',
                                            'currentclass',
                                            'birthdate',
                                            'created',
346 347

                                            'second_period',
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
                                        ))
                                    ->fields('esec',
                                    array('choice_no'
                                    ))
                                    ->fields('eesch',
                                    array('name'
                                    ))
                                    ->fields('eese',
                                    array('name',
                                    ))
                                    ->fields('eesp',
                                    array('name',
                                    ))
                                    ->fields('esc',
                                    array('finalized',
                                    ))
                                    ->fields('eeschfin',
                                    array('id',
                                            'name',
                                            'street_address',
                                            'phone_number'
                                    ));

            $esQuery->addJoin('left outer', 'epal_student_epal_chosen', 'esec', 'es.id=esec.student_id');
            $esQuery->addJoin('left outer', 'eepal_school_field_data', 'eesch', 'esec.epal_id=eesch.id');
            $esQuery->addJoin('left outer', 'epal_student_sector_field', 'essf', 'es.id=essf.student_id');
            $esQuery->addJoin('left outer', 'eepal_sectors_field_data', 'eese', 'essf.sectorfield_id=eese.id');
            $esQuery->addJoin('left outer', 'epal_student_course_field', 'escf', 'es.id=escf.student_id');
            $esQuery->addJoin('left outer', 'eepal_specialty_field_data', 'eesp', 'escf.coursefield_id=eesp.id');
            $esQuery->addJoin('left outer', 'epal_student_class', 'esc', 'es.id=esc.student_id');
            $esQuery->addJoin('left outer', 'eepal_school_field_data', 'eeschfin', 'esc.epal_id=eeschfin.id');
            $esQuery->condition('es.id', intval($studentId), '=');
            $esQuery->condition('es.epaluser_id', $epalUser->id(), '=');
            $esQuery->orderBy('esec.choice_no');

            $epalStudents = $esQuery->execute()->fetchAll(\PDO::FETCH_OBJ);

            if ($epalStudents && sizeof($epalStudents) > 0) {
                $epalSchoolsChosen = array();
                foreach ($epalStudents as $es) {
                    array_push($epalSchoolsChosen, array(
                        'epal_id' => $es->eesch_name,
                        'choice_no' => $es->choice_no,
                      ));
                }

                $epalStudent = reset($epalStudents);
                $list = array();

                    $crypt = new Crypt();
                    try {
                        $name_decoded = $crypt->decrypt($epalStudent->name);
                        $studentsurname_decoded = $crypt->decrypt($epalStudent->studentsurname);
                        $fatherfirstname_decoded = $crypt->decrypt($epalStudent->fatherfirstname);
                        $motherfirstname_decoded = $crypt->decrypt($epalStudent->motherfirstname);
                        $regionaddress_decoded = $crypt->decrypt($epalStudent->regionaddress);
                        $regiontk_decoded = $crypt->decrypt($epalStudent->regiontk);
                        $regionarea_decoded = $crypt->decrypt($epalStudent->regionarea);
                        $relationtostudent_decoded = $crypt->decrypt($epalStudent->relationtostudent);
                        $telnum_decoded = $crypt->decrypt($epalStudent->telnum);
                        $guardian_name_decoded = $crypt->decrypt($epalStudent->guardian_name);
                        $guardian_surname_decoded = $crypt->decrypt($epalStudent->guardian_surname);
                        $guardian_fathername_decoded = $crypt->decrypt($epalStudent->guardian_fathername);
                        $guardian_mothername_decoded = $crypt->decrypt($epalStudent->guardian_mothername);
                    } catch (\Exception $e) {
                        unset($crypt);
                        $this->logger->warning($e->getMessage());

                        return $this->respondWithStatus([
                            'message' => t('An unexpected error occured during DECODING data in getStudentApplications Method '),
                                   ], Response::HTTP_INTERNAL_SERVER_ERROR);
                    }
                    unset($crypt);

                    if ($epalStudent->finalized === null)  {
                        $status = "0";
                    }
                    else if ($epalStudent->finalized === "1")  {
                        $status = "1";
                    } else  {
                        $status = "2";
                    }
                    $list[] = array(
                            'applicationId' => $epalStudent->id,
                            'name' => $name_decoded,
                            'studentsurname' => $studentsurname_decoded,
                            'fatherfirstname' => $fatherfirstname_decoded,
                            'fathersurname' => $epalStudent->fathersurname,
                            'motherfirstname' => $motherfirstname_decoded,
                            'mothersurname' => $epalStudent->mothersurname,
                            '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,
                            'lastschool_schoolyear' => $epalStudent->lastschool_schoolyear,
                            'lastschool_class' => $epalStudent->lastschool_class,
                            'currentclass' => $epalStudent->currentclass,
                            'currentsector' => $epalStudent->eese_name,
                            'currentcourse' => $epalStudent->eesp_name,
                            'regionaddress' => $regionaddress_decoded,
                            'regiontk' => $regiontk_decoded,
                            'regionarea' => $regionarea_decoded,
                            'telnum' => $telnum_decoded,
                            'relationtostudent' => $relationtostudent_decoded,
                            'birthdate' => substr($epalStudent->birthdate, 8, 2).'/'.substr($epalStudent->birthdate, 5, 2).'/'.substr($epalStudent->birthdate, 0, 4),
                            'created' => date('d/m/Y H:i', $epalStudent->created),
                            'epalSchoolsChosen' => $epalSchoolsChosen,
                            'applicantsResultsDisabled' => $applicantsResultsDisabled,
                            'status' => $status,
                            'schoolName' => $epalStudent->eeschfin_name,
                            'schoolAddress' => $epalStudent->street_address,
                            'schoolTel' => $epalStudent->phone_number,
461 462
                            'secondPeriod' => $epalStudent->second_period,
                            //'secondPeriodSettingEnabled' => $secondPeriodEnabled,
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
                        );

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

479
    public function getEpalChosen(Request $request, $studentId)
Open Source Developer's avatar
Open Source Developer committed
480 481 482 483 484
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
485 486 487 488 489 490 491 492 493 494 495
            $userid = $epalUser->user_id->entity->id();
            $studentIdNew = intval($studentId);
            $ecQuery = $this->connection->select('epal_student_epal_chosen', 'ec')
                                                                    ->fields('ec', array('choice_no'))
                                                                    ->fields('es', array('name'));
            $ecQuery->addJoin('inner', 'eepal_school_field_data', 'es', 'ec.epal_id=es.id');
            $ecQuery->condition('ec.user_id', $userid, '=');
            $ecQuery->condition('ec.student_id', $studentIdNew, '=');
            $ecQuery->orderBy('ec.choice_no');
            $epalChosen = $ecQuery->execute()->fetchAll(\PDO::FETCH_OBJ);
            $i = 0;
Open Source Developer's avatar
Open Source Developer committed
496

497 498
            if ($epalChosen && sizeof($epalChosen) > 0) {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
499 500
                foreach ($epalChosen as $object) {
                    $list[] = array(
501 502 503 504
                            'epal_id' => $object->name,
                            'choice_no' => $object->choice_no,
                        );
                    ++$i;
Open Source Developer's avatar
Open Source Developer committed
505
                }
506

Open Source Developer's avatar
Open Source Developer committed
507
                return $this->respondWithStatus(
508 509 510 511
                        $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t('EPAL chosen not found!!!'),
Open Source Developer's avatar
Open Source Developer committed
512
                ], Response::HTTP_FORBIDDEN);
513
            }
Open Source Developer's avatar
Open Source Developer committed
514 515
        } else {
            return $this->respondWithStatus([
516
                    'message' => t('User not found'),
Open Source Developer's avatar
Open Source Developer committed
517 518
                ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans  
Open Source Developer committed
519 520
    }

Open Source Developer's avatar
trans  
Open Source Developer committed
521 522 523 524 525 526
    public function getCritiria(Request $request, $studentId, $type)
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
527 528 529 530
            $userid = $epalUser->user_id->entity->id();
            $studentIdNew = intval($studentId);
            $critiriaChosen = $this->entityTypeManager->getStorage('epal_student_moria')->loadByProperties(array('user_id' => $userid, 'student_id' => $studentIdNew));
            $i = 0;
Open Source Developer's avatar
trans  
Open Source Developer committed
531 532

            if ($critiriaChosen) {
533
                $list = array();
Open Source Developer's avatar
trans  
Open Source Developer committed
534
                foreach ($critiriaChosen as $object) {
535 536
                    $critirio_id = $object->criterio_id->entity->id();
                    $critiriatype = $this->entityTypeManager->getStorage('epal_criteria')->loadByProperties(array('id' => $critirio_id));
537
                    $typeofcritiria = reset($critiriatype);
538 539
                    $typecrit = $typeofcritiria->category->value;
                    if ($typecrit == 'Κοινωνικό' && $type == 1) {
Open Source Developer's avatar
trans  
Open Source Developer committed
540
                        $list[] = array(
541 542
                            'critirio' => $object->criterio_id->entity->get('name')->value,
                            'critirio_id' => $critirio_id,
Open Source Developer's avatar
trans  
Open Source Developer committed
543 544
                            );

545
                        ++$i;
Open Source Developer's avatar
trans  
Open Source Developer committed
546
                    }
547
                    if ($typecrit == 'Εισοδηματικό' && $type == 2) {
Open Source Developer's avatar
trans  
Open Source Developer committed
548
                        $list[] = array(
549 550
                            'critirio' => $object->criterio_id->entity->get('name')->value,
                            'critirio_id' => $critirio_id,
Open Source Developer's avatar
trans  
Open Source Developer committed
551 552
                            );

553
                        ++$i;
Open Source Developer's avatar
trans  
Open Source Developer committed
554 555 556 557
                    }
                }

                return $this->respondWithStatus(
558 559 560 561
                        $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t('EPAL chosen not found!!!'),
Open Source Developer's avatar
trans  
Open Source Developer committed
562
                ], Response::HTTP_FORBIDDEN);
563
            }
Open Source Developer's avatar
trans  
Open Source Developer committed
564 565
        } else {
            return $this->respondWithStatus([
566
                    'message' => t('User not found'),
Open Source Developer's avatar
trans  
Open Source Developer committed
567 568 569 570
                ], Response::HTTP_FORBIDDEN);
        }
    }

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

    public function getResults(Request $request, $studentId) {

      //έλεγχος πρόσβασης
      $authToken = $request->headers->get('PHP_AUTH_USER');
      $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
      $epalUser = reset($epalUsers);
      if ($epalUser) {
          $userid = $epalUser->id();
          $studentIdNew = intval($studentId);
          $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid, 'id' => $studentIdNew));
          $i = 0;
          if ($epalStudents) {
              $list = array();

              //ανάκτηση τιμής από ρυθμίσεις διαχειριστή για lock_results
              $config_storage = $this->entityTypeManager->getStorage('epal_config');
              $epalConfigs = $config_storage->loadByProperties(array('name' => 'epal_config'));
              $epalConfig = reset($epalConfigs);
              if (!$epalConfig) {
                 return $this->respondWithStatus([
                         'message' => t("EpalConfig Enity not found"),
                     ], Response::HTTP_FORBIDDEN);
              }
              else {
                 $applicantsResultsDisabled = $epalConfig->lock_results->getString();
597
                 //$secondPeriodEnabled = $epalConfig->activate_second_period->getString();
598 599
              }

600 601 602 603
              $status = "0";
              $schoolName = '';
              $schoolAddress = '';
              $schoolTel = '';
604
              $secondPeriod = "0";
605

606 607 608 609 610
              //ανάκτηση αποτελέσματος
              // εύρεση τοποθέτησης (περίπτωση μαθητή που τοποθετήθηκε "οριστικά")

              if ($applicantsResultsDisabled === "0")   {

611 612 613 614 615
        				$escQuery = $this->connection->select('epal_student_class', 'esc')
        										->fields('esc', array('student_id', 'epal_id', 'finalized'))
                                                ->fields('esch', array('id', 'name', 'street_address','phone_number'));
                        $escQuery->addJoin('inner', 'eepal_school_field_data', 'esch', 'esc.epal_id=esch.id');
                        $escQuery->condition('esc.student_id', intval($studentId), '=');
616 617
                        //$escQuery->condition('esc.second_period', intval($secondPeriodEnabled), '=');

618 619 620 621 622
        				$epalStudentClasses = $escQuery->execute()->fetchAll(\PDO::FETCH_OBJ);
                if  (sizeof($epalStudentClasses) === 1) {
                  $epalStudentClass = reset($epalStudentClasses);

                    if ($epalStudentClass->finalized === "1")  {
623
                      $status = "1";
624 625 626
                      $schoolName = $epalStudentClass->name;
                      $schoolAddress = $epalStudentClass->street_address;
                      $schoolTel = $epalStudentClass->phone_number;
627 628
                      //$secondPeriod = $epalStudentClass->second_period;
                      //$secondPeriodEnabled = $secondPeriodEnabled;
629 630 631
                    }
                    else  {
                        $status = "2";
632 633
                        //$secondPeriod = $epalStudentClass->second_period;
                        //$secondPeriodEnabled = $secondPeriodEnabled;
634 635 636 637
                    }
                }
                else  {
                    $status = "0";
638 639
                    //$secondPeriod = $epalStudentClass->second_period;
                    //$secondPeriodEnabled = $secondPeriodEnabled;
640 641 642 643 644 645 646 647 648 649
                }

            } //endif $applicantsResultsDisabled === "0"

            $list[] = array(
                      'applicantsResultsDisabled' => $applicantsResultsDisabled,
                      'status' => $status,
                      'schoolName' => $schoolName,
                      'schoolAddress' => $schoolAddress,
                      'schoolTel' => $schoolTel,
650 651
                      //'secondPeriod' => $secondPeriod,
                      //'secondPeriodSettingEnabled' => $secondPeriodEnabled,
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
              );

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

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

    }

667 668
    private function respondWithStatus($arr, $s)
    {
Open Source Developer's avatar
Open Source Developer committed
669 670
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
671

Open Source Developer's avatar
Open Source Developer committed
672 673
        return $res;
    }
Open Source Developer's avatar
trans  
Open Source Developer committed
674
}