Skip to content

Add activity #2702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion app/Console/Commands/CertificatesIssues.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle(): int
{

$issues = Participation::whereNull('participation_url')
->where('status', '=', 'PENDING')
->where('status', 'PENDING')
->where('created_at', '<', Carbon::now()->subMinutes(5))->get();

Log::info('certificate with issues: '.count($issues));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CertificatesOfParticipationGeneration extends Command
public function handle(): int
{

$participations = Participation::whereNull('participation_url')->where('status', '=', 'PENDING')->orderByDesc('created_at')->get();
$participations = Participation::whereNull('participation_url')->where('status', 'PENDING')->orderByDesc('created_at')->get();

$this->info(count($participations).' certificates of participation to generate');

Expand Down
73 changes: 73 additions & 0 deletions app/Console/Commands/EventsIndexOptimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class EventsIndexOptimize extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'events:index-optimize {--rollback : Drop the indexes instead of creating them}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add or drop recommended indexes on the events table for performance optimization';

/**
* Execute the console command.
*/
public function handle()
{
$rollback = $this->option('rollback');

$this->info(($rollback ? 'Dropping' : 'Adding') . ' indexes on events table...');

$indexes = [
'idx_status' => 'CREATE INDEX idx_status ON events(status)',
'idx_country_iso' => 'CREATE INDEX idx_country_iso ON events(country_iso)',
'idx_status_country' => 'CREATE INDEX idx_status_country ON events(status, country_iso)',
'idx_start_date' => 'CREATE INDEX idx_start_date ON events(start_date)',
'idx_end_date' => 'CREATE INDEX idx_end_date ON events(end_date)',
'idx_activity_type' => 'CREATE INDEX idx_activity_type ON events(activity_type)',
'idx_highlighted_status' => 'CREATE INDEX idx_highlighted_status ON events(highlighted_status)',
'idx_lat_lon' => 'CREATE INDEX idx_lat_lon ON events(latitude, longitude)',
'idx_creator_id' => 'CREATE INDEX idx_creator_id ON events(creator_id)',
'idx_user_email' => 'CREATE INDEX idx_user_email ON events(user_email)',
'idx_status_start' => 'CREATE INDEX idx_status_start ON events(status, start_date)',
];

foreach ($indexes as $name => $sql) {
try {
if ($rollback) {
DB::statement("DROP INDEX {$name} ON events");
$this->info("Dropped index: {$name}");
} else {
$exists = DB::select(
"SELECT COUNT(1) as count FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = 'events' AND index_name = ?",
[$name]
);

if (!empty($exists) && $exists[0]->count == 0) {
DB::statement($sql);
$this->info("Created index: {$name}");
} else {
$this->line("Index already exists: {$name}");
}
}
} catch (\Throwable $e) {
$this->error("Failed to " . ($rollback ? 'drop' : 'create') . " index {$name}: " . $e->getMessage());
}
}

$this->info('Index operation completed.');
return 0;
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/Excellence.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function handle(): void
//Select the winners from the Database
$winners = [];
foreach ($codeweek4all_codes as $codeweek4all_code) {
$creators = Event::whereYear('end_date', '=', $edition)->where('status', '=', 'APPROVED')->where('codeweek_for_all_participation_code', '=', $codeweek4all_code)->pluck('creator_id');
$creators = Event::whereYear('end_date', '=', $edition)->where('status', 'APPROVED')->where('codeweek_for_all_participation_code', '=', $codeweek4all_code)->pluck('creator_id');
foreach ($creators as $creator) {
if (! in_array($creator, $winners)) {
$winners[] = $creator;
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/LocationExtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function handle(): void
Event::whereNull('deleted_at')->
// where('id','=',163373)->
// where('creator_id',153701)->
where('status', '=', 'APPROVED')->
where('status', 'APPROVED')->
whereNull('location_id')->chunkById($this->step, function ($events, $index) {

$this->reportProgress($index);
Expand Down
83 changes: 83 additions & 0 deletions app/Console/Commands/SyncEventAgesFromAudience.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Console\Commands;

use App\Event;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SyncEventAgesFromAudience extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'events:sync-ages-from-audience';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Backfill the `ages` field in events based on audience_event using AGE_AUDIENCE_MAP';

/**
* Mapping from Event::AGES keys to audience IDs (from database seeder)
* This allows automatic linking between selected age ranges and appropriate audiences.
* Used for syncing Event.ages with Event.audiences via many-to-many relation.
*/
public const AGE_AUDIENCE_MAP = [
'under-5' => [1], // Pre-school children
'6-9' => [2], // Elementary school students
'10-12' => [2], // Upper primary → still Elementary
'13-15' => [3], // Lower secondary → High school
'16-18' => [3], // Upper secondary → High school
'19-25' => [4, 5], // Graduate & Post graduate students
'over-25' => [6, 7, 9], // Employed + Unemployed adults + Teachers
];

/**
* Execute the console command.
*/
public function handle()
{
$this->info('Syncing event.ages from audience_event...');

$map = self::AGE_AUDIENCE_MAP;

$audienceToAges = [];
foreach ($map as $ageKey => $audienceIds) {
foreach ($audienceIds as $audienceId) {
$audienceToAges[$audienceId][] = $ageKey;
}
}

$links = DB::table('audience_event')
->select('event_id', 'audience_id')
->get()
->groupBy('event_id');

$updated = 0;

foreach ($links as $eventId => $group) {
$ageKeys = collect($group)
->flatMap(function ($row) use ($audienceToAges) {
return $audienceToAges[$row->audience_id] ?? [];
})
->unique()
->values()
->all();

if (!empty($ageKeys)) {
Event::where('id', $eventId)->update([
'ages' => json_encode($ageKeys),
]);
$updated++;
}
}

$this->info("Updated `ages` field for $updated events.");
return 0;
}
}
Loading