Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visitor logs #3468

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion Modules/Project/Entities/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
use Modules\Project\Database\Factories\ProjectFactory;
use Modules\User\Entities\User;
use OwenIt\Auditing\Contracts\Auditable;
use App\Traits\PageVisitable;

class Project extends Model implements Auditable
{
use HasFactory, HasTags, Filters, SoftDeletes, \OwenIt\Auditing\Auditable;
use HasFactory, HasTags, Filters, SoftDeletes, \OwenIt\Auditing\Auditable, PageVisitable;

protected $table = 'projects';

Expand Down
336 changes: 172 additions & 164 deletions Modules/Project/Resources/views/index.blade.php

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Modules/Project/Resources/views/menu_header.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<ul class="nav nav-pills mt-3 mb-2">
@php
$filters = request()->except('page');
$isUserAdmin = auth()->user()->isAdmin() || auth()->user()->isSuperAdmin();
@endphp

<li class="nav-item mr-3">
Expand All @@ -26,4 +27,14 @@
href="{{ route('project.index', array_merge($filters, ['status' => 'inactive', 'is_amc' => '0'])) }}">Inactive
Projects({{ $inactiveProjectsCount }})</a>
</li>

@if ($isUserAdmin)
<li class="nav-item">
<a class="nav-link {{ request()->input('status', 'active') == 'visitors_log' ? 'active' : '' }}"
href="{{ route('project.index', array_merge($filters, ['status' => 'visitors_log', 'visit_interval' => 'daily'])) }}"
>
Visitors Log
</a>
</li>
@endif
</ul>
62 changes: 62 additions & 0 deletions Modules/Project/Resources/views/visitors_log.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<div class="mb-2">
<form class="d-md-flex justify-content-between ml-2 my-3"
action="{{ route('project.index', ['status' => 'visitors_log']) }}">
<div class='d-flex justify-content-between align-items-md-center mb-2 mb-xl-0'>
<h4 class="">
Visitor Log
</h4>
<input type="hidden" name="status" value="{{ $status }}">
<select class="fz-14 fz-lg-16 p-1 bg-info ml-3 my-auto text-white rounded border-0" name="visit_interval"
onchange="this.form.submit()">
<option
value="daily"
{{ request()->get('visit_interval') == 'daily' ? 'selected' : '' }}
>
{{ __('Daily') }}
</option>

<option
value="weekly"
{{ request()->get('visit_interval') == 'weekly' ? 'selected' : '' }}
>
{{ __('Weekly') }}
</option>

<option
value="monthly"
{{ request()->get('visit_interval') == 'monthly' ? 'selected' : '' }}
>
{{ __('Monthly') }}
</option>
</select>
</div>
</form>

<div>
<table class="table">
<thead>
<tr>
<th scope="col">S. No.</th>
<th scope="col">Name</th>
<th scope="col">Page Path</th>
<th scope="col">Visit Count</th>
<th scope="col">Visit Date</th>
</tr>
</thead>
<tbody>
@foreach ($visitor_logs as $visitor_log)
@php
$user = $visitor_log->user()->withTrashed()->first();
@endphp
<tr>
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ optional($user)->name }}</td>
<td>{{ $visitor_log->page_path }}</td>
<td>{{ $visitor_log->visit_count }}</td>
<td>{{ $visitor_log->created_at->format(config('constants.full_display_date_format')) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
19 changes: 19 additions & 0 deletions app/Models/PageVisit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Modules\User\Entities\User;

class PageVisit extends Model
{
use HasFactory;

protected $fillable = ['user_id', 'page_path', 'visit_count'];

public function user()

Check warning on line 15 in app/Models/PageVisit.php

View check run for this annotation

Codecov / codecov/patch

app/Models/PageVisit.php#L15

Added line #L15 was not covered by tests
{
return $this->belongsTo(User::class);

Check warning on line 17 in app/Models/PageVisit.php

View check run for this annotation

Codecov / codecov/patch

app/Models/PageVisit.php#L17

Added line #L17 was not covered by tests
}
}
52 changes: 52 additions & 0 deletions app/Traits/PageVisitable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Traits;

use App\Models\PageVisit;

trait PageVisitable
{
public function recordPageVisit($userId, $pagePath)

Check warning on line 9 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L9

Added line #L9 was not covered by tests
{
$today = now()->today();

Check warning on line 11 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L11

Added line #L11 was not covered by tests

$visit = PageVisit::where('user_id', $userId)->where('page_path', $pagePath)->whereDate('created_at', $today)->first();

Check warning on line 13 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L13

Added line #L13 was not covered by tests

if (! $visit) {
$visit = new PageVisit;
$visit->user_id = $userId;
$visit->page_path = $pagePath;

Check warning on line 18 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L15-L18

Added lines #L15 - L18 were not covered by tests
}

$visit->visit_count++;
$visit->save();

Check warning on line 22 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L21-L22

Added lines #L21 - L22 were not covered by tests
}

public function getPageVisits($pagePath, $interval = 'daily')

Check warning on line 25 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L25

Added line #L25 was not covered by tests
{
$visits = PageVisit::where('page_path', $pagePath);
$this->applyTimeIntervalFilter($visits, $interval);

Check warning on line 28 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L27-L28

Added lines #L27 - L28 were not covered by tests

return $visits->orderByDesc('visit_count')->get();

Check warning on line 30 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L30

Added line #L30 was not covered by tests
}

protected function applyTimeIntervalFilter($query, $interval)

Check warning on line 33 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L33

Added line #L33 was not covered by tests
{
$today = now()->today();

Check warning on line 35 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L35

Added line #L35 was not covered by tests

switch ($interval) {
case 'daily':
$query->whereDate('created_at', $today);
break;

Check warning on line 40 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L38-L40

Added lines #L38 - L40 were not covered by tests

case 'weekly':
$query->whereDate('created_at', '>=', $today->startOfWeek())
->whereDate('created_at', '<=', $today->endOfWeek());
break;

Check warning on line 45 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L42-L45

Added lines #L42 - L45 were not covered by tests

case 'monthly':
$query->whereMonth('created_at', $today->month);
break;

Check warning on line 49 in app/Traits/PageVisitable.php

View check run for this annotation

Codecov / codecov/patch

app/Traits/PageVisitable.php#L47-L49

Added lines #L47 - L49 were not covered by tests
}
}
}
35 changes: 35 additions & 0 deletions database/migrations/2024_03_12_151035_create_page_visits_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePageVisitsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('page_visits', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('user_id');
$table->string('page_path');
$table->unsignedBigInteger('visit_count')->default(0);
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('page_visits');
}
}
Loading