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.
zhishifufei_php/application/admin/model/questions/ExaminationRecord.php

112 lines
4.4 KiB

10 months ago
<?php
// +----------------------------------------------------------------------
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
// +----------------------------------------------------------------------
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
// +----------------------------------------------------------------------
namespace app\admin\model\questions;
use traits\ModelTrait;
use basic\ModelBasic;
use app\admin\model\questions\TestPaper;
use app\admin\model\questions\TestPaperObtain;
use app\admin\model\questions\TestPaperScoreGrade;
use app\admin\model\questions\ExaminationTestRecord;
use app\admin\model\questions\TestPaperQuestions;
use app\admin\model\user\User;
use service\PhpSpreadsheetService;
/**
* 用户考试记录 Model
* Class ExaminationRecord
* @package app\admin\model\questions
*/
class ExaminationRecord extends ModelBasic
{
use ModelTrait;
public static function setWhere($where)
{
$model = self::alias('r')->join('TestPaper t', 'r.test_id=t.id')
->join('User u', 'r.uid=u.uid')->order('r.add_time desc')->where(['r.is_submit' => 1, 't.is_del' => 0]);
if (isset($where['test_id']) && $where['test_id']) $model = $model->where('r.test_id', $where['test_id']);
if (isset($where['type']) && $where['type']) $model = $model->where('r.type', $where['type']);
if (isset($where['uid']) && $where['uid']) $model = $model->where('r.uid', $where['uid']);
if (isset($where['title']) && $where['title'] != '') $model = $model->where('r.uid', 'like', "%$where[title]%");
return $model;
}
/**用户答题结果
* @param $where
* @return array
* @throws \think\Exception
*/
public static function getExaminationRecord($where)
{
$model = self::setWhere($where)->field('r.id,r.test_id,r.type,r.uid,r.score,r.accuracy,t.title,u.nickname,r.add_time');
if (isset($where['excel']) && $where['excel'] == 1) {
$data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
} else {
$data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
}
foreach ($data as $key => &$value) {
if ($where['type'] == 2) {
$value['grade'] = TestPaperScoreGrade::getTestPaperScoreGrade($value['test_id'], $value['score']);
}
$value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
}
if (isset($where['excel']) && $where['excel'] == 1) {
self::SaveExcel($data, $where['type']);
}
$count = self::setWhere($where)->count();
return compact('data', 'count');
}
/**
* 保存并下载excel
* $list array
* return
*/
public static function SaveExcel($list, $type)
{
$export = [];
$filename = '答题记录' . time() . '.xlsx';
if ($type == 1) {
$array = ['ID', 'UID', '昵称', '标题', '正确率%', '提交时间'];
foreach ($list as $index => $item) {
$export[] = [
$item['id'],
$item['uid'],
$item['nickname'],
$item['title'],
$item['accuracy'],
$item['add_time']
];
}
$filename = '练习记录' . time() . '.xlsx';
} else if ($type == 2) {
$array = ['ID', 'UID', '昵称', '标题', '分数', '正确率%', '分数等级', '提交时间'];
foreach ($list as $index => $item) {
$export[] = [
$item['id'],
$item['uid'],
$item['nickname'],
$item['title'],
$item['score'],
$item['accuracy'],
$item['grade'],
$item['add_time']
];
}
$filename = '考试记录' . time() . '.xlsx';
}
PhpSpreadsheetService::outdata($filename, $export, $array);
}
}