diff --git a/app/Console/Commands/GiveRole.php b/app/Console/Commands/GiveRole.php new file mode 100644 index 0000000..a6c9cc0 --- /dev/null +++ b/app/Console/Commands/GiveRole.php @@ -0,0 +1,43 @@ +ask('Enter the email of the user'); + $role = $this->ask('Enter the role to give to the user'); + $user = \App\Models\User::where('email', $email)->first(); + if ($user) { + $user->assignRole($role); + $this->info('Role given successfully!'); + } else { + $this->error('User not found!'); + } + $loop = $this->confirm('Do you want to give another role to a user?'); + } + $this->info('Bye!'); + } +} diff --git a/app/Console/Commands/SeedRoles.php b/app/Console/Commands/SeedRoles.php new file mode 100644 index 0000000..d18ca23 --- /dev/null +++ b/app/Console/Commands/SeedRoles.php @@ -0,0 +1,40 @@ +forgetCachedPermissions(); + Role::create(['name' => 'admin']); + $user = Role::create(['name' => 'user']); + + $travelRoutes = Permission::create(['name' => 'travel routes']); + $travelRoutes->assignRole($user); + + $this->info('Roles and permissions seeded successfully!'); + } +} diff --git a/app/Models/Answer.php b/app/Models/Answer.php new file mode 100644 index 0000000..6bd3e1b --- /dev/null +++ b/app/Models/Answer.php @@ -0,0 +1,27 @@ +belongsTo(Question::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } +} diff --git a/app/Models/Checkpoint.php b/app/Models/Checkpoint.php new file mode 100644 index 0000000..97a4f55 --- /dev/null +++ b/app/Models/Checkpoint.php @@ -0,0 +1,36 @@ +belongsTo(Route::class); + } + + public function question() + { + return $this->hasMany(Question::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } +} diff --git a/app/Models/Media.php b/app/Models/Media.php new file mode 100644 index 0000000..52d27f9 --- /dev/null +++ b/app/Models/Media.php @@ -0,0 +1,26 @@ +morphTo(); + } +} diff --git a/app/Models/Progress.php b/app/Models/Progress.php new file mode 100644 index 0000000..05abc96 --- /dev/null +++ b/app/Models/Progress.php @@ -0,0 +1,37 @@ +belongsTo(User::class); + } + + public function route() + { + return $this->belongsTo(Route::class); + } + + public function checkpoint() + { + return $this->has(Checkpoint::class); + } +} diff --git a/app/Models/Question.php b/app/Models/Question.php new file mode 100644 index 0000000..8c3c985 --- /dev/null +++ b/app/Models/Question.php @@ -0,0 +1,33 @@ +belongsTo(Checkpoint::class); + } + + public function answers() + { + return $this->hasMany(Answer::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } +} diff --git a/app/Models/Route.php b/app/Models/Route.php new file mode 100644 index 0000000..f0be1b8 --- /dev/null +++ b/app/Models/Route.php @@ -0,0 +1,43 @@ +hasMany(Checkpoint::class); + } + + protected $fillable = [ + 'name', + 'description', + 'difficulty', + 'length', + 'duration', + 'user_id', + 'available_at', + 'unavailable_at', + 'is_public' + ]; + + public function user() + { + return $this->belongsTo(User::class); + } + + public function progress() + { + return $this->hasMany(Progress::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 14709f5..3283300 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -10,6 +10,7 @@ use Laravel\Jetstream\HasProfilePhoto; use Laravel\Jetstream\HasTeams; use Laravel\Sanctum\HasApiTokens; +use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { @@ -19,6 +20,7 @@ class User extends Authenticatable use HasTeams; use Notifiable; use TwoFactorAuthenticatable; + use HasRoles; /** * The attributes that are mass assignable. @@ -64,4 +66,13 @@ protected function casts(): array 'password' => 'hashed', ]; } + + public function routes() + { + return $this->hasMany(Route::class); + } + + public function progress() { + return $this->hasMany(Progress::class); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..e5c57f7 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -19,6 +20,8 @@ public function register(): void */ public function boot(): void { - // + Gate::before(function ($user, $ability) { + return $user->hasRole('admin') ? true : null; + }); } } diff --git a/bootstrap/providers.php b/bootstrap/providers.php index 55e8e15..35e69f9 100644 --- a/bootstrap/providers.php +++ b/bootstrap/providers.php @@ -4,4 +4,5 @@ App\Providers\AppServiceProvider::class, App\Providers\FortifyServiceProvider::class, App\Providers\JetstreamServiceProvider::class, + Spatie\Permission\PermissionServiceProvider::class, ]; diff --git a/config/fortify.php b/config/fortify.php index 0551d1d..0be801d 100644 --- a/config/fortify.php +++ b/config/fortify.php @@ -77,7 +77,7 @@ /* |-------------------------------------------------------------------------- - | Fortify Routes Prefix / Subdomain + | Fortify Route Prefix / Subdomain |-------------------------------------------------------------------------- | | Here you may specify which prefix Fortify will assign to all the routes @@ -92,7 +92,7 @@ /* |-------------------------------------------------------------------------- - | Fortify Routes Middleware + | Fortify Route Middleware |-------------------------------------------------------------------------- | | Here you may specify which middleware Fortify will assign to the routes @@ -121,7 +121,7 @@ /* |-------------------------------------------------------------------------- - | Register View Routes + | Register View Route |-------------------------------------------------------------------------- | | Here you may specify if the routes returning views should be disabled as diff --git a/config/permission.php b/config/permission.php new file mode 100644 index 0000000..2a520f3 --- /dev/null +++ b/config/permission.php @@ -0,0 +1,186 @@ + [ + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * Eloquent model should be used to retrieve your permissions. Of course, it + * is often just the "Permission" model but you may use whatever you like. + * + * The model you want to use as a Permission model needs to implement the + * `Spatie\Permission\Contracts\Permission` contract. + */ + + 'permission' => Spatie\Permission\Models\Permission::class, + + /* + * When using the "HasRoles" trait from this package, we need to know which + * Eloquent model should be used to retrieve your roles. Of course, it + * is often just the "Role" model but you may use whatever you like. + * + * The model you want to use as a Role model needs to implement the + * `Spatie\Permission\Contracts\Role` contract. + */ + + 'role' => Spatie\Permission\Models\Role::class, + + ], + + 'table_names' => [ + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'roles' => 'roles', + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * table should be used to retrieve your permissions. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'permissions' => 'permissions', + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * table should be used to retrieve your models permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_permissions' => 'model_has_permissions', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your models roles. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_roles' => 'model_has_roles', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'role_has_permissions' => 'role_has_permissions', + ], + + 'column_names' => [ + /* + * Change this if you want to name the related pivots other than defaults + */ + 'role_pivot_key' => null, //default 'role_id', + 'permission_pivot_key' => null, //default 'permission_id', + + /* + * Change this if you want to name the related model primary key other than + * `model_id`. + * + * For example, this would be nice if your primary keys are all UUIDs. In + * that case, name this `model_uuid`. + */ + + 'model_morph_key' => 'model_id', + + /* + * Change this if you want to use the teams feature and your related model's + * foreign key is other than `team_id`. + */ + + 'team_foreign_key' => 'team_id', + ], + + /* + * When set to true, the method for checking permissions will be registered on the gate. + * Set this to false if you want to implement custom logic for checking permissions. + */ + + 'register_permission_check_method' => true, + + /* + * When set to true, Laravel\Octane\Events\OperationTerminated event listener will be registered + * this will refresh permissions on every TickTerminated, TaskTerminated and RequestTerminated + * NOTE: This should not be needed in most cases, but an Octane/Vapor combination benefited from it. + */ + 'register_octane_reset_listener' => false, + + /* + * Teams Feature. + * When set to true the package implements teams using the 'team_foreign_key'. + * If you want the migrations to register the 'team_foreign_key', you must + * set this to true before doing the migration. + * If you already did the migration then you must make a new migration to also + * add 'team_foreign_key' to 'roles', 'model_has_roles', and 'model_has_permissions' + * (view the latest version of this package's migration file) + */ + + 'teams' => false, + + /* + * Passport Client Credentials Grant + * When set to true the package will use Passports Client to check permissions + */ + + 'use_passport_client_credentials' => false, + + /* + * When set to true, the required permission names are added to exception messages. + * This could be considered an information leak in some contexts, so the default + * setting is false here for optimum safety. + */ + + 'display_permission_in_exception' => false, + + /* + * When set to true, the required role names are added to exception messages. + * This could be considered an information leak in some contexts, so the default + * setting is false here for optimum safety. + */ + + 'display_role_in_exception' => false, + + /* + * By default wildcard permission lookups are disabled. + * See documentation to understand supported syntax. + */ + + 'enable_wildcard_permission' => false, + + /* + * The class to use for interpreting wildcard permissions. + * If you need to modify delimiters, override the class and specify its name here. + */ + // 'permission.wildcard_permission' => Spatie\Permission\WildcardPermission::class, + + /* Cache-specific settings */ + + 'cache' => [ + + /* + * By default all permissions are cached for 24 hours to speed up performance. + * When permissions or roles are updated the cache is flushed automatically. + */ + + 'expiration_time' => \DateInterval::createFromDateString('24 hours'), + + /* + * The cache key used to store all permissions. + */ + + 'key' => 'spatie.permission.cache', + + /* + * You may optionally indicate a specific cache driver to use for permission and + * role caching using any of the `store` drivers listed in the cache.php config + * file. Using 'default' here means to use the `default` set in cache.php. + */ + + 'store' => 'default', + ], +]; diff --git a/database/factories/AnswerFactory.php b/database/factories/AnswerFactory.php new file mode 100644 index 0000000..704d9ba --- /dev/null +++ b/database/factories/AnswerFactory.php @@ -0,0 +1,26 @@ + + */ +class AnswerFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'question_id' => Question::all()->random()->id, + 'answer' => $this->faker->sentence(10), + 'is_correct' => $this->faker->boolean, + ]; + } +} diff --git a/database/factories/CheckpointFactory.php b/database/factories/CheckpointFactory.php new file mode 100644 index 0000000..0e76c0e --- /dev/null +++ b/database/factories/CheckpointFactory.php @@ -0,0 +1,31 @@ + + */ +class CheckpointFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $address = $this->faker->address; + return [ + 'name' => $address, + 'description' => $this->faker->sentence(30), + 'route_id' => Route::all()->random()->id, + 'order' => 0, + 'latitude' => $this->faker->latitude, + 'longitude' => $this->faker->longitude, + 'address' => $address, + ]; + } +} diff --git a/database/factories/MediaFactory.php b/database/factories/MediaFactory.php new file mode 100644 index 0000000..4fe4409 --- /dev/null +++ b/database/factories/MediaFactory.php @@ -0,0 +1,27 @@ + + */ +class MediaFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'url' => $this->faker->imageUrl(), + 'type' => 'image', + 'mediable_type' => 'App\Models\Checkpoint', + 'mediable_id' => Checkpoint::all()->random()->id, + ]; + } +} diff --git a/database/factories/ProgressFactory.php b/database/factories/ProgressFactory.php new file mode 100644 index 0000000..7a0ad5b --- /dev/null +++ b/database/factories/ProgressFactory.php @@ -0,0 +1,31 @@ + + */ +class ProgressFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $route = Route::all()->random(); + return [ + 'user_id' => User::all()->random()->id, + 'route_id' => $route->id, + 'progress' => $this->faker->randomFloat(2, 0, $route->length), + 'checkpoint_id' => $route->checkpoints->random()->id, + 'started_at' => $this->faker->dateTimeBetween('-1 year', 'now'), + 'completed_at' => $this->faker->dateTimeBetween('-1 year', 'now'), + ]; + } +} diff --git a/database/factories/QuestionFactory.php b/database/factories/QuestionFactory.php new file mode 100644 index 0000000..926da4e --- /dev/null +++ b/database/factories/QuestionFactory.php @@ -0,0 +1,27 @@ + + */ +class QuestionFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'checkpoint_id' => Checkpoint::all()->random()->id, + 'question' => $this->faker->sentence(10), + 'type' => $this->faker->randomElement(['multiple_choice', 'true_or_false', 'short_answer']), + 'order' => 0, + ]; + } +} diff --git a/database/factories/RouteFactory.php b/database/factories/RouteFactory.php new file mode 100644 index 0000000..c124af2 --- /dev/null +++ b/database/factories/RouteFactory.php @@ -0,0 +1,31 @@ + + */ +class RouteFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->sentence(6), + 'description' => $this->faker->sentence(20), + 'user_id' => User::all()->random()->id, + 'length' => $this->faker->randomFloat(2, 1, 100), + 'duration' => $this->faker->randomFloat(2, 1, 300), + 'available_at' => null, + 'unavailable_at' => null, + 'is_public' => $this->faker->boolean(50), + ]; + } +} diff --git a/database/migrations/2024_05_25_100036_create_permission_tables.php b/database/migrations/2024_05_25_100036_create_permission_tables.php new file mode 100644 index 0000000..b865d48 --- /dev/null +++ b/database/migrations/2024_05_25_100036_create_permission_tables.php @@ -0,0 +1,138 @@ +bigIncrements('id'); // permission id + $table->string('name'); // For MySQL 8.0 use string('name', 125); + $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); + $table->timestamps(); + + $table->unique(['name', 'guard_name']); + }); + + Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) { + $table->bigIncrements('id'); // role id + if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing + $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable(); + $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index'); + } + $table->string('name'); // For MySQL 8.0 use string('name', 125); + $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); + $table->timestamps(); + if ($teams || config('permission.testing')) { + $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']); + } else { + $table->unique(['name', 'guard_name']); + } + }); + + Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) { + $table->unsignedBigInteger($pivotPermission); + + $table->string('model_type'); + $table->unsignedBigInteger($columnNames['model_morph_key']); + $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); + + $table->foreign($pivotPermission) + ->references('id') // permission id + ->on($tableNames['permissions']) + ->onDelete('cascade'); + if ($teams) { + $table->unsignedBigInteger($columnNames['team_foreign_key']); + $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index'); + + $table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'], + 'model_has_permissions_permission_model_type_primary'); + } else { + $table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'], + 'model_has_permissions_permission_model_type_primary'); + } + + }); + + Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) { + $table->unsignedBigInteger($pivotRole); + + $table->string('model_type'); + $table->unsignedBigInteger($columnNames['model_morph_key']); + $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index'); + + $table->foreign($pivotRole) + ->references('id') // role id + ->on($tableNames['roles']) + ->onDelete('cascade'); + if ($teams) { + $table->unsignedBigInteger($columnNames['team_foreign_key']); + $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index'); + + $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'], + 'model_has_roles_role_model_type_primary'); + } else { + $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'], + 'model_has_roles_role_model_type_primary'); + } + }); + + Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) { + $table->unsignedBigInteger($pivotPermission); + $table->unsignedBigInteger($pivotRole); + + $table->foreign($pivotPermission) + ->references('id') // permission id + ->on($tableNames['permissions']) + ->onDelete('cascade'); + + $table->foreign($pivotRole) + ->references('id') // role id + ->on($tableNames['roles']) + ->onDelete('cascade'); + + $table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary'); + }); + + app('cache') + ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null) + ->forget(config('permission.cache.key')); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + $tableNames = config('permission.table_names'); + + if (empty($tableNames)) { + throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.'); + } + + Schema::drop($tableNames['role_has_permissions']); + Schema::drop($tableNames['model_has_roles']); + Schema::drop($tableNames['model_has_permissions']); + Schema::drop($tableNames['roles']); + Schema::drop($tableNames['permissions']); + } +}; diff --git a/database/migrations/2024_05_25_114853_create_routes_table.php b/database/migrations/2024_05_25_114853_create_routes_table.php new file mode 100644 index 0000000..ae8312f --- /dev/null +++ b/database/migrations/2024_05_25_114853_create_routes_table.php @@ -0,0 +1,35 @@ +id(); + $table->string('name'); + $table->text('description'); + $table->foreignId('user_id')->constrained()->onDelete('cascade'); + $table->integer('length')->nullable(); + $table->integer('duration')->nullable(); + $table->dateTime('available_at')->nullable(); + $table->dateTime('unavailable_at')->nullable(); + $table->boolean('is_public')->default(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('routes'); + } +}; diff --git a/database/migrations/2024_05_25_115537_create_checkpoints_table.php b/database/migrations/2024_05_25_115537_create_checkpoints_table.php new file mode 100644 index 0000000..efc9609 --- /dev/null +++ b/database/migrations/2024_05_25_115537_create_checkpoints_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('name'); + $table->text('description'); + $table->foreignId('route_id')->constrained()->onDelete('cascade'); + $table->integer('order'); + $table->string('latitude')->nullable(); + $table->string('longitude')->nullable(); + $table->string('address'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('checkpoints'); + } +}; diff --git a/database/migrations/2024_05_25_115853_create_questions_table.php b/database/migrations/2024_05_25_115853_create_questions_table.php new file mode 100644 index 0000000..6ba3f84 --- /dev/null +++ b/database/migrations/2024_05_25_115853_create_questions_table.php @@ -0,0 +1,31 @@ +id(); + $table->foreignId('checkpoint_id')->constrained()->cascadeOnDelete(); + $table->text('question'); + $table->enum('type', ['multiple_choice', 'true_or_false', 'short_answer']); + $table->integer('order')->default(0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('questions'); + } +}; diff --git a/database/migrations/2024_05_25_120449_create_answers_table.php b/database/migrations/2024_05_25_120449_create_answers_table.php new file mode 100644 index 0000000..f630884 --- /dev/null +++ b/database/migrations/2024_05_25_120449_create_answers_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('question_id')->constrained()->onDelete('cascade'); + $table->text('answer'); + $table->boolean('is_correct')->default(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('answers'); + } +}; diff --git a/database/migrations/2024_05_25_120624_create_media_table.php b/database/migrations/2024_05_25_120624_create_media_table.php new file mode 100644 index 0000000..05db074 --- /dev/null +++ b/database/migrations/2024_05_25_120624_create_media_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('url'); + $table->enum('type', ['image', 'video', 'audio']); + $table->string('alt')->nullable(); + $table->integer('mediable_id'); + $table->string('mediable_type'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('media'); + } +}; diff --git a/database/migrations/2024_05_25_120858_create_progress_table.php b/database/migrations/2024_05_25_120858_create_progress_table.php new file mode 100644 index 0000000..b7229e9 --- /dev/null +++ b/database/migrations/2024_05_25_120858_create_progress_table.php @@ -0,0 +1,35 @@ +id(); + $table->foreignId('user_id')->constrained()->cascadeOnDelete(); + $table->foreignId('route_id')->constrained()->cascadeOnDelete(); + $table->integer('progress')->default(0); + $table->foreignId('checkpoint_id')->nullable()->constrained()->nullOnDelete(); + $table->foreignId('question_id')->nullable()->constrained()->nullOnDelete(); + $table->integer('score')->default(0); + $table->timestamp('started_at')->nullable(); + $table->timestamp('completed_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('progress'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index d01a0ef..96ad23f 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,7 +2,14 @@ namespace Database\Seeders; +use App\Models\Answer; +use App\Models\Checkpoint; +use App\Models\Media; +use App\Models\Progress; +use App\Models\Question; +use App\Models\Route; use App\Models\User; + // use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; @@ -13,11 +20,69 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - // User::factory(10)->create(); + User::factory(rand(10, 100))->create(); User::factory()->create([ 'name' => 'Test User', 'email' => 'test@example.com', ]); + + Route::factory(50)->create(); + + for ($i = 1; $i <= 450; $i++) { + $checkpoint = Checkpoint::factory()->create(); + $checkpointExisting = Checkpoint::where('route_id', $checkpoint->route_id); + if ($checkpointExisting->count() > 1) { + $checkpoint->order = $checkpointExisting->count() - 1; + $checkpoint->save(); + } + } + + foreach (Checkpoint::all() as $checkpoint) { + $question = null; + for ($i = 1; $i <= rand(0, 5); $i++) { + $question = Question::factory()->create([ + 'checkpoint_id' => $checkpoint->id, + ]); + $questionExisting = Question::where('checkpoint_id', $question->checkpoint_id); + if ($questionExisting->count() > 1) { + $question->order = $questionExisting->count() - 1; + $question->save(); + } + + $type = $question->type; + + if ($type === 'multiple_choice') { + $true = rand(0, 3); + for ($j = 0; $j < rand(3, 5); $j++) { + $correct = false; + if ((bool) $true) $correct = (bool) rand(0, 1); + if ($correct) $true -= 1; + $question->answers()->create([ + 'answer' => fake()->sentence(6) . ($correct ? ' (correct)' : ''), + 'is_correct' => $correct, + ]); + } + } elseif ($type === 'true_or_false') { + $question->answers()->create([ + 'answer' => 'True (correct)', + 'is_correct' => true, + ]); + $question->answers()->create([ + 'answer' => 'False', + 'is_correct' => false, + ]); + } elseif ($type === 'short_answer') { + Answer::factory(rand(1, 5))->create([ + 'question_id' => $question->id, + 'is_correct' => true, + ]); + } + } + } + + Progress::factory(rand(50, 150))->create(); + + Media::factory(rand(50, 150))->create(); } } diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 267eb93..990ff09 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -51,7 +51,7 @@ diff --git a/resources/views/layouts/guest.blade.php b/resources/views/layouts/guest.blade.php index 7c33d5a..addc302 100644 --- a/resources/views/layouts/guest.blade.php +++ b/resources/views/layouts/guest.blade.php @@ -51,7 +51,7 @@ diff --git a/resources/views/layouts/main.blade.php b/resources/views/layouts/main.blade.php index 9e32af4..bdde8a0 100644 --- a/resources/views/layouts/main.blade.php +++ b/resources/views/layouts/main.blade.php @@ -61,7 +61,7 @@ class="h-full" diff --git a/tests/Feature/Models/AnswersTest.php b/tests/Feature/Models/AnswersTest.php new file mode 100644 index 0000000..b46239f --- /dev/null +++ b/tests/Feature/Models/AnswersTest.php @@ -0,0 +1,7 @@ +get('/'); + + $response->assertStatus(200); +}); diff --git a/tests/Feature/Models/CheckpointsTest.php b/tests/Feature/Models/CheckpointsTest.php new file mode 100644 index 0000000..b46239f --- /dev/null +++ b/tests/Feature/Models/CheckpointsTest.php @@ -0,0 +1,7 @@ +get('/'); + + $response->assertStatus(200); +}); diff --git a/tests/Feature/Models/MediaTest.php b/tests/Feature/Models/MediaTest.php new file mode 100644 index 0000000..b46239f --- /dev/null +++ b/tests/Feature/Models/MediaTest.php @@ -0,0 +1,7 @@ +get('/'); + + $response->assertStatus(200); +}); diff --git a/tests/Feature/Models/ProgressTest.php b/tests/Feature/Models/ProgressTest.php new file mode 100644 index 0000000..b46239f --- /dev/null +++ b/tests/Feature/Models/ProgressTest.php @@ -0,0 +1,7 @@ +get('/'); + + $response->assertStatus(200); +}); diff --git a/tests/Feature/Models/QuestionsTest.php b/tests/Feature/Models/QuestionsTest.php new file mode 100644 index 0000000..b46239f --- /dev/null +++ b/tests/Feature/Models/QuestionsTest.php @@ -0,0 +1,7 @@ +get('/'); + + $response->assertStatus(200); +}); diff --git a/tests/Feature/Models/RoutesTest.php b/tests/Feature/Models/RoutesTest.php new file mode 100644 index 0000000..20b26a0 --- /dev/null +++ b/tests/Feature/Models/RoutesTest.php @@ -0,0 +1,5 @@ +