/lib/classes/plugininfo/qbehaviour.php

https://github.com/markn86/moodle · PHP · 139 lines · 80 code · 16 blank · 43 comment · 19 complexity · 9be013d00a234f66d3d30b47077a832f MD5 · raw file

  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. * Defines classes used for plugin info.
  18. *
  19. * @package core
  20. * @copyright 2011 David Mudrak <david@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace core\plugininfo;
  24. use moodle_url, core_plugin_manager;
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Class for question behaviours.
  28. */
  29. class qbehaviour extends base {
  30. /**
  31. * Finds all enabled plugins, the result may include missing plugins.
  32. * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  33. */
  34. public static function get_enabled_plugins() {
  35. $plugins = core_plugin_manager::instance()->get_installed_plugins('qbehaviour');
  36. if (!$plugins) {
  37. return array();
  38. }
  39. if ($disabled = get_config('question', 'disabledbehaviours')) {
  40. $disabled = explode(',', $disabled);
  41. } else {
  42. $disabled = array();
  43. }
  44. $enabled = array();
  45. foreach ($plugins as $plugin => $version) {
  46. if (in_array($plugin, $disabled)) {
  47. continue;
  48. }
  49. $enabled[$plugin] = $plugin;
  50. }
  51. return $enabled;
  52. }
  53. public static function enable_plugin(string $pluginname, int $enabled): bool {
  54. $haschanged = false;
  55. $plugins = [];
  56. $oldvalue = get_config('question', 'disabledbehaviours');
  57. if (!empty($oldvalue)) {
  58. $plugins = array_flip(explode(',', $oldvalue));
  59. }
  60. // Only set visibility if it's different from the current value.
  61. if ($enabled && array_key_exists($pluginname, $plugins)) {
  62. unset($plugins[$pluginname]);
  63. $haschanged = true;
  64. } else if (!$enabled && !array_key_exists($pluginname, $plugins)) {
  65. $plugins[$pluginname] = $pluginname;
  66. $haschanged = true;
  67. }
  68. if ($haschanged) {
  69. $new = implode(',', array_flip($plugins));
  70. add_to_config_log('disabledbehaviours', $oldvalue, $new, 'question');
  71. set_config('disabledbehaviours', $new, 'question');
  72. // Reset caches.
  73. \core_plugin_manager::reset_caches();
  74. }
  75. return $haschanged;
  76. }
  77. public function is_uninstall_allowed() {
  78. global $DB;
  79. if ($this->name === 'missing') {
  80. // qbehaviour_missing is used by the system. It cannot be uninstalled.
  81. return false;
  82. }
  83. return !$DB->record_exists('question_attempts', array('behaviour' => $this->name));
  84. }
  85. /**
  86. * Pre-uninstall hook.
  87. *
  88. * This is intended for disabling of plugin, some DB table purging, etc.
  89. *
  90. * NOTE: to be called from uninstall_plugin() only.
  91. * @private
  92. */
  93. public function uninstall_cleanup() {
  94. if ($disabledbehaviours = get_config('question', 'disabledbehaviours')) {
  95. $disabledbehaviours = explode(',', $disabledbehaviours);
  96. $disabledbehaviours = array_unique($disabledbehaviours);
  97. } else {
  98. $disabledbehaviours = array();
  99. }
  100. if (($key = array_search($this->name, $disabledbehaviours)) !== false) {
  101. unset($disabledbehaviours[$key]);
  102. set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
  103. }
  104. if ($behaviourorder = get_config('question', 'behavioursortorder')) {
  105. $behaviourorder = explode(',', $behaviourorder);
  106. $behaviourorder = array_unique($behaviourorder);
  107. } else {
  108. $behaviourorder = array();
  109. }
  110. if (($key = array_search($this->name, $behaviourorder)) !== false) {
  111. unset($behaviourorder[$key]);
  112. set_config('behavioursortorder', implode(',', $behaviourorder), 'question');
  113. }
  114. parent::uninstall_cleanup();
  115. }
  116. /**
  117. * Return URL used for management of plugins of this type.
  118. * @return moodle_url
  119. */
  120. public static function get_manage_url() {
  121. return new moodle_url('/admin/qbehaviours.php');
  122. }
  123. }