weekly release 2.3dev (blame stronk7 for 0202 mistake)
[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 *
0972665f 20 * @package core
87fcac8d 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
0972665f
PS
29defined('MOODLE_INTERNAL') || die();
30
87fcac8d 31// make sure we include moodleform first!
32require_once ($CFG->libdir.'/formslib.php');
33
34/**
35* During-export config form.
36*
37* This is the form that is actually used while exporting.
38* Plugins and callers don't get to define their own class
39* as we have to handle form elements from both places
40* See the docs here for more information:
728ebac7
AB
41* http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_export_config
42* http://docs.moodle.org/dev/Adding_a_Portfolio_Button_to_a_page#has_export_config
87fcac8d 43*/
44final class portfolio_export_form extends moodleform {
45
46 public function definition() {
47
48 $mform =& $this->_form;
49 $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG);
c95a6095 50 $mform->addElement('hidden', 'id', $this->_customdata['id']);
87fcac8d 51 $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
d18e0fe6 52 $mform->setType('instance', PARAM_INT);
c95a6095
PL
53 $mform->setType('stage', PARAM_INT);
54 $mform->setType('id', PARAM_INT);
87fcac8d 55
56 if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) {
57 if (count($this->_customdata['formats']) > 1) {
58 $options = array();
59 foreach ($this->_customdata['formats'] as $key) {
60 $options[$key] = get_string('format_' . $key, 'portfolio');
61 }
62 $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
63 } else {
64 $f = array_shift($this->_customdata['formats']);
65 $mform->addElement('hidden', 'format', $f);
d18e0fe6 66 $mform->setType('format', PARAM_RAW);
87fcac8d 67 }
68 }
69
70 // only display the option to wait or not if it's applicable
71 if (array_key_exists('expectedtime', $this->_customdata)
72 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW
73 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) {
74 $radioarray = array();
75 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
76 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0);
77 $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
78 $mform->setDefault('wait', 0);
79 } else {
80 if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) {
81 $mform->addElement('hidden', 'wait', 1);
82 } else {
83 $mform->addElement('hidden', 'wait', 0);
84 }
d18e0fe6 85 $mform->setType('wait', PARAM_INT);
87fcac8d 86 }
87
88 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
89 $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
90 }
91
92 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
93 $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
94 }
95
96 $this->add_action_buttons(true, get_string('next'));
97 }
98
99 public function validation($data) {
100
101 $errors = array();
102
103 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
104 $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
105 if (is_array($pluginerrors)) {
106 $errors = $pluginerrors;
107 }
108 }
109 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
110 $callererrors = $this->_customdata['caller']->export_config_validation($data);
111 if (is_array($callererrors)) {
112 $errors = array_merge($errors, $callererrors);
113 }
114 }
115 return $errors;
116 }
117}
118
119/**
120* Admin config form
121*
122* This form is extendable by plugins who want the admin to be able to configure more than just the name of the instance.
123* This is NOT done by subclassing this class, see the docs for portfolio_plugin_base for more information:
728ebac7 124* http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_admin_config
87fcac8d 125*/
126final class portfolio_admin_form extends moodleform {
127
128 protected $instance;
129 protected $plugin;
6010eda2
MD
130 protected $portfolio;
131 protected $action;
132 protected $visible;
87fcac8d 133
134 public function definition() {
135 global $CFG;
136 $this->plugin = $this->_customdata['plugin'];
137 $this->instance = (isset($this->_customdata['instance'])
138 && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
139 ? $this->_customdata['instance'] : null;
6010eda2
MD
140 $this->portfolio = $this->_customdata['portfolio'];
141 $this->action = $this->_customdata['action'];
142 $this->visible = $this->_customdata['visible'];
87fcac8d 143
144 $mform =& $this->_form;
145 $strrequired = get_string('required');
146
6010eda2
MD
147 $mform->addElement('hidden', 'pf', $this->portfolio);
148 $mform->setType('pf', PARAM_ALPHA);
149 $mform->addElement('hidden', 'action', $this->action);
150 $mform->setType('action', PARAM_ALPHA);
151 $mform->addElement('hidden', 'visible', $this->visible);
152 $mform->setType('visible', PARAM_INT);
87fcac8d 153 $mform->addElement('hidden', 'plugin', $this->plugin);
aff24313 154 $mform->setType('plugin', PARAM_PLUGIN);
87fcac8d 155
2726b405 156 if (!$this->instance) {
157 $insane = portfolio_instance_sanity_check($this->instance);
158 } else {
159 $insane = portfolio_plugin_sanity_check($this->plugin);
87fcac8d 160 }
161
162 if (isset($insane) && is_array($insane)) {
163 $insane = array_shift($insane);
164 }
165 if (isset($insane) && is_string($insane)) { // something went wrong, warn...
166 $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin));
167 }
168
169 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
170 $mform->addRule('name', $strrequired, 'required', null, 'client');
171
2726b405 172 // let the plugin add the fields they want (either statically or not)
173 if (portfolio_static_function($this->plugin, 'has_admin_config')) {
174 if (!$this->instance) {
b8824f38
PL
175 require_once($CFG->libdir . '/portfolio/plugin.php');
176 require_once($CFG->dirroot . '/portfolio/' . $this->plugin . '/lib.php');
177 call_user_func(array('portfolio_plugin_' . $this->plugin, 'admin_config_form'), $mform);
2726b405 178 } else {
179 $this->instance->admin_config_form($mform);
180 }
181 }
87fcac8d 182
183 // and set the data if we have some.
184 if ($this->instance) {
185 $data = array('name' => $this->instance->get('name'));
186 foreach ($this->instance->get_allowed_config() as $config) {
187 $data[$config] = $this->instance->get_config($config);
188 }
189 $this->set_data($data);
190 } else {
191 $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
192 }
193
194 $this->add_action_buttons(true, get_string('save', 'portfolio'));
195 }
196
197 public function validation($data) {
198 global $DB;
199
200 $errors = array();
201 if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
202 $errors = array('name' => get_string('err_uniquename', 'portfolio'));
203 }
204
205 $pluginerrors = array();
206 if ($this->instance) {
207 $pluginerrors = $this->instance->admin_config_validation($data);
208 }
209 else {
210 $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
211 }
212 if (is_array($pluginerrors)) {
213 $errors = array_merge($errors, $pluginerrors);
214 }
215 return $errors;
216 }
217}
218
219/**
220* User config form.
221*
222* This is the form for letting the user configure an instance of a plugin.
223* In order to extend this, you don't subclass this in the plugin..
224* see the docs in portfolio_plugin_base for more information:
728ebac7 225* http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_user_config
87fcac8d 226*/
227final class portfolio_user_form extends moodleform {
228
229 protected $instance;
230 protected $userid;
231
232 public function definition() {
233 $this->instance = $this->_customdata['instance'];
234 $this->userid = $this->_customdata['userid'];
235
236 $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
c9833243 237 $this->_form->setType('config', PARAM_INT);
87fcac8d 238
239 $this->instance->user_config_form($this->_form, $this->userid);
240
241 $data = array();
242 foreach ($this->instance->get_allowed_user_config() as $config) {
243 $data[$config] = $this->instance->get_user_config($config, $this->userid);
244 }
245 $this->set_data($data);
246 $this->add_action_buttons(true, get_string('save', 'portfolio'));
247 }
248
249 public function validation($data) {
250
251 $errors = $this->instance->user_config_validation($data);
252
253 }
254}
255
256
257/**
258* Form that just contains the dropdown menu of available instances
259*
260* This is not used by portfolio_add_button, but on the first step of the export
261* if the plugin instance has not yet been selected.
262*/
263class portfolio_instance_select extends moodleform {
264
265 private $caller;
266
267 function definition() {
268 $this->caller = $this->_customdata['caller'];
59dd457e 269 $options = $this->_customdata['options'];
87fcac8d 270 $mform =& $this->_form;
271 $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
c95a6095 272 $mform->addElement('hidden', 'id', $this->_customdata['id']);
87fcac8d 273 $this->add_action_buttons(true, get_string('next'));
274 }
275}