PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cache/forms.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 375 lines | 207 code | 43 blank | 125 comment | 30 complexity | b3c7a519f9f513b562da33ce88a36041 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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('lockmethod', 'cache'), $locks);
  61. $form->addHelpButton('lock', 'lockmethod', '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('lockmethod', '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 = 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. $definition = $this->_customdata['definition'];
  120. $form = $this->_form;
  121. list($component, $area) = explode('/', $definition, 2);
  122. list($currentstores, $storeoptions, $defaults) =
  123. cache_administration_helper::get_definition_store_options($component, $area);
  124. $form->addElement('hidden', 'definition', $definition);
  125. $form->setType('definition', PARAM_SAFEPATH);
  126. $form->addElement('hidden', 'action', 'editdefinitionmapping');
  127. $form->setType('action', PARAM_ALPHA);
  128. $requiredoptions = max(3, count($currentstores)+1);
  129. $requiredoptions = min($requiredoptions, count($storeoptions));
  130. $options = array('' => get_string('none'));
  131. foreach ($storeoptions as $option => $def) {
  132. $options[$option] = $option;
  133. if ($def['default']) {
  134. $options[$option] .= ' '.get_string('mappingdefault', 'cache');
  135. }
  136. }
  137. for ($i = 0; $i < $requiredoptions; $i++) {
  138. $title = '...';
  139. if ($i === 0) {
  140. $title = get_string('mappingprimary', 'cache');
  141. } else if ($i === $requiredoptions-1) {
  142. $title = get_string('mappingfinal', 'cache');
  143. }
  144. $form->addElement('select', 'mappings['.$i.']', $title, $options);
  145. }
  146. $i = 0;
  147. foreach ($currentstores as $store => $def) {
  148. $form->setDefault('mappings['.$i.']', $store);
  149. $i++;
  150. }
  151. if (!empty($defaults)) {
  152. $form->addElement('static', 'defaults', get_string('defaultmappings', 'cache'),
  153. html_writer::tag('strong', join(', ', $defaults)));
  154. $form->addHelpButton('defaults', 'defaultmappings', 'cache');
  155. }
  156. $this->add_action_buttons();
  157. }
  158. }
  159. /**
  160. * Form to set definition sharing option
  161. *
  162. * @package core
  163. * @category cache
  164. * @copyright 2013 Sam Hemelryk
  165. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  166. */
  167. class cache_definition_sharing_form extends moodleform {
  168. /**
  169. * The definition of the form
  170. */
  171. protected final function definition() {
  172. $definition = $this->_customdata['definition'];
  173. $sharingoptions = $this->_customdata['sharingoptions'];
  174. $form = $this->_form;
  175. $form->addElement('hidden', 'definition', $definition);
  176. $form->setType('definition', PARAM_SAFEPATH);
  177. $form->addElement('hidden', 'action', 'editdefinitionsharing');
  178. $form->setType('action', PARAM_ALPHA);
  179. // We use a group here for validation.
  180. $count = 0;
  181. $group = array();
  182. foreach ($sharingoptions as $value => $text) {
  183. $count++;
  184. $group[] = $form->createElement('checkbox', $value, null, $text);
  185. }
  186. $form->addGroup($group, 'sharing', get_string('sharing', 'cache'), '<br />');
  187. $form->setType('sharing', PARAM_INT);
  188. $form->addElement('text', 'userinputsharingkey', get_string('userinputsharingkey', 'cache'));
  189. $form->addHelpButton('userinputsharingkey', 'userinputsharingkey', 'cache');
  190. $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_INPUT.']', 'notchecked');
  191. $form->setType('userinputsharingkey', PARAM_ALPHANUMEXT);
  192. $values = array_keys($sharingoptions);
  193. if (in_array(cache_definition::SHARING_ALL, $values)) {
  194. // If you share with all thenthe other options don't really make sense.
  195. foreach ($values as $value) {
  196. $form->disabledIf('sharing['.$value.']', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
  197. }
  198. $form->disabledIf('userinputsharingkey', 'sharing['.cache_definition::SHARING_ALL.']', 'checked');
  199. }
  200. $this->add_action_buttons();
  201. }
  202. /**
  203. * Sets the data for this form.
  204. *
  205. * @param array $data
  206. */
  207. public function set_data($data) {
  208. if (!isset($data['sharing'])) {
  209. // Set the default value here. mforms doesn't handle defaults very nicely.
  210. $data['sharing'] = cache_administration_helper::get_definition_sharing_options(cache_definition::SHARING_DEFAULT);
  211. }
  212. parent::set_data($data);
  213. }
  214. /**
  215. * Validates this form
  216. *
  217. * @param array $data
  218. * @param array $files
  219. * @return array
  220. */
  221. public function validation($data, $files) {
  222. $errors = parent::validation($data, $files);
  223. if (count($errors) === 0 && !isset($data['sharing'])) {
  224. // They must select at least one sharing option.
  225. $errors['sharing'] = get_string('sharingrequired', 'cache');
  226. }
  227. return $errors;
  228. }
  229. }
  230. /**
  231. * Form to set the mappings for a mode.
  232. *
  233. * @package core
  234. * @category cache
  235. * @copyright 2012 Sam Hemelryk
  236. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  237. */
  238. class cache_mode_mappings_form extends moodleform {
  239. /**
  240. * The definition of the form
  241. */
  242. protected function definition() {
  243. $form = $this->_form;
  244. $stores = $this->_customdata;
  245. $options = array(
  246. cache_store::MODE_APPLICATION => array(),
  247. cache_store::MODE_SESSION => array(),
  248. cache_store::MODE_REQUEST => array()
  249. );
  250. foreach ($stores as $storename => $store) {
  251. foreach ($store['modes'] as $mode => $enabled) {
  252. if ($enabled && ($mode !== cache_store::MODE_SESSION || $store['supports']['searchable'])) {
  253. if (empty($store['default'])) {
  254. $options[$mode][$storename] = $store['name'];
  255. } else {
  256. $options[$mode][$storename] = get_string('store_'.$store['name'], 'cache');
  257. }
  258. }
  259. }
  260. }
  261. $form->addElement('hidden', 'action', 'editmodemappings');
  262. $form->setType('action', PARAM_ALPHA);
  263. foreach ($options as $mode => $optionset) {
  264. $form->addElement('select', 'mode_'.$mode, get_string('mode_'.$mode, 'cache'), $optionset);
  265. }
  266. $this->add_action_buttons();
  267. }
  268. }
  269. /**
  270. * Form to add a cache lock instance.
  271. *
  272. * All cache lock plugins that wish to have custom configuration should override
  273. * this form, and more explicitly the plugin_definition and plugin_validation methods.
  274. *
  275. * @package core
  276. * @category cache
  277. * @copyright 2013 Sam Hemelryk
  278. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  279. */
  280. class cache_lock_form extends moodleform {
  281. /**
  282. * Defines this form.
  283. */
  284. final public function definition() {
  285. $plugin = $this->_customdata['lock'];
  286. $this->_form->addElement('hidden', 'action', 'newlockinstance');
  287. $this->_form->setType('action', PARAM_ALPHANUMEXT);
  288. $this->_form->addElement('hidden', 'lock', $plugin);
  289. $this->_form->setType('lock', PARAM_COMPONENT);
  290. $this->_form->addElement('text', 'name', get_string('lockname', 'cache'));
  291. $this->_form->setType('name', PARAM_ALPHANUMEXT);
  292. $this->_form->addRule('name', get_string('required'), 'required');
  293. $this->_form->addElement('static', 'namedesc', '', get_string('locknamedesc', 'cache'));
  294. $this->plugin_definition();
  295. $this->add_action_buttons();
  296. }
  297. /**
  298. * Validates this form.
  299. *
  300. * @param array $data
  301. * @param array $files
  302. * @return array
  303. */
  304. final public function validation($data, $files) {
  305. $errors = parent::validation($data, $files);
  306. if (!isset($errors['name'])) {
  307. $config = cache_config::instance();
  308. if (in_array($data['name'], array_keys($config->get_locks()))) {
  309. $errors['name'] = get_string('locknamenotunique', 'cache');
  310. }
  311. }
  312. $errors = $this->plugin_validation($data, $files, $errors);
  313. return $errors;
  314. }
  315. /**
  316. * Plugin specific definition.
  317. */
  318. public function plugin_definition() {
  319. // No custom validation going on here.
  320. }
  321. /**
  322. * Plugin specific validation.
  323. *
  324. * @param array $data
  325. * @param array $files
  326. * @param array $errors
  327. * @return array
  328. */
  329. public function plugin_validation($data, $files, array $errors) {
  330. return $errors;
  331. }
  332. }