From 93c61c18cfce818bd407ff3be6f9bdd443eba731 Mon Sep 17 00:00:00 2001
From: toyomoyo <toyomoyo>
Date: Mon, 3 Dec 2007 06:27:21 +0000
Subject: [PATCH] MDL-10915, additional setting for emoticons

---
 admin/settings/appearance.php |   1 +
 lang/en_utf8/admin.php        |   2 +
 lib/adminlib.php              | 142 ++++++++++++++++++++++++++++++++++
 lib/weblib.php                |  48 ++++--------
 4 files changed, 159 insertions(+), 34 deletions(-)

diff --git a/admin/settings/appearance.php b/admin/settings/appearance.php
index 5bae9b3ec8..a81b5219f7 100644
--- a/admin/settings/appearance.php
+++ b/admin/settings/appearance.php
@@ -97,6 +97,7 @@ if (!empty($CFG->aspellpath)) { // make aspell settings disappear if path isn't
   $temp->add(new admin_setting_special_editordictionary());
 }
 $temp->add(new admin_setting_special_editorhidebuttons());
+$temp->add(new admin_setting_emoticons());
 $ADMIN->add('appearance', $temp);
 
 // "documentation" settingpage
diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php
index bf64ffa44b..02154cd9ba 100644
--- a/lang/en_utf8/admin.php
+++ b/lang/en_utf8/admin.php
@@ -87,6 +87,7 @@ $string['configdocroot'] = 'Defines the path to the Moodle Docs. You can change
 $string['configdoctonewwindow'] = 'If you enable this, then links to Moodle Docs will be shown in a new window.';
 $string['configeditorfontlist'] = 'Select the fonts that should appear in the editor\'s drop-down list.';
 $string['configeditordictionary'] = 'This value will be used if aspell doesn\'t have dictionary for users own language.';
+$string['configemoticons'] = 'Change the code on the left that relates to the name of the emoticon on the right.  To add new emoticons, add a code and a name, then add an image as name.gif in /pix/s.';
 $string['configenableajax'] = 'This setting allows you to control the use of AJAX (advanced client/server interfaces using Javascript) across the whole site.  With this setting enabled users can still make a choice in their profile, otherwise AJAX is disabled for everybody.';
 $string['configenablecourserequests'] = 'This will allow any user to request a course be created.';
 $string['configenableglobalsearch'] = 'This setting enables global text searching in resources and activities, it is not compatible with PHP 4.';
@@ -283,6 +284,7 @@ $string['editorkillword'] = 'Word format filter';
 $string['editorspellinghelp'] = 'Enable or disable spell-checking. When enabled, <strong>aspell</strong> must be installed on the server.';
 $string['editorspelling'] = 'Editor spelling';
 $string['editstrings'] = 'Edit words or phrases';
+$string['emoticons'] = 'Emoticons';
 $string['enableajax'] = 'Enable AJAX';
 $string['enablecourserequests'] = 'Enable course requests';
 $string['enableglobalsearch'] = 'Enable global search';
diff --git a/lib/adminlib.php b/lib/adminlib.php
index 5f45718dff..ffa9b146da 100644
--- a/lib/adminlib.php
+++ b/lib/adminlib.php
@@ -2043,6 +2043,148 @@ class admin_setting_special_editorfontlist extends admin_setting {
 
 }
 
+class admin_setting_emoticons extends admin_setting {
+
+    var $items;
+
+    function admin_setting_emoticons() {
+        global $CFG;
+        $name = 'emoticons';
+        $visiblename = get_string('emoticons', 'admin');
+        $description = get_string('configemoticons', 'admin');
+        $defaults = array('k0' => ':-)',
+                          'v0' => 'smiley',
+                          'k1' => ':)',
+                          'v1' => 'smiley',
+                          'k2' => ':-D',
+                          'v2' => 'biggrin',
+                          'k3' => ';-)',
+                          'v3' => 'wink',
+                          'k4' => ':-/',
+                          'v4' => 'mixed',
+                          'k5' => 'V-.',
+                          'v5' => 'thoughtful',
+                          'k6' => ':-P',
+                          'v6' => 'tongueout',
+                          'k7' => 'B-)',
+                          'v7' => 'cool',
+                          'k8' => '^-)',
+                          'v8' => 'approve',
+                          'k9' => '8-)',
+                          'v9' => 'wideeyes',
+                          'k10' => ':o)',
+                          'v10' => 'clown',
+                          'k11' => ':-(',
+                          'v11' => 'sad',
+                          'k12' => ':(',
+                          'v12' => 'sad',
+                          'k13' => '8-.',
+                          'v13' => 'shy',
+                          'k14' => ':-I',
+                          'v14' => 'blush',
+                          'k15' => ':-X',
+                          'v15' => 'kiss',
+                          'k16' => '8-o',
+                          'v16' => 'surprise',
+                          'k17' => 'P-|',
+                          'v17' => 'blackeye',
+                          'k18' => '8-[',
+                          'v18' => 'angry',
+                          'k19' => 'xx-P',
+                          'v19' => 'dead',
+                          'k20' => '|-.',
+                          'v20' => 'sleepy',
+                          'k21' => '}-]',
+                          'v21' => 'evil',
+                          'k22' => '(h)',
+                          'v22' => 'heart',
+                          'k23' => '(heart)',
+                          'v23' => 'heart',
+                          'k24' => '(y)',
+                          'v24' => 'yes',
+                          'k25' => '(n)',
+                          'v25' => 'no',
+                          'k26' => '(martin)',
+                          'v26' => 'martin',
+                          'k27' => '( )',
+                          'v27' => 'egg');
+        parent::admin_setting($name, $visiblename, $description, $defaults);
+    }
+
+    function get_setting() {
+        global $CFG;
+        if (isset($CFG->emoticons)) {
+            $i = 0;
+            $currentsetting = array();
+            $items = explode('{;}', $CFG->emoticons);
+            foreach ($items as $item) {
+              $item = explode('{:}', $item);
+              $currentsetting['k' . $i] = $item[0];
+              $currentsetting['v' . $i] = $item[1];
+              $i++;
+            }
+            return $currentsetting;
+        } else {
+            return NULL;
+        }
+    }
+
+    function write_setting($data) {
+
+        // there miiight be an easier way to do this :)
+        // if this is changed, make sure the $defaults array above is modified so that this
+        // function processes it correctly
+
+        $keys = array();
+        $values = array();
+
+        foreach ($data as $key => $value) {
+            if (substr($key,0,1) == 'k') {
+                $keys[substr($key,1)] = $value;
+            } elseif (substr($key,0,1) == 'v') {
+                $values[substr($key,1)] = $value;
+            }
+        }
+
+        $result = '';
+        for ($i = 0; $i < count($keys); $i++) {
+            if (($keys[$i] !== '') && ($values[$i] !== '')) {
+                $result .= clean_param($keys[$i],PARAM_NOTAGS) . '{:}' . clean_param($values[$i], PARAM_NOTAGS) . '{;}';
+            }
+        }
+
+        $result = substr($result, 0, -3); // trim the last separator
+
+        return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
+    }
+
+    function output_html() {
+
+        if ($this->get_setting() === NULL) {
+            $currentsetting = $this->defaultsetting;
+        } else {
+            $currentsetting = $this->get_setting();
+        }
+
+        $return = '<div class="form-group">';
+        for ($i = 0; $i < count($currentsetting) / 2; $i++) {
+            $return .= '<input type="text" class="form-text" name="s_emoticons[k' . $i . ']" value="' . $currentsetting['k' . $i] . '" />';
+            $return .= '&nbsp;&nbsp;';
+            $return .= '<input type="text" class="form-text" name="s_emoticons[v' . $i . ']" value="' . $currentsetting['v' . $i] . '" /><br />';
+        }
+        $return .= '<input type="text" class="form-text" name="s_emoticons[k' . $i . ']" value="" />';
+        $return .= '&nbsp;&nbsp;';
+        $return .= '<input type="text" class="form-text" name="s_emoticons[v' . $i . ']" value="" /><br />';
+        $return .= '<input type="text" class="form-text" name="s_emoticons[k' . ($i + 1) . ']" value="" />';
+        $return .= '&nbsp;&nbsp;';
+        $return .= '<input type="text" class="form-text" name="s_emoticons[v' . ($i + 1) . ']" value="" />';
+        $return .= '</div>';
+
+        return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
+    }
+
+}
+
 class admin_setting_special_editordictionary extends admin_setting_configselect {
 
     function admin_setting_special_editordictionary() {
diff --git a/lib/weblib.php b/lib/weblib.php
index 2a90f015c8..6d86c4ba9f 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -2015,49 +2015,29 @@ function cleanAttributes2($htmlArray){
 function replace_smilies(&$text) {
 ///
     global $CFG;
-
     $lang = current_language();
-
-/// this builds the mapping array only once
+    $emoticonstring = $CFG->emoticons;
     static $e = array();
     static $img = array();
-    static $emoticons = array(
-        ':-)'  => 'smiley',
-        ':)'   => 'smiley',
-        ':-D'  => 'biggrin',
-        ';-)'  => 'wink',
-        ':-/'  => 'mixed',
-        'V-.'  => 'thoughtful',
-        ':-P'  => 'tongueout',
-        'B-)'  => 'cool',
-        '^-)'  => 'approve',
-        '8-)'  => 'wideeyes',
-        ':o)'  => 'clown',
-        ':-('  => 'sad',
-        ':('   => 'sad',
-        '8-.'  => 'shy',
-        ':-I'  => 'blush',
-        ':-X'  => 'kiss',
-        '8-o'  => 'surprise',
-        'P-|'  => 'blackeye',
-        '8-['  => 'angry',
-        'xx-P' => 'dead',
-        '|-.'  => 'sleepy',
-        '}-]'  => 'evil',
-        '(h)'  => 'heart',
-        '(heart)'  => 'heart',
-        '(y)'  => 'yes',
-        '(n)'  => 'no',
-        '(martin)'  => 'martin',
-        '( )'  => 'egg'
-        );
+    static $emoticons = null;
+
+    if (is_null($emoticons)) {
+        $emoticons = array();
+        if ($emoticonstring) {
+            $items = explode('{;}', $CFG->emoticons);
+            foreach ($items as $item) {
+               $item = explode('{:}', $item);
+              $emoticons[$item[0]] = $item[1]; 
+            }
+        }
+    }
+
 
     if (empty($img[$lang])) {  /// After the first time this is not run again
         $e[$lang] = array();
         $img[$lang] = array();
         foreach ($emoticons as $emoticon => $image){
             $alttext = get_string($image, 'pix');
-
             $e[$lang][] = $emoticon;
             $img[$lang][] = '<img alt="'. $alttext .'" width="15" height="15" src="'. $CFG->pixpath .'/s/'. $image .'.gif" />';
         }
-- 
2.39.5