Skip to content

Commit

Permalink
Changes to support the requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ committed Oct 18, 2024
1 parent e4fe952 commit d2e8369
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 36 deletions.
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'
];
}
}
}
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 @@ -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',
Expand Down Expand Up @@ -213,6 +215,7 @@ public function submit()

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

Expand All @@ -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) {
Expand All @@ -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;
}
}


Expand Down
48 changes: 35 additions & 13 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 @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

Expand All @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -334,4 +356,4 @@ public function render()
{
return view('livewire.backend.edit-courses');
}
}
}
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
18 changes: 10 additions & 8 deletions resources/views/livewire/backend/create-courses.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
<div class="row" id="row1">
<div class="col-12 col-sm-6 py-2">
<div class="col ps-0">
<label for="drop1">
<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 @@ -42,11 +43,11 @@
</div>
<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 @@ -56,9 +57,10 @@
</div>
<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 @@ -111,7 +113,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 @@ -29,7 +29,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 @@ -42,11 +43,12 @@
</div>
<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 @@ -56,9 +58,10 @@
</div>
<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 @@ -112,7 +115,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

0 comments on commit d2e8369

Please sign in to comment.