Loheide-eu-mitarbeiterplatt.../app/Filament/Pages/AskSettings.php
Friederich Loheide 4d58364104
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.3 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.4 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.4 (pull_request) Has been skipped
CI / Browser-Tests (pull_request) Successful in 14m21s
CI / PHP 8.3 / mysql:8.4-1 (pull_request) Successful in 19m7s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 5m20s
CI / Build-Artefakte aktuell (pull_request) Successful in 5m46s
Frag das Portal als schwebende Blase, Suchliste im Bild (0.65.0)
- AskBubble unten rechts (abschaltbar), Gespräch bleibt beim Seitenwechsel
- Antwortanzeige als gemeinsame Komponente AskAnswer
- Trefferliste der Kopfzeilen-Suche in der App-Oberfläche linksbündig
- CI: optionale Kern-Plugins für Browser-Tests einschalten, Suchtest mit Demo-Inhalt

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 19:02:18 +00:00

112 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Filament\Pages;
use App\Ai\AiAssistant;
use App\Ask\Knowledge;
use App\Models\PortalQuestion;
use App\Support\Audit;
use App\Support\Settings;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\DB;
use UnitEnum;
/**
* „Frag das Portal“: ein- und ausschalten, Auswertung der (namenlosen) Fragen –
* was oft gefragt wird und wozu nichts im Portal steht.
*/
class AskSettings extends Page
{
protected string $view = 'filament.pages.ask-settings';
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedChatBubbleLeftEllipsis;
protected static string|UnitEnum|null $navigationGroup = 'System';
protected static ?int $navigationSort = 7;
protected static ?string $navigationLabel = 'Frag das Portal';
protected static ?string $title = 'Frag das Portal';
protected static ?string $slug = 'frag-das-portal';
/** Auswertungszeitraum in Tagen */
public int $days = 90;
public static function canAccess(): bool
{
return auth()->user()?->can('ask.manage') ?? false;
}
public function toggle(Settings $settings): void
{
$enabled = ! (bool) $settings->get('ask.enabled', false);
$settings->set('ask.enabled', $enabled);
Audit::log('ask.toggled', ['enabled' => $enabled]);
Notification::make()->success()->title($enabled ? '„Frag das Portal“ ist eingeschaltet' : '„Frag das Portal“ ist ausgeschaltet')->send();
}
public function toggleBubble(Settings $settings): void
{
$settings->set('ask.bubble', ! (bool) $settings->get('ask.bubble', true));
}
public function setDays(int $days): void
{
$this->days = in_array($days, [7, 30, 90, 365], true) ? $days : 90;
}
/**
* @return array<string, mixed>
*/
protected function getViewData(): array
{
$ai = app(AiAssistant::class);
$since = now()->subDays($this->days)->toDateString();
$base = fn () => PortalQuestion::query()->where('asked_on', '>=', $since);
$total = $base()->count();
$answered = $base()->where('answered', true)->count();
// Gleiche Fragen zusammenfassen (Groß-/Kleinschreibung und Satzzeichen am Ende egal)
$group = fn (bool $answered) => $base()->where('answered', $answered)
->select(DB::raw('MIN(question) as question'), DB::raw('COUNT(*) as total'), DB::raw('MAX(asked_on) as last_on'))
->groupBy(DB::raw("TRIM(TRAILING '?' FROM LOWER(question))"))
->orderByDesc('total')->orderByDesc('last_on')->limit(25)->get();
return [
'enabled' => (bool) app(Settings::class)->get('ask.enabled', false),
'bubble' => (bool) app(Settings::class)->get('ask.bubble', true),
'aiReady' => $ai->available(),
'provider' => AiAssistant::PROVIDERS[$ai->provider()],
'external' => $ai->isExternal(),
'sources' => array_values(app(Knowledge::class)->labels()),
'total' => $total,
'answeredShare' => $total ? (int) round($answered / $total * 100) : null,
'unanswered' => $group(false),
'frequent' => $group(true),
];
}
protected function getHeaderActions(): array
{
return [
Action::make('clear')
->label('Auswertung leeren')
->icon(Heroicon::OutlinedTrash)
->color('gray')
->requiresConfirmation()
->modalDescription('Alle gespeicherten Fragen werden gelöscht.')
->action(function () {
PortalQuestion::query()->delete();
Audit::log('ask.cleared');
Notification::make()->success()->title('Auswertung geleert')->send();
}),
];
}
}