Fixes the configuration file

This commit is contained in:
2026-03-16 14:34:07 +01:00
parent ede31b15cb
commit 29a94899da
16 changed files with 559 additions and 18 deletions

74
app/Configs/Content.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace App\Configs;
use Illuminate\Support\Arr;
use Laravel\Nova\Fields\Markdown;
/**
* Config Field class for the "content" config group.
* Defines fields, defaults, and Nova fields for content settings.
*/
final class Content
{
public string $title = 'Content';
public string $description = 'Content settings';
public string $key = 'content';
/**
* Returns the field definitions with their type and default value for this config group.
*/
public function getFieldKeys(): array
{
return [
'disclaimer' => [
'type' => 'markdown',
'default' => '',
],
];
}
/**
* Returns the default value for the given field key, or null if the key does not exist.
*/
public function getDefault(string $key): mixed
{
$fields = $this->getFieldKeys();
if (! Arr::has($fields, $key)) {
return null;
}
return Arr::get($fields, "{$key}.default");
}
/**
* Returns Nova field instances for this config group, each wired with
* resolveUsing (read from json_value with default fallback) and
* fillUsing (write back into json_value) callbacks.
*/
public function getNovaFields(): array
{
return [
Markdown::make('Disclaimer', 'disclaimer')
->resolveUsing(function (mixed $value, mixed $resource): string {
$jsonValue = Arr::get((array) $resource->json_value, 'disclaimer');
if ($jsonValue === null || $jsonValue === '') {
return (string) $this->getDefault('disclaimer');
}
return (string) $jsonValue;
})
->fillUsing(function (mixed $request, mixed $model, string $attribute, string $requestAttribute): void {
$current = Arr::wrap((array) $model->json_value);
Arr::set($current, $attribute, $request->{$requestAttribute});
$model->json_value = $current;
}),
];
}
}

View File

@@ -103,15 +103,17 @@ private function calculateAndUpdateScore(Screening $screening, array $answers):
} }
/** /**
* Calculate the total score from the answers. * Calculate the total score from the answers yes = 1, unknown = 0.5, no = 0.
*/ */
private function calculateScore(array $answers): int private function calculateScore(array $answers): float
{ {
$score = 0; $score = 0;
foreach ($answers as $value) { foreach ($answers as $value) {
if ($value === 'yes') { if ($value === 'yes') {
$score++; $score++;
} elseif ($value === 'unknown') {
$score += 0.5;
} }
} }

View File

@@ -25,7 +25,7 @@ public function rules(): array
{ {
return [ return [
'answers' => ['required', 'array', 'size:10'], 'answers' => ['required', 'array', 'size:10'],
'answers.*' => ['required', 'string', 'in:yes,no'], 'answers.*' => ['required', 'string', 'in:yes,unknown,no'],
]; ];
} }
@@ -40,7 +40,7 @@ public function messages(): array
'answers.size' => 'All 10 screening questions must be answered.', 'answers.size' => 'All 10 screening questions must be answered.',
'answers.*.required' => 'Each screening question must have an answer.', 'answers.*.required' => 'Each screening question must have an answer.',
'answers.*.string' => 'Each answer must be a valid text value.', 'answers.*.string' => 'Each answer must be a valid text value.',
'answers.*.in' => 'Each answer must be either "yes" or "no".', 'answers.*.in' => 'Each answer must be "yes", "I don\'t know", or "no".',
]; ];
} }
} }

42
app/Models/Config.php Normal file
View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
final class Config extends Model
{
/** The primary key column for config entries is a string key, not an auto-incrementing integer. */
protected $primaryKey = 'key';
/** Disable auto-incrementing since the primary key is a string. */
public $incrementing = false;
/** The primary key type is a string. */
protected $keyType = 'string';
/** Allow mass assignment on all columns. */
protected $guarded = [];
/**
* Returns the attribute cast definitions for this model,
* ensuring json_value is always hydrated as a PHP array.
*/
protected function casts(): array
{
return [
'json_value' => 'array',
];
}
/**
* Scope to filter config records by their string key identifier.
*/
public function scopeForKey(Builder $query, string $key): Builder
{
return $query->where('key', $key);
}
}

139
app/Nova/ConfigResource.php Normal file
View File

@@ -0,0 +1,139 @@
<?php
declare(strict_types=1);
namespace App\Nova;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
final class ConfigResource extends Resource
{
/**
* The model the resource corresponds to.
*
* @var class-string<\App\Models\Config>
*/
public static string $model = \App\Models\Config::class;
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = ['key'];
/**
* The logical group associated with the resource.
*
* @var string
*/
public static $group = 'Other';
/**
* Get the displayable label of the resource.
*/
public static function label(): string
{
return 'Settings';
}
/**
* Get the displayable singular label of the resource.
*/
public static function singularLabel(): string
{
return 'Setting';
}
/**
* Resolves the display title from the config class title or falls back to the raw key.
*/
public function title(): string
{
return $this->getConfigClass()?->title ?? $this->resource->key;
}
/**
* Get the fields displayed by the resource.
*
* @return array<int, \Laravel\Nova\Fields\Field|\Laravel\Nova\Panel|\Laravel\Nova\ResourceTool|\Illuminate\Http\Resources\MergeValue>
*/
public function fields(NovaRequest $request): array
{
return array_merge(
[
Text::make('Name', 'key')
->resolveUsing(fn ($value) => $this->getConfigClass()?->title ?? $value)
->exceptOnForms(),
Text::make('Description', 'key')
->resolveUsing(fn () => $this->getConfigClass()?->description ?? '')
->exceptOnForms(),
],
array_map(
fn ($field) => $field->hideFromIndex(),
$this->getConfigClass()?->getNovaFields() ?? []
),
[
DateTime::make('Created At')->exceptOnForms()->hideFromIndex()->sortable()->filterable(),
DateTime::make('Updated At')->exceptOnForms()->hideFromIndex()->sortable()->filterable(),
],
);
}
/**
* Get the cards available for the request.
*
* @return array<int, \Laravel\Nova\Card>
*/
public function cards(NovaRequest $request): array
{
return [];
}
/**
* Get the filters available for the resource.
*
* @return array<int, \Laravel\Nova\Filters\Filter>
*/
public function filters(NovaRequest $request): array
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @return array<int, \Laravel\Nova\Lenses\Lens>
*/
public function lenses(NovaRequest $request): array
{
return [];
}
/**
* Get the actions available for the resource.
*
* @return array<int, \Laravel\Nova\Actions\Action>
*/
public function actions(NovaRequest $request): array
{
return [];
}
/**
* Resolves the config class instance for this resource's key via the Config service.
*/
private function getConfigClass(): ?object
{
$key = $this->resource?->key;
if ($key === null) {
return null;
}
return app(\App\Services\Config::class)->getConfigClassForGroup($key);
}
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Policies;
use App\Models\Config;
use App\Models\User;
final class ConfigPolicy
{
/**
* Determine whether the user can view any config records.
* Config records are auto-created by the Config Service, so all users may view the list.
*/
public function viewAny(User $user): bool
{
return true;
}
/**
* Determine whether the user can view a specific config record.
*/
public function view(User $user, Config $config): bool
{
return true;
}
/**
* Determine whether the user can update the config record.
* All users with Nova access may edit config values.
*/
public function update(User $user, Config $config): bool
{
return true;
}
/**
* Determine whether the user can create config records.
* Config records are auto-created by the Config Service manual creation is not permitted.
*/
public function create(User $user): bool
{
return false;
}
/**
* Determine whether the user can delete the config record.
* Config records are managed by the Config Service and must not be manually deleted.
*/
public function delete(User $user, Config $config): bool
{
return false;
}
/**
* Determine whether the user can restore the config record.
*/
public function restore(User $user, Config $config): bool
{
return false;
}
/**
* Determine whether the user can permanently delete the config record.
*/
public function forceDelete(User $user, Config $config): bool
{
return false;
}
}

View File

@@ -16,7 +16,7 @@ final class AppServiceProvider extends ServiceProvider
*/ */
public function register(): void public function register(): void
{ {
// $this->app->singleton(\App\Services\Config::class);
} }
/** /**

View File

@@ -4,6 +4,7 @@
use App\Models\User; use App\Models\User;
use App\Nova\CategoryResource; use App\Nova\CategoryResource;
use App\Nova\ConfigResource;
use App\Nova\Dashboards\Main; use App\Nova\Dashboards\Main;
use App\Nova\LogResource; use App\Nova\LogResource;
use App\Nova\QuestionGroupResource; use App\Nova\QuestionGroupResource;
@@ -46,6 +47,10 @@ public function boot(): void
MenuSection::make('Users', [ MenuSection::make('Users', [
MenuItem::resource(\App\Nova\User::class), MenuItem::resource(\App\Nova\User::class),
])->icon('users')->collapsible(), ])->icon('users')->collapsible(),
MenuSection::make('Settings', [
MenuItem::resource(ConfigResource::class),
])->icon('cog')->collapsible(),
]; ];
}); });
} }

126
app/Services/Config.php Normal file
View File

@@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\Config as ConfigModel;
use Illuminate\Database\QueryException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
final class Config
{
/** Typed public property for the content group's disclaimer field. */
public string $contentDisclaimer = '';
/** Indexed config class instances keyed by their group key string. */
private array $configClasses = [];
/**
* Auto-discovers Config Field classes from app/Configs/, loads or creates
* their database rows, and populates typed public properties with cast values.
*/
public function __construct()
{
$this->discoverConfigClasses();
$this->hydrateProperties();
}
/**
* Scans app/Configs/ for PHP files, instantiates each class,
* and indexes them by their key property.
*/
private function discoverConfigClasses(): void
{
$files = glob(app_path('Configs/*.php')) ?: [];
foreach ($files as $file) {
$className = 'App\\Configs\\'.pathinfo($file, PATHINFO_FILENAME);
$instance = new $className;
$this->configClasses[$instance->key] = $instance;
}
}
/**
* Iterates over all discovered config classes, ensures a DB row exists,
* resolves typed field values, and assigns them to public properties.
*/
private function hydrateProperties(): void
{
try {
$dbRecords = ConfigModel::all()->keyBy('key');
foreach ($this->configClasses as $key => $instance) {
$record = $this->ensureDbRecord($key, $dbRecords);
$jsonValue = Arr::wrap((array) ($record->json_value ?? []));
$this->assignFieldProperties($key, $instance, $jsonValue);
}
} catch (QueryException) {
// Silently skip if configs table does not yet exist during migrations.
}
}
/**
* Ensures a database record exists for the given config key,
* creating one with an empty json_value if it does not.
*/
private function ensureDbRecord(string $key, Collection $dbRecords): ConfigModel
{
if ($dbRecords->has($key)) {
return $dbRecords->get($key);
}
return ConfigModel::firstOrCreate(
['key' => $key],
['json_value' => ''],
);
}
/**
* Resolves each field value from json_value (with default fallback),
* applies type casting, and assigns to the matching typed public property.
*/
private function assignFieldProperties(string $key, object $instance, array $jsonValue): void
{
foreach ($instance->getFieldKeys() as $fieldKey => $definition) {
$type = Arr::get($definition, 'type', 'string');
$rawValue = Arr::get($jsonValue, $fieldKey);
if ($rawValue === null || $rawValue === '') {
$rawValue = $instance->getDefault($fieldKey);
}
$castedValue = $this->castValue($rawValue, $type);
$propertyName = Str::camel($key).Str::studly($fieldKey);
if (property_exists($this, $propertyName)) {
$this->{$propertyName} = $castedValue;
}
}
}
/**
* Casts a raw value to the PHP type defined by the config field type string.
*/
private function castValue(mixed $value, string $type): mixed
{
return match ($type) {
'string', 'markdown' => (string) $value,
'int', 'integer' => (int) $value,
'bool', 'boolean' => (bool) $value,
'float' => (float) $value,
default => $value,
};
}
/**
* Returns the Config Field class instance for a given group key,
* or null when no class has been discovered for that key.
*/
public function getConfigClassForGroup(string $group): ?object
{
return Arr::get($this->configClasses, $group);
}
}

View File

@@ -16,7 +16,7 @@ public function up(): void
Schema::create('screenings', function (Blueprint $table) { Schema::create('screenings', function (Blueprint $table) {
$table->id(); $table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete(); $table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->integer('score')->nullable(); $table->decimal('score', 4, 1)->nullable();
$table->boolean('passed')->nullable(); $table->boolean('passed')->nullable();
$table->timestamps(); $table->timestamps();
}); });

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Creates the configs table used to store database-driven key/value configuration.
*/
public function up(): void
{
Schema::create('configs', function (Blueprint $table) {
$table->string('key')->primary();
$table->json('json_value')->nullable();
$table->timestamps();
});
}
/**
* Drops the configs table on rollback.
*/
public function down(): void
{
Schema::dropIfExists('configs');
}
};

View File

@@ -71,7 +71,7 @@ const handleStartCategory = (categoryId) => {
<div <div
v-for="category in categories" v-for="category in categories"
:key="category.id" :key="category.id"
class="bg-surface/50 rounded-lg p-4 flex items-center justify-between hover:bg-surface/70 transition-colors" class="rounded-lg p-4 flex items-center justify-between hover:bg-white/5 transition-colors"
> >
<span class="text-white font-medium">{{ category.name }}</span> <span class="text-white font-medium">{{ category.name }}</span>
<AppButton size="md" @click="handleStartCategory(category.id)"> <AppButton size="md" @click="handleStartCategory(category.id)">

View File

@@ -1,5 +1,5 @@
<script setup> <script setup>
import { computed } from 'vue' import { computed, watch } from 'vue'
import { Head, useForm } from '@inertiajs/vue3' import { Head, useForm } from '@inertiajs/vue3'
import AppLayout from '@/Layouts/AppLayout.vue' import AppLayout from '@/Layouts/AppLayout.vue'
import AppButton from '@/Components/AppButton.vue' import AppButton from '@/Components/AppButton.vue'
@@ -32,6 +32,18 @@ const handleSubmit = () => {
form.put(`/screening/${props.screening.id}`) form.put(`/screening/${props.screening.id}`)
} }
const currentScore = computed(() => {
return Object.values(form.answers).reduce((score, value) => {
if (value === 'yes') return score + 1
if (value === 'unknown') return score + 0.5
return score
}, 0)
})
watch(currentScore, (score) => {
console.log('Current screening score:', score)
})
const allAnswered = computed(() => { const allAnswered = computed(() => {
return Object.values(form.answers).every(v => v !== null) return Object.values(form.answers).every(v => v !== null)
}) })
@@ -42,13 +54,13 @@ const allAnswered = computed(() => {
<div class="max-w-4xl mx-auto px-4 py-8"> <div class="max-w-4xl mx-auto px-4 py-8">
<h1 class="text-3xl font-bold text-white mb-2">Pre-Screening Questions</h1> <h1 class="text-3xl font-bold text-white mb-2">Pre-Screening Questions</h1>
<p class="text-gray-400 mb-8">Answer all 10 questions to proceed. Each "Yes" answer scores 1 point. You need at least 5 points to pass.</p> <p class="text-gray-400 mb-8">Answer all 10 questions to proceed. Each "Yes" scores 1 point, "I don't know" scores half a point. You need at least 5 points to pass.</p>
<div class="space-y-4 mb-8"> <div class="space-y-4 mb-8">
<div <div
v-for="(question, index) in questions" v-for="(question, index) in questions"
:key="index" :key="index"
class="bg-surface/50 rounded-lg p-5" class="rounded-lg p-5"
:data-cy="`screening-answer-${index + 1}`" :data-cy="`screening-answer-${index + 1}`"
> >
<div class="flex items-start gap-4"> <div class="flex items-start gap-4">
@@ -60,6 +72,7 @@ const allAnswered = computed(() => {
:name="`question-${index + 1}`" :name="`question-${index + 1}`"
:options="[ :options="[
{ value: 'yes', label: 'Yes' }, { value: 'yes', label: 'Yes' },
{ value: 'unknown', label: 'I don\'t know' },
{ value: 'no', label: 'No' }, { value: 'no', label: 'No' },
]" ]"
/> />

View File

@@ -85,7 +85,7 @@ const resultDisplay = computed(() => {
</div> </div>
<!-- Session Details --> <!-- Session Details -->
<div class="bg-surface/50 rounded-lg p-6 mb-8"> <div class="rounded-lg p-6 mb-8">
<h2 class="text-xl font-semibold text-white mb-4">Session Details</h2> <h2 class="text-xl font-semibold text-white mb-4">Session Details</h2>
<dl class="grid grid-cols-2 gap-4 text-sm"> <dl class="grid grid-cols-2 gap-4 text-sm">
<div> <div>

View File

@@ -1,8 +1,9 @@
<script setup> <script setup>
import { computed, reactive, ref, watch, nextTick } from 'vue' import { computed, onMounted, onUnmounted, reactive, ref, watch, nextTick } from 'vue'
import { Head, useForm, router } from '@inertiajs/vue3' import { Head, useForm, router } from '@inertiajs/vue3'
import AppLayout from '@/Layouts/AppLayout.vue' import AppLayout from '@/Layouts/AppLayout.vue'
import AppButton from '@/Components/AppButton.vue' import AppButton from '@/Components/AppButton.vue'
import { ArrowLeftIcon } from '@heroicons/vue/24/outline'
import QuestionCard from '@/Components/QuestionCard.vue' import QuestionCard from '@/Components/QuestionCard.vue'
defineOptions({ layout: AppLayout }) defineOptions({ layout: AppLayout })
@@ -39,6 +40,24 @@ const initializeAnswers = () => {
} }
initializeAnswers() initializeAnswers()
const bottomBackButtonRef = ref(null)
const showStickyBack = ref(false)
let observer = null
onMounted(() => {
if (!bottomBackButtonRef.value) return
observer = new IntersectionObserver(
([entry]) => { showStickyBack.value = !entry.isIntersecting },
{ threshold: 0 }
)
observer.observe(bottomBackButtonRef.value.$el || bottomBackButtonRef.value)
})
onUnmounted(() => {
observer?.disconnect()
})
// Validation state // Validation state
const validationErrors = ref({}) const validationErrors = ref({})
const processing = ref(false) const processing = ref(false)
@@ -175,6 +194,27 @@ const completeSession = async () => {
<Head :title="`${session.category.name} Questionnaire`" /> <Head :title="`${session.category.name} Questionnaire`" />
<div class="max-w-3xl mx-auto px-4 py-10"> <div class="max-w-3xl mx-auto px-4 py-10">
<Transition
enter-active-class="transition-all duration-200 ease-out"
enter-from-class="opacity-0 translate-x-4"
enter-to-class="opacity-100 translate-x-0"
leave-active-class="transition-all duration-150 ease-in"
leave-from-class="opacity-100 translate-x-0"
leave-to-class="opacity-0 translate-x-4"
>
<div v-if="showStickyBack" class="fixed top-[88px] right-6 z-40">
<AppButton
variant="ghost"
size="lg"
:href="`/screening/${session.screening_id}/result`"
data-cy="sticky-back"
>
<ArrowLeftIcon class="h-5 w-5" />
Back
</AppButton>
</div>
</Transition>
<!-- Title area --> <!-- Title area -->
<div class="mb-10"> <div class="mb-10">
<h1 class="text-2xl font-bold text-white">{{ session.category.name }} Questionnaire</h1> <h1 class="text-2xl font-bold text-white">{{ session.category.name }} Questionnaire</h1>
@@ -183,7 +223,7 @@ const completeSession = async () => {
<div class="space-y-8"> <div class="space-y-8">
<!-- User Info Section --> <!-- User Info Section -->
<div class="bg-white/[0.03] border border-white/[0.06] rounded-xl p-8"> <div class="border border-white/[0.06] rounded-xl p-8">
<h2 class="text-lg font-semibold text-white mb-5">Basic Information</h2> <h2 class="text-lg font-semibold text-white mb-5">Basic Information</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4"> <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
@@ -202,7 +242,7 @@ const completeSession = async () => {
<div <div
v-for="group in questionGroups" v-for="group in questionGroups"
:key="group.id" :key="group.id"
class="bg-white/[0.03] border border-white/[0.06] rounded-xl p-8" class="border border-white/[0.06] rounded-xl p-8"
> >
<div class="flex items-center gap-3 mb-1"> <div class="flex items-center gap-3 mb-1">
<div class="w-2 h-2 rounded-full bg-primary/60"></div> <div class="w-2 h-2 rounded-full bg-primary/60"></div>
@@ -229,7 +269,7 @@ const completeSession = async () => {
</div> </div>
<!-- Additional Comments --> <!-- Additional Comments -->
<div class="bg-white/[0.03] border border-white/[0.06] rounded-xl p-8"> <div class="border border-white/[0.06] rounded-xl p-8">
<h2 class="text-lg font-semibold text-white mb-5">Additional Comments</h2> <h2 class="text-lg font-semibold text-white mb-5">Additional Comments</h2>
<textarea <textarea
v-model="additionalComments.additional_comments" v-model="additionalComments.additional_comments"
@@ -260,7 +300,7 @@ const completeSession = async () => {
</Transition> </Transition>
<div class="flex justify-between items-center"> <div class="flex justify-between items-center">
<AppButton variant="ghost" size="lg" :href="`/screening/${session.screening_id}/result`" data-cy="back-to-screening"> <AppButton ref="bottomBackButtonRef" variant="ghost" size="lg" :href="`/screening/${session.screening_id}/result`" data-cy="back-to-screening">
Back Back
</AppButton> </AppButton>

View File

@@ -17,7 +17,7 @@
Route::post('/logout', [SocialiteController::class, 'logout'])->name('logout')->middleware('auth'); Route::post('/logout', [SocialiteController::class, 'logout'])->name('logout')->middleware('auth');
// Questionnaire routes (authenticated) // Questionnaire routes (authenticated)
Route::middleware('auth')->group(function () { Route::middleware('auth')->missing(fn () => redirect('/'))->group(function () {
// Screening routes // Screening routes
Route::post('/screening', [ScreeningController::class, 'store'])->name('screening.store'); Route::post('/screening', [ScreeningController::class, 'store'])->name('screening.store');
Route::get('/screening/{screening}', [ScreeningController::class, 'show'])->name('screening.show'); Route::get('/screening/{screening}', [ScreeningController::class, 'show'])->name('screening.show');
@@ -42,4 +42,3 @@
return redirect('/'); return redirect('/');
}); });