CourseWork/app/Services/StudentService.php

101 lines
2.7 KiB
PHP
Raw Normal View History

2024-04-23 14:52:49 +04:00
<?php
namespace App\Services;
2024-05-27 18:40:22 +04:00
use App\Enums\ScoreEnum;
2024-04-23 14:52:49 +04:00
use App\Models\Student;
2024-06-23 17:02:18 +04:00
use App\Models\Subject;
2024-04-23 14:52:49 +04:00
use App\Models\User;
2024-06-25 01:42:23 +04:00
use Barryvdh\DomPDF\Facade\Pdf;
2024-06-23 00:58:31 +04:00
use Illuminate\Database\Eloquent\Collection;
2024-04-23 14:52:49 +04:00
use Illuminate\Pagination\LengthAwarePaginator;
2024-06-23 00:58:31 +04:00
use Illuminate\Support\Facades\Auth;
2024-04-23 14:52:49 +04:00
2024-06-16 12:20:48 +04:00
class StudentService
2024-04-23 14:52:49 +04:00
{
2024-04-27 15:10:21 +04:00
public function create(array $data): Student
2024-04-23 14:52:49 +04:00
{
2024-04-27 15:10:21 +04:00
$user = User::create([
'email' => $data['email'],
'password' => $data['password'],
]);
$student = Student::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
'grade_id' => $data['grade_id'],
]);
2024-04-23 14:52:49 +04:00
$student->user()->save($user);
2024-05-27 18:40:22 +04:00
$student
->lessons()
->syncWithPivotValues($student->grade->lessons->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
2024-04-23 14:52:49 +04:00
return $student;
}
2024-06-16 12:20:48 +04:00
public function update(Student $student, array $data): Student
2024-04-23 14:52:49 +04:00
{
2024-06-16 12:20:48 +04:00
$student->user()->update([
2024-04-27 15:10:21 +04:00
'email' => $data['email'],
'password' => $data['password'],
]);
2024-06-16 12:20:48 +04:00
$student->update([
2024-04-27 15:10:21 +04:00
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
'grade_id' => $data['grade_id'],
]);
2024-04-23 14:52:49 +04:00
2024-06-16 12:20:48 +04:00
return $student;
2024-04-23 14:52:49 +04:00
}
public function delete($model): void
{
2024-04-27 15:10:21 +04:00
$model->user()->delete();
$model->delete();
2024-04-23 14:52:49 +04:00
}
2024-06-23 00:58:31 +04:00
2024-06-23 17:02:18 +04:00
public function getScores(Subject $subject): Collection
2024-06-23 00:58:31 +04:00
{
$student = Auth::user()->userable;
2024-06-23 17:02:18 +04:00
return $student->lessons()->where('subject_id', $subject->id)->get();
}
public function getAvgScore(Subject $subject)
{
$student = Auth::user()->userable;
$scores = $student
->lessons()
->where('subject_id', $subject->id)
->whereIn('score', ScoreEnum::getNumScores())
->pluck('score');
return round($scores->avg(), 2);
}
public function getDebts(): Collection
{
$student = Auth::user()->userable;
return $student->lessons()->whereIn('score', ScoreEnum::getDebtScores())->get();
2024-06-23 00:58:31 +04:00
}
2024-06-25 01:42:23 +04:00
public function exportAvgScores()
{
$subjects = Auth::user()->userable->grade->subjects;
$avgScores = collect();
$subjects->each(function ($subject) use ($avgScores) {
$avgScores->put($subject->name, $this->getAvgScore($subject));
});
return Pdf::loadView('students.avg-scores', [
'avgScores' => $avgScores,
])->download('Успеваемость.pdf');
}
2024-04-23 14:52:49 +04:00
}