/**
  * Write a log entry to the activity log
  *
  * @param $action
  *  The type of action to be logged
  * 
  * @param $params
  * An array of parameters about the log entry
  * - 'user': The user object to associate the log entry
  * - 'message': A log message
  * - (optional) 'object': A database model to associate the log with
  * - (optional) 'points_earned': The number of points earned by the action
  * - (optional) 'artwork_id': The assession number of an artwork to associate the log entry
  */
 public static function write($action, array $params)
 {
     $log = new ActivityLog();
     $log->user = $params['user'];
     $log->action = $action;
     $log->message = $params['message'];
     $log->points_earned = isset($params['points_earned']) ? $params['points_earned'] : 0;
     $log->timestamp = new DateTime('now');
     if (isset($params['artwork_id'])) {
         $log->artwork_id = $params['artwork_id'];
     }
     $log->save();
     // Associate an object if present
     if (isset($params['object']) && !empty($params['object'])) {
         $params['object']->activityLogs()->save($log);
     }
 }
 private function getResults($filter = null)
 {
     $user = Auth::getUser();
     if (!$user) {
         return;
     }
     $results = ActivityLog::byUser($user->id)->whereIn('action', ['activity', 'artwork', 'reward', 'unlocked']);
     if ($filter && $filter != 'all') {
         $results = $results->where('action', $filter);
     }
     $results->orderBy('timestamp', 'desc');
     return $results->paginate(10);
 }
 /** 
  * Execute the console command.
  * @return void
  */
 public function fire()
 {
     // Taxonomy terms
     $termRelations = $this->db->table('wp_term_relationships')->get();
     foreach ($termRelations as $relation) {
         $activity = Activity::findWordpress($relation->object_id)->first();
         if ($activity) {
             if (!$activity->categories->contains($relation->term_taxonomy_id)) {
                 $category = Category::find($relation->term_taxonomy_id);
                 if ($category) {
                     $activity->categories()->save($category);
                 }
             }
         }
     }
     // p2p connections
     $p2ps = $this->db->table('wp_p2p')->get();
     foreach ($p2ps as $p2p) {
         list($from, $t, $to) = explode('-', $p2p->p2p_type);
         switch ($from) {
             case 'activity':
                 $from = Activity::findWordpress($p2p->p2p_from)->first();
                 $from_table = 'activity';
                 break;
             case 'badge':
                 $from = Badge::findWordpress($p2p->p2p_from)->first();
                 $from_table = 'badge';
                 break;
             case 'dma-location':
                 $from = Location::findWordpress($p2p->p2p_from)->first();
                 $from_table = 'location';
                 break;
             case 'step':
                 $from = Step::findWordpress($p2p->p2p_from)->first();
                 $from_table = 'step';
                 break;
             default:
                 $from = false;
         }
         switch ($to) {
             case 'activity':
                 $to = Activity::findWordpress($p2p->p2p_to)->first();
                 $to_table = 'activity';
                 break;
             case 'badge':
                 $to = Badge::findWordpress($p2p->p2p_to)->first();
                 $to_table = 'badge';
                 break;
             case 'dma-location':
                 $to = Location::findWordpress($p2p->p2p_to)->first();
                 $to_table = 'location';
                 break;
             case 'step':
                 $to = Step::findWordpress($p2p->p2p_to)->first();
                 $to_table = 'step';
                 break;
             default:
                 $to = false;
         }
         if ($from && $to) {
             $table = 'dma_friends_' . $from_table . '_' . $to_table;
             switch ($table) {
                 case 'dma_friends_activity_step':
                     $from->steps()->save($to);
                     $this->info('activity: ' . $from->title . ' --> step: ' . $to->title);
                     break;
                 case 'dma_friends_step_badge':
                     $to->steps()->save($from);
                     $this->info('step: ' . $from->title . ' --> badge: ' . $to->title);
                     break;
                 default:
                     $values = [$from_table . '_id' => $from->id, $to_table . '_id' => $to->id];
                     if (Schema::hasTable($table)) {
                         DB::table($table)->insert($values);
                         $this->info('from: ' . $from->title . ' ----- ' . $to->title);
                     } else {
                         $this->error('table doesnt exist: ' . $table);
                     }
             }
         }
     }
     // User achievements
     $achievements = $this->db->table('wp_usermeta')->where('meta_key', '_badgeos_achievements')->get();
     $post = new Post();
     $this->info('Sync Achievements');
     foreach ($achievements as $achievement) {
         $user = User::find($achievement->user_id);
         if (empty($user)) {
             continue;
         }
         // Flush existing records
         DB::table($this->userStepTable)->where('user_id', $user->id)->delete();
         DB::table($this->userBadgeTable)->where('user_id', $user->id)->delete();
         $data = unserialize($achievement->meta_value);
         // wtf we don't need arrays in our arrays if we want to array
         $data = array_pop($data);
         foreach ($data as $d) {
             $link = ['user_id' => $user->id, 'created_at' => $post->epochToTimestamp($d->date_earned)];
             // About half way thru the data for the location key changes.
             // so lets deal with that
             if (isset($d->location)) {
                 $location_id = $d->location;
             } elseif (isset($d->location_earned)) {
                 $location_id = $d->location_earned;
             } else {
                 $location_id = null;
             }
             $location = Location::findWordpress($location_id)->first();
             if (isset($location->id)) {
                 $link['location_id'] = $location->id;
             }
             if ($d->post_type == 'step') {
                 $step = Step::findWordpress($d->ID)->first();
                 if ($step) {
                     $link['step_id'] = $step->id;
                     DB::table($this->userStepTable)->insert($link);
                 }
             } elseif ($d->post_type == 'badge') {
                 $badge = Badge::findWordpress($d->ID)->first();
                 if ($badge) {
                     $link['badge_id'] = $badge->id;
                     DB::table($this->userBadgeTable)->insert($link);
                 }
             }
         }
     }
     // Syncronize activities and users
     $this->info('Importing Activity/User relations');
     $table = 'dma_friends_activity_user';
     DB::table($table)->delete();
     ActivityLog::where('action', '=', 'activity')->chunk(100, function ($activityLogs) use($table) {
         foreach ($activityLogs as $activityLog) {
             if ($activityLog->object_id) {
                 echo '.';
                 $pivotTable = ['user_id' => $activityLog->user_id, 'activity_id' => $activityLog->object_id, 'created_at' => $activityLog->timestamp, 'updated_at' => $activityLog->timestamp];
                 DB::table($table)->insert($pivotTable);
             }
         }
     });
     // Syncronize rewards and users
     $this->info('Importing Reward/User relations');
     $table = 'dma_friends_reward_user';
     //DB::table($table)->delete();
     ActivityLog::where('action', '=', 'reward')->where('timestamp', '<', '2015-02-02 12:10:35')->chunk(100, function ($activityLogs) use($table) {
         foreach ($activityLogs as $activityLog) {
             if ($activityLog->object_id) {
                 echo '.';
                 if (Reward::find($activityLog->object_id) && User::find($activityLog->user_id)) {
                     $pivotTable = ['user_id' => $activityLog->user_id, 'reward_id' => $activityLog->object_id, 'created_at' => $activityLog->timestamp, 'updated_at' => $activityLog->timestamp];
                     DB::table($table)->insert($pivotTable);
                 }
             }
         }
     });
     $this->info('Sync complete');
 }