Overview

Packages

  • controladores
  • modelos
  • None
  • PHP

Classes

  • Activity
  • ActivityController
  • AuthController
  • BaseController
  • Course
  • DatabaseSeeder
  • ExampleTest
  • Guardian
  • HomeController
  • Message
  • MessageController
  • OptStudentCriteriaGrade
  • Period
  • Profile
  • SchoolSetup
  • Student
  • StudentCriteriaGrade
  • StudentSubjectSubperiodAverage
  • StudentSubjectSubperiodAverageController
  • SubPeriod
  • TestCase
  • User
  • UserController

Exceptions

  • Exception
  • InvalidArgumentException
  • LogicException

Functions

  • ws_response
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Controlador de autenticacion de usuarios
  5:  * @package controladores
  6:  * @author David Castro
  7:  * @since  2016-02-23
  8:  */
  9: class UserController extends BaseController {
 10: 
 11:     /**
 12:      * Método para identificar el perfil del usuario autenticado
 13:      *
 14:      * @param   void
 15:      *
 16:      * @return  perfil de usuario estudiante o representante
 17:      * @throws  InvalidArgumentException
 18:      * @since   2016-02-23
 19:      * @author  David Castro
 20:      *
 21:      * @edit    2016-02-23<br />
 22:      *          Fernando Salas <rfsalas@rutatec.com><br />
 23:      *          documentación del método<br/>
 24:      *          #edit1
 25:      */
 26:     public function postInfo() {
 27:         try {
 28:             $user_id = JWTAuth::getPayload(JWTAuth::getToken())->get('sub');
 29:             $user = User::find($user_id);
 30:             $user_data = array();
 31:             if (!empty($user)) {
 32:                 $user_data['id'] = $user->serial_usr;
 33:                 $profile = Profile::join(
 34:                                 'user_profile', function ($join) use ($user) {
 35:                             $join->on('profile.serial_prf', '=', 'user_profile.serial_prf')
 36:                             ->where('user_profile.serial_usr', '=', $user->serial_usr)
 37:                             ->where('user_profile.status_upr', '=', 'ACTIVE');
 38:                         }
 39:                         )
 40:                         ->whereIn('profile.name_prf', User::$allowedProfiles)
 41:                         ->where('profile.status_prf', '=', 'ACTIVE')
 42:                         ->select('profile.serial_prf', 'profile.name_prf')
 43:                         ->first();
 44:                 if (!empty($profile)) {
 45:                     $user_data['profile'] = array(
 46:                         'id' => $profile->serial_prf,
 47:                         'name' => $profile->name_prf
 48:                     );
 49:                     if ($profile->name_prf == 'Representante') {
 50:                         $this->guardianInfo($user, $user_data);
 51:                     } else {
 52:                         if ($profile->name_prf == 'Estudiante') {
 53:                             $this->studentInfo($user, $user_data);
 54:                         }
 55:                     }
 56:                     return ws_response(false, $user_data, '', 200);
 57:                 }
 58:             }
 59:             return ws_response(false, $user_data, '', 200);
 60:         } catch (Exception $ex) {
 61:             return ws_response(true, null, 'ERROR ' . $ex->getCode() . '! ' . $ex->getMessage(), 500);
 62:         }
 63:     }
 64: 
 65:     /**
 66:      * Método para obtener la informacón del representante
 67:      *
 68:      * @param   string user
 69:      * @param   array user_data
 70:      *
 71:      * @return  datos del representante
 72:      * @throws  InvalidArgumentException
 73:      * @since   2016-02-23
 74:      * @author  David Castro
 75:      *
 76:      * @edit    2016-02-23<br />
 77:      *          Fernando Salas <rfsalas@rutatec.com><br />
 78:      *          documentación del método<br/>
 79:      *          #edit1
 80:      */
 81:     public function guardianInfo($user, &$user_data) {
 82:         $guardian = Guardian::where('serial_usr', '=', $user->serial_usr)
 83:                 ->where('status_grd', '=', 'ACTIVE')
 84:                 ->first();
 85:         if (!empty($guardian)) {
 86:             $user_data['name'] = $guardian->first_name_grd . ' ' . $guardian->last_name_grd;
 87:             $user_data['gender'] = $guardian->gender_grd;
 88:             $user_data['image'] = $guardian->user->image();
 89:             $students = array();
 90:             foreach ($guardian->students as $s) {
 91:                 $course = $s->course();
 92:                 $image = $s->user->image();
 93:                 $students[] = array(
 94:                     'id' => $s->serial_std,
 95:                     'name' => $s->first_name_std . ' ' . $s->last_name_std,
 96:                     'gender' => $s->gender_std,
 97:                     'course' => $course,
 98:                     'image' => $image
 99:                 );
100:             }
101:             if (!empty($students)) {
102:                 $user_data['students'] = $students;
103:             }
104:         }
105:         return $user_data;
106:     }
107: 
108:     /**
109:      * Método para obtener la información del estudiante
110:      *
111:      * @param   string $user
112:      * @param   array $user_data
113:      *
114:      *
115:      * @return  array user_data
116:      * @throws  InvalidArgumentException
117:      * @since   2016-02-23
118:      * @author  David Castro
119:      *
120:      * @edit    2016-02-23<br />
121:      *          Fernando Salas <rfsalas@rutatec.com><br />
122:      *          documentación del método<br/>
123:      *          #edit1
124:      */
125:     public function studentInfo($user, &$user_data) {
126:         $student = Student::where('serial_usr', '=', $user->serial_usr)
127:                 ->where('status_std', '=', 'ACTIVE')
128:                 ->first();
129:         if (!empty($student)) {
130:             $course = $student->course();
131:             $image = $student->user->image();
132:             $user_data['id_std'] = $student->serial_std;
133:             $user_data['name'] = $student->first_name_std . ' ' . $student->last_name_std;
134:             $user_data['gender'] = $student->gender_std;
135:             $user_data['course'] = $course;
136:             $user_data['image'] = $image;
137:         }
138:         return $user_data;
139:     }
140: 
141:     /**
142:      * Método para devolver las notificaciones del usuario autenticado
143:      *
144:      * @param   String $token
145:      * @param   String $action
146:      * 
147:      * @return  Notificaciones del usuario
148:      * @throws  InvalidArgumentException
149:      * @since   2016-02-23
150:      * @author  David Castro
151:      *
152:      * @edit    2016-02-23<br />
153:      *          Fernando Salas <rfsalas@rutatec.com><br />
154:      *          documentación del método<br/>
155:      *          #edit1
156:      */
157:     public function postRegisterNotification() {
158:         try {
159:             $serial_usr = JWTAuth::getPayload(JWTAuth::getToken())->get('sub');
160:             $token_push = Input::get('token_push');
161:             $action = Input::get('action');
162: 
163:             $user = User::find($serial_usr);
164:             // if $action == 'ADD' then new device, else remove device
165:             if ($token_push && $action == 'ADD') {
166:                 //  Verification if exit notification key for create or add device
167:                 if (!$user->notification_name_key && !$user->notification_key) {
168:                     //create google gcm service
169:                     $school = SchoolSetup::first();
170:                     $notification_name_key = 'appUser-' . $user->username_usr . '-' . $school->token_school . '-' . date(
171:                                     'Y-m-d H:i:s'
172:                     );
173:                     $user->notification_name_key = $notification_name_key;
174:                     $result = $this->createService($token_push, $notification_name_key);
175:                 } else {
176:                     // Add device at google gcm service
177:                     $result = $this->modifiedServices(
178:                             $token_push, $user->notification_name_key, $user->notification_key, 'add'
179:                     );
180:                 }
181:                 $result = json_decode($result, true);
182:                 //register verification in google gcm service
183:                 if (array_key_exists('notification_key', $result)) {
184:                     //register in DB of 'notification key' and number device
185:                     if (!$user->notification_key) {
186:                         $user->notification_key = $result['notification_key'];
187:                         $user->number_ids = 1;
188:                     } else {
189:                         $user->number_ids = $user->number_ids + 1;
190:                     }
191:                     $user->update();
192:                     //verification of pending messages
193:                     $messsges = self::postVerificationMessagesPending($user->serial_usr, $token_push);
194:                     return ws_response(false, 'Register Notification', 'success, ' . $messsges, 200);
195:                 } else {
196:                     return ws_response(true, 'Register Notification', 'fail: ' . $result['error'], 500);
197:                 }
198:             } elseif ($token_push && $action == 'DELETE') {
199:                 //remove device at google gcm service
200:                 $result = $this->modifiedServices(
201:                         $token_push, $user->notification_name_key, $user->notification_key, 'remove'
202:                 );
203:                 $result = json_decode($result, true);
204:                 //register verification in google gcm service
205:                 if (array_key_exists('notification_key', $result)) {
206:                     //revome device or google gcm service
207:                     if ($user->number_ids == 1) {
208:                         $user->notification_name_key = '';
209:                         $user->notification_key = '';
210:                         $user->number_ids = 0;
211:                     } else {
212:                         $user->number_ids = $user->number_ids - 1;
213:                     }
214:                     $user->update();
215:                     return ws_response(false, 'Register Notification', 'success', 200);
216:                 } else {
217:                     return ws_response(true, 'Register Notification', 'fail: ' . $result['error'], 500);
218:                 }
219:             } else {
220:                 return ws_response(true, 'Register Notification', 'fail: No value token_push or action', 500);
221:             }
222:         } catch (Exception $ex) {
223:             return ws_response(true, null, 'ERROR ' . $ex->getCode() . '! ' . $ex->getMessage(), 500);
224:         }
225:     }
226: 
227:     /**
228:      * Método para crear el formato json de los webservices creados
229:      *
230:      * @param   String $token_push
231:      * @param   String $notification_key
232:      * 
233:      * @return  json Web services creado
234:      * @throws  InvalidArgumentException
235:      * @since   2016-02-23
236:      * @author  David Castro
237:      *
238:      * @edit    2016-02-23<br />
239:      *          Fernando Salas <rfsalas@rutatec.com><br />
240:      *          documentación del método<br/>
241:      *          #edit1
242:      */
243:     private function createService($token_push, $notification_key) {
244:         $google_api_key = 'AIzaSyCmPV4IBQLA8QSMf96HrsDiEnLDuNeDzBs'; //Esto deberia ser una variable global
245:         $url = 'https://android.googleapis.com/gcm/notification'; //Esto tambien
246:         $number_project = '1005475082231'; //esto tambien
247:         //build fields at send to service google gcm
248:         $fields = array(
249:             'operation' => 'create',
250:             'notification_key_name' => $notification_key,
251:             'registration_ids' => [$token_push]
252:         );
253:         //built header at send to service google gcm
254:         $headers = array(
255:             'Content-Type: application/json',
256:             'Authorization: key=' . $google_api_key,
257:             'project_id: ' . $number_project
258:         );
259: 
260:         // Open connection
261:         $ch = curl_init();
262:         // Set the url, number of POST vars, POST data
263:         curl_setopt($ch, CURLOPT_URL, $url);
264:         curl_setopt($ch, CURLOPT_POST, true);
265:         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
266:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
267:         // Disabling SSL Certificate support temporarly
268:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
269:         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
270: 
271:         $result = curl_exec($ch);
272:         curl_close($ch);
273:         return $result;
274:     }
275: 
276:     //    Method for create json at send to service google gcm for create service
277:     /**
278:      * Método para modificar los webservices creados
279:      *
280:      * @param   void
281:      * 
282:      * 
283:      * @return  json Web service
284:      * @throws  InvalidArgumentException
285:      * @since   2016-02-23
286:      * @author  David Castro
287:      *
288:      * @edit    Fernando Salas <rfsalas@rutatec.com><br />
289:      *          documentación del método<br/>
290:      *          #edit1
291:      */
292:     private function modifiedServices($token_push, $notification_name_key, $notification_key, $action) {
293:         $google_api_key = 'AIzaSyCmPV4IBQLA8QSMf96HrsDiEnLDuNeDzBs'; //Esto deberia ser una variable global
294:         $url = 'https://android.googleapis.com/gcm/notification'; //Esto tambien
295:         $number_project = '1005475082231'; //esto tambien
296:         //build fields at send to service google gcm
297:         $fields = array(
298:             'operation' => $action,
299:             'notification_key_name' => $notification_name_key,
300:             'notification_key' => $notification_key,
301:             'registration_ids' => [$token_push]
302:         );
303:         //built header at send to service google gcm
304:         $headers = array(
305:             'Content-Type: application/json',
306:             'Authorization: key=' . $google_api_key,
307:             'project_id: ' . $number_project
308:         );
309: 
310:         // Open connection
311:         $ch = curl_init();
312:         // Set the url, number of POST vars, POST data
313:         curl_setopt($ch, CURLOPT_URL, $url);
314:         curl_setopt($ch, CURLOPT_POST, true);
315:         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
316:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
317:         // Disabling SSL Certificate support temporarly
318:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
319:         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
320: 
321:         $result = curl_exec($ch);
322:         curl_close($ch);
323:         return $result;
324:     }
325: 
326:     /**
327:      * Método para ver los mensajes pendientes del usuario
328:      *
329:      * @param   integer $user_id
330:      * @param   integer $user_id
331:      * 
332:      * 
333:      * @return  String mensajes de correos pendientes
334:      * @throws  InvalidArgumentException
335:      * @since   2016-02-23
336:      * @author  David Castro
337:      *
338:      * @edit    Fernando Salas <rfsalas@rutatec.com><br />
339:      *          documentación del método<br/>
340:      *          #edit1
341:      */
342:     private function postVerificationMessagesPending($user_id, $token_push) {
343: 
344:         $messages = Message::where('serial_usr_receive', $user_id)->where('checked_msg', 'NO')->get();
345:         //has pending messages
346:         if ($messages->count() > 0) {
347: 
348:             $category = 'CATEGORY_MESSAGE';
349:             //hes one or more pending messages
350:             if ($messages->count() > 1) {
351: 
352:                 $title = $messages->count() . ' comunicados nuevos';
353:                 $body_expanded = array();
354:                 $body_contracted = '';
355:                 $names = array();
356:                 $priority = 'DEFAULT';
357:                 $multiple = true;
358:                 $notification_id = null;
359:                 // built data array with information of  2 or more messages
360:                 foreach ($messages as $message) {
361:                     if (!array_key_exists($message['serial_usr_send'], $names)) {
362:                         $body_contracted = $body_contracted . $message->user_from_name_msg . ', ';
363:                     }
364:                     $names[$message->serial_usr_send] = $message->user_from_name_msg;
365:                     $text = substr($message->message_msg, 3, -4);
366:                     $new_line = '<b>' . $message->user_from_name_msg . '</b> ' . substr($text, 0, 30);
367:                     array_push($body_expanded, $new_line);
368:                 }
369: 
370:                 $body_contracted = substr($body_contracted, 0, -2);
371:                 if (strpos($body_contracted, ',')) {
372:                     $body['body_contracted'] = $body_contracted;
373:                 } else {
374:                     $body['body_contracted'] = $title;
375:                     $title = $body_contracted;
376:                 }
377: 
378:                 $body['body_expand'] = $body_expanded;
379:             } else {
380:                 // built data array with information one messages
381:                 $message = Message::find($messages[0]->serial_msg);
382:                 $title = $message->user_from_name_msg;
383:                 $body = $message->subject_msg;
384:                 $priority = $message->priority_msg == 'URGENT' ? 'MAX' : 'DEFAULT';
385:                 $multiple = false;
386:                 $notification_id = $messages[0]->serial_msg;
387:             }
388:             $message_data = array();
389:             $message_data['title'] = $title;
390:             $message_data['body'] = $body;
391:             $message_data['priority'] = $priority;
392:             $message_data['category'] = $category;
393:             $message_data['multiple'] = $multiple;
394:             $message_data['notification_id'] = $notification_id;
395:             $message_data['user_id'] = $user_id;
396:             // send notification
397:             $result = self::postSendNotification($user_id, $message_data, $token_push);
398:             return 'messages_send';
399:         } else {
400:             return 'not_messages_pending';
401:         }
402:     }
403: 
404: //    Method for send notification when  registration device or add device
405:     /**
406:      * Método para enviar notificacion cuando se registro en un dispositivo
407:      *
408:      * @param   integer $serial_usr
409:      * @param   date $message_data
410:      * @param   string $token_push
411:      * 
412:      * 
413:      * @return  String mensajes de correos pendientes
414:      * @throws  InvalidArgumentException
415:      * @since   2016-02-23
416:      * @author  David Castro
417:      *
418:      * @edit    Fernando Salas <rfsalas@rutatec.com><br />
419:      *          documentación del método<br/>
420:      *          #edit1
421:      */
422:     private function postSendNotification($serial_usr, $message_data, $token_push) {
423: 
424:         $user = User::find($serial_usr);
425:         $notification_key = $user->notification_key;
426: 
427:         $google_api_key = 'AIzaSyCmPV4IBQLA8QSMf96HrsDiEnLDuNeDzBs'; //Esto deberia ser una variable global
428:         $url = 'https://gcm-http.googleapis.com/gcm/send'; //Esto tambien
429:         //build fields at send to service google gcm
430:         $fields = array(
431:             'to' => $token_push,
432:             'data' => $message_data
433:         );
434:         //build headers at send to service google gcm
435:         $headers = array(
436:             'Content-Type: application/json',
437:             'Authorization: key=' . $google_api_key,
438:         );
439: 
440:         // Open connection
441:         $ch = curl_init();
442:         // Set the url, number of POST vars, POST data
443:         curl_setopt($ch, CURLOPT_URL, $url);
444:         curl_setopt($ch, CURLOPT_POST, true);
445:         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
446:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
447:         // Disabling SSL Certificate support temporarly
448:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
449:         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
450:         $result = curl_exec($ch);
451:         curl_close($ch);
452:         return $result;
453:     }
454: 
455: }
456: 
learnbox_ws API documentation generated by ApiGen