Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
  19   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20   */
  21  
  22  define('CLI_SCRIPT', true);
  23  
  24  require(__DIR__.'/../../config.php');
  25  
  26  $categorysortorder = [
  27      'Smileys & People',
  28      'Animals & Nature',
  29      'Food & Drink',
  30      'Travel & Places',
  31      'Activities',
  32      'Objects',
  33      'Symbols',
  34      'Flags'
  35  ];
  36  
  37  // Source: https://github.com/iamcal/emoji-data
  38  $rawdata = file_get_contents('./emoji_pretty.json');
  39  $jsondata = json_decode($rawdata, true);
  40  $emojibycategory = [];
  41  $obsoletes = [];
  42  
  43  foreach ($jsondata as $data) {
  44      $category = $data['category'];
  45      $unified = $data['unified'];
  46  
  47      if ($category === 'Skin Tones') {
  48          continue;
  49      }
  50  
  51      if (!empty($data['obsoleted_by'])) {
  52          // Skip any obsolete emojis. We'll merge these short names into the
  53          // newer emoji later on.
  54          $obsoletes[] = [
  55              'shortname' => $data['short_name'],
  56              'by' => $data['obsoleted_by']
  57          ];
  58          continue;
  59      }
  60  
  61      if (!isset($emojibycategory[$category])) {
  62          $emojibycategory[$category] = [
  63              'name' => $category,
  64              'emojis' => []
  65          ];
  66      }
  67  
  68      $emojibycategory[$category]['emojis'][] = [
  69          'sortorder' => (int) $data['sort_order'],
  70          'unified' => $unified,
  71          'shortnames' => [$data['short_name']]
  72      ];
  73  }
  74  
  75  $emojibycategory = array_values($emojibycategory);
  76  // Sort the emojis within each category into the order specified in the raw data.
  77  $emojibycategory = array_map(function($category) {
  78      usort($category['emojis'], function($a, $b) {
  79          return $a['sortorder'] <=> $b['sortorder'];
  80      });
  81      return $category;
  82  }, $emojibycategory);
  83  
  84  // Add the short names for the obsoleted emojis into the list of short names
  85  // of the newer emoji.
  86  foreach ($obsoletes as $obsolete) {
  87      $emojibycategory = array_map(function($category) use ($obsolete) {
  88          $category['emojis'] = array_map(function($emoji) use ($obsolete) {
  89              if ($obsolete['by'] == $emoji['unified']) {
  90                  $emoji['shortnames'] = array_merge($emoji['shortnames'], [$obsolete['shortname']]);
  91              }
  92              unset($emoji['sortorder']);
  93              return $emoji;
  94          }, $category['emojis']);
  95          return $category;
  96      }, $emojibycategory);
  97  }
  98  // Sort the emoji categories into the correct order.
  99  usort($emojibycategory, function($a, $b) use ($categorysortorder) {
 100      $aindex = array_search($a['name'], $categorysortorder);
 101      $bindex = array_search($b['name'], $categorysortorder);
 102      return $aindex <=> $bindex;
 103  });
 104  
 105  $emojibyshortname = array_reduce($jsondata, function($carry, $data) {
 106      $unified = null;
 107      $shortname = $data['short_name'];
 108      if (!empty($data['obsoleted_by'])) {
 109          $unified = $data['obsoleted_by'];
 110      } else {
 111          $unified = $data['unified'];
 112      }
 113      $carry[$shortname] = $unified;
 114      return $carry;
 115  }, []);
 116  
 117  $loader = new \Mustache_Loader_ArrayLoader([
 118      'data.js' => file_get_contents('./data.js.mustache')
 119  ]);
 120  $mustache = new \core\output\mustache_engine(['loader' => $loader]);
 121  
 122  echo $mustache->render('data.js', [
 123      'byCategory' => json_encode($emojibycategory, JSON_PRETTY_PRINT),
 124      'byShortName' => json_encode($emojibyshortname, JSON_PRETTY_PRINT)
 125  ]);