123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace app\admin\model\exam;
- use addons\exam\enum\RoomSignupStatus;
- use addons\exam\model\BaseModel;
- use addons\exam\model\RoomModel;
- use app\admin\model\User;
- use think\Db;
- class RoomGradeModel extends BaseModel
- {
-
- protected $name = 'exam_room_grade';
-
- protected $autoWriteTimestamp = 'int';
-
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
-
- protected $append
- = [
- 'is_pass_text',
- 'is_makeup_text',
- 'grade_time_text'
- ];
- public function getIsPassList()
- {
- return ['0' => __('Is_pass 0'), '1' => __('Is_pass 1')];
- }
- public function getIsMakeupList()
- {
- return ['0' => __('Is_makeup 0'), '1' => __('Is_makeup 1')];
- }
- public function getIsPassTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['is_pass']) ? $data['is_pass'] : '');
- $list = $this->getIsPassList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getIsMakeupTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['is_makeup']) ? $data['is_makeup'] : '');
- $list = $this->getIsMakeupList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getGradeTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['grade_time']) ? $data['grade_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setGradeTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- public function user()
- {
- return $this->belongsTo(User::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function belongsUser()
- {
- return $this->belongsTo(User::class);
- }
- public function paper()
- {
- return $this->belongsTo(PaperModel::class, 'paper_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function cate()
- {
- return $this->belongsTo(CateModel::class, 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function cates()
- {
- return $this->belongsTo(CateModel::class, 'cate_id', 'id');
- }
- public function room()
- {
- return $this->belongsTo(RoomModel::class, 'room_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function signup()
- {
- return $this->belongsTo(RoomSignupModel::class, 'user_id', 'user_id', [])->where('status', RoomSignupStatus::ACCEPT);
- }
- public function signup1()
- {
- return $this->belongsTo(RoomSignupModel::class, 'user_id', 'user_id', [], 'LEFT')->setEagerlyType(0)->where('status', RoomSignupStatus::ACCEPT);
- }
-
- public static function rankData($room_id)
- {
- if (!$room = RoomModel::get($room_id)) {
- fail('考场信息不存在');
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $grade_count = RoomGradeModel::where('room_id', $room['id'])->where('paper_id', $room['paper_id'])->group('user_id')->count();
- $pass_count = RoomGradeModel::where('room_id', $room['id'])->where('is_pass', 1)->group('user_id')->count();
- $pass_rate = round(($pass_count / $grade_count) * 100, 2) . '%';
-
- $list = !$grade_count ? [] : \addons\exam\model\RoomGradeModel::with(
- [
-
- 'user' => BaseModel::withSimpleUser(),
- ]
- )
- ->where('room_id', $room['id'])
- ->where('paper_id', $room['paper_id'])
- ->where('is_makeup', 0)
- ->group('user_id')
- ->order('score desc, grade_time asc')
- ->select();
-
- Db::transaction(function () use ($room, $grade_count, $pass_count, $pass_rate, &$list) {
-
- $room->grade_count = $grade_count;
- $room->pass_count = $pass_count;
- $room->pass_rate = $pass_rate;
- $room->is_rank = 1;
- $room->save();
-
- foreach ($list as $index => &$grade) {
- $grade['rank'] = $index + 1;
- $grade->save();
- }
- });
- return [
- 'summary' => [
- 'grade_count' => $grade_count,
- 'pass_count' => $pass_count,
- 'pass_rate' => $pass_rate . '%',
- 'cache_time' => date('Y-m-d H:i:s'),
- ],
-
- 'list' => count($list) > 10 ? array_slice($list, 0, 10) : $list,
- ];
- }
- }
|