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
namespace customfield_select;
18
 
19
use coding_exception;
20
 
21
/**
22
 * Class field
23
 *
24
 * @package customfield_select
25
 * @copyright 2018 David Matamoros <davidmc@moodle.com>
26
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class field_controller extends \core_customfield\field_controller {
29
    /**
30
     * Customfield type
31
     */
32
    const TYPE = 'select';
33
 
34
    /**
35
     * Add fields for editing a select field.
36
     *
37
     * @param \MoodleQuickForm $mform
38
     */
39
    public function config_form_definition(\MoodleQuickForm $mform) {
40
        $mform->addElement('header', 'header_specificsettings', get_string('specificsettings', 'customfield_select'));
41
        $mform->setExpanded('header_specificsettings', true);
42
 
43
        $mform->addElement('textarea', 'configdata[options]', get_string('menuoptions', 'customfield_select'));
44
        $mform->setType('configdata[options]', PARAM_TEXT);
45
 
46
        $mform->addElement('text', 'configdata[defaultvalue]', get_string('defaultvalue', 'core_customfield'), 'size="50"');
47
        $mform->setType('configdata[defaultvalue]', PARAM_TEXT);
48
    }
49
 
50
    /**
51
     * @deprecated since Moodle 3.10 - MDL-68569 please use $field->get_options
52
     */
53
    public static function get_options_array(): void {
54
        throw new coding_exception('get_options_array() is deprecated, please use $field->get_options() instead');
55
    }
56
 
57
    /**
58
     * Return configured field options
59
     *
60
     * @return array
61
     */
62
    public function get_options(): array {
63
        $optionconfig = $this->get_configdata_property('options');
64
        if ($optionconfig) {
11 efrain 65
            $context = $this->get_handler()->get_configuration_context();
66
            $options = array_map(
67
                fn(string $option) => format_string($option, true, ['context' => $context]),
68
                preg_split("/\s*\n\s*/", trim($optionconfig), -1, PREG_SPLIT_NO_EMPTY),
69
            );
1 efrain 70
        } else {
71
            $options = array();
72
        }
73
        return array_merge([''], $options);
74
    }
75
 
76
    /**
77
     * Validate the data from the config form.
78
     * Sub classes must reimplement it.
79
     *
80
     * @param array $data from the add/edit profile field form
81
     * @param array $files
82
     * @return array associative array of error messages
83
     */
84
    public function config_form_validation(array $data, $files = array()): array {
85
        $options = preg_split("/\s*\n\s*/", trim($data['configdata']['options']));
86
        $errors = [];
87
        if (!$options || count($options) < 2) {
88
            $errors['configdata[options]'] = get_string('errornotenoughoptions', 'customfield_select');
89
        } else if (!empty($data['configdata']['defaultvalue'])) {
90
            $defaultkey = array_search($data['configdata']['defaultvalue'], $options);
91
            if ($defaultkey === false) {
92
                $errors['configdata[defaultvalue]'] = get_string('errordefaultvaluenotinlist', 'customfield_select');
93
            }
94
        }
95
        return $errors;
96
    }
97
 
98
    /**
99
     * Does this custom field type support being used as part of the block_myoverview
100
     * custom field grouping?
101
     * @return bool
102
     */
103
    public function supports_course_grouping(): bool {
104
        return true;
105
    }
106
 
107
    /**
108
     * If this field supports course grouping, then this function needs overriding to
109
     * return the formatted values for this.
110
     * @param array $values the used values that need formatting
111
     * @return array
112
     */
113
    public function course_grouping_format_values($values): array {
114
        $options = $this->get_options();
115
        $ret = [];
116
        foreach ($values as $value) {
117
            if (isset($options[$value])) {
11 efrain 118
                $ret[$value] = $options[$value];
1 efrain 119
            }
120
        }
121
        $ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
122
            $this->get_formatted_name());
123
        return $ret;
124
    }
125
 
126
    /**
127
     * Locate the value parameter in the field options array, and return it's index
128
     *
129
     * @param string $value
130
     * @return int
131
     */
132
    public function parse_value(string $value) {
133
        return (int) array_search($value, $this->get_options());
134
    }
135
}