MDL-29857 - portfolio: Fix E_STRICT errors
[moodle.git] / lib / portfolio / forms.php
CommitLineData
87fcac8d 1<?php
93dd2725
RW
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
87fcac8d 17/**
87fcac8d 18 * This file contains all the form definitions used by the portfolio code.
93dd2725
RW
19 *
20 * @package core_portfolio
21 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>,
22 * Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
87fcac8d 24 */
25
0972665f
PS
26defined('MOODLE_INTERNAL') || die();
27
87fcac8d 28// make sure we include moodleform first!
29require_once ($CFG->libdir.'/formslib.php');
30
31/**
93dd2725 32 * During-export config form.
18cdcdbf 33 *
93dd2725
RW
34 * This is the form that is actually used while exporting.
35 * Plugins and callers don't get to define their own class
36 * as we have to handle form elements from both places
37 * See the docs here for more information:
38 * http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_export_config
39 * http://docs.moodle.org/dev/Adding_a_Portfolio_Button_to_a_page#has_export_config
40 *
41 * @package core_portfolio
42 * @category portfolio
43 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
87fcac8d 46final class portfolio_export_form extends moodleform {
47
93dd2725
RW
48 /**
49 * prepare form
50 */
87fcac8d 51 public function definition() {
52
53 $mform =& $this->_form;
54 $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG);
c95a6095 55 $mform->addElement('hidden', 'id', $this->_customdata['id']);
87fcac8d 56 $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
d18e0fe6 57 $mform->setType('instance', PARAM_INT);
c95a6095
PL
58 $mform->setType('stage', PARAM_INT);
59 $mform->setType('id', PARAM_INT);
87fcac8d 60
61 if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) {
62 if (count($this->_customdata['formats']) > 1) {
63 $options = array();
64 foreach ($this->_customdata['formats'] as $key) {
65 $options[$key] = get_string('format_' . $key, 'portfolio');
66 }
67 $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
68 } else {
69 $f = array_shift($this->_customdata['formats']);
70 $mform->addElement('hidden', 'format', $f);
d18e0fe6 71 $mform->setType('format', PARAM_RAW);
87fcac8d 72 }
73 }
74
75 // only display the option to wait or not if it's applicable
76 if (array_key_exists('expectedtime', $this->_customdata)
77 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW
78 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) {
79 $radioarray = array();
f20edd52
PS
80 $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
81 $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0);
87fcac8d 82 $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
83 $mform->setDefault('wait', 0);
84 } else {
85 if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) {
86 $mform->addElement('hidden', 'wait', 1);
87 } else {
88 $mform->addElement('hidden', 'wait', 0);
89 }
d18e0fe6 90 $mform->setType('wait', PARAM_INT);
87fcac8d 91 }
92
93 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
94 $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
95 }
96
97 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
98 $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
99 }
100
101 $this->add_action_buttons(true, get_string('next'));
102 }
103
93dd2725
RW
104 /**
105 * Validate portfolio export form
106 *
107 * @param stdClass $data portfolio information from form data
108 * @return array
109 */
9b27ffa0 110 public function validation($data, $files) {
87fcac8d 111
112 $errors = array();
113
114 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
115 $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
116 if (is_array($pluginerrors)) {
117 $errors = $pluginerrors;
118 }
119 }
120 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
121 $callererrors = $this->_customdata['caller']->export_config_validation($data);
122 if (is_array($callererrors)) {
123 $errors = array_merge($errors, $callererrors);
124 }
125 }
126 return $errors;
127 }
128}
129
130/**
93dd2725 131 * Admin config form.
18cdcdbf 132 *
93dd2725
RW
133 * This form is extendable by plugins who want the admin to be able to configure more than just the name of the instance.
134 * This is NOT done by subclassing this class, see the docs for portfolio_plugin_base for more information:
135 * {@link http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_admin_config}
136 *
137 * @package core_portfolio
138 * @category portfolio
139 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
140 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
141 */
87fcac8d 142final class portfolio_admin_form extends moodleform {
143
93dd2725 144 /** @var object to hold porfolio instance configuration */
87fcac8d 145 protected $instance;
93dd2725
RW
146
147 /** @var string plugin name*/
87fcac8d 148 protected $plugin;
93dd2725
RW
149
150 /** @var string portfolio plugin name*/
6010eda2 151 protected $portfolio;
93dd2725
RW
152
153 /** @var string plugin availability*/
6010eda2 154 protected $action;
93dd2725
RW
155
156 /** @var int portfolio plugin visibility*/
6010eda2 157 protected $visible;
87fcac8d 158
93dd2725
RW
159 /**
160 * prepare form
161 */
87fcac8d 162 public function definition() {
163 global $CFG;
164 $this->plugin = $this->_customdata['plugin'];
165 $this->instance = (isset($this->_customdata['instance'])
166 && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
167 ? $this->_customdata['instance'] : null;
6010eda2
MD
168 $this->portfolio = $this->_customdata['portfolio'];
169 $this->action = $this->_customdata['action'];
170 $this->visible = $this->_customdata['visible'];
87fcac8d 171
172 $mform =& $this->_form;
173 $strrequired = get_string('required');
174
6010eda2
MD
175 $mform->addElement('hidden', 'pf', $this->portfolio);
176 $mform->setType('pf', PARAM_ALPHA);
177 $mform->addElement('hidden', 'action', $this->action);
178 $mform->setType('action', PARAM_ALPHA);
179 $mform->addElement('hidden', 'visible', $this->visible);
180 $mform->setType('visible', PARAM_INT);
87fcac8d 181 $mform->addElement('hidden', 'plugin', $this->plugin);
aff24313 182 $mform->setType('plugin', PARAM_PLUGIN);
87fcac8d 183
2726b405 184 if (!$this->instance) {
185 $insane = portfolio_instance_sanity_check($this->instance);
186 } else {
187 $insane = portfolio_plugin_sanity_check($this->plugin);
87fcac8d 188 }
189
190 if (isset($insane) && is_array($insane)) {
191 $insane = array_shift($insane);
192 }
193 if (isset($insane) && is_string($insane)) { // something went wrong, warn...
194 $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin));
195 }
196
197 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
198 $mform->addRule('name', $strrequired, 'required', null, 'client');
199
2726b405 200 // let the plugin add the fields they want (either statically or not)
201 if (portfolio_static_function($this->plugin, 'has_admin_config')) {
202 if (!$this->instance) {
b8824f38
PL
203 require_once($CFG->libdir . '/portfolio/plugin.php');
204 require_once($CFG->dirroot . '/portfolio/' . $this->plugin . '/lib.php');
205 call_user_func(array('portfolio_plugin_' . $this->plugin, 'admin_config_form'), $mform);
2726b405 206 } else {
207 $this->instance->admin_config_form($mform);
208 }
209 }
87fcac8d 210
211 // and set the data if we have some.
212 if ($this->instance) {
213 $data = array('name' => $this->instance->get('name'));
214 foreach ($this->instance->get_allowed_config() as $config) {
215 $data[$config] = $this->instance->get_config($config);
216 }
217 $this->set_data($data);
218 } else {
219 $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
220 }
221
222 $this->add_action_buttons(true, get_string('save', 'portfolio'));
223 }
224
93dd2725
RW
225 /**
226 * Validate admin config form
227 *
228 * @param stdObject $data form data
229 * @return array
230 */
9b27ffa0 231 public function validation($data, $files) {
87fcac8d 232 global $DB;
233
234 $errors = array();
235 if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
236 $errors = array('name' => get_string('err_uniquename', 'portfolio'));
237 }
238
239 $pluginerrors = array();
240 if ($this->instance) {
241 $pluginerrors = $this->instance->admin_config_validation($data);
242 }
243 else {
244 $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
245 }
246 if (is_array($pluginerrors)) {
247 $errors = array_merge($errors, $pluginerrors);
248 }
249 return $errors;
250 }
251}
252
253/**
93dd2725 254 * User config form.
18cdcdbf 255 *
93dd2725
RW
256 * This is the form for letting the user configure an instance of a plugin.
257 * In order to extend this, you don't subclass this in the plugin..
258 * see the docs in portfolio_plugin_base for more information:
259 * {@link http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_user_config}
260 *
261 * @package core_portfolio
262 * @category portfolio
263 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
264 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
265 */
87fcac8d 266final class portfolio_user_form extends moodleform {
267
93dd2725 268 /** @var object user porfolio instance */
87fcac8d 269 protected $instance;
93dd2725
RW
270
271 /** @var int hold user id */
87fcac8d 272 protected $userid;
273
93dd2725
RW
274 /**
275 * prepare form
276 */
87fcac8d 277 public function definition() {
278 $this->instance = $this->_customdata['instance'];
279 $this->userid = $this->_customdata['userid'];
280
281 $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
c9833243 282 $this->_form->setType('config', PARAM_INT);
87fcac8d 283
284 $this->instance->user_config_form($this->_form, $this->userid);
285
286 $data = array();
287 foreach ($this->instance->get_allowed_user_config() as $config) {
288 $data[$config] = $this->instance->get_user_config($config, $this->userid);
289 }
290 $this->set_data($data);
291 $this->add_action_buttons(true, get_string('save', 'portfolio'));
292 }
293
93dd2725
RW
294 /**
295 * User user config form.
296 *
297 * @param stdClass $data form data
298 */
9b27ffa0 299 public function validation($data, $files) {
87fcac8d 300
301 $errors = $this->instance->user_config_validation($data);
302
303 }
304}
305
306
307/**
93dd2725 308 * Form that just contains the dropdown menu of available instances.
18cdcdbf 309 *
93dd2725
RW
310 * This is not used by portfolio_add_button, but on the first step of the export,
311 * if the plugin instance has not yet been selected.
312 *
313 * @package core_portfolio
314 * @category portfolio
315 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
316 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
317 */
87fcac8d 318class portfolio_instance_select extends moodleform {
319
93dd2725 320 /** @var portfolio_caller_base plugin instance */
87fcac8d 321 private $caller;
322
93dd2725
RW
323 /**
324 * The required basic elements to the form.
325 */
87fcac8d 326 function definition() {
327 $this->caller = $this->_customdata['caller'];
59dd457e 328 $options = $this->_customdata['options'];
87fcac8d 329 $mform =& $this->_form;
330 $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
c95a6095 331 $mform->addElement('hidden', 'id', $this->_customdata['id']);
87fcac8d 332 $this->add_action_buttons(true, get_string('next'));
333 }
334}