public static function getLastNDaysActivity($n, $activityType, $quiz = null)
 {
     $activityHistory = QuizUserActivity::select(DB::raw('DATE(created_at) as date'), DB::raw('count(*) as activityCount'))->where('type', '=', $activityType)->where('created_at', '>', DB::raw('DATE_SUB(NOW(), INTERVAL ' . $n . ' DAY)'));
     if ($quiz) {
         $activityHistory = $activityHistory->where('quiz_id', '=', $quiz->id);
     }
     $activityHistory = $activityHistory->groupBy('date')->get()->toArray();
     $lastNDays = self::lastNDays($n);
     $lastNDaysActivity = array();
     foreach ($lastNDays as $key => $day) {
         $lastNDaysActivity[$key] = array('date' => $day, $activityType => 0);
         foreach ($activityHistory as $activity) {
             if ($activity['date'] === $day) {
                 $lastNDaysActivity[$key][$activityType] = $activity['activityCount'];
             }
         }
     }
     return $lastNDaysActivity;
 }
 public function activity($nameString, $quizId = null, $activityType)
 {
     try {
         $quiz = Quiz::findOrFail($quizId);
         $user = Auth::user();
         if (!$user) {
             return Response::json(array('error' => 'Not logged in'), 400);
         }
         $quizUserActivity = QuizUserActivity::firstOrNew(array('user_id' => $user->id, 'quiz_id' => $quiz->id, 'type' => $activityType));
         if ($quizUserActivity->created_at) {
             $quizUserActivity->touch();
         } else {
             $quizUserActivity->save();
         }
         self::incrementQuizStats($quiz, $activityType);
         return Response::json(array('message' => 'Activity recorded'));
     } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
         return Response::notFound('Quiz not found');
     }
 }