SubmitedApplications.php 20.8 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
65
66
67
68
        $transaction = $this->connection->startTransaction();
        try {
            $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
            $epalUser = reset($epalUsers);
            if ($epalUser) {
                $userid = $epalUser->id();
69

70
71
72
73
74
75
76
77
78
79
80
81
                $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);
                    }
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                    $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);
                }
105
106
            } else {
                return $this->respondWithStatus([
107
                'message' => t('EPAL user not found'),
108
109
                ], Response::HTTP_FORBIDDEN);
            }
110
111
112
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
            $transaction->rollback();
113

114
115
116
117
            return $this->respondWithStatus([
                'error_code' => 5001,
            ], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
118
119
    }

Open Source Developer's avatar
trans    
Open Source Developer committed
120
121
    public function getSubmittedApplications(Request $request)
    {
Open Source Developer's avatar
Open Source Developer committed
122
123
124
125
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
126
            $userid = $epalUser->id();
127

Open Source Developer's avatar
tranfer    
Open Source Developer committed
128
129
            $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid));
            $i = 0;
130
            $list = array();
Open Source Developer's avatar
tranfer    
Open Source Developer committed
131
            if ($epalStudents) {
132
133
                $crypt = new Crypt();

134

135
136
137
138
139
140
141
142
143
144
                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;
                    }
145
146
147
148
                    try {
                        $name_decoded = $crypt->decrypt($object->name->value);
                        $studentsurname_decoded = $crypt->decrypt($object->studentsurname->value);
                    } catch (\Exception $e) {
149
150
                        unset($crypt);
                        $this->logger->warning($e->getMessage());
151

152
                        return $this->respondWithStatus([
153
                          'message' => t('An unexpected error occured during DECODING data in getSubmittedApplications Method '),
154
155
156
                        ], Response::HTTP_INTERNAL_SERVER_ERROR);
                    }

Open Source Developer's avatar
Open Source Developer committed
157
                    $list[] = array(
158
                            'id' => $object->id(),
159
160
161
                            //'name' => $object -> name ->value,
                            'name' => $name_decoded,
                            //'studentsurname' => $object -> studentsurname ->value);
162
163
                            'studentsurname' => $studentsurname_decoded,
                            'candelete' => $canDelete, );
164
                    ++$i;
Open Source Developer's avatar
tranfer    
Open Source Developer committed
165
                }
166

167
168
                unset($crypt);

Open Source Developer's avatar
tranfer    
Open Source Developer committed
169
                return $this->respondWithStatus(
170
171
                        $list, Response::HTTP_OK);
            } else {
172
173
                return $this->respondWithStatus(
                        $list, Response::HTTP_OK);
174
            }
Open Source Developer's avatar
Open Source Developer committed
175
176
        } else {
            return $this->respondWithStatus([
177
                    'message' => t('User not found'),
Open Source Developer's avatar
Open Source Developer committed
178
179
180
181
                ], Response::HTTP_FORBIDDEN);
        }
    }

182
    public function getStudentApplications(Request $request, $studentId)
Open Source Developer's avatar
Open Source Developer committed
183
184
185
186
187
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
188
189
190
191
            $userid = $epalUser->id();
            $studentIdNew = intval($studentId);
            $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid, 'id' => $studentIdNew));
            $i = 0;
192

193
194
            if ($epalStudents) {
                $list = array();
195

Open Source Developer's avatar
Open Source Developer committed
196
                foreach ($epalStudents as $object) {
197
198
199
200
201
                    $sectorName = '';
                    $courseName = '';
                    if ($object->currentclass->value === '2') {
                        $sectors = $this->entityTypeManager->getStorage('epal_student_sector_field')->loadByProperties(array('student_id' => $object->id()));
                        $sector = reset($sectors);
202
                        if ($sector) {
203
                            $sectorName = $this->entityTypeManager->getStorage('eepal_sectors')->load($sector->sectorfield_id->target_id)->name->value;
204
205
                        }
                    } elseif ($object->currentclass->value === '3' || $object->currentclass->value === '4') {
206
207
                        $courses = $this->entityTypeManager->getStorage('epal_student_course_field')->loadByProperties(array('student_id' => $object->id()));
                        $course = reset($courses);
208
                        if ($course) {
209
                            $courseName = $this->entityTypeManager->getStorage('eepal_specialty')->load($course->coursefield_id->target_id)->name->value;
210
                        }
211
                    }
212
213

                    $crypt = new Crypt();
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                    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) {
229
230
                        //print_r($e->getMessage());
                        unset($crypt);
231
232
233
234
235
                        $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);
236
237
238
                    }
                    unset($crypt);

Open Source Developer's avatar
Open Source Developer committed
239
                    $list[] = array(
240
                            'applicationId' => $object->id(),
241
242
243
                            'name' => $name_decoded,
                            'studentsurname' => $studentsurname_decoded,
                            'fatherfirstname' => $fatherfirstname_decoded,
244
                            'fathersurname' => $object->fathersurname->value,
245
                            'motherfirstname' => $motherfirstname_decoded,
246
247
                            'mothersurname' => $object->mothersurname->value,
                            'guardian_name' => $guardian_name_decoded,
248
                            'guardian_surname' => $guardian_surname_decoded,
249
250
251
252
253
254
255
256
257
258
259
260
                            '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,
261
                            'relationtostudent' => $relationtostudent_decoded,
262
263
                            '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),
264

Open Source Developer's avatar
trans    
Open Source Developer committed
265
                        );
Open Source Developer's avatar
Open Source Developer committed
266

267
                    ++$i;
Open Source Developer's avatar
Open Source Developer committed
268
                }
269

Open Source Developer's avatar
Open Source Developer committed
270
                return $this->respondWithStatus(
271
272
273
274
                        $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t('EPAL user not found'),
Open Source Developer's avatar
Open Source Developer committed
275
                ], Response::HTTP_FORBIDDEN);
276
            }
Open Source Developer's avatar
Open Source Developer committed
277
278
        } else {
            return $this->respondWithStatus([
279
                    'message' => t('User not found!!!!'),
Open Source Developer's avatar
Open Source Developer committed
280
281
282
283
                ], Response::HTTP_FORBIDDEN);
        }
    }

284
    public function getEpalChosen(Request $request, $studentId)
Open Source Developer's avatar
Open Source Developer committed
285
286
287
288
289
    {
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
290
291
292
293
294
295
296
297
298
299
300
            $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
301

302
303
            if ($epalChosen && sizeof($epalChosen) > 0) {
                $list = array();
Open Source Developer's avatar
Open Source Developer committed
304
305
                foreach ($epalChosen as $object) {
                    $list[] = array(
306
307
308
309
                            'epal_id' => $object->name,
                            'choice_no' => $object->choice_no,
                        );
                    ++$i;
Open Source Developer's avatar
Open Source Developer committed
310
                }
311

Open Source Developer's avatar
Open Source Developer committed
312
                return $this->respondWithStatus(
313
314
315
316
                        $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t('EPAL chosen not found!!!'),
Open Source Developer's avatar
Open Source Developer committed
317
                ], Response::HTTP_FORBIDDEN);
318
            }
Open Source Developer's avatar
Open Source Developer committed
319
320
        } else {
            return $this->respondWithStatus([
321
                    'message' => t('User not found'),
Open Source Developer's avatar
Open Source Developer committed
322
323
                ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
324
325
    }

Open Source Developer's avatar
trans    
Open Source Developer committed
326
327
328
329
330
331
    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) {
332
333
334
335
            $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
336
337

            if ($critiriaChosen) {
338
                $list = array();
Open Source Developer's avatar
trans    
Open Source Developer committed
339
                foreach ($critiriaChosen as $object) {
340
341
                    $critirio_id = $object->criterio_id->entity->id();
                    $critiriatype = $this->entityTypeManager->getStorage('epal_criteria')->loadByProperties(array('id' => $critirio_id));
342
                    $typeofcritiria = reset($critiriatype);
343
344
                    $typecrit = $typeofcritiria->category->value;
                    if ($typecrit == 'Κοινωνικό' && $type == 1) {
Open Source Developer's avatar
trans    
Open Source Developer committed
345
                        $list[] = array(
346
347
                            'critirio' => $object->criterio_id->entity->get('name')->value,
                            'critirio_id' => $critirio_id,
Open Source Developer's avatar
trans    
Open Source Developer committed
348
349
                            );

350
                        ++$i;
Open Source Developer's avatar
trans    
Open Source Developer committed
351
                    }
352
                    if ($typecrit == 'Εισοδηματικό' && $type == 2) {
Open Source Developer's avatar
trans    
Open Source Developer committed
353
                        $list[] = array(
354
355
                            'critirio' => $object->criterio_id->entity->get('name')->value,
                            'critirio_id' => $critirio_id,
Open Source Developer's avatar
trans    
Open Source Developer committed
356
357
                            );

358
                        ++$i;
Open Source Developer's avatar
trans    
Open Source Developer committed
359
360
361
362
                    }
                }

                return $this->respondWithStatus(
363
364
365
366
                        $list, Response::HTTP_OK);
            } else {
                return $this->respondWithStatus([
                    'message' => t('EPAL chosen not found!!!'),
Open Source Developer's avatar
trans    
Open Source Developer committed
367
                ], Response::HTTP_FORBIDDEN);
368
            }
Open Source Developer's avatar
trans    
Open Source Developer committed
369
370
        } else {
            return $this->respondWithStatus([
371
                    'message' => t('User not found'),
Open Source Developer's avatar
trans    
Open Source Developer committed
372
373
374
375
                ], Response::HTTP_FORBIDDEN);
        }
    }

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

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

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

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

409
410
411
412
413
        				$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), '=');
414
       																	  //->condition('eStudent.finalized', "1" , '=');
415
416
417
418
419
        				$epalStudentClasses = $escQuery->execute()->fetchAll(\PDO::FETCH_OBJ);
                if  (sizeof($epalStudentClasses) === 1) {
                  $epalStudentClass = reset($epalStudentClasses);

                    if ($epalStudentClass->finalized === "1")  {
420
                      $status = "1";
421
422
423
                      $schoolName = $epalStudentClass->name;
                      $schoolAddress = $epalStudentClass->street_address;
                      $schoolTel = $epalStudentClass->phone_number;
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
                    }
                    else  {
                        $status = "2";
                    }
                }
                else  {
                    $status = "0";
                }

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

            $list[] = array(
                      'applicantsResultsDisabled' => $applicantsResultsDisabled,
                      'status' => $status,
                      'schoolName' => $schoolName,
                      'schoolAddress' => $schoolAddress,
                      'schoolTel' => $schoolTel,
              );

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

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

    }

456
457
    private function respondWithStatus($arr, $s)
    {
Open Source Developer's avatar
Open Source Developer committed
458
459
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
460

Open Source Developer's avatar
Open Source Developer committed
461
462
        return $res;
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
463
}