Course.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Course extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable = [
  9. 'parent_id',
  10. 'discipline_id',
  11. 'name',
  12. 'course_type_id',
  13. 'causal_id',
  14. 'max_members',
  15. 'instructor',
  16. 'price',
  17. 'months',
  18. 'date_from',
  19. 'date_to',
  20. 'course_duration_id',
  21. 'course_frequency_id',
  22. 'course_level_id',
  23. 'enabled',
  24. 'year',
  25. 'subscription_price',
  26. 'sub_causal_id',
  27. 'category_id',
  28. 'active',
  29. 'type',
  30. 'when',
  31. 'prices',
  32. 'instructor_id'
  33. ];
  34. public function parent()
  35. {
  36. return $this->belongsTo(Course::class);
  37. }
  38. public function discipline()
  39. {
  40. return $this->belongsTo(Discipline::class, 'discipline_id');
  41. }
  42. public function type()
  43. {
  44. return $this->belongsTo(CourseType::class, 'course_type_id');
  45. }
  46. public function duration()
  47. {
  48. return $this->belongsTo(CourseDuration::class, 'course_duration_id');
  49. }
  50. public function frequency()
  51. {
  52. return $this->belongsTo(CourseFrequency::class, 'course_frequency_id');
  53. }
  54. public function level()
  55. {
  56. return $this->belongsTo(CourseLevel::class, 'course_level_id');
  57. }
  58. public function category()
  59. {
  60. return $this->belongsTo(Category::class, 'category_id');
  61. }
  62. public function member()
  63. {
  64. return $this->belongsTo(Member::class);
  65. }
  66. public function getins()
  67. {
  68. return $this->belongsTo(User::class, 'instructor_id');
  69. }
  70. public function childs() {
  71. return $this->hasMany(\App\Models\Course::class,'parent_id','id') ;
  72. }
  73. public function getTree()
  74. {
  75. $str = '';
  76. if ($this->parent_id != null)
  77. {
  78. $a = $this->recursiveName($this->parent_id, array($this->name));
  79. $a = array_reverse($a);
  80. $str = implode(" - ", $a);
  81. }
  82. else
  83. {
  84. $str = $this->name;
  85. }
  86. return $str;
  87. }
  88. public function recursiveName($parent_id, $array)
  89. {
  90. $x = \App\Models\Course::findOrFail($parent_id);
  91. $array[] = $x->name;
  92. if ($x->parent_id != null)
  93. {
  94. return $this->recursiveName($x->parent_id, $array);
  95. }
  96. else
  97. {
  98. return $array;
  99. }
  100. }
  101. public function recursiveParent($parent_id, $array)
  102. {
  103. if ($parent_id == null)
  104. return $array;
  105. $x = \App\Models\Course::findOrFail($parent_id);
  106. $array[] = $x->id;
  107. if ($x->parent_id != null)
  108. {
  109. return $this->recursiveParent($x->parent_id, $array);
  110. }
  111. else
  112. {
  113. return $array;
  114. }
  115. }
  116. public function getCount()
  117. {
  118. return \App\Models\MemberCourse::where('course_id', $this->id)->count();
  119. }
  120. public function getTypeFieldAttribute()
  121. {
  122. return $this->attributes['type'] ?? null;
  123. }
  124. // Method to get type field with capitalization
  125. public function getFormattedTypeField()
  126. {
  127. return ucfirst($this->attributes['type'] ?? 'No Type');
  128. }
  129. public function getDetailsName() {
  130. $courseDiscipline = $this->discipline?->name ?? '';
  131. $courseName = $this->name ?? 'Corso Sconosciuto';
  132. $levelName = is_object($this->level) ? $this->level->name : '';
  133. $frequencyName = is_object($this->frequency) ? $this->frequency->name : '';
  134. $typeName = $this->getFormattedTypeField() ?? '';
  135. // Build display name with level and frequency
  136. $displayNameParts = [];
  137. if ($courseDiscipline) $displayNameParts[] = $courseDiscipline;
  138. $displayNameParts[] = $courseName;
  139. if ($levelName) $displayNameParts[] = $levelName;
  140. // if ($typeName) $displayNameParts[] = $typeName;
  141. if ($frequencyName) $displayNameParts[] = $frequencyName;
  142. return implode(' - ', $displayNameParts);
  143. }
  144. }