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_text;
18
 
19
/**
20
 * Class data
21
 *
1441 ariadna 22
 * @package   customfield_text
1 efrain 23
 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
24
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class data_controller extends \core_customfield\data_controller {
27
 
28
    /**
29
     * Return the name of the field where the information is stored
30
     * @return string
31
     */
32
    public function datafield(): string {
33
        return 'charvalue';
34
    }
35
 
36
    /**
37
     * Add fields for editing a text field.
38
     *
39
     * @param \MoodleQuickForm $mform
40
     */
41
    public function instance_form_definition(\MoodleQuickForm $mform) {
42
        $field = $this->get_field();
43
        $config = $field->get('configdata');
44
        $type = $config['ispassword'] ? 'password' : 'text';
45
        $elementname = $this->get_form_element_name();
46
        $mform->addElement($type, $elementname, $this->get_field()->get_formatted_name(), 'size=' . (int)$config['displaysize']);
47
        $mform->setType($elementname, PARAM_TEXT);
48
        if (!empty($config['defaultvalue'])) {
49
            $mform->setDefault($elementname, $config['defaultvalue']);
50
        }
51
        if ($field->get_configdata_property('required')) {
52
            $mform->addRule($elementname, null, 'required', null, 'client');
53
        }
54
    }
55
 
56
    /**
57
     * Validates data for this field.
58
     *
59
     * @param array $data
60
     * @param array $files
61
     * @return array
62
     */
63
    public function instance_form_validation(array $data, array $files): array {
64
 
65
        $errors = parent::instance_form_validation($data, $files);
66
        $maxlength = $this->get_field()->get_configdata_property('maxlength');
67
        $elementname = $this->get_form_element_name();
68
        if (($maxlength > 0) && ($maxlength < \core_text::strlen($data[$elementname]))) {
69
            $errors[$elementname] = get_string('errormaxlength', 'customfield_text', $maxlength);
70
        }
71
        return $errors;
72
    }
73
 
74
    /**
75
     * Returns the default value as it would be stored in the database (not in human-readable format).
76
     *
77
     * @return mixed
78
     */
79
    public function get_default_value() {
80
        return $this->get_field()->get_configdata_property('defaultvalue');
81
    }
82
 
83
    /**
84
     * Returns value in a human-readable format
85
     *
86
     * @return mixed|null value or null if empty
87
     */
88
    public function export_value() {
89
        $value = parent::export_value();
90
        if ($value === null) {
91
            return null;
92
        }
93
 
94
        $link = $this->get_field()->get_configdata_property('link');
95
        if ($link) {
1441 ariadna 96
            $url = new \moodle_url(str_replace('$$', $this->get_value(), $link));
1 efrain 97
            $linktarget = $this->get_field()->get_configdata_property('linktarget');
98
            $attributes = $linktarget ? ['target' => $linktarget] : [];
99
            $value = \html_writer::link($url, $value, $attributes);
100
        }
101
 
102
        return $value;
103
    }
104
}