MDL-19211 back to the old text format preferences logic + minor refactoring and improvements

This commit is contained in:
Petr Skoda
2010-07-10 12:12:59 +00:00
parent e065ea4f06
commit 20e5da7d91
19 changed files with 126 additions and 151 deletions
+1 -1
View File
@@ -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);
}
+2 -4
View File
@@ -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');
+8 -9
View File
@@ -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'));
-5
View File
@@ -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()) {
+1 -8
View File
@@ -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;
-2
View File
@@ -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';
+3 -3
View File
@@ -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);
+1
View File
@@ -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);
}
+1 -1
View File
@@ -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 = '';
+90 -27
View File
@@ -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;
}
+1 -1
View File
@@ -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'});
+1 -1
View File
@@ -143,7 +143,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element {
$str = $this->_getTabs();
$str .= '<div>';
$editor = get_preferred_texteditor($format);
$editor = editors_get_preferred_editor($format);
$strformats = format_text_menu();
$formats = $editor->get_supported_formats();
foreach ($formats as $fid) {
+1 -69
View File
@@ -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);
}
}
+1 -5
View File
@@ -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();
}
/**
+1 -1
View File
@@ -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) {
+1 -1
View File
@@ -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) {
+2 -2
View File
@@ -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,
+5 -10
View File
@@ -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);
+6 -1
View File
@@ -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)) {