Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Contains event class for displaying a calendar event's icon.
  19   *
  20   * @package   core_calendar
  21   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_calendar\external;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use \core\external\exporter;
  30  use \core_calendar\local\event\entities\event_interface;
  31  
  32  /**
  33   * Class for displaying a calendar event's icon.
  34   *
  35   * @package   core_calendar
  36   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class event_icon_exporter extends exporter {
  40  
  41      /**
  42       * Constructor.
  43       *
  44       * @param event_interface $event
  45       * @param array $related The related data.
  46       */
  47      public function __construct(event_interface $event, $related = []) {
  48          global $PAGE;
  49          $coursemodule = $event->get_course_module();
  50          $category = $event->get_category();
  51          $categoryid = $category ? $category->get('id') : null;
  52          $course = $event->get_course();
  53          $courseid = $course ? $course->get('id') : null;
  54          $group = $event->get_group();
  55          $groupid = $group ? $group->get('id') : null;
  56          $user = $event->get_user();
  57          $userid = $user ? $user->get('id') : null;
  58          $isactivityevent = !empty($coursemodule);
  59          $issiteevent = ($course && $courseid == SITEID);
  60          $iscategoryevent = ($category && !empty($categoryid));
  61          $iscourseevent = ($course && !empty($courseid) && $courseid != SITEID && empty($groupid));
  62          $isgroupevent = ($group && !empty($groupid));
  63          $isuserevent = ($user && !empty($userid));
  64  
  65          if ($isactivityevent) {
  66              $key = 'icon';
  67              $component = $coursemodule->get('modname');
  68  
  69              if (get_string_manager()->string_exists($event->get_type(), $component)) {
  70                  $alttext = get_string($event->get_type(), $component);
  71              } else {
  72                  $alttext = get_string('activityevent', 'calendar');
  73              }
  74          } else if ($event->get_component()) {
  75              // Guess the icon and the title for the component event. By default display calendar icon and the
  76              // plugin name as the alttext.
  77              if ($PAGE->theme->resolve_image_location($event->get_type(), $event->get_component())) {
  78                  $key = $event->get_type();
  79                  $component = $event->get_component();
  80              } else {
  81                  $key = 'i/otherevent';
  82                  $component = 'core';
  83              }
  84  
  85              if (get_string_manager()->string_exists($event->get_type(), $event->get_component())) {
  86                  $alttext = get_string($event->get_type(), $event->get_component());
  87              } else {
  88                  $alttext = get_string('pluginname', $event->get_component());
  89              }
  90          } else if ($issiteevent) {
  91              $key = 'i/siteevent';
  92              $component = 'core';
  93              $alttext = get_string('typesite', 'calendar');
  94          } else if ($iscategoryevent) {
  95              $key = 'i/categoryevent';
  96              $component = 'core';
  97              $alttext = get_string('typecategory', 'calendar');
  98          } else if ($iscourseevent) {
  99              $key = 'i/courseevent';
 100              $component = 'core';
 101              $alttext = get_string('typecourse', 'calendar');
 102          } else if ($isgroupevent) {
 103              $key = 'i/groupevent';
 104              $component = 'core';
 105              $alttext = get_string('typegroup', 'calendar');
 106          } else if ($isuserevent) {
 107              $key = 'i/userevent';
 108              $component = 'core';
 109              $alttext = get_string('typeuser', 'calendar');
 110          } else {
 111              // Default to site event icon?
 112              $key = 'i/siteevent';
 113              $component = 'core';
 114              $alttext = get_string('typesite', 'calendar');
 115          }
 116  
 117          $data = new \stdClass();
 118          $data->key = $key;
 119          $data->component = $component;
 120          $data->alttext = $alttext;
 121  
 122          parent::__construct($data, $related);
 123      }
 124  
 125      /**
 126       * Return the list of properties.
 127       *
 128       * @return array
 129       */
 130      protected static function define_properties() {
 131          return [
 132              'key' => ['type' => PARAM_TEXT],
 133              'component' => ['type' => PARAM_TEXT],
 134              'alttext' => ['type' => PARAM_TEXT],
 135          ];
 136      }
 137  
 138      /**
 139       * Returns a list of objects that are related.
 140       *
 141       * @return array
 142       */
 143      protected static function define_related() {
 144          return [
 145              'context' => 'context',
 146          ];
 147      }
 148  }