Skip to content

Commit

Permalink
Merge pull request #116 from OrifInformatique/Release-4.3
Browse files Browse the repository at this point in the history
Release 4.2.1
- Block double link between same apprentice and trainer
- Fix display of disabled course plans
- Rework user courses management
  • Loading branch information
DidierViret committed Jul 10, 2024
2 parents 3fbda98 + 32dcda2 commit 9f94bdd
Show file tree
Hide file tree
Showing 28 changed files with 561 additions and 231 deletions.
187 changes: 122 additions & 65 deletions orif/plafor/Controllers/Apprentice.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,29 +195,74 @@ public function view_apprentice($apprentice_id = 0) {
return $this->display_view('Plafor\apprentice/view',$output);
}


/**
* Display the list of user courses linked to one given user
*
* @param int $id_apprentice ID of the concerned apprentice
*
* @return void
*
*/
public function list_user_courses($id_apprentice = null)
{
if(is_numeric($id_apprentice))
$user = $this->user_model->where('id', $id_apprentice)->first();

if(!isset($user) || empty($user))
return redirect()->to('plafor/apprentice/list_apprentice');

$user_courses = $this->user_course_model->where('fk_user', $id_apprentice)->findAll();

// Get the course_plan informations for each user_course
foreach($user_courses as &$user_course)
{
$user_course['course_plan'] = $this->course_plan_model->withDeleted()->find($user_course['fk_course_plan']);
$user_course['status'] = $this->user_course_status_model->getUserCourseStatusName($user_course['fk_status']);

if($user_course['date_end'] === '0000-00-00')
$user_course['date_end'] = null;
}

$output = array(
'title' => sprintf(lang('plafor_lang.title_user_course_plan_list'), $user['username']),
'user_courses' => $user_courses,
'id_apprentice' => $id_apprentice
);

return $this->display_view(['Plafor\user_course\list'], $output);
}


/**
* Displays a form to create a link between an apprentice and a course plan
*
* @param int $id_apprentice : ID of the apprentice
* @param int $id_user_course : ID of the user's course
* @param int $id_apprentice ID of the apprentice
* @param int $id_user_course ID of the user's course
*
* @return void
*
*/
public function save_user_course($id_apprentice = 0, $id_user_course = 0) {
public function save_user_course($id_apprentice = 0, $id_user_course = 0)
{
// Access permissions
if ($this->session->get('user_access')>=config('\User\Config\UserConfig')->access_lvl_trainer) {
// Gets data of the apprentice and their course, if they exist
$user_type_id = $this->user_type_model->
where('access_level', config('\User\Config\UserConfig')->access_level_apprentice)->first()['id'];
$apprentice = $this->user_model->where('fk_user_type', $user_type_id)->find($id_apprentice);
if($this->session->get('user_access')>=config('\User\Config\UserConfig')->access_lvl_trainer)
{
$user_type_id = $this->user_type_model
->where('access_level', config('\User\Config\UserConfig')->access_level_apprentice)
->first()['id'];

$apprentice = $this->user_model
->where('fk_user_type', $user_type_id)
->find($id_apprentice);

$user_course = $this->user_course_model->find($id_user_course);

// Redirection
if (is_null($apprentice)) {
if(is_null($apprentice))
return redirect()->to(base_url('plafor/apprentice/list_apprentice'));
}

// Actions upon form submission
if (count($_POST) > 0) {
if(count($_POST) > 0)
{
$fk_course_plan = $this->request->getPost('course_plan');
$new_user_course = array(
'fk_user' => $id_apprentice,
Expand All @@ -227,65 +272,73 @@ public function save_user_course($id_apprentice = 0, $id_user_course = 0) {
'date_end' => $this->request->getPost('date_end'),
);

// Query to perform
if (!is_null($user_course)) {
if (!is_null($user_course))
// User's course already exists - updates it
$this->user_course_model->update($id_user_course, $new_user_course);
} else if ($this->user_course_model->where('fk_user', $id_apprentice)->where('fk_course_plan', $fk_course_plan)->first() == null) {
// No user's course was found in database - inserts a new one
$id_user_course = $this->user_course_model->insert($new_user_course);

$course_plan = $this->user_course_model->getCoursePlan($new_user_course['fk_course_plan']);
$competenceDomainIds = [];

foreach ($this->course_plan_model->getCompetenceDomains($course_plan['id']) as $competence_domain) {
$competenceDomainIds[] = $competence_domain['id'];
}

else
{
$user_has_course = $this->user_course_model->where(['fk_user' => $id_apprentice, 'fk_course_plan' => $fk_course_plan])->findAll() ? true : false;

// If the apprentice already follows the course plan submitted, prevent the creation of the entry.
if(!$user_has_course)
{
// No user's course was found in database - inserts a new one
$id_user_course = $this->user_course_model->insert($new_user_course);

$course_plan = $this->user_course_model->getCoursePlan($new_user_course['fk_course_plan']);
$competenceDomainIds = [];

foreach ($this->course_plan_model->getCompetenceDomains($course_plan['id']) as $competence_domain)
$competenceDomainIds[] = $competence_domain['id'];

$operational_competences = [];
// No operational competence associated
try
{
$operational_competences = $this->operational_comp_model->withDeleted()->whereIn('fk_competence_domain', $competenceDomainIds)->findAll();
}

catch (\Exception $e) {};

// Adds an acquisition status of level 1 for each objective
$objectiveIds = array();
foreach ($operational_competences as $operational_competence)
{
foreach ($this->operational_comp_model->getObjectives($operational_competence['id']) as $objective)
$objectiveIds[] = $objective['id'];
}

$operational_competences = [];
// No operational competence associated
try {
$operational_competences = $this->operational_comp_model->withDeleted()->whereIn('fk_competence_domain', $competenceDomainIds)->findAll();
} catch (\Exception $e) {
};
// Adds an acquisition status of level 1 for each objective
$objectiveIds = array();
foreach ($operational_competences as $operational_competence) {
foreach ($this->operational_comp_model->getObjectives($operational_competence['id']) as $objective) {
$objectiveIds[] = $objective['id'];
foreach ($objectiveIds as $objectiveId)
{
$acquisition_status = array(
'fk_objective' => $objectiveId,
'fk_user_course' => $id_user_course,
'fk_acquisition_level' => 1
);

$this->acquisition_status_model->insert($acquisition_status);
}
}
foreach ($objectiveIds as $objectiveId) {
$acquisition_status = array(
'fk_objective' => $objectiveId,
'fk_user_course' => $id_user_course,
'fk_acquisition_level' => 1
);

$this->acquisition_status_model->insert($acquisition_status);
}
}
// Error handling
if ($this->user_course_model->errors() == null) {
// No error - redirects to apprentice page
return redirect()->to(base_url('plafor/apprentice/view_apprentice/' . $id_apprentice));
}

if($this->user_course_model->errors() == null)
return redirect()->to(base_url('plafor/apprentice/list_user_courses/' . $id_apprentice));
}

// Preparing data for the view
// Course plans
$course_plans = [];
$status = [];

foreach ($this->course_plan_model->findAll() as $courseplan)
$course_plans[$courseplan['id']] = $courseplan['official_name'];
// Status of user's course
$status = [];
foreach ($this->user_course_status_model->findAll() as $usercoursestatus) {

foreach ($this->user_course_status_model->findAll() as $usercoursestatus)
$status[$usercoursestatus['id']] = $usercoursestatus['name'];
}

// Data to send to the view
$output = array(
'title' => lang('plafor_lang.title_user_course_plan_link'),
'title' => lang('plafor_lang.title_add_user_course'),
'course_plans' => $course_plans,
'user_course' => $user_course,
'status' => $status,
Expand All @@ -294,9 +347,10 @@ public function save_user_course($id_apprentice = 0, $id_user_course = 0) {
);

return $this->display_view('Plafor\user_course/save', $output);
} else {
}

else
return $this->display_view('\User\errors\403error');
}
}

/**
Expand Down Expand Up @@ -345,16 +399,18 @@ public function save_apprentice_link($id_apprentice = 0, $id_link = 0) {
return redirect()->to(base_url("plafor/apprentice/view_apprentice/{$id_apprentice}"));
}
}
// It seems that the MY_model dropdown method can't return a filtered result
// so here we get every users that are trainer, then we create a array
// with the matching constitution

// Gets data of trainers for the dropdown menu
// Gets data of trainers for the dropdown menu BUT ignore the trainers who are
// already linked to the selected apprentice
$trainersRaw = $this->user_model->getTrainers();
$trainers = array();
$linked_apprentices = array();

foreach ($trainersRaw as $trainer){
$trainers[$trainer['id']] = $trainer['username'];
$linked_apprentices = $this->trainer_apprentice_model->getApprenticeIdsFromTrainer($trainer['id']);
if (is_null($linked_apprentices) || !in_array($id_apprentice, $linked_apprentices)){
$trainers[$trainer['id']] = $trainer['username'];
}
}

// Data to send to the view
Expand Down Expand Up @@ -662,9 +718,10 @@ public function view_user_course($id_user_course = 0) {

// Preparing data for the view
$user_course_status = $this->user_course_model->getUserCourseStatus($user_course['fk_status']);
$course_plan = $this->user_course_model->getCoursePlan($user_course['fk_course_plan']);
$course_plan = $this->user_course_model->getCoursePlan($user_course['fk_course_plan'], true);
$trainers_apprentice = $this->trainer_apprentice_model->where('fk_apprentice',$apprentice['id'])->findAll();


// If url parameters contains filter operationalCompetenceId
if ($this->request->getGet('operationalCompetenceId')!=null){
$objectives = [];
Expand Down Expand Up @@ -773,4 +830,4 @@ public function delete_user($user_id = 0, $action = 0) {
}
return $this->display_view('\User\errors\403error');
}
}
}
Loading

0 comments on commit 9f94bdd

Please sign in to comment.