CourseController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Http;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Admin;
  7. use App\Models\Course;
  8. use App\Models\Destination;
  9. use App\Models\Level;
  10. use App\Models\Institute;
  11. use App\Traits\CourseSearch;
  12. use Auth;
  13. use Mail;
  14. use Session;
  15. use DB;
  16. class CourseController extends Controller
  17. {
  18. /**
  19. * Create a new controller instance.
  20. *
  21. * @return void
  22. */
  23. public function __construct()
  24. {
  25. }
  26. public function filters()
  27. {
  28. $destinations = model('Destination')::select('id','name','name as label','sams_id as refId')->orderBy('name')->get();
  29. $levels = model('Level')::select('id','name','name as label','sams_id as refId')->orderBy('name')->get();
  30. return [
  31. 'destinations' => $destinations,
  32. 'levels' => $levels
  33. ];
  34. }
  35. /**
  36. * Show the application dashboard.
  37. *
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function search(Request $request)
  41. {
  42. $courseIds = CourseSearch::searchByRequest($request);
  43. return [
  44. 'search_results' => $this->getCourseDetails($courseIds),
  45. 'filter_data' => $this->filterData($request)
  46. ];
  47. }
  48. function getCourseDetails($courseIds){
  49. $data = [];
  50. $result_ids = model('Course')::whereIn('id',$courseIds)->select('courses.*', \DB::raw('(SELECT name FROM institutes WHERE courses.institute_id = institutes.id ) as sort'))->orderBy('sort')->pluck('id')->toArray();
  51. foreach($result_ids as $id){
  52. $course = model('Course')::find($id);
  53. $campuses = model('CourseCampus')::where('course_id', $course->id)->pluck('campus_id')->toArray();
  54. $intakes = model('CourseIntake')::where('course_id', $course->id)->pluck('intake')->toArray();
  55. $intakeMonths = [];
  56. foreach($intakes as $intake){
  57. array_push($intakeMonths, date("M", mktime(0, 0, 0, $intake, 1)));
  58. }
  59. $data[] = [
  60. 'destination' => $course->destination->name,
  61. 'flag' => flag_url($course->destination->short_code),
  62. 'institute' => $course->institute->name,
  63. 'university_link' => $course->institute->website_link,
  64. 'campuses' => model('Campus')::whereIn('id', $campuses)->pluck('name')->toArray(),
  65. 'level' => $course->level->name,
  66. 'courseName' => $course->name,
  67. 'length' => $course->length.' months',
  68. 'tution_fee' => $course->tution_fee == 0?'-':$course->destination->currency.number_format($course->tution_fee, 0),
  69. 'intakes' => count($intakeMonths) > 0?$intakeMonths:'-',
  70. 'ielts' => $course->ielts?$course->ielts:'-',
  71. 'toefl' => $course->toefl?$course->toefl:'-',
  72. 'pte' => $course->pte?$course->pte:'-',
  73. 'duolingo' => $course->duolingo?$course->duolingo:'-',
  74. 'scholarship' => $course->scholarship?$course->scholarship:'-',
  75. 'deposit_required' => $course->deposit_required,
  76. //'min_gpa' => $course->min_gpa,
  77. //'ucas_fee' => $course->ucas_fee,
  78. 'sop' => $course->sop,
  79. 'cv' => $course->cv,
  80. 'reference' => $course->reference,
  81. 'entry_requirements' => $course->entry_requirement
  82. ];
  83. }
  84. return $data;
  85. }
  86. function filterData($request){
  87. $intakes = model('CourseIntake')::select('intake')->distinct()->orderBy('intake')->pluck('intake')->toArray();
  88. foreach($intakes as $intake){
  89. $intakeList[] = [
  90. 'id' => $intake,
  91. 'label' => date("F", mktime(0, 0, 0, $intake, 1))
  92. ];
  93. }
  94. $english_tests = ['IELTS', 'TOEFL', 'PTE', 'Duolingo'];
  95. foreach($english_tests as $test){
  96. $english_testsList[] = [
  97. 'id' => strtolower($test),
  98. 'label' => $test
  99. ];
  100. }
  101. $durations = [1,2,3,4];
  102. foreach($durations as $duration){
  103. $durationsList[] = [
  104. 'id' => (12 * $duration),
  105. 'label' => $duration==1?$duration.' Year':$duration.' Years'
  106. ];
  107. }
  108. return [
  109. 'institutes' => model('Institute')::where('destination_id', $request->destination_id)->select('id','name','name as label','sams_id as refId')->orderBy('name')->get(),
  110. 'intakes' => $intakeList,
  111. 'english_tests' => $english_testsList,
  112. 'durations' => $durationsList
  113. ];
  114. }
  115. }