VideoQuestion.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Video\Question as Request;
  3. use App\Models\Video\Question as Model;
  4. /**
  5. * 课程习题
  6. *
  7. * @author 刘相欣
  8. *
  9. */
  10. class VideoQuestion extends Auth{
  11. protected function _initialize(){
  12. parent::_initialize();
  13. $this->assign('breadcrumb1','课程习题');
  14. $this->assign('breadcrumb2','习题题目');
  15. }
  16. /**
  17. * 列表页
  18. *
  19. * */
  20. public function index(Model $Model){
  21. // 接收参数
  22. $name = request('title','');
  23. // 查询条件
  24. $map = [];
  25. // 组合条件
  26. if( $name ) $map[] = ['title','=',$name];
  27. // 查询数据
  28. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10))->appends(request()->all());
  29. // 循环处理数据
  30. foreach ($list as $key => $value) {
  31. // 重组
  32. $list[$key] = $value;
  33. }
  34. // 分配数据
  35. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  36. $this->assign('list',$list);
  37. // 加载模板
  38. return $this->fetch();
  39. }
  40. /**
  41. * 添加
  42. *
  43. * */
  44. public function add(Request $request,Model $Model){
  45. if( request()->isMethod('post') ){
  46. // 验证参数
  47. $request->scene('add')->validate();
  48. // 接收数据
  49. $data['title'] = request('title','');
  50. $data['score'] = request('score',0);
  51. $data['status'] = 1;
  52. // 写入数据表
  53. $id = $Model->add($data);
  54. // 如果操作失败
  55. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  56. // 记录行为
  57. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  58. // 告知结果
  59. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  60. }
  61. // 分配数据
  62. $this->assign('crumbs','新增');
  63. // 加载模板
  64. return $this->fetch();
  65. }
  66. /**
  67. * 修改
  68. *
  69. * */
  70. public function edit(Request $request,Model $Model){
  71. // 接收参数
  72. $id = request('id',0);
  73. // 查询用户
  74. $oldData = $Model->where(['id'=>$id])->first();
  75. // 修改
  76. if(request()->isMethod('post')){
  77. // 验证参数
  78. $request->scene('edit')->validate();
  79. // 接收数据
  80. $data['title'] = request('title','');
  81. $data['score'] = request('score',0);
  82. // 写入数据表
  83. $result = $Model->edit($id,$data);
  84. // 如果操作失败
  85. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  86. // 记录行为
  87. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  88. // 告知结果
  89. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  90. }
  91. // 错误告知
  92. if( !$oldData ) return $this->error('查无数据');
  93. // 分配数据
  94. $this->assign('oldData',$oldData);
  95. $this->assign('crumbs','修改');
  96. // 加载模板
  97. return $this->fetch();
  98. }
  99. /**
  100. * 修改状态
  101. *
  102. * */
  103. public function set_status(Request $request,Model $Model){
  104. // 验证参数
  105. $request->scene('set_status')->validate();
  106. // 设置状态
  107. $id = request('id',0);
  108. $status = request('status',0);
  109. // 查询用户
  110. $oldData = $Model->where(['id'=>$id])->first();
  111. // 如果用户不存在
  112. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  113. // 执行修改
  114. $result = $Model->edit($id,['status'=>$status]);
  115. // 提示新增失败
  116. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  117. // 记录行为
  118. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  119. // 告知结果
  120. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  121. }
  122. }