Moodle 2.2 How to disable editor from a user setting?

Moodle 2.2 How to disable editor from a user setting?

by John St -
Number of replies: 3

I'd like to make a user setting in a theme that would disable/enable tinymce for just that theme (using moodle 2.2). Any ideas? I know how to do a standard theme-based user setting, and I could probably force remove tiny via javascript, but I would rather not load it at all if the user decides they don't want it... 

Average of ratings: -
In reply to John St

Re: Moodle 2.2 How to disable editor from a user setting?

by Mauno Korpelainen -

I have done this in stylist theme with yui editor switcher that sets a user preference to database - it's working the same way as splash theme in changing colors from given options, check the code of Splash theme first. You can see stylist theme in action in http://korpelainen.net/m2math and if you want to check the files it's included my math plugin package ( http://korpelainen.net/mpluga.zip or http://korpelainen.net/mplugb.zip )

Attached editor switcher is included to theme/stylist/yui and you should of course use your custom theme in the switcher instead of "stylist".

In stylist lib.php I used

$CFG->texteditors = 'textarea';
if (stylist_get_editor()=='tinymath') {$CFG->texteditors = 'tinymath,textarea';}
if (stylist_get_editor()=='tinymce') {$CFG->texteditors = 'tinymce,textarea';}

/**
 * Adds the JavaScript for the editor switcher to the page.
 *
 * The editor switcher is a YUI moodle module that is located in
 *     theme/stylist/yui/editorswitcher/editorswitcher.js
 *
 * @param moodle_page $page
 */
function stylist_initialise_editorswitcher(moodle_page $page) {
    user_preference_allow_ajax_update('theme_stylist_chosen_editor', PARAM_ALPHA);
    $page->requires->yui_module('moodle-theme_stylist-editorswitcher', 'M.theme_stylist.initeditorswitcher', array(array('div'=>'#editorswitcher')));
}
/**
 * Gets the editor the user has selected, or the default if they have never changed
 *
 * @param string $default The default editor to use
 * @return string The editor the user has selected
 */
function stylist_get_editor($default='tinymath') {
    return get_user_preferences('theme_stylist_chosen_editor', $default);
}

/**
 * Checks if the user is switching editors with a refresh (JS disabled)
 *
 * If they are this updates the users preference in the database
 *
 * @return bool
 */
function stylist_check_editorswitch() {
    $changeeditor = optional_param('stylisteditor', null, PARAM_ALPHA);
    if (in_array($changeeditor, array('tinymath','tinymce','textarea'))) {
        return set_user_preference('theme_stylist_chosen_editor', $changeeditor);
    }
    return false;
}

and in theme layout files (or actuall one file extras.php that is required by all layout files)

stylist_check_editorswitch();
stylist_initialise_editorswitcher($PAGE);

and

$bodyclasses[] = 'stylist-'.stylist_get_editor();

and a dropdown list script

function menu_goto( menuform )
{
    selecteditem = menuform.newurl.selectedIndex ;
    newurl = menuform.newurl.options[ selecteditem ].value ;
    if (newurl.length != 0) {
      location.href = newurl ;
    }
}

together with a separate Aardwark style profile block

            <form action="dummyeditor">
<select name="newurl" onchange="menu_goto(this.form)">
<option value="" selected="selected">----- Select editor -----</option>
<option value="<?php echo new moodle_url($PAGE->url, array('stylisteditor'=>'tinymath')); ?>">Tinymath</option>
<option value="<?php echo new moodle_url($PAGE->url, array('stylisteditor'=>'tinymce')); ?>">TinyMCE</option>
<option value="<?php echo new moodle_url($PAGE->url, array('stylisteditor'=>'textarea')); ?>">No editor</option>
</select>
</form>

You could probably do it much easier with jQuery but since yui is the main library I have been using the yui user preferences that work otherwise ok but if editor is already initialized you need to use the switcher twice to get a result - if you change the page before rendering/not rendering editor it works like expected.

Switcher

Average of ratings: Useful (1)
In reply to Mauno Korpelainen

Re: Moodle 2.2 How to disable editor from a user setting?

by John St -

Brilliant! Thanks! While the editor is now great in IOS/android tablets, it is a bit problematic on the handheld screen. I'd like to give the user the option to turn it off/on.

Bonus question- Any way to maybe load the simple version of the editor instead of the full version? 

In reply to John St

Re: Moodle 2.2 How to disable editor from a user setting?

by Mauno Korpelainen -

In my opinion the best option would be to create a separate modified editor for mobiles/tablets - let's call it tinymobile or tinytablet to lib/editor and those editors could have this way a different init code with custom toolbars, buttons, css etc. Then you could change the editor according to device or user selections. That's how tinymath with math plugins is used instead of tinymce - or ckeditor...or any other pluggable editor

But it is also possible to create for example different editor skins and change only the editor skin, or to create different editor themes (current moodle is using advanced theme of tinymce with skin "o2k7" ).

Basicly it's a question of selecting correct init code for different cases - see for example http://fiddle.tinymce.com/raaaab where class of textarea is used as a editor theme selector and you can change/add the classes with tools like jQuery, right... smile

Or check http://fiddle.tinymce.com/qaaaab

This way you could swich off/on editor without the need to reload page - like with "toggle buttons" but changes would not be automatically saved to (yui) user preferences.