Skip to content

Commit

Permalink
Add Research consent data start-end date filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pvgennip committed Nov 17, 2020
1 parent c925527 commit 1e9b9be
Show file tree
Hide file tree
Showing 4 changed files with 981 additions and 661 deletions.
45 changes: 28 additions & 17 deletions app/Http/Controllers/ResearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ public function show($id, Request $request)
// Make dates table
$dates = [];

$moment_start = new Moment($research->start_date);
$moment_end = new Moment($research->end_date);
$moment_now = new Moment();
$moment_2wk = $moment_now->startof('day')->addDays(-14)->format('Y-m-d');
$moment_now = new Moment();
$moment_ytd = $moment_now->endof('day')->addDays(-1)->format('Y-m-d');

$start_date = $request->input('start_date', $moment_2wk);
$end_date = $request->input('end_date', $moment_ytd);

$moment_start = new Moment($start_date);
$moment_end = new Moment($end_date);

if ($moment_now < $moment_end)
$moment_end = $moment_now;
Expand All @@ -149,7 +156,7 @@ public function show($id, Request $request)
->select('users.name','users.id')
->selectRaw('sum(research_user.consent) as consents')
->where('research_user.research_id', $id)
->whereDate('research_user.updated_at', '<', $research->end_date)
->whereDate('research_user.updated_at', '<', $end_date)
->groupBy('research_user.user_id')
->having('consents', '>', 0)
->pluck('name','id')
Expand All @@ -168,7 +175,7 @@ public function show($id, Request $request)
$consents = DB::table('research_user')
->where('research_id', $id)
->whereIn('user_id', $consent_users_selected)
->whereDate('updated_at', '<', $research->end_date)
->whereDate('updated_at', '<', $end_date)
->groupBy('user_id')
->get();

Expand Down Expand Up @@ -206,7 +213,7 @@ public function show($id, Request $request)
->select('users.id','users.name','research_user.updated_at','research_user.consent')
->where('research_user.research_id', $id)
->whereIn('user_id', $consent_users_selected)
->whereDate('research_user.updated_at', '<', $research->end_date)
->whereDate('research_user.updated_at', '<', $end_date)
->get()
->toArray();

Expand Down Expand Up @@ -337,7 +344,7 @@ public function show($id, Request $request)
foreach ($users as $u)
{
$user_id = $u->id;
$user_consents = DB::table('research_user')->where('research_id', $id)->where('user_id', $user_id)->whereDate('updated_at', '<', $research->end_date)->orderBy('updated_at','asc')->get()->toArray();
$user_consents = DB::table('research_user')->where('research_id', $id)->where('user_id', $user_id)->whereDate('updated_at', '<', $end_date)->orderBy('updated_at','asc')->get()->toArray();

$user_consent = $user_consents[0]->consent;
$date_curr_consent = $user_consents[0]->updated_at;
Expand All @@ -356,10 +363,10 @@ public function show($id, Request $request)
//die(print_r([$user_consents, $date_curr_consent, $date_next_consent, $index]));

// add user data
$user_apiaries = Location::where('user_id', $user_id)->where('created_at', '<', $research->end_date)->orderBy('created_at')->get();
$user_hives = Hive::where('user_id', $user_id)->where('created_at', '<', $research->end_date)->orderBy('created_at')->get();
$user_devices = Device::where('user_id', $user_id)->where('created_at', '<', $research->end_date)->orderBy('created_at')->get();
$user_inspections = User::find($user_id)->inspections()->with('items')->where('created_at', '>=', $research->start_date)->where('created_at', '<', $research->end_date)->orderBy('created_at')->get();
$user_apiaries = Location::where('user_id', $user_id)->where('created_at', '<', $end_date)->orderBy('created_at')->get();
$user_hives = Hive::where('user_id', $user_id)->where('created_at', '<', $end_date)->orderBy('created_at')->get();
$user_devices = Device::where('user_id', $user_id)->where('created_at', '<', $end_date)->orderBy('created_at')->get();
$user_inspections = User::find($user_id)->inspections()->with('items')->where('created_at', '>=', $start_date)->where('created_at', '<', $end_date)->orderBy('created_at')->get();
$user_measurements = [];

//die(print_r([$user_apiaries->toArray(), $user_hives->toArray()]));
Expand Down Expand Up @@ -506,10 +513,10 @@ public function show($id, Request $request)
{
//die(print_r([$consents, $spreadsheet_array[__('export.devices')], $spreadsheet_array['Sensor data']]));
$fileName = strtolower(env('APP_NAME')).'-export-'.$research->name;
$download_url = $this->export($spreadsheet_array, $fileName);
$download_url = $this->export($spreadsheet_array, $fileName, $start_date, $end_date);
}

return view('research.show', compact('research', 'dates', 'consent_users_select', 'consent_users_selected', 'download_url', 'sensor_urls', 'totals'));
return view('research.show', compact('research', 'dates', 'consent_users_select', 'consent_users_selected', 'download_url', 'sensor_urls', 'totals', 'start_date', 'end_date'));
}

/**
Expand Down Expand Up @@ -606,7 +613,7 @@ public function destroy($id)

/* Data export functions */

private function export($spreadsheetArray, $fileName='export')
private function export($spreadsheetArray, $fileName='export', $start_date='Research start', $end_date='Research end')
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
Expand All @@ -616,10 +623,14 @@ private function export($spreadsheetArray, $fileName='export')
$sheet->setCellValue('A1', 'Meta data');
$sheet->setCellValue('A3', env('APP_NAME').' data export');
$sheet->setCellValue('C3', date('Y-m-d H:i:s'));
$sheet->setCellValue('A4', 'Sheets');
$sheet->setCellValue('C4', count($spreadsheetArray));

$row = 6;
$sheet->setCellValue('A4', 'Start date');
$sheet->setCellValue('C4', $start_date);
$sheet->setCellValue('A5', 'End date');
$sheet->setCellValue('C5', $end_date);
$sheet->setCellValue('A6', 'Sheets');
$sheet->setCellValue('C6', count($spreadsheetArray));

$row = 8;
foreach ($spreadsheetArray as $title => $data)
{
$sheet->setCellValue('A'.$row, $title);
Expand Down
Loading

0 comments on commit 1e9b9be

Please sign in to comment.