<?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.

/**
 * Suppression d'une compétence (bloquée si référencée quelque part).
 *
 * @package    local_kid
 */

require(__DIR__ . '/../../../config.php');

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

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

$id = required_param('id', PARAM_INT);
$promotionid = required_param('promotionid', PARAM_INT);
$action = required_param('action', PARAM_ALPHA);
require_sesskey();

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

if ($action === 'delete') {
    $deleted = competency_repository::delete($id);
    if ($deleted) {
        redirect($returnurl, get_string('competency_deleted', 'local_kid'), null, \core\output\notification::NOTIFY_SUCCESS);
    }
    redirect($returnurl, get_string('competency_delete_blocked', 'local_kid'), null, \core\output\notification::NOTIFY_ERROR);
}

throw new \moodle_exception('invalidparameter', 'error');
