3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Classes representing JS event handlers, used by output components.
21 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
26 * @copyright 2009 Nicolas Connault
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') ||
die();
33 * Helper class used by other components that involve an action on the page (URL or JS).
35 * @copyright 2009 Nicolas Connault
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class component_action
{
42 * The DOM event that will trigger this action when caught
43 * @var string $event DOM event
48 * The JS function you create must have two arguments:
50 * 2. An object/array of arguments ($jsfunctionargs)
51 * @var string $jsfunction A function name to call when the button is clicked
53 public $jsfunction = false;
56 * @var array $jsfunctionargs An array of arguments to pass to the JS function
58 public $jsfunctionargs = array();
62 * @param string $event DOM event
63 * @param moodle_url $url A moodle_url object, required if no jsfunction is given
64 * @param string $method 'post' or 'get'
65 * @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
66 * @param array $jsfunctionargs An array of arguments to pass to the jsfunction
69 public function __construct($event, $jsfunction, $jsfunctionargs=array()) {
70 $this->event
= $event;
72 $this->jsfunction
= $jsfunction;
73 $this->jsfunctionargs
= $jsfunctionargs;
75 if (!empty($this->jsfunctionargs
)) {
76 if (empty($this->jsfunction
)) {
77 throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
87 class confirm_action
extends component_action
{
88 public function __construct($message, $callback=null) {
89 parent
::__construct('click', 'M.util.show_confirm_dialog', array('message' => $message, 'callback' => $callback));
95 * Component action for a popup window.
97 * @copyright 2009 Nicolas Connault
98 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
101 class popup_action
extends component_action
{
103 public $jsfunction = 'openpopup';
106 * @var array $params An array of parameters that will be passed to the openpopup JS function
108 public $params = array(
115 'scrollbars' => true,
119 'directories' => false,
120 'fullscreen' => false,
121 'dependent' => true);
125 * @param string $event DOM event
126 * @param moodle_url|string $url A moodle_url object, required if no jsfunction is given
127 * @param string $method 'post' or 'get'
128 * @param array $params An array of popup parameters
131 public function __construct($event, $url, $name='popup', $params=array()) {
135 $url = new moodle_url($url);
138 $_name = $this->name
;
139 if (($_name = preg_replace("/\s/", '_', $_name)) != $this->name
) {
140 throw new coding_exception('The $name of a popup window shouldn\'t contain spaces - string modified. '. $this->name
.' changed to '. $_name);
141 $this->name
= $_name;
144 $this->name
= 'popup';
147 foreach ($this->params
as $var => $val) {
148 if (array_key_exists($var, $params)) {
149 $this->params
[$var] = $params[$var];
153 $attributes = array('url' => $url->out(false), 'name' => $name, 'options' => $this->get_js_options($params));
154 if (!empty($params['fullscreen'])) {
155 $attributes['fullscreen'] = 1;
157 parent
::__construct($event, $this->jsfunction
, $attributes);
161 * Returns a string of concatenated option->value pairs used by JS to call the popup window,
162 * based on this object's variables
164 * @return string String of option->value pairs for JS popup function.
166 public function get_js_options() {
169 foreach ($this->params
as $var => $val) {
170 if (is_string($val) ||
is_int($val)) {
171 $jsoptions .= "$var=$val,";
172 } elseif (is_bool($val)) {
173 $jsoptions .= ($val) ?
"$var," : "$var=0,";
177 $jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);