messaging MDL-22320 moved the message send button closer to the message box
[moodle.git] / lib / portfolio / forms.php
CommitLineData
87fcac8d 1<?php
2/**
3 * Moodle - Modular Object-Oriented Dynamic Learning Environment
4 * http://moodle.org
5 * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * @package moodle
21 * @subpackage portfolio
22 * @author Penny Leach <penny@catalyst.net.nz>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
24 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
25 *
26 * This file contains all the form definitions used by the portfolio code.
27 */
28
29// make sure we include moodleform first!
30require_once ($CFG->libdir.'/formslib.php');
31
32/**
33* During-export config form.
34*
35* This is the form that is actually used while exporting.
36* Plugins and callers don't get to define their own class
37* as we have to handle form elements from both places
38* See the docs here for more information:
39* http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_export_config
40* http://docs.moodle.org/en/Development:Adding_a_Portfolio_Button_to_a_page#has_export_config
41*/
42final class portfolio_export_form extends moodleform {
43
44 public function definition() {
45
46 $mform =& $this->_form;
47 $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG);
c95a6095 48 $mform->addElement('hidden', 'id', $this->_customdata['id']);
87fcac8d 49 $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
d18e0fe6 50 $mform->setType('instance', PARAM_INT);
c95a6095
PL
51 $mform->setType('stage', PARAM_INT);
52 $mform->setType('id', PARAM_INT);
87fcac8d 53
54 if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) {
55 if (count($this->_customdata['formats']) > 1) {
56 $options = array();
57 foreach ($this->_customdata['formats'] as $key) {
58 $options[$key] = get_string('format_' . $key, 'portfolio');
59 }
60 $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
61 } else {
62 $f = array_shift($this->_customdata['formats']);
63 $mform->addElement('hidden', 'format', $f);
d18e0fe6 64 $mform->setType('format', PARAM_RAW);
87fcac8d 65 }
66 }
67
68 // only display the option to wait or not if it's applicable
69 if (array_key_exists('expectedtime', $this->_customdata)
70 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW
71 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) {
72 $radioarray = array();
73 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
74 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0);
75 $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
76 $mform->setDefault('wait', 0);
77 } else {
78 if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) {
79 $mform->addElement('hidden', 'wait', 1);
80 } else {
81 $mform->addElement('hidden', 'wait', 0);
82 }
d18e0fe6 83 $mform->setType('wait', PARAM_INT);
87fcac8d 84 }
85
86 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
87 $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
88 }
89
90 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
91 $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
92 }
93
94 $this->add_action_buttons(true, get_string('next'));
95 }
96
97 public function validation($data) {
98
99 $errors = array();
100
101 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
102 $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
103 if (is_array($pluginerrors)) {
104 $errors = $pluginerrors;
105 }
106 }
107 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
108 $callererrors = $this->_customdata['caller']->export_config_validation($data);
109 if (is_array($callererrors)) {
110 $errors = array_merge($errors, $callererrors);
111 }
112 }
113 return $errors;
114 }
115}
116
117/**
118* Admin config form
119*
120* This form is extendable by plugins who want the admin to be able to configure more than just the name of the instance.
121* This is NOT done by subclassing this class, see the docs for portfolio_plugin_base for more information:
122* http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_admin_config
123*/
124final class portfolio_admin_form extends moodleform {
125
126 protected $instance;
127 protected $plugin;
128
129 public function definition() {
130 global $CFG;
131 $this->plugin = $this->_customdata['plugin'];
132 $this->instance = (isset($this->_customdata['instance'])
133 && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
134 ? $this->_customdata['instance'] : null;
135
136 $mform =& $this->_form;
137 $strrequired = get_string('required');
138
139 $mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->get('id') : 0);
d18e0fe6 140 $mform->setType('edit', PARAM_INT);
87fcac8d 141 $mform->addElement('hidden', 'new', $this->plugin);
d18e0fe6 142 $mform->setType('new', PARAM_INT);
87fcac8d 143 $mform->addElement('hidden', 'plugin', $this->plugin);
d18e0fe6 144 $mform->setType('plugin', PARAM_SAFEDIR);
87fcac8d 145
2726b405 146 if (!$this->instance) {
147 $insane = portfolio_instance_sanity_check($this->instance);
148 } else {
149 $insane = portfolio_plugin_sanity_check($this->plugin);
87fcac8d 150 }
151
152 if (isset($insane) && is_array($insane)) {
153 $insane = array_shift($insane);
154 }
155 if (isset($insane) && is_string($insane)) { // something went wrong, warn...
156 $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin));
157 }
158
159 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
160 $mform->addRule('name', $strrequired, 'required', null, 'client');
161
2726b405 162 // let the plugin add the fields they want (either statically or not)
163 if (portfolio_static_function($this->plugin, 'has_admin_config')) {
164 if (!$this->instance) {
b8824f38
PL
165 require_once($CFG->libdir . '/portfolio/plugin.php');
166 require_once($CFG->dirroot . '/portfolio/' . $this->plugin . '/lib.php');
167 call_user_func(array('portfolio_plugin_' . $this->plugin, 'admin_config_form'), $mform);
2726b405 168 } else {
169 $this->instance->admin_config_form($mform);
170 }
171 }
87fcac8d 172
173 // and set the data if we have some.
174 if ($this->instance) {
175 $data = array('name' => $this->instance->get('name'));
176 foreach ($this->instance->get_allowed_config() as $config) {
177 $data[$config] = $this->instance->get_config($config);
178 }
179 $this->set_data($data);
180 } else {
181 $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
182 }
183
184 $this->add_action_buttons(true, get_string('save', 'portfolio'));
185 }
186
187 public function validation($data) {
188 global $DB;
189
190 $errors = array();
191 if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
192 $errors = array('name' => get_string('err_uniquename', 'portfolio'));
193 }
194
195 $pluginerrors = array();
196 if ($this->instance) {
197 $pluginerrors = $this->instance->admin_config_validation($data);
198 }
199 else {
200 $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
201 }
202 if (is_array($pluginerrors)) {
203 $errors = array_merge($errors, $pluginerrors);
204 }
205 return $errors;
206 }
207}
208
209/**
210* User config form.
211*
212* This is the form for letting the user configure an instance of a plugin.
213* In order to extend this, you don't subclass this in the plugin..
214* see the docs in portfolio_plugin_base for more information:
215* http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_user_config
216*/
217final class portfolio_user_form extends moodleform {
218
219 protected $instance;
220 protected $userid;
221
222 public function definition() {
223 $this->instance = $this->_customdata['instance'];
224 $this->userid = $this->_customdata['userid'];
225
226 $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
d18e0fe6 227 $mform->setType('config', PARAM_INT);
87fcac8d 228
229 $this->instance->user_config_form($this->_form, $this->userid);
230
231 $data = array();
232 foreach ($this->instance->get_allowed_user_config() as $config) {
233 $data[$config] = $this->instance->get_user_config($config, $this->userid);
234 }
235 $this->set_data($data);
236 $this->add_action_buttons(true, get_string('save', 'portfolio'));
237 }
238
239 public function validation($data) {
240
241 $errors = $this->instance->user_config_validation($data);
242
243 }
244}
245
246
247/**
248* Form that just contains the dropdown menu of available instances
249*
250* This is not used by portfolio_add_button, but on the first step of the export
251* if the plugin instance has not yet been selected.
252*/
253class portfolio_instance_select extends moodleform {
254
255 private $caller;
256
257 function definition() {
258 $this->caller = $this->_customdata['caller'];
59dd457e 259 $options = $this->_customdata['options'];
87fcac8d 260 $mform =& $this->_form;
261 $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
c95a6095 262 $mform->addElement('hidden', 'id', $this->_customdata['id']);
87fcac8d 263 $this->add_action_buttons(true, get_string('next'));
264 }
265}