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
/**
18
 * The chooser renderable.
19
 *
20
 * @package    core
21
 * @copyright  2016 Frédéric Massart - FMCorz.net
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core\output;
26
 
27
use lang_string;
28
use moodle_url;
29
use stdClass;
30
 
31
/**
32
 * The chooser renderable class.
33
 *
34
 * @package    core
35
 * @copyright  2016 Frédéric Massart - FMCorz.net
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class chooser implements renderable, templatable {
39
    /** @var moodle_url The form action URL. */
40
    public $actionurl;
41
    /** @var lang_string The instructions to display. */
42
    public $instructions;
43
    /** @var string The form method. */
44
    public $method = 'post';
45
    /** @var string The name of the parameter for the items value. */
46
    public $paramname;
47
    /** @var array The list of hidden parameters. See {@link self::add_param}. */
48
    public $params = [];
49
    /** @var chooser_section[] The sections */
50
    public $sections;
51
    /** @var lang_string The chooser title. */
52
    public $title;
53
 
54
    /**
55
     * Constructor.
56
     *
57
     * @param moodle_url $actionurl The form action URL.
58
     * @param lang_string $title The title of the chooser.
59
     * @param chooser_section[] $sections The sections.
60
     * @param string $paramname The name of the parameter for the items value.
61
     */
62
    public function __construct(moodle_url $actionurl, lang_string $title, array $sections, $paramname) {
63
        $this->actionurl = $actionurl;
64
        $this->title = $title;
65
        $this->sections = $sections;
66
        $this->paramname = $paramname;
67
    }
68
 
69
    /**
70
     * Add a parameter to submit with the form.
71
     *
72
     * @param string $name The parameter name.
73
     * @param string $value The parameter value.
74
     * @param string $id The parameter ID.
75
     */
76
    public function add_param($name, $value, $id = null) {
77
        if (!$id) {
78
            $id = $name;
79
        }
80
        $this->params[] = [
81
            'name' => $name,
82
            'value' => $value,
1441 ariadna 83
            'id' => $id,
1 efrain 84
        ];
85
    }
86
 
87
    /**
88
     * Set the chooser instructions.
89
     *
90
     * @param lang_string $value The instructions.
91
     */
92
    public function set_instructions(lang_string $value) {
93
        $this->instructions = $value;
94
    }
95
 
96
    /**
97
     * Set the form method.
98
     *
99
     * @param string $value The method.
100
     */
101
    public function set_method($value) {
102
        $this->method = $value;
103
    }
104
 
105
    /**
106
     * Export for template.
107
     *
108
     * @param renderer_base The renderer.
109
     * @return stdClass
110
     */
111
    public function export_for_template(renderer_base $output) {
112
        $data = new stdClass();
113
 
114
        $data->actionurl = $this->actionurl->out(false);
115
        $data->instructions = (string) $this->instructions;
116
        $data->method = $this->method;
117
        $data->paramname = $this->paramname;
118
        $data->params = $this->params;
119
        $data->sesskey = sesskey();
120
        $data->title = (string) $this->title;
121
 
1441 ariadna 122
        $data->sections = array_map(function ($section) use ($output) {
1 efrain 123
            return $section->export_for_template($output);
124
        }, $this->sections);
125
 
126
        return $data;
127
    }
128
}