diff --git a/app/Domains/AcademicProgram/AcademicProgram.php b/app/Domains/AcademicProgram/AcademicProgram.php index 248b4ce..4f72bc6 100644 --- a/app/Domains/AcademicProgram/AcademicProgram.php +++ b/app/Domains/AcademicProgram/AcademicProgram.php @@ -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 @@ -35,4 +52,4 @@ public static function getTypes(): array 'TE' => 'Technical Elective' ]; } -} \ No newline at end of file +} diff --git a/app/Http/Livewire/Backend/CreateCourses.php b/app/Http/Livewire/Backend/CreateCourses.php index f544b1f..aa59e11 100644 --- a/app/Http/Livewire/Backend/CreateCourses.php +++ b/app/Http/Livewire/Backend/CreateCourses.php @@ -16,6 +16,8 @@ class CreateCourses extends Component //for selectors public $academicProgramsList = []; public $semestersList = []; + public $curriculumList = []; + //form inputs //1st form step @@ -46,7 +48,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', @@ -213,6 +215,7 @@ public function submit() public function updatedAcademicProgram() { + $this->updateCurriculumList(); $this->updateSemestersList(); } @@ -221,6 +224,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) { @@ -231,6 +249,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; + } } diff --git a/app/Http/Livewire/Backend/EditCourses.php b/app/Http/Livewire/Backend/EditCourses.php index 0409f61..0a4838a 100644 --- a/app/Http/Livewire/Backend/EditCourses.php +++ b/app/Http/Livewire/Backend/EditCourses.php @@ -17,6 +17,7 @@ class EditCourses extends Component // Selectors public $academicProgramsList = []; public $semestersList = []; + public $curriculumList = []; // Form inputs // 1st form step @@ -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', @@ -138,7 +139,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; } @@ -181,8 +187,12 @@ public function mount(Course $course) ]; })->toArray(); $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) @@ -218,20 +228,14 @@ public function previous() public function update() { - try { - - $this->validateCurrentStep(); - $this->updateCourse(); - return redirect()->route('dashboard.courses.index')->with('Success', 'Course updated successfully.'); - } catch (\Exception $e) { - \Log::error("Error in update method: " . $e->getMessage()); - session()->flash('error', 'There was an error updating the course: ' . $e->getMessage()); - } - $this->resetForm(); + $this->validateCurrentStep(); + $this->updateCourse(); + return redirect()->route('dashboard.courses.index')->with('Success', 'Course updated successfully.'); } public function updatedAcademicProgram() { + $this->updateCurriculumList(); $this->updateSemestersList(); } @@ -240,6 +244,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) { @@ -250,6 +267,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() @@ -334,4 +356,4 @@ public function render() { return view('livewire.backend.edit-courses'); } -} \ No newline at end of file +} diff --git a/database/migrations/2024_10_11_124838_alter_column_version.php b/database/migrations/2024_10_11_124838_alter_column_version.php new file mode 100644 index 0000000..6043e33 --- /dev/null +++ b/database/migrations/2024_10_11_124838_alter_column_version.php @@ -0,0 +1,39 @@ +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(); + }); + } +} diff --git a/resources/views/backend/semesters/create.blade.php b/resources/views/backend/semesters/create.blade.php index 08b14f4..e439d55 100644 --- a/resources/views/backend/semesters/create.blade.php +++ b/resources/views/backend/semesters/create.blade.php @@ -55,8 +55,9 @@
- {!! Form::label('version', 'Version*', ['class' => 'col-md-2 col-form-label']) !!} + {!! Form::label('version', 'Curriculum*', ['class' => 'col-md-2 col-form-label']) !!}
+ {{-- 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', diff --git a/resources/views/backend/semesters/edit.blade.php b/resources/views/backend/semesters/edit.blade.php index 2fb08e9..e84ffd4 100644 --- a/resources/views/backend/semesters/edit.blade.php +++ b/resources/views/backend/semesters/edit.blade.php @@ -53,8 +53,9 @@
- {!! Form::label('version', 'Version*', ['class' => 'col-md-2 col-form-label']) !!} + {!! Form::label('version', 'Curriculum*', ['class' => 'col-md-2 col-form-label']) !!}
+ {{-- TODO make this depends from the Academic Program --}} {!! Form::select('version', \App\Domains\AcademicProgram\Semester\Models\Semester::getVersions(), null, [ 'class' => 'form-control', 'required' => true, diff --git a/resources/views/livewire/backend/create-courses.blade.php b/resources/views/livewire/backend/create-courses.blade.php index 56c8bbe..9c8f2b1 100644 --- a/resources/views/livewire/backend/create-courses.blade.php +++ b/resources/views/livewire/backend/create-courses.blade.php @@ -25,11 +25,12 @@
-
- @foreach ($academicProgramsList as $academicProgramId => $academicProgramTitle)
- +
- - @foreach (App\Domains\AcademicProgram\Course\Models\Course::getVersions() as $key => $value) + @foreach ($curriculumList as $key => $value) @endforeach @@ -56,9 +57,10 @@
- +
- @foreach ($semestersList as $semesterId => $semesterTitle) @@ -111,7 +113,7 @@
- +
@error('credits') {{ $message }} diff --git a/resources/views/livewire/backend/edit-courses.blade.php b/resources/views/livewire/backend/edit-courses.blade.php index 44c2db6..ddf4758 100644 --- a/resources/views/livewire/backend/edit-courses.blade.php +++ b/resources/views/livewire/backend/edit-courses.blade.php @@ -29,7 +29,8 @@ Academic Program
- @foreach ($academicProgramsList as $academicProgramId => $academicProgramTitle)
- +
- - @foreach (App\Domains\AcademicProgram\Course\Models\Course::getVersions() as $key => $value) + @foreach ($curriculumList as $key => $value) @endforeach @@ -56,9 +58,10 @@
- +
- @foreach ($semestersList as $semesterId => $semesterTitle) @@ -112,7 +115,7 @@
- +
@error('credits') {{ $message }}