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', '~~暂无数据'); // 加载模板 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(); } } }