Skip to content

Commit a03d7f0

Browse files
committed
add notifications
1 parent e28f9ff commit a03d7f0

File tree

11 files changed

+365
-44
lines changed

11 files changed

+365
-44
lines changed

app/Helpers/MainHelper.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
namespace App\Helpers;
3+
use App\Models\MenuLink;
4+
use Illuminate\Support\Facades\Notification;
5+
class MainHelper {
6+
7+
protected static $lowerLimit = 70;
8+
protected static $upperLimit = 255;
9+
protected static $colorGap = 20;
10+
protected static $generated = array();
11+
12+
public static function notify_user(
13+
$options=[]
14+
){
15+
$options = array_merge([
16+
'user_id'=>1,
17+
'content'=>[],
18+
'action_url'=>"",
19+
'methods'=>['database'],
20+
'image'=>"",
21+
'btn_text'=>"عرض الإشعار"
22+
],$options);
23+
$user = \App\Models\User::where('id',$options['user_id'])->first();
24+
if($user!=null){
25+
\App\Models\User::where('email', $user->email )->first()->notify(
26+
new \App\Notifications\GeneralNotification([
27+
'content'=>$options['content'],
28+
'action_url'=>$options['action_url'],
29+
'btn_text'=>$options['btn_text'],
30+
'methods'=>$options['methods'],
31+
'message'=>$options['message'],
32+
'image'=>$options['image']
33+
])
34+
);
35+
}
36+
}
37+
38+
39+
public static function notify_visitors(
40+
$options=[]
41+
){
42+
43+
$options = array_merge([
44+
'emails'=>["admin@admin.com"],
45+
'content'=>[],
46+
'action_url'=>"",
47+
'methods'=>['mail'],
48+
'image'=>"",
49+
'btn_text'=>"عرض الإشعار"
50+
],$options);
51+
52+
dd($options['emails']);
53+
Notification::route('mail', $options['emails'])
54+
->notify(new \App\Notifications\GeneralNotification([
55+
'content'=>$options['content'],
56+
'action_url'=>$options['action_url'],
57+
'btn_text'=>$options['btn_text'],
58+
'methods'=>$options['methods'],
59+
'image'=>$options['image']
60+
]));
61+
}
62+
63+
64+
65+
public static function slug($string){
66+
$t = $string;
67+
$specChars = array(
68+
' ' => '-', '!' => '', '"' => '',
69+
'#' => '', '$' => '', '%' => '',
70+
'&amp;' => '','&nbsp;' => '',
71+
'\'' => '', '(' => '',
72+
')' => '', '*' => '', '+' => '',
73+
',' => '', '' => '', '.' => '',
74+
'/-' => '', ':' => '', ';' => '',
75+
'<' => '', '=' => '', '>' => '',
76+
'?' => '', '@' => '', '[' => '',
77+
'\\' => '', ']' => '', '^' => '',
78+
'_' => '', '`' => '', '{' => '',
79+
'|' => '', '}' => '', '~' => '',
80+
'-----' => '-', '----' => '-', '---' => '-',
81+
'/' => '', '--' => '-', '/_' => '-',
82+
);
83+
foreach ($specChars as $k => $v) {
84+
$t = str_replace($k, $v, $t);
85+
}
86+
87+
return substr($t,0,230);
88+
}
89+
90+
91+
92+
93+
}

app/Http/Controllers/EnrollController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Events\enroll_subject;
66

77
use App\Events\objective_complete;
8+
use App\Helpers\MainHelper;
89
use App\Models\Assignment;
910
use App\Models\Level;
1011
use App\Models\Objective;
@@ -28,8 +29,14 @@ public function enroll(Subject $subject){
2829
// new enroll
2930
$user->enrolledSubject()->attach($subject->id,[
3031
'level_id' => Level::all()->first()->id,
32+
'points' => 0,
33+
]);
34+
(new MainHelper)->notify_user([
35+
'user_id'=>1,
36+
'message'=>"Student $user->name has enroll $subject->name Subject" ,
37+
'url'=>"http://example.com",
38+
'methods'=>['database']
3139
]);
32-
3340

3441
event(new enroll_subject($user,$subject));
3542

app/Http/Controllers/FrontContrller.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ public function subjectInfo(Subject $subject)
4545
if ($subject_enroll) {
4646
$authLevel = Level::findOrFail($subject_enroll->pivot->level_id);
4747
$thisLevel =SubjectLevel::where('subject_id', $subject->id)->where('level_id',$authLevel->id)->first();
48-
$nextLevel = SubjectLevel::where('subject_id', $subject->id)
49-
->orderBy('point')->where('point', '>',$thisLevel->point)
50-
->where('level_id','!=',$thisLevel->id)->first();
48+
if ($thisLevel){
49+
$nextLevel = SubjectLevel::where('subject_id', $subject->id)
50+
->orderBy('point')->where('point', '>',$thisLevel->point)
51+
->where('level_id','!=',$thisLevel->id)->first();
52+
}
53+
5154

5255
}
5356

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\View\Components\Notifications as NotificationComponent;
7+
class NotificationsController extends Controller
8+
{
9+
public function index(Request $request){
10+
auth()->user()->unreadNotifications->markAsRead();
11+
$notifications = \Auth::user()->notifications()->simplePaginate();
12+
return view('admin.notifications.index',compact('notifications'));
13+
}
14+
public function notifications_see(Request $request){
15+
session(['seen_notifications'=>0]);
16+
auth()->user()->unreadNotifications->markAsRead();
17+
}
18+
19+
public function notifications_ajax(Request $request){
20+
21+
$notifications = \Auth::user()->notifications()->limit(15)->get();
22+
$not_response = array(
23+
'response' => (new NotificationComponent($notifications))->render(1),
24+
'count_unseen_notifications' => intval($notifications->whereNull('read_at')->count()),
25+
);
26+
$data = [
27+
'notifications' => [
28+
'response' => $not_response,
29+
'counter_session' => intval(session('seen_notifications')),
30+
],
31+
'alert' => false,
32+
];
33+
34+
if ($data['notifications']['counter_session'] < $not_response['count_unseen_notifications'])
35+
$data['alert'] = true;
36+
37+
session(['seen_notifications' => $not_response['count_unseen_notifications']]);
38+
return $data;
39+
}
40+
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Notifications\Messages\MailMessage;
8+
use Illuminate\Notifications\Notification;
9+
10+
use NotificationChannels\Telegram\TelegramChannel;
11+
use NotificationChannels\Telegram\TelegramMessage;
12+
13+
class GeneralNotification extends Notification implements ShouldQueue
14+
{
15+
use Queueable;
16+
public $tries = 2;
17+
public $timeout = 10;
18+
public $options;
19+
20+
public function __construct($options=[]){
21+
array_merge([
22+
'content'=>"",
23+
'action_url'=>env("APP_URL"),
24+
'btn_text'=>env("APP_NAME"),
25+
'methods'=>['database'],
26+
'image'=>env("DEFAULT_IMAGE_AVATAR"),
27+
'inline_content'=>""
28+
],$options);
29+
$this->options=$options;
30+
$this->options['inline_content']=implode("\n",$options['content']);
31+
}
32+
33+
34+
public function via($notifiable){
35+
return $this->options['methods'];
36+
37+
}
38+
39+
public function toMail($notifiable){
40+
41+
return (new MailMessage)
42+
->subject("لديك إشعار جديد")
43+
->greeting("مرحباً")
44+
->line($this->options['inline_content'])
45+
->action($this->options['btn_text'], $this->options['action_url']);
46+
47+
}
48+
public function toDatabase($notifiable){
49+
50+
$content=$this->options['message'];
51+
return [
52+
// 'message'=>'<a href="'.$this->options['action_url'].'">'.$content.'</a>',
53+
'message'=>$content,
54+
'image'=>$this->options['image'],
55+
];
56+
}
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateNotificationsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('notifications', function (Blueprint $table) {
17+
$table->uuid('id')->primary();
18+
$table->string('type');
19+
$table->morphs('notifiable');
20+
$table->text('data');
21+
$table->timestamp('read_at')->nullable();
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('notifications');
34+
}
35+
}

resources/views/frontend/subjects/info.blade.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@
1111
<div class="widget-content">
1212
<div class="content">
1313
<div class="price">Level</div>
14-
<div class="form-progress">
15-
<progress class="form-progress-bar" min="{{$thisLevel->point}}"
16-
max="{{$nextLevel? $nextLevel->point : $thisLevel->point}}"
17-
value="{{$totalPoints}}"
18-
aria-labelledby="form-progress-completion"></progress>
19-
20-
<div class="form-progress-indicator one active">
21-
<img src=" {{$thisLevel->level->getFirstMediaUrl('icon')}}">
22-
</div>
23-
<div class="form-progress-indicator two {{$nextLevel ? '':'active'}}">
24-
@if($nextLevel)
25-
<img src=" {{$nextLevel->level->getFirstMediaUrl('icon')}}">
26-
@else
27-
<img src=" {{$thisLevel->level->getFirstMediaUrl('icon')}}">
28-
@endif
14+
@if(\Illuminate\Support\Facades\Auth::check() && $subject->authEnrolledStudent())
15+
<div class="form-progress">
16+
<progress class="form-progress-bar" min="{{$thisLevel->point ?? 0}}"
17+
max="{{$nextLevel? $nextLevel->point ?? 0 : $thisLevel->point ?? 0}}"
18+
value="{{$totalPoints}}"
19+
aria-labelledby="form-progress-completion"></progress>
20+
21+
<div class="form-progress-indicator one active">
22+
<img src=" {{$thisLevel ? $thisLevel->level->getFirstMediaUrl('icon') : null }}">
23+
</div>
24+
<div class="form-progress-indicator two {{$nextLevel ? '':'active'}}">
25+
@if($nextLevel)
26+
<img src=" {{$nextLevel->level->getFirstMediaUrl('icon')}}">
27+
@else
28+
{{-- <img src=" {{$thisLevel->level->getFirstMediaUrl('icon')}}">--}}
29+
@endif
30+
</div>
31+
2932
</div>
33+
@endif
3034

31-
</div>
3235
<div>
3336
{{-- @if($authLevel)--}}
3437
{{-- <p>Level <b>{{$authLevel->name}}</b> from <b>{{$lastLevel->name}}</b></p>--}}

resources/views/layouts/app.blade.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@
1818

1919
<link rel="stylesheet" href="{{ asset('assets/css/toastr.css') }}">
2020
<link rel="stylesheet" href="{{ asset('assets/css/custom.css') }}">
21+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
2122
@livewireStyles
22-
23+
@if(auth()->check())
24+
@php
25+
if(session('seen_notifications')==null)
26+
session(['seen_notifications'=>0]);
27+
$notifications=auth()->user()->notifications()->orderBy('created_at','DESC')->limit(50)->get();
28+
$unreadNotifications=auth()->user()->unreadNotifications()->count();
29+
@endphp
30+
@endif
2331
</head>
2432
<body class="g-sidenav-show bg-gray-100">
2533
<div class="min-height-300 bg-primary position-absolute w-100"></div>

0 commit comments

Comments
 (0)