<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

/**
 * Ajout ou modification d'un stage.
 *
 * @package    local_kid
 */

require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot . '/local/kid/lib.php');
require_once($CFG->dirroot . '/local/kid/classes/form/stage_form.php');

use local_kid\local\access_helper;
use local_kid\local\competency_repository;
use local_kid\local\stage_repository;

require_login();
$context = context_system::instance();
access_helper::require_capability_or_teacher('local/kid:manage_stage', $context);

$id = optional_param('id', 0, PARAM_INT);
$promotionid = required_param('promotionid', PARAM_INT);
$stage = $id ? stage_repository::get($id) : null;
if ($id && !$stage) {
    throw new \moodle_exception('invalidrecord', 'error');
}

$returnurl = new moodle_url('/local/kid/pages/admin.php', ['tab' => 'stages', 'promotionid' => $promotionid]);

$PAGE->set_url('/local/kid/pages/stage_edit.php', ['id' => $id, 'promotionid' => $promotionid]);
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$title = $stage ? get_string('stage_edit_title', 'local_kid') : get_string('stage_add', 'local_kid');
$PAGE->set_title($title);
local_kid_setup_page($PAGE);

$competencyoptions = [];
foreach (competency_repository::get_active_for_promotion($promotionid) as $competency) {
    $competencyoptions[$competency->id] = $competency->codecourt . ' — ' . competency_repository::label($competency);
}

$form = new local_kid_stage_form($PAGE->url, [
    'isediting' => (bool) $stage,
    'competencyoptions' => $competencyoptions,
]);

if ($stage) {
    $existingcompetencies = array_keys(stage_repository::get_scope_competencies($stage));
    $form->set_data([
        'id' => $stage->id,
        'promotionid' => $promotionid,
        'libelle' => $stage->libelle,
        'datedebut' => $stage->datedebut ?: 0,
        'datefin' => $stage->datefin ?: 0,
        'competencyscope' => $stage->competencyscope,
        'competencyids' => $existingcompetencies,
        'active' => $stage->active,
    ]);
} else {
    $form->set_data(['promotionid' => $promotionid]);
}

if ($form->is_cancelled()) {
    redirect($returnurl);
} else if ($data = $form->get_data()) {
    $datedebut = !empty($data->datedebut) ? (int) $data->datedebut : null;
    $datefin = !empty($data->datefin) ? (int) $data->datefin : null;
    $competencyids = $data->competencyscope === 'custom' ? (array) $data->competencyids : [];

    if ($data->id) {
        stage_repository::update((int) $data->id, $data->libelle, $datedebut, $datefin, $data->competencyscope, $competencyids, $USER->id);
    } else {
        stage_repository::create($promotionid, $data->libelle, $datedebut, $datefin, $data->competencyscope, $competencyids, $USER->id);
    }
    redirect($returnurl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
}

$renderer = $PAGE->get_renderer('local_kid');
echo $OUTPUT->header();
echo $renderer->render_shell(
    \local_kid\local\nav_helper::get_sidebar_items('admin'),
    $OUTPUT->heading($title, 2) . $form->render()
);
echo $OUTPUT->footer();
