/** * Execute the console command. * * @return mixed */ public function handle() { $motions = Motion::active()->get(); $now = Carbon::now(); foreach ($motions as $motion) { $motionRankAttr = DB::table('votes')->where('motion_id', $motion->id)->selectRaw('motion_id, sum(position) as rank')->groupBy('motion_id')->first(); if (!$motionRankAttr) { $rank = 0; } else { $rank = $motionRankAttr->rank; } $motionRank = MotionRank::create(array('rank' => $rank, 'motion_id' => $motion->id)); // If it is within the autoextend period if ($motionRank->created_at['carbon']->diffInHours($motion->closing['carbon']) <= Setting::get('motion.hours_before_closing_autoextend', 12)) { $motionRanks = MotionRank::where('motion_id', $motion->id)->latest()->get(); if ($motionRanks->count() >= 2) { // Make sure there are two records to compare against each other $currentRank = $motionRanks[0]->rank; $previousRank = $motionRanks[1]->rank; if ($currentRank > 0 && $previousRank < 0 || $currentRank < 0 && $previousRank > 0) { //If between these two it changed $motion->closing = $motion->closing['carbon']->addHours(Setting::get('motion.hours_to_autoextend_by', 12)); $motion->save(); } } } if ($motion->closing['carbon'] <= Carbon::now()) { // Close the motion, no more ranks after this one DB::table('motions')->where('id', $motion->id)->update(['active' => 0]); } } }
/** * Handle the event. * * @param SendDailyEmails $event * @return void */ public function handle(SendDailyEmails $event) { $users = User::validVoter()->get(); //Get users who want a daily summary $dailySummaryEmailUsers = $users->filter(function ($user) { return $user->preferences['emails']['daily_summary']; }); // Get latest or new motion $latestLaunchedMotions = Motion::active()->updatedAfter(Carbon::now()->subHours(24))->get(); // \DB::enableQueryLog(); $recentlyClosedMotions = Motion::expired()->closingAfter(Carbon::now()->subHours(24))->get(); // echo print_r(\DB::getQueryLog()); $closingSoonMotions = Motion::active()->closingAfter(Carbon::now())->closingBefore(Carbon::now()->addHours(24))->get(); $data = array("latestLaunchedMotions" => $latestLaunchedMotions, "recentlyClosedMotions" => $recentlyClosedMotions, "closingSoonMotions" => $closingSoonMotions, "title" => "Daily Summary"); if ($latestLaunchedMotions->isEmpty() && $recentlyClosedMotions->isEmpty() && $closingSoonMotions->isEmpty()) { //No updates today return true; } foreach ($users as $user) { Mail::send('emails.summary', $data, function ($m) use($user) { $m->to($user->email, $user->first_name . ' ' . $user->last_name)->subject('Daily IserveU Summary'); }); } }