Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/Http/Livewire/ProfileStudents.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ProfileStudents extends Component
public $travel_other_filter = '';

public $tag_filter = '';

public $filing_status = '';

protected $listeners = [
'profileStudentStatusUpdated' => 'refreshStudents'
Expand Down Expand Up @@ -70,7 +72,7 @@ public function getStudentsProperty()
->willTravelOther($this->travel_other_filter)
->willWorkWithAnimals($this->animals_filter)
->needsResearchCredit($this->credit_filter)
->with('user:id,email')
->with('user:id,email', 'research_profile', 'tags')
->orderBy('last_name')
->get();
}
Expand All @@ -85,6 +87,19 @@ public function refreshStudents()
$this->students = $this->getStudentsProperty();
}

public function exportToCsv()
{
$students = $this->students->where('application.status', $this->filing_status);

if ($students->isEmpty()) {
$this->emit('alert', "No records available for the filters applied", 'danger');
}
else {
$student_apps = Student::exportStudentApps($students);
return $student_apps->toCsv('students_apps.csv');
}
}

public function render()
{
return view('livewire.profile-students', [
Expand Down
44 changes: 44 additions & 0 deletions app/Macros/CollectionMacros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace App\Macros;

use Illuminate\Support\Collection;

class CollectionMacros
{
public static function register()
{
/**
* Adds a macro to export the collection to a streamed CSV response.
*
* @param string|null $name The name of the CSV file to download. Defaults to 'export.csv'.
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
Collection::macro('toCsv', function ($name = null) {

/** @var \Illuminate\Support\Collection $this */
$results = $this;

return response()->streamDownload(function () use ($results) {
if ($results->isEmpty()) return;

$titles = implode(',', array_keys((array) $results->first()->getAttributes()));

$values = $results->map(function ($result) {
return collect($result->getAttributes())->map(function ($value) {
if (is_null($value)) return '""';

$value = (string) $value;
$value = str_replace(["\r", "\n", "\t"], ' ', $value);
$value = preg_replace('/\s+/', ' ', $value);
$value = str_replace('"', '""', $value);

return "\"{$value}\"";
})->implode(',');
});

$values->prepend($titles);
echo $values->implode("\n");
}, $name ?? 'export.csv');
});
}
}
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Macros\CollectionMacros;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Pagination\Paginator;
Expand Down Expand Up @@ -46,6 +47,7 @@ public function boot()
view()->share('settings', $settings);
});

CollectionMacros::register();
}

/**
Expand Down
78 changes: 78 additions & 0 deletions app/Student.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,84 @@ public function updateAcceptedStats(Profile $profile, $accepted = true)
}
}

/**
* Transforms a collection of students with their associated 'user' and 'research_profile'
* data into a flat collection suitable for CSV export.
*
* @param \Illuminate\Database\Eloquent\Collection $students
* @return \Illuminate\Support\Collection
*
*/
public static function exportStudentApps(EloquentCollection $students)
{
$apps = $students->map(function ($student) {

$st = clone $student;

$st->email = $st->user->email;
$st->link = "https://profiles.utdallas.edu/students/{$st->slug}";
$st->brief_intro = $st->research_profile->brief_intro;
$st->intro = $st->research_profile->intro;
$st->interest = $st->research_profile->interest;
$st->major = $st->research_profile->major;
$st->schools = $st->research_profile->schools;
$st->languages = $st->research_profile->languages;
$st->languages_other = $st->research_profile->language_other_name;
$st->languages_proficiency = $st->research_profile->lang_proficiency;
$st->semesters = $st->research_profile->semesters;
$st->availability = $st->research_profile->availability;
$st->earn_credit = $st->research_profile->credit;
$st->graduation_date = $st->research_profile->graduation_date;
$st->bbs_travel_centers = $st->research_profile->travel;
$st->bbs_travel_other = $st->research_profile->travel_other;
$st->bbs_comfortable_animals = $st->research_profile->animals;
$st->other_info = $st->research_profile->other_info;

$st->earn_credit = match ($st->earn_credit) {
'1' => 'credit',
'0' => 'volunteer',
'-1' => 'no preference',
default => $st->earn_credit,
};

foreach ([
'bbs_travel_centers',
'bbs_travel_other',
'bbs_comfortable_animals',
] as $yes_no_field) {
$st->$yes_no_field = match ($st->$yes_no_field) {
'1' => 'yes',
'0' => 'no',
default => $st->$yes_no_field,
};
}

$st->topics = $st->tags->pluck('name')->implode(", ");

foreach($st->getAttributes() as $attr => $value) {
if (is_array($value)) {
if (array_is_list($value)) {
$st->$attr = implode(", ", $value);
} else {
$json = json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
$json = str_replace([',', ':'], [', ', ': '], $json);
$json = str_replace(["\r", "\n"], ' ', $json);
$st->$attr = $json;
}
}
if ($value === null) {
$st->$attr = '';
}
}

unset($st->id, $st->user_id, $st->type, $st->application, $st->tags, $st->research_profile, $st->user);

return $st;
});

return $apps;
}

/**
* Increments the Student Application view count
*
Expand Down
13 changes: 13 additions & 0 deletions resources/views/livewire/profile-students.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<div class="row">
<div class="col-12 d-flex justify-content-end mr-2 mb-2">
<button
class="btn btn-primary btn-sm"
wire:click="exportToCsv"
type="button"
aria-label="Download student data as CSV"
title="Download as CSV"
>
<i class="fas fa-download"></i>
CSV
</button>
</div>
<div class="col-md-3 mb-md-0 mb-3">
<div
class="nav flex-column nav-pills h-100 border-right bg-light"
Expand All @@ -18,6 +30,7 @@ class="nav-link @if($loop->first) active @endif"
aria-controls="tab_{{ Str::slug($status) }}"
aria-selected="{{ $loop->first ? 'true' : 'false' }}"
wire:ignore.self
wire:click="$set('filing_status', @js($status))"
>
<span class="fa-fw mr-2 {{ $status_icons[$status] }}" style="opacity:0.3"></span>
{{ $status_name }}
Expand Down
Loading