Course.php 3.7 KB

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