MDL-24507 support for the TinyMCE moodleemoticon plugin
authorDavid Mudrak <david.mudrak@gmail.com>
Wed, 27 Oct 2010 18:12:26 +0000 (18:12 +0000)
committerDavid Mudrak <david.mudrak@gmail.com>
Wed, 27 Oct 2010 18:12:26 +0000 (18:12 +0000)
lib/editor/tinymce/lang/en/editor_tinymce.php
lib/editor/tinymce/lib.php
lib/moodlelib.php
lib/outputcomponents.php

index d799d49..978c333 100644 (file)
@@ -32,7 +32,8 @@ $string['dragmath:dragmath_desc'] = 'Insert equation';
 $string['dragmath:dragmath_javaneeded'] = 'To use this page you need a Java-enabled browser. Download the latest Java plug-in from {$a}.';
 $string['dragmath:dragmath_title'] = 'DragMath Equation Editor';
 $string['media_dlg:filename'] = 'Filename';
-$string['moodlenolink:desc'] = "Prevent automatic linking";
+$string['moodlenolink:desc'] = 'Prevent automatic linking';
+$string['moodleemoticon:desc'] = 'Insert emoticon';
 $string['pluginname'] = 'TinyMCE HTML editor';
 
 
index 31d4209..4bbe2b4 100644 (file)
@@ -71,7 +71,7 @@ class tinymce_texteditor extends texteditor {
     }
 
     protected function get_init_params($elementid, array $options=null) {
-        global $CFG, $PAGE;
+        global $CFG, $PAGE, $OUTPUT;
 
         //TODO: we need to implement user preferences that affect the editor setup too
 
@@ -98,6 +98,11 @@ class tinymce_texteditor extends texteditor {
         } else {
             $xdragmath = '';
         }
+        if (array_key_exists('filter/emoticon', $filters)) {
+            $xemoticon = 'moodleemoticon,';
+        } else {
+            $xemoticon = '';
+        }
 
         $params = array(
                     'mode' => "exact",
@@ -115,7 +120,7 @@ class tinymce_texteditor extends texteditor {
                     'apply_source_formatting' => true,
                     'remove_script_host' => false,
                     'entity_encoding' => "raw",
-                    'plugins' => "{$xmedia}advimage,safari,table,style,layer,advhr,advlink,emotions,inlinepopups,searchreplace,paste,directionality,fullscreen,moodlenolink,{$xdragmath}nonbreaking,contextmenu,insertdatetime,save,iespell,preview,print,noneditable,visualchars,xhtmlxtras,template,pagebreak,spellchecker",
+                    'plugins' => "{$xmedia}advimage,safari,table,style,layer,advhr,advlink,emotions,inlinepopups,searchreplace,paste,directionality,fullscreen,moodlenolink,{$xemoticon}{$xdragmath}nonbreaking,contextmenu,insertdatetime,save,iespell,preview,print,noneditable,visualchars,xhtmlxtras,template,pagebreak,spellchecker",
                     'theme_advanced_font_sizes' => "1,2,3,4,5,6,7",
                     'theme_advanced_layout_manager' => "SimpleLayout",
                     'theme_advanced_toolbar_align' => "left",
@@ -123,7 +128,7 @@ class tinymce_texteditor extends texteditor {
                     'theme_advanced_buttons1_add' => "|,undo,redo,|,search,replace,|,fullscreen",
                     'theme_advanced_buttons2' => "bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright",
                     'theme_advanced_buttons2_add' => "|,cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl",
-                    'theme_advanced_buttons3' => "bullist,numlist,outdent,indent,|,link,unlink,moodlenolink,|,image,{$xmedia}{$xdragmath}nonbreaking,charmap",
+                    'theme_advanced_buttons3' => "bullist,numlist,outdent,indent,|,link,unlink,moodlenolink,|,image,{$xemoticon}{$xmedia}{$xdragmath}nonbreaking,charmap",
                     'theme_advanced_buttons3_add' => "table,|,code,spellchecker",
                     'theme_advanced_fonts' => "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",
                     'theme_advanced_resize_horizontal' => true,
@@ -132,6 +137,20 @@ class tinymce_texteditor extends texteditor {
                     'theme_advanced_statusbar_location' => "bottom",
                     'spellchecker_rpc_url' => $CFG->wwwroot."/lib/editor/tinymce/tiny_mce/$this->version/plugins/spellchecker/rpc.php"
                   );
+
+        if ($xemoticon) {
+            $manager = get_emoticon_manager();
+            $emoticons = $manager->get_emoticons();
+            $imgs = array();
+            // see the TinyMCE plugin moodleemoticon for how the emoticon index is (ab)used :-S
+            $index = 0;
+            foreach ($emoticons as $emoticon) {
+                $imgs[$emoticon->text] = $OUTPUT->render(
+                    $manager->prepare_renderable_emoticon($emoticon, array('class' => 'emoticon emoticon-index-'.$index++)));
+            }
+            $params['moodleemoticon_emoticons'] = json_encode($imgs);
+        }
+
         if (empty($CFG->xmlstrictheaders) and (!empty($options['legacy']) or !empty($options['noclean']) or !empty($options['trusted']))) {
             // now deal somehow with non-standard tags, people scream when we do not make moodle code xtml strict,
             // but they scream even more when we strip all tags that are not strict :-(
index fc4f95f..30252a4 100644 (file)
@@ -6781,16 +6781,17 @@ class emoticon_manager {
      * Converts emoticon object into renderable pix_emoticon object
      *
      * @param stdClass $emoticon emoticon object
+     * @param array $attributes explicit HTML attributes to set
      * @return pix_emoticon
      */
-    public function prepare_renderable_emoticon(stdClass $emoticon) {
+    public function prepare_renderable_emoticon(stdClass $emoticon, array $attributes = array()) {
         $stringmanager = get_string_manager();
         if ($stringmanager->string_exists($emoticon->altidentifier, $emoticon->altcomponent)) {
             $alt = get_string($emoticon->altidentifier, $emoticon->altcomponent);
         } else {
-            $alt = $emoticon->text;
+            $alt = s($emoticon->text);
         }
-        return new pix_emoticon($emoticon->imagename, $alt, $emoticon->imagecomponent);
+        return new pix_emoticon($emoticon->imagename, $alt, $emoticon->imagecomponent, $attributes);
     }
 
     /**
index 3938f93..1050632 100644 (file)
@@ -348,10 +348,14 @@ class pix_emoticon extends pix_icon implements renderable {
     /**
      * Constructor
      * @param string $pix short icon name
-     * @param string $component component name
+     * @param string $alt alternative text
+     * @param string $component emoticon image provider
+     * @param array $attributes explicit HTML attributes
      */
-    public function __construct($pix, $alt, $component = 'moodle') {
-        $attributes = array('class' => 'emoticon');
+    public function __construct($pix, $alt, $component = 'moodle', array $attributes = array()) {
+        if (empty($attributes['class'])) {
+            $attributes['class'] = 'emoticon';
+        }
         parent::__construct($pix, $alt, $component, $attributes);
     }
 }