Ejemplo n.º 1
0
 public function create()
 {
     $this->_methodName = 'create';
     $this->resolveParams();
     if ($this->checkAuth() && !Auth::checkAdmin()) {
         throw new \App\Exceptions\ExceptionApiMethodbad($this->_typeName, $this->_methodName, $this->_request_params);
     }
     $arNeed = ['name' => 'required', 'description' => 'required', 'score' => 'required|numeric', 'timer' => 'required', 'userAvailable' => 'required_without:userAll', 'userAll' => 'required_without:userAvailable|in:true,false'];
     $this->checkAttr($arNeed);
     $this->checkAuth();
     $this->checkAuthAdmin();
     $task = new \App\Task();
     $task->name = $this->_request_params['name'];
     $task->description = $this->_request_params['description'];
     $task->point = $this->_request_params['score'];
     $task->timer = $this->_request_params['timer'];
     $task->save();
     if (!isset($this->_request_params['userAvailable'])) {
         $users = \App\Users::all();
     } else {
         $userArray = explode(';', $this->_request_params['userAvailable']);
         $users = \App\Users::whereIn('id', $userArray)->get();
     }
     foreach ($users as $user) {
         $task_user = new \App\UserTask();
         $task_user->id_task = $task->id;
         $task_user->id_user = $user->id;
         $task_user->status = 'available';
         $task_user->save();
         $users_array[] = $user->email;
     }
     $MESSAGE = 'Added new task';
     $url = 'https://api.parse.com/1/push';
     $data = array('where' => ['email' => ['$in' => $users_array]], 'data' => array('alert' => $task->name, 'title' => $MESSAGE));
     $_data = json_encode($data);
     $headers = array('X-Parse-Application-Id: ' . $this->APPLICATION_ID, 'X-Parse-REST-API-Key: ' . $this->REST_API_KEY, 'Content-Type: application/json', 'Content-Length: ' . strlen($_data));
     $curl = curl_init($url);
     curl_setopt($curl, CURLOPT_POST, 1);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     $response = curl_exec($curl);
     $this->_arData['data']['idTask'] = $task->id;
     return $this;
 }