123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Models\Custom;
- use App\Models\Video\LearnAnswer as Model;
- use App\Models\Video\Course as VideoCourse;
- use App\Models\Video\Answer as VideoAnswer;
- use App\Models\Video\Question as VideoQuestion;
- /**
- * 学习记录
- *
- * @author 刘相欣
- *
- */
- class VideoLearnAnswer extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','课中习题');
- $this->assign('breadcrumb2','答题记录');
- }
- /**
- * 列表页
- *
- * */
- public function index(Model $Model,VideoCourse $VideoCourse,Custom $Custom,VideoQuestion $VideoQuestion,VideoAnswer $VideoAnswer){
- // 接收参数
- $recordId = request('record_id',0);
- $courseId = request('course_id',0);
- $customCode = request('custom_code','');
- $customName = request('custom_name','');
- // 转码
- $customCode = $Custom->codeToId($customCode);
- // 查询条件
- $map = [];
- // 组合条件
- if( $courseId ) $map[] = ['video_course.id','=',$courseId];
- if( $recordId ) $map[] = ['video_learn_answer.record_id','=',$recordId];
- if( $customCode ) $map[] = ['custom.uid','=',$customCode];
- if( $customName ) $map[] = ['custom.username','LIKE','%'.$customName.'%'];
- // 查询数据
- $list = $Model->query()
- ->join('video_course','video_course.id','=','video_learn_answer.course_id')
- ->join('custom','custom.uid','=','video_learn_answer.custom_uid')
- ->where($map)->select(['video_learn_answer.*','video_course.name as course_name','custom.username as custom_name'])
- ->orderByDesc('id')->paginate(config('page_num',10));
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 时间
- $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
- $value['question_title']= $VideoQuestion->getOne($value['question_id'],'title');
- $value['answer_list'] = $VideoAnswer->getListByQuestion($value['question_id']);
- $value['answer_list'] = array_values($value['answer_list']);
- // 重组
- $list[$key] = $value;
- }
- // 查询数据
- $courseList = $VideoCourse->query()->orderByDesc('id')->get(['id','name']);
- // 分配数据
- $this->assign('list',$list);
- $this->assign('courseList',$courseList);
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- // 加载模板
- return $this->fetch();
- }
- /**
- * 导出表格导入
- *
- * */
- public function down_excel(Model $Model,Custom $Custom,VideoQuestion $VideoQuestion,VideoAnswer $VideoAnswer){
- // 接收参数
- $recordId = request('record_id',0);
- $courseId = request('course_id',0);
- $customCode = request('custom_code','');
- $customName = request('custom_name','');
- // 转码
- $customCode = $Custom->codeToId($customCode);
- // 查询条件
- $map = [];
- // 组合条件
- if( $courseId ) $map[] = ['video_course.id','=',$courseId];
- if( $recordId ) $map[] = ['video_learn_answer.record_id','=',$recordId];
- if( $customCode ) $map[] = ['custom.uid','=',$customCode];
- if( $customName ) $map[] = ['custom.username','LIKE','%'.$customName.'%'];
- // 查询数据
- $list = $Model->query()
- ->join('video_course','video_course.id','=','video_learn_answer.course_id')
- ->join('custom','custom.uid','=','video_learn_answer.custom_uid')
- ->where($map)->select(['video_learn_answer.*','video_course.name as course_name','custom.username as custom_name'])
- ->get()->toArray();
- $data = [];
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 时间
- $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
- $value['question_title']= $VideoQuestion->getOne($value['question_id'],'title');
- $value['answer_list'] = $VideoAnswer->getListByQuestion($value['question_id']);
- $value['answer_list'] = array_values($value['answer_list']);
- foreach ( $value['answer_list'] as $k => $v) {
- if( $v['is_answer'] ) $value['answer_list'][$k]['value'] .= "【答案】";
- }
- // 重组
- $data[$key] = [
- 'id' => $value['id'],
- 'course_name' => $value['course_name'],
- 'custom_code' => $value['custom_code'],
- 'custom_name' => $value['custom_name'],
- 'question_title' => $value['question_title'],
- 'answer_list' => implode("\n",array_column($value['answer_list'],'value') ),
- 'is_answer' => $value['is_answer']?'答对':'答错',
- 'get_score' => $value['get_score'],
- 'insert_time' => date('Y-m-d H:i:s',$value['insert_time'])
- ];
- // 重组
- $list[$key] = $value;
- }
-
- try {
- // 去下载
- $this->toDown($data);
- } catch (\Throwable $th) {
- echo $th->getMessage();
- }
-
- }
- /**
- * 去下载
- */
- private function toDown($data){
- try {
- $config = ['path' =>public_path().'/uploads/'];
- $excel = new \Vtiful\Kernel\Excel($config);
- $header = ['ID','课程名称','客户编码','客户昵称','习题题目','习题选项','是否答对','得分','答题时间'];
- $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')->header($header)->data($data)->output();
- $filename = uniqid().'.xlsx';
- header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- header('Content-Disposition: attachment;filename="' . $filename . '"');
- header('Content-Length: ' . filesize($filePath));
- header('Content-Transfer-Encoding: binary');
- header('Cache-Control: must-revalidate');
- header('Cache-Control: max-age=0');
- header('Pragma: public');
- ob_clean();
- flush();
- if (copy($filePath, 'php://output') === false) {
- dd('下载出错');
- }
- @unlink($filePath);
- exit();
- }catch (\Throwable $th) {
- return $th->getMessage();
- }
- }
- }
|