Merge branch 'MDL-78173' of https://github.com/paulholden/moodle
[moodle.git] / lib / portfolio / forms.php
blob718757b960ae463ad4167ed90384de310a41ed19
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.
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/>.
17 /**
18 * This file contains all the form definitions used by the portfolio code.
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
26 defined('MOODLE_INTERNAL') || die();
28 // make sure we include moodleform first!
29 require_once ($CFG->libdir.'/formslib.php');
31 /**
32 * During-export config form.
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
38 * @package core_portfolio
39 * @category portfolio
40 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 final class portfolio_export_form extends moodleform {
45 /**
46 * prepare form
48 public function definition() {
50 $mform =& $this->_form;
51 $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG);
52 $mform->addElement('hidden', 'id', $this->_customdata['id']);
53 $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
54 $mform->setType('instance', PARAM_INT);
55 $mform->setType('stage', PARAM_INT);
56 $mform->setType('id', PARAM_INT);
58 if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) {
59 if (count($this->_customdata['formats']) > 1) {
60 $options = array();
61 foreach ($this->_customdata['formats'] as $key) {
62 $options[$key] = get_string('format_' . $key, 'portfolio');
64 $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
65 } else {
66 $f = array_shift($this->_customdata['formats']);
67 $mform->addElement('hidden', 'format', $f);
68 $mform->setType('format', PARAM_RAW);
72 // only display the option to wait or not if it's applicable
73 if (array_key_exists('expectedtime', $this->_customdata)
74 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW
75 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) {
76 $radioarray = array();
77 $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
78 $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0);
79 $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
80 $mform->setDefault('wait', 0);
81 } else {
82 if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) {
83 $mform->addElement('hidden', 'wait', 1);
84 } else {
85 $mform->addElement('hidden', 'wait', 0);
87 $mform->setType('wait', PARAM_INT);
90 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
91 $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
94 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
95 $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
98 $this->add_action_buttons(true, get_string('next'));
102 * Validate portfolio export form
104 * @param stdClass $data portfolio information from form data
105 * @return array
107 public function validation($data, $files) {
109 $errors = array();
111 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
112 $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
113 if (is_array($pluginerrors)) {
114 $errors = $pluginerrors;
117 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
118 $callererrors = $this->_customdata['caller']->export_config_validation($data);
119 if (is_array($callererrors)) {
120 $errors = array_merge($errors, $callererrors);
123 return $errors;
128 * Admin config form.
130 * This form is extendable by plugins who want the admin to be able to configure more than just the name of the instance.
131 * This is NOT done by subclassing this class.
133 * @package core_portfolio
134 * @category portfolio
135 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
136 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
138 final class portfolio_admin_form extends moodleform {
140 /** @var object to hold porfolio instance configuration */
141 protected $instance;
143 /** @var string plugin name*/
144 protected $plugin;
146 /** @var string portfolio plugin name*/
147 protected $portfolio;
149 /** @var string plugin availability*/
150 protected $action;
152 /** @var int portfolio plugin visibility*/
153 protected $visible;
156 * prepare form
158 public function definition() {
159 global $CFG;
160 $this->plugin = $this->_customdata['plugin'];
161 $this->instance = (isset($this->_customdata['instance'])
162 && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
163 ? $this->_customdata['instance'] : null;
164 $this->portfolio = $this->_customdata['portfolio'];
165 $this->action = $this->_customdata['action'];
166 $this->visible = $this->_customdata['visible'];
168 $mform =& $this->_form;
169 $strrequired = get_string('required');
171 $mform->addElement('hidden', 'pf', $this->portfolio);
172 $mform->setType('pf', PARAM_ALPHA);
173 $mform->addElement('hidden', 'action', $this->action);
174 $mform->setType('action', PARAM_ALPHA);
175 $mform->addElement('hidden', 'visible', $this->visible);
176 $mform->setType('visible', PARAM_INT);
177 $mform->addElement('hidden', 'plugin', $this->plugin);
178 $mform->setType('plugin', PARAM_PLUGIN);
180 if (!$this->instance) {
181 $insane = portfolio_instance_sanity_check($this->instance);
182 } else {
183 $insane = portfolio_plugin_sanity_check($this->plugin);
186 if (isset($insane) && is_array($insane)) {
187 $insane = array_shift($insane);
189 if (isset($insane) && is_string($insane)) { // something went wrong, warn...
190 $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin));
193 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
194 $mform->addRule('name', $strrequired, 'required', null, 'client');
195 $mform->setType('name', PARAM_TEXT);
197 // let the plugin add the fields they want (either statically or not)
198 if (portfolio_static_function($this->plugin, 'has_admin_config')) {
199 require_once($CFG->libdir . '/portfolio/plugin.php');
200 require_once($CFG->dirroot . '/portfolio/' . $this->plugin . '/lib.php');
201 $classname = 'portfolio_plugin_' . $this->plugin;
202 $classname::admin_config_form($mform);
205 // and set the data if we have some.
206 if ($this->instance) {
207 $data = array('name' => $this->instance->get('name'));
208 foreach ($this->instance->get_allowed_config() as $config) {
209 $data[$config] = $this->instance->get_config($config);
211 $this->set_data($data);
212 } else {
213 $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
216 $this->add_action_buttons(true, get_string('save', 'portfolio'));
220 * Validate admin config form
222 * @param stdObject $data form data
223 * @return array
225 public function validation($data, $files) {
226 global $DB;
228 $errors = array();
229 if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
230 $errors = array('name' => get_string('err_uniquename', 'portfolio'));
233 $pluginerrors = array();
234 $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
235 if (is_array($pluginerrors)) {
236 $errors = array_merge($errors, $pluginerrors);
238 return $errors;
243 * User config form.
245 * This is the form for letting the user configure an instance of a plugin.
246 * In order to extend this, you don't subclass this in the plugin.
248 * @package core_portfolio
249 * @category portfolio
250 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
251 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
253 final class portfolio_user_form extends moodleform {
255 /** @var object user porfolio instance */
256 protected $instance;
258 /** @var int hold user id */
259 protected $userid;
262 * prepare form
264 public function definition() {
265 $this->instance = $this->_customdata['instance'];
266 $this->userid = $this->_customdata['userid'];
268 $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
269 $this->_form->setType('config', PARAM_INT);
271 $this->instance->user_config_form($this->_form, $this->userid);
273 $data = array();
274 foreach ($this->instance->get_allowed_user_config() as $config) {
275 $data[$config] = $this->instance->get_user_config($config, $this->userid);
277 $this->set_data($data);
278 $this->add_action_buttons(true, get_string('save', 'portfolio'));
282 * User user config form.
284 * @param stdClass $data form data
286 public function validation($data, $files) {
288 $errors = $this->instance->user_config_validation($data);
295 * Form that just contains the dropdown menu of available instances.
297 * This is not used by portfolio_add_button, but on the first step of the export,
298 * if the plugin instance has not yet been selected.
300 * @package core_portfolio
301 * @category portfolio
302 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
303 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
305 class portfolio_instance_select extends moodleform {
307 /** @var portfolio_caller_base plugin instance */
308 private $caller;
311 * The required basic elements to the form.
313 function definition() {
314 $this->caller = $this->_customdata['caller'];
315 $options = $this->_customdata['options'];
316 $mform =& $this->_form;
317 $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
318 $mform->addElement('hidden', 'id', $this->_customdata['id']);
319 $mform->setType('id', PARAM_INT);
320 $this->add_action_buttons(true, get_string('next'));