CourseController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers\Admin\Student;
  3. use App\Http\Controllers\Admin\Controller;
  4. use App\Models\Course;
  5. use App\Models\Student;
  6. use App\Models\StudentCourse;
  7. use App\Models\StudentCourseTeacher;
  8. use App\Models\Teacher;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. class CourseController extends Controller
  12. {
  13. protected $redirect_index = '/admin/Student/Course/index';
  14. protected $view_path = 'admin.students.courses.';
  15. protected $pre_uri = '/admin/Student/Course/';
  16. protected $model_name = '课程';
  17. protected $model;
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->model = new StudentCourse();
  22. }
  23. public function index(Request $request)
  24. {
  25. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  26. return $this->showWarning('找不到学员');
  27. }
  28. $list = $this->model->where('student_id', $student->id)->orderBy('created_at', 'desc');
  29. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  30. $keyword = '%' . trim($request->input('keyword')) . '%';
  31. $list = $list->where('name', 'like', $keyword);
  32. }
  33. $list = $list->paginate();
  34. foreach($list as $item) {
  35. $item->end_date = Carbon::createFromTimestamp(strtotime($item->apply_date))->addDays($item->duration)->toDateString();
  36. $now = Carbon::now()->toDateString();
  37. if($now > $item->end_date) {
  38. $item->remain_days = 0;
  39. } else {
  40. $item->remain_days = Carbon::now()->diffInDays($item->end_date) + 1;
  41. }
  42. }
  43. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  44. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name', 'student'));
  45. }
  46. public function create(Request $request)
  47. {
  48. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  49. return $this->showWarning('找不到学员');
  50. }
  51. $courses = Course::orderBy('created_at', 'desc')->get();
  52. $teachers = Teacher::orderBy('created_at', 'desc')->get();
  53. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  54. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers', 'courses', 'student'));
  55. }
  56. public function store(Request $request)
  57. {
  58. if(!$request->isMethod('POST')) {
  59. return $this->showWarning('访问错误');
  60. }
  61. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  62. return $this->showWarning('数据错误');
  63. }
  64. $data = $request->input('data');
  65. $data['assign_teacher'] = $request->input('assign_teacher') == 1 ? 1 : 2;
  66. $res = $this->model->create($data);
  67. if(!$res) {
  68. return $this->showWarning('数据库保存失败!');
  69. }
  70. $res->updateStudentCourseTeachers($request->input('teachers'));
  71. return $this->showMessage('操作成功', $this->redirect_index . '?student_id=' . $res->student_id);
  72. }
  73. public function edit(Request $request)
  74. {
  75. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  76. return $this->showWarning('数据错误!');
  77. }
  78. $courses = Course::orderBy('created_at', 'desc')->get();
  79. $teachers = Teacher::orderBy('created_at', 'desc')->get();
  80. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  81. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model', 'courses', 'teachers'));
  82. }
  83. public function update(Request $request)
  84. {
  85. if(!$request->isMethod('POST')) {
  86. return $this->showWarning('访问错误');
  87. }
  88. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  89. return $this->showWarning('数据错误');
  90. }
  91. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  92. return $this->showWarning('找不到合同');
  93. }
  94. $data = $request->input('data');
  95. $data['assign_teacher'] = $request->input('assign_teacher') == 1 ? 1 : 2;
  96. $res = $this->model->where('id', $item->id)->update($data);
  97. if(!$res) {
  98. return $this->showWarning('数据库保存失败!');
  99. }
  100. if(!empty($item)) {
  101. $item->updateStudentCourseTeachers($request->input('teachers'));
  102. }
  103. return $this->showMessage('操作成功', $this->redirect_index . '?student_id=' . $item->student_id);
  104. }
  105. public function delete(Request $request)
  106. {
  107. if(!$request->isMethod('POST')) {
  108. return $this->showWarning('访问错误');
  109. }
  110. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  111. return $this->showWarning('访问错误');
  112. }
  113. StudentCourseTeacher::where('student_course_id', $item->id)->delete();
  114. $res = $item->delete();
  115. if(!$res) {
  116. return $this->showWarning('数据库删除失败');
  117. }
  118. return $this->showMessage('操作成功');
  119. }
  120. }