You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.3 KiB
74 lines
2.3 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Http\Resources\ExerciseResource;
|
|
use App\Models\Exercise;
|
|
|
|
class ExerciseService
|
|
{
|
|
|
|
public function getExercises($params)
|
|
{
|
|
$limit = isset($params['limit']) ? min((int)$params['limit'], 100) : 10;
|
|
$offset = isset($params['offset']) ? max((int)$params['offset'], 0) : 0;
|
|
$lessonName = $params['search'] ?? null;
|
|
|
|
$query = Exercise::select('id', 'lesson_name', 'description', 'status', 'level', 'year', 'created_at');
|
|
|
|
if (!empty($lessonName)) {
|
|
$query->where('lesson_name', 'like', '%' . $lessonName . '%');
|
|
}
|
|
|
|
// Lấy dữ liệu chính
|
|
$data = $query->orderByDesc('created_at')
|
|
->skip($offset)
|
|
->take($limit)
|
|
->get()
|
|
->map(function ($item) {
|
|
$item->level = match ((int)$item->level) {
|
|
0 => 'easy',
|
|
1 => 'normal',
|
|
2 => 'hard',
|
|
default => 'unknown',
|
|
};
|
|
return $item;
|
|
});
|
|
|
|
return $data;
|
|
}
|
|
public function createExercise($dataExercise)
|
|
{
|
|
$dataCreateExercise = [
|
|
'lesson_name' => $dataExercise['name'] ?? null,
|
|
'description' => $dataExercise['description'] ?? null,
|
|
'subject_id' => $dataExercise['subject_id'] ?? null,
|
|
'level' => $dataExercise['level'] ?? null,
|
|
'status' => $dataExercise['status'] ?? null,
|
|
'category_id' => $dataExercise['category_id'] ?? null,
|
|
'year' => $dataExercise['year'] ?? null,
|
|
];
|
|
|
|
$exercise = Exercise::create($dataCreateExercise);
|
|
|
|
return $exercise->id;
|
|
}
|
|
|
|
public function detail($id)
|
|
{
|
|
$exercise = Exercise::with([
|
|
'skills:id,name',
|
|
'questionGroups' => function ($q) {
|
|
$q->select('id', 'exercise_id', 'content');
|
|
},
|
|
'questionGroups.questions' => function ($q) {
|
|
$q->select('id', 'content', 'group_id', 'question_type_id', 'description', 'score', 'explanation', 'hint');
|
|
},
|
|
'questionGroups.questions.type:id,code,name',
|
|
'questionGroups.questions.choices',
|
|
'questionGroups.questions.blanks',
|
|
])->findOrFail($id);
|
|
|
|
return new ExerciseResource($exercise);
|
|
}
|
|
}
|
|
|