DirectorView.php 15.6 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 12
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
Open Source Developer's avatar
transf  
Open Source Developer committed
13 14 15 16

class DirectorView extends ControllerBase
{
    protected $entityTypeManager;
17 18
    protected $logger;
    protected $testSchoolId='0640050';
Open Source Developer's avatar
transf  
Open Source Developer committed
19

20 21
    public function __construct(EntityTypeManagerInterface $entityTypeManager,
        LoggerChannelFactoryInterface $loggerChannel)
Open Source Developer's avatar
transf  
Open Source Developer committed
22 23
    {
        $this->entityTypeManager = $entityTypeManager;
24
        $this->logger = $loggerChannel->get('epal-school');
Open Source Developer's avatar
transf  
Open Source Developer committed
25 26 27 28 29
    }

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



public function getSectorsPerSchool(Request $request, $epalId)
    {
39

Open Source Developer's avatar
transf  
Open Source Developer committed
40
        $authToken = $request->headers->get('PHP_AUTH_USER');
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
//             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $user->mail->value, 'id' => intval($epalId)));
             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $this->testSchoolId, 'id' => intval($epalId)));
             $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;
             }


                $userid = $user -> id();
Open Source Developer's avatar
transf  
Open Source Developer committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72
                $epalIdNew = intval($epalId);
                $sectorPerSchool = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id'=> $epalIdNew));
                $i = 0;

            if ($sectorPerSchool) {
                 $list = array();
                foreach ($sectorPerSchool as $object) {
                    $list[] = array(
                            'sector_id' => $object -> sector_id ->entity->get('name')->value ,
                            'id' => $object -> sector_id -> entity -> id()
);

                   	 $i++;
                }
73

Open Source Developer's avatar
transf  
Open Source Developer committed
74 75 76 77 78 79 80
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
81
                ], Response::HTTP_OK);
Open Source Developer's avatar
transf  
Open Source Developer committed
82
                }
83

Open Source Developer's avatar
transf  
Open Source Developer committed
84 85 86 87 88 89 90 91


        } else {

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

Open Source Developer's avatar
transf  
Open Source Developer committed
93 94
    }

Open Source Developer's avatar
trans  
Open Source Developer committed
95 96
public function getSpecialPerSchool(Request $request, $epalId , $sectorId)
    {
97

Open Source Developer's avatar
trans  
Open Source Developer committed
98
        $authToken = $request->headers->get('PHP_AUTH_USER');
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
//             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $user->mail->value, 'id' => intval($epalId)));
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $this->testSchoolId, 'id' => intval($epalId)));
             $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;
             }
                $userid = $user -> id();
Open Source Developer's avatar
trans  
Open Source Developer committed
115 116 117 118 119 120 121 122 123 124 125 126
                $epalIdNew = intval($epalId);
                $specialityPerSchool = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id'=> $epalIdNew));
                $i = 0;

            if ($specialityPerSchool) {
                 $list = array();
                 $SectorIdNew = intval($sectorId);
                foreach ($specialityPerSchool as $object) {
                       $idSpecial =  $object -> specialty_id -> entity -> id() ;

                       $specialityPerSector = $this->entityTypeManager->getStorage('eepal_specialty')->loadByProperties(array('id'=> $idSpecial,'sector_id' => $SectorIdNew ));
                       $specialPerSec = reset($specialityPerSector);
127
                       if ($specialPerSec)
Open Source Developer's avatar
trans  
Open Source Developer committed
128 129 130 131 132
                        {       $list[] = array(
                                'specialty_id' => $object -> specialty_id ->entity->get('name')->value ,
                                'id' => $object -> specialty_id -> entity -> id()     );
                                $i++;
                        }
133

Open Source Developer's avatar
trans  
Open Source Developer committed
134
                }
135

Open Source Developer's avatar
trans  
Open Source Developer committed
136 137 138 139 140 141 142
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
143
                ], Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
144
                }
145

Open Source Developer's avatar
trans  
Open Source Developer committed
146

Open Source Developer's avatar
transf  
Open Source Developer committed
147

Open Source Developer's avatar
trans  
Open Source Developer committed
148 149 150 151 152 153
        } else {

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

Open Source Developer's avatar
trans  
Open Source Developer committed
155 156 157
    }


Open Source Developer's avatar
trans  
Open Source Developer committed
158
public function getStudentPerSchool(Request $request, $epalId , $selectId, $classId)
Open Source Developer's avatar
trans  
Open Source Developer committed
159
    {
160

Open Source Developer's avatar
trans  
Open Source Developer committed
161
        $authToken = $request->headers->get('PHP_AUTH_USER');
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

       $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if ($user) {
//            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $user->mail->value, 'id' => intval($epalId)));
            $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $this->testSchoolId, 'id' => intval($epalId)));
            $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;
            }

                $userid = $user -> id();
Open Source Developer's avatar
trans  
Open Source Developer committed
179 180
                $epalIdNew = intval($epalId);
                $selectIdNew = intval($selectId);
Open Source Developer's avatar
trans  
Open Source Developer committed
181 182 183 184
                if ($classId == 1)
                {
                 $selectIdNew = -1;
                 $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalIdNew, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
185

Open Source Developer's avatar
trans  
Open Source Developer committed
186 187
                }
                else
188
                {
Open Source Developer's avatar
trans  
Open Source Developer committed
189
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalIdNew, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
190
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
191 192 193 194
                $i = 0;

            if ($studentPerSchool) {
                 $list = array();
195 196
                 foreach ($studentPerSchool as $object)
                        {
Open Source Developer's avatar
trans  
Open Source Developer committed
197 198
                        $studentId = $object -> id() ;
                        $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $studentId));
Open Source Developer's avatar
trans  
Open Source Developer committed
199
                        $epalStudent = reset($epalStudents);
Open Source Developer's avatar
trans  
Open Source Developer committed
200 201
                        $i = 0;
                        if ($epalStudents) {
202

Open Source Developer's avatar
trans  
Open Source Developer committed
203
                           $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
204 205 206 207
                            'id' => $epalStudent -> id(),
                            'name' => $epalStudent -> name ->value,
                            'studentsurname' => $epalStudent -> studentsurname ->value,
                            'fatherfirstname' => $epalStudent -> fatherfirstname ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
208
                            'fathersurname' =>$epalStudent -> fathersurname ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
209 210 211
                            'motherfirstname' => $epalStudent -> motherfirstname ->value,
                            'mothersurname' =>$epalStudent -> mothersurname ->value,
                            'birthdate' =>$epalStudent -> birthdate ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
212 213 214 215 216 217 218 219 220 221 222 223
                            );

                             $i++;
                        }
                }
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("No students found!!!"),
224
                ], Response::HTTP_OK);
Open Source Developer's avatar
trans  
Open Source Developer committed
225 226 227
                }


228 229

        }
Open Source Developer's avatar
trans  
Open Source Developer committed
230 231 232 233 234 235
        else {

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

Open Source Developer's avatar
trans  
Open Source Developer committed
237
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
238 239 240



Open Source Developer's avatar
trans  
Open Source Developer committed
241 242 243 244
    public function ConfirmStudents(Request $request)
    {

        if (!$request->isMethod('POST')) {
245
            return $this->respondWithStatus([
Open Source Developer's avatar
trans  
Open Source Developer committed
246 247 248 249 250
                    "message" => t("Method Not Allowed")
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
//             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $user->mail->value, 'id' => intval($epalId)));
             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $this->testSchoolId));
             $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
trans  
Open Source Developer committed
266
            $postData = null;
Open Source Developer's avatar
trans  
Open Source Developer committed
267

Open Source Developer's avatar
trans  
Open Source Developer committed
268
            if ($content = $request->getContent())
Open Source Developer's avatar
trans  
Open Source Developer committed
269 270 271 272 273
            {
                 $postData = json_decode($content);
                 $arr = $postData->students;

                foreach ($arr as $value) {
274
                    $valnew = intval($value);
Open Source Developer's avatar
trans  
Open Source Developer committed
275 276 277 278 279
                 $studentForConfirm = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('id' => $valnew ));
                    $studentConfirm = reset($studentForConfirm);
                  if ($studentConfirm) {
                         $studentConfirm->set('directorconfirm', true);
                         $studentConfirm->save();
280 281
                    }
                }
Open Source Developer's avatar
trans  
Open Source Developer committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                return $this->respondWithStatus([
                    'message' => t("saved"),
                ], Response::HTTP_OK);
            }
             else
              {
                  return $this->respondWithStatus([
                    'message' => t("post with no data"),
                ], Response::HTTP_BAD_REQUEST);
                }

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




Open Source Developer's avatar
Open Source Developer committed
303 304 305 306 307 308


public function SaveCapacity(Request $request,$taxi,$tomeas,$specialit,$schoolid)
    {

        if (!$request->isMethod('POST')) {
309
            return $this->respondWithStatus([
Open Source Developer's avatar
Open Source Developer committed
310 311 312 313 314
                    "message" => t("Method Not Allowed")
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

315 316 317 318 319 320 321 322 323 324 325 326 327 328
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
         $user = reset($users);
         if ($user) {
//             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $user->mail->value, 'id' => intval($epalId)));
             $schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('registry_no' => $this->testSchoolId));
             $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
329 330 331 332 333 334 335 336 337 338 339 340 341
            $postData = null;

            if ($content = $request->getContent())
            {
                 $postData = json_decode($content);
                 $cap = $postData->capacity;
                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();
342
                    }
Open Source Developer's avatar
Open Source Developer committed
343 344 345 346 347 348 349 350 351 352
                }


                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();
353
                    }
Open Source Developer's avatar
Open Source Developer committed
354 355 356 357 358 359 360 361 362 363
                }


                if (($tomeas != 0) || ($specialit != 0))
                {
                 $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();
364
                    }
Open Source Developer's avatar
Open Source Developer committed
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
                }






                return $this->respondWithStatus([
                    'message' => t("saved"),
                ], Response::HTTP_OK);
            }
             else
              {
                  return $this->respondWithStatus([
                    'message' => t("post with no data"),
                ], Response::HTTP_BAD_REQUEST);
                }

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



Open Source Developer's avatar
transf  
Open Source Developer committed
392 393 394 395 396 397 398 399 400 401 402
   private function respondWithStatus($arr, $s) {
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
        return $res;
    }





}