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

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/formslib.php');

class local_kid_eval_form extends moodleform {

    protected function definition() {
        $mform = $this->_form;
        $isediting = !empty($this->_customdata['isediting']);

        $mform->addElement('hidden', 'id');
        $mform->setType('id', PARAM_INT);
        $mform->addElement('hidden', 'promotionid');
        $mform->setType('promotionid', PARAM_INT);

        $mform->addElement('text', 'titre', get_string('eval_titre', 'local_kid'), ['size' => 60]);
        $mform->setType('titre', PARAM_TEXT);
        $mform->addRule('titre', get_string('required'), 'required', null, 'client');

        if (!$isediting) {
            $mform->addElement('select', 'sourcetype', get_string('eval_sourcetype', 'local_kid'), [
                'manual' => get_string('eval_sourcetype_manual', 'local_kid'),
                'moodle_devoir' => get_string('eval_sourcetype_devoir', 'local_kid'),
            ]);

            // Cascading category → course → devoir picker (amd/src/eval_devoir_picker.js):
            // only the category list is rendered server-side (bounded,
            // no dependency); course and cmid start as a single placeholder
            // option and are filled via AJAX once their parent is chosen —
            // see local_kid_get_courses_for_category/local_kid_get_assigns_for_course.
            $categoryoptions = $this->_customdata['categoryoptions'];
            $mform->addElement('select', 'assigncategoryid', get_string('eval_devoir_category', 'local_kid'), $categoryoptions,
                ['data-role' => 'kid-assign-category']);
            $mform->setType('assigncategoryid', PARAM_INT);
            $mform->hideIf('assigncategoryid', 'sourcetype', 'eq', 'manual');

            // assigncourseid only narrows the devoir list below — its own
            // submitted value is never read after that (only cmid is
            // persisted), so it doesn't need the hidden-field treatment
            // below.
            $mform->addElement('select', 'assigncourseid', get_string('eval_devoir_course', 'local_kid'),
                [0 => get_string('eval_devoir_course_placeholder_initial', 'local_kid')],
                ['data-role' => 'kid-assign-course', 'data-placeholder-ready' => get_string('eval_devoir_course_placeholder_ready', 'local_kid')]);
            $mform->setType('assigncourseid', PARAM_INT);
            $mform->hideIf('assigncourseid', 'sourcetype', 'eq', 'manual');

            // The visible devoir select is deliberately NOT the field that
            // gets read (cmid_picker, not cmid): HTML_QuickForm_select::
            // exportValue() silently drops any submitted value that isn't
            // one of the <option>s the PHP side declared when the form was
            // built — since real devoir options only exist because
            // amd/src/eval_devoir_picker.js appends them client-side after
            // an AJAX call, PHP would otherwise null out whatever the user
            // picked, even in a real browser with JS working correctly. A
            // plain hidden 'cmid' field (no such option-list filtering)
            // carries the real value instead; the JS keeps it in sync with
            // the visible select (classes/form/eval_form.php pairs with
            // amd/src/eval_devoir_picker.js's cmidPicker/cmidValue).
            $mform->addElement('select', 'cmid_picker', get_string('eval_devoir', 'local_kid'),
                [0 => get_string('eval_devoir_placeholder_initial', 'local_kid')],
                ['data-role' => 'kid-assign-cmid', 'data-placeholder-ready' => get_string('eval_devoir_placeholder_ready', 'local_kid')]);
            $mform->hideIf('cmid_picker', 'sourcetype', 'eq', 'manual');

            $mform->addElement('hidden', 'cmid', '', ['data-role' => 'kid-assign-cmid-value']);
            $mform->setType('cmid', PARAM_INT);
        }

        $competencyoptions = $this->_customdata['competencyoptions'];
        $mform->addElement('select', 'competencyids', get_string('eval_competencies', 'local_kid'), $competencyoptions,
            ['multiple' => 'multiple', 'size' => min(10, count($competencyoptions))]);
        $mform->addRule('competencyids', get_string('required'), 'required', null, 'client');

        $this->add_action_buttons();
    }

    /**
     * cmid can't carry a client-side "required" rule (it starts empty and is
     * only ever filled by amd/src/eval_devoir_picker.js) — enforced here
     * instead, only when adding a new évaluation sourced from a Devoir
     * Moodle (editing never touches this field; manual entries ignore it).
     */
    public function validation($data, $files) {
        $errors = parent::validation($data, $files);

        if (empty($data['id']) && ($data['sourcetype'] ?? null) === 'moodle_devoir' && empty($data['cmid'])) {
            $errors['cmid'] = get_string('eval_devoir_required', 'local_kid');
        }

        return $errors;
    }
}
