<?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.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Surveypro pluginform class.
 *
 * @package   surveyprofield_age
 * @copyright 2013 onwards kordan <stringapiccola@gmail.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace surveyprofield_age;

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

use mod_surveypro\local\form\item_setupbaseform;

require_once($CFG->dirroot . '/lib/formslib.php');
require_once($CFG->dirroot . '/mod/surveypro/field/age/lib.php');

/**
 * The class representing the plugin form
 *
 * @package   surveyprofield_age
 * @copyright 2013 onwards kordan <stringapiccola@gmail.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class itemsetupform extends item_setupbaseform
{
    /**
     * Definition.
     *
     * @return void
     */
    public function definition() {
        // Start with common section of the form.
        parent::definition();

        $mform = $this->_form;

        // Get _customdata.
        // Useless: $item = $this->_customdata['item'];.

        $maximumage = get_config('surveyprofield_age', 'maximumage');

        $yearsrange = range(0, $maximumage);
        $years = array_combine($yearsrange, $yearsrange);
        $monthsrange = range(0, 11);
        $months = array_combine($monthsrange, $monthsrange);

        // Item: defaultoption.
        $fieldname = 'defaultoption';
        $strcustomdefault = get_string('customdefault', 'surveyprofield_age');
        $strinvitedefault = get_string('invitedefault', 'mod_surveypro');
        $strnoanswer = get_string('noanswer', 'mod_surveypro');
        $elementgroup = [];
        $elementgroup[] = $mform->createElement('radio', $fieldname, '', $strcustomdefault, SURVEYPRO_CUSTOMDEFAULT);
        $elementgroup[] = $mform->createElement('radio', $fieldname, '', $strinvitedefault, SURVEYPRO_INVITEDEFAULT);
        $elementgroup[] = $mform->createElement('radio', $fieldname, '', $strnoanswer, SURVEYPRO_NOANSWERDEFAULT);
        $mform->addGroup($elementgroup, $fieldname . '_group', get_string($fieldname, 'surveyprofield_age'), ' ', false);
        $mform->setDefault($fieldname, SURVEYPRO_INVITEDEFAULT);
        $mform->addHelpButton($fieldname . '_group', $fieldname, 'surveyprofield_age');

        // Item: defaultvalue.
        $fieldname = 'defaultvalue';
        $elementgroup = [];
        $elementgroup[] = $mform->createElement('select', $fieldname . 'year', '', $years);
        $elementgroup[] = $mform->createElement('select', $fieldname . 'month', '', $months);
        $mform->addGroup($elementgroup, $fieldname . '_group', null, ' ', false);
        $mform->disabledIf($fieldname . '_group', 'defaultoption', 'neq', SURVEYPRO_CUSTOMDEFAULT);
        $mform->setDefault($fieldname . 'year', '0');
        $mform->setDefault($fieldname . 'month', '0');

        // Here I open a new fieldset.
        $fieldname = 'validation';
        $mform->addElement('header', $fieldname, get_string($fieldname, 'mod_surveypro'));

        // Item: lowerbound.
        $fieldname = 'lowerbound';
        $elementgroup = [];
        $elementgroup[] = $mform->createElement('select', $fieldname . 'year', '', $years);
        $elementgroup[] = $mform->createElement('select', $fieldname . 'month', '', $months);
        $mform->addGroup($elementgroup, $fieldname . '_group', get_string($fieldname, 'surveyprofield_age'), ' ', false);
        $mform->addHelpButton($fieldname . '_group', $fieldname, 'surveyprofield_age');
        $mform->setDefault($fieldname . 'year', '0');
        $mform->setDefault($fieldname . 'month', '0');

        // Item: upperbound.
        $fieldname = 'upperbound';
        $elementgroup = [];
        $elementgroup[] = $mform->createElement('select', $fieldname . 'year', '', $years);
        $elementgroup[] = $mform->createElement('select', $fieldname . 'month', '', $months);
        $mform->addGroup($elementgroup, $fieldname . '_group', get_string($fieldname, 'surveyprofield_age'), ' ', false);
        $mform->addHelpButton($fieldname . '_group', $fieldname, 'surveyprofield_age');
        $mform->setDefault($fieldname . 'year', $maximumage);
        $mform->setDefault($fieldname . 'month', '11');

        $this->add_item_buttons();
    }

    /**
     * Validation.
     *
     * @param array $data
     * @param array $files
     * @return array $errors
     */
    public function validation($data, $files) {
        // Get _customdata.
        $item = $this->_customdata['item'];

        $errors = parent::validation($data, $files);

        // Editing teacher can not set "noanswer" as default option if the item is mandatory.
        if (($data['defaultoption'] == SURVEYPRO_NOANSWERDEFAULT) && isset($data['required'])) {
            $a = get_string('noanswer', 'mod_surveypro');
            $errors['defaultoption_group'] = get_string('ierr_notalloweddefault', 'mod_surveypro', $a);
        }

        $lowerbound = $item->item_age_to_unix_time($data['lowerboundyear'], $data['lowerboundmonth']);
        $upperbound = $item->item_age_to_unix_time($data['upperboundyear'], $data['upperboundmonth']);
        if ($lowerbound == $upperbound) {
            $errors['lowerbound_group'] = get_string('ierr_lowerequaltoupper', 'surveyprofield_age');
        }
        if ($lowerbound > $upperbound) {
            $errors['lowerbound_group'] = get_string('ierr_lowergreaterthanupper', 'surveyprofield_age');
        }

        // Constrain default between boundaries.
        if ($data['defaultoption'] == SURVEYPRO_CUSTOMDEFAULT) {
            $defaultvalue = $item->item_age_to_unix_time($data['defaultvalueyear'], $data['defaultvaluemonth']);

            // Internal range.
            if (($defaultvalue < $lowerbound) || ($defaultvalue > $upperbound)) {
                $errors['defaultvalue_group'] = get_string('ierr_outofrangedefault', 'surveyprofield_age');
            }
        }

        return $errors;
    }
}
