VideoExamAnswer.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\Custom;
  3. use App\Models\Video\ExamAnswer as Model;
  4. use App\Models\Video\Course as VideoCourse;
  5. use App\Models\Video\Answer as VideoAnswer;
  6. use App\Models\Video\Question as VideoQuestion;
  7. /**
  8. * 学习记录
  9. *
  10. * @author 刘相欣
  11. *
  12. */
  13. class VideoExamAnswer extends Auth{
  14. protected function _initialize(){
  15. parent::_initialize();
  16. $this->assign('breadcrumb1','课后评测');
  17. $this->assign('breadcrumb2','答题记录');
  18. }
  19. /**
  20. * 列表页
  21. *
  22. * */
  23. public function index(Model $Model,VideoCourse $VideoCourse,Custom $Custom,VideoQuestion $VideoQuestion,VideoAnswer $VideoAnswer){
  24. // 接收参数
  25. $recordId = request('record_id',0);
  26. $courseId = request('course_id',0);
  27. $customCode = request('custom_code','');
  28. $customName = request('custom_name','');
  29. // 转码
  30. $customCode = $Custom->codeToId($customCode);
  31. // 查询条件
  32. $map = [];
  33. // 组合条件
  34. if( $courseId ) $map[] = ['video_course.id','=',$courseId];
  35. if( $recordId ) $map[] = ['video_exam_answer.record_id','=',$recordId];
  36. if( $customCode ) $map[] = ['custom.uid','=',$customCode];
  37. if( $customName ) $map[] = ['custom.username','LIKE','%'.$customName.'%'];
  38. // 查询数据
  39. $list = $Model->query()
  40. ->join('video_course','video_course.id','=','video_exam_answer.course_id')
  41. ->join('custom','custom.uid','=','video_exam_answer.custom_uid')
  42. ->where($map)->select(['video_exam_answer.*','video_course.name as course_name','custom.username as custom_name'])
  43. ->orderByDesc('id')->paginate(config('page_num',10));
  44. // 循环处理数据
  45. foreach ($list as $key => $value) {
  46. // 时间
  47. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  48. $value['question_title']= $VideoQuestion->getOne($value['question_id'],'title');
  49. $value['answer_list'] = $VideoAnswer->getListByQuestion($value['question_id']);
  50. $value['answer_list'] = array_values($value['answer_list']);
  51. // 重组
  52. $list[$key] = $value;
  53. }
  54. // 查询数据
  55. $courseList = $VideoCourse->query()->orderByDesc('id')->get(['id','name']);
  56. // 分配数据
  57. $this->assign('list',$list);
  58. $this->assign('courseList',$courseList);
  59. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  60. // 加载模板
  61. return $this->fetch();
  62. }
  63. /**
  64. * 导出表格导入
  65. *
  66. * */
  67. public function down_excel(Model $Model,Custom $Custom,VideoQuestion $VideoQuestion,VideoAnswer $VideoAnswer){
  68. // 接收参数
  69. $recordId = request('record_id',0);
  70. $courseId = request('course_id',0);
  71. $customCode = request('custom_code','');
  72. $customName = request('custom_name','');
  73. // 转码
  74. $customCode = $Custom->codeToId($customCode);
  75. // 查询条件
  76. $map = [];
  77. // 组合条件
  78. if( $courseId ) $map[] = ['video_course.id','=',$courseId];
  79. if( $recordId ) $map[] = ['video_exam_answer.record_id','=',$recordId];
  80. if( $customCode ) $map[] = ['custom.uid','=',$customCode];
  81. if( $customName ) $map[] = ['custom.username','LIKE','%'.$customName.'%'];
  82. // 查询数据
  83. $list = $Model->query()
  84. ->join('video_course','video_course.id','=','video_exam_answer.course_id')
  85. ->join('custom','custom.uid','=','video_exam_answer.custom_uid')
  86. ->where($map)->select(['video_exam_answer.*','video_course.name as course_name','custom.username as custom_name'])
  87. ->get()->toArray();
  88. $data = [];
  89. // 循环处理数据
  90. foreach ($list as $key => $value) {
  91. // 时间
  92. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  93. $value['question_title']= $VideoQuestion->getOne($value['question_id'],'title');
  94. $value['answer_list'] = $VideoAnswer->getListByQuestion($value['question_id']);
  95. $value['answer_list'] = array_values($value['answer_list']);
  96. foreach ( $value['answer_list'] as $k => $v) {
  97. if( $v['is_answer'] ) $value['answer_list'][$k]['value'] .= "【答案】";
  98. }
  99. // 重组
  100. $data[$key] = [
  101. 'id' => $value['id'],
  102. 'course_name' => $value['course_name'],
  103. 'custom_code' => $value['custom_code'],
  104. 'custom_name' => $value['custom_name'],
  105. 'question_title' => $value['question_title'],
  106. 'answer_list' => implode("\n",array_column($value['answer_list'],'value') ),
  107. 'is_answer' => $value['is_answer']?'答对':'答错',
  108. 'get_score' => $value['get_score'],
  109. 'insert_time' => date('Y-m-d H:i:s',$value['insert_time'])
  110. ];
  111. // 重组
  112. $list[$key] = $value;
  113. }
  114. try {
  115. // 去下载
  116. $this->toDown($data);
  117. } catch (\Throwable $th) {
  118. echo $th->getMessage();
  119. }
  120. }
  121. /**
  122. * 去下载
  123. */
  124. private function toDown($data){
  125. try {
  126. $config = ['path' =>public_path().'/uploads/'];
  127. $excel = new \Vtiful\Kernel\Excel($config);
  128. $header = ['ID','课程名称','客户编码','客户昵称','习题题目','习题选项','是否答对','得分','答题时间'];
  129. $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')->header($header)->data($data)->output();
  130. $filename = uniqid().'.xlsx';
  131. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  132. header('Content-Disposition: attachment;filename="' . $filename . '"');
  133. header('Content-Length: ' . filesize($filePath));
  134. header('Content-Transfer-Encoding: binary');
  135. header('Cache-Control: must-revalidate');
  136. header('Cache-Control: max-age=0');
  137. header('Pragma: public');
  138. ob_clean();
  139. flush();
  140. if (copy($filePath, 'php://output') === false) {
  141. dd('下载出错');
  142. }
  143. @unlink($filePath);
  144. exit();
  145. }catch (\Throwable $th) {
  146. return $th->getMessage();
  147. }
  148. }
  149. }