PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/portfolio/forms.php

https://bitbucket.org/ngmares/moodle
PHP | 325 lines | 165 code | 46 blank | 114 comment | 36 complexity | bb6b59e26a80c36cd2ec2a1d4d160536 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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. // let the plugin add the fields they want (either statically or not)
  174. if (portfolio_static_function($this->plugin, 'has_admin_config')) {
  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);
  178. }
  179. // and set the data if we have some.
  180. if ($this->instance) {
  181. $data = array('name' => $this->instance->get('name'));
  182. foreach ($this->instance->get_allowed_config() as $config) {
  183. $data[$config] = $this->instance->get_config($config);
  184. }
  185. $this->set_data($data);
  186. } else {
  187. $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name')));
  188. }
  189. $this->add_action_buttons(true, get_string('save', 'portfolio'));
  190. }
  191. /**
  192. * Validate admin config form
  193. *
  194. * @param stdObject $data form data
  195. * @return array
  196. */
  197. public function validation($data, $files) {
  198. global $DB;
  199. $errors = array();
  200. if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
  201. $errors = array('name' => get_string('err_uniquename', 'portfolio'));
  202. }
  203. $pluginerrors = array();
  204. $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
  205. if (is_array($pluginerrors)) {
  206. $errors = array_merge($errors, $pluginerrors);
  207. }
  208. return $errors;
  209. }
  210. }
  211. /**
  212. * User config form.
  213. *
  214. * This is the form for letting the user configure an instance of a plugin.
  215. * In order to extend this, you don't subclass this in the plugin..
  216. * see the docs in portfolio_plugin_base for more information:
  217. * {@link http://docs.moodle.org/dev/Writing_a_Portfolio_Plugin#has_user_config}
  218. *
  219. * @package core_portfolio
  220. * @category portfolio
  221. * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
  222. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  223. */
  224. final class portfolio_user_form extends moodleform {
  225. /** @var object user porfolio instance */
  226. protected $instance;
  227. /** @var int hold user id */
  228. protected $userid;
  229. /**
  230. * prepare form
  231. */
  232. public function definition() {
  233. $this->instance = $this->_customdata['instance'];
  234. $this->userid = $this->_customdata['userid'];
  235. $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
  236. $this->_form->setType('config', PARAM_INT);
  237. $this->instance->user_config_form($this->_form, $this->userid);
  238. $data = array();
  239. foreach ($this->instance->get_allowed_user_config() as $config) {
  240. $data[$config] = $this->instance->get_user_config($config, $this->userid);
  241. }
  242. $this->set_data($data);
  243. $this->add_action_buttons(true, get_string('save', 'portfolio'));
  244. }
  245. /**
  246. * User user config form.
  247. *
  248. * @param stdClass $data form data
  249. */
  250. public function validation($data, $files) {
  251. $errors = $this->instance->user_config_validation($data);
  252. }
  253. }
  254. /**
  255. * Form that just contains the dropdown menu of available instances.
  256. *
  257. * This is not used by portfolio_add_button, but on the first step of the export,
  258. * if the plugin instance has not yet been selected.
  259. *
  260. * @package core_portfolio
  261. * @category portfolio
  262. * @copyright 2008 Penny Leach <penny@catalyst.net.nz>
  263. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  264. */
  265. class portfolio_instance_select extends moodleform {
  266. /** @var portfolio_caller_base plugin instance */
  267. private $caller;
  268. /**
  269. * The required basic elements to the form.
  270. */
  271. function definition() {
  272. $this->caller = $this->_customdata['caller'];
  273. $options = $this->_customdata['options'];
  274. $mform =& $this->_form;
  275. $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
  276. $mform->addElement('hidden', 'id', $this->_customdata['id']);
  277. $this->add_action_buttons(true, get_string('next'));
  278. }
  279. }