generated from liutk/owl-admin-base
120 lines
4.0 KiB
PHP
120 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services\Train;
|
|
|
|
use App\Admin\Filters\TrainExaminationFilter;
|
|
use App\Admin\Services\BaseService;
|
|
use App\Enums\ExamStatus;
|
|
use App\Enums\MessageType;
|
|
use App\Models\Employee;
|
|
use App\Models\Train\Examination;
|
|
use App\Models\Train\Question;
|
|
use App\Services\MessageService;
|
|
|
|
class ExaminationService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['papers'];
|
|
|
|
protected string $modelName = Examination::class;
|
|
|
|
protected string $modelFilterName = TrainExaminationFilter::class;
|
|
|
|
public function getDetail($id)
|
|
{
|
|
$query = $this->query();
|
|
|
|
$this->addRelations($query, 'detail');
|
|
|
|
$model = $query->find($id);
|
|
$model->min_mark = $model->papers->whereNotNull('finished_at')->min('mark');
|
|
$model->max_mark = $model->papers->whereNotNull('finished_at')->max('mark');
|
|
$model->avg_mark = floor($model->papers->whereNotNull('finished_at')->avg('mark') * 100) / 100;
|
|
$model->total_finished = $model->papers->whereNotNull('finished_at')->count().'/'.$model->papers->count();
|
|
|
|
return $model;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (isset($data['questions']) && $data['questions']) {
|
|
$totalQuestions = 0;
|
|
$totalScore = 0;
|
|
$questions = [];
|
|
$ids = array_column($data['questions'], 'id');
|
|
$questionList = Question::whereIn('id', $ids)->get();
|
|
foreach ($data['questions'] as $index => $question) {
|
|
$model = $questionList->firstWhere('id', $question['id']);
|
|
if ($model) {
|
|
// sort: 序号, id: Question.id, title: 题目, cate: 类型, options: 选项, score: 分值
|
|
$item = [
|
|
'sort' => $index + 1,
|
|
'id' => $model->id,
|
|
'title' => $model->title,
|
|
'cate' => $model->cate,
|
|
'cate_name' => $model->cate->text(),
|
|
'options' => $model->options,
|
|
'score' => data_get($question, 'score', 1),
|
|
];
|
|
array_push($questions, $item);
|
|
|
|
$totalQuestions++;
|
|
$totalScore += $item['score'];
|
|
}
|
|
}
|
|
$data['questions'] = $questions;
|
|
$data['total_questions'] = $totalQuestions;
|
|
$data['total_score'] = $totalScore;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function publish(Examination $examination, array $ids)
|
|
{
|
|
if ($examination->exam_status == ExamStatus::Published) {
|
|
return $this->setError('已经发布了');
|
|
}
|
|
|
|
$questions = [];
|
|
foreach ($examination->questions as $question) {
|
|
$question['options'] = array_map(function ($option) {
|
|
$option['selected'] = false;
|
|
|
|
return $option;
|
|
}, $question['options']);
|
|
array_push($questions, $question);
|
|
}
|
|
$employees = Employee::whereIn('id', $ids)->get();
|
|
// 为员工生成考卷
|
|
foreach ($employees as $employee) {
|
|
$paper = $examination->papers()->create([
|
|
'employee_id' => $employee->id,
|
|
'content' => $questions,
|
|
]);
|
|
|
|
(new MessageService())->create(
|
|
MessageType::Exam,
|
|
'考试通知',
|
|
'您有一张待完成试卷,请尽快完成考试。',
|
|
[$employee],
|
|
['paper' => ['id' => $paper->id]],
|
|
);
|
|
}
|
|
|
|
$examination->update(['exam_status' => ExamStatus::Published, 'published_at' => now()]);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function cancel(Examination $examination)
|
|
{
|
|
if ($examination->exam_status == ExamStatus::None) {
|
|
return $this->setError('已经取消了');
|
|
}
|
|
$examination->papers()->delete();
|
|
$examination->update(['exam_status' => ExamStatus::None, 'published_at' => null]);
|
|
|
|
return true;
|
|
}
|
|
}
|