Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
tutors.csv
students.csv
telegram.csv
database/seeders/images/

# deployment
deployOwn/
Expand Down
84 changes: 42 additions & 42 deletions app/Http/Controllers/Api/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,48 +261,48 @@ public function coursesUserAmountPerEvent(Request $request): JsonResponse
*/
public function courseStatistics(Request $request): JsonResponse
{
$event = Event::with(['registrations.user'])->find($request->event);

if (!$event) {
return response()->json(['message' => 'Event not found'], 404);
}

$result = [];
if ($event->consider_alcohol === true) {
$result['drinks_alcohol'] = [
'true' => $event->registrations->where('drinks_alcohol', true)->count(),
'false' => $event->registrations->where('drinks_alcohol', false)->count(),
'name' => 'Drinks Alcohol',
];
}
$event = Event::with(['registrations.user'])->find($request->event);

$formData = $event->registrations->pluck('form_responses')->filter(function ($item) {
return !is_null($item);
});
if (!$event) {
return response()->json(['message' => 'Event not found'], 404);
}

// Count statistics from formData
foreach ($formData as $data) {
if (is_string($data)) {
$data = json_decode($data, true);
$result = [];
if ($event->consider_alcohol === true) {
$result['drinks_alcohol'] = [
'true' => $event->registrations->where('drinks_alcohol', true)->count(),
'false' => $event->registrations->where('drinks_alcohol', false)->count(),
'name' => 'Drinks Alcohol',
];
}
if (is_array($data)) {
foreach ($data as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = [
$value => 1,
'name' => $key,
];
} else {
if (isset($result[$key][$value])) {
$result[$key][$value]++;
} else {
$result[$key][$value] = 1;
}

$formData = $event->registrations->pluck('form_responses')->filter(function ($item) {
return !is_null($item);
});

// Count statistics from formData
foreach ($formData as $data) {
if (is_string($data)) {
$data = json_decode($data, true);
}
if (is_array($data)) {
foreach ($data as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = [
$value => 1,
'name' => $key,
];
} else {
if (isset($result[$key][$value])) {
$result[$key][$value]++;
} else {
$result[$key][$value] = 1;
}
}
}
}
}
}
}
return response()->json($result);
return response()->json($result);
}

/**
Expand Down Expand Up @@ -420,14 +420,14 @@ public function users(): JsonResponse
*/
public function userRegistrations(User $user): JsonResponse
{
$registrations = $user->registrations()->with(['event', 'group'])->get();
return response()->json([
'registrations' => $registrations
]);
$registrations = $user->registrations()->with(['event', 'group'])->get();
return response()->json([
'registrations' => $registrations
]);
}


/**
/**
* Generate a presigned URL for avatar upload
*/
public function generatePresignedUrlForAvatarUpload(Request $request): JsonResponse
Expand Down
64 changes: 63 additions & 1 deletion database/seeders/StudentSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use App\Models\Course;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class StudentSeeder extends Seeder
{
Expand All @@ -13,7 +16,21 @@ class StudentSeeder extends Seeder
*
* @var string
*/
private const STUDENTS_CSV_PATH = __DIR__.'/students.csv';
private const STUDENTS_CSV_PATH = __DIR__ . '/students.csv';

/**
* Set path to the folder with student images.
*
* @var string
*/
private const STUDENT_IMAGES_PATH = __DIR__ . '/images';

/**
* Set possible file extensions for the student images.
*
* @var array
*/
private const STUDENT_IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png'];

/**
* Run the tutor seeds.
Expand Down Expand Up @@ -57,6 +74,51 @@ public function run(): void
$user->course_id = $courseByKey[$student[2]]->id;
$user->email = strtolower($student[3]);

// check if an image with the name of the user in one of the possible extensions exists
$imageBasePath = self::STUDENT_IMAGES_PATH . '/' . strtolower($user->firstname) . '_' . strtolower($user->lastname);
$imageExtension = null;
$imagePath = null;
foreach (self::STUDENT_IMAGE_EXTENSIONS as $extension) {
if (file_exists($imageBasePath . '.' . $extension)) {
$imageExtension = $extension;
$imagePath = $imageBasePath . '.' . $extension;
break;
}
}

if ($imagePath) {
// generate presigned url
$uuid = Str::uuid()->toString();
$path = 'avatars/' . $uuid . '.' . $imageExtension;
$presignedUrl = Storage::disk('s3')->temporaryUploadUrl(
$path,
now()->addMinutes(5)
);

// extract relevant data from presigned url
$uploadUrl = $presignedUrl['url'] ?? null;
$uploadHeaders = $presignedUrl['headers'] ?? [];

// get mime type of image
$mime = mime_content_type($imagePath);

// upload avatar using presigned url
$req = Http::withBody(file_get_contents($imagePath), $mime);
if (! empty($uploadHeaders)) {
$req = $req->withHeaders($uploadHeaders);
}
$response = $req->put($uploadUrl);

if ($response->successful()) {
// save avatar path to user
$user->avatar = $path;

$this->command->info("Found and uploaded avatar for user {$user->firstname} {$user->lastname}");
} else {
$this->command->warn("Failed to upload avatar for user {$user->firstname} {$user->lastname}");
}
}

// save the user
$user->save();
}
Expand Down
72 changes: 67 additions & 5 deletions database/seeders/TutorSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use App\Models\Course;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class TutorSeeder extends Seeder
{
Expand All @@ -13,7 +16,21 @@ class TutorSeeder extends Seeder
*
* @var string
*/
private const TUTORS_CSV_PATH = __DIR__.'/tutors.csv';
private const TUTORS_CSV_PATH = __DIR__ . '/tutors.csv';

/**
* Set path to the folder with tutor images.
*
* @var string
*/
private const TUTOR_IMAGES_PATH = __DIR__ . '/images';

/**
* Set possible file extensions for the tutor images.
*
* @var array
*/
private const TUTOR_IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png'];

/**
* Run the tutor seeds.
Expand Down Expand Up @@ -51,7 +68,7 @@ public function run(): void
}

// create a new user
echo 'Creating tutor '.$tutor[0].' '.$tutor[1].' ('.$tutor[3].')'.PHP_EOL;
echo 'Creating tutor ' . $tutor[0] . ' ' . $tutor[1] . ' (' . $tutor[3] . ')' . PHP_EOL;
$user = new User;
$user->lastname = $tutor[0];
$user->firstname = $tutor[1];
Expand All @@ -60,15 +77,60 @@ public function run(): void

//set user to disabled
if (array_key_exists(5, $tutor) && $tutor[5] == '1') {
echo 'Setting tutor to disabled for '.$tutor[0].' '.$tutor[1].' ('.$tutor[3].')'.PHP_EOL;
echo 'Setting tutor to disabled for ' . $tutor[0] . ' ' . $tutor[1] . ' (' . $tutor[3] . ')' . PHP_EOL;
$user->is_disabled = true;
}

// check if an image with the name of the user in one of the possible extensions exists
$imageBasePath = self::TUTOR_IMAGES_PATH . '/' . strtolower($user->firstname) . '_' . strtolower($user->lastname);
$imageExtension = null;
$imagePath = null;
foreach (self::TUTOR_IMAGE_EXTENSIONS as $extension) {
if (file_exists($imageBasePath . '.' . $extension)) {
$imageExtension = $extension;
$imagePath = $imageBasePath . '.' . $extension;
break;
}
}

if ($imagePath) {
// generate presigned url
$uuid = Str::uuid()->toString();
$path = 'avatars/' . $uuid . '.' . $imageExtension;
$presignedUrl = Storage::disk('s3')->temporaryUploadUrl(
$path,
now()->addMinutes(5)
);

// extract relevant data from presigned url
$uploadUrl = $presignedUrl['url'] ?? null;
$uploadHeaders = $presignedUrl['headers'] ?? [];

// get mime type of image
$mime = mime_content_type($imagePath);

// upload avatar using presigned url
$req = Http::withBody(file_get_contents($imagePath), $mime);
if (! empty($uploadHeaders)) {
$req = $req->withHeaders($uploadHeaders);
}
$response = $req->put($uploadUrl);

if ($response->successful()) {
// save avatar path to user
$user->avatar = $path;

$this->command->info("Found and uploaded avatar for user {$user->firstname} {$user->lastname}");
} else {
$this->command->warn("Failed to upload avatar for user {$user->firstname} {$user->lastname}");
}
}

// save the user
$user->save();

// assigne user role
echo 'Assigning role tutor to '.$tutor[0].' '.$tutor[1].' ('.$tutor[3].')'.PHP_EOL;
echo 'Assigning role tutor to ' . $tutor[0] . ' ' . $tutor[1] . ' (' . $tutor[3] . ')' . PHP_EOL;
$user->assignRole('tutor');

// check if the user has additional roles
Expand All @@ -82,7 +144,7 @@ public function run(): void
if (empty($role)) {
continue;
}
echo 'Assigning role '.$role.' to '.$tutor[0].' '.$tutor[1].' ('.$tutor[3].')'.PHP_EOL;
echo 'Assigning role ' . $role . ' to ' . $tutor[0] . ' ' . $tutor[1] . ' (' . $tutor[3] . ')' . PHP_EOL;
$user->assignRole($role);
}
}
Expand Down
2 changes: 2 additions & 0 deletions database/seeders/students.csv.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lastname;firstname;course;email
Mustermann;Max;INF;max.mustermann@alumni.fh-aachen.de
Loading