PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/cache/forms.php

https://bitbucket.org/moodle/moodle
PHP | 385 lines | 216 code | 44 blank | 125 comment | 35 complexity | 7b6c861198aa1ad70a7f7a4d4ec09995 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Forms used for the administration and managemement of the cache setup.
  18. *
  19. * This file is part of Moodle's cache API, affectionately called MUC.
  20. *
  21. * @package core
  22. * @category cache
  23. * @copyright 2012 Sam Hemelryk
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. defined('MOODLE_INTERNAL') || die();
  27. require_once($CFG->dirroot.'/lib/formslib.php');
  28. /**
  29. * Add store instance form.
  30. *
  31. * @package core
  32. * @category cache
  33. * @copyright 2012 Sam Hemelryk
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class cachestore_addinstance_form extends moodleform {
  37. /**
  38. * The definition of the add instance form
  39. */
  40. protected final function definition() {
  41. $form = $this->_form;
  42. $store = $this->_customdata['store'];
  43. $plugin = $this->_customdata['plugin'];
  44. $locks = $this->_customdata['locks'];
  45. $form->addElement('hidden', 'plugin', $plugin);
  46. $form->setType('plugin', PARAM_PLUGIN);
  47. $form->addElement('hidden', 'editing', !empty($this->_customdata['store']));
  48. $form->setType('editing', PARAM_BOOL);
  49. if (!$store) {
  50. $form->addElement('text', 'name', get_string('storename', 'cache'));
  51. $form->addHelpButton('name', 'storename', 'cache');
  52. $form->addRule('name', get_string('required'), 'required');
  53. $form->setType('name', PARAM_NOTAGS);
  54. } else {
  55. $form->addElement('hidden', 'name', $store);
  56. $form->addElement('static', 'name-value', get_string('storename', 'cache'), $store);
  57. $form->setType('name', PARAM_NOTAGS);
  58. }
  59. if (is_array($locks)) {
  60. $form->addElement('select', 'lock', get_string('locking', 'cache'), $locks);
  61. $form->addHelpButton('lock', 'locking', 'cache');
  62. $form->setType('lock', PARAM_ALPHANUMEXT);
  63. } else {
  64. $form->addElement('hidden', 'lock', '');
  65. $form->setType('lock', PARAM_ALPHANUMEXT);
  66. $form->addElement('static', 'lock-value', get_string('locking', 'cache'),
  67. '<em>'.get_string('nativelocking', 'cache').'</em>');
  68. }
  69. if (method_exists($this, 'configuration_definition')) {
  70. $form->addElement('header', 'storeconfiguration', get_string('storeconfiguration', 'cache'));
  71. $this->configuration_definition();
  72. }
  73. $this->add_action_buttons();
  74. }
  75. /**
  76. * Validates the add instance form data
  77. *
  78. * @param array $data
  79. * @param array $files
  80. * @return array
  81. */
  82. public function validation($data, $files) {
  83. $errors = parent::validation($data, $files);
  84. if (!array_key_exists('name', $errors)) {
  85. if (!preg_match('#^[a-zA-Z0-9\-_ ]+$#', $data['name'])) {
  86. $errors['name'] = get_string('storenameinvalid', 'cache');
  87. } else if (empty($this->_customdata['store'])) {
  88. $stores = core_cache\administration_helper::get_store_instance_summaries();
  89. if (array_key_exists($data['name'], $stores)) {
  90. $errors['name'] = get_string('storenamealreadyused', 'cache');
  91. }
  92. }
  93. }
  94. if (method_exists($this, 'configuration_validation')) {
  95. $newerrors = $this->configuration_validation($data, $files, $errors);
  96. // We need to selectiviliy merge here
  97. foreach ($newerrors as $element => $error) {
  98. if (!array_key_exists($element, $errors)) {
  99. $errors[$element] = $error;
  100. }
  101. }
  102. }
  103. return $errors;
  104. }
  105. }
  106. /**
  107. * Form to set definition mappings
  108. *
  109. * @package core
  110. * @category cache
  111. * @copyright 2012 Sam Hemelryk
  112. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  113. */
  114. class cache_definition_mappings_form extends moodleform {
  115. /**
  116. * The definition of the form
  117. */
  118. protected final function definition() {
  119. global $OUTPUT;
  120. $definition = $this->_customdata['definition'];
  121. $form = $this->_form;
  122. list($component, $area) = explode('/', $definition, 2);
  123. list($currentstores, $storeoptions, $defaults) =
  124. core_cache\administration_helper::get_definition_store_options($component, $area);
  125. $storedata = core_cache\administration_helper::get_definition_summaries();
  126. if ($storedata[$definition]['mode'] != cache_store::MODE_REQUEST) {
  127. if (isset($storedata[$definition]['canuselocalstore']) && $storedata[$definition]['canuselocalstore']) {
  128. $form->addElement('html', $OUTPUT->notification(get_string('localstorenotification', 'cache'), 'notifymessage'));
  129. } else {
  130. $form->addElement('html', $OUTPUT->notification(get_string('sharedstorenotification', 'cache'), 'notifymessage'));
  131. }
  132. }
  133. $form->addElement('hidden', 'definition', $definition);
  134. $form->setType('definition', PARAM_SAFEPATH);
  135. $form->addElement('hidden', 'action', 'editdefinitionmapping');
  136. $form->setType('action', PARAM_ALPHA);
  137. $requiredoptions = max(3, count($currentstores)+1);
  138. $requiredoptions = min($requiredoptions, count($storeoptions));
  139. $options = array('' => get_string('none'));
  140. foreach ($storeoptions as $option => $def) {
  141. $options[$option] = $option;
  142. if ($def['default']) {
  143. $options[$option] .= ' '.get_string('mappingdefault', 'cache');
  144. }
  145. }
  146. for ($i = 0; $i < $requiredoptions; $i++) {
  147. $title = '...';
  148. if ($i === 0) {
  149. $title = get_string('mappingprimary', 'cache');
  150. } else if ($i === $requiredoptions-1) {
  151. $title = get_string('mappingfinal', 'cache');
  152. }
  153. $form->addElement('select', 'mappings['.$i.']', $title, $options);
  154. }
  155. $i = 0;
  156. foreach ($currentstores as $store => $def) {
  157. $form->setDefault('mappings['.$i.']', $store);
  158. $i++;
  159. }
  160. if (!empty($defaults)) {
  161. $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'),
  162. html_writer::tag('strong', join(', ', $defaults)));
  163. $form->addHelpButton('defaults', 'defaultmappings', 'cache');
  164. }
  165. $this->add_action_buttons();
  166. }
  167. }
  168. /**
  169. * Form to set definition sharing option
  170. *
  171. * @package core
  172. * @category cache
  173. * @copyright 2013 Sam Hemelryk
  174. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  175. */
  176. class cache_definition_sharing_form extends moodleform {
  177. /**
  178. * The definition of the form
  179. */
  180. protected final function definition() {
  181. $definition = $this->_customdata['definition'];
  182. $sharingoptions = $this->_customdata['sharingoptions'];
  183. $form = $this->_form;
  184. $form->addElement('hidden', 'definition', $definition);
  185. $form->setType('definition', PARAM_SAFEPATH);
  186. $form->addElement('hidden', 'action', 'editdefinitionsharing');
  187. $form->setType('action', PARAM_ALPHA);
  188. // We use a group here for validation.
  189. $count = 0;
  190. $group = array();
  191. foreach ($sharingoptions as $value => $text) {
  192. $count++;
  193. $group[] = $form->createElement('checkbox', $value, null, $text);
  194. }
  195. $form->addGroup($group, 'sharing', get_string('sharing', 'cache'), '<br />');
  196. $form->setType('sharing', PARAM_INT);
  197. $form->addElement('text', 'userinputsharingkey', get_string('userinputsharingkey', 'cache'));
  198. $form->addHelpButton('userinputsharingkey', 'userinputsharingkey', 'cache');
  199. $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_INPUT.']', 'notchecked');
  200. $form->setType('userinputsharingkey', PARAM_ALPHANUMEXT);
  201. $values = array_keys($sharingoptions);
  202. if (in_array(cache_definition::SHARING_ALL, $values)) {
  203. // If you share with all thenthe other options don't really make sense.
  204. foreach ($values as $value) {
  205. $form->disabledIf('sharing['.$value.']', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
  206. }
  207. $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
  208. }
  209. $this->add_action_buttons();
  210. }
  211. /**
  212. * Sets the data for this form.
  213. *
  214. * @param array $data
  215. */
  216. public function set_data($data) {
  217. if (!isset($data['sharing'])) {
  218. // Set the default value here. mforms doesn't handle defaults very nicely.
  219. $data['sharing'] = core_cache\administration_helper::get_definition_sharing_options(cache_definition::SHARING_DEFAULT);
  220. }
  221. parent::set_data($data);
  222. }
  223. /**
  224. * Validates this form
  225. *
  226. * @param array $data
  227. * @param array $files
  228. * @return array
  229. */
  230. public function validation($data, $files) {
  231. $errors = parent::validation($data, $files);
  232. if (count($errors) === 0 && !isset($data['sharing'])) {
  233. // They must select at least one sharing option.
  234. $errors['sharing'] = get_string('sharingrequired', 'cache');
  235. }
  236. return $errors;
  237. }
  238. }
  239. /**
  240. * Form to set the mappings for a mode.
  241. *
  242. * @package core
  243. * @category cache
  244. * @copyright 2012 Sam Hemelryk
  245. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  246. */
  247. class cache_mode_mappings_form extends moodleform {
  248. /**
  249. * The definition of the form
  250. */
  251. protected function definition() {
  252. $form = $this->_form;
  253. $stores = $this->_customdata;
  254. $options = array(
  255. cache_store::MODE_APPLICATION => array(),
  256. cache_store::MODE_SESSION => array(),
  257. cache_store::MODE_REQUEST => array()
  258. );
  259. foreach ($stores as $storename => $store) {
  260. foreach ($store['modes'] as $mode => $enabled) {
  261. if ($enabled && ($mode !== cache_store::MODE_SESSION || $store['supports']['searchable'])) {
  262. if (empty($store['default'])) {
  263. $options[$mode][$storename] = $store['name'];
  264. } else {
  265. $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
  266. }
  267. }
  268. }
  269. }
  270. $form->addElement('hidden', 'action', 'editmodemappings');
  271. $form->setType('action', PARAM_ALPHA);
  272. foreach ($options as $mode => $optionset) {
  273. $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
  274. }
  275. $this->add_action_buttons();
  276. }
  277. }
  278. /**
  279. * Form to add a cache lock instance.
  280. *
  281. * All cache lock plugins that wish to have custom configuration should override
  282. * this form, and more explicitly the plugin_definition and plugin_validation methods.
  283. *
  284. * @package core
  285. * @category cache
  286. * @copyright 2013 Sam Hemelryk
  287. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  288. */
  289. class cache_lock_form extends moodleform {
  290. /**
  291. * Defines this form.
  292. */
  293. final public function definition() {
  294. $plugin = $this->_customdata['lock'];
  295. $this->_form->addElement('hidden', 'action', 'newlockinstance');
  296. $this->_form->setType('action', PARAM_ALPHANUMEXT);
  297. $this->_form->addElement('hidden', 'lock', $plugin);
  298. $this->_form->setType('lock', PARAM_COMPONENT);
  299. $this->_form->addElement('text', 'name', get_string('lockname', 'cache'));
  300. $this->_form->setType('name', PARAM_ALPHANUMEXT);
  301. $this->_form->addRule('name', get_string('required'), 'required');
  302. $this->_form->addElement('static', 'namedesc', '', get_string('locknamedesc', 'cache'));
  303. $this->plugin_definition();
  304. $this->add_action_buttons();
  305. }
  306. /**
  307. * Validates this form.
  308. *
  309. * @param array $data
  310. * @param array $files
  311. * @return array
  312. */
  313. final public function validation($data, $files) {
  314. $errors = parent::validation($data, $files);
  315. if (!isset($errors['name'])) {
  316. $config = cache_config::instance();
  317. if (in_array($data['name'], array_keys($config->get_locks()))) {
  318. $errors['name'] = get_string('locknamenotunique', 'cache');
  319. }
  320. }
  321. $errors = $this->plugin_validation($data, $files, $errors);
  322. return $errors;
  323. }
  324. /**
  325. * Plugin specific definition.
  326. */
  327. public function plugin_definition() {
  328. // No custom validation going on here.
  329. }
  330. /**
  331. * Plugin specific validation.
  332. *
  333. * @param array $data
  334. * @param array $files
  335. * @param array $errors
  336. * @return array
  337. */
  338. public function plugin_validation($data, $files, array $errors) {
  339. return $errors;
  340. }
  341. }