<?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'une évaluation.
 *
 * @package    local_kid
 */

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

use local_kid\local\access_helper;
use local_kid\local\competency_repository;
use local_kid\local\eval_repository;

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

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

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

$PAGE->set_url('/local/kid/pages/eval_edit.php', ['id' => $id, 'promotionid' => $promotionid]);
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$title = $eval ? get_string('eval_edit_title', 'local_kid') : get_string('eval_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);
}

$categoryoptions = [];
if (!$eval) {
    $categoryoptions[0] = get_string('eval_devoir_category_placeholder', 'local_kid');
    $categoryoptions += \core_course_category::make_categories_list();
}

$form = new local_kid_eval_form($PAGE->url, [
    'isediting' => (bool) $eval,
    'categoryoptions' => $categoryoptions,
    'competencyoptions' => $competencyoptions,
]);

if ($eval) {
    $existingcompetencies = array_keys(eval_repository::get_competencies($id));
    $form->set_data([
        'id' => $eval->id,
        'promotionid' => $promotionid,
        'titre' => $eval->titre,
        'competencyids' => $existingcompetencies,
    ]);
} else {
    $form->set_data(['promotionid' => $promotionid]);
}

if ($form->is_cancelled()) {
    redirect($returnurl);
} else if ($data = $form->get_data()) {
    if ($data->id) {
        eval_repository::update((int) $data->id, $data->titre, $data->competencyids, $USER->id);
    } else {
        eval_repository::create($promotionid, $data->titre, $data->sourcetype,
            $data->sourcetype === 'moodle_devoir' ? (int) $data->cmid : null, $data->competencyids, $USER->id);
    }
    redirect($returnurl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
}

if (!$eval) {
    $PAGE->requires->js_call_amd('local_kid/eval_devoir_picker', 'init');
}

$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();
