From e4647381d2dd8013a3f040ac3ca51c92408abfd4 Mon Sep 17 00:00:00 2001 From: Betsy Castro <5490820+betsyecastro@users.noreply.github.com> Date: Tue, 8 Apr 2025 10:44:42 -0500 Subject: [PATCH 01/25] Exports student applications to CSV --- app/Http/Livewire/ProfileStudents.php | 15 +++++++++--- app/Providers/AppServiceProvider.php | 23 +++++++++++++++++++ .../views/livewire/profile-students.blade.php | 3 +++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/Http/Livewire/ProfileStudents.php b/app/Http/Livewire/ProfileStudents.php index 926d176f..b5893288 100644 --- a/app/Http/Livewire/ProfileStudents.php +++ b/app/Http/Livewire/ProfileStudents.php @@ -56,7 +56,7 @@ class ProfileStudents extends Component 'semester_filter' => ['except' => '', 'as' => 'semester'], ]; - public function getStudentsProperty() + public function getStudentsBuilderProperty() { return $this->profile->students() ->submitted() @@ -71,8 +71,12 @@ public function getStudentsProperty() ->willWorkWithAnimals($this->animals_filter) ->needsResearchCredit($this->credit_filter) ->with('user:id,email') - ->orderBy('last_name') - ->get(); + ->orderBy('last_name'); + } + + public function getStudentsProperty() + { + return $this->getStudentsBuilderProperty()->get(); } public function updated($name, $value) @@ -85,6 +89,11 @@ public function refreshStudents() $this->students = $this->getStudentsProperty(); } + public function export() + { + return $this->getStudentsBuilderProperty()->toCsv(); + } + public function render() { return view('livewire.profile-students', [ diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 899a7bd0..7420c002 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -9,6 +9,7 @@ use App\Setting; use Collective\Html\FormFacade as Form; use Illuminate\Support\Facades\Cache; +use Illuminate\Database\Eloquent\Builder; class AppServiceProvider extends ServiceProvider { @@ -46,6 +47,28 @@ public function boot() view()->share('settings', $settings); }); + Builder::macro('toCsv', function ($name = null) { + $query = $this; + + return response()->streamDownload(function () use ($query) { + $results = $query->get(); + + if ($results->count() < 1) return; + + $titles = implode(',', array_keys((array) $results->first()->getAttributes())); + + $values = $results->map(function ($result) { + return implode(',', collect($result->getAttributes())->map(function ($thing) { + return '"'.$thing.'"'; + })->toArray()); + }); + + $values->prepend($titles); + + echo $values->implode("\n"); + }, $name ?? 'export.csv'); + }); + } /** diff --git a/resources/views/livewire/profile-students.blade.php b/resources/views/livewire/profile-students.blade.php index 5c066885..2e272989 100644 --- a/resources/views/livewire/profile-students.blade.php +++ b/resources/views/livewire/profile-students.blade.php @@ -1,4 +1,7 @@
+
+ +