/**
  * Display a listing of the resource.
  *
  * @param User              $user
  * @param StickerRepository $stickerRepository
  *
  * @return Response
  */
 public function index(User $user, StickerRepository $stickerRepository)
 {
     $tasks = $user->todoTasks;
     $stickers = $user->toDoStickers()->with('tasks')->get();
     $stickersArray = [];
     foreach ($stickers as $sticker) {
         $stickersArray[$sticker->id] = $sticker->toArray();
         $pm = new ProgressManager($this->user, $sticker);
         $stickersArray[$sticker->id]['progress'] = $pm->getTaskProgress();
     }
     foreach ($tasks as $task) {
         if (empty($stickersArray[$task->stickerId])) {
             if ($sticker = $stickerRepository->find($task->stickerId)) {
                 $stickersArray[$sticker->id] = $sticker->toArray();
             } else {
                 continue;
             }
         }
         if (!array_key_exists('todoTasks', $stickersArray[$task->stickerId])) {
             $stickersArray[$task->stickerId]['todoTasks'] = [];
         }
         $stickersArray[$task->stickerId]['todoTasks'][] = $task->toArray();
     }
     return $this->response(['user' => $user, 'stickers' => array_values($stickersArray)]);
 }
 /**
  * @api            {get} /stickers/:slug Get Sticker Info
  * @apiGroup       Stickers
  * @apiDescription Returns information about a sticker, with a list of tasks.
  *
  * @param Sticker $sticker
  *
  * @return \Illuminate\Http\Response
  */
 public function show(Sticker $sticker)
 {
     $sticker->load(['tasks', 'category', 'category.stickers']);
     $stickerArray = $sticker->toArray();
     if ($this->user) {
         $pm = new ProgressManager($this->user, $sticker);
         $stickerArray['progress'] = $pm->getTaskProgress();
         if ($stickerArray['hasCompleted']) {
             $earnt = $this->user->earntStickers()->where('stickerId', $sticker->id)->first();
             $stickerArray['completedOn'] = (new DateTime($earnt->pivot->createdAt))->format(DateTime::ATOM);
         }
         usort($stickerArray['tasks'], function ($a, $b) {
             return $a['hasCompleted'] > $b['hasCompleted'];
         });
     }
     // TODO: Cache category and category.stickers
     return $this->response(['sticker' => $stickerArray]);
 }
 /**
  * - Send notification to the user
  * - Check if the user has earnt the sticker
  *
  * @param  SubmissionApproved $event
  *
  * @return void
  */
 public function handle(SubmissionApproved $event)
 {
     /** @var Post|null $post */
     $post = Post::where('submissionId', $event->submission->id)->first();
     if ($post) {
         EventLog::create(['type' => EventLog::TYPE_SUBMISSION_APPROVED, 'userId' => $event->submission->userId, 'taskId' => $event->submission->taskId, 'postId' => $post->id]);
     }
     $submission = $event->submission;
     $nm = new NotificationManager();
     $notification = $nm->createNotification($submission->userId, Notification::TYPE_SUBMISSION_APPROVED);
     $notification->submissionId = $submission->id;
     $notification->taskId = $submission->taskId;
     $nm->saveNotification($notification);
     $task = $submission->task;
     $task->completedCount += 1;
     $task->save();
     $pm = new ProgressManager($submission->user, $submission->task->sticker);
     if ($pm->completedSticker()) {
         $pm->giveSticker();
     }
     $submission->getTask()->updateRating();
 }