public function onSubmit()
 {
     $params['code'] = $code = post('activity_code');
     $user = Auth::getUser();
     // Try to process activity codes first
     $activity = ActivityCode::process($user, $params);
     // If still nothing see if its an assession id
     if (!$activity) {
         $activity = LikeWorkOfArt::process($user, $params);
     }
     // FIXME : Find and run  UserMostRecentBadge component.
     // Do this before call Postman there is a bug either in Larvel or OctoberCMS.
     // Postman internally calls App::make('twig.string') but for some reason it affects View::make(...)  that is been
     // run by UserMostRecentBadge
     $mostRecent = $this->controller->findComponentByName('UserMostRecentBadge');
     $mostRecent->onRun();
     // Send Flash and kiosk notification
     $typeMessage = $activity ? 'successful' : 'error';
     $template = 'activity_code_' . $typeMessage;
     Postman::send($template, function (NotificationMessage $notification) use($user, $code, $activity) {
         // Set user in the notification
         $notification->to($user, $user->name);
         // Send code and activity just in case we want to use in the template
         $notification->addData(['code' => $code, 'activity' => $activity]);
         // Set type of flash
         //$notification->addViewSettings(['type' =>  ( $activity ) ? 'info' : 'error']);
         // Determine the content of the message
         $holder = $activity ? 'activityMessage' : 'activityError';
         $messages = Session::pull($holder);
         if (is_array($messages) && count($messages) > 1) {
             $messages = implode("<hr/>", $messages);
         } else {
             if (is_array($messages)) {
                 $messages = $messages[0];
             }
         }
         $notification->message($messages);
     }, ['flash', 'kiosk']);
     return ['#flashMessages' => $this->renderPartial('@flashMessages'), 'span.points' => number_format($user->points), 'div.most-recent-badge' => $this->controller->renderComponent('UserMostRecentBadge')];
 }
 public function onNotificationsReady()
 {
     Postman::listen(['sms', 'regex' => '/.*/'], function (IncomingMessage $message) {
         // Find user using mobile phone
         $phoneUser = $message->getFrom();
         // Getting first user match. Assuming that phone is
         // unique in the database
         if ($user = User::where('phone', $phoneUser)->orWhere('phone', str_replace('+1', '', $phoneUser))->first()) {
             // Get code from message
             $params['code'] = $code = $message->getContent();
             // process Activity code first
             if (!($activity = ActivityCode::process($user, $params))) {
                 // Not found activity with that code.
                 // Trying if is a object assession number
                 $activity = LikeWorkOfArt::process($user, $params);
             }
             // Send SMS and kiosk notification
             $typeMessage = $activity ? 'successful' : 'error';
             $template = 'activity_code_' . $typeMessage;
             Postman::send($template, function (NotificationMessage $notification) use($user, $code, $activity) {
                 // Reply to same phone number
                 $notification->to($user, $user->name);
                 // Send code and activity just in case we want to use in the template
                 $notification->addData(['code' => $code, 'activity' => $activity, 'viaSMS' => true]);
                 // Determine the content of the message
                 $holder = $activity ? 'activityMessage' : 'activityError';
                 $message = Session::pull($holder);
                 if (is_array($message)) {
                     foreach ($message as $m) {
                         $notification->message($m);
                     }
                 } else {
                     $notification->message($message);
                 }
             }, ['sms', 'kiosk']);
             Log::debug('Incoming SMS', ['user' => $user, 'code' => $code, 'activity' => $activity]);
         } else {
             Postman::send('simple', function (NotificationMessage $notification) use($phoneUser) {
                 $user = new User();
                 $user->phone = $phoneUser;
                 // Reply to same phone number
                 $notification->to($user, $user->name);
                 $notification->message(Lang::get('dma.friends::lang.user.memberPhoneNotFound'));
             }, ['sms']);
         }
     });
 }
 public function onComplete()
 {
     // Get submit data
     $json = post('completion');
     // Validate json submission
     $data = $this->validateSubmission($json);
     if ($data) {
         $params['code'] = $data['activity']->activity_code;
         $user = Auth::getUser();
         // Make sure the activity is NOT ignored before completing
         // Completed activites cannot be ignored (or else elastic has trouble filtering them properly)
         $rating = Rating::where('activity_id', $data['activity']->id)->where('user_id', $user->id)->where('rating', 0)->first();
         if ($rating) {
             Rating::where('activity_id', $data['activity']->id)->where('user_id', $user->id)->delete();
         }
         $activity = ActivityCode::process($user, $params);
         Flash::success('Great! What do you want to do next?<br /><strong>Completed:</strong> ' . $activity->title);
     }
     // return flash message
     return ['#flashMessages' => $this->renderPartial('@flashMessages')];
 }
 /**
  * 
  * @param RainLab\User\Models\User  $user
  * @param string $code
  * @return Array
  * Return an associative code with a intended HTTP Code
  * and an Array with information of the activity code  
  * 
  */
 protected function processActivityCode($user, $code)
 {
     $params = [];
     // Get code from message
     $params['code'] = $code;
     // process Activity code first
     if (!($activity = ActivityCode::process($user, $params))) {
         // Not found activity with that code.
         // Trying if is a object assession number
         $activity = LikeWorkOfArt::process($user, $params);
     }
     // Determine the content of the message
     $holder = $activity ? 'activityMessage' : 'activityError';
     $message = Session::pull($holder);
     if (is_array($message)) {
         $message = implode('\\n', array_filter($message));
     }
     $payload = ['success' => $activity ? true : false, 'activity_code' => $code, 'message' => $message, 'feedback_message' => $activity ? $activity->feedback_message : null, 'complete_message' => $activity ? $activity->complete_message : null];
     $httpCode = 200;
     // Check if is not a boolean if not is because is an intance of an activity
     if (!is_bool($activity)) {
         // Check if and artwork is attached to the activity
         // if so this activity is a LikeWorkOfArt
         $objectData = array_get($activity, 'objectData', null);
         if (!is_null($objectData)) {
             $httpCode = 201;
             $payload['data']['artwork'] = $objectData;
         }
     }
     return ['http_code' => $httpCode, 'payload' => $payload];
 }