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
29 changes: 25 additions & 4 deletions app/Console/Commands/TrainingResourcesReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\ResourceItem;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;

/**
* Training resources report for https://codeweek.eu/training and related Learn & Teach resources.
Expand Down Expand Up @@ -54,6 +55,10 @@ public function handle(): int
}

if ($onlineCoursesOnly) {
if (! Schema::hasTable('online_courses')) {
$this->error('The online_courses table does not exist. Run: php artisan migrate');
return self::FAILURE;
}
$stats = $this->onlineCoursesStats($baseline);
if ($format === 'json') {
$this->line(json_encode([
Expand All @@ -74,7 +79,7 @@ public function handle(): int
'report_period_downloads' => 'June/Sept 2024 – Jan 2026 (requested). Download data not collected by application.',
'training_page' => $this->trainingPageStats(),
'learn_teach_resources' => $this->learnTeachResourcesStats($baseline),
'online_courses' => $this->onlineCoursesStats($baseline),
'online_courses' => $this->onlineCoursesStats($baseline, true),
'downloads' => $this->downloadsSection(),
'geographical_distribution' => $this->geographySection(),
'downloads_over_time' => $this->downloadsOverTimeSection(),
Expand Down Expand Up @@ -117,8 +122,22 @@ private function learnTeachResourcesStats(Carbon $baseline): array
];
}

private function onlineCoursesStats(Carbon $baseline): array
/** @param bool $allowMissingTable When true (e.g. full report), return a placeholder instead of throwing. */
private function onlineCoursesStats(Carbon $baseline, bool $allowMissingTable = false): array
{
if (! Schema::hasTable('online_courses')) {
if ($allowMissingTable) {
return [
'total_now' => null,
'added_since_baseline' => null,
'baseline_date' => $baseline->format('Y-m-d'),
'url' => 'https://codeweek.eu/online-courses',
'note' => 'Online courses table not found. Run: php artisan migrate',
];
}
throw new \RuntimeException('The online_courses table does not exist. Run: php artisan migrate');
}

$total = OnlineCourse::query()->where('active', true)->count();
$addedSince = OnlineCourse::query()
->where('active', true)
Expand Down Expand Up @@ -196,8 +215,10 @@ private function printTextReport(array $report, Carbon $baseline): void
$this->line('');

$this->line('--- Online Courses (https://codeweek.eu/online-courses) ---');
$this->line('Total online courses now: ' . $report['online_courses']['total_now']);
$this->line('Added since baseline (' . $baseline->format('Y-m-d') . '): ' . $report['online_courses']['added_since_baseline']);
if (array_key_exists('total_now', $report['online_courses']) && $report['online_courses']['total_now'] !== null) {
$this->line('Total online courses now: ' . $report['online_courses']['total_now']);
$this->line('Added since baseline (' . $baseline->format('Y-m-d') . '): ' . $report['online_courses']['added_since_baseline']);
}
$this->line($report['online_courses']['note']);
$this->line('');

Expand Down
7 changes: 6 additions & 1 deletion app/Nova/OnlineCourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static function singularLabel()
return 'Online Course';
}

public static function authorizedToViewAny(Request $request): bool
{
return true;
}

public function fields(Request $request): array
{
return [
Expand All @@ -50,6 +55,6 @@ public function fields(Request $request): array

public static function indexQuery(NovaRequest $request, $query)
{
return $query->ordered();
return $query->orderBy('position')->orderBy('created_at', 'desc');
}
}
Loading