Skip to content

Commit 43acb25

Browse files
author
Mohamad Murad
committed
add reward points for enroll subject
1 parent f188c4b commit 43acb25

16 files changed

+442
-14
lines changed

app/Events/baseEvent.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Support\Facades\Session;
6+
7+
class baseEvent
8+
{
9+
10+
11+
public function pointSession($msg){
12+
if (Session::has('points')){
13+
Session::flash('points',array_merge(Session::get('points'),[$msg]));
14+
}else{
15+
Session::flash('points',[$msg]);
16+
}
17+
18+
}
19+
20+
21+
}

app/Events/enroll_subject.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Behavior;
6+
use Illuminate\Broadcasting\Channel;
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Broadcasting\PrivateChannel;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
use Illuminate\Support\Facades\Session;
14+
15+
class enroll_subject extends baseEvent
16+
{
17+
use Dispatchable, InteractsWithSockets, SerializesModels;
18+
19+
/**
20+
* Create a new event instance.
21+
*
22+
* @return void
23+
*/
24+
public function __construct($user, $subject)
25+
{
26+
$enroll_subject = Behavior::where('name', 'enroll_subject')->first();
27+
//$pointsCount = $subject->points ? $subject->points->count : null;
28+
$points = $enroll_subject->subject_points($subject->id);
29+
30+
foreach ($points as $point) {
31+
32+
$user->rewardPoints()->UpdateOrCreate([
33+
'points_behaviors_id' => $point->pivot->id,
34+
], [
35+
'points_behaviors_id' => $point->pivot->id,
36+
'count' => $point->count,
37+
]);
38+
39+
$this->pointSession('You are Reward ' . $point->count . ' Points');
40+
}
41+
42+
43+
// event(new BadgeGift($subject, 'enroll_subject'));
44+
}
45+
46+
/**
47+
* Get the channels the event should broadcast on.
48+
*
49+
* @return \Illuminate\Broadcasting\Channel|array
50+
*/
51+
public function broadcastOn()
52+
{
53+
return new PrivateChannel('channel-name');
54+
}
55+
}

app/Http/Controllers/EnrollController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace App\Http\Controllers;
44

5-
use App\Events\objective_complete;
6-
use App\Events\subject_complete;
5+
use App\Events\enroll_subject;
6+
77
use App\Models\Objective;
88
use App\Models\Subject;
99
use Illuminate\Support\Facades\Auth;
@@ -24,7 +24,9 @@ public function enroll(Subject $subject){
2424
$user->enrolledSubject()->attach($subject->id,[
2525
//'level_id' => Levels::all()->first()->id,
2626
]);
27-
/// event(new enroll_subject($user,$subject));
27+
28+
29+
event(new enroll_subject($user,$subject));
2830

2931

3032
if (!$placement){
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\PointsBehavior;
6+
use Illuminate\Http\Request;
7+
8+
class PointsBehaviorController extends Controller
9+
{
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Show the form for creating a new resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function create()
26+
{
27+
//
28+
}
29+
30+
/**
31+
* Store a newly created resource in storage.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @return \Illuminate\Http\Response
35+
*/
36+
public function store(Request $request)
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Display the specified resource.
43+
*
44+
* @param \App\Models\PointsBehavior $pointsBehavior
45+
* @return \Illuminate\Http\Response
46+
*/
47+
public function show(PointsBehavior $pointsBehavior)
48+
{
49+
//
50+
}
51+
52+
/**
53+
* Show the form for editing the specified resource.
54+
*
55+
* @param \App\Models\PointsBehavior $pointsBehavior
56+
* @return \Illuminate\Http\Response
57+
*/
58+
public function edit(PointsBehavior $pointsBehavior)
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Update the specified resource in storage.
65+
*
66+
* @param \Illuminate\Http\Request $request
67+
* @param \App\Models\PointsBehavior $pointsBehavior
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function update(Request $request, PointsBehavior $pointsBehavior)
71+
{
72+
//
73+
}
74+
75+
/**
76+
* Remove the specified resource from storage.
77+
*
78+
* @param \App\Models\PointsBehavior $pointsBehavior
79+
* @return \Illuminate\Http\Response
80+
*/
81+
public function destroy(PointsBehavior $pointsBehavior)
82+
{
83+
//
84+
}
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Points;
6+
use Illuminate\Http\Request;
7+
8+
class PointsController extends Controller
9+
{
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Show the form for creating a new resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function create()
26+
{
27+
//
28+
}
29+
30+
/**
31+
* Store a newly created resource in storage.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @return \Illuminate\Http\Response
35+
*/
36+
public function store(Request $request)
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Display the specified resource.
43+
*
44+
* @param \App\Models\Points $points
45+
* @return \Illuminate\Http\Response
46+
*/
47+
public function show(Points $points)
48+
{
49+
//
50+
}
51+
52+
/**
53+
* Show the form for editing the specified resource.
54+
*
55+
* @param \App\Models\Points $points
56+
* @return \Illuminate\Http\Response
57+
*/
58+
public function edit(Points $points)
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Update the specified resource in storage.
65+
*
66+
* @param \Illuminate\Http\Request $request
67+
* @param \App\Models\Points $points
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function update(Request $request, Points $points)
71+
{
72+
//
73+
}
74+
75+
/**
76+
* Remove the specified resource from storage.
77+
*
78+
* @param \App\Models\Points $points
79+
* @return \Illuminate\Http\Response
80+
*/
81+
public function destroy(Points $points)
82+
{
83+
//
84+
}
85+
}

app/Http/Controllers/SubjectController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\Behavior;
56
use App\Models\Category;
7+
use App\Models\Rules;
68
use App\Models\Subject;
79
use Illuminate\Http\Request;
810
use Illuminate\Support\Facades\Auth;
@@ -69,6 +71,14 @@ public function store(Request $request)
6971
'creator_id' => Auth::id(),
7072
'category_id' => $request->get('category_id'),
7173
]);
74+
$point = $subject->points()->create([
75+
'count' => $request->get('process_points'),
76+
]);
77+
78+
$enroll_subject = Behavior::where('name', 'enroll_subject')->first()->id;
79+
80+
$point->behavior()->attach($enroll_subject);
81+
7282
if ($request->hasFile('cover')) {
7383
$subject->addMediaFromRequest('cover')->toMediaCollection('cover');
7484
}

app/Models/Behavior.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,22 @@ public function ScopeShowing($query)
2424
{
2525
return $query->where('hidden', 0);
2626
}
27+
28+
29+
public function points()
30+
{
31+
return $this->belongsToMany(Points::class, 'points_behaviors', 'behavior_id', 'point_id')
32+
->withTimestamps();
33+
}
34+
35+
public function subject_points($id)
36+
{
37+
return $this->belongsToMany(Points::class, 'points_behaviors', 'behavior_id', 'point_id')
38+
->whereHas('subject',function ($query) use ($id){
39+
$query->where('id', '=', $id);
40+
})
41+
->withPivot('id')
42+
->withTimestamps()
43+
->get();
44+
}
2745
}

app/Models/Points.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Points extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = [
13+
'name',
14+
'icon',
15+
'count',
16+
];
17+
18+
public function behavior(){
19+
return $this->belongsToMany(Behavior::class,'points_behaviors','point_id','behavior_id')
20+
->withTimestamps();
21+
}
22+
23+
public function subject(){
24+
return $this->morphTo(Subject::class,'model_type','model_id');
25+
}
26+
}

app/Models/PointsBehavior.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class PointsBehavior extends Model
9+
{
10+
use HasFactory;
11+
}

0 commit comments

Comments
 (0)