Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 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
namespace core\output\actions;
18
 
19
use core\exception\coding_exception;
20
use core\output\renderer_base;
21
use core\output\templatable;
22
use stdClass;
23
 
24
/**
25
 * Helper class used by other components that involve an action on the page (URL or JS).
26
 *
27
 * @copyright 2009 Nicolas Connault
28
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @since Moodle 2.0
30
 * @package core
31
 * @category output
32
 */
33
class component_action implements templatable {
34
    /**
35
     * @var string $event The DOM event that will trigger this action when caught
36
     */
37
    public $event;
38
 
39
    /**
40
     * @var string A function name to call when the button is clicked
41
     * The JS function you create must have two arguments:
42
     *      1. The event object
43
     *      2. An object/array of arguments ($jsfunctionargs)
44
     */
45
    public $jsfunction = false;
46
 
47
    /**
48
     * @var array An array of arguments to pass to the JS function
49
     */
50
    public $jsfunctionargs = [];
51
 
52
    /**
53
     * Constructor
54
     * @param string $event DOM event
55
     * @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
56
     * @param array $jsfunctionargs An array of arguments to pass to the jsfunction
57
     */
58
    public function __construct($event, $jsfunction, $jsfunctionargs = []) {
59
        $this->event = $event;
60
 
61
        $this->jsfunction = $jsfunction;
62
        $this->jsfunctionargs = $jsfunctionargs;
63
 
64
        if (!empty($this->jsfunctionargs)) {
65
            if (empty($this->jsfunction)) {
66
                throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
67
            }
68
        }
69
    }
70
 
71
    /**
72
     * Export for template.
73
     *
74
     * @param renderer_base $output The renderer.
75
     * @return stdClass
76
     */
77
    public function export_for_template(renderer_base $output) {
78
        $args = !empty($this->jsfunctionargs) ? json_encode($this->jsfunctionargs) : false;
79
        return (object) [
80
            'event' => $this->event,
81
            'jsfunction' => $this->jsfunction,
82
            'jsfunctionargs' => $args,
83
        ];
84
    }
85
}
86
 
87
// Alias this class to the old name.
88
// This file will be autoloaded by the legacyclasses autoload system.
89
// In future all uses of this class will be corrected and the legacy references will be removed.
90
class_alias(component_action::class, \component_action::class);