CourseController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  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. use Response;
  17. class CourseController extends Controller
  18. {
  19. /**
  20. * Create a new controller instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. $this->middleware('admin');
  27. $this->api_base_url = apiUrl();
  28. }
  29. /**
  30. * Show the application dashboard.
  31. *
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function index()
  35. {
  36. $loggedin = Auth::guard('admin')->user();
  37. $institutes = Institute::orderBy('name', 'ASC')->get();
  38. return view('admin.courses.list',compact('institutes'));
  39. }
  40. public function load(Request $request)
  41. {
  42. $loggedin = Auth::guard('admin')->user();
  43. $search_text = $request->search;
  44. $qry = model('Course')::whereNotNull('destination_id');
  45. if($request->institute != 0){
  46. $qry = (clone $qry)->where('institute_id', $request->institute)->orderBy('name');
  47. }
  48. if($search_text){
  49. $qry = (clone $qry)->where('name', 'like', '%' . $search_text . '%');
  50. }
  51. $courses = $qry->orderBy('name', 'ASC')->paginate(40);
  52. return view('admin.courses.load',compact('courses'));
  53. }
  54. public function create()
  55. {
  56. $loggedin = Auth::guard('admin')->user();
  57. $destinations = $this->get_destinations();
  58. return view('admin.courses.add',compact('destinations'));
  59. }
  60. /**
  61. * Store a newly created resource in storage.
  62. *
  63. * @param \Illuminate\Http\Request $request
  64. * @return \Illuminate\Http\Response
  65. */
  66. public function store(Request $request)
  67. {
  68. $loggedin = Auth::guard('admin')->user();
  69. $this->validate($request,[
  70. 'courseName' => 'required',
  71. 'destination_id' => 'required',
  72. 'institute_id' => 'required',
  73. 'level_id' => 'required',
  74. 'department' => 'required',
  75. 'tutionFee' => 'required'
  76. ]);
  77. //dd($request);
  78. DB::beginTransaction();
  79. try {
  80. $course = new \App\Models\Course;
  81. $course->destination_id = $this->getModelId('Destination', $request->destination_id);
  82. $course->institute_id = $this->getInstituteId($request->institute_id, $course->destination_id);
  83. $course->level_id = $this->getModelId('Level', $request->level_id);
  84. $course->department = $request->department;
  85. $course->name = $request->courseName;
  86. $course->length = $request->courseLength;
  87. $course->tution_fee = $request->tutionFee;
  88. $course->ielts = $request->ielts;
  89. $course->toefl = $request->toefl;
  90. $course->pte = $request->pte;
  91. $course->duolingo = $request->duolingo;
  92. $course->scholarship = $request->scholarship;
  93. $course->deposit_required = $request->depositRequired;
  94. //$course->ucas_fee = $request->ucasFee;
  95. //$course->min_gpa = $request->minGpa;
  96. $course->sop = $request->sop;
  97. $course->cv = $request->cv;
  98. $course->reference = $request->reference;
  99. $course->entry_requirement = $request->entry_requirement;
  100. $course->save();
  101. if($request->campuses){
  102. foreach($request->campuses as $campus){
  103. $courseCampus = new \App\Models\CourseCampus;
  104. $courseCampus->campus_id = $this->getCampusId($course->institute_id, $campus);
  105. $courseCampus->course_id = $course->id;
  106. $courseCampus->save();
  107. }
  108. }
  109. if($request->intakes){
  110. foreach($request->intakes as $intake){
  111. $courseCampus = new \App\Models\CourseIntake;
  112. $courseCampus->intake = $intake;
  113. $courseCampus->course_id = $course->id;
  114. $courseCampus->save();
  115. }
  116. }
  117. if($request->tags){
  118. $tags = explode(',', $request->tags);
  119. foreach($tags as $tag){
  120. $tagRow = new \App\Models\CourseTag;
  121. $tagRow->keyword = $tag;
  122. $tagRow->course_id = $course->id;
  123. $tagRow->save();
  124. }
  125. }
  126. DB::commit();
  127. return redirect()->action('Admin\CourseController@index')->with('success','Successfully Created!');
  128. } catch (\Exception $e) {
  129. dd($e);
  130. DB::rollBack();
  131. return redirect()->action('Admin\CourseController@index')->with('error', "Something Wrong!");
  132. }
  133. }
  134. /**
  135. * Display the specified resource.
  136. *
  137. * @param int $id
  138. * @return \Illuminate\Http\Response
  139. */
  140. public function show($id)
  141. {
  142. $loggedin = Auth::guard('admin')->user();
  143. $course = Course::find($id);
  144. $selected = model('CourseCampus')::where('course_id', $course->id)->pluck('campus_id')->toArray();
  145. $selected_campuses = model('Campus')::whereIn('id', $selected)->get();
  146. return view('admin.courses.view',compact('course','selected_campuses'));
  147. }
  148. /**
  149. * Show the form for editing the specified resource.
  150. *
  151. * @param int $id
  152. * @return \Illuminate\Http\Response
  153. */
  154. public function edit($id)
  155. {
  156. $loggedin = Auth::guard('admin')->user();
  157. $course = Course::find($id);
  158. $destinations = $this->get_destinations();
  159. $institutes = $this->get_institutes($course->destination->sams_id);
  160. $campuses = $this->get_campuses($course->institute->sams_id);
  161. $levels = $this->get_levels($course->institute->sams_id,0);
  162. $intakes = $this->get_intakes($course->institute->sams_id,$course->level->sams_id);
  163. //dd($intakes);
  164. $selected = model('CourseCampus')::where('course_id', $course->id)->pluck('campus_id')->toArray();
  165. $selected_campuses = model('Campus')::whereIn('id', $selected)->pluck('sams_id')->toArray();
  166. $selected_intakes = model('CourseIntake')::where('course_id', $course->id)->pluck('intake')->toArray();
  167. $selected_tags = model('CourseTag')::where('course_id', $course->id)->pluck('keyword')->toArray();
  168. return view('admin.courses.edit',compact('course','destinations','institutes','campuses','levels','intakes','selected_campuses','selected_intakes','selected_tags'));
  169. }
  170. /**
  171. * Update the specified resource in storage.
  172. *
  173. * @param \Illuminate\Http\Request $request
  174. * @param int $id
  175. * @return \Illuminate\Http\Response
  176. */
  177. public function update(Request $request, $id)
  178. {
  179. $loggedin = Auth::guard('admin')->user();
  180. $this->validate($request,[
  181. 'courseName' => 'required',
  182. 'destination_id' => 'required',
  183. 'institute_id' => 'required',
  184. 'level_id' => 'required',
  185. 'department' => 'required',
  186. 'tutionFee' => 'required'
  187. ]);
  188. //dd($request);
  189. DB::beginTransaction();
  190. try {
  191. $course = model('Course')::find($request->id);
  192. $course->destination_id = $this->getModelId('Destination', $request->destination_id);
  193. $course->institute_id = $this->getInstituteId($request->institute_id, $course->destination_id);
  194. $course->level_id = $this->getModelId('Level', $request->level_id);
  195. $course->department = $request->department;
  196. $course->name = $request->courseName;
  197. $course->length = $request->courseLength;
  198. $course->tution_fee = $request->tutionFee;
  199. $course->ielts = $request->ielts;
  200. $course->toefl = $request->toefl;
  201. $course->pte = $request->pte;
  202. $course->duolingo = $request->duolingo;
  203. $course->scholarship = $request->scholarship;
  204. $course->deposit_required = $request->depositRequired;
  205. //$course->ucas_fee = $request->ucasFee;
  206. //$course->min_gpa = $request->minGpa;
  207. $course->sop = $request->sop;
  208. $course->cv = $request->cv;
  209. $course->reference = $request->reference;
  210. $course->entry_requirement = $request->entry_requirement;
  211. $course->update();
  212. if($request->campuses){
  213. model('CourseCampus')::where('course_id', $course->id)->delete();
  214. foreach($request->campuses as $campus){
  215. $courseCampus = new \App\Models\CourseCampus;
  216. $courseCampus->campus_id = $this->getCampusId($course->institute_id, $campus);
  217. $courseCampus->course_id = $course->id;
  218. $courseCampus->save();
  219. }
  220. }
  221. if($request->intakes){
  222. model('CourseIntake')::where('course_id', $course->id)->delete();
  223. foreach($request->intakes as $intake){
  224. $courseCampus = new \App\Models\CourseIntake;
  225. $courseCampus->intake = $intake;
  226. $courseCampus->course_id = $course->id;
  227. $courseCampus->save();
  228. }
  229. }
  230. if($request->tags){
  231. model('CourseTag')::where('course_id', $course->id)->delete();
  232. $tags = explode(',', $request->tags);
  233. foreach($tags as $tag){
  234. $tagRow = new \App\Models\CourseTag;
  235. $tagRow->keyword = $tag;
  236. $tagRow->course_id = $course->id;
  237. $tagRow->save();
  238. }
  239. }
  240. DB::commit();
  241. return redirect()->action('Admin\CourseController@index')->with('success','Successfully Updated!');
  242. }catch (\Exception $e) {
  243. dd($e);
  244. DB::rollBack();
  245. return redirect()->action('Admin\CourseController@index')->with('error', "Something Wrong!");
  246. }
  247. }
  248. /**
  249. * Remove the specified resource from storage.
  250. *
  251. * @param int $id
  252. * @return \Illuminate\Http\Response
  253. */
  254. public function destroy($id)
  255. {
  256. $loggedin = Auth::guard('admin')->user();
  257. $course = Course::find($id);
  258. DB::beginTransaction();
  259. try {
  260. model('CourseCampus')::where('course_id', $course->id)->delete();
  261. model('CourseIntake')::where('course_id', $course->id)->delete();
  262. model('CourseTag')::where('course_id', $course->id)->delete();
  263. Course::whereId($id)->delete();
  264. DB::commit();
  265. return redirect()->action('Admin\CourseController@index')->with('success','Successfully Deleted!');
  266. }catch (\Exception $e) {
  267. DB::rollBack();
  268. return redirect()->action('Admin\CourseController@index')->with('error', "Something Wrong!");
  269. }
  270. }
  271. public function bulkEntry()
  272. {
  273. $loggedin = Auth::guard('admin')->user();
  274. return view('admin.courses.bulk');
  275. }
  276. /**
  277. * Store a newly created resource in storage.
  278. *
  279. * @param \Illuminate\Http\Request $request
  280. * @return \Illuminate\Http\Response
  281. */
  282. public function storeExcel(Request $request)
  283. {
  284. $loggedin = Auth::guard('admin')->user();
  285. $this->validate($request,[
  286. 'file' => 'required'
  287. ]);
  288. DB::beginTransaction();
  289. try {
  290. $importedList = [];
  291. if($request->hasFile('file')){
  292. $course_import = new \App\Imports\CourseImport();
  293. $file = $request->file('file');
  294. $course_import->import($file);
  295. $importedList = $course_import->getImportedRows();
  296. }
  297. if(count($importedList)){
  298. foreach ($importedList as $key => $value) {
  299. if(!empty($value['name'])){
  300. $instituteInfo = $this->get_institute_and_destination(trim($value['institute']));
  301. $levelInfo = $this->get_level_by_name(trim($value['level']));
  302. $arr[] = $value;
  303. $course = new \App\Models\Course;
  304. $course->destination_id = $instituteInfo['destination_id'];
  305. $course->institute_id = $instituteInfo['institute_id'];
  306. $course->level_id = $levelInfo;
  307. $course->department = trim($value['department']);
  308. $course->name = trim($value['name']);
  309. $course->length = trim($value['length']);
  310. $course->tution_fee = trim($value['tuitionfee']);
  311. $course->ielts = $value['ielts']?trim($value['ielts']):null;
  312. $course->toefl = $value['toefl']?trim($value['toefl']):null;
  313. $course->pte = $value['pte']?trim($value['pte']):null;
  314. $course->duolingo = $value['duolingo']?trim($value['duolingo']):null;
  315. $course->scholarship = trim($value['scholarship']);
  316. $course->deposit_required = $value['depositrequired'];
  317. $course->sop = trim($value['sop'])=='Yes'?'1':'0';
  318. $course->cv = trim($value['cv'])=='Yes'?'1':'0';
  319. $course->reference = trim($value['reference']);
  320. $course->entry_requirement = $value['entryrequirement'];
  321. $course->save();
  322. if($value['campuses']){
  323. $campuses = explode(',', $value['campuses']);
  324. foreach($campuses as $campus){
  325. $courseCampus = new \App\Models\CourseCampus;
  326. $courseCampus->campus_id = $this->get_campus_by_name(trim($campus), $course->institute_id);
  327. $courseCampus->course_id = $course->id;
  328. $courseCampus->save();
  329. }
  330. }
  331. if($value['intakes']){
  332. $intakes = explode(',', $value['intakes']);
  333. $monthList = [
  334. 'Jan' => 1,
  335. 'Feb' => 2,
  336. 'Mar' => 3,
  337. 'Apr' => 4,
  338. 'May' => 5,
  339. 'Jun' => 6,
  340. 'Jul' => 7,
  341. 'Aug' => 8,
  342. 'Sept' => 9,
  343. 'Sep' => 9,
  344. 'Oct' => 10,
  345. 'Nov' => 11,
  346. 'Dec' => 12,
  347. ];
  348. foreach($intakes as $intake){
  349. $dt = new \App\Models\CourseIntake;
  350. $dt->intake = $monthList[trim($intake)];
  351. $dt->course_id = $course->id;
  352. $dt->save();
  353. }
  354. }
  355. if($value['tags']){
  356. $tags = explode(',', $value['tags']);
  357. foreach($tags as $tag){
  358. $tagRow = new \App\Models\CourseTag;
  359. $tagRow->keyword = trim($tag);
  360. $tagRow->course_id = $course->id;
  361. $tagRow->save();
  362. }
  363. }
  364. }
  365. }
  366. }
  367. DB::commit();
  368. return redirect()->action('Admin\CourseController@index')->with('success','Successfully Uploaded!');
  369. } catch (\Exception $e) {
  370. dd($e);
  371. DB::rollBack();
  372. return redirect()->action('Admin\CourseController@index')->with('error', "Something Wrong!");
  373. }
  374. }
  375. public function downloadBulkTemplate()
  376. {
  377. $file = public_path(). "/templates/courses_bulk_upload_template.xlsx";
  378. if (file_exists($file)) {
  379. return Response::download($file);
  380. } else {
  381. echo('File not found.');
  382. }
  383. }
  384. public function get_institute_and_destination($instituteName){
  385. $data = [];
  386. $check = findRowByColumn('Institute','name', $instituteName);
  387. if($check){
  388. $data = ['institute_id' => $check->id, 'destination_id' => $check->destination_id];
  389. }else{
  390. $getData = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-institute-by-name?name='.$instituteName);
  391. if($getData){
  392. $destination_id = $this->getModelId('Destination', $getData['destination'].'::'.$getData['destinationName']);
  393. $data = ['institute_id' => $this->getInstituteId($getData['id'].'::'.$getData['name'], $destination_id), 'destination_id' => $destination_id];
  394. }
  395. }
  396. return $data;
  397. }
  398. public function get_level_by_name($name){
  399. $data = [];
  400. $check = findRowByColumn('Level','name', $name);
  401. if($check){
  402. return $check->id;
  403. }else{
  404. $getData = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-level-by-name?name='.$name);
  405. if($getData){
  406. return $this->getModelId('Level', $getData['id'].'::'.$getData['name']);
  407. }
  408. }
  409. return $data;
  410. }
  411. public function get_campus_by_name($name, $institute){
  412. $data = [];
  413. $check = model('Campus')::where('name', $name)->where('institute_id', $institute)->first();
  414. if($check){
  415. return $check->id;
  416. }else{
  417. $sams_id = model('Institute')::find($institute)->sams_id;
  418. $getData = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-campus-by-name?name='.$name.'&institute='.$sams_id);
  419. if($getData){
  420. return $this->getCampusId($institute, $getData['id'].'::'.$getData['campus_name']);
  421. }
  422. }
  423. return $data;
  424. }
  425. public function get_destinations(){
  426. $data = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-destinations');
  427. return $data;
  428. }
  429. public function get_institutes($destination){
  430. $data = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-institutes?destination='.$destination.'&type=all');
  431. return $data;
  432. }
  433. public function get_campuses($institute){
  434. $data = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-campuses?institute='.$institute);
  435. return $data;
  436. }
  437. public function get_levels($institute, $group){
  438. $data = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-levels?institute='.$institute.'&group='.$group);
  439. return $data;
  440. }
  441. public function get_intakes($institute, $level){
  442. $data = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-intakes?institute='.$institute.'&level='.$level);
  443. return $data;
  444. }
  445. public function getCountry($id){
  446. $data = getApiRequest($this->api_base_url.'api/sams-institutes/v1/get-country?id='.$id);
  447. return $data;
  448. }
  449. public function getInstituteId($input, $destination) //create or find row by sams Id
  450. {
  451. $arr = explode('::', $input);
  452. $id = $arr[0];
  453. $name = $arr[1];
  454. $check = findRowByColumn('Institute','sams_id', $id);
  455. if($check){
  456. return $check->id;
  457. }else{
  458. $row = new \App\Models\Institute;
  459. $row->sams_id = $id;
  460. $row->destination_id = $destination;
  461. $row->name = $name;
  462. $row->save();
  463. return $row->id;
  464. }
  465. }
  466. public function getModelId($model, $input) //create or find row by sams Id
  467. {
  468. $arr = explode('::', $input);
  469. $id = $arr[0];
  470. $name = $arr[1];
  471. $check = findRowByColumn($model,'sams_id', $id);
  472. if($check){
  473. return $check->id;
  474. }else{
  475. if($model == 'Destination'){
  476. $row = new \App\Models\Destination;
  477. $country = $this->getCountry($id);
  478. if($country){
  479. $row->short_code = $country['short_code'];
  480. $row->currency = $country['currency_symbol'];
  481. }
  482. }elseif($model == 'Level'){
  483. $row = new \App\Models\Level;
  484. }
  485. $row->sams_id = $id;
  486. $row->name = $name;
  487. $row->save();
  488. return $row->id;
  489. }
  490. }
  491. public function getCampusId($institute, $input) //create or find row by sams Id
  492. {
  493. $arr = explode('::', $input);
  494. $id = $arr[0];
  495. $name = $arr[1];
  496. $check = findRowByColumn('Campus','sams_id', $id);
  497. if($check){
  498. return $check->id;
  499. }else{
  500. $row = new \App\Models\Campus;
  501. $row->sams_id = $id;
  502. $row->institute_id = $institute;
  503. $row->name = $name;
  504. $row->save();
  505. return $row->id;
  506. }
  507. }
  508. public function edit_entry_req($institute_id, $course_id)
  509. {
  510. $loggedin = Auth::guard('admin')->user();
  511. $serial = 1;
  512. if($course_id != 0){
  513. $course = Course::find($course_id);
  514. $serial = Course::where('institute_id',$institute_id)->where('id', '<=', $course_id)->count();
  515. }else{
  516. $course = Course::where('institute_id',$institute_id)->first();
  517. }
  518. $total_courses = Course::where('institute_id',$institute_id)->count();
  519. $selected_tags = model('CourseTag')::where('course_id', $course->id)->pluck('keyword')->toArray();
  520. return view('admin.courses.edit_entry_req',compact('course','selected_tags','serial','total_courses'));
  521. }
  522. /**
  523. * Update the specified resource in storage.
  524. *
  525. * @param \Illuminate\Http\Request $request
  526. * @param int $id
  527. * @return \Illuminate\Http\Response
  528. */
  529. public function update_entry_req(Request $request, $id)
  530. {
  531. $loggedin = Auth::guard('admin')->user();
  532. DB::beginTransaction();
  533. try {
  534. $course = model('Course')::find($request->id);
  535. $course->entry_requirement = $request->entry_requirement;
  536. $course->update();
  537. if($request->tags){
  538. model('CourseTag')::where('course_id', $course->id)->delete();
  539. $tags = explode(',', $request->tags);
  540. foreach($tags as $tag){
  541. $tagRow = new \App\Models\CourseTag;
  542. $tagRow->keyword = $tag;
  543. $tagRow->course_id = $course->id;
  544. $tagRow->save();
  545. }
  546. }
  547. DB::commit();
  548. $next_course = model('Course')::where('id','>',$request->id)->first();
  549. if($next_course){
  550. return redirect()->action('Admin\CourseController@edit_entry_req',['institute'=>$course->institute_id,'course'=>$next_course->id])->with('success','Successfully Updated! Redirected to Next Course.');
  551. }else{
  552. return redirect()->action('Admin\ListController@institutes')->with('success','All Done!');
  553. }
  554. }catch (\Exception $e) {
  555. DB::rollBack();
  556. return redirect()->action('Admin\CourseController@index')->with('error', "Something Wrong!");
  557. }
  558. }
  559. }