StudentCourse.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\Model;
  5. class StudentCourse extends Model
  6. {
  7. protected $table = 'student_courses';
  8. protected $guarded = [];
  9. public function course()
  10. {
  11. return $this->hasOne('App\Models\Course', 'id', 'course_id');
  12. }
  13. public function getTeacherNames()
  14. {
  15. if($this['assign_teacher'] == 1) {
  16. return '全部';
  17. }
  18. $ids = $this['studentCourseTeachers']->pluck('teacher_id');
  19. return (new Teacher())->whereIn('id', $ids)->get()->implode('name', ',');
  20. }
  21. public function getTeacherFullNames()
  22. {
  23. if($this['assign_teacher'] == 1) {
  24. return '全部';
  25. }
  26. $ids = $this['studentCourseTeachers']->pluck('teacher_id');
  27. return (new Teacher())->whereIn('id', $ids)->get()->implode('name', '/');
  28. }
  29. public function studentCourseTeachers()
  30. {
  31. return $this->hasMany('App\Models\StudentCourseTeacher');
  32. }
  33. public function getTeacherIds()
  34. {
  35. if($this['assign_teacher'] == 1) {
  36. return collect();
  37. }
  38. return $this['studentCourseTeachers']->pluck('teacher_id');
  39. }
  40. public function updateStudentCourseTeachers($teachers, $assign_teacher)
  41. {
  42. StudentCourseTeacher::where('student_course_id', $this['id'])->delete();
  43. if($assign_teacher == 2) {
  44. if(!empty($teachers) && is_array($teachers)) {
  45. foreach($teachers as $teacher) {
  46. StudentCourseTeacher::create([
  47. 'student_course_id' => $this['id'],
  48. 'teacher_id' => $teacher,
  49. 'student_id' => $this['student_id']
  50. ]);
  51. }
  52. }
  53. } else {
  54. StudentCourseTeacher::create([
  55. 'student_course_id' => $this['id'],
  56. 'teacher_id' => 0,
  57. 'student_id' => $this['student_id']
  58. ]);
  59. }
  60. }
  61. public function computeEndDate()
  62. {
  63. $apply_date = $this['apply_date'];
  64. if(empty($apply_date) || empty($this['duration'])) {
  65. return '';
  66. } else {
  67. $days = $this['duration'];
  68. $leave_days = (int)Leave::where('student_id', $this['id'])->get()->count('days');
  69. $days += $leave_days;
  70. return Carbon::createFromTimestamp(strtotime($apply_date))->addDays($days)->toDateString();
  71. }
  72. }
  73. }