StudentCourse.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $leaves = Leave::where('student_id', $this['student_id'])->get();
  69. $leave_days = 0;
  70. foreach($leaves as $leave) {
  71. $leave_days += (int) $leave->days;
  72. }
  73. $days += $leave_days;
  74. return Carbon::createFromTimestamp(strtotime($apply_date))->addDays($days)->toDateString();
  75. }
  76. }
  77. }