Exemple #1
0
 public function checkForAwards($week_of = null)
 {
     $now = $week_of ? $week_of : Carbon::now()->setTimeZone('America/New_York');
     $awards = Award::getAwards();
     foreach ($awards as $award) {
         $type = $award->name;
         $freq = Award::frequencyForType($type);
         if ($freq == Award::AWARD_FREQ_WEEK && $now->dayOfWeek == Config::get('awards.award_week_day') && $now->hour >= Config::get('awards.award_week_hour')) {
             $this->log("-[{$type}] query...");
             $quary_award = Award::awardsForWeek($type, $now)->first();
             if ($quary_award == NULL) {
                 $results = ['type' => $type];
                 $new_award = NULL;
                 // -------------------------------------
                 if ($type == Award::AWARD_MOST_TASK_CLAIMED_WEEK) {
                     $top_user_created_tasks = User::orderByCreatedTasks()->first();
                     $new_award = $this->store(['user_id' => $top_user_created_tasks->id, 'name' => $type], true);
                     array_push($results, $new_award);
                 } else {
                     if ($type == Award::AWARD_MOST_TASK_CREATED_WEEK) {
                         $top_user_claimed_this_week = User::mostHelpfulForWeek()->first();
                         $new_award = $this->store(['user_id' => $top_user_claimed_this_week->id, 'name' => $type], true);
                         array_push($results, $new_award);
                     } else {
                         if ($type == Award::AWARD_MOST_ACTIVE_PROJECT_WEEK) {
                             $top_active_project = Project::orderByMostTasks()->with('user')->first();
                             $new_award = $this->store(['user_id' => $top_active_project->user->id, 'project_id' => $top_active_project->id, 'name' => $type], true);
                             array_push($results, $new_award);
                         }
                     }
                 }
                 // alter the dates
                 if ($week_of != NULL && $new_award) {
                     $new_award->created_at = $new_award->updated_at = $now;
                     $new_award->save();
                 }
                 $this->log($results);
             } else {
                 $this->log("\t[{$type}] exist Award::({$quary_award->id})");
             }
         }
     }
 }