Skip to content
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

[UI/BE] make curriculums depends on the academic program in frontend #148 #191

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
27 changes: 22 additions & 5 deletions app/Domains/AcademicProgram/AcademicProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@ public static function getAcademicPrograms(): array
];
}

public static function getVersions(): array
public static function getVersions($academicProgram = null): array
{
// TODO integrate with Taxonomies
return [
1 => 'Current Curriculum',
2 => 'Curriculum - Effective from E22'
$academicPrograms = [
'undergraduate' => [
1 => 'Current Curriculum',
2 => 'Curriculum - Effective from E22'
],
'postgraduate' => [
3 => 'Current Curriculum - PG',
]
];

if ($academicProgram == null) {
$allAcademicPrograms = [];
foreach ($academicPrograms as $programs) {
foreach ($programs as $key => $value) $allAcademicPrograms[$key] = $value;
}
return $allAcademicPrograms;
} else if (array_key_exists($academicProgram, $academicPrograms)) {
return $academicPrograms[$academicProgram];
} else {
return [];
}
}

public static function getTypes(): array
Expand All @@ -35,4 +52,4 @@ public static function getTypes(): array
'TE' => 'Technical Elective'
];
}
}
}
7 changes: 6 additions & 1 deletion app/Domains/AcademicProgram/Course/Models/Course.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ public function semester()

public function version()
{
return $this->getVersions()[$this->version];
$versions = $this->getVersions();
if ($this->version != null && array_key_exists($this->version, $versions)) {
return $versions[$this->version];
} else {
return "Unknown";
}
}

public function modules()
Expand Down
25 changes: 24 additions & 1 deletion app/Http/Livewire/Backend/CreateCourses.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CreateCourses extends Component
//for selectors
public $academicProgramsList = [];
public $semestersList = [];
public $curriculumList = [];


//form inputs
//1st form step
Expand Down Expand Up @@ -47,7 +49,7 @@ public function rules()
return [
'academicProgram' => 'required|string',
'semester' => 'required|string',
'version' => ['required', 'string', Rule::in(array_keys(Course::getVersions()))],
'version' => ['required', Rule::in(array_keys(Course::getVersions()))],
'type' => ['required', 'string', Rule::in(array_keys(Course::getTypes()))],
'code' => 'required|string|unique:courses,code',
'name' => 'required|string|max:255',
Expand Down Expand Up @@ -216,6 +218,7 @@ public function submit()

public function updatedAcademicProgram()
{
$this->updateCurriculumList();
$this->updateSemestersList();
}

Expand All @@ -224,6 +227,21 @@ public function updatedVersion()
$this->updateSemestersList();
}


public function updateCurriculumList()
{
if ($this->academicProgram) {
$this->curriculumList = Course::getVersions($this->academicProgram);
} else {
$this->curriculumList = [];
}

if (!array_key_exists($this->version, $this->curriculumList)) {
// Unset if it not belongs to
$this->version = null;
}
}

public function updateSemestersList()
{
if ($this->academicProgram && $this->version) {
Expand All @@ -234,6 +252,11 @@ public function updateSemestersList()
} else {
$this->semestersList = [];
}

if (count($this->semestersList) == 0 || !array_key_exists($this->semester, $this->semestersList)) {
// Unset if it not belongs to
$this->semester = null;
}
}


Expand Down
34 changes: 32 additions & 2 deletions app/Http/Livewire/Backend/EditCourses.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class EditCourses extends Component
// Selectors
public $academicProgramsList = [];
public $semestersList = [];
public $curriculumList = [];

// Form inputs
// 1st form step
Expand Down Expand Up @@ -50,7 +51,7 @@ public function rules()
$validationRules = [
'academicProgram' => 'required|string',
'semester' => 'required|int',
'version' => ['required', 'string', Rule::in(array_keys(Course::getVersions()))],
'version' => ['required', Rule::in(array_keys(Course::getVersions()))],
'type' => ['required', 'string', Rule::in(array_keys(Course::getTypes()))],
'code' => 'required|string',
'name' => 'required|string|max:255',
Expand Down Expand Up @@ -139,7 +140,12 @@ protected function validateMarksAllocation()
public function updated($propertyName)
{
$this->canUpdate = false;
$this->validateCurrentStep();

if (!($this->version == null || $this->semester == null)) {
// This to allow fillings while either version or semester is null
$this->validateCurrentStep();
}

if ($this->getErrorBag()->has('marks_allocation.total')) {
return;
}
Expand Down Expand Up @@ -184,8 +190,13 @@ public function mount(Course $course)
})->toArray();
$this->prerequisites = $course->prerequisites;

$this->prerequisites = $course->prerequisites->pluck('id')->toArray();

// Update semesters list based on academic program and version
$this->updateSemestersList();

// Update curriculum list based on academic program
$this->updateCurriculumList();
}

public function updatePrerequisites($selectedCourses)
Expand Down Expand Up @@ -234,6 +245,7 @@ public function update()

public function updatedAcademicProgram()
{
$this->updateCurriculumList();
$this->updateSemestersList();
}

Expand All @@ -242,6 +254,19 @@ public function updatedVersion()
$this->updateSemestersList();
}

public function updateCurriculumList()
{
if ($this->academicProgram) {
$this->curriculumList = Course::getVersions($this->academicProgram);
} else {
$this->curriculumList = [];
}
if (!array_key_exists($this->version, $this->curriculumList)) {
// Unset if it not belongs to
$this->version = '';
}
}

public function updateSemestersList()
{
if ($this->academicProgram && $this->version) {
Expand All @@ -252,6 +277,11 @@ public function updateSemestersList()
} else {
$this->semestersList = [];
}

if (count($this->semestersList) == 0 || !array_key_exists($this->semester, $this->semestersList)) {
// Unset if it not belongs to
$this->semester = '';
}
}

protected function updateCourse()
Expand Down
7 changes: 5 additions & 2 deletions database/factories/CourseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ class CourseFactory extends Factory
*/
public function definition()
{
$academicProgram = $this->faker->randomElement(array_keys(Course::getAcademicPrograms()));
return [
'code' => $this->faker->unique()->regexify('[A-Z]{4}[0-9]{4}'),
'semester_id' => $this->faker->numberBetween(1, 8),
'academic_program' => $this->faker->randomElement(array_keys(Course::getAcademicPrograms())),
'version' => $this->faker->randomElement([1, 2]),
'academic_program' => $academicProgram,
'version' => $this->faker->randomElement(
array_keys(Course::getVersions($academicProgram))
),
'name' => $this->faker->sentence(3),
'credits' => $this->faker->numberBetween(1, 6),
'type' => $this->faker->randomElement(array_keys(Course::getTypes())),
Expand Down
39 changes: 39 additions & 0 deletions database/migrations/2024_10_11_124838_alter_column_version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Domains\AcademicProgram\Course\Models\Course;

class AlterColumnVersion extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('semesters', function (Blueprint $table) {
$table->integer('version')->change();
});
Schema::table('courses', function (Blueprint $table) {
$table->integer('version')->change();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('semesters', function (Blueprint $table) {
$table->enum('version', array_keys(Course::getVersions()))->change();
});
Schema::table('courses', function (Blueprint $table) {
$table->enum('version', array_keys(Course::getVersions()))->change();
});
}
}
3 changes: 2 additions & 1 deletion resources/views/backend/semesters/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@

<!-- Version -->
<div class="form-group row">
{!! Form::label('version', 'Version*', ['class' => 'col-md-2 col-form-label']) !!}
{!! Form::label('version', 'Curriculum*', ['class' => 'col-md-2 col-form-label']) !!}
<div class="col-md-10">
{{-- TODO make this depends from the Academic Program --}}
{!! Form::select('version', \App\Domains\AcademicProgram\Semester\Models\Semester::getVersions(), null, [
'class' => 'form-select',
'placeholder' => 'Select Version',
Expand Down
3 changes: 2 additions & 1 deletion resources/views/backend/semesters/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@

<!-- Version -->
<div class="form-group row">
{!! Form::label('version', 'Version*', ['class' => 'col-md-2 col-form-label']) !!}
{!! Form::label('version', 'Curriculum*', ['class' => 'col-md-2 col-form-label']) !!}
<div class="col-md-10">
{{-- TODO make this depends from the Academic Program --}}
{!! Form::select('version', \App\Domains\AcademicProgram\Semester\Models\Semester::getVersions(), null, [
'class' => 'form-control',
'required' => true,
Expand Down
23 changes: 14 additions & 9 deletions resources/views/livewire/backend/create-courses.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
<div class="col ps-0">
<label for="drop1">
Academic Program*
</label>
<label for="dropAcademicProgram">
Academic Program
</label>
</div>
<select class="form-select" wire:model="academicProgram">
<select id="dropAcademicProgram" name="dropAcademicProgram" class="form-select"
wire:model="academicProgram">
<option style="display:none" selected></option>
@foreach ($academicProgramsList as $academicProgramId => $academicProgramTitle)
<option value="{{ $academicProgramId }}">{{ $academicProgramTitle }}
Expand All @@ -46,11 +49,12 @@
{{-- Curriculum --}}
<div class="col-12 col-sm-6 py-2">
<div class="col ps-0">
<label for="drop1">Curriculum*</label>
<label for="dropCurriculum">Curriculum*</label>
</div>
<select class="form-select" wire:model="version">
<select id="dropCurriculum" name="dropCurriculum" class="form-select"
wire:model="version">
<option style="display:none" selected></option>
@foreach (App\Domains\AcademicProgram\Course\Models\Course::getVersions() as $key => $value)
@foreach ($curriculumList as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
Expand All @@ -64,7 +68,8 @@
<div class="col ps-0">
<label for="drop1">Semester*</label>
</div>
<select class="form-select" wire:model="semester">
<select id="dropSemester" name="dropSemester" class="form-select"
wire:model="semester">
<option style="display:none" selected></option>
@foreach ($semestersList as $semesterId => $semesterTitle)
<option value="{{ $semesterId }}">{{ $semesterTitle }}</option>
Expand Down Expand Up @@ -105,9 +110,9 @@
{{-- Course Type --}}
<div class="col-12 col-sm-6 py-2">
<div class="col ps-0">
<label for="drop1">Type*</label>
<label for="dropType">Type*</label>
</div>
<select class="form-select" wire:model="type">
<select id="dropType" name="dropType" class="form-select" wire:model="type">
<option style="display:none" selected></option>
@foreach (App\Domains\AcademicProgram\Course\Models\Course::getTypes() as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
Expand All @@ -124,7 +129,7 @@
<label>Credits*</label>
</div>
<div class="input-group">
<input type="text" class="form-control" wire:model.lazy ="credits">
<input type="number" class="form-control" wire:model.lazy ="credits">
</div>
@error('credits')
<span class="text-danger">{{ $message }}</span>
Expand Down
17 changes: 10 additions & 7 deletions resources/views/livewire/backend/edit-courses.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
Academic Program*
</label>
</div>
<select class="form-select" wire:model="academicProgram">
<select id="academicProgram" name="academicProgram" class="form-select"
wire:model="academicProgram">
<option style="display:none" selected></option>
@foreach ($academicProgramsList as $academicProgramId => $academicProgramTitle)
<option value="{{ $academicProgramId }}">{{ $academicProgramTitle }}
Expand All @@ -46,11 +47,12 @@
{{-- Curriculum --}}
<div class="col-12 col-sm-6 py-2">
<div class="col ps-0">
<label for="drop1">Curriculum*</label>
<label for="dropCurriculum">Curriculum*</label>
</div>
<select class="form-select" wire:model="version">
<select id="dropCurriculum" name="dropCurriculum" class="form-select"
wire:model="version">
<option style="display:none" selected></option>
@foreach (App\Domains\AcademicProgram\Course\Models\Course::getVersions() as $key => $value)
@foreach ($curriculumList as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
Expand All @@ -62,9 +64,10 @@
{{-- Semester --}}
<div class="col-12 py-2">
<div class="col ps-0">
<label for="drop1">Semester*</label>
<label for="dropSemester">Semester*</label>
</div>
<select class="form-select" wire:model="semester">
<select id="dropSemester" name="dropSemester" class="form-select"
wire:model="semester">
<option style="display:none" selected></option>
@foreach ($semestersList as $semesterId => $semesterTitle)
<option value="{{ $semesterId }}">{{ $semesterTitle }}</option>
Expand Down Expand Up @@ -125,7 +128,7 @@
<label>Credits*</label>
</div>
<div class="input-group">
<input type="text" class="form-control" wire:model.lazy ="credits">
<input type="number" class="form-control" wire:model.lazy ="credits">
</div>
@error('credits')
<span class="text-danger">{{ $message }}</span>
Expand Down
Loading
Loading