SubmitedApplications.php 5.68 KB
Newer Older
Open Source Developer's avatar
trans    
Open Source Developer committed
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace Drupal\epal\Controller;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Open Source Developer's avatar
Open Source Developer committed
12
class SubmitedApplications extends ControllerBase
Open Source Developer's avatar
trans    
Open Source Developer committed
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    protected $entityTypeManager;

    public function __construct(EntityTypeManagerInterface $entityTypeManager)
    {
        $this->entityTypeManager = $entityTypeManager;
    }

    public static function create(ContainerInterface $container)
    {
        return new static(
            $container->get('entity_type.manager')
        );
26
    }
Open Source Developer's avatar
trans    
Open Source Developer committed
27
28
29
30


    public function getSubmittedApplications(Request $request)
    {
31

Open Source Developer's avatar
Open Source Developer committed
32
33
34
35
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
Open Source Developer's avatar
tranfer    
Open Source Developer committed
36
            $userid = $epalUser -> id();
37

Open Source Developer's avatar
tranfer    
Open Source Developer committed
38
39
40
41
42
            $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid));
            $i = 0;
            if ($epalStudents) {
                 $list = array();
                foreach ($epalStudents as $object) {
43

Open Source Developer's avatar
styles    
Open Source Developer committed
44
                    $indexid = intval($object -> id())-1;
Open Source Developer's avatar
Open Source Developer committed
45
                    $list[] = array(
Open Source Developer's avatar
styles    
Open Source Developer committed
46
                            'id' => $indexid,
Open Source Developer's avatar
Open Source Developer committed
47
                            'name' => $object -> name ->value,
Open Source Developer's avatar
tranfer    
Open Source Developer committed
48
49
50
                            'studentsurname' => $object -> studentsurname ->value);
                    $i++;
                }
51

Open Source Developer's avatar
tranfer    
Open Source Developer committed
52
53
54
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
Open Source Developer's avatar
Open Source Developer committed
55
56
57
                }
            else {
                       return $this->respondWithStatus([
Open Source Developer's avatar
tranfer    
Open Source Developer committed
58
                    'message' => t("EPAL user not found"),
Open Source Developer's avatar
Open Source Developer committed
59
60
                ], Response::HTTP_FORBIDDEN);
                }
61
62


Open Source Developer's avatar
Open Source Developer committed
63
64
65
66
67
68
69


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

Open Source Developer's avatar
Open Source Developer committed
71
72
73
    }


74

Open Source Developer's avatar
Open Source Developer committed
75
76
77

 public function getStudentApplications(Request $request, $studentId)
    {
78

Open Source Developer's avatar
Open Source Developer committed
79
80
81
82
83
        $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();
Open Source Developer's avatar
Open Source Developer committed
84
85
86
87
88
89
90
91
92
                $studentIdNew = intval($studentId) ;
                $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid, 'id'=> $studentIdNew));
                $i = 0;

                 if ($epalStudents) {
                 $list = array();
                foreach ($epalStudents as $object) {
                    $list[] = array(
                            'name' => $object -> name ->value,
Open Source Developer's avatar
Open Source Developer committed
93
94
                            'studentsurname' => $object -> studentsurname ->value,
                            'fatherfirstname' => $object -> fatherfirstname ->value,
Open Source Developer's avatar
styles    
Open Source Developer committed
95
                            'fathersurname' =>$object -> fathersurname ->value,
Open Source Developer's avatar
Open Source Developer committed
96
97
98
99
100
                            'motherfirstname' => $object -> motherfirstname ->value,
                            'mothersurname' =>$object -> mothersurname ->value,
                            'birthdate' =>$object -> birthdate ->value,
);

Open Source Developer's avatar
Open Source Developer committed
101
102
                    $i++;
                }
103

Open Source Developer's avatar
Open Source Developer committed
104
105
106
107
108
109
110
111
112
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("EPAL user not found"),
                ], Response::HTTP_FORBIDDEN);
                }
113

Open Source Developer's avatar
Open Source Developer committed
114

Open Source Developer's avatar
Open Source Developer committed
115

Open Source Developer's avatar
Open Source Developer committed
116
117
118
119
120
        } else {
            return $this->respondWithStatus([
                    'message' => t("User not found"),
                ], Response::HTTP_FORBIDDEN);
        }
121

Open Source Developer's avatar
Open Source Developer committed
122
123
124
125
126
    }


public function getEpalChosen(Request $request, $studentId)
    {
127

Open Source Developer's avatar
Open Source Developer committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
                $userid = $epalUser -> user_id -> entity -> id();
                $studentIdNew = intval($studentId) ;
                 $epalChosen = $this->entityTypeManager->getStorage('epal_student_epal_chosen')->loadByProperties(array( 'user_id'=>$userid,'student_id'=> $studentIdNew));
                $i = 0;

            if ($epalChosen) {
                 $list = array();
                foreach ($epalChosen as $object) {
                    $list[] = array(
                            'epal_id' => $object -> epal_id ->entity->get('name')->value ,
                            'choice_no' => $object -> choice_no ->value,
);

                    $i++;
                }
147

Open Source Developer's avatar
Open Source Developer committed
148
149
150
151
152
153
154
155
156
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("EPAL chosen not found!!!"),
                ], Response::HTTP_FORBIDDEN);
                }
157

Open Source Developer's avatar
Open Source Developer committed
158
159


Open Source Developer's avatar
Open Source Developer committed
160
161
        } else {
            return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
162
                    'message' => t("User not found"),
Open Source Developer's avatar
Open Source Developer committed
163
164
                ], Response::HTTP_FORBIDDEN);
        }
165

Open Source Developer's avatar
trans    
Open Source Developer committed
166
167
168
    }


Open Source Developer's avatar
Open Source Developer committed
169
170
171



Open Source Developer's avatar
Open Source Developer committed
172
173
174
175
176
   private function respondWithStatus($arr, $s) {
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
        return $res;
    }
Open Source Developer's avatar
Open Source Developer committed
177
178
179
180
181





Open Source Developer's avatar
trans    
Open Source Developer committed
182
}