Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 34
DashboardController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 34
 index
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 34
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
class DashboardController extends Controller
{
    /**
     * Show the stats of the website.
     * @return Factory|View
     */
    public function index() :View
    {
        $articles = DB::table('articles')
            ->join('article_descriptions', 'articles.id', 'article_descriptions.article_id')
            ->orderByDesc('visit_counts')
            ->select(['article_descriptions.title', 'articles.visit_counts'])
            ->where('article_descriptions.language_id', currentLanguage()->id)
            ->get()->take(10);
        $difDays = date('d');
        $difMonths = date('n');
        $difDays = 30 - $difDays . ' day';
        $difMonths = 12 - $difMonths . ' month';
        $lastYear = date("Y-m-d", strtotime("-1 year $difMonths $difDays"));
        $nextYear = date("Y-m-d", strtotime("0 year $difMonths $difDays"));
        $lastWeek = date("Y-m-d", strtotime("-1 week"));
        $nextWeek = date("Y-m-d", strtotime("1 day")); // to include today in the statistics
        $toDay = date("Y-m-d");
        $days = [];
        $websiteVisitors = DB::table('website_visitors')
            ->orderBy('created_at', 'asc')
            ->whereBetween('created_at', [$lastYear, $nextYear])
            ->pluck('created_at')->toArray();
        $weekVisitors = DB::table('website_visitors')
            ->orderBy('created_at', 'asc')
            ->whereBetween('created_at', [$lastWeek, $nextWeek])
            ->pluck('created_at')->toArray();
//        $websiteVisitors = DB::table('website_visitors')
//            ->select(DB::raw('CAST(created_at as date) as created_at'))
//            ->distinct('created_at')->pluck('created_at')->toArray();
        foreach ($websiteVisitors as $key => $websiteVisitor) {
            $websiteVisitors[$key] = date('n', strtotime($websiteVisitor));
            $days[] = date('Y-m-d', strtotime($websiteVisitor));
        }
        $monthVisitCounts = array_count_values($websiteVisitors);
        $allVisitorCounts = count($websiteVisitors);
        $weekVisitorCounts = count($weekVisitors);
        $days = array_count_values($days);
        $dayVisitCounts = array_key_exists($toDay, $days) ? $days[$toDay] : '';
        return view('admin.index', compact(
            ['articles','monthVisitCounts','allVisitorCounts','weekVisitorCounts','dayVisitCounts',]
        ));
    }
}