PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/portfolio/forms.php

http://github.com/moodle/moodle
PHP | 328 lines | 168 code | 46 blank | 114 comment | 36 complexity | 023b0d7539629e50cab9fd5741d3b6d8 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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.
  13. //
  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/>.
  16. /**
  17. * This file contains all the form definitions used by the portfolio code.
  18. *
  19. * @package core_portfolio
  20. * @copyright 2008 Penny Leach <penny@catalyst.net.nz>,
  21. * Martin Dougiamas (http://dougiamas.com)
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. // make sure we include moodleform first!
  26. require_once ($CFG->libdir.'/formslib.php');
  27. /**
  28. * During-export config form.
  29. *
  30. * This is the form that is actually used while exporting.
  31. * Plugins and callers don't get to define their own class
  32. * as we have to handle form elements from both places
  33. * See the docs here for more information:
  34. * http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_export_config
  35. * http://docs.moodle.org/dev/Adding_a_Portfolio_Button_to_a_page#has_export_config
  36. *
  37. * @package core_portfolio
  38. * @category portfolio
  39. * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
  40. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41. */
  42. final class portfolio_export_form extends moodleform {
  43. /**
  44. * prepare form
  45. */
  46. public function definition() {
  47. $mform =& $this->_form;
  48. $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG);
  49. $mform->addElement('hidden', 'id', $this->_customdata['id']);
  50. $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
  51. $mform->setType('instance', PARAM_INT);
  52. $mform->setType('stage', PARAM_INT);
  53. $mform->setType('id', PARAM_INT);
  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);
  64. $mform->setType('format', PARAM_RAW);
  65. }
  66. }
  67. // only display the option to wait or not if it's applicable
  68. if (array_key_exists('expectedtime', $this->_customdata)
  69. && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW
  70. && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) {
  71. $radioarray = array();
  72. $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
  73. $radioarray[] = $mform->createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0);
  74. $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
  75. $mform->setDefault('wait', 0);
  76. } else {
  77. if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) {
  78. $mform->addElement('hidden', 'wait', 1);
  79. } else {
  80. $mform->addElement('hidden', 'wait', 0);
  81. }
  82. $mform->setType('wait', PARAM_INT);
  83. }
  84. if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
  85. $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
  86. }
  87. if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
  88. $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
  89. }
  90. $this->add_action_buttons(true, get_string('next'));
  91. }
  92. /**
  93. * Validate portfolio export form
  94. *
  95. * @param stdClass $data portfolio information from form data
  96. * @return array
  97. */
  98. public function validation($data, $files) {
  99. $errors = array();
  100. if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
  101. $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
  102. if (is_array($pluginerrors)) {
  103. $errors = $pluginerrors;
  104. }
  105. }
  106. if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
  107. $callererrors = $this->_customdata['caller']->export_config_validation($data);
  108. if (is_array($callererrors)) {
  109. $errors = array_merge($errors, $callererrors);
  110. }
  111. }
  112. return $errors;
  113. }
  114. }
  115. /**
  116. * Admin config form.
  117. *
  118. * This form is extendable by plugins who want the admin to be able to configure more than just the name of the instance.
  119. * This is NOT done by subclassing this class, see the docs for portfolio_plugin_base for more information:
  120. * {@link http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_admin_config}
  121. *
  122. * @package core_portfolio
  123. * @category portfolio
  124. * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
  125. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  126. */
  127. final class portfolio_admin_form extends moodleform {
  128. /** @var object to hold porfolio instance configuration */
  129. protected $instance;
  130. /** @var string plugin name*/
  131. protected $plugin;
  132. /** @var string portfolio plugin name*/
  133. protected $portfolio;
  134. /** @var string plugin availability*/
  135. protected $action;
  136. /** @var int portfolio plugin visibility*/
  137. protected $visible;
  138. /**
  139. * prepare form
  140. */
  141. public function definition() {
  142. global $CFG;
  143. $this->plugin = $this->_customdata['plugin'];
  144. $this->instance = (isset($this->_customdata['instance'])
  145. && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
  146. ? $this->_customdata['instance'] : null;
  147. $this->portfolio = $this->_customdata['portfolio'];
  148. $this->action = $this->_customdata['action'];
  149. $this->visible = $this->_customdata['visible'];
  150. $mform =& $this->_form;
  151. $strrequired = get_string('required');
  152. $mform->addElement('hidden', 'pf', $this->portfolio);
  153. $mform->setType('pf', PARAM_ALPHA);
  154. $mform->addElement('hidden', 'action', $this->action);
  155. $mform->setType('action', PARAM_ALPHA);
  156. $mform->addElement('hidden', 'visible', $this->visible);
  157. $mform->setType('visible', PARAM_INT);
  158. $mform->addElement('hidden', 'plugin', $this->plugin);
  159. $mform->setType('plugin', PARAM_PLUGIN);
  160. if (!$this->instance) {
  161. $insane = portfolio_instance_sanity_check($this->instance);
  162. } else {
  163. $insane = portfolio_plugin_sanity_check($this->plugin);
  164. }
  165. if (isset($insane) && is_array($insane)) {
  166. $insane = array_shift($insane);
  167. }
  168. if (isset($insane) && is_string($insane)) { // something went wrong, warn...
  169. $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin));
  170. }
  171. $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
  172. $mform->addRule('name', $strrequired, 'required', null, 'client');
  173. $mform->setType('name', PARAM_TEXT);
  174. // let the plugin add the fields they want (either statically or not)
  175. if (portfolio_static_function($this->plugin, 'has_admin_config')) {
  176. require_once($CFG->libdir . '/portfolio/plugin.php');
  177. require_once($CFG->dirroot . '/portfolio/' . $this->plugin . '/lib.php');
  178. $classname = 'portfolio_plugin_' . $this->plugin;
  179. $classname::admin_config_form($mform);
  180. }
  181. // and set the data if we have some.
  182. if ($this->instance) {
  183. $data = array('name' => $this->instance->get('name'));
  184. foreach ($this->instance->get_allowed_config() as $config) {
  185. $data[$config] = $this->instance->get_config($config);
  186. }
  187. $this->set_data($data);
  188. } else {
  189. $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
  190. }
  191. $this->add_action_buttons(true, get_string('save', 'portfolio'));
  192. }
  193. /**
  194. * Validate admin config form
  195. *
  196. * @param stdObject $data form data
  197. * @return array
  198. */
  199. public function validation($data, $files) {
  200. global $DB;
  201. $errors = array();
  202. if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
  203. $errors = array('name' => get_string('err_uniquename', 'portfolio'));
  204. }
  205. $pluginerrors = array();
  206. $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
  207. if (is_array($pluginerrors)) {
  208. $errors = array_merge($errors, $pluginerrors);
  209. }
  210. return $errors;
  211. }
  212. }
  213. /**
  214. * User config form.
  215. *
  216. * This is the form for letting the user configure an instance of a plugin.
  217. * In order to extend this, you don't subclass this in the plugin..
  218. * see the docs in portfolio_plugin_base for more information:
  219. * {@link http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_user_config}
  220. *
  221. * @package core_portfolio
  222. * @category portfolio
  223. * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
  224. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  225. */
  226. final class portfolio_user_form extends moodleform {
  227. /** @var object user porfolio instance */
  228. protected $instance;
  229. /** @var int hold user id */
  230. protected $userid;
  231. /**
  232. * prepare form
  233. */
  234. public function definition() {
  235. $this->instance = $this->_customdata['instance'];
  236. $this->userid = $this->_customdata['userid'];
  237. $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
  238. $this->_form->setType('config', PARAM_INT);
  239. $this->instance->user_config_form($this->_form, $this->userid);
  240. $data = array();
  241. foreach ($this->instance->get_allowed_user_config() as $config) {
  242. $data[$config] = $this->instance->get_user_config($config, $this->userid);
  243. }
  244. $this->set_data($data);
  245. $this->add_action_buttons(true, get_string('save', 'portfolio'));
  246. }
  247. /**
  248. * User user config form.
  249. *
  250. * @param stdClass $data form data
  251. */
  252. public function validation($data, $files) {
  253. $errors = $this->instance->user_config_validation($data);
  254. }
  255. }
  256. /**
  257. * Form that just contains the dropdown menu of available instances.
  258. *
  259. * This is not used by portfolio_add_button, but on the first step of the export,
  260. * if the plugin instance has not yet been selected.
  261. *
  262. * @package core_portfolio
  263. * @category portfolio
  264. * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
  265. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  266. */
  267. class portfolio_instance_select extends moodleform {
  268. /** @var portfolio_caller_base plugin instance */
  269. private $caller;
  270. /**
  271. * The required basic elements to the form.
  272. */
  273. function definition() {
  274. $this->caller = $this->_customdata['caller'];
  275. $options = $this->_customdata['options'];
  276. $mform =& $this->_form;
  277. $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
  278. $mform->addElement('hidden', 'id', $this->_customdata['id']);
  279. $mform->setType('id', PARAM_INT);
  280. $this->add_action_buttons(true, get_string('next'));
  281. }
  282. }