Skip to content

Commit

Permalink
Merge pull request #113 from hpmouton/main
Browse files Browse the repository at this point in the history
JCP Module
  • Loading branch information
hpmouton authored Oct 28, 2024
2 parents 376c89b + 70b5dfc commit 221c298
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 214 deletions.
127 changes: 40 additions & 87 deletions app/Livewire/System/Jcps/JCPCreateForm.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
<?php

namespace App\Livewire\System\Jcps;

use App\Models\Audit\assessment;
use App\Models\Audit\Jcp;
use App\Models\Audit\jcp;
use App\Models\Audit\prerequisite;
use App\Models\Audit\qualification;
use App\Models\Audit\skill;
use App\Models\User;
use Livewire\Component;


class JCPCreateForm extends Component
{
public $currentPage = 1;

{
public $user_id = '';

public $position_title = '';

public $duty_station = '';

public $job_grade = '';

public $job_purpose = '';

public $is_active = '';

public $jcp_qualifications = [];

public $jcp_skills = [];

public $jcp_prerequisites = [];

public function mount()
Expand All @@ -39,125 +29,87 @@ public function mount()
];
}

// Page information
public $pages = [
1 => [
'title' => 'Job Competency Profile Information',
'description' => 'Make sure the information you enter corresponds to the Job Description.',
],
2 => [
'title' => 'Prerequisite Information',
'description' => 'Enter the information regarding required qualifications, certifications, and/or licenses.',
],
3 => [
'title' => 'Skills Information',
'description' => 'Enter the information regarding required skills and abilities.',
],
];

public function nextPage()
{
$this->validateForm();
$this->currentPage++;
}

public function previousPage()
{
$this->currentPage--;
}

public function addSkill()
{
$this->jcp_skills[] = ['skill_id' => '', 'required_rating' => 1];
}


public function removeSkill($index)
{
unset($this->jcp_skills[$index]);
$this->jcp_skills = array_values($this->jcp_skills); // Reindex the array
}


public function createJCPInformation()
{
$this->save();
}

public function save()
{
dd($this->validateForm());

// Check if a record already exists for the given user_id and assessment_id
$existingJcp = Jcp::where('user_id', $this->user_id)
->where('assessment_id', 1)
->first();

if ($existingJcp) {
session()->flash('error', 'A JCP for this user and assessment already exists.');

return redirect()->route('jcp.index'); // redirect back with an error message
}

$this->validateForm();
// Create a new JCP
$jcp = Jcp::create([

$jcp = JCP::create([

'position_title' => $this->position_title,
'duty_station' => $this->duty_station,
'user_id' => $this->user_id,
'job_grade' => $this->job_grade,
'job_purpose' => $this->job_purpose,
'is_active' => $this->is_active,

'is_active' => is_null($this->is_active) ? null : 0, //Add Frontend for these elements
'user_id' => $this->user_id,
'assessment_id' => assessment::find(1)->id, //testing
]);


// Attach qualifications
foreach ($this->jcp_qualifications as $qualId) {
$jcp->qualifications()->attach($qualId);
}
$jcp->qualifications()->sync($this->jcp_qualifications);


// Attach prerequisites
foreach ($this->jcp_prerequisites as $prerequisite_id) {
$jcp->prerequisites()->attach($prerequisite_id);
}
$jcp->prerequisites()->sync($this->jcp_prerequisites);

// Attach skills with required rating
foreach ($this->jcp_skills as $skill) {
if (! empty($skill['skill_id'])) {
if (!empty($skill['skill_id'])) {
$jcp->skills()->attach($skill['skill_id'], ['required_level' => $skill['required_rating']]);
}
}

session()->flash('status', 'JCP successfully created.');
$this->reset();

return redirect()->route('jcp.index');
return redirect()->route('jcp.index')->with('success','JCP successfully created.');
}


public function validateForm()
{
$rules = [];
if ($this->currentPage === 1) {
$rules = [
'position_title' => 'required|string|max:255',
'duty_station' => 'required|string|max:255',
'job_grade' => 'required|string|max:255',
'job_purpose' => 'required|string',
'user_id' => 'required|exists:users,id',
];
} elseif ($this->currentPage === 2) {
$rules = [
'jcp_qualifications.*' => 'required|exists:qualifications,id',
'jcp_prerequisites.*' => 'required|exists:prerequisites,id',
];
} elseif ($this->currentPage === 3) {
$rules = [
'jcp_skills.*.skill_id' => 'required|exists:skills,id',
'jcp_skills.*.required_rating' => 'required|integer|min:1|max:5',
];
}
$this->validate([

$this->validate($rules);
}
'position_title' => 'required|string|max:255',

'duty_station' => 'required|string|max:255',

'user_id' => 'required|exists:users,id',

'job_grade' => 'required|string',

'job_purpose' => 'required|string',

'is_active' => 'boolean',

'jcp_qualifications' => 'required|array',

'jcp_qualifications.*' => 'exists:qualifications,id', // Validate each qualification

// Add other validations as necessary

]);

}
public function render()
{
return view('livewire.system.jcps.j-c-p-create-form', [
Expand All @@ -167,4 +119,5 @@ public function render()
'prerequisites' => Prerequisite::all(),
]);
}

}
5 changes: 0 additions & 5 deletions app/Models/Audit/assessment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class assessment extends Model

protected $guarded = [];

public function jcp()
{
return $this->hasMany(jcp::class, 'assessment_id');

}

public function enrolled()
{
Expand Down
4 changes: 0 additions & 4 deletions app/Models/Audit/jcp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public function employee()
return $this->belongsTo(User::class, 'user_id');
}

public function assessment()
{
return $this->belongsTo(assessment::class, 'assessment_id');
}

public function skills()
{
Expand Down
2 changes: 0 additions & 2 deletions database/factories/Audit/jcpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ class jcpFactory extends Factory
*/
public function definition(): array
{
$assessment_ids = \App\Models\Audit\assessment::select('id')->get();
$user_ids = \App\Models\User::select('id')->get();

return [
//This populates the jcp model fields
'assessment_id' => $this->faker->randomElement($assessment_ids),
'user_id' => $this->faker->unique()->randomElement($user_ids),
'position_title' => $this->faker->jobTitle(),
'job_grade' => $this->faker->numerify('B-#'),
Expand Down
2 changes: 0 additions & 2 deletions database/migrations/2024_03_22_094647_create_jcps_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function up(): void
{
Schema::create('jcps', function (Blueprint $table) {
$table->id();
$table->foreignId('assessment_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();

$table->string('position_title');
Expand All @@ -23,7 +22,6 @@ public function up(): void
$table->integer('is_active')->default(1);

// Add a unique constraint
$table->unique(['user_id', 'assessment_id']);

$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<div class="flex">

<div class="flex-initial w-full">
<input type="text" wire:model.live.debounce.300ms="search" placeholder="Search Assessments directory..." class="mb-4 p-2 px-4 shadow-sm w-full border border-gray-300 rounded-l-3xl">
<input type="text" wire:model.live.debounce.300ms="search" placeholder="Search Assessments directory..." class="mb-4 p-2 px-4 shadow-sm w-full border border-gray-300 rounded-r-md rounded-l-3xl">
</div>
<div class="flex-initial w-auto ml-3 mb-4">


<a class="flex flex-row p-2 px-4 bg-sky-400 hover:bg-sky-500 text-white transition ease-in-out duration-300 rounded-r-3xl shadow-sm" href={{ route('assessments.create')}}>
<a class="flex flex-row p-2 px-4 bg-sky-200 border hover:bg-sky-300 text-sky-800 transition ease-in-out duration-300 rounded-r-3xl rounded-l-md shadow-sm" href={{ route('assessments.create')}}>
<x-gmdi-add-o class="w-6 h-6" />
Assessment
</a>
Expand Down
Loading

0 comments on commit 221c298

Please sign in to comment.