PageRenderTime 61ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/cache/locallib.php

https://bitbucket.org/moodle/moodle
PHP | 674 lines | 434 code | 41 blank | 199 comment | 62 complexity | bed37f301d2057f550965340f7fcb0d7 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. * The supplementary cache API.
  18. *
  19. * This file is part of Moodle's cache API, affectionately called MUC.
  20. * It contains elements of the API that are not required in order to use caching.
  21. * Things in here are more in line with administration and management of the cache setup and configuration.
  22. *
  23. * @package core
  24. * @category cache
  25. * @copyright 2012 Sam Hemelryk
  26. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27. */
  28. defined('MOODLE_INTERNAL') || die();
  29. /**
  30. * Cache configuration writer.
  31. *
  32. * This class should only be used when you need to write to the config, all read operations exist within the cache_config.
  33. *
  34. * @package core
  35. * @category cache
  36. * @copyright 2012 Sam Hemelryk
  37. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38. */
  39. class cache_config_writer extends cache_config {
  40. /**
  41. * Switch that gets set to true when ever a cache_config_writer instance is saving the cache configuration file.
  42. * If this is set to true when save is next called we must avoid the trying to save and instead return the
  43. * generated config so that is may be used instead of the file.
  44. * @var bool
  45. */
  46. protected static $creatingconfig = false;
  47. /**
  48. * Returns an instance of the configuration writer.
  49. *
  50. * @return cache_config_writer
  51. */
  52. public static function instance() {
  53. $factory = cache_factory::instance();
  54. return $factory->create_config_instance(true);
  55. }
  56. /**
  57. * Saves the current configuration.
  58. *
  59. * Exceptions within this function are tolerated but must be of type cache_exception.
  60. * They are caught during initialisation and written to the error log. This is required in order to avoid
  61. * infinite loop situations caused by the cache throwing exceptions during its initialisation.
  62. */
  63. protected function config_save() {
  64. global $CFG;
  65. static $confighash = '';
  66. $cachefile = static::get_config_file_path();
  67. $directory = dirname($cachefile);
  68. if ($directory !== $CFG->dataroot && !file_exists($directory)) {
  69. $result = make_writable_directory($directory, false);
  70. if (!$result) {
  71. throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Cannot create config directory. Check the permissions on your moodledata directory.');
  72. }
  73. }
  74. if (!file_exists($directory) || !is_writable($directory)) {
  75. throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Config directory is not writable. Check the permissions on the moodledata/muc directory.');
  76. }
  77. // Prepare a configuration array to store.
  78. $configuration = $this->generate_configuration_array();
  79. // Prepare the file content.
  80. $content = "<?php defined('MOODLE_INTERNAL') || die();\n \$configuration = ".var_export($configuration, true).";";
  81. // Do both file content and hash based detection because this might be called
  82. // many times within a single request.
  83. $hash = sha1($content);
  84. if (($hash === $confighash) || (file_exists($cachefile) && $content === file_get_contents($cachefile))) {
  85. // Config is unchanged so don't bother locking and writing.
  86. $confighash = $hash;
  87. return;
  88. }
  89. // We need to create a temporary cache lock instance for use here. Remember we are generating the config file
  90. // it doesn't exist and thus we can't use the normal API for this (it'll just try to use config).
  91. $lockconf = reset($this->configlocks);
  92. if ($lockconf === false) {
  93. debugging('Your cache configuration file is out of date and needs to be refreshed.', DEBUG_DEVELOPER);
  94. // Use the default
  95. $lockconf = array(
  96. 'name' => 'cachelock_file_default',
  97. 'type' => 'cachelock_file',
  98. 'dir' => 'filelocks',
  99. 'default' => true
  100. );
  101. }
  102. $factory = cache_factory::instance();
  103. $locking = $factory->create_lock_instance($lockconf);
  104. if ($locking->lock('configwrite', 'config', true)) {
  105. $tempcachefile = "{$cachefile}.tmp";
  106. // Its safe to use w mode here because we have already acquired the lock.
  107. $handle = fopen($tempcachefile, 'w');
  108. fwrite($handle, $content);
  109. fflush($handle);
  110. fclose($handle);
  111. $locking->unlock('configwrite', 'config');
  112. @chmod($tempcachefile, $CFG->filepermissions);
  113. rename($tempcachefile, $cachefile);
  114. // Tell PHP to recompile the script.
  115. core_component::invalidate_opcode_php_cache($cachefile);
  116. } else {
  117. throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Unable to open the cache config file.');
  118. }
  119. }
  120. /**
  121. * Generates a configuration array suitable to be written to the config file.
  122. * @return array
  123. */
  124. protected function generate_configuration_array() {
  125. $configuration = array();
  126. $configuration['siteidentifier'] = $this->siteidentifier;
  127. $configuration['stores'] = $this->configstores;
  128. $configuration['modemappings'] = $this->configmodemappings;
  129. $configuration['definitions'] = $this->configdefinitions;
  130. $configuration['definitionmappings'] = $this->configdefinitionmappings;
  131. $configuration['locks'] = $this->configlocks;
  132. return $configuration;
  133. }
  134. /**
  135. * Adds a plugin instance.
  136. *
  137. * This function also calls save so you should redirect immediately, or at least very shortly after
  138. * calling this method.
  139. *
  140. * @param string $name The name for the instance (must be unique)
  141. * @param string $plugin The name of the plugin.
  142. * @param array $configuration The configuration data for the plugin instance.
  143. * @return bool
  144. * @throws cache_exception
  145. */
  146. public function add_store_instance($name, $plugin, array $configuration = array()) {
  147. if (array_key_exists($name, $this->configstores)) {
  148. throw new cache_exception('Duplicate name specificed for cache plugin instance. You must provide a unique name.');
  149. }
  150. $class = 'cachestore_'.$plugin;
  151. if (!class_exists($class)) {
  152. $plugins = core_component::get_plugin_list_with_file('cachestore', 'lib.php');
  153. if (!array_key_exists($plugin, $plugins)) {
  154. throw new cache_exception('Invalid plugin name specified. The plugin does not exist or is not valid.');
  155. }
  156. $file = $plugins[$plugin];
  157. if (file_exists($file)) {
  158. require_once($file);
  159. }
  160. if (!class_exists($class)) {
  161. throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.');
  162. }
  163. }
  164. $reflection = new ReflectionClass($class);
  165. if (!$reflection->isSubclassOf('cache_store')) {
  166. throw new cache_exception('Invalid cache plugin specified. The plugin does not extend the required class.');
  167. }
  168. if (!$class::are_requirements_met()) {
  169. throw new cache_exception('Unable to add new cache plugin instance. The requested plugin type is not supported.');
  170. }
  171. $this->configstores[$name] = array(
  172. 'name' => $name,
  173. 'plugin' => $plugin,
  174. 'configuration' => $configuration,
  175. 'features' => $class::get_supported_features($configuration),
  176. 'modes' => $class::get_supported_modes($configuration),
  177. 'mappingsonly' => !empty($configuration['mappingsonly']),
  178. 'class' => $class,
  179. 'default' => false
  180. );
  181. if (array_key_exists('lock', $configuration)) {
  182. $this->configstores[$name]['lock'] = $configuration['lock'];
  183. unset($this->configstores[$name]['configuration']['lock']);
  184. }
  185. // Call instance_created()
  186. $store = new $class($name, $this->configstores[$name]['configuration']);
  187. $store->instance_created();
  188. $this->config_save();
  189. return true;
  190. }
  191. /**
  192. * Adds a new lock instance to the config file.
  193. *
  194. * @param string $name The name the user gave the instance. PARAM_ALHPANUMEXT
  195. * @param string $plugin The plugin we are creating an instance of.
  196. * @param string $configuration Configuration data from the config instance.
  197. * @throws cache_exception
  198. */
  199. public function add_lock_instance($name, $plugin, $configuration = array()) {
  200. if (array_key_exists($name, $this->configlocks)) {
  201. throw new cache_exception('Duplicate name specificed for cache lock instance. You must provide a unique name.');
  202. }
  203. $class = 'cachelock_'.$plugin;
  204. if (!class_exists($class)) {
  205. $plugins = core_component::get_plugin_list_with_file('cachelock', 'lib.php');
  206. if (!array_key_exists($plugin, $plugins)) {
  207. throw new cache_exception('Invalid lock name specified. The plugin does not exist or is not valid.');
  208. }
  209. $file = $plugins[$plugin];
  210. if (file_exists($file)) {
  211. require_once($file);
  212. }
  213. if (!class_exists($class)) {
  214. throw new cache_exception('Invalid lock plugin specified. The plugin does not contain the required class.');
  215. }
  216. }
  217. $reflection = new ReflectionClass($class);
  218. if (!$reflection->implementsInterface('cache_lock_interface')) {
  219. throw new cache_exception('Invalid lock plugin specified. The plugin does not implement the required interface.');
  220. }
  221. $this->configlocks[$name] = array_merge($configuration, array(
  222. 'name' => $name,
  223. 'type' => 'cachelock_'.$plugin,
  224. 'default' => false
  225. ));
  226. $this->config_save();
  227. }
  228. /**
  229. * Deletes a lock instance given its name.
  230. *
  231. * @param string $name The name of the plugin, PARAM_ALPHANUMEXT.
  232. * @return bool
  233. * @throws cache_exception
  234. */
  235. public function delete_lock_instance($name) {
  236. if (!array_key_exists($name, $this->configlocks)) {
  237. throw new cache_exception('The requested store does not exist.');
  238. }
  239. if ($this->configlocks[$name]['default']) {
  240. throw new cache_exception('You can not delete the default lock.');
  241. }
  242. foreach ($this->configstores as $store) {
  243. if (isset($store['lock']) && $store['lock'] === $name) {
  244. throw new cache_exception('You cannot delete a cache lock that is being used by a store.');
  245. }
  246. }
  247. unset($this->configlocks[$name]);
  248. $this->config_save();
  249. return true;
  250. }
  251. /**
  252. * Sets the mode mappings.
  253. *
  254. * These determine the default caches for the different modes.
  255. * This function also calls save so you should redirect immediately, or at least very shortly after
  256. * calling this method.
  257. *
  258. * @param array $modemappings
  259. * @return bool
  260. * @throws cache_exception
  261. */
  262. public function set_mode_mappings(array $modemappings) {
  263. $mappings = array(
  264. cache_store::MODE_APPLICATION => array(),
  265. cache_store::MODE_SESSION => array(),
  266. cache_store::MODE_REQUEST => array(),
  267. );
  268. foreach ($modemappings as $mode => $stores) {
  269. if (!array_key_exists($mode, $mappings)) {
  270. throw new cache_exception('The cache mode for the new mapping does not exist');
  271. }
  272. $sort = 0;
  273. foreach ($stores as $store) {
  274. if (!array_key_exists($store, $this->configstores)) {
  275. throw new cache_exception('The instance name for the new mapping does not exist');
  276. }
  277. if (array_key_exists($store, $mappings[$mode])) {
  278. throw new cache_exception('This cache mapping already exists');
  279. }
  280. $mappings[$mode][] = array(
  281. 'store' => $store,
  282. 'mode' => $mode,
  283. 'sort' => $sort++
  284. );
  285. }
  286. }
  287. $this->configmodemappings = array_merge(
  288. $mappings[cache_store::MODE_APPLICATION],
  289. $mappings[cache_store::MODE_SESSION],
  290. $mappings[cache_store::MODE_REQUEST]
  291. );
  292. $this->config_save();
  293. return true;
  294. }
  295. /**
  296. * Edits a give plugin instance.
  297. *
  298. * The plugin instance is determined by its name, hence you cannot rename plugins.
  299. * This function also calls save so you should redirect immediately, or at least very shortly after
  300. * calling this method.
  301. *
  302. * @param string $name
  303. * @param string $plugin
  304. * @param array $configuration
  305. * @return bool
  306. * @throws cache_exception
  307. */
  308. public function edit_store_instance($name, $plugin, $configuration) {
  309. if (!array_key_exists($name, $this->configstores)) {
  310. throw new cache_exception('The requested instance does not exist.');
  311. }
  312. $plugins = core_component::get_plugin_list_with_file('cachestore', 'lib.php');
  313. if (!array_key_exists($plugin, $plugins)) {
  314. throw new cache_exception('Invalid plugin name specified. The plugin either does not exist or is not valid.');
  315. }
  316. $class = 'cachestore_'.$plugin;
  317. $file = $plugins[$plugin];
  318. if (!class_exists($class)) {
  319. if (file_exists($file)) {
  320. require_once($file);
  321. }
  322. if (!class_exists($class)) {
  323. throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.'.$class);
  324. }
  325. }
  326. $this->configstores[$name] = array(
  327. 'name' => $name,
  328. 'plugin' => $plugin,
  329. 'configuration' => $configuration,
  330. 'features' => $class::get_supported_features($configuration),
  331. 'modes' => $class::get_supported_modes($configuration),
  332. 'mappingsonly' => !empty($configuration['mappingsonly']),
  333. 'class' => $class,
  334. 'default' => $this->configstores[$name]['default'] // Can't change the default.
  335. );
  336. if (array_key_exists('lock', $configuration)) {
  337. $this->configstores[$name]['lock'] = $configuration['lock'];
  338. unset($this->configstores[$name]['configuration']['lock']);
  339. }
  340. $this->config_save();
  341. return true;
  342. }
  343. /**
  344. * Deletes a store instance.
  345. *
  346. * This function also calls save so you should redirect immediately, or at least very shortly after
  347. * calling this method.
  348. *
  349. * @param string $name The name of the instance to delete.
  350. * @return bool
  351. * @throws cache_exception
  352. */
  353. public function delete_store_instance($name) {
  354. if (!array_key_exists($name, $this->configstores)) {
  355. throw new cache_exception('The requested store does not exist.');
  356. }
  357. if ($this->configstores[$name]['default']) {
  358. throw new cache_exception('The can not delete the default stores.');
  359. }
  360. foreach ($this->configmodemappings as $mapping) {
  361. if ($mapping['store'] === $name) {
  362. throw new cache_exception('You cannot delete a cache store that has mode mappings.');
  363. }
  364. }
  365. foreach ($this->configdefinitionmappings as $mapping) {
  366. if ($mapping['store'] === $name) {
  367. throw new cache_exception('You cannot delete a cache store that has definition mappings.');
  368. }
  369. }
  370. // Call instance_deleted()
  371. $class = 'cachestore_'.$this->configstores[$name]['plugin'];
  372. $store = new $class($name, $this->configstores[$name]['configuration']);
  373. $store->instance_deleted();
  374. unset($this->configstores[$name]);
  375. $this->config_save();
  376. return true;
  377. }
  378. /**
  379. * Creates the default configuration and saves it.
  380. *
  381. * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
  382. * be called when there is no configuration file already.
  383. *
  384. * @param bool $forcesave If set to true then we will forcefully save the default configuration file.
  385. * @return true|array Returns true if the default configuration was successfully created.
  386. * Returns a configuration array if it could not be saved. This is a bad situation. Check your error logs.
  387. */
  388. public static function create_default_configuration($forcesave = false) {
  389. // HACK ALERT.
  390. // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
  391. // default store plugins are protected from deletion.
  392. $writer = new self;
  393. $writer->configstores = self::get_default_stores();
  394. $writer->configdefinitions = self::locate_definitions();
  395. $writer->configmodemappings = array(
  396. array(
  397. 'mode' => cache_store::MODE_APPLICATION,
  398. 'store' => 'default_application',
  399. 'sort' => -1
  400. ),
  401. array(
  402. 'mode' => cache_store::MODE_SESSION,
  403. 'store' => 'default_session',
  404. 'sort' => -1
  405. ),
  406. array(
  407. 'mode' => cache_store::MODE_REQUEST,
  408. 'store' => 'default_request',
  409. 'sort' => -1
  410. )
  411. );
  412. $writer->configlocks = array(
  413. 'default_file_lock' => array(
  414. 'name' => 'cachelock_file_default',
  415. 'type' => 'cachelock_file',
  416. 'dir' => 'filelocks',
  417. 'default' => true
  418. )
  419. );
  420. $factory = cache_factory::instance();
  421. // We expect the cache to be initialising presently. If its not then something has gone wrong and likely
  422. // we are now in a loop.
  423. if (!$forcesave && $factory->get_state() !== cache_factory::STATE_INITIALISING) {
  424. return $writer->generate_configuration_array();
  425. }
  426. $factory->set_state(cache_factory::STATE_SAVING);
  427. $writer->config_save();
  428. return true;
  429. }
  430. /**
  431. * Returns an array of default stores for use.
  432. *
  433. * @return array
  434. */
  435. protected static function get_default_stores() {
  436. global $CFG;
  437. require_once($CFG->dirroot.'/cache/stores/file/lib.php');
  438. require_once($CFG->dirroot.'/cache/stores/session/lib.php');
  439. require_once($CFG->dirroot.'/cache/stores/static/lib.php');
  440. return array(
  441. 'default_application' => array(
  442. 'name' => 'default_application',
  443. 'plugin' => 'file',
  444. 'configuration' => array(),
  445. 'features' => cachestore_file::get_supported_features(),
  446. 'modes' => cachestore_file::get_supported_modes(),
  447. 'default' => true,
  448. ),
  449. 'default_session' => array(
  450. 'name' => 'default_session',
  451. 'plugin' => 'session',
  452. 'configuration' => array(),
  453. 'features' => cachestore_session::get_supported_features(),
  454. 'modes' => cachestore_session::get_supported_modes(),
  455. 'default' => true,
  456. ),
  457. 'default_request' => array(
  458. 'name' => 'default_request',
  459. 'plugin' => 'static',
  460. 'configuration' => array(),
  461. 'features' => cachestore_static::get_supported_features(),
  462. 'modes' => cachestore_static::get_supported_modes(),
  463. 'default' => true,
  464. )
  465. );
  466. }
  467. /**
  468. * Updates the default stores within the MUC config file.
  469. */
  470. public static function update_default_config_stores() {
  471. $factory = cache_factory::instance();
  472. $factory->updating_started();
  473. $config = $factory->create_config_instance(true);
  474. $config->configstores = array_merge($config->configstores, self::get_default_stores());
  475. $config->config_save();
  476. $factory->updating_finished();
  477. }
  478. /**
  479. * Updates the definition in the configuration from those found in the cache files.
  480. *
  481. * Calls config_save further down, you should redirect immediately or asap after calling this method.
  482. *
  483. * @param bool $coreonly If set to true only core definitions will be updated.
  484. */
  485. public static function update_definitions($coreonly = false) {
  486. $factory = cache_factory::instance();
  487. $factory->updating_started();
  488. $config = $factory->create_config_instance(true);
  489. $config->write_definitions_to_cache(self::locate_definitions($coreonly));
  490. $factory->updating_finished();
  491. }
  492. /**
  493. * Locates all of the definition files.
  494. *
  495. * @param bool $coreonly If set to true only core definitions will be updated.
  496. * @return array
  497. */
  498. protected static function locate_definitions($coreonly = false) {
  499. global $CFG;
  500. $files = array();
  501. if (file_exists($CFG->dirroot.'/lib/db/caches.php')) {
  502. $files['core'] = $CFG->dirroot.'/lib/db/caches.php';
  503. }
  504. if (!$coreonly) {
  505. $plugintypes = core_component::get_plugin_types();
  506. foreach ($plugintypes as $type => $location) {
  507. $plugins = core_component::get_plugin_list_with_file($type, 'db/caches.php');
  508. foreach ($plugins as $plugin => $filepath) {
  509. $component = clean_param($type.'_'.$plugin, PARAM_COMPONENT); // Standardised plugin name.
  510. $files[$component] = $filepath;
  511. }
  512. }
  513. }
  514. $definitions = array();
  515. foreach ($files as $component => $file) {
  516. $filedefs = self::load_caches_file($file);
  517. foreach ($filedefs as $area => $definition) {
  518. $area = clean_param($area, PARAM_AREA);
  519. $id = $component.'/'.$area;
  520. $definition['component'] = $component;
  521. $definition['area'] = $area;
  522. if (array_key_exists($id, $definitions)) {
  523. debugging('Error: duplicate cache definition found with id: '.$id, DEBUG_DEVELOPER);
  524. continue;
  525. }
  526. $definitions[$id] = $definition;
  527. }
  528. }
  529. return $definitions;
  530. }
  531. /**
  532. * Writes the updated definitions for the config file.
  533. * @param array $definitions
  534. */
  535. private function write_definitions_to_cache(array $definitions) {
  536. // Preserve the selected sharing option when updating the definitions.
  537. // This is set by the user and should never come from caches.php.
  538. foreach ($definitions as $key => $definition) {
  539. unset($definitions[$key]['selectedsharingoption']);
  540. unset($definitions[$key]['userinputsharingkey']);
  541. if (isset($this->configdefinitions[$key]) && isset($this->configdefinitions[$key]['selectedsharingoption'])) {
  542. $definitions[$key]['selectedsharingoption'] = $this->configdefinitions[$key]['selectedsharingoption'];
  543. }
  544. if (isset($this->configdefinitions[$key]) && isset($this->configdefinitions[$key]['userinputsharingkey'])) {
  545. $definitions[$key]['userinputsharingkey'] = $this->configdefinitions[$key]['userinputsharingkey'];
  546. }
  547. }
  548. $this->configdefinitions = $definitions;
  549. foreach ($this->configdefinitionmappings as $key => $mapping) {
  550. if (!array_key_exists($mapping['definition'], $definitions)) {
  551. unset($this->configdefinitionmappings[$key]);
  552. }
  553. }
  554. $this->config_save();
  555. }
  556. /**
  557. * Loads the caches file if it exists.
  558. * @param string $file Absolute path to the file.
  559. * @return array
  560. */
  561. private static function load_caches_file($file) {
  562. if (!file_exists($file)) {
  563. return array();
  564. }
  565. $definitions = array();
  566. include($file);
  567. return $definitions;
  568. }
  569. /**
  570. * Sets the mappings for a given definition.
  571. *
  572. * @param string $definition
  573. * @param array $mappings
  574. * @throws coding_exception
  575. */
  576. public function set_definition_mappings($definition, $mappings) {
  577. if (!array_key_exists($definition, $this->configdefinitions)) {
  578. throw new coding_exception('Invalid definition name passed when updating mappings.');
  579. }
  580. foreach ($mappings as $store) {
  581. if (!array_key_exists($store, $this->configstores)) {
  582. throw new coding_exception('Invalid store name passed when updating definition mappings.');
  583. }
  584. }
  585. foreach ($this->configdefinitionmappings as $key => $mapping) {
  586. if ($mapping['definition'] == $definition) {
  587. unset($this->configdefinitionmappings[$key]);
  588. }
  589. }
  590. $sort = count($mappings);
  591. foreach ($mappings as $store) {
  592. $this->configdefinitionmappings[] = array(
  593. 'store' => $store,
  594. 'definition' => $definition,
  595. 'sort' => $sort
  596. );
  597. $sort--;
  598. }
  599. $this->config_save();
  600. }
  601. /**
  602. * Update the site identifier stored by the cache API.
  603. *
  604. * @param string $siteidentifier
  605. * @return string The new site identifier.
  606. */
  607. public function update_site_identifier($siteidentifier) {
  608. $this->siteidentifier = md5((string)$siteidentifier);
  609. $this->config_save();
  610. return $this->siteidentifier;
  611. }
  612. /**
  613. * Sets the selected sharing options and key for a definition.
  614. *
  615. * @param string $definition The name of the definition to set for.
  616. * @param int $sharingoption The sharing option to set.
  617. * @param string|null $userinputsharingkey The user input key or null.
  618. * @throws coding_exception
  619. */
  620. public function set_definition_sharing($definition, $sharingoption, $userinputsharingkey = null) {
  621. if (!array_key_exists($definition, $this->configdefinitions)) {
  622. throw new coding_exception('Invalid definition name passed when updating sharing options.');
  623. }
  624. if (!($this->configdefinitions[$definition]['sharingoptions'] & $sharingoption)) {
  625. throw new coding_exception('Invalid sharing option passed when updating definition.');
  626. }
  627. $this->configdefinitions[$definition]['selectedsharingoption'] = (int)$sharingoption;
  628. if (!empty($userinputsharingkey)) {
  629. $this->configdefinitions[$definition]['userinputsharingkey'] = (string)$userinputsharingkey;
  630. }
  631. $this->config_save();
  632. }
  633. }