1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Class core_customfield_test_instance_form
19 *
20 * @package     core_customfield
21 * @copyright   2019 Marina Glancy
22 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->libdir . '/formslib.php');
29
30/**
31 * Class core_customfield_test_instance_form
32 *
33 * @package     core_customfield
34 * @copyright   2019 Marina Glancy
35 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_customfield_test_instance_form extends moodleform {
38    /** @var \core_customfield\handler */
39    protected $handler;
40
41    /** @var stdClass */
42    protected $instance;
43
44    /**
45     * Form definition
46     */
47    public function definition() {
48        $this->handler = $this->_customdata['handler'];
49        $this->instance = $this->_customdata['instance'];
50
51        $this->_form->addElement('hidden', 'id');
52        $this->_form->setType('id', PARAM_INT);
53
54        $this->handler->instance_form_definition($this->_form, $this->instance->id);
55
56        $this->add_action_buttons();
57
58        $this->handler->instance_form_before_set_data($this->instance);
59        $this->set_data($this->instance);
60    }
61
62    /**
63     * Definition after data
64     */
65    public function definition_after_data() {
66        $this->handler->instance_form_definition_after_data($this->_form, $this->instance->id);
67    }
68
69    /**
70     * Form validation
71     *
72     * @param array $data
73     * @param array $files
74     * @return array
75     */
76    public function validation($data, $files) {
77        return $this->handler->instance_form_validation($data, $files);
78    }
79}
80