Proyectos de Subversion Moodle

Rev

| 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
/**
18
 * Test page for choice dropdown field type.
19
 *
20
 * @copyright 2023 Ferran Recio <ferran@moodle.com>
21
 * @package   core_form
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
use core\output\choicelist;
26
 
27
require_once(__DIR__ . '/../../../../../config.php');
28
 
29
defined('BEHAT_SITE_RUNNING') || die();
30
 
31
global $CFG, $PAGE, $OUTPUT;
32
require_once($CFG->libdir . '/formslib.php');
33
$PAGE->set_url('/lib/form/tests/behat/fixtures/field_choicedropdown_testpage.php');
34
$PAGE->add_body_class('limitedwidth');
35
require_login();
36
$PAGE->set_context(core\context\system::instance());
37
 
38
/**
39
 * Class test_choice_dropdown
40
 * @package core_form
41
 */
42
class test_choice_dropdown extends moodleform {
43
    /**
44
     * Define the export form.
45
     */
46
    public function definition() {
47
        $mform = $this->_form;
48
 
49
        $options = new choicelist();
50
        $options->set_allow_empty(false);
51
        $options->add_option('option1', "Text option 1", [
52
            'description' => 'Option 1 description',
53
            'icon' => new pix_icon('t/hide', 'Eye icon 1'),
54
        ]);
55
        $options->add_option('option2', "Text option 2", [
56
            'description' => 'Option 2 description',
57
            'icon' => new pix_icon('t/stealth', 'Eye icon 2'),
58
        ]);
59
        $options->add_option('option3', "Text option 3", [
60
            'description' => 'Option 3 description',
61
            'icon' => new pix_icon('t/show', 'Eye icon 3'),
62
        ]);
63
 
64
        $mform->addElement('header', 'database', "Basic example");
65
        $mform->addElement('choicedropdown', 'example0', "Basic choice dropdown", $options);
66
 
67
        $mform->addElement('header', 'database', "Disable choice dropdown");
68
        $mform->addElement('checkbox', 'disableme', 'Check to disable the first choice dropdown field.');
69
        $mform->addElement('choicedropdown', 'example1', "Disable if example", $options);
70
        $mform->disabledIf('example1', 'disableme', 'checked');
71
 
72
        $mform->addElement('header', 'database', "Hide choice dropdown");
73
        $mform->addElement('checkbox', 'hideme', 'Check to hide the first choice dropdown field.');
74
        $mform->addElement('choicedropdown', 'example2', "Hide if example", $options);
75
        $mform->hideIf('example2', 'hideme', 'checked');
76
 
77
        $options = new choicelist();
78
        $options->set_allow_empty(false);
79
        $options->add_option('hide', 'Hide or disable subelements');
80
        $options->add_option('show', 'Show or enable subelements');
81
 
82
        $mform->addElement('header', 'database', "Use choice dropdown to hide or disable other fields");
83
        $mform->addElement('choicedropdown', 'example3', "Control choice dropdown", $options);
84
 
85
        $mform->addElement('text', 'hideinput', 'Hide if element', ['maxlength' => 80, 'size' => 50]);
86
        $mform->hideIf('hideinput', 'example3', 'eq', 'hide');
87
        $mform->setDefault('hideinput', 'Is this visible?');
88
        $mform->setType('hideinput', PARAM_TEXT);
89
 
90
        $mform->addElement('text', 'disabledinput', 'Disabled if element', ['maxlength' => 80, 'size' => 50]);
91
        $mform->disabledIf('disabledinput', 'example3', 'eq', 'hide');
92
        $mform->setDefault('disabledinput', 'Is this enabled?');
93
        $mform->setType('disabledinput', PARAM_TEXT);
94
 
95
        $this->add_action_buttons(false, 'Send form');
96
    }
97
}
98
 
99
echo $OUTPUT->header();
100
 
101
echo "<h2>Quickform integration test</h2>";
102
 
103
$form = new test_choice_dropdown();
104
 
105
$data = $form->get_data();
106
if ($data) {
107
    echo "<h3>Submitted data</h3>";
108
    echo '<div id="submitted_data"><ul>';
109
    $data = (array) $data;
110
    foreach ($data as $field => $value) {
111
        echo "<li id=\"sumbmitted_{$field}\">$field: $value</li>";
112
    }
113
    echo '</ul></div>';
114
}
115
$form->display();
116
 
117
echo $OUTPUT->footer();