From 20e5da7d91d84616264a46bb69eb6d6477c0ee4c Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 10 Jul 2010 12:12:59 +0000 Subject: [PATCH] MDL-19211 back to the old text format preferences logic + minor refactoring and improvements --- admin/editors.php | 2 +- admin/settings/plugins.php | 6 +- admin/uploaduser_form.php | 17 ++-- admin/user/user_bulk_message.php | 5 - calendar/lib.php | 9 +- lang/en/admin.php | 2 - lib/adminlib.php | 6 +- lib/db/upgrade.php | 1 + lib/deprecatedlib.php | 2 +- lib/editorlib.php | 117 ++++++++++++++++++------ lib/filelib.php | 2 +- lib/form/editor.php | 2 +- lib/form/format.php | 70 +------------- mod/assignment/lib.php | 6 +- mod/data/field/textarea/field.class.php | 2 +- mod/data/templates.php | 2 +- mod/forum/post.php | 4 +- mod/forum/restorelib.php | 15 +-- user/editlib.php | 7 +- 19 files changed, 126 insertions(+), 151 deletions(-) diff --git a/admin/editors.php b/admin/editors.php index 4604e4c5db8..476df6cd5f8 100644 --- a/admin/editors.php +++ b/admin/editors.php @@ -17,7 +17,7 @@ $action = optional_param('action', '', PARAM_ACTION); $editor = optional_param('editor', '', PARAM_SAFEDIR); // get currently installed and enabled auth plugins -$available_editors = get_available_editors(); +$available_editors = editors_get_available(); if (!empty($editor) and empty($available_editors[$editor])) { redirect ($returnurl); } diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index 611ac11ae33..3425105e277 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -133,12 +133,10 @@ if ($hassiteconfig) { $ADMIN->add('modules', new admin_category('editorsettings', get_string('editors', 'editor'))); $temp = new admin_settingpage('manageeditors', get_string('editorsettings', 'editor')); $temp->add(new admin_setting_manageeditors()); - $htmleditors = get_available_editors(); - $temp->add(new admin_setting_configselect('defaulthtmleditor', get_string('defaulthtmleditor', 'admin'), null, 'tinymce', $htmleditors)); - $temp->add(new admin_setting_configcheckbox('htmleditor', get_string('usehtmleditor', 'admin'), get_string('confightmleditor','admin'), 1)); + $htmleditors = editors_get_available(); $ADMIN->add('editorsettings', $temp); - $editors_available = get_available_editors(); + $editors_available = editors_get_available(); foreach ($editors_available as $editor=>$editorstr) { if (file_exists($CFG->dirroot . '/lib/editor/'.$editor.'/settings.php')) { $settings = new admin_settingpage('editorsettings'.$editor, get_string('pluginname', 'editor_'.$editor), 'moodle/site:config'); diff --git a/admin/uploaduser_form.php b/admin/uploaduser_form.php index 29d3cb3d9f8..3dbc42eee0a 100644 --- a/admin/uploaduser_form.php +++ b/admin/uploaduser_form.php @@ -50,9 +50,6 @@ class admin_uploaduser_form2 extends moodleform { function definition (){ global $CFG, $USER; - //no editors here - we need proper empty fields - $CFG->htmleditor = null; - $mform =& $this->_form; $columns =& $this->_customdata; @@ -188,16 +185,18 @@ class admin_uploaduser_form2 extends moodleform { $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices); $mform->setDefault('autosubscribe', 1); -/* TODO: reimplement editor preferences - if ($CFG->htmleditor) { - $choices = array(0 => get_string('texteditor'), 1 => get_string('htmleditor')); + $editors = editors_get_enabled(); + if (count($editors) > 1) { + $choices = array(); + $choices['0'] = get_string('texteditor'); + $choices['1'] = get_string('htmleditor'); $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices); $mform->setDefault('htmleditor', 1); } else { - $mform->addElement('static', 'htmleditor', get_string('textediting'), get_string('texteditor')); + $mform->addElement('hidden', 'htmleditor'); + $mform->setDefault('htmleditor', 1); + $mform->setType('htmleditor', PARAM_INT); } - $mform->setAdvanced('htmleditor'); -*/ if (empty($CFG->enableajax)) { $mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno')); diff --git a/admin/user/user_bulk_message.php b/admin/user/user_bulk_message.php index a739a01e3ca..2decac4c04e 100755 --- a/admin/user/user_bulk_message.php +++ b/admin/user/user_bulk_message.php @@ -34,11 +34,6 @@ if ($confirm and !empty($msg) and confirm_sesskey()) { redirect($return); } -// disable html editor if not enabled in preferences -if (!get_user_preferences('message_usehtmleditor', 0)) { - $CFG->htmleditor = ''; -} - $msgform = new user_message_form('user_bulk_message.php'); if ($msgform->is_cancelled()) { diff --git a/calendar/lib.php b/calendar/lib.php index 5eb2f53b591..5c899150778 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -1673,14 +1673,7 @@ class calendar_event { $data->description = $data->description['text']; } else if (empty($data->description)) { $data->description = ''; - } - - if (empty($data->format)) { - if (can_use_html_editor()) { - $data->format = FORMAT_HTML; - } else { - $data->format = FORMAT_MOODLE; - } + $data->format = editors_get_preferred_format(); } $this->properties = $data; diff --git a/lang/en/admin.php b/lang/en/admin.php index 979666bfdb4..d4122072e76 100755 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -225,7 +225,6 @@ $string['configgradebookroles'] = 'This setting allows you to control who appear $string['configgradeexport'] = 'Choose which gradebook export formats are your primary methods for exporting grades. Chosen plugins will then set and use a "last exported" field for every grade. For example, this might result in exported records being identified as being "new" or "updated". If you are not sure about this then leave everything unchecked.'; $string['configguestroleid'] = 'This role is automatically assigned to the guest user. It is also temporarily assigned to not enrolled users when they enter course that allows guests without password. Please verify that the role has moodle/legacy:guest and moodle/course:view capability.'; $string['confighiddenuserfields'] = 'Select which user information fields you wish to hide from other users other than course teachers/admins. This will increase student privacy. Hold CTRL key to select multiple fields.'; -$string['confightmleditor'] = 'Choose whether or not to allow use of the embedded HTML text editor. Even if you choose allow, this editor will only appear when the user is using a compatible web browser. Users can also choose not to use it.'; $string['configidnumber'] = 'This option specifies whether (a) Users are not be asked for an ID number at all, (b) Users are asked for an ID number but can leave it blank or (c) Users are asked for an ID Number and cannot leave it blank. If given the User\'s ID number is displayed in their Profile.'; $string['configintcachemax'] = 'For internal cache only. Maximum number of records to keep in the cache. Recommended value: 50. Use lower values to reduce memory usage.'; $string['configintro'] = 'On this page you can specify a number of configuration variables that help make Moodle work properly on your server. Don\'t worry too much about it - the defaults will usually work fine and you can always come back to this page later and change these settings.'; @@ -420,7 +419,6 @@ $string['debugstringids'] = 'Show origin of languages strings'; $string['debugvalidators'] = 'Show validator links'; $string['defaultallowedmodules'] = 'Default allowed modules'; $string['defaulthomepage'] = 'Default home page for users'; -$string['defaulthtmleditor'] = 'Default HTML editor'; $string['defaultrequestcategory'] = 'Default category for course requests'; $string['defaultsettinginfo'] = 'Default: {$a}'; $string['defaultuserroleid'] = 'Default role for all users'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 81730ca12ba..663395bda45 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -1690,7 +1690,7 @@ class admin_setting_confightmleditor extends admin_setting_configtext { $defaultinfo = "\n".$default; } - $editor = get_preferred_texteditor(FORMAT_HTML); + $editor = editors_get_preferred_editor(FORMAT_HTML); $editor->use_editor($this->get_id(), array('noclean'=>true)); return format_admin_setting($this, $this->visiblename, @@ -4929,7 +4929,7 @@ class admin_setting_manageeditors extends admin_setting { } $textlib = textlib_get_instance(); - $editors_available = get_available_editors(); + $editors_available = editors_get_available(); foreach ($editors_available as $editor=>$editorstr) { if (strpos($editor, $query) !== false) { return true; @@ -4956,7 +4956,7 @@ class admin_setting_manageeditors extends admin_setting { 'up', 'down', 'none')); $txt->updown = "$txt->up/$txt->down"; - $editors_available = get_available_editors(); + $editors_available = editors_get_available(); $active_editors = explode(',', $CFG->texteditors); $active_editors = array_reverse($active_editors); diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 28b829d2013..7596e9b03f1 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -4718,6 +4718,7 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); unset_config('editorkillword'); unset_config('editorhidebuttons'); unset_config('editorfontlist'); + unset_config('htmleditor'); upgrade_main_savepoint(true, 2010071000); } diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 9db5c732216..228ad187471 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -2761,7 +2761,7 @@ function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $v if ($usehtmleditor) { editors_head_setup(); - $editor = get_preferred_texteditor(FORMAT_HTML); + $editor = editors_get_preferred_editor(FORMAT_HTML); $editor->use_editor($id, array('legacy'=>true)); } else { $editorclass = ''; diff --git a/lib/editorlib.php b/lib/editorlib.php index e0942dd62da..cbcc06e7048 100644 --- a/lib/editorlib.php +++ b/lib/editorlib.php @@ -18,7 +18,7 @@ /** * Utility classes and functions for text editor integration. * - * @package moodlecore + * @package core * @subpackage editor * @copyright 2009 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -27,40 +27,42 @@ /** * Returns users preferred editor for given format * - * @todo implement user preferences for text editors - * - * @global object - * @global object * @param int $format text format or null of none * @return texteditor object */ -function get_preferred_texteditor($format=null) { - global $CFG, $USER; +function editors_get_preferred_editor($format = NULL) { + global $USER; - if (empty($CFG->texteditors)) { - $CFG->texteditors = 'tinymce,textarea'; - } - $active = explode(',', $CFG->texteditors); + $enabled = editors_get_enabled(); - // TODO: implement user preferences for text editors + $preventhtml = (count($enabled) > 1 and empty($USER->htmleditor)); // now find some plugin that supports format and is available $editor = false; - foreach ($active as $editorname) { - if (!$e = get_texteditor($editorname)) { - continue; - } + foreach ($enabled as $e) { if (!$e->supported_by_browser()) { // bad luck, this editor is not compatible continue; } + if ($preventhtml and $format == FORMAT_HTML and $e->get_preferred_format() == FORMAT_HTML) { + // this is really not what we want but we could use it if nothing better found + $editor = $e; + continue; + } if (!$supports = $e->get_supported_formats()) { + // buggy editor! continue; } if (is_null($format)) { // format does not matter - $editor = $e; - break; + if ($preventhtml and $e->get_preferred_format() == FORMAT_HTML) { + // this is really not what we want but we could use it if nothing better found + $editor = $e; + continue; + } else { + $editor = $e; + break; + } } if (in_array($format, $supports)) { // editor supports this format, yay! @@ -76,10 +78,59 @@ function get_preferred_texteditor($format=null) { return $editor; } +/** + * Returns users preferred text format. + * @return int standard text format + */ +function editors_get_preferred_format() { + global $USER; + + $editors = editors_get_enabled(); + if (count($editors) == 1) { + $editor = reset($editors); + return $editor->get_preferred_format(); + } + + foreach ($editors as $editor) { + if (empty($USER->htmleditor) and $editor->get_preferred_format() == FORMAT_HTML) { + // we do not prefer this one + continue; + } + return $editor->get_preferred_format(); + } + + // user did not want html editor, but there is no other choice, sorry + $editor = reset($editors); + return $editor->get_preferred_format(); +} + +/** + * Returns list of enabled text editors + * @return array of name=>texteditor + */ +function editors_get_enabled() { + global $CFG; + + if (empty($CFG->texteditors)) { + $CFG->texteditors = 'tinymce,textarea'; + } + $active = array(); + foreach(explode(',', $CFG->texteditors) as $e) { + if ($editor = get_texteditor($e)) { + $active[$e] = $editor; + } + } + + if (empty($active)) { + return array('textarea'=>get_texteditor('textarea')); // must exist and can edit anything + } + + return $active; +} + /** * Returns instance of text editor * - * @global object * @param string $editorname name of editor (textarea, tinymce, ...) * @return object|bool texeditor instance or false if does not exist */ @@ -103,7 +154,7 @@ function get_texteditor($editorname) { * * @return array Array ('editorname'=>'localised editor name') */ -function get_available_editors() { +function editors_get_available() { $editors = array(); foreach (get_plugin_list('editor') as $editorname => $dir) { $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname); @@ -184,16 +235,28 @@ abstract class texteditor { } } -//=== DEPRECATED ===================== +//=== TO BE DEPRECATED in 2.1 ===================== + /** - * can_use_html_editor is deprecated... - * @deprecated - * @todo Deprecated: eradicate completely, replace with something else + * Does the user want and can edit using rich text html editor? + * @todo Deprecate: eradicate completely, replace with something else in the future * @return bool */ function can_use_html_editor() { - //TODO: eradicate completely, replace with something else + global $USER; - $tinymyce = get_texteditor('tinymce'); - return $tinymyce->supported_by_browser(); + $editors = editors_get_enabled(); + if (count($editors) > 1) { + if (empty($USER->htmleditor)) { + return false; + } + } + + foreach ($editors as $editor) { + if ($editor->get_preferred_format() == FORMAT_HTML) { + return true; + } + } + + return false; } diff --git a/lib/filelib.php b/lib/filelib.php index 667553b5099..522b5ecee75 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -201,7 +201,7 @@ function file_prepare_standard_editor($data, $field, array $options, $context=nu $data->{$field} = ''; } if (!isset($data->{$field.'format'})) { - $data->{$field.'format'} = FORMAT_HTML; // TODO: use better default based on user preferences and browser capabilities + $data->{$field.'format'} = editors_get_preferred_format(); } if (!$options['noclean']) { $data->{$field} = clean_text($data->{$field}, $data->{$field.'format'}); diff --git a/lib/form/editor.php b/lib/form/editor.php index 16e58a20eaf..2dbe20e4ca9 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -143,7 +143,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $str = $this->_getTabs(); $str .= '
'; - $editor = get_preferred_texteditor($format); + $editor = editors_get_preferred_editor($format); $strformats = format_text_menu(); $formats = $editor->get_supported_formats(); foreach ($formats as $fid) { diff --git a/lib/form/format.php b/lib/form/format.php index 72e1845ab5e..fb67fd99d2d 100644 --- a/lib/form/format.php +++ b/lib/form/format.php @@ -10,12 +10,6 @@ require_once "$CFG->libdir/form/select.php"; */ class MoodleQuickForm_format extends MoodleQuickForm_select{ - /** - * Whether we are using html editor. - * - * @var unknown_type - */ - var $_useHtmlEditor; /** * Class constructor * @@ -29,69 +23,7 @@ class MoodleQuickForm_format extends MoodleQuickForm_select{ */ function MoodleQuickForm_format($elementName=null, $elementLabel=null, $attributes=null, $useHtmlEditor=null) { - if ($elementName == null){ - $elementName = 'format'; - } - if ($elementLabel == null){ - $elementLabel = get_string('format'); - } - HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes); - $this->_type = 'format'; - - $this->_useHtmlEditor=$useHtmlEditor; - if ($this->_useHtmlEditor === null){ - $this->_useHtmlEditor=can_use_html_editor(); - } - - $this->setPersistantFreeze($this->_useHtmlEditor); - if ($this->_useHtmlEditor){ - $this->freeze(); - } else { - $this->unfreeze(); - } + throw new coding_error('MFORMS: Coding error, text formats are handled only by new editor element.'); } //end constructor - /** - * Called by HTML_QuickForm whenever form event is made on this element - * - * @param string $event Name of event - * @param mixed $arg event arguments - * @param object $caller calling object - * @since 1.0 - * @access public - * @return mixed - */ - function onQuickFormEvent($event, $arg, &$caller) - { - switch ($event) { - case 'createElement': - $menu = format_text_menu(); - $this->load($menu); - break; - case 'updateValue' : - $value = $this->_findValue($caller->_constantValues); - if (null === $value) { - $value = $this->_findValue($caller->_submitValues); - // Fix for bug #4465 & #5269 - // XXX: should we push this to element::onQuickFormEvent()? - if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) { - $value = $this->_findValue($caller->_defaultValues); - } - } - if (null !== $value) { - $format=$value; - }else{ - $format=FORMAT_MOODLE; - } - if ($this->_useHtmlEditor){ - $this->setValue(array(FORMAT_HTML)); - }else{ - $this->setValue(array($format)); - } - return true; - break; - } - return parent::onQuickFormEvent($event, $arg, $caller); - } - } diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 9eaf7e757ce..fc3150334fe 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -135,11 +135,7 @@ class assignment_base { // get current group only when really needed /// Set up things for a HTML editor if it's needed - if ($this->usehtmleditor = can_use_html_editor()) { - $this->defaultformat = FORMAT_HTML; - } else { - $this->defaultformat = FORMAT_MOODLE; - } + $this->defaultformat = editors_get_preferred_format(); } /** diff --git a/mod/data/field/textarea/field.class.php b/mod/data/field/textarea/field.class.php index 6adbb13fa66..4d5b703c1ba 100755 --- a/mod/data/field/textarea/field.class.php +++ b/mod/data/field/textarea/field.class.php @@ -60,7 +60,7 @@ class data_field_textarea extends data_field_base { $format = FORMAT_PLAIN; } - $editor = get_preferred_texteditor($format); + $editor = editors_get_preferred_editor($format); $strformats = format_text_menu(); $formats = $editor->get_supported_formats(); foreach ($formats as $fid) { diff --git a/mod/data/templates.php b/mod/data/templates.php index 6e1a6d88a84..29d3c5448a6 100755 --- a/mod/data/templates.php +++ b/mod/data/templates.php @@ -157,7 +157,7 @@ if (empty($data->addtemplate) and empty($data->singletemplate) and editors_head_setup(); $format = FORMAT_HTML; -$editor = get_preferred_texteditor($format); +$editor = editors_get_preferred_editor($format); $strformats = format_text_menu(); $formats = $editor->get_supported_formats(); foreach ($formats as $fid) { diff --git a/mod/forum/post.php b/mod/forum/post.php index f258a6243b2..89128e8b410 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -138,7 +138,7 @@ if (!empty($forum)) { // User is starting a new discussion in a forum $post->subject = ''; $post->userid = $USER->id; $post->message = ''; - $post->messageformat = FORMAT_HTML; // TODO: better default + $post->messageformat = editors_get_preferred_format(); $post->messagetrust = 0; if (isset($groupid)) { @@ -537,7 +537,7 @@ $mform_post->set_data(array( 'attachments'=>$draftitemid, 'subject'=>$post->subject, 'message'=>array( 'text'=>$currenttext, - 'format'=>empty($post->messageformat) ? FORMAT_HTML : $post->messageformat, //TODO: add some better default + 'format'=>empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat, 'itemid'=>$draftid_editor ), 'subscribe'=>$subscribe?1:0, diff --git a/mod/forum/restorelib.php b/mod/forum/restorelib.php index b9be204bb2f..e529cdc9d9a 100644 --- a/mod/forum/restorelib.php +++ b/mod/forum/restorelib.php @@ -38,10 +38,10 @@ // | forum_posts // |-------------(UL,pk->id,fk->discussion, // | nt->parent,files) - // | - // | - // | - // forum_read + // | + // | + // | + // forum_read // (UL,pk->id,fk->post // // Meaning: pk->primary key field of the table @@ -150,11 +150,6 @@ //Load forum/lib.php require_once ($CFG->dirroot.'/mod/forum/lib.php'); // Calculate the default format - if (can_use_html_editor()) { - $defaultformat = FORMAT_HTML; - } else { - $defaultformat = FORMAT_MOODLE; - } //Create discussion/post data $sd = new stdClass; $sd->course = $forum->course; @@ -162,7 +157,7 @@ $sd->name = $forum->name; $sd->assessed = $forum->assessed; $sd->message = $forum->intro; - $sd->messageformat = $defaultformat; + $sd->messageformat = $forum->introformat; $sd->mailnow = false; //Insert dicussion/post data $sdid = forum_add_discussion($sd, $sd->intro, $forum); diff --git a/user/editlib.php b/user/editlib.php index e296b9f02fb..409c9bcfbcb 100644 --- a/user/editlib.php +++ b/user/editlib.php @@ -162,12 +162,17 @@ function useredit_shared_definition(&$mform, $editoroptions = null) { $mform->setDefault('trackforums', 0); } - if (!empty($CFG->htmleditor)) { + $editors = editors_get_enabled(); + if (count($editors) > 1) { $choices = array(); $choices['0'] = get_string('texteditor'); $choices['1'] = get_string('htmleditor'); $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices); $mform->setDefault('htmleditor', 1); + } else { + $mform->addElement('hidden', 'htmleditor'); + $mform->setDefault('htmleditor', 1); + $mform->setType('htmleditor', PARAM_INT); } if (empty($CFG->enableajax)) {