DirectorView.php 9.23 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 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

class DirectorView extends ControllerBase
{
    protected $entityTypeManager;

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

    public static function create(ContainerInterface $container)
    {
        return new static(
            $container->get('entity_type.manager')
        );
    } 



public function getSectorsPerSchool(Request $request, $epalId)
    {
  
        $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();
                $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++;
                }
                
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
                ], Response::HTTP_FORBIDDEN);
                }
            


        } else {

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

Open Source Developer's avatar
trans  
Open Source Developer committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
public function getSpecialPerSchool(Request $request, $epalId , $sectorId)
    {
  
        $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();
                $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);
                       if ($specialPerSec)  
                        {       $list[] = array(
                                'specialty_id' => $object -> specialty_id ->entity->get('name')->value ,
                                'id' => $object -> specialty_id -> entity -> id()     );
                                $i++;
                        }
                        
                }
                
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("School not found!!!"),
                ], Response::HTTP_FORBIDDEN);
                }
            

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

Open Source Developer's avatar
trans  
Open Source Developer committed
119 120 121 122 123 124 125 126 127 128
        } else {

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


Open Source Developer's avatar
trans  
Open Source Developer committed
129
public function getStudentPerSchool(Request $request, $epalId , $selectId, $classId)
Open Source Developer's avatar
trans  
Open Source Developer committed
130 131 132 133 134 135 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) {
                $userid = $epalUser -> user_id -> entity -> id();
                $epalIdNew = intval($epalId);
                $selectIdNew = intval($selectId);
Open Source Developer's avatar
trans  
Open Source Developer committed
140 141 142 143 144 145 146 147 148 149
                if ($classId == 1)
                {
                 $selectIdNew = -1;
                 $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalIdNew, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
                   
                }
                else
                {    
                $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id'=> $epalIdNew, 'specialization_id' => $selectIdNew, 'currentclass' => $classId ));
                }   
Open Source Developer's avatar
trans  
Open Source Developer committed
150 151 152 153 154 155 156 157
                $i = 0;

            if ($studentPerSchool) {
                 $list = array();
                 foreach ($studentPerSchool as $object) 
                        {     
                        $studentId = $object -> id() ;
                        $epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id'=> $studentId));
Open Source Developer's avatar
trans  
Open Source Developer committed
158
                        $epalStudent = reset($epalStudents);
Open Source Developer's avatar
trans  
Open Source Developer committed
159 160 161 162
                        $i = 0;
                        if ($epalStudents) {
                             
                           $list[] = array(
Open Source Developer's avatar
trans  
Open Source Developer committed
163 164 165 166
                            '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
167
                            'fathersurname' =>$epalStudent -> fathersurname ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
168 169 170
                            'motherfirstname' => $epalStudent -> motherfirstname ->value,
                            'mothersurname' =>$epalStudent -> mothersurname ->value,
                            'birthdate' =>$epalStudent -> birthdate ->value,
Open Source Developer's avatar
trans  
Open Source Developer committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
                            );

                             $i++;
                        }
                }
                return $this->respondWithStatus(
                        $list
                    , Response::HTTP_OK);
                }
             else {
                       return $this->respondWithStatus([
                    'message' => t("No students found!!!"),
                ], Response::HTTP_FORBIDDEN);
                }
            


        } 
        else {

            return $this->respondWithStatus([
                    'message' => t("User not found!"),
                ], Response::HTTP_FORBIDDEN);
        }
        
    }
Open Source Developer's avatar
transf  
Open Source Developer committed
197 198 199



Open Source Developer's avatar
trans  
Open Source Developer committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    public function ConfirmStudents(Request $request)
    {

        if (!$request->isMethod('POST')) {
            return $this->respondWithStatus([ 
                    "message" => t("Method Not Allowed")
                ], Response::HTTP_METHOD_NOT_ALLOWED);
        }
        $authToken = $request->headers->get('PHP_AUTH_USER');

        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
        $epalUser = reset($epalUsers);
        if ($epalUser) {
            $postData = null;
Open Source Developer's avatar
trans  
Open Source Developer committed
214

Open Source Developer's avatar
trans  
Open Source Developer committed
215
            if ($content = $request->getContent())
Open Source Developer's avatar
trans  
Open Source Developer committed
216 217 218 219 220 221 222 223 224 225 226 227
            {
                 $postData = json_decode($content);
                 $arr = $postData->students;

                foreach ($arr as $value) {
                    $valnew = intval($value);  
                 $studentForConfirm = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('id' => $valnew ));
                    $studentConfirm = reset($studentForConfirm);
                  if ($studentConfirm) {
                         $studentConfirm->set('directorconfirm', true);
                         $studentConfirm->save();
                    }   
Open Source Developer's avatar
trans  
Open Source Developer committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
                } 
                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
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
   private function respondWithStatus($arr, $s) {
        $res = new JsonResponse($arr);
        $res->setStatusCode($s);
        return $res;
    }





}