This commit is contained in:
m.zargarov 2024-06-25 00:52:48 +04:00
parent 321ce81ff0
commit c7fefd138a
10 changed files with 79 additions and 27 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace App\Console\Commands;
use App\Models\Grade;
use Illuminate\Console\Command;
class AddScores extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:add-scores {grade}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add 4 and 5 scores for students';
/**
* Execute the console command.
*/
public function handle()
{
$grade = Grade::firstWhere('id', $this->argument('grade'));
$grade->students->random(7)->each(function ($student) {
$student
->lessons()
->syncWithPivotValues($student->lessons()->pluck('id'), ['score' => 4]);
});
$grade->students->random(5)->each(function ($student) {
$student
->lessons()
->syncWithPivotValues($student->lessons()->pluck('id'), ['score' => 5]);
});
}
}

View File

@ -69,7 +69,7 @@ class GradeController extends Controller
*/
public function edit(Grade $grade): View
{
if(request()->user()->cannot('update', Grade::class)) {
if(request()->user()->cannot('update', $grade)) {
abort(403);
}
@ -83,11 +83,13 @@ class GradeController extends Controller
*/
public function update(GradePostRequest $request, Grade $grade): RedirectResponse
{
if(request()->user()->cannot('update', Grade::class)) {
if(request()->user()->cannot('update', $grade)) {
abort(403);
}
return redirect()->route('grades.show', $grade->update($request->validated()));
$grade->update($request->validated());
return redirect()->route('grades.show', $grade);
}
/**
@ -95,9 +97,10 @@ class GradeController extends Controller
*/
public function destroy(Grade $grade): RedirectResponse
{
if(request()->user()->cannot('delete', Grade::class)) {
if(request()->user()->cannot('delete', $grade)) {
abort(403);
}
$grade->delete();
return redirect()->route('grades.index');

View File

@ -113,12 +113,9 @@ class LessonController extends Controller
abort(403);
}
return redirect()->route(
'grades.lessons.show',[
$grade,
$lesson->update($request->validated()),
]
);
$lesson->update($request->validated());
return redirect()->route('grades.lessons.show',[$grade, $lesson,]);
}
/**

View File

@ -82,10 +82,9 @@ class SubjectController extends Controller
abort(403);
}
return redirect()->route(
'subjects.show',
$subject->update($request->validated())
);
$subject->update($request->validated());
return redirect()->route('subjects.show', $subject);
}
/**

View File

@ -6,6 +6,7 @@ use App\Models\Admin;
use App\Models\Grade;
use App\Models\Lesson;
use App\Models\Student;
use App\Models\Teacher;
use App\Models\User;
class LessonPolicy
@ -13,9 +14,9 @@ class LessonPolicy
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user, Grade $grade): bool
public function viewAny(User $user): bool
{
return $user->userable_type != Student::class || $user->userable->grade_id == $grade->id;
return $user->userable_type == Teacher::class;
}
/**
@ -23,7 +24,7 @@ class LessonPolicy
*/
public function view(User $user, Lesson $lesson): bool
{
return $user->userable_type != Student::class || $user->userable->grade_id == $lesson->grade_id;
return $user->userable_type == Teacher::class;
}
/**
@ -31,7 +32,7 @@ class LessonPolicy
*/
public function create(User $user): bool
{
return $user->userable_type != Student::class;
return $user->userable_type == Teacher::class;
}
/**
@ -39,7 +40,7 @@ class LessonPolicy
*/
public function update(User $user, Lesson $lesson): bool
{
return $user->userable_type != Student::class;
return $user->userable_type == Teacher::class;
}
/**
@ -47,6 +48,6 @@ class LessonPolicy
*/
public function delete(User $user, Lesson $lesson): bool
{
return $user->userable_type != Student::class;
return $user->userable_type == Teacher::class;
}
}

View File

@ -42,4 +42,9 @@ class SubjectPolicy
{
return $user->userable_type == Student::class;
}
public function pdf(User $user): bool
{
return $user->userable_type == Student::class;
}
}

View File

@ -15,6 +15,7 @@ return [
'commands' => [
App\Console\Commands\AddAdmin::class,
App\Console\Commands\AddScores::class,
],
/*

View File

@ -36,11 +36,13 @@
class="btn btn-block col-8">{{ $grade->name }}</a>
</div>
</td>
<td>
<div>
<a href="{{ route('grades.lessons.index', $grade) }}" class="btn btn-primary">Занятия</a>
</div>
</td>
@can('viewAny', \App\Models\Lesson::class)
<td>
<div>
<a href="{{ route('grades.lessons.index', $grade) }}" class="btn btn-primary">Занятия</a>
</div>
</td>
@endcan
@can('list', \App\Models\Grade::class)
<td>
<div>

View File

@ -69,7 +69,9 @@
@can('create', \App\Models\Subject::class)
<a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a>
@endcan
<a href="{{ route('export-pdf') }}" class="btn btn-info">PDF</a>
@can('pdf', \App\Models\Subject::class)
<a href="{{ route('export-pdf') }}" class="btn btn-info">PDF</a>
@endcan
</div>
<div class="card-footer">{{ $subjects->links() }}</div>
</div>

View File

@ -33,7 +33,6 @@ Route::middleware('auth')->group(function () {
'subjects' => SubjectController::class,
'students' => StudentController::class,
'teachers' => TeacherController::class,
'grades.lessons' => LessonController::class,
]);
Route::middleware([AdminAction::class])->group(function () {