3 * Moodle - Modular Object-Oriented Dynamic Learning Environment
5 * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
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.
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.
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/>.
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
26 * This file contains all the form definitions used by the portfolio code.
29 defined('MOODLE_INTERNAL') ||
die();
31 // make sure we include moodleform first!
32 require_once ($CFG->libdir
.'/formslib.php');
35 * During-export config form.
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:
41 * http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_export_config
42 * http://docs.moodle.org/en/Development:Adding_a_Portfolio_Button_to_a_page#has_export_config
44 final class portfolio_export_form
extends moodleform
{
46 public function definition() {
48 $mform =& $this->_form
;
49 $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG
);
50 $mform->addElement('hidden', 'id', $this->_customdata
['id']);
51 $mform->addElement('hidden', 'instance', $this->_customdata
['instance']->get('id'));
52 $mform->setType('instance', PARAM_INT
);
53 $mform->setType('stage', PARAM_INT
);
54 $mform->setType('id', PARAM_INT
);
56 if (array_key_exists('formats', $this->_customdata
) && is_array($this->_customdata
['formats'])) {
57 if (count($this->_customdata
['formats']) > 1) {
59 foreach ($this->_customdata
['formats'] as $key) {
60 $options[$key] = get_string('format_' . $key, 'portfolio');
62 $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
64 $f = array_shift($this->_customdata
['formats']);
65 $mform->addElement('hidden', 'format', $f);
66 $mform->setType('format', PARAM_RAW
);
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);
80 if ($this->_customdata
['expectedtime'] == PORTFOLIO_TIME_LOW
) {
81 $mform->addElement('hidden', 'wait', 1);
83 $mform->addElement('hidden', 'wait', 0);
85 $mform->setType('wait', PARAM_INT
);
88 if (array_key_exists('plugin', $this->_customdata
) && is_object($this->_customdata
['plugin'])) {
89 $this->_customdata
['plugin']->export_config_form($mform, $this->_customdata
['userid']);
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']);
96 $this->add_action_buttons(true, get_string('next'));
99 public function validation($data) {
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;
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);
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:
124 * http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_admin_config
126 final class portfolio_admin_form
extends moodleform
{
130 protected $portfolio;
134 public function definition() {
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;
140 $this->portfolio
= $this->_customdata
['portfolio'];
141 $this->action
= $this->_customdata
['action'];
142 $this->visible
= $this->_customdata
['visible'];
144 $mform =& $this->_form
;
145 $strrequired = get_string('required');
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
);
153 $mform->addElement('hidden', 'plugin', $this->plugin
);
154 $mform->setType('plugin', PARAM_SAFEDIR
);
156 if (!$this->instance
) {
157 $insane = portfolio_instance_sanity_check($this->instance
);
159 $insane = portfolio_plugin_sanity_check($this->plugin
);
162 if (isset($insane) && is_array($insane)) {
163 $insane = array_shift($insane);
165 if (isset($insane) && is_string($insane)) { // something went wrong, warn...
166 $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin
));
169 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
170 $mform->addRule('name', $strrequired, 'required', null, 'client');
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
) {
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);
179 $this->instance
->admin_config_form($mform);
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);
189 $this->set_data($data);
191 $this->set_data(array('name' => portfolio_static_function($this->plugin
, 'get_name')));
194 $this->add_action_buttons(true, get_string('save', 'portfolio'));
197 public function validation($data) {
201 if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
202 $errors = array('name' => get_string('err_uniquename', 'portfolio'));
205 $pluginerrors = array();
206 if ($this->instance
) {
207 $pluginerrors = $this->instance
->admin_config_validation($data);
210 $pluginerrors = portfolio_static_function($this->plugin
, 'admin_config_validation', $data);
212 if (is_array($pluginerrors)) {
213 $errors = array_merge($errors, $pluginerrors);
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:
225 * http://docs.moodle.org/en/Development:Writing_a_Portfolio_Plugin#has_user_config
227 final class portfolio_user_form
extends moodleform
{
232 public function definition() {
233 $this->instance
= $this->_customdata
['instance'];
234 $this->userid
= $this->_customdata
['userid'];
236 $this->_form
->addElement('hidden', 'config', $this->instance
->get('id'));
237 $mform->setType('config', PARAM_INT
);
239 $this->instance
->user_config_form($this->_form
, $this->userid
);
242 foreach ($this->instance
->get_allowed_user_config() as $config) {
243 $data[$config] = $this->instance
->get_user_config($config, $this->userid
);
245 $this->set_data($data);
246 $this->add_action_buttons(true, get_string('save', 'portfolio'));
249 public function validation($data) {
251 $errors = $this->instance
->user_config_validation($data);
258 * Form that just contains the dropdown menu of available instances
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.
263 class portfolio_instance_select
extends moodleform
{
267 function definition() {
268 $this->caller
= $this->_customdata
['caller'];
269 $options = $this->_customdata
['options'];
270 $mform =& $this->_form
;
271 $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
272 $mform->addElement('hidden', 'id', $this->_customdata
['id']);
273 $this->add_action_buttons(true, get_string('next'));