PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/StudentsController.php

https://gitlab.com/MualnuamSolutions/bhssjunior
PHP | 609 lines | 447 code | 106 blank | 56 comment | 62 complexity | 6d361cfed2097c788fd72fbefc0fe3b3 MD5 | raw file
  1. <?php
  2. use \Mualnuam\ImageHelper;
  3. class StudentsController extends \BaseController
  4. {
  5. public function __construct()
  6. {
  7. $this->beforeFilter('sentry');
  8. }
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return Response
  13. */
  14. public function index()
  15. {
  16. $studentTable = (new Student)->getTable();
  17. $enrollmentTable = (new Enrollment)->getTable();
  18. $academicSessionTable = (new AcademicSession)->getTable();
  19. $classRoomTable = (new ClassRoom)->getTable();
  20. $search = Input::get('s');
  21. $order = Input::get('order', 'name');
  22. $limit = Input::get('limit', Config::get('view.pagination_limit'));
  23. $orderOptions = [
  24. 'name' => 'Order by Name Alphabetically',
  25. 'id' => 'Order By Recently Added',
  26. 'regno' => 'Order By Reg. No',
  27. ];
  28. $limits = [
  29. 15 => 'Show 15',
  30. 25 => 'Show 25',
  31. 40 => 'Show 40',
  32. 50 => 'Show 50',
  33. 100 => 'Show 100',
  34. ];
  35. $orderDirection = [
  36. 'name' => 'asc',
  37. 'id' => 'desc',
  38. 'regno' => 'asc'
  39. ];
  40. $currentSession = AcademicSession::currentSession();
  41. $students = Student::leftJoin($enrollmentTable, $enrollmentTable . '.student_id', '=', $studentTable . '.id')
  42. ->where(function($query) use ($search, $studentTable, $currentSession) {
  43. if($search != "") {
  44. $query->where($studentTable . '.name', 'LIKE', '%' . $search . '%');
  45. $query->orWhere($studentTable . '.father', 'LIKE', '%' . $search . '%');
  46. $query->orWhere($studentTable . '.regno', 'LIKE', '%' . $search . '%');
  47. $query->orWhere($studentTable . '.contact1', 'LIKE', $search . '%');
  48. }
  49. })
  50. ->groupBy($studentTable . '.id')
  51. ->orderBy($studentTable . '.' . $order, $orderDirection[$order])
  52. ->select(
  53. $studentTable . '.id',
  54. $studentTable . '.regno',
  55. $studentTable . '.name',
  56. $studentTable . '.father',
  57. $studentTable . '.contact1'
  58. )
  59. ->paginate($limit);
  60. return View::make('students.index', compact('students', 'orderOptions', 'limits'));
  61. }
  62. /**
  63. * Show the form for creating a new resource.
  64. *
  65. * @return Response
  66. */
  67. public function create()
  68. {
  69. $classRooms = ClassRoom::orderBy('name', 'asc')->lists('name', 'id');
  70. $academicSessions = AcademicSession::getDropDownList();
  71. return View::make('students.create', compact('classRooms', 'academicSessions'));
  72. }
  73. /**
  74. * Store a newly created resource in storage.
  75. *
  76. * @return Response
  77. */
  78. public function store()
  79. {
  80. $student = new Student;
  81. if ($student->save()) {
  82. Notification::success('New student created');
  83. return Redirect::route('students.index');
  84. } else {
  85. return Redirect::route('students.create', $student->id)->withErrors($student->errors());
  86. }
  87. }
  88. /**
  89. * Display the specified resource.
  90. *
  91. * @param int $id
  92. * @return Response
  93. */
  94. public function show($id)
  95. {
  96. $student = Student::find($id);
  97. $enrollments = Enrollment::with('academicSession', 'classRoom')->whereStudentId($student->id)->paginate();
  98. $photos = Photo::whereStudentId($student->id)->orderBy('created_at', 'desc')->paginate();
  99. $measurements = Measurement::get($student->id, $paginate = false);
  100. $classRooms = ClassRoom::orderBy('name', 'asc')->lists('name', 'id');
  101. return View::make('students.show', compact('student', 'classRooms', 'enrollments', 'photos', 'measurements'));
  102. }
  103. /**
  104. * Show the form for editing the specified resource.
  105. *
  106. * @param int $id
  107. * @return Response
  108. */
  109. public function edit($id)
  110. {
  111. $student = Student::find($id);
  112. $classRooms = ClassRoom::orderBy('name', 'asc')->lists('name', 'id');
  113. $academicSessions = AcademicSession::getDropDownList();
  114. return View::make('students.edit', compact('student', 'academicSessions', 'classRooms'));
  115. }
  116. /**
  117. * Update the specified resource in storage.
  118. *
  119. * @param int $id
  120. * @return Response
  121. */
  122. public function update($id)
  123. {
  124. $student = Student::find($id);
  125. if ($student->save()) {
  126. Notification::success('Student updated');
  127. return Redirect::route('students.edit', $id);
  128. }
  129. }
  130. /**
  131. * Remove the specified resource from storage.
  132. *
  133. * @param int $id
  134. * @return Response
  135. */
  136. public function destroy($id)
  137. {
  138. if($id) {
  139. $student = Student::find($id);
  140. if($student) {
  141. // Delete student enrollment
  142. Enrollment::where("student_id", "=", $student->id)->delete();
  143. // Delete student marks
  144. Mark::where("student_id", "=", $student->id)->delete();
  145. // Delete student measurement if any
  146. Measurement::where("student_id", "=", $student->id)->delete();
  147. // Delete student photos if any
  148. Photo::where("student_id", "=", $student->id)->delete();
  149. // Delete student
  150. Student::destroy($student->id);
  151. Notification::success('Student successfully removed.');
  152. return Redirect::route('students.index');
  153. }
  154. Notification::error('Student not found');
  155. return Redirect::route('students.index');
  156. }
  157. }
  158. public function enrollments($id)
  159. {
  160. $student = Student::find($id);
  161. $enrollments = Enrollment::with('academicSession', 'classRoom')
  162. ->where('student_id', '=', $id)->paginate();
  163. return View::make('students.enrollments', compact('enrollments', 'student'));
  164. }
  165. /*public function editEnrollment($id)
  166. {
  167. $enrollment = Enrollment::find($id);
  168. $student = Student::find($enrollment->student_id);
  169. $classRooms = ClassRoom::orderBy('name', 'asc')->lists('name', 'id');
  170. $academicSessions = AcademicSession::getDropDownList();
  171. return View::make('students.editEnrollment', compact('enrollment', 'student', 'academicSessions', 'classRooms'));
  172. }*/
  173. public function addEnrollment($id)
  174. {
  175. $student = Student::find($id);
  176. $classRooms = ClassRoom::orderBy('name', 'asc')->lists('name', 'id');
  177. $academicSessions = AcademicSession::getDropDownList();
  178. return View::make('students.addenrollment', compact('student', 'academicSessions', 'classRooms'));
  179. }
  180. public function storeEnrollment($id)
  181. {
  182. $student = Student::find($id);
  183. $enrollment = new Enrollment;
  184. $enrollment->student_id = $student->id;
  185. if ($enrollment->save()) {
  186. Notification::success('New enrollment created for ' . $student->name);
  187. return Redirect::route('students.enrollments', $student->id);
  188. } else {
  189. return Redirect::route('students.addEnrollment', $student->id)->withErrors($enrollment->errors());
  190. }
  191. }
  192. public function removeEnrollment($id)
  193. {
  194. $enrollment = Enrollment::find($id);
  195. $student = Student::find($enrollment->student_id);
  196. if ($enrollment->delete()) {
  197. Notification::success('Enrollment record removed for ' . $student->name);
  198. return Redirect::route('students.enrollments', $student->id);
  199. } else {
  200. Notification::alert('Enrollment record cannot be removed.');
  201. return Redirect::route('students.enrollments', $student->id);
  202. }
  203. }
  204. public function photos($id)
  205. {
  206. $student = Student::find($id);
  207. $photos = Photo::orderBy('created_at', 'desc')->where('student_id', '=', $id)->paginate();
  208. return View::make('students.photos', compact('photos', 'student'));
  209. }
  210. public function defaultPhoto($id)
  211. {
  212. $student = Student::find($id);
  213. $photo = Photo::find(Input::get('default'));
  214. if($photo) {
  215. Photo::where('student_id', '=', $student->id)->update(['default' => 0]);
  216. $photo->default = 1;
  217. $photo->save();
  218. Notification::success('Default photo updated successfully.');
  219. return Redirect::route('students.photos', $student->id);
  220. }
  221. else {
  222. Notification::alert('Setting default photo failed. Photo not found');
  223. return Redirect::route('students.photos', $student->id);
  224. }
  225. }
  226. public function addPhoto($id)
  227. {
  228. $student = Student::find($id);
  229. return View::make('students.addphoto', compact('student'));
  230. }
  231. public function storePhoto($id)
  232. {
  233. $student = Student::find($id);
  234. Photo::where('student_id', '=', $student->id)->update(['default' => 0]);
  235. $photo = new Photo;
  236. $photo->path = ImageHelper::store(Input::get('photo'));
  237. $photo->student_id = $student->id;
  238. $photo->default = 1;
  239. $photo->save();
  240. Notification::success('New photo uploaded successfully.');
  241. return Redirect::route('students.photos', $student->id);
  242. }
  243. public function removePhoto($id)
  244. {
  245. $student = Student::find($id);
  246. $photo = Photo::find(Input::get('delete'));
  247. if($photo) {
  248. ImageHelper::remove(public_path($photo->path));
  249. if($photo->default) {
  250. $photo->delete();
  251. Photo::where('student_id', '=', $student->id)->update(['default' => 0]);
  252. $photo = Photo::orderBy('created_at', 'desc')->where('student_id', '=', $student->id)->first();
  253. $photo->default = 1;
  254. $photo->save();
  255. }
  256. else {
  257. $photo->delete();
  258. }
  259. Notification::success('Photo deleted successfully.');
  260. return Redirect::route('students.photos', $student->id);
  261. }
  262. else {
  263. Notification::alert('Delete photo failed. Photo not found');
  264. return Redirect::route('students.photos', $student->id);
  265. }
  266. }
  267. public function measurements($id)
  268. {
  269. $student = Student::find($id);
  270. $measurements = Measurement::get($student->id, $paginate = true);
  271. return View::make('students.measurements', compact('measurements', 'student'));
  272. }
  273. public function addMeasurement($id)
  274. {
  275. $student = Student::find($id);
  276. $academicSessions = AcademicSession::getDropDownList();
  277. return View::make('students.addmeasurement', compact('student', 'academicSessions'));
  278. }
  279. public function storeMeasurement($id)
  280. {
  281. $student = Student::find($id);
  282. $measurement = new Measurement;
  283. $measurement->student_id = $student->id;
  284. $measurement->created_at = Input::get('created_at');
  285. if ($measurement->save()) {
  286. Notification::success('New measurement added for ' . $student->name);
  287. return Redirect::route('students.measurements', $student->id);
  288. } else {
  289. return Redirect::route('students.addMeasurement', $student->id)->withErrors($measurement->errors());
  290. }
  291. }
  292. public function removeMeasurement($id)
  293. {
  294. $measurement = Measurement::find($id);
  295. $student = Student::find($measurement->student_id);
  296. if ($measurement->delete()) {
  297. Notification::success('Measurement record removed for ' . $student->name);
  298. return Redirect::route('students.measurements', $student->id);
  299. } else {
  300. Notification::alert('Measurement record cannot be removed.');
  301. return Redirect::route('students.measurements', $student->id);
  302. }
  303. }
  304. public function preparePrint()
  305. {
  306. $studentTable = (new Student)->getTable();
  307. $classRoomTable = (new Student)->getTable();
  308. $enrollmentTable = (new Enrollment)->getTable();
  309. $academicSessions = AcademicSession::getDropDownList();
  310. $classes = array('' => 'Select Class') + ClassRoom::getDropDownList();
  311. $students = [];
  312. $academicSession = Input::get('academic_session', AcademicSession::currentSession()->id);
  313. $class = Input::get('class');
  314. $address = Input::get('address');
  315. $house = Input::get('house');
  316. $gender = Input::get('gender');
  317. $input = Input::all();
  318. $academicSession = AcademicSession::find($academicSession);
  319. if($academicSession) {
  320. $students = Enrollment::join($studentTable, $studentTable . '.id', '=', $enrollmentTable . '.student_id')
  321. ->where(function($q) use($academicSession, $input){
  322. $q->where('academic_session_id', '=', $academicSession->id);
  323. if(isset($input['class']) && $input['class'] != '')
  324. $q->where('class_room_id', '=', $input['class']);
  325. if(isset($input['address']) && $input['address'] != '')
  326. $q->where('address', 'LIKE', "%{$input['address']}%");
  327. if(isset($input['house']) && $input['house'] != '')
  328. $q->where('house', 'LIKE', "%{$input['house']}%");
  329. if(isset($input['gender']) && $input['gender'] != "")
  330. $q->where('gender', '=', $input['gender']);
  331. })
  332. ->orderBy("{$studentTable}.name", 'asc')
  333. ->paginate(50);
  334. }
  335. else {
  336. Notification::alert('Invalid selection, please try again');
  337. return Redirect::route('students.print');
  338. }
  339. $printFields1 = [
  340. 'regno' => "Registration No",
  341. 'name' => "Name",
  342. 'gender' => "Gender",
  343. 'dob' => "Date of Birth",
  344. 'father' => "Father's Name",
  345. 'fathers_occupation' => "Father's Occupation",
  346. 'mother' => "Mother's Name",
  347. 'mothers_occupation' => "Mother's Occupation",
  348. 'address' => "Address",
  349. 'contact1' => "Contact 1",
  350. 'contact2' => "Contact 2",
  351. ];
  352. $printFields2 = [
  353. 'session' => "Session",
  354. 'class' => "Class",
  355. 'roll_no' => "Roll No",
  356. 'house' => "House",
  357. 'height' => "Height",
  358. 'weight' => "Weight",
  359. 'photo' => "Photo",
  360. ];
  361. return View::make('students.prepareprint', compact('academicSessions', 'classes', 'students', 'printFields1', 'printFields2'));
  362. }
  363. public function doPrint()
  364. {
  365. $fields = explode(',', Input::get('fields'));
  366. $order = Input::get('order');
  367. $direction = Input::get('direction');
  368. $export = Input::get('export', false);
  369. $print = Input::get('print', false);
  370. $studentTable = (new Student)->getTable();
  371. $classRoomTable = (new ClassRoom)->getTable();
  372. $enrollmentTable = (new Enrollment)->getTable();
  373. $photoTable = (new Photo)->getTable();
  374. $measurementTable = (new Measurement)->getTable();
  375. $academicSession = Input::get('academic_session', AcademicSession::currentSession()->id);
  376. $class = Input::get('class');
  377. $address = Input::get('address');
  378. $house = Input::get('house');
  379. $gender = Input::get('gender');
  380. $order = Input::get('order', 'name');
  381. $direction = Input::get('direction', 'asc');
  382. $input = Input::all();
  383. if($order == '')
  384. $order = $studentTable . '.name';
  385. elseif($order == 'class')
  386. $order = 'level';
  387. elseif($order == 'name')
  388. $order = $studentTable . '.name';
  389. if($direction == '')
  390. $direction = 'asc';
  391. $academicSession = AcademicSession::find($academicSession);
  392. if($academicSession) {
  393. $results = Enrollment::join($studentTable, $studentTable . '.id', '=', $enrollmentTable . '.student_id')
  394. ->leftJoin($photoTable, $photoTable . '.student_id', '=', $studentTable . '.id')
  395. ->where($enrollmentTable . '.academic_session_id', '=', $academicSession->id)
  396. ->where(function($q) use($academicSession, $input){
  397. if(isset($input['class']) && $input['class'] != '')
  398. $q->where('class_room_id', '=', $input['class']);
  399. if(isset($input['address']) && $input['address'] != '')
  400. $q->where('address', 'LIKE', "%{$input['address']}%");
  401. if(isset($input['house']) && $input['house'] != '')
  402. $q->where('house', 'LIKE', "%{$input['house']}%");
  403. if(isset($input['gender']) && $input['gender'] != "")
  404. $q->where('gender', '=', $input['gender']);
  405. })
  406. ->orderBy($order, $direction)
  407. ->get();
  408. $class = ClassRoom::find($class);
  409. $printFields = [
  410. 'photo' => "Photo",
  411. 'regno' => "Registration No",
  412. 'roll_no' => "Roll No",
  413. 'name' => "Name",
  414. 'gender' => "Gender",
  415. 'dob' => "Date of Birth",
  416. 'father' => "Father's Name",
  417. 'fathers_occupation' => "Father's Occupation",
  418. 'mother' => "Mother's Name",
  419. 'mothers_occupation' => "Mother's Occupation",
  420. 'address' => "Address",
  421. 'contact1' => "Contact 1",
  422. 'contact2' => "Contact 2",
  423. 'house' => "House",
  424. 'height' => "Height",
  425. 'weight' => "Weight",
  426. 'session' => "Session",
  427. 'class' => "Class",
  428. ];
  429. if($export) {
  430. $this->exportCsv($results, $fields, $address, $house, $academicSession, $class, $gender);
  431. }
  432. return View::make('students.print', compact('results', 'fields', 'address', 'house', 'academicSession', 'class', 'gender', 'printFields'));
  433. }
  434. }
  435. private function exportCsv($results, $fields, $address, $house, $academicSession, $class, $gender)
  436. {
  437. $printFields = [
  438. 'regno' => "Registration No",
  439. 'roll_no' => "Roll No",
  440. 'name' => "Name",
  441. 'gender' => "Gender",
  442. 'dob' => "Date of Birth",
  443. 'father' => "Father's Name",
  444. 'fathers_occupation' => "Father's Occupation",
  445. 'mother' => "Mother's Name",
  446. 'mothers_occupation' => "Mother's Occupation",
  447. 'class' => "Class",
  448. 'session' => "Session",
  449. 'address' => "Address",
  450. 'contact1' => "Contact 1",
  451. 'contact2' => "Contact 2",
  452. 'house' => "House",
  453. 'height' => "Height",
  454. 'weight' => "Weight",
  455. 'photo' => "Photo",
  456. ];
  457. // output headers so that the file is downloaded rather than displayed
  458. header('Content-Type: text/csv; charset=utf-8');
  459. header('Content-Disposition: attachment; filename='.uniqid().'_'.date('Y-m-d').'.csv');
  460. // create a file pointer connected to the output stream
  461. $output = fopen('php://output', 'w');
  462. $columns = ['#'];
  463. foreach($printFields as $field => $label) {
  464. if(in_array($field, $fields)) {
  465. $columns[] = $printFields[$field];
  466. }
  467. }
  468. // output the column headings
  469. fputcsv($output, $columns);
  470. foreach($results as $key => $row) {
  471. $data = [++$key];
  472. $measurements = Measurement::whereStudentId($row->student_id)
  473. ->whereAcademicSessionId($academicSession->id)
  474. ->get();
  475. foreach($printFields as $field => $label) {
  476. if(in_array($field, $fields)) {
  477. if($field == 'session') {
  478. $data[] = $row->academicSession->session;
  479. }
  480. elseif($field == 'class') {
  481. $data[] = $row->classRoom->name;
  482. }
  483. elseif($field == 'photo') {
  484. $data[] = 'http://software.bhssjr.com/' . $row->path;
  485. }
  486. elseif(in_array($field, ['height', 'weight'])) {
  487. $measurementData = [];
  488. foreach($measurements as $measurement) {
  489. $measurementData[] = $measurement->$field . ($field=='weight'?' Kg':'').'-'.date('d/m/Y', strtotime($measurement->created_at));
  490. }
  491. $data[] = implode(' | ', $measurementData);
  492. }
  493. else {
  494. $data[] = $row->$field ? : '-';
  495. }
  496. }
  497. }
  498. fputcsv($output, $data);
  499. }
  500. exit;
  501. }
  502. }