Need help with an array.

Need help with an array.

- Matt Campbell の投稿
返信数: 15
MDL-10915 and MDL-11045 have been an issue for me for a while, and so far I've done as everyone else has done and edited the emoticons list in /lib/weblib.php. Tim Hunt mentioned that this was probably a fairly easy fix that a novice moodler could do, so I thought I'd see if I was up to the challenge and post the patches back into the tracker (I had never done any PHP before starting to work with Moodle, but I've used it long enough that I can generally hack some small solutions).

I've made some changes to /admin/adminlib.php and /admin/setting/appearance.php so that I now get a list of all the smilies and can add, delete, or edit any of them to give me what I need. They are written to a single database record in mdl_config and look something like this:

ウインク:wink;思慮深い:thoughtful;クール:cool;賛成:approve;もうろうとした目:wideeyes;恥ずかしい:shy;驚き:surprise;青あざ:blackeye;怒り:angry;ヘトヘト:dead;眠たい:sleepy;意地悪:evil;heart:heart;heart:heart;Yes:yes;No:no;martin:martin;egg:egg

-Edit:
Well, you WOULD be able to see it but the codes are being converted to smilies! They are the same codes you'd see if you hit the smiley button in the html editor.

Unfortunately, this is where my skills run into a dead end - I need to be able to pull this list back into the $emoticons variable in /lib/weblib.php so that Moodle uses the list in the database rather than the hardcoded list in /lib/weblib.php.

Could anyone share how to do this? Nothing that I've tried seems to load anything.

Thanks,
Matt
Matt Campbell への返信

Re: Need help with an array.

- Guido Vega の投稿
Hi Matt,

Try to replace the $emoticons declaration (i.e. the lines that go like static $emoticons = array(...);) for the following:

/************************************* Start Code Here *************************************/

// get your config string (just replace "emoticons" for what ever is in your database)
$emoticonstring = get_config(null, 'emoticons');
$emoticons = array();

// check that we got the string
if ($emoticonstring) {
// split the string in ; to get the face:name pair
$emoticonsarray = explode(';', $emoticonstring);

// now check that we split the string correctly and got an array
if (count($emoticonsarray)) {

// traverse the array and split the string into face and name values and load them to the $emoticons array
foreach ($emoticonsarray as $emoticon) {
$emotionpair = explode(';', $emoticon);
$emoticons[$emotionpair[0]] = $emotionpair[1];
}
}
}

/************************************* Code Ends Here *************************************/

I hope this helps

GV
Guido Vega への返信

Re: Need help with an array.

- Tim Hunt の投稿
画像 Core developers 画像 Documentation writers 画像 Particularly helpful Moodlers 画像 Peer reviewers 画像 Plugin developers
It's more normal to do
global $CFG;
$emoticonstring = $CFG-> emoticons

than to use get_config.

Martins suggestion of doing this as a filter may be a better approach. Look at some of the code in the filter directory to see what this would involve. However, emoticons are already a special case in places like format_text, so perhaps it is not such a problem.

Note that that bit of weblib is performance critical. It is called many times on each page as text is output, so don't repeat all the hard work of breaking up $CFG-> emoticons each time. Use a static cache instead. That is:
static $emoticons = null;
if (is_null($emoticons)) {
// Do your code above.
}


The
if (count($emoticonsarray))

above will always return true, so get rid of it. The thing you do need to check is whether $emotionpair[1] is set. That is the right way to catch syntax errors in $CFG-> emoticons.
Tim Hunt への返信

Re: Need help with an array.

- Matt Campbell の投稿
Thanks, Tim and Guido. I've got it working now - I think I used a combination of what the two of you gave me, plus some of what I had!

There are a total of three files changed, plus corresponding lang files. I've got one last issue to figure out (read below!), and then I'll see if I can get anyone to test or look at my changes.

However, I have one problem remaining - some of the emoticons use : or ; - which is what the array uses to build and then split them back up. As a result, this characters are stripped and those codes and emoticons are removed entirely. Is there a way to build the array with different seperators, or some way to protect the symbols in the emoticons so they can still use : or ;?

Thanks,
Matt
Matt Campbell への返信

Re: Need help with an array.

- Guido Vega の投稿
Hi Matt,

You can use whatever unlikely combination of characters to separate the name-value pairs and the name and values. Try replacing ; with {;} and : with {:} then in the first call to explode you can call it like explode('{;}', $emoticonstring) and the second one like explode('{:}', $emoticon);

I hope that helped.

GV

Matt Campbell への返信

Re: Need help with an array.

- Joseph Rézeau の投稿
画像 Core developers 画像 Particularly helpful Moodlers 画像 Plugin developers 画像 Testers 画像 Translators
Hi Matt,
I do agree that the automatic replacement of emoticon characters by picture emoticons is a nuisance in Moodle. You propose a setting for Admin to control which emoticons would automatically generated and which ones would not. Buy why should Admin have this power rather than teachers?
I have a simpler, radical solution: definitively scrap that automatic conversion of smileys.angry
Joseph
Joseph Rézeau への返信

Re: Need help with an array.

- Matt Campbell の投稿
I would think the overhead to have this as a course setting would be prohibitive - plus this is just little old me trying to figure this out, and I'm not sure I'm up to setting this up at course level! Another factor is that you can ADD emoticons, but the images need to go into /pix - which is a site administrator duty, not a teacher's.

It DOES work to remove them all from the HTML editor page, and then you don't get any of the automatic replacement (you can still click on the smiley icon and get the pop-up, however.) I do get an offset error in my functions in /lib/adminlin.php, however, so I may have to figure that out.

It seems to me that if you want to give teachers control of this, the better answer would be to have an option in the course settings to allow or not allow emoticons - that way teachers could choose to have them or not, but still have it at the site level as to what they mean.

Also, the automatic replacement seems to be part of function text_to_html in /lib/weblib.php - strip out the code that turns smileys into images, and that should take it out entirely (I think - haven't tried it.)

Thanks,
Matt
Matt Campbell への返信

Re: Need help with an array.

- Tim Hunt の投稿
画像 Core developers 画像 Documentation writers 画像 Particularly helpful Moodlers 画像 Peer reviewers 画像 Plugin developers
Once you have everything working, and the code nicely cleaned up and following all the Moodle coding guidelines, you need to create a patch of the changes and attach it to an issue in the tracker. (If there is not a suitable issue already, create one.)

Then you have to wait for one of the core developers to have enough time to review it.
Tim Hunt への返信

Re: Need help with an array.

- Matt Campbell の投稿
Thanks, Tim. I've definitely tried to use Moodle's coding guidelines, and other than a new class and function, most of what I've done has borrowed heavily off of existing code. I'll double-check this today and add some more comments, and then make some patches for one of the issues and post back here when I'm done.

Thanks,
Matt
Matt Campbell への返信

kill smiley! patch

- Marc Grober の投稿
I found your patch but am a bit unsure how to proceed. Can you offer some guidance as to how to patch my 1.8.2, and where to find the explanation of what you are patching....

Thanks
Marc Grober への返信

Re: kill smiley! patch

- Matt Campbell の投稿
I really wrote this against 1.9, and it is now part of core, so you'll get this when you upgrade. I'm not sure that it would patch against 1.8.2 very well...

If you just want to remove or edit your smileys in Moodle 1.8.2, go to /lib/weblib.php and search for function replace_smilies(&$text) - then just go into the list you'll see in the code and edit it there.

Thanks,
Matt
Matt Campbell への返信

Re: kill smiley! patch

- Marc Grober の投稿
Thanks....
Will that effect just what is entered into the moodle or program text as well?
Actually for time being I would just like to eliminate smiley substitution for text altogether.... Is there a quick way to do that?
Matt Campbell への返信

Re: Need help with an array.

- Matt Gibson の投稿
I like the way the :martin icon looks like a lego head of a mexican bandit 笑顔

Would your changes mean that we could define our own custom icon sets at site or course level? It would be great if teachers could choose which ones to enable/allow or even add their own.


Matt Gibson への返信

Re: Need help with an array.

- Matt Campbell の投稿
Most definitely at the site level. For example, I grabbed an icon that looked like a game controller, saved it as /pix/s/game.gif, then went to Appearance->HTML Editor and added a new emoticon, code is [o] and name is game. Then, when I type in [o], I get the controller icon on my install.

I would have no idea how to implement this down to the course level - I think it would take someone more talented than I am!

Thanks,
Matt