MDL-14591 - split portfoliolib into logical parts for better readability
[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);
48 $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
49
50 if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) {
51 if (count($this->_customdata['formats']) > 1) {
52 $options = array();
53 foreach ($this->_customdata['formats'] as $key) {
54 $options[$key] = get_string('format_' . $key, 'portfolio');
55 }
56 $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
57 } else {
58 $f = array_shift($this->_customdata['formats']);
59 $mform->addElement('hidden', 'format', $f);
60 }
61 }
62
63 // only display the option to wait or not if it's applicable
64 if (array_key_exists('expectedtime', $this->_customdata)
65 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW
66 && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) {
67 $radioarray = array();
68 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
69 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0);
70 $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
71 $mform->setDefault('wait', 0);
72 } else {
73 if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) {
74 $mform->addElement('hidden', 'wait', 1);
75 } else {
76 $mform->addElement('hidden', 'wait', 0);
77 }
78 }
79
80 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
81 $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
82 }
83
84 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
85 $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
86 }
87
88 $this->add_action_buttons(true, get_string('next'));
89 }
90
91 public function validation($data) {
92
93 $errors = array();
94
95 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
96 $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
97 if (is_array($pluginerrors)) {
98 $errors = $pluginerrors;
99 }
100 }
101 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
102 $callererrors = $this->_customdata['caller']->export_config_validation($data);
103 if (is_array($callererrors)) {
104 $errors = array_merge($errors, $callererrors);
105 }
106 }
107 return $errors;
108 }
109}
110
111/**
112* Admin config form
113*
114* This form is extendable by plugins who want the admin to be able to configure more than just the name of the instance.
115* This is NOT done by subclassing this class, see the docs for portfolio_plugin_base for more information:
116* http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_admin_config
117*/
118final class portfolio_admin_form extends moodleform {
119
120 protected $instance;
121 protected $plugin;
122
123 public function definition() {
124 global $CFG;
125 $this->plugin = $this->_customdata['plugin'];
126 $this->instance = (isset($this->_customdata['instance'])
127 && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
128 ? $this->_customdata['instance'] : null;
129
130 $mform =& $this->_form;
131 $strrequired = get_string('required');
132
133 $mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->get('id') : 0);
134 $mform->addElement('hidden', 'new', $this->plugin);
135 $mform->addElement('hidden', 'plugin', $this->plugin);
136
137 // let the plugin add the fields they want (either statically or not)
138 if (portfolio_static_function($this->plugin, 'has_admin_config')) {
139 if (!$this->instance) {
140 $insane = portfolio_instance_sanity_check($this->instance);
141 portfolio_static_function($this->plugin, 'admin_config_form', $mform);
142 } else {
143 $insane = portfolio_plugin_sanity_check($this->plugin);
144 $this->instance->admin_config_form($mform);
145 }
146 }
147
148 if (isset($insane) && is_array($insane)) {
149 $insane = array_shift($insane);
150 }
151 if (isset($insane) && is_string($insane)) { // something went wrong, warn...
152 $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin));
153 }
154
155 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
156 $mform->addRule('name', $strrequired, 'required', null, 'client');
157
158
159 // and set the data if we have some.
160 if ($this->instance) {
161 $data = array('name' => $this->instance->get('name'));
162 foreach ($this->instance->get_allowed_config() as $config) {
163 $data[$config] = $this->instance->get_config($config);
164 }
165 $this->set_data($data);
166 } else {
167 $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
168 }
169
170 $this->add_action_buttons(true, get_string('save', 'portfolio'));
171 }
172
173 public function validation($data) {
174 global $DB;
175
176 $errors = array();
177 if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
178 $errors = array('name' => get_string('err_uniquename', 'portfolio'));
179 }
180
181 $pluginerrors = array();
182 if ($this->instance) {
183 $pluginerrors = $this->instance->admin_config_validation($data);
184 }
185 else {
186 $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
187 }
188 if (is_array($pluginerrors)) {
189 $errors = array_merge($errors, $pluginerrors);
190 }
191 return $errors;
192 }
193}
194
195/**
196* User config form.
197*
198* This is the form for letting the user configure an instance of a plugin.
199* In order to extend this, you don't subclass this in the plugin..
200* see the docs in portfolio_plugin_base for more information:
201* http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_user_config
202*/
203final class portfolio_user_form extends moodleform {
204
205 protected $instance;
206 protected $userid;
207
208 public function definition() {
209 $this->instance = $this->_customdata['instance'];
210 $this->userid = $this->_customdata['userid'];
211
212 $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
213
214 $this->instance->user_config_form($this->_form, $this->userid);
215
216 $data = array();
217 foreach ($this->instance->get_allowed_user_config() as $config) {
218 $data[$config] = $this->instance->get_user_config($config, $this->userid);
219 }
220 $this->set_data($data);
221 $this->add_action_buttons(true, get_string('save', 'portfolio'));
222 }
223
224 public function validation($data) {
225
226 $errors = $this->instance->user_config_validation($data);
227
228 }
229}
230
231
232/**
233* Form that just contains the dropdown menu of available instances
234*
235* This is not used by portfolio_add_button, but on the first step of the export
236* if the plugin instance has not yet been selected.
237*/
238class portfolio_instance_select extends moodleform {
239
240 private $caller;
241
242 function definition() {
243 $this->caller = $this->_customdata['caller'];
244 $options = portfolio_instance_select(
245 portfolio_instances(),
246 $this->caller->supported_formats($this->caller),
247 get_class($this->caller),
248 'instance',
249 true,
250 true
251 );
252 if (empty($options)) {
253 debugging('noavailableplugins', 'portfolio');
254 return false;
255 }
256 $mform =& $this->_form;
257 $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
258 $this->add_action_buttons(true, get_string('next'));
259 }
260}
261
262?>