/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // Insert dummy record
     Centre::create(['name' => 'Henderson', 'address' => '117 Bukit Merah View', 'postal_code' => '151117', 'lng' => 103.82190657700056, 'lat' => 1.2843010410004467]);
     Centre::create(['name' => 'Dakota Crescent', 'address' => '62 Dakota Crescent', 'postal_code' => '390062', 'lng' => 103.889069356, 'lat' => 1.3076509340004]);
     Centre::create(['name' => 'Toa Payoh', 'address' => '169 Toa Payoh Lorong 1', 'postal_code' => '310169', 'lng' => 103.84266425, 'lat' => 1.3317424160005]);
     Centre::create(['name' => 'Singapore General Hospital', 'address' => '1 Hospital Drive, 169608, Singapore', 'postal_code' => '169608', 'lng' => 103.835499702, 'lat' => 1.2798006200005]);
     Centre::create(['name' => 'Bukit Merah Polyclinic', 'address' => '163 Bukit Merah Central, 150163, Singapore', 'postal_code' => '150163', 'lng' => 103.816965114, 'lat' => 1.2837871400004]);
     Centre::create(['name' => 'Queenstown Polyclinic', 'address' => '580 Stirling Road, 148958, Singapore', 'postal_code' => '148958', 'lng' => 103.80115498500055, 'lat' => 1.2985064950004244]);
 }
 public function getRecords(SearchCourseRequest $request)
 {
     //Get level records
     $levelString = implode(',', $request->input('level'));
     $levels = explode(',', $levelString);
     //Get Method records
     $methodString = implode(',', $request->input('method'));
     $methods = explode(',', $methodString);
     //Get Paper records
     $paperString = implode(',', $request->input('paper'));
     $papers = explode(',', $paperString);
     //Get Element records
     $elementString = implode(',', $request->input('element'));
     $elements = explode(',', $elementString);
     //Get Centre records
     $centreString = implode(',', $request->input('centre'));
     $centres = explode(',', $centreString);
     $courseLevels = Course::whereIn('level_id', $levels)->whereIn('method_id', $methods)->whereIn('paper_id', $papers)->whereIn('element_id', $elements)->whereIn('centre_id', $centres)->get();
     $centreList = Centre::whereIn('id', $centres)->get();
     return view('acca.results')->with('centres', $centreList)->with('courses', $courseLevels);
 }
 /**
  * Update an existing location/centre.
  * Responds to requests to POST /centres/{id}
  *
  * @param  int  $id  the ID of the location/centre
  * @param  \App\Http\Requests\EditCentreRequest  $request
  * @return Response
  */
 public function update($id, EditCentreRequest $request)
 {
     $errors = array();
     $geoInfo = json_decode($this->postalCodeToAddress($request), true);
     if ($geoInfo['status'] == 'error') {
         $errors = array_add($errors, 'postal', 'Postal code does not exist.');
     }
     if (count($errors) > 0) {
         return back()->withErrors($errors)->withInput();
     } else {
         $centre = Centre::findOrFail($id);
         $centre->update(['name' => $request->get('name'), 'address' => $request->get('address'), 'postal_code' => $request->get('postal'), 'lng' => $geoInfo['x'], 'lat' => $geoInfo['y']]);
         return redirect('centres')->with('success', 'Location is updated successfully!');
     }
 }
 public function compose(View $view)
 {
     $view->with('centres', \App\Centre::all());
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $centre = Request::all();
     Centre::create($centre);
     return redirect()->route('admin_centres_index')->with('status', 'Centre created');
 }
 /**
  * Get the sub chart (right) for the given branch/centre.
  * Responds to requests to GET /stats/getSubCharts
  *
  * @param  \Illuminate\Http\Request  $request
  * @return Response
  */
 public function ajaxRetrieveSubCharts(Request $request)
 {
     $charts = array();
     $centre = $request->get('centre');
     if ($centre == 'all') {
         $centreActivities = Centre::with('activities')->whereIn('centre_id', Auth::user()->centres->lists('centre_id'))->get()->sortBy('name');
         $html = View::make('stats._centresTable', compact('centreActivities'))->render();
         $charts['html'] = $html;
     } else {
         $unfilled = Activity::ofCentre($centre)->unfilled()->get();
         $urgent = Activity::ofCentre($centre)->urgent()->get();
         $awaitingApproval = Activity::ofCentre($centre)->awaitingApproval()->get();
         $approved = Activity::ofCentre($centre)->approved()->get();
         $html = View::make('stats._centreTable', compact('unfilled', 'urgent', 'awaitingApproval', 'approved'))->render();
         $charts['html'] = $html;
     }
     return json_encode($charts);
 }
 /**
  * Show the form to edit an elderly/senior.
  * Responds to requests to GET /elderly/{id}/edit
  *
  * @param  int  $id  the ID of the elderly/senior
  * @return Response
  */
 public function edit($id)
 {
     $validator = JsValidator::formRequest('App\\Http\\Requests\\EditElderlyRequest');
     $elderly = Elderly::findOrFail($id);
     $genderList = ['M' => 'Male', 'F' => 'Female'];
     $languages = ElderlyLanguage::distinct()->lists('language', 'language')->sort();
     if (Auth::user()->is_admin) {
         $centreList = Centre::all()->lists('name', 'centre_id')->sort();
     } else {
         $centreList = Auth::user()->centres()->get()->lists('name', 'centre_id')->sort();
     }
     if (is_array(old('languages'))) {
         foreach (old('languages') as $l) {
             $languages[$l] = $l;
         }
     }
     return view('elderly.edit', compact('validator', 'elderly', 'centreList', 'genderList', 'languages'));
 }
 /**
  * Show the form to edit a staff.
  * Responds to requests to GET /staff/{id}/edit
  *
  * @param  int  $id  the ID of the staff
  * @return Response
  */
 public function edit($id)
 {
     if (Auth::user()->staff_id == $id && !Auth::user()->is_admin) {
         return redirect('staff')->withErrors(['You cannot edit your own profile.']);
     }
     $validator = JsValidator::formRequest('App\\Http\\Requests\\EditStaffRequest');
     $staff = Staff::findOrFail($id);
     $staffType = [false => 'Regular Staff', true => 'Administrator'];
     if (Auth::user()->is_admin) {
         $centreList = Centre::all()->lists('name', 'centre_id')->sort();
     } else {
         $centreList = Auth::user()->centres()->get()->lists('name', 'centre_id');
     }
     return view('staff.edit', compact('validator', 'staff', 'staffType', 'centreList'));
 }
 /**
  * Update an existing activity.
  * Responds to requests to PUT /activities/{id}
  *
  * @param  int  $id  the ID of the activity
  * @param  \App\Http\Requests\EditActivityRequest  $request
  * @return Response
  */
 public function update($id, EditActivityRequest $request)
 {
     $errors = array();
     $startLocationId = $request->get('start_location');
     $endLocationId = $request->get('end_location');
     $elderlyId = $request->get('senior');
     $startLocation = new Centre();
     $endLocation = new Centre();
     $elderly = new Elderly();
     if ($request->get('start_location') == "others") {
         $postal = $request->get('start_postal');
         $geoInfo = json_decode($this->postalCodeToAddress($postal), true);
         if ($geoInfo['status'] == 'ok') {
             $startLocation->name = ucwords(strtolower($request->get('start_location_name')));
             $startLocation->address = $geoInfo['address'];
             $startLocation->postal_code = $postal;
             $startLocation->lng = $geoInfo['x'];
             $startLocation->lat = $geoInfo['y'];
         } else {
             $errors = array_add($errors, 'start_location', 'Postal code for branch does not exist.');
         }
     }
     if ($request->get('end_location') == "others") {
         if ($request->get('start_location_name') == $request->get('end_location_name')) {
             $endLocation = $startLocation;
         } else {
             $postal = $request->get('end_postal');
             $geoInfo = json_decode($this->postalCodeToAddress($postal), true);
             if ($geoInfo['status'] == 'ok') {
                 $endLocation->name = ucwords(strtolower($request->get('end_location_name')));
                 $endLocation->address = $geoInfo['address'];
                 $endLocation->postal_code = $postal;
                 $endLocation->lng = $geoInfo['x'];
                 $endLocation->lat = $geoInfo['y'];
             } else {
                 $errors = array_add($errors, 'end_location', 'Postal code for appointment venue does not exist.');
             }
         }
     }
     if ($request->get('senior') == "others") {
         $elderly->nric = $request->get('senior_nric');
         $elderly->name = $request->get('senior_name');
         $elderly->gender = $request->get('senior_gender');
         $elderly->birth_year = $request->get('senior_birth_year');
         $elderly->next_of_kin_name = $request->get('senior_nok_name');
         $elderly->next_of_kin_contact = $request->get('senior_nok_contact');
         $elderly->medical_condition = $request->get('senior_medical');
         $elderly->centre_id = $request->get('centre');
         if (count($request->get('languages')) < 1) {
             $errors = array_add($errors, 'languages', 'Language is required.');
         } else {
             foreach ($request->get('languages') as $language) {
                 $v = Validator::make(['language' => $language], ['language' => 'alpha']);
                 if ($v->fails()) {
                     $errors = array_add($errors, 'languages', 'Language must be valid words.');
                 }
             }
         }
     }
     if (count($errors) > 0) {
         return back()->withErrors($errors)->withInput();
     } else {
         if ($request->get('start_location') == "others") {
             $startLocation->save();
             $startLocationId = $startLocation->centre_id;
         }
         if ($request->get('end_location') == "others") {
             $endLocation->save();
             $endLocationId = $endLocation->centre_id;
         }
         if ($request->get('senior') == "others") {
             $elderly->save();
             foreach ($request->get('languages') as $language) {
                 ElderlyLanguage::create(['elderly_id' => $elderly->elderly_id, 'language' => $language]);
             }
             $elderlyId = $elderly->elderly_id;
         }
         $activity = Activity::findOrFail($id);
         $activity->update(['datetime_start' => $request->get('date') . " " . $request->get('time'), 'expected_duration_minutes' => $request->get('duration'), 'category' => 'transport', 'more_information' => $request->get('more_information'), 'location_from_id' => $startLocationId, 'location_to_id' => $endLocationId, 'elderly_id' => $elderlyId, 'centre_id' => $request->get('centre'), 'staff_id' => Auth::user()->staff_id]);
         return redirect()->route('activities.show', compact('activity'))->with('success', 'Activity is updated successfully!');
     }
 }
 /**
  * Retrieves the filters for activity search.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return  JSON  array of filtered search with activity IDs
  */
 public function retrieveFilter(Request $request)
 {
     if ($request->get('filter') == null) {
         $status = ["Missing parameter"];
         return response()->json(compact('status'));
     } else {
         $filter = $request->get('filter');
         if ($filter == 'start') {
             if ($request->get('token') != null) {
                 $authenticatedUser = JWTAuth::setToken($request->get('token'))->authenticate();
                 $id = $authenticatedUser->volunteer_id;
                 $limit = $request->get('limit');
                 $approvedActivities = Activity::with('tasks')->whereHas('tasks', function ($query) {
                     $query->where('approval', 'like', 'approved');
                 })->lists('activity_id');
                 $appliedActivities = Task::Where('volunteer_id', '=', $id)->where(function ($query) {
                     $query->where('approval', '=', 'rejected')->orWhere('approval', '=', 'pending');
                 })->lists('activity_id');
                 $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                 $activityList = Activity::whereNotIn('activity_id', $notApproved)->groupBy('location_from_id')->lists('location_from_id');
                 $toReturn = [];
                 foreach ($activityList as $location) {
                     $locationName = Centre::findOrFail($location)->name;
                     $locationList = Activity::where('datetime_start', '>', Carbon::now())->whereNotIn('activity_id', $approvedActivities)->whereNotIn('activity_id', $appliedActivities)->whereNotIn('activity_id', $notApproved)->where('location_from_id', $location)->distinct()->lists('activity_id');
                     if (!$locationList->isEmpty()) {
                         array_push($toReturn, ["location_from" => $locationName, "activity_ids" => implode(",", $locationList->toArray())]);
                     }
                 }
                 return response()->json($toReturn);
             } else {
                 $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                 $activityList = Activity::whereNotIn('activity_id', $notApproved)->groupBy('location_from_id')->lists('location_from_id');
                 $toReturn = [];
                 foreach ($activityList as $location) {
                     $locationName = Centre::findOrFail($location)->name;
                     $locationList = Activity::where('datetime_start', '>', Carbon::now())->whereNotIn('activity_id', $notApproved)->where('location_from_id', $location)->distinct()->lists('activity_id');
                     if (!$locationList->isEmpty()) {
                         array_push($toReturn, ["location_from" => $locationName, "activity_ids" => implode(",", $locationList->toArray())]);
                     }
                 }
                 return response()->json($toReturn);
             }
         } elseif ($filter == 'end') {
             if ($request->get('token') != null) {
                 $authenticatedUser = JWTAuth::setToken($request->get('token'))->authenticate();
                 $id = $authenticatedUser->volunteer_id;
                 $limit = $request->get('limit');
                 $approvedActivities = Activity::with('tasks')->whereHas('tasks', function ($query) {
                     $query->where('approval', 'like', 'approved');
                 })->lists('activity_id');
                 $appliedActivities = Task::Where('volunteer_id', '=', $id)->where(function ($query) {
                     $query->where('approval', '=', 'rejected')->orWhere('approval', '=', 'pending');
                 })->lists('activity_id');
                 $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                 $notcompleted = Task::where('status', '=', 'completed')->distinct()->lists('activity_id');
                 $activityList = Activity::groupBy('location_to_id')->lists('location_to_id');
                 $toReturn = [];
                 $locationNameString = "";
                 $locationIdString = "";
                 foreach ($activityList as $location) {
                     $locationName = Centre::findOrFail($location)->name;
                     //echo $location;
                     $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                     $locationList = Activity::where('datetime_start', '>', Carbon::now())->whereNotIn('activity_id', $approvedActivities)->whereNotIn('activity_id', $appliedActivities)->where('location_to_id', $location)->whereNotIn('activity_id', $notApproved)->distinct()->lists('activity_id');
                     //$stringToTitle = 'location_to';
                     $stringToList = $locationName . ' ' . $locationList;
                     //$locationNameString = $locationNameString . ',' .  $locationName;
                     //$locationIdString = $locationIdString . ',' . $locationList;
                     //$toReturn = array();
                     if (!$locationList->isEmpty()) {
                         array_push($toReturn, ["location_to" => $locationName, "activity_ids" => implode(",", $locationList->toArray())]);
                     }
                 }
                 return response()->json($toReturn);
             } else {
                 $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                 $notcompleted = Task::where('status', '=', 'completed')->distinct()->lists('activity_id');
                 $activityList = Activity::groupBy('location_to_id')->lists('location_to_id');
                 $toReturn = [];
                 $locationNameString = "";
                 $locationIdString = "";
                 foreach ($activityList as $location) {
                     $locationName = Centre::findOrFail($location)->name;
                     //echo $location;
                     $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                     $locationList = Activity::where('datetime_start', '>', Carbon::now())->where('location_to_id', $location)->whereNotIn('activity_id', $notApproved)->distinct()->lists('activity_id');
                     //$stringToTitle = 'location_to';
                     $stringToList = $locationName . ' ' . $locationList;
                     //$locationNameString = $locationNameString . ',' .  $locationName;
                     //$locationIdString = $locationIdString . ',' . $locationList;
                     //$toReturn = array();
                     if (!$locationList->isEmpty()) {
                         array_push($toReturn, ["location_to" => $locationName, "activity_ids" => implode(",", $locationList->toArray())]);
                     }
                 }
                 return response()->json($toReturn);
             }
         } else {
             if ($request->get('token') != null) {
                 $authenticatedUser = JWTAuth::setToken($request->get('token'))->authenticate();
                 $id = $authenticatedUser->volunteer_id;
                 $limit = $request->get('limit');
                 $approvedActivities = Activity::with('tasks')->whereHas('tasks', function ($query) {
                     $query->where('approval', 'like', 'approved');
                 })->lists('activity_id');
                 $appliedActivities = Task::Where('volunteer_id', '=', $id)->where(function ($query) {
                     $query->where('approval', '=', 'rejected')->orWhere('approval', '=', 'pending');
                 })->lists('activity_id');
                 $activityList = Activity::groupBy('datetime_start')->lists('datetime_start');
                 $toReturn = [];
                 foreach ($activityList as $dateTimeStart) {
                     $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                     $locationList = Activity::where('datetime_start', '>', Carbon::now())->whereNotIn('activity_id', $approvedActivities)->whereNotIn('activity_id', $appliedActivities)->whereNotIn('activity_id', $notApproved)->where('datetime_start', '=', $dateTimeStart)->whereNotIn('activity_id', $notApproved)->distinct()->lists('activity_id');
                     //$toReturn = array();
                     if (!$locationList->isEmpty()) {
                         array_push($toReturn, ["time" => $dateTimeStart, "activity_ids" => implode(",", $locationList->toArray())]);
                     }
                 }
                 return response()->json($toReturn);
             } else {
                 $activityList = Activity::groupBy('datetime_start')->lists('datetime_start');
                 $toReturn = [];
                 foreach ($activityList as $dateTimeStart) {
                     $notApproved = Task::where('approval', '=', 'approved')->distinct()->lists('activity_id');
                     $locationList = Activity::where('datetime_start', '>', Carbon::now())->whereNotIn('activity_id', $notApproved)->where('datetime_start', '=', $dateTimeStart)->whereNotIn('activity_id', $notApproved)->distinct()->lists('activity_id');
                     //$toReturn = array();
                     if (!$locationList->isEmpty()) {
                         array_push($toReturn, ["time" => $dateTimeStart, "activity_ids" => implode(",", $locationList->toArray())]);
                     }
                 }
                 return response()->json($toReturn);
             }
         }
     }
 }
 public function compose(View $view)
 {
     $examList = array('' => 'Select a exam date') + DB::table('sittings')->select(DB::raw('concat (month," / ",year) as sitting,id'))->lists('sitting', 'id');
     $view->with('methodList', \App\Method::all())->with('levelList', \App\Level::all())->with('paperList', \App\Paper::all())->with('centreList', \App\Centre::all())->with('elementList', \App\Element::all())->with('examList', $examList);
 }