Successful integration of CKEditor

Successful integration of CKEditor

by Carrera PHP -
Number of replies: 13

Successful integration of CKEditor

The default HTMLArea editor of Moodle 1.9.x will work smoothly on IE and Gecko (Mozilla) browser but in it wasn’t so in my case. I use Google chrome and Firefox browsers for all my needs and I required Moodle to work seamlessly in all versions of IE starting from 6. Until recently, I was thinking that my HTMLArea would work in all IE versions; I didn't give it much thought and continued working with the content creation. The day before yesterday when I was working with IE specific template issues and had arrived at some solution, I found the HTMLArea failing to render in the edit pages when testing it with IE7 and IE8.

I know that Moodle 2.0 will have TinyMCE as its default editor and that we can also use TinyMCE within Moodle 1.9.x with some code modifications, but I wanted to use CKEditor because of its elegance and list of features. While googling for CKEditor, I found this link (http://docs.moodle.org/en/fck_editor) which is used for integrating older version of CKEditor with 1.9.x and for 1.8.x. Though this document was useful it had some information which needs modification in order to make CKEditor work.

Steps to installation

1. In moodle/lib/weblib.php replace the print_textarea function with the one provided.

2. Download CKEditor to Moodle/lib/editor (be sure to name the folder with the editor as "ckeditor"

3. Make sure in the print_textarea function that the path to ckeditor.js is correct If there is a change in your file structure

Code

function print_textarea( $usehtmleditor , $rows , $cols , $width , $height , $name , $value = '' , $courseid = 0 , $return = false , $id = '' ) {

/// $width and height are legacy fields and no longer used as pixels like they used to be.

/// However, you can set them to zero to override the mincols and minrows values below.

global $CFG , $COURSE , $HTTPSPAGEREQUIRED ;

static $scriptcount = 0 ; // For loading the htmlarea script only once.

$mincols = 65 ;

$minrows = 10 ;

$str = '' ;

if ( $id === '' ) {

$id = 'edit-' . $name ;

}

if ( empty ( $CFG ->editorsrc) ) { // for backward compatibility.

if ( empty ( $courseid )) {

$courseid = $COURSE ->id;

}

if ( $usehtmleditor ) {

if (! empty ( $courseid ) and has_capability( 'moodle/course:managefiles' , get_context_instance(CONTEXT_COURSE, $courseid ))) {

$httpsrequired = empty ( $HTTPSPAGEREQUIRED ) ? '' : '&httpsrequired=1' ;

// needed for course file area browsing in image insert plugin

$str .= ( $scriptcount < 1 ) ? '<script type="text/javascript" src="' .

$CFG ->httpswwwroot . '/lib/editor/ckeditor/ckeditor.js"></script>' . "\n" : '' ;

} else {

$httpsrequired = empty ( $HTTPSPAGEREQUIRED ) ? '' : '?httpsrequired=1' ;

$str .= ( $scriptcount < 1 ) ? '<script type="text/javascript" src="' .

$CFG ->httpswwwroot . '/lib/editor/ckeditor/ckeditor.js"></script>' . "\n" : '' ;

}

$str .= ( $scriptcount < 1 ) ? '<script type="text/javascript" src="' .

$CFG ->httpswwwroot . '/lib/editor/ckeditor/ckeditor.js"></script>' . "\n" : '' ;

$scriptcount ++;

$str .= '<script type="text/javascript" src="' . $CFG ->wwwroot . '/lib/editor/ckeditor/fckeditor.js"></script>' ;

if ( $height ) { // Usually with legacy calls

if ( $rows < $minrows ) {

$rows = $minrows ;

}

}

if ( $width ) { // Usually with legacy calls

if ( $cols < $mincols ) {

$cols = $mincols ;

}

}

}

}

$str .= '<textarea id="' . $id . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' ;

if ( $usehtmleditor ) {

$str .= htmlspecialchars( $value ); // needed for editing of cleaned text!

} else {

$str .= s( $value );

}

$str .= '</textarea>' . "\n" ;

$str .= " <script type=\"text/javascript\">

//<![CDATA[

CKEDITOR.replace( '" . $id . "',

{

skin : 'kama',

width : 600

});

//]]>

</script> " ;

if ( $return ) {

return $str ;

}

echo $str ;

}

I have specified the CKEditor width to be 600, in order to fix an IE6 specific bug related to Width of the Editor.

Try this code if you need CKEditor and let me know whether it’s working smoothly in your implementation.

Regards,

Carrera PHP

Average of ratings: -
In reply to Carrera PHP

Re: Successful integration of CKEditor

by Carrera PHP -

Replace the below code

CKEDITOR.replace( '" . $id . "',
     {
       skin : 'kama',
       width : 600
     });

with the following one to attain a complete control over the buttons of the CKEditor

	CKEDITOR.replace( '".$id."',
	    {
    	        skin : 'kama',
                width : 600,
                font_names : 'Arial;Times New Roman;Verdana',
		fontSize_sizes : '12/12px;18/18px;24/24px;36/36px;',						
		toolbar:
                    [
                        ['Source','-','Save','NewPage','Preview','-','Templates'],
                        ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
                        ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
                        ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
                        '/',
                        ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
                        ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
                        ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                        ['Link','Unlink','Anchor'],
                        ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
                        '/',
                        ['Styles','Format','Font','FontSize'],
                        ['TextColor','BGColor'],
                        ['Maximize', 'ShowBlocks','-','About']
                    ]
        });
	CKEDITOR.config.font_defaultLabel = 'Arial';
	CKEDITOR.config.fontSize_defaultLabel = '12px';

To know more about the customization, try the following link:
CKEditor Javascript API

In reply to Carrera PHP

Re: Successful integration of CKEditor

by Max UDA -
I did everything you point out, but I can not do that work. Htmlarea editor remains the default editor.
What am I doing wrong?
Am I missing something?
Thanks

In reply to Max UDA

Re: Successful integration of CKEditor

by Carrera PHP -
@Max,

CKEditor is working perfectly along with Moodle 1.9.7 in All the browsers, Can you explain more about the changes made...

Can you attach the weblib.php file? if yes, attach as a normal *.txt , *.phps or *.7z(zipped). Please do not use other file formats to attach...



Regards,
Carrera PHP
In reply to Max UDA

Re: Successful integration of CKEditor

by Max UDA -
All it´s working now.
It seems that everything was due to a cache problem.
thanks

In reply to Carrera PHP

Re: Successful integration of CKEditor

by Patrick Bloch -
hi
i just try your code and it works perfectly, a great thanks.
Since i work 5 or 6 hours on moodle editor, it will be a great help.

But i have a little question about the "insert images" button.
it dasn't work as before, no link to the image folder and no upload possible.
since i am new on moodle i may (must) have made a mistake.

thanks a lot in advance if you can help me.

sorry for my "bad" english.

best regards
Patrick
In reply to Patrick Bloch

Re: Successful integration of CKEditor

by arsqqq Mars -
CKEDITOR.replace( '" . $id . "',
{
skin : 'kama',
width : 700,
filebrowserBrowseUrl:'../lib/editor/htmlarea/popups/insert_image.php',
filebrowserUploadUrl:'../lib/editor/htmlarea/popups/insert_image.php'
});

but though it does not solve the problem completely
In reply to arsqqq Mars

Re: Successful integration of CKEditor

by Mauno Korpelainen -

arsqqq,

if you use this kind of code students can upload any files they like to your site with image plugin which makes your site open to crackers as well wink

We discussed about integration of different editors (replacing HTMLArea with TinyMCE, FCKEditor or YUIRTE) about 2 years ago in some old posts and it's not only a question of replacing textarea with different editors. It's also a question about things like translation, modifications and security of plugins, changability of settings, roles...

File manager / file browser / file uploader plugin is one of those things that need both correct course id and different capability checks before they can be used safely - no matter what kind of editor or integrations method it is.

In reply to Mauno Korpelainen

Re: Successful integration of CKEditor

by Patrick Bloch -
thanks for your help
i finally decided to use tiny ..after a few integration it works quite fine thank to moodle forum again smile

In reply to Patrick Bloch

Re: Successful integration of CKEditor

by Vijay N -

Hi guys, 

 

I am little confused at how to add ckeditor to moodle 2.0. 

 

Where is this "print_textarea function" that is being referred to over here. I just download the ckeditor to moodle/lib/editor. I am not sure how to make the required changes as per the documentation?

 

Please can I have some guidance?

Thank you in advance.

V.

In reply to Vijay N

Re: Successful integration of CKEditor

by Vijay N -

Moodle 2.0 does not have a print_textarea function listed inside weblib.php. Any help with where the text editor is loaded?

 

V.

Average of ratings: Useful (1)
In reply to Carrera PHP

Re: Successful integration of CKEditor

by Sam Nasser -

works great and fine. thank you for your usefull tip. Although i have one question, when i click on image to insert image, i don't have a button to upload my image! so  CKEditor needs CKFinder to enable uploading of images. So my question is, it there something i can do to enable inserting images? or in another question, how to integrate ckfinder with ckeditor?

 

Regards.