Browse Source

【Add】添加答题记录下载

liuxiangxin 3 months ago
parent
commit
cdd8331321

+ 92 - 0
app/Http/Controllers/Admin/VideoExamAnswer.php

@@ -65,4 +65,96 @@ class VideoExamAnswer extends Auth{
 		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_exam_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_exam_answer.course_id')
+									->join('custom','custom.uid','=','video_exam_answer.custom_uid')
+									->where($map)->select(['video_exam_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();
+        }
+	}
+
 }

+ 92 - 0
app/Http/Controllers/Admin/VideoLearnAnswer.php

@@ -65,4 +65,96 @@ class VideoLearnAnswer extends Auth{
 		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();
+        }
+	}
+
+
 }

+ 12 - 9
resources/views/admin/video_exam_answer/index.blade.php

@@ -4,7 +4,7 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 @endsection
 @section('content')
 
-<form action="" method="get" class="form-horizontal form-line">
+<form action="" method="get" class="form-horizontal form-line" name="thisform">
 	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-12" style="margin-right: 2px;">
 		<input type="text" class="form-control" name="record_id" value="{{request('record_id','')}}" placeholder="请输入记录ID查询" />
 	</div>
@@ -22,8 +22,13 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-12" style="margin-right: 2px;">
 		<input type="text" class="form-control" name="custom_name" value="{{request('custom_name','')}}" placeholder="请输入客户昵称查询" />
 	</div>
-	<input type="submit" class="btn btn-sm btn-primary" value="查询"/>
-	<a href="{{url('admin/video_learn_question/index')}}" class="btn btn-sm btn-default" >重置</a>
+	<div class="form-group col col-xs-4 col-md-2" style="margin-right: 2px;">
+		<button type="button" onclick="alter_from_attr({'method':'get','action':''})" class="btn btn-sm btn-primary" style="margin-right: 20px;"> 查询</button>
+		<a href="{{url('admin/video_exam_answer/index')}}" class="btn btn-sm btn-default"  style="margin-right: 20px;" >重置</a>
+		@if( check_auth('admin/video_exam_answer/down_excel') )
+		<button type="button" onclick="alter_from_attr({'method':'get','action':`{{url('admin/video_exam_answer/down_excel')}}`})" class="btn btn-sm btn-primary"> 下载表格</button>
+		@endif
+	</div>
 </form>
 
 <div class="row">
@@ -36,8 +41,8 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 						<th>课程名称</th>
 						<th>客户编码</th>
 						<th>客户昵称</th>
-						<th>习题ID</th>
 						<th>习题题目</th>
+						<th>习题选项</th>
 						<th>正确与否</th>
 						<th>所得积分</th>
 						<th>答题时间</th>
@@ -52,16 +57,14 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 							<td>{{$a['course_name']}}</td>
 							<td>{{$a['custom_code']}}</td>
 							<td>{{$a['custom_name']}}</td>
-							<td>{{$a['question_name']}}</td>
+							<td>{{$a['question_title']}}</td>
 							<td>
 								@foreach ($a['answer_list'] as $v)
-								<p>
-									{{$v['value']}} 
-									@if($v['is_answer']) 【答案】 @endif
+								<p @if( $a['answer_id'] == $v['id'] )   @if( $a['is_answer'] )  class="text-green" @else class="text-red"  @endif  @endif>
+									{{$v['value']}} @if($v['is_answer']) 【○】 @endif
 								</p>
 								@endforeach
 							</td>
-							<td>{{$a['is_answer']?'答对':'答错'}}</td>
 							<td>{{$a['get_score']}}</td>
 							<td> {{date('Y/m/d H:i:s',$a['insert_time'])}}</td>
 							<td>

+ 11 - 8
resources/views/admin/video_learn_answer/index.blade.php

@@ -22,8 +22,13 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-12" style="margin-right: 2px;">
 		<input type="text" class="form-control" name="custom_name" value="{{request('custom_name','')}}" placeholder="请输入客户昵称查询" />
 	</div>
-	<input type="submit" class="btn btn-sm btn-primary" value="查询"/>
-	<a href="{{url('admin/video_learn_question/index')}}" class="btn btn-sm btn-default" >重置</a>
+	<div class="form-group col col-xs-4 col-md-2" style="margin-right: 2px;">
+		<button type="button" onclick="alter_from_attr({'method':'get','action':''})" class="btn btn-sm btn-primary"  style="margin-right: 20px;" > 查询</button>
+		<a href="{{url('admin/video_learn_answer/index')}}" class="btn btn-sm btn-default"  style="margin-right: 20px;" >重置</a>
+		@if( check_auth('admin/video_learn_answer/down_excel') )
+		<button type="button" onclick="alter_from_attr({'method':'get','action':`{{url('admin/video_learn_answer/down_excel')}}`})" class="btn btn-sm btn-primary"> 下载表格</button>
+		@endif
+	</div>
 </form>
 
 <div class="row">
@@ -36,8 +41,8 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 						<th>课程名称</th>
 						<th>客户编码</th>
 						<th>客户昵称</th>
-						<th>习题ID</th>
 						<th>习题题目</th>
+						<th>习题选项</th>
 						<th>正确与否</th>
 						<th>所得积分</th>
 						<th>答题时间</th>
@@ -52,16 +57,14 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 							<td>{{$a['course_name']}}</td>
 							<td>{{$a['custom_code']}}</td>
 							<td>{{$a['custom_name']}}</td>
-							<td>{{$a['question_name']}}</td>
+							<td>{{$a['question_title']}}</td>
 							<td>
 								@foreach ($a['answer_list'] as $v)
-								<p>
-									{{$v['value']}} 
-									@if($v['is_answer']) 【答案】 @endif
+								<p @if( $a['answer_id'] == $v['id'] )   @if( $a['is_answer'] )  class="text-green" @else class="text-red"  @endif  @endif>
+									{{$v['value']}} @if($v['is_answer']) 【○】 @endif 
 								</p>
 								@endforeach
 							</td>
-							<td>{{$a['is_answer']?'答对':'答错'}}</td>
 							<td>{{$a['get_score']}}</td>
 							<td> {{date('Y/m/d H:i:s',$a['insert_time'])}}</td>
 							<td>

+ 10 - 1
routes/api.php

@@ -293,6 +293,9 @@ Route::any('video_learn_record/update_playtime',[App\Http\Controllers\Api\Video\
 /**
  * 课中习题
  */
+// 答题记录
+Route::any('video_learn_answer/get_list',[App\Http\Controllers\Api\Video\LearnAnswer::class,'get_list']);
+// 答题
 Route::any('video_learn_answer/play_exam',[App\Http\Controllers\Api\Video\LearnAnswer::class,'play_exam']);
 
 
@@ -302,10 +305,16 @@ Route::any('video_learn_answer/play_exam',[App\Http\Controllers\Api\Video\LearnA
  */
 Route::any('video_exam_question/get_list',[App\Http\Controllers\Api\Video\ExamQuestion::class,'get_list']);
 
+/**
+ * 课后答题记录
+ */
+Route::any('video_exam_answer/get_list',[App\Http\Controllers\Api\Video\ExamAnswer::class,'get_list']);
+
 /**
  * 评测报告
  * 
  */
 Route::any('video_exam_record/hand_in',[App\Http\Controllers\Api\Video\ExamRecord::class,'hand_in']);
 Route::any('video_exam_record/get_report',[App\Http\Controllers\Api\Video\ExamRecord::class,'get_report']);
-Route::any('video_exam_record/get_report_last',[App\Http\Controllers\Api\Video\ExamRecord::class,'get_report_last']);
+Route::any('video_exam_record/get_report_last',[App\Http\Controllers\Api\Video\ExamRecord::class,'get_report_last']);
+

+ 11 - 7
routes/web.php

@@ -714,6 +714,14 @@ Route::middleware('admin')->prefix('admin')->group(function(){
     // 状态
     Route::any('video_learn_question/set_status',[App\Http\Controllers\Admin\VideoLearnQuestion::class,'set_status']);
 
+    /* 学习记录 */
+    Route::any('video_learn_record/index',[App\Http\Controllers\Admin\VideoLearnRecord::class,'index']);
+
+    /* 学习答题记录 */
+    Route::any('video_learn_answer/index',[App\Http\Controllers\Admin\VideoLearnAnswer::class,'index']);
+    /* 答题记录下载 */
+    Route::any('video_learn_answer/down_excel',[App\Http\Controllers\Admin\VideoLearnAnswer::class,'down_excel']);
+
     /* 课后习题 */
     // 列表
     Route::any('video_exam_question/index',[App\Http\Controllers\Admin\VideoExamQuestion::class,'index']);
@@ -724,16 +732,12 @@ Route::middleware('admin')->prefix('admin')->group(function(){
     // 状态
     Route::any('video_exam_question/set_status',[App\Http\Controllers\Admin\VideoExamQuestion::class,'set_status']);
 
-
-    /* 学习记录 */
-    Route::any('video_learn_record/index',[App\Http\Controllers\Admin\VideoLearnRecord::class,'index']);
-    /* 学习答题记录 */
-    Route::any('video_learn_answer/index',[App\Http\Controllers\Admin\VideoLearnAnswer::class,'index']);
-
     /* 评测记录 */
     Route::any('video_exam_record/index',[App\Http\Controllers\Admin\VideoExamRecord::class,'index']);
+
     /* 评测答题记录 */
     Route::any('video_exam_answer/index',[App\Http\Controllers\Admin\VideoExamAnswer::class,'index']);
-
+    /* 答题记录下载 */
+    Route::any('video_exam_answer/down_excel',[App\Http\Controllers\Admin\VideoExamAnswer::class,'down_excel']);
 
 });