Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Select plugin data controller
19
 *
20
 * @package   customfield_select
21
 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace customfield_select;
26
 
27
defined('MOODLE_INTERNAL') || die;
28
 
29
/**
30
 * Class data
31
 *
32
 * @package customfield_select
33
 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class data_controller extends \core_customfield\data_controller {
37
 
38
    /**
39
     * Return the name of the field where the information is stored
40
     * @return string
41
     */
42
    public function datafield(): string {
43
        return 'intvalue';
44
    }
45
 
46
    /**
47
     * Returns the default value as it would be stored in the database (not in human-readable format).
48
     *
49
     * @return mixed
50
     */
51
    public function get_default_value() {
52
        $defaultvalue = $this->get_field()->get_configdata_property('defaultvalue');
53
        if ('' . $defaultvalue !== '') {
54
            $key = array_search($defaultvalue, $this->get_field()->get_options());
55
            if ($key !== false) {
56
                return $key;
57
            }
58
        }
59
        return 0;
60
    }
61
 
62
    /**
63
     * Add fields for editing a textarea field.
64
     *
65
     * @param \MoodleQuickForm $mform
66
     */
67
    public function instance_form_definition(\MoodleQuickForm $mform) {
68
        $field = $this->get_field();
69
        $config = $field->get('configdata');
70
        $options = $field->get_options();
71
 
72
        $elementname = $this->get_form_element_name();
11 efrain 73
        $mform->addElement('select', $elementname, $this->get_field()->get_formatted_name(), $options);
1 efrain 74
 
75
        if (($defaultkey = array_search($config['defaultvalue'], $options)) !== false) {
76
            $mform->setDefault($elementname, $defaultkey);
77
        }
78
        if ($field->get_configdata_property('required')) {
79
            $mform->addRule($elementname, null, 'required', null, 'client');
80
        }
81
    }
82
 
83
    /**
84
     * Validates data for this field.
85
     *
86
     * @param array $data
87
     * @param array $files
88
     * @return array
89
     */
90
    public function instance_form_validation(array $data, array $files): array {
91
        $errors = parent::instance_form_validation($data, $files);
92
        if ($this->get_field()->get_configdata_property('required')) {
93
            // Standard required rule does not work on select element.
94
            $elementname = $this->get_form_element_name();
95
            if (empty($data[$elementname])) {
96
                $errors[$elementname] = get_string('err_required', 'form');
97
            }
98
        }
99
        return $errors;
100
    }
101
 
102
    /**
103
     * Returns value in a human-readable format
104
     *
105
     * @return mixed|null value or null if empty
106
     */
107
    public function export_value() {
108
        $value = $this->get_value();
109
 
110
        if ($this->is_empty($value)) {
111
            return null;
112
        }
113
 
114
        $options = $this->get_field()->get_options();
115
        if (array_key_exists($value, $options)) {
11 efrain 116
            return $options[$value];
1 efrain 117
        }
118
 
119
        return null;
120
    }
121
}