Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 29... Línea 29...
29
require_once($CFG->dirroot.'/repository/lib.php');
29
require_once($CFG->dirroot.'/repository/lib.php');
Línea 30... Línea 30...
30
 
30
 
Línea 31... Línea 31...
31
class data_field_textarea extends data_field_base {
31
class data_field_textarea extends data_field_base {
-
 
32
 
-
 
33
    var $type = 'textarea';
-
 
34
 
-
 
35
    /**
-
 
36
     * Prefix for the field name to split field id and the random part.
-
 
37
     * @var string
-
 
38
     */
32
 
39
    protected const RND_PREFIX = 'xZx';
33
    var $type = 'textarea';
40
 
34
    /**
41
    /**
35
     * priority for globalsearch indexing
42
     * priority for globalsearch indexing
36
     *
43
     *
37
     * @var int
44
     * @var int
Línea -... Línea 45...
-
 
45
     */
-
 
46
    protected static $priority = self::LOW_PRIORITY;
-
 
47
 
-
 
48
    /**
-
 
49
     * The field name for the content field.
-
 
50
     *
-
 
51
     * @var string
-
 
52
     */
-
 
53
    protected $fieldname = null;
-
 
54
 
-
 
55
    /**
-
 
56
     * Returns a random field name for the content field.
-
 
57
     *
-
 
58
     * @return string
-
 
59
     */
-
 
60
    protected function get_content_field_name() {
-
 
61
        if ($this->fieldname === null) {
-
 
62
            $this->fieldname = sprintf(
-
 
63
                'field_%s_%s%s',
-
 
64
                $this->field->id,
-
 
65
                self::RND_PREFIX,
-
 
66
                bin2hex(random_bytes(5))
-
 
67
            );
-
 
68
        }
-
 
69
        return $this->fieldname;
-
 
70
    }
-
 
71
 
-
 
72
    /**
-
 
73
     * Check if the given field name is the content field that contains the actual data.
-
 
74
     *
-
 
75
     * @param string $name
-
 
76
     * @return bool
-
 
77
     */
-
 
78
    protected function is_content_field(string $name): bool {
38
     */
79
        return (strlen($name) === 13 && substr($name, 0, 3) === self::RND_PREFIX);
39
    protected static $priority = self::LOW_PRIORITY;
80
    }
40
 
81
 
Línea 41... Línea 82...
41
    public function supports_preview(): bool {
82
    public function supports_preview(): bool {
Línea 80... Línea 121...
80
        global $CFG, $DB, $OUTPUT, $PAGE;
121
        global $CFG, $DB, $OUTPUT, $PAGE;
Línea 81... Línea 122...
81
 
122
 
82
        $text   = '';
123
        $text   = '';
83
        $format = 0;
124
        $format = 0;
84
        $str = '<div title="' . s($this->field->description) . '" class="d-inline-flex">';
125
        $str = '<div title="' . s($this->field->description) . '" class="d-inline-flex">';
85
        $str .= '<label for="field_' . $this->field->id . '">';
126
        $str .= '<label for="' . $this->get_content_field_name() . '">';
86
        $str .= html_writer::span($this->field->name, 'accesshide');
127
        $str .= html_writer::span($this->field->name, 'accesshide');
87
        if ($this->field->required) {
128
        if ($this->field->required) {
88
            $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
129
            $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
89
            $str .= html_writer::div($image, 'inline-req');
130
            $str .= html_writer::div($image, 'inline-req');
Línea 107... Línea 148...
107
            if (isset($formdata->$fieldname)) {
148
            if (isset($formdata->$fieldname)) {
108
                $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
149
                $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
109
            } else {
150
            } else {
110
                $draftitemid = file_get_unused_draft_itemid();
151
                $draftitemid = file_get_unused_draft_itemid();
111
            }
152
            }
112
            $fieldname = 'field_' . $this->field->id;
153
            $fieldname = 'field_' . $this->field->id . '_' . self::RND_PREFIX;
-
 
154
            foreach (array_keys(get_object_vars($formdata)) as $prop) {
113
            if (isset($formdata->$fieldname)) {
155
                if (strpos($prop, $fieldname) === 0) {
114
                $text = $formdata->$fieldname;
156
                    $text = $formdata->$prop;
-
 
157
                    break;
-
 
158
                }
115
            }
159
            }
116
        } else if ($recordid &&
160
        } else if ($recordid &&
117
                   $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
161
                   $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
118
            $format = $content->content1;
162
            $format = $content->content1;
119
            $text = clean_text($content->content, $format);
163
            $text = clean_text($content->content, $format);
Línea 179... Línea 223...
179
        $formats =  $editor->get_supported_formats();
223
        $formats =  $editor->get_supported_formats();
180
        foreach ($formats as $fid) {
224
        foreach ($formats as $fid) {
181
            $formats[$fid] = $strformats[$fid];
225
            $formats[$fid] = $strformats[$fid];
182
        }
226
        }
183
        $editor->set_text($text);
227
        $editor->set_text($text);
184
        $editor->use_editor($field, $options, $fpoptions);
228
        $editor->use_editor($this->get_content_field_name(), $options, $fpoptions);
185
        $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
229
        $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
186
        $str .= '<div class="mod-data-input">';
230
        $str .= '<div class="mod-data-input">';
187
        $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" class="form-control" ' .
231
        $str .= '<div><textarea id="' . $this->get_content_field_name() . '" name="' . $this->get_content_field_name() . '" ' .
188
            'data-fieldtype="editor" ' .
232
            'rows="'.$this->field->param3 . '" class="form-control" data-fieldtype="editor" ' .
189
            'cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
233
            'cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
190
        $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
234
        $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
191
        $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
235
        $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1" class="form-select mt-2">';
192
        foreach ($formats as $key=>$desc) {
236
        foreach ($formats as $key=>$desc) {
193
            $selected = ($format == $key) ? 'selected="selected"' : '';
237
            $selected = ($format == $key) ? 'selected="selected"' : '';
194
            $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
238
            $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
195
        }
239
        }
196
        $str .= '</select>';
240
        $str .= '</select>';
Línea 199... Línea 243...
199
        $str .= '</div>';
243
        $str .= '</div>';
200
        $str .= '</div>';
244
        $str .= '</div>';
201
        return $str;
245
        return $str;
202
    }
246
    }
Línea 203... Línea -...
203
 
-
 
204
 
247
 
205
    function display_search_field($value = '') {
248
    function display_search_field($value = '') {
206
        return '<label class="accesshide" for="f_' . $this->field->id . '">' . s($this->field->name) . '</label>' .
249
        return '<label class="accesshide" for="f_' . $this->field->id . '">' . s($this->field->name) . '</label>' .
207
               '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
250
               '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
208
               'value="' . s($value) . '" class="form-control"/>';
251
               'value="' . s($value) . '" class="form-control"/>';
Línea 239... Línea 282...
239
        $names = explode('_', $name);
282
        $names = explode('_', $name);
240
        if (!empty($names[2])) {
283
        if (!empty($names[2])) {
241
            if ($names[2] == 'itemid') {
284
            if ($names[2] == 'itemid') {
242
                // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
285
                // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
243
                return true;
286
                return true;
-
 
287
            } else if ($this->is_content_field($names[2])) {
-
 
288
                $content->content = clean_param($value, PARAM_RAW_TRIMMED);
244
            } else {
289
            } else {
245
                $content->{$names[2]} = clean_param($value, PARAM_NOTAGS);  // content[1-4]
290
                $content->{$names[2]} = clean_param($value, PARAM_NOTAGS);  // content[1-4]
246
            }
291
            }
247
        } else {
292
        } else {
248
            $content->content = clean_param($value, PARAM_CLEAN);
293
            $content->content = clean_param($value, PARAM_RAW_TRIMMED);
249
        }
294
        }
Línea 250... Línea 295...
250
 
295
 
251
        if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
296
        if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
252
            $content->id = $oldcontent->id;
297
            $content->id = $oldcontent->id;
Línea 317... Línea 362...
317
        // Clean first.
362
        // Clean first.
318
        if (count($names) == 2) {
363
        if (count($names) == 2) {
319
            // Don't assume that this is coming from a text editor with tags.
364
            // Don't assume that this is coming from a text editor with tags.
320
            return strval($value) !== '';
365
            return strval($value) !== '';
321
        }
366
        }
-
 
367
        if ($this->is_content_field($names[2])) {
-
 
368
            return strval($value) !== '';
-
 
369
        }
322
        return false;
370
        return false;
323
    }
371
    }
Línea 324... Línea 372...
324
 
372
 
325
    /**
373
    /**