2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Classes representing JS event handlers, used by output components.
20 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
25 * @copyright 2009 Nicolas Connault
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
32 * Helper class used by other components that involve an action on the page (URL or JS).
34 * @copyright 2009 Nicolas Connault
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class component_action
{
43 * @var string $event The DOM event that will trigger this action when caught
48 * @var string A function name to call when the button is clicked
49 * The JS function you create must have two arguments:
51 * 2. An object/array of arguments ($jsfunctionargs)
53 public $jsfunction = false;
56 * @var array An array of arguments to pass to the JS function
58 public $jsfunctionargs = array();
62 * @param string $event DOM event
63 * @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
64 * @param array $jsfunctionargs An array of arguments to pass to the jsfunction
66 public function __construct($event, $jsfunction, $jsfunctionargs=array()) {
67 $this->event
= $event;
69 $this->jsfunction
= $jsfunction;
70 $this->jsfunctionargs
= $jsfunctionargs;
72 if (!empty($this->jsfunctionargs
)) {
73 if (empty($this->jsfunction
)) {
74 throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
84 * @copyright 2009 Nicolas Connault
85 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
90 class confirm_action
extends component_action
{
92 * Constructs the confirm action object
94 * @param string $message The message to display to the user when they are shown
95 * the confirm dialogue.
96 * @param string $callback Deprecated since 2.7
97 * @param string $continuelabel The string to use for he continue button
98 * @param string $cancellabel The string to use for the cancel button
100 public function __construct($message, $callback = null, $continuelabel = null, $cancellabel = null) {
101 if ($callback !== null) {
102 debugging('The callback argument to new confirm_action() has been deprecated.' .
103 ' If you need to use a callback, please write Javascript to use moodle-core-notification-confirmation ' .
104 'and attach to the provided events.',
107 parent
::__construct('click', 'M.util.show_confirm_dialog', array(
108 'message' => $message,
109 'continuelabel' => $continuelabel, 'cancellabel' => $cancellabel));
115 * Component action for a popup window.
117 * @copyright 2009 Nicolas Connault
118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
123 class popup_action
extends component_action
{
126 * @var string The JS function to call for the popup
128 public $jsfunction = 'openpopup';
131 * @var array An array of parameters that will be passed to the openpopup JS function
133 public $params = array(
140 'scrollbars' => true,
144 'directories' => false,
145 'fullscreen' => false,
146 'dependent' => true);
151 * @param string $event DOM event
152 * @param moodle_url|string $url A moodle_url object, required if no jsfunction is given
153 * @param string $name The JS function to call for the popup (default 'popup')
154 * @param array $params An array of popup parameters
156 public function __construct($event, $url, $name='popup', $params=array()) {
160 $url = new moodle_url($url);
163 $_name = $this->name
;
164 if (($_name = preg_replace("/\s/", '_', $_name)) != $this->name
) {
165 throw new coding_exception('The $name of a popup window shouldn\'t contain spaces - string modified. '. $this->name
.' changed to '. $_name);
166 $this->name
= $_name;
169 $this->name
= 'popup';
172 foreach ($this->params
as $var => $val) {
173 if (array_key_exists($var, $params)) {
174 $this->params
[$var] = $params[$var];
178 $attributes = array('url' => $url->out(false), 'name' => $name, 'options' => $this->get_js_options($params));
179 if (!empty($params['fullscreen'])) {
180 $attributes['fullscreen'] = 1;
182 parent
::__construct($event, $this->jsfunction
, $attributes);
186 * Returns a string of concatenated option->value pairs used by JS to call the popup window,
187 * based on this object's variables
189 * @return string String of option->value pairs for JS popup function.
191 public function get_js_options() {
194 foreach ($this->params
as $var => $val) {
195 if (is_string($val) ||
is_int($val)) {
196 $jsoptions .= "$var=$val,";
197 } elseif (is_bool($val)) {
198 $jsoptions .= ($val) ?
"$var," : "$var=0,";
202 $jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);