From 116ad39b7ac217c8f9c8bff4b5a4c8a8f83b5b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 18 Aug 2012 15:21:37 +0200 Subject: [PATCH] MDL-34877 add tinymce subplugin setting support Includes migration of spell related settings to spellchecker plugin. --- lib/editor/tinymce/adminlib.php | 9 ++++ lib/editor/tinymce/classes/plugin.php | 52 +++++++++++++++++++ lib/editor/tinymce/lang/en/editor_tinymce.php | 1 + .../tinymce/plugins/spellchecker/config.php | 7 ++- .../plugins/spellchecker/db/install.php | 32 ++++++++++++ .../plugins/spellchecker/db/upgrade.php | 40 ++++++++++++++ .../plugins/spellchecker/db/upgradelib.php | 41 +++++++++++++++ .../tinymce/plugins/spellchecker/lib.php | 3 +- .../tinymce/plugins/spellchecker/settings.php | 38 ++++++++++++++ .../tinymce/plugins/spellchecker/version.php | 2 +- lib/editor/tinymce/settings.php | 33 ++++++++---- 11 files changed, 243 insertions(+), 15 deletions(-) create mode 100644 lib/editor/tinymce/plugins/spellchecker/db/install.php create mode 100644 lib/editor/tinymce/plugins/spellchecker/db/upgrade.php create mode 100644 lib/editor/tinymce/plugins/spellchecker/db/upgradelib.php create mode 100644 lib/editor/tinymce/plugins/spellchecker/settings.php diff --git a/lib/editor/tinymce/adminlib.php b/lib/editor/tinymce/adminlib.php index 8e599d63b91..4d2dba28876 100644 --- a/lib/editor/tinymce/adminlib.php +++ b/lib/editor/tinymce/adminlib.php @@ -34,4 +34,13 @@ class plugininfo_tinymce extends plugininfo_base { public function get_uninstall_url() { return new moodle_url('/lib/editor/tinymce/subplugins.php', array('delete' => $this->name, 'sesskey' => sesskey())); } + + public function get_settings_url() { + global $CFG; + if (file_exists("$CFG->dirroot/lib/editor/tinymce/plugins/$this->name/settings.php")) { + return new moodle_url('/admin/settings.php', array('section'=>'tinymce'.$this->name.'settings')); + } else { + return null; + } + } } diff --git a/lib/editor/tinymce/classes/plugin.php b/lib/editor/tinymce/classes/plugin.php index 9179031e46d..8dca9b6a00f 100644 --- a/lib/editor/tinymce/classes/plugin.php +++ b/lib/editor/tinymce/classes/plugin.php @@ -37,6 +37,9 @@ abstract class editor_tinymce_plugin { /** @var string Plugin folder */ protected $plugin; + /** @var array Plugin settings */ + protected $config = null; + /** * @param string $plugin Name of folder */ @@ -44,6 +47,55 @@ abstract class editor_tinymce_plugin { $this->plugin = $plugin; } + /** + * Makes sure config is loaded and cached. + * @return void + */ + protected function load_config() { + if (!isset($this->config)) { + $name = $this->get_name(); + $this->config = get_config("tinymce_$name"); + } + } + + /** + * Returns plugin config value. + * @param string $name + * @param string $default value if config does not exist yet + * @return string value or default + */ + public function get_config($name, $default = null) { + $this->load_config(); + return isset($this->config->$name) ? $this->config->$name : $default; + } + + /** + * Sets plugin config value. + * @param string $name name of config + * @param string $value string config value, null means delete + * @return string value + */ + public function set_config($name, $value) { + $pluginname = $this->get_name(); + $this->load_config(); + if ($value === null) { + unset($this->config->$name); + } else { + $this->config->$name = $value; + } + set_config($name, $value, "tinymce_$pluginname"); + } + + /** + * Returns name of this tinymce plugin. + * @return string + */ + public function get_name() { + // All class names start with "tinymce_". + $words = explode('_', get_class($this), 2); + return $words[1]; + } + /** * Adjusts TinyMCE init parameters for this plugin. * diff --git a/lib/editor/tinymce/lang/en/editor_tinymce.php b/lib/editor/tinymce/lang/en/editor_tinymce.php index c52236fa153..1563b2ee746 100644 --- a/lib/editor/tinymce/lang/en/editor_tinymce.php +++ b/lib/editor/tinymce/lang/en/editor_tinymce.php @@ -29,6 +29,7 @@ $string['common:browsemedia'] = 'Find or upload a sound, video or applet...'; $string['fontselectlist'] = 'Available fonts list'; $string['media_dlg:filename'] = 'Filename'; $string['pluginname'] = 'TinyMCE HTML editor'; +$string['settings'] = 'General settings'; $string['subplugindeleteconfirm'] = 'You are about to completely delete TinyMCE subplugin \'{$a}\'. This will completely delete everything in the database associated with this subplugin. Are you SURE you want to continue?'; diff --git a/lib/editor/tinymce/plugins/spellchecker/config.php b/lib/editor/tinymce/plugins/spellchecker/config.php index 6e36f413384..4f1ac23e6e4 100644 --- a/lib/editor/tinymce/plugins/spellchecker/config.php +++ b/lib/editor/tinymce/plugins/spellchecker/config.php @@ -27,8 +27,11 @@ require('../../../../../config.php'); @error_reporting(E_ALL ^ E_NOTICE); // Hide notices even if Moodle is configured to show them. // General settings -$config['general.engine'] = get_config('editor_tinymce', 'spellengine') ? - get_config('editor_tinymce', 'spellengine') : 'GoogleSpell'; +$engine = get_config('tinymce_spellchecker', 'spellengine'); +if (!$engine) { + $engine = 'GoogleSpell'; +} +$config['general.engine'] = $engine; // GoogleSpell settings $config['GoogleSpell.proxyhost'] = isset($CFG->proxyhost) ? $CFG->proxyhost : ''; diff --git a/lib/editor/tinymce/plugins/spellchecker/db/install.php b/lib/editor/tinymce/plugins/spellchecker/db/install.php new file mode 100644 index 00000000000..dc659b5fdcc --- /dev/null +++ b/lib/editor/tinymce/plugins/spellchecker/db/install.php @@ -0,0 +1,32 @@ +. + +/** + * Spellchecker post install script. + * + * @package tinymce_spellchecker + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +function xmldb_tinymce_spellchecker_install() { + global $CFG, $DB; + require_once(__DIR__.'/upgradelib.php'); + + tinymce_spellchecker_migrate_settings(); +} diff --git a/lib/editor/tinymce/plugins/spellchecker/db/upgrade.php b/lib/editor/tinymce/plugins/spellchecker/db/upgrade.php new file mode 100644 index 00000000000..4012b1f4350 --- /dev/null +++ b/lib/editor/tinymce/plugins/spellchecker/db/upgrade.php @@ -0,0 +1,40 @@ +. + +/** + * Spellchecker upgrade script. + * + * @package tinymce_spellchecker + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +function xmldb_tinymce_spellchecker_upgrade($oldversion) { + global $CFG, $DB; + require_once(__DIR__.'/upgradelib.php'); + + $dbman = $DB->get_manager(); + + if ($oldversion < 2012051800) { + tinymce_spellchecker_migrate_settings(); + upgrade_plugin_savepoint(true, 2012051800, 'tinymce', 'spellchecker'); + } + + + return true; +} diff --git a/lib/editor/tinymce/plugins/spellchecker/db/upgradelib.php b/lib/editor/tinymce/plugins/spellchecker/db/upgradelib.php new file mode 100644 index 00000000000..0a99c1dc5b3 --- /dev/null +++ b/lib/editor/tinymce/plugins/spellchecker/db/upgradelib.php @@ -0,0 +1,41 @@ +. + +/** + * Spellchecker upgrade script. + * + * @package tinymce_spellchecker + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Migrate spell related settings from tinymce. + */ +function tinymce_spellchecker_migrate_settings() { + $engine = get_config('editor_tinymce', 'spellengine'); + if ($engine !== false) { + set_config('spellengine', $engine, 'tinymce_spellchecker'); + unset_config('spellengine', 'editor_tinymce'); + } + $list = get_config('editor_tinymce', 'spelllanguagelist'); + if ($list !== false) { + set_config('spelllanguagelist', $list, 'tinymce_spellchecker'); + unset_config('spelllanguagelist', 'editor_tinymce'); + } +} diff --git a/lib/editor/tinymce/plugins/spellchecker/lib.php b/lib/editor/tinymce/plugins/spellchecker/lib.php index 2755b438f2e..2a4cfde6788 100644 --- a/lib/editor/tinymce/plugins/spellchecker/lib.php +++ b/lib/editor/tinymce/plugins/spellchecker/lib.php @@ -30,8 +30,7 @@ class tinymce_spellchecker extends editor_tinymce_plugin { global $CFG; // Check at least one language is supported. - $config = $params['moodle_config']; - $spelllanguagelist = empty($config->spelllanguagelist) ? '' : $config->spelllanguagelist; + $spelllanguagelist = $this->get_config('spelllanguagelist', ''); if ($spelllanguagelist !== '') { // Add button after code button in advancedbuttons3. $added = $this->add_button_after($params, 3, 'spellchecker', 'code', false); diff --git a/lib/editor/tinymce/plugins/spellchecker/settings.php b/lib/editor/tinymce/plugins/spellchecker/settings.php new file mode 100644 index 00000000000..c4081e3a2ea --- /dev/null +++ b/lib/editor/tinymce/plugins/spellchecker/settings.php @@ -0,0 +1,38 @@ +. + +/** + * Spellchecker settings. + * + * @package tinymce_spellchecker + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +if ($ADMIN->fulltree) { + $options = array( + 'PSpell'=>'PSpell', + 'GoogleSpell'=>'Google Spell', + 'PSpellShell'=>'PSpellShell'); + $settings->add(new admin_setting_configselect('tinymce_spellchecker/spellengine', + get_string('spellengine', 'admin'), '', 'GoogleSpell', $options)); + $settings->add(new admin_setting_configtextarea('tinymce_spellchecker/spelllanguagelist', + get_string('spelllanguagelist', 'admin'), '', + '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,' . + 'Portuguese=pt,Spanish=es,Swedish=sv', PARAM_RAW)); +} diff --git a/lib/editor/tinymce/plugins/spellchecker/version.php b/lib/editor/tinymce/plugins/spellchecker/version.php index 83aac6d199e..0ba52d01eed 100644 --- a/lib/editor/tinymce/plugins/spellchecker/version.php +++ b/lib/editor/tinymce/plugins/spellchecker/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); // The current plugin version (Date: YYYYMMDDXX). -$plugin->version = 2012051701; +$plugin->version = 2012051800; // Required Moodle version. $plugin->requires = 2011112900; // Full name of the plugin (used for diagnostics). diff --git a/lib/editor/tinymce/settings.php b/lib/editor/tinymce/settings.php index 3d973e1788e..461e62f6296 100644 --- a/lib/editor/tinymce/settings.php +++ b/lib/editor/tinymce/settings.php @@ -24,18 +24,31 @@ defined('MOODLE_INTERNAL') || die; +$ADMIN->add('editorsettings', new admin_category('editortinymce', new lang_string('pluginname', 'editor_tinymce'))); + +$settings = new admin_settingpage('editorsettingstinymce', new lang_string('settings', 'editor_tinymce')); if ($ADMIN->fulltree) { - $options = array( - 'PSpell'=>'PSpell', - 'GoogleSpell'=>'Google Spell', - 'PSpellShell'=>'PSpellShell'); - $settings->add(new admin_setting_configselect('editor_tinymce/spellengine', - get_string('spellengine', 'admin'), '', 'GoogleSpell', $options)); - $settings->add(new admin_setting_configtextarea('editor_tinymce/spelllanguagelist', - get_string('spelllanguagelist', 'admin'), '', - '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,' . - 'Portuguese=pt,Spanish=es,Swedish=sv', PARAM_RAW)); $settings->add(new admin_setting_configtextarea('editor_tinymce/fontselectlist', get_string('fontselectlist', 'editor_tinymce'), '', 'Trebuchet=Trebuchet MS,Verdana,Arial,Helvetica,sans-serif;Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;Georgia=georgia,times new roman,times,serif;Tahoma=tahoma,arial,helvetica,sans-serif;Times New Roman=times new roman,times,serif;Verdana=verdana,arial,helvetica,sans-serif;Impact=impact;Wingdings=wingdings', PARAM_RAW)); } +$ADMIN->add('editortinymce', $settings); +unset($settings); + +$subplugins = get_plugin_list('tinymce'); +$disabled = array(); // Disabling of subplugins to be implemented later. +foreach ($subplugins as $name=>$dir) { + if (file_exists("$dir/settings.php")) { + $settings = new admin_settingpage('tinymce'.$name.'settings', new lang_string('pluginname', 'tinymce_'.$name), 'moodle/site:config', in_array($name, $disabled)); + // settings.php may create a subcategory or unset the settings completely. + include("$dir/settings.php"); + if ($settings) { + $ADMIN->add('editortinymce', $settings); + } + } +} +unset($subplugins); +unset($disabled); + +// TinyMCE does not have standard settings page. +$settings = null;