Proyectos de Subversion Moodle

Rev

Rev 4 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4 ariadna 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
 * Point of View block configuration form definition.
19
 *
20
 * @package    block_point_view
21
 * @copyright  2020 Quentin Fombaron, 2021 Astor Bizard
22
 * @author     Quentin Fombaron <q.fombaron@outlook.fr>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die;
27
 
28
require_once(__DIR__ . '/locallib.php');
29
 
30
/**
31
 * block_point_view_edit_form Class
32
 *
33
 *
34
 * @package    block_point_view
35
 * @copyright  2020 Quentin Fombaron
36
 * @author     Quentin Fombaron <q.fombaron@outlook.fr>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class block_point_view_edit_form extends block_edit_form {
40
 
41
    /**
42
     * Configuration page
43
     *
44
     * @param MoodleQuickForm $mform
45
     */
46
    protected function specific_definition($mform) {
47
 
48
        global $COURSE, $OUTPUT, $DB;
49
 
50
        if (get_config('block_point_view', 'enable_point_views_admin')) {
51
 
52
            // Add block_point_view class to form element for styling,
53
            // as it is not done for the body element on block edition page.
54
            $mform->updateAttributes([ 'class' => $mform->getAttribute('class') . ' block_point_view' ]);
55
 
56
            $mform->addElement('header', 'general_header', get_string('blocksettings', 'block'));
57
 
58
            // Block content.
59
 
60
            $editoroptions = [ 'maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $this->block->context ];
61
            $mform->addElement(
62
                'editor',
63
                'config_text',
64
                get_string('contentinputlabel', 'block_point_view'),
65
                null,
66
                $editoroptions
67
            );
68
            $mform->setType('config_text', PARAM_RAW);
69
 
70
            $mform->addHelpButton('config_text', 'contentinputlabel', 'block_point_view');
71
 
72
            // Reaction activation.
73
            $mform->addElement('selectyesno', 'config_enable_point_views',
74
                    get_string('enablepoint_views', 'block_point_view'));
75
 
76
            $this->add_checkbox_with_help($mform, 'config_enable_point_views_new_modules', 'enableforfuturemodules', 1);
77
            $mform->disabledIf('config_enable_point_views_new_modules', 'config_enable_point_views', 'eq', 0);
78
 
79
            $this->add_checkbox_with_help($mform, 'config_show_other_users_reactions', 'showotherreactions', 1);
80
            $mform->disabledIf('config_show_other_users_reactions', 'config_enable_point_views', 'eq', 0);
81
 
82
            $this->add_checkbox_with_help($mform, 'config_highlight_activity_rows', 'highlightactivityrows', 1);
83
            $mform->disabledIf('config_highlight_activity_rows', 'config_enable_point_views', 'eq', 0);
84
 
85
            // Difficulty tracks activation.
86
            $mform->addElement('selectyesno', 'config_enable_difficultytracks',
87
                    get_string('enabledifficulties', 'block_point_view'));
88
 
89
            // Reactions and difficulty tracks configuration by activity.
90
 
91
            $mform->addElement('header', 'activities_header', get_string('header_activities', 'block_point_view'));
92
 
93
            $modinfo = get_fast_modinfo($COURSE->id, -1);
94
            $cms = $modinfo->cms;
95
            $modtypes = array_keys($modinfo->instances);
96
 
97
            if (empty($cms)) {
98
                $this->add_warning_message($mform, get_string('noactivity', 'block_point_view'));
99
            }
100
 
101
            // Enable/Disable by activity module type.
102
            foreach ($modtypes as $type) {
103
                $this->add_enable_disable_buttons($mform, '',
104
                        $type,
105
                        'enable_type', 'disable_type',
106
                        get_string('modulenameplural', $type),
107
                        'enable_disable_type',
108
                        'data-type="' . $type . '"',
109
                        [ 'class' => 'reactions' ]);
110
            }
111
 
112
            $oldsection = '';
113
            $sectionid = 0;
114
            // Enable/Disable by activity or section.
115
            foreach ($cms as $cm) {
116
 
117
                if ($cm->sectionnum != $oldsection) {
118
 
119
                    $sectionid++;
120
                    $sectionname = get_section_name($COURSE, $cm->sectionnum);
121
 
122
                    $this->add_enable_disable_buttons($mform, '<h4>' . $sectionname . '</h4>',
123
                            'sec' . $sectionid,
124
                            'enableall', 'disableall',
125
                            $sectionname,
126
                            'enable_disable_section',
127
                            'data-section="sec' . $sectionid . '"',
128
                            [ 'class' => 'pt-3' ]);
129
 
130
                    $oldsection = $cm->sectionnum;
131
                }
132
 
133
                $icon = $OUTPUT->pix_icon('icon', $cm->get_module_type_name(), $cm->modname,
134
                        [ 'class' => 'iconlarge activityicon' ]);
135
 
5 ariadna 136
                $this->add_activity_config($mform, $cm->id, $sectionid, $cm->modname, $icon . $cm->get_formatted_name());
4 ariadna 137
            }
138
 
139
            // Emoji configuration.
140
 
141
            $this->add_emoji_selection($mform);
142
 
143
            // Reaction reinitialisation.
144
            $mform->addElement('header', 'reset_header', get_string('resetreactionsheader', 'block_point_view'));
145
 
146
            $buttons = $this->get_action_button('cleanup_reactions', 'warning',
147
                    'cleanupcoursereactions', format_string($COURSE->fullname));
148
            $buttons .= $OUTPUT->help_icon('cleanupreactions', 'block_point_view');
149
            $buttons .= $this->get_action_button('reset_reactions', 'danger',
150
                    'resetcoursereactions', format_string($COURSE->fullname));
151
            $buttons .= $OUTPUT->help_icon('resetreactions', 'block_point_view');
152
 
153
            $mform->addElement('html', html_writer::div($buttons, 'mt-2 mb-3'));
154
 
155
            $cms = get_fast_modinfo($COURSE->id, -1)->cms;
156
            $cmshtml = '';
157
            if (count($cms)) {
158
                $sectionid = -1;
159
                foreach ($cms as $cm) {
160
                    if (!$DB->record_exists('block_point_view', [ 'courseid' => $COURSE->id, 'cmid' => $cm->id ])) {
161
                        continue;
162
                    }
163
                    if ($cm->section != $sectionid) {
164
                        if ($sectionid != -1) {
165
                            $cmshtml .= '</div>'; // Close section.
166
                        }
167
                        $sectionid = $cm->section;
168
                        // Open section.
169
                        $cmshtml .= '<div class="pl-3"><h3>' . get_section_name($COURSE->id, $cm->sectionnum) . '</h3>';
170
                    }
171
                    $icon = $OUTPUT->image_icon('icon', $cm->modfullname, $cm->modname, [ 'class' => 'activityicon' ]);
172
                    $resetbutton = $this->get_action_button('', 'danger', 'resetreactions', null,
173
                            'data-cmid="' . $cm->id    . '" data-role="reset_module"');
174
                    $cmshtml .= html_writer::div($icon . $cm->get_formatted_name() . $resetbutton, 'mb-1');
175
                }
176
            }
177
            if (empty($cmshtml)) {
178
                $cmshtml = get_string('noreactionsyet', 'block_point_view');
179
            } else {
180
                $cmshtml .= '</div>'; // Close section.
181
            }
182
            $mform->addElement('html', html_writer::div(
183
                    html_writer::tag('legend', get_string('resetreactionsbymodule', 'block_point_view')) . $cmshtml, 'ml-3 mb-2'));
184
 
185
            // Call javascript from a static function in locallib, because Moodle linter won't let us call global $PAGE from here
186
            // (and $this->page actually contains the course page, not the edit form page).
187
            block_point_view_require_edit_form_javascript($this->block->context->id);
5 ariadna 188
 
189
        } else {
190
            $this->add_warning_message($mform, get_string('blockdisabled', 'block_point_view'));
4 ariadna 191
        }
192
    }
193
 
194
    /**
195
     * Helper function to add an advcheckbox element with a help button to a form.
196
     *
197
     * @param MoodleQuickForm $mform
198
     * @param string $name Checkbox element name.
199
     * @param string $str String identifier for label and help button (in block_point_view component).
200
     * @param mixed $default Default value for the checkbox.
201
     */
202
    protected function add_checkbox_with_help(&$mform, $name, $str, $default = 0) {
203
        $mform->addElement('advcheckbox', $name, get_string($str, 'block_point_view'));
204
        $mform->addHelpButton($name, $str, 'block_point_view');
205
        $mform->setDefault($name, $default);
206
    }
207
 
208
    /**
209
     * Helper function to add two buttons (enable/disable) to a form.
210
     *
211
     * @param MoodleQuickForm $mform
212
     * @param string $grouplabel The label to put before the two buttons.
213
     * @param string $name The name of the buttons (their "id" will be "enableall<$name>" and "disableall<$name>").
214
     * @param string $enablestr String identifier for enable button label (in block_point_view component).
215
     * @param string $disablestr String identifier for disable button label (in block_point_view component).
216
     * @param string $a Additional data to pass to get_string for enable and disable button labels.
217
     * @param string $helpstr String identifier for help button (in block_point_view component).
218
     * @param string $dataattr Data attributes for both buttons.
219
     * @param array $attributes Attributes to be added to the form element containing both buttons.
220
     */
221
    protected function add_enable_disable_buttons(&$mform, $grouplabel, $name,
222
            $enablestr, $disablestr, $a, $helpstr, $dataattr = '', $attributes = []) {
223
 
224
        global $OUTPUT;
225
 
226
        $templatecontext = new stdClass();
227
        $templatecontext->helpbutton = $OUTPUT->help_icon($helpstr, 'block_point_view');
228
        $templatecontext->enablename = 'enableall' . $name;
229
        $templatecontext->enablelabel = get_string($enablestr, 'block_point_view', $a);
230
        $templatecontext->disablename = 'disableall' . $name;
231
        $templatecontext->disablelabel = get_string($disablestr, 'block_point_view', $a);
232
        $templatecontext->dataattr = $dataattr;
233
        $element = &$mform->addElement('static', '', $grouplabel,
234
                $OUTPUT->render_from_template('block_point_view/enabledisable', $templatecontext));
235
        if (!empty($attributes)) {
236
            $element->setAttributes($attributes);
237
        }
238
    }
239
 
240
    /**
241
     * Helper function to create settings for one course module in the form (reactions checkbox and difficulty track select).
242
     *
243
     * @param MoodleQuickForm $mform
244
     * @param int $cmid Course module id.
245
     * @param int $sectionid Section id this module belongs to.
246
     * @param string $type Course module type name.
247
     * @param string $label Label for form elements (likely, course module name and icon).
248
     */
249
    protected function add_activity_config(&$mform, $cmid, $sectionid, $type, $label) {
250
        $group = [];
251
 
252
        // Checkbox for reactions.
253
        $group[] =& $mform->createElement( 'advcheckbox', 'config_moduleselectm' . $cmid,
254
                get_string('reactions', 'block_point_view'), null,
255
                [
256
                        'class' => 'reactions enablemodulereactions cbsec' . $sectionid . ' cb' . $type,
257
                        'data-section' => 'sec' . $sectionid,
258
                        'data-type' => $type,
259
                ],
260
                [ 0, $cmid ]
261
        );
262
 
263
        // Difficulty track.
264
        $group[] =& $mform->createElement( 'html',
265
                '<span id="track_' . $cmid . '" class="block_point_view track selecttrack difficultytracks"></span>' );
266
 
267
        // Difficulty track select.
268
        $group[] =& $mform->createElement( 'select', 'config_difficulty_' . $cmid, '',
269
                [
270
                        get_string('nonetrack', 'block_point_view'),
271
                        get_string('greentrack', 'block_point_view'),
272
                        get_string('bluetrack', 'block_point_view'),
273
                        get_string('redtrack', 'block_point_view'),
274
                        get_string('blacktrack', 'block_point_view'),
275
                ],
276
                [ 'class' => 'difficultytracks moduletrackselect', 'data-id' => $cmid ]
277
        );
278
 
279
        $mform->addGroup( $group, 'config_activity_' . $cmid, $label, '', false );
280
 
281
    }
282
 
283
    /**
284
     * Helper function to add a warning to a form.
285
     * @param MoodleQuickForm $mform
286
     * @param string $message
287
     */
288
    protected function add_warning_message(&$mform, $message) {
289
        $warning = html_writer::tag( 'div', $message, ['class' => 'warning'] );
290
        $mform->addElement('static', '', '', $warning);
291
    }
292
 
293
    /**
294
     * Add all form controls for emoji selection to the form.
295
     * @param MoodleQuickForm $mform
296
     */
297
    protected function add_emoji_selection(&$mform) {
298
        global $CFG;
299
 
300
        $mform->addElement('header', 'images_header', get_string('header_images', 'block_point_view'));
301
 
302
        $pixfiles = [ 'easy', 'better', 'hard' ];
303
 
304
        $adminpixenabled = get_config('block_point_view', 'enable_pix_admin');
305
        $custompixexist = false;
306
 
307
        // List existing pix. Three options:
308
        // - default pix (in blocks/point_view/pix),
309
        // - admin pix (in block administration settings),
310
        // - custom pix (in block configuration).
311
        $pix = [ 'default' => [], 'admin' => [], 'custom' => [] ];
312
        foreach ($pixfiles as $file) {
313
            $defaultsrc = $CFG->wwwroot . '/blocks/point_view/pix/' . $file . '.png';
314
            $pix['default'][$file] = $defaultsrc;
315
 
316
            if ($adminpixenabled) {
317
                $adminsrc = block_point_view_pix_url(1, 'point_views_pix_admin', $file);
318
                if (!$adminsrc) {
319
                    $adminsrc = $defaultsrc;
320
                }
321
                $pix['admin'][$file] = $adminsrc;
322
            }
323
 
324
            $customsrc = block_point_view_pix_url($this->block->context->id, 'point_views_pix', $file);
325
            if ($customsrc) {
326
                $custompixexist = true;
327
            } else {
328
                $customsrc = isset($adminsrc) ? $adminsrc : $defaultsrc;
329
            }
330
            $pix['custom'][$file] = $customsrc;
331
        }
332
 
333
        if ($custompixexist) {
334
            $deletecustombutton = $this->get_action_button('delete_custom_pix', 'warning', 'delete_custom_pix');
335
        } else {
336
            $pix['custom'] = [];
337
            $deletecustombutton = null;
338
        }
339
 
340
        // Create select for the three options.
341
        $pixselect = [];
342
        $pixselect[] = &$mform->createElement('html', '<div class="pixselectgroup">');
343
        $this->create_emoji_radioselect($mform, $pixselect, 'default', $pix);
344
        if ($adminpixenabled) {
345
            $this->create_emoji_radioselect($mform, $pixselect, 'admin', $pix);
346
        }
347
        $this->create_emoji_radioselect($mform, $pixselect, 'custom', $pix, $deletecustombutton);
348
        $pixselect[] = &$mform->createElement('html', '</div>');
349
 
350
        $mform->addGroup($pixselect, 'pixselectgroup', get_string('emojitouse', 'block_point_view'), '', false);
351
        $mform->setDefault('config_pixselect', $adminpixenabled ? 'admin' : 'default');
352
        $mform->addHelpButton('pixselectgroup', 'emojitouse', 'block_point_view');
353
 
354
        // Create file manager for custom emoji.
355
        $mform->addElement(
356
                'filemanager',
357
                'config_point_views_pix',
358
                get_string('customemoji', 'block_point_view'),
359
                null,
360
                [ 'subdirs' => 0, 'maxfiles' => 11, 'accepted_types' => '.png' ]
361
                );
362
 
363
        $mform->addHelpButton('config_point_views_pix', 'customemoji', 'block_point_view');
364
        $mform->disabledIf('config_point_views_pix', 'config_pixselect', 'neq', 'custom');
365
 
366
        // Create fields for custom reaction text.
367
        $current = block_point_view_get_current_pix($this->block, $pixfiles);
368
        foreach ($pixfiles as $file) {
369
 
370
            $elementname = 'config_pix_text_' . $file;
371
            $defaulttext = get_string('defaulttext' . $file, 'block_point_view');
372
 
373
            $mform->addElement('text',
374
                    $elementname,
375
                    html_writer::empty_tag('img', [
376
                            'src' => $current[$file],
377
                            'class' => 'pix-preview currentpix my-1',
378
                            'alt' => $defaulttext,
379
                            'data-reaction' => $file,
380
                    ]) .
381
                    get_string('emojidesc', 'block_point_view')
382
                    );
383
 
384
            $mform->setDefault($elementname, $defaulttext);
385
            $mform->setType($elementname, PARAM_RAW);
386
            $mform->addHelpButton($elementname, 'emojidesc', 'block_point_view');
387
 
388
        }
389
    }
390
 
391
    /**
392
     * Helper function to create a radio select element for emoji and add it to a form.
393
     *
394
     * @param MoodleQuickForm $mform
395
     * @param Html_Common[] $group Array to which the element should be added.
396
     * @param string $value
397
     * @param string[][] $pix Array of pix sources.
398
     * @param string|null $additionallegend Optional html to add after the emoji.
399
     */
400
    protected function create_emoji_radioselect(&$mform, &$group, $value, $pix, $additionallegend = null) {
401
        $group[] = $mform->createElement('radio', 'config_pixselect', '',
402
                get_string($value . 'pix', 'block_point_view'), $value, [ 'class' => 'pr-2 mr-0 w-100 justify-content-start' ]);
403
 
404
        $legend = '<label for="id_config_pixselect_' . $value . '" class="d-inline-block">';
405
        foreach ($pix[$value] as $file => $src) {
406
            $legend .= html_writer::empty_tag('img', [
407
                    'src' => $src,
408
                    'class' => 'pix-preview my-1',
409
                    'data-reaction' => $file,
410
                    'data-source' => $value,
411
            ]);
412
        }
413
        $legend .= '</label>';
414
        if ($additionallegend !== null) {
415
            $legend = '<span>' . $legend . $additionallegend . '</span>';
416
        }
417
        $group[] = $mform->createElement('html', $legend);
418
    }
419
 
420
    /**
421
     * Helper function to create an action button.
422
     *
423
     * @param string $id Button id.
424
     * @param string $type Button outline type (e.g. 'warning', 'danger'...).
425
     * @param string $str String identifier for the button label (in block_point_view component).
426
     * @param string|null $a Additional data to pass to get_string for button label.
427
     * @param string $dataattributes HTML data attributes.
428
     * @return string HTML fragment for the button.
429
     */
430
    protected function get_action_button($id, $type, $str, $a = null, $dataattributes = '') {
431
        return '<button ' . ($id ? 'id="' . $id . '"' : '') . 'class="btn btn-outline-' . $type . ' ml-3 mr-1"
432
                type="button" ' . $dataattributes . '>' .
433
                   get_string($str, 'block_point_view', $a) .
434
               '</button>';
435
    }
436
 
437
    /**
438
     * Validation of filemanager files
439
     *
440
     * @param array $data
441
     * @param array $files
442
     * @return array
443
     */
444
    public function validation($data, $files) {
445
 
446
        global $USER;
447
 
448
        $errors = [];
449
 
450
        if (isset($data['config_pixselect']) && $data['config_pixselect'] == 'custom') {
451
 
452
            $fs = get_file_storage();
453
 
454
            $usercontext = context_user::instance($USER->id);
455
 
456
            $expected = [
457
                'easy',
458
                'better',
459
                'hard',
460
                'group_',
461
                'group_E',
462
                'group_B',
463
                'group_H',
464
                'group_EB',
465
                'group_EH',
466
                'group_BH',
467
                'group_EBH',
468
            ];
469
 
470
            $draftfiles = $fs->get_area_files(
471
                $usercontext->id,
472
                'user',
473
                'draft',
474
                $data['config_point_views_pix'],
475
                'filename',
476
                false
477
                );
478
 
479
            if (empty($draftfiles)) {
480
                $errors['config_point_views_pix'] = get_string('errorfilemanagerempty', 'block_point_view');
481
            }
482
 
483
            foreach ($draftfiles as $file) {
484
 
485
                $pathinfo = pathinfo($file->get_filename());
486
 
487
                if (!in_array($pathinfo['filename'], $expected, true)) {
488
 
489
                    if (!isset($errors['config_point_views_pix'])) {
490
                        $errors['config_point_views_pix'] = '';
491
                    }
492
 
493
                    $errors['config_point_views_pix'] .= get_string(
494
                        'errorfilemanager',
495
                        'block_point_view',
496
                        $pathinfo['filename']
497
                        ) . '<br />';
498
                }
499
            }
500
        }
501
 
502
        return $errors;
503
    }
504
 
505
    /**
506
     * File manager and Editor data
507
     *
508
     * @param array $defaults
509
     */
510
    public function set_data($defaults) {
511
 
512
        $text = '';
513
        if (!empty($this->block->config) && is_object($this->block->config)) {
514
            $text = $this->block->config->text;
515
            $draftideditor = file_get_submitted_draft_itemid('config_text');
516
            if (empty($text)) {
517
                $currenttext = '';
518
            } else {
519
                $currenttext = $text;
520
            }
521
            $defaults->config_text['text'] = file_prepare_draft_area(
522
                $draftideditor,
523
                $this->block->context->id,
524
                'block_point_view',
525
                'content',
526
                0,
527
                [ 'subdirs' => true ],
528
                $currenttext
529
            );
530
            $defaults->config_text['itemid'] = $draftideditor;
531
            $defaults->config_text['format'] = $this->block->config->format;
532
 
533
            $draftidpix = file_get_submitted_draft_itemid('config_point_views_pix');
534
 
535
            file_prepare_draft_area(
536
                $draftidpix,
537
                $this->block->context->id,
538
                'block_point_view',
539
                'point_views_pix',
540
                0,
541
                [
542
                    'subdirs' => 0,
543
                    'maxfiles' => 20,
544
                    'accepted_types' => [ '.png' ],
545
                ]
546
                );
547
 
548
            $defaults->config_point_views_pix = $draftidpix;
549
 
550
            $this->block->config->point_views_pix = $draftidpix;
551
        }
552
 
553
        unset($this->block->config->text);
554
 
555
        if (!get_config('block_point_view', 'enable_pix_admin')
556
                && isset($this->block->config->pixselect)
557
                && $this->block->config->pixselect == 'admin') {
558
            $this->block->config->pixselect = 'default';
559
        }
560
 
561
        parent::set_data($defaults);
562
 
563
        if (!isset($this->block->config)) {
564
            $this->block->config = new stdClass();
565
        }
566
        $this->block->config->text = $text;
567
 
568
    }
569
}