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
///////////////////////////////////////////////////////////////////////////
3
//                                                                       //
4
// NOTICE OF COPYRIGHT                                                   //
5
//                                                                       //
6
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
7
//          http://moodle.org                                            //
8
//                                                                       //
9
// Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
10
//                                                                       //
11
// This program is free software; you can redistribute it and/or modify  //
12
// it under the terms of the GNU General Public License as published by  //
13
// the Free Software Foundation; either version 2 of the License, or     //
14
// (at your option) any later version.                                   //
15
//                                                                       //
16
// This program is distributed in the hope that it will be useful,       //
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
19
// GNU General Public License for more details:                          //
20
//                                                                       //
21
//          http://www.gnu.org/copyleft/gpl.html                         //
22
//                                                                       //
23
///////////////////////////////////////////////////////////////////////////
24
 
25
class data_field_radiobutton extends data_field_base {
26
 
27
    var $type = 'radiobutton';
28
    /**
29
     * priority for globalsearch indexing
30
     *
31
     * @var int
32
     */
33
    protected static $priority = self::HIGH_PRIORITY;
34
 
35
    public function supports_preview(): bool {
36
        return true;
37
    }
38
 
39
    public function get_data_content_preview(int $recordid): stdClass {
40
        $options = explode("\n", $this->field->param1);
41
        $options = array_map('trim', $options);
42
        $selected = $options[$recordid % count($options)];
43
        return (object)[
44
            'id' => 0,
45
            'fieldid' => $this->field->id,
46
            'recordid' => $recordid,
47
            'content' => $selected,
48
            'content1' => null,
49
            'content2' => null,
50
            'content3' => null,
51
            'content4' => null,
52
        ];
53
    }
54
 
55
    function display_add_field($recordid = 0, $formdata = null) {
56
        global $CFG, $DB, $OUTPUT;
57
 
58
        if ($formdata) {
59
            $fieldname = 'field_' . $this->field->id;
60
            if (isset($formdata->$fieldname)) {
61
                $content = $formdata->$fieldname;
62
            } else {
63
                $content = '';
64
            }
65
        } else if ($recordid) {
11 efrain 66
            $contentfield = $DB->get_field('data_content', 'content', ['fieldid' => $this->field->id, 'recordid' => $recordid]);
67
            $content = trim($contentfield ?? '');
1 efrain 68
        } else {
69
            $content = '';
70
        }
71
 
72
        $str = '<div title="' . s($this->field->description) . '">';
73
        $str .= '<fieldset><legend><span class="accesshide">' . s($this->field->name);
74
 
75
        if ($this->field->required) {
76
            $str .= '&nbsp;' . get_string('requiredelement', 'form') . '</span></legend>';
77
            $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
78
            $str .= html_writer::div($image, 'inline-req');
79
        } else {
80
            $str .= '</span></legend>';
81
        }
82
 
83
        $i = 0;
84
        $requiredstr = '';
85
        $options = explode("\n", $this->field->param1);
86
        foreach ($options as $radio) {
87
            $radio = trim($radio);
88
            if ($radio === '') {
89
                continue; // skip empty lines
90
            }
91
            $str .= '<input type="radio" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '" ';
92
            $str .= 'value="' . s($radio) . '" class="mod-data-input mr-1" ';
93
 
94
            if ($content == $radio) {
95
                // Selected by user.
96
                $str .= 'checked />';
97
            } else {
98
                $str .= '/>';
99
            }
100
 
101
            $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$radio.'</label><br />';
102
            $i++;
103
        }
104
        $str .= '</fieldset>';
105
        $str .= '</div>';
106
        return $str;
107
    }
108
 
109
    function display_search_field($value = '') {
110
        global $CFG, $DB;
111
 
112
        $varcharcontent = $DB->sql_compare_text('content', 255);
113
        $used = $DB->get_records_sql(
114
            "SELECT DISTINCT $varcharcontent AS content
115
               FROM {data_content}
116
              WHERE fieldid=?
117
             ORDER BY $varcharcontent", array($this->field->id));
118
 
119
        $options = array();
120
        if(!empty($used)) {
121
            foreach ($used as $rec) {
122
                $options[$rec->content] = $rec->content;  //Build following indicies from the sql.
123
            }
124
        }
125
        $return = html_writer::label(get_string('fieldtypelabel', "datafield_" . $this->type),
126
            'menuf_' . $this->field->id, false, array('class' => 'accesshide'));
127
        $return .= html_writer::select($options, 'f_'.$this->field->id, $value,
128
            array('' => 'choosedots'), array('class' => 'custom-select'));
129
        return $return;
130
    }
131
 
132
    public function parse_search_field($defaults = null) {
133
        $param = 'f_'.$this->field->id;
134
        if (empty($defaults[$param])) {
135
            $defaults = array($param => '');
136
        }
137
        return optional_param($param, $defaults[$param], PARAM_NOTAGS);
138
    }
139
 
140
    function generate_sql($tablealias, $value) {
141
        global $DB;
142
 
143
        static $i=0;
144
        $i++;
145
        $name = "df_radiobutton_$i";
146
        $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
147
 
148
        return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
149
    }
150
 
151
    /**
152
     * Check if a field from an add form is empty
153
     *
154
     * @param mixed $value
155
     * @param mixed $name
156
     * @return bool
157
     */
158
    function notemptyfield($value, $name) {
159
        return strval($value) !== '';
160
    }
161
 
162
    /**
163
     * Return the plugin configs for external functions.
164
     *
165
     * @return array the list of config parameters
166
     * @since Moodle 3.3
167
     */
168
    public function get_config_for_external() {
169
        // Return all the config parameters.
170
        $configs = [];
171
        for ($i = 1; $i <= 10; $i++) {
172
            $configs["param$i"] = $this->field->{"param$i"};
173
        }
174
        return $configs;
175
    }
176
}