Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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.
  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  require_once($CFG->dirroot . "/calendar/lib.php");
  30  require_once($CFG->libdir . "/filelib.php");
  31  
  32  use \core\external\exporter;
  33  use \core_calendar\local\event\container;
  34  use \core_calendar\local\event\entities\event_interface;
  35  use \core_calendar\local\event\entities\action_event_interface;
  36  use \core_course\external\course_summary_exporter;
  37  use \core\external\coursecat_summary_exporter;
  38  use \renderer_base;
  39  use moodle_url;
  40  
  41  /**
  42   * Class for displaying a calendar event.
  43   *
  44   * @package   core_calendar
  45   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  46   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47   */
  48  class event_exporter_base extends exporter {
  49  
  50      /**
  51       * @var event_interface $event
  52       */
  53      protected $event;
  54  
  55      /**
  56       * Constructor.
  57       *
  58       * @param event_interface $event
  59       * @param array $related The related data.
  60       */
  61      public function __construct(event_interface $event, $related = []) {
  62          $this->event = $event;
  63  
  64          $starttimestamp = $event->get_times()->get_start_time()->getTimestamp();
  65          $endtimestamp = $event->get_times()->get_end_time()->getTimestamp();
  66          $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
  67          $userid = $event->get_user() ? $event->get_user()->get('id') : null;
  68          $categoryid = $event->get_category() ? $event->get_category()->get('id') : null;
  69  
  70          $data = new \stdClass();
  71          $data->id = $event->get_id();
  72          $data->name = $event->get_name();
  73          $data->description = file_rewrite_pluginfile_urls(
  74              $event->get_description()->get_value(),
  75              'pluginfile.php',
  76              $related['context']->id,
  77              'calendar',
  78              'event_description',
  79              $event->get_id()
  80          );
  81          $data->descriptionformat = $event->get_description()->get_format();
  82          $data->location = external_format_text($event->get_location(), FORMAT_PLAIN, $related['context']->id)[0];
  83          $data->groupid = $groupid;
  84          $data->userid = $userid;
  85          $data->categoryid = $categoryid;
  86          $data->eventtype = $event->get_type();
  87          $data->timestart = $starttimestamp;
  88          $data->timeduration = $endtimestamp - $starttimestamp;
  89          $data->timesort = $event->get_times()->get_sort_time()->getTimestamp();
  90          $data->timeusermidnight = $event->get_times()->get_usermidnight_time()->getTimestamp();
  91          $data->visible = $event->is_visible() ? 1 : 0;
  92          $data->timemodified = $event->get_times()->get_modified_time()->getTimestamp();
  93          $data->component = $event->get_component();
  94          $data->overdue = $data->timesort < time();
  95  
  96          if ($repeats = $event->get_repeats()) {
  97              $data->repeatid = $repeats->get_id();
  98              $data->eventcount = $repeats->get_num() + 1;
  99          }
 100  
 101          if ($cm = $event->get_course_module()) {
 102              $data->modulename = $cm->get('modname');
 103              $data->instance = $cm->get('id');
 104              $data->activityname = $cm->get('name');
 105  
 106              $component = 'mod_' . $data->modulename;
 107              if (!component_callback_exists($component, 'core_calendar_get_event_action_string')) {
 108                  $modulename = get_string('modulename', $data->modulename);
 109                  $data->activitystr = get_string('requiresaction', 'calendar', $modulename);
 110              } else {
 111                  $data->activitystr = component_callback(
 112                      $component,
 113                      'core_calendar_get_event_action_string',
 114                      [$event->get_type()]
 115                  );
 116              }
 117          }
 118  
 119          parent::__construct($data, $related);
 120      }
 121  
 122      /**
 123       * Return the list of properties.
 124       *
 125       * @return array
 126       */
 127      protected static function define_properties() {
 128          return [
 129              'id' => ['type' => PARAM_INT],
 130              'name' => ['type' => PARAM_TEXT],
 131              'description' => [
 132                  'type' => PARAM_RAW,
 133                  'optional' => true,
 134                  'default' => null,
 135                  'null' => NULL_ALLOWED
 136              ],
 137              'descriptionformat' => [
 138                  'type' => PARAM_INT,
 139                  'optional' => true,
 140                  'default' => null,
 141                  'null' => NULL_ALLOWED
 142              ],
 143              'location' => [
 144                  'type' => PARAM_RAW,
 145                  'optional' => true,
 146                  'default' => null,
 147                  'null' => NULL_ALLOWED
 148              ],
 149              'categoryid' => [
 150                  'type' => PARAM_INT,
 151                  'optional' => true,
 152                  'default' => null,
 153                  'null' => NULL_ALLOWED
 154              ],
 155              'groupid' => [
 156                  'type' => PARAM_INT,
 157                  'optional' => true,
 158                  'default' => null,
 159                  'null' => NULL_ALLOWED
 160              ],
 161              'userid' => [
 162                  'type' => PARAM_INT,
 163                  'optional' => true,
 164                  'default' => null,
 165                  'null' => NULL_ALLOWED
 166              ],
 167              'repeatid' => [
 168                  'type' => PARAM_INT,
 169                  'optional' => true,
 170                  'default' => null,
 171                  'null' => NULL_ALLOWED
 172              ],
 173              'eventcount' => [
 174                  'type' => PARAM_INT,
 175                  'optional' => true,
 176                  'default' => null,
 177                  'null' => NULL_ALLOWED
 178              ],
 179              'component' => [
 180                  'type' => PARAM_COMPONENT,
 181                  'optional' => true,
 182                  'default' => null,
 183                  'null' => NULL_ALLOWED
 184              ],
 185              'modulename' => [
 186                  'type' => PARAM_TEXT,
 187                  'optional' => true,
 188                  'default' => null,
 189                  'null' => NULL_ALLOWED
 190              ],
 191              'activityname' => [
 192                  'type' => PARAM_TEXT,
 193                  'optional' => true,
 194                  'default' => null,
 195                  'null' => NULL_ALLOWED
 196              ],
 197              'activitystr' => [
 198                  'type' => PARAM_TEXT,
 199                  'optional' => true,
 200                  'default' => null,
 201                  'null' => NULL_ALLOWED
 202              ],
 203              'instance' => [
 204                  'type' => PARAM_INT,
 205                  'optional' => true,
 206                  'default' => null,
 207                  'null' => NULL_ALLOWED
 208              ],
 209              'eventtype' => ['type' => PARAM_TEXT],
 210              'timestart' => ['type' => PARAM_INT],
 211              'timeduration' => ['type' => PARAM_INT],
 212              'timesort' => ['type' => PARAM_INT],
 213              'timeusermidnight' => ['type' => PARAM_INT],
 214              'visible' => ['type' => PARAM_INT],
 215              'timemodified' => ['type' => PARAM_INT],
 216              'overdue' => [
 217                  'type' => PARAM_BOOL,
 218                  'optional' => true,
 219                  'default' => false,
 220                  'null' => NULL_ALLOWED
 221              ],
 222          ];
 223      }
 224  
 225      /**
 226       * Return the list of additional properties.
 227       *
 228       * @return array
 229       */
 230      protected static function define_other_properties() {
 231          return [
 232              'icon' => [
 233                  'type' => event_icon_exporter::read_properties_definition(),
 234              ],
 235              'category' => [
 236                  'type' => coursecat_summary_exporter::read_properties_definition(),
 237                  'optional' => true,
 238              ],
 239              'course' => [
 240                  'type' => course_summary_exporter::read_properties_definition(),
 241                  'optional' => true,
 242              ],
 243              'subscription' => [
 244                  'type' => event_subscription_exporter::read_properties_definition(),
 245                  'optional' => true,
 246              ],
 247              'canedit' => [
 248                  'type' => PARAM_BOOL
 249              ],
 250              'candelete' => [
 251                  'type' => PARAM_BOOL
 252              ],
 253              'deleteurl' => [
 254                  'type' => PARAM_URL
 255              ],
 256              'editurl' => [
 257                  'type' => PARAM_URL
 258              ],
 259              'viewurl' => [
 260                  'type' => PARAM_URL
 261              ],
 262              'formattedtime' => [
 263                  'type' => PARAM_RAW,
 264              ],
 265              'isactionevent' => [
 266                  'type' => PARAM_BOOL
 267              ],
 268              'iscourseevent' => [
 269                  'type' => PARAM_BOOL
 270              ],
 271              'iscategoryevent' => [
 272                  'type' => PARAM_BOOL
 273              ],
 274              'groupname' => [
 275                  'type' => PARAM_RAW,
 276                  'optional' => true,
 277                  'default' => null,
 278                  'null' => NULL_ALLOWED
 279              ],
 280              'normalisedeventtype' => [
 281                  'type' => PARAM_TEXT
 282              ],
 283              'normalisedeventtypetext' => [
 284                  'type' => PARAM_TEXT
 285              ],
 286              'action' => [
 287                  'type' => event_action_exporter::read_properties_definition(),
 288                  'optional' => true,
 289              ],
 290              'purpose' => [
 291                  'type' => PARAM_TEXT
 292              ],
 293          ];
 294      }
 295  
 296      /**
 297       * Get the additional values to inject while exporting.
 298       *
 299       * @param renderer_base $output The renderer.
 300       * @return array Keys are the property names, values are their values.
 301       */
 302      protected function get_other_values(renderer_base $output) {
 303          $values = [];
 304          $event = $this->event;
 305          $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
 306          $context = $this->related['context'];
 307          $course = $this->related['course'];
 308          $values['isactionevent'] = false;
 309          $values['iscourseevent'] = false;
 310          $values['iscategoryevent'] = false;
 311          $values['normalisedeventtype'] = $event->get_type();
 312          if ($moduleproxy = $event->get_course_module()) {
 313              // We need a separate property to flag if an event is action event.
 314              // That's required because canedit return true but action action events cannot be edited on the calendar UI.
 315              // But they are considered editable because you can drag and drop the event on the month view.
 316              $values['isactionevent'] = true;
 317              // Activity events are normalised to "look" like course events.
 318              $values['normalisedeventtype'] = 'course';
 319          } else if ($event->get_type() == 'course') {
 320              $values['iscourseevent'] = true;
 321          } else if ($event->get_type() == 'category') {
 322              $values['iscategoryevent'] = true;
 323          }
 324          $timesort = $event->get_times()->get_sort_time()->getTimestamp();
 325          $iconexporter = new event_icon_exporter($event, ['context' => $context]);
 326          $identifier = 'type' . $values['normalisedeventtype'];
 327          $stringexists = get_string_manager()->string_exists($identifier, 'calendar');
 328          if (!$stringexists) {
 329              // Property normalisedeventtype is used to build the name of the CSS class for the events.
 330              $values['normalisedeventtype'] = 'other';
 331          }
 332          $values['normalisedeventtypetext'] = $stringexists ? get_string($identifier, 'calendar') : '';
 333  
 334          $purpose = 'none';
 335          if ($moduleproxy) {
 336              $purpose = plugin_supports('mod', $moduleproxy->get('modname'), FEATURE_MOD_PURPOSE, 'none');
 337          }
 338          $values['purpose'] = $purpose;
 339  
 340          $values['icon'] = $iconexporter->export($output);
 341  
 342          $subscriptionexporter = new event_subscription_exporter($event);
 343          $values['subscription'] = $subscriptionexporter->export($output);
 344  
 345          $proxy = $this->event->get_category();
 346          if ($proxy && $proxy->get('id')) {
 347              $category = $proxy->get_proxied_instance();
 348              $categorysummaryexporter = new coursecat_summary_exporter($category, ['context' => $context]);
 349              $values['category'] = $categorysummaryexporter->export($output);
 350          }
 351  
 352          if ($course && $course->id != SITEID) {
 353              $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
 354              $values['course'] = $coursesummaryexporter->export($output);
 355          }
 356  
 357          $courseid = (!$course) ? SITEID : $course->id;
 358  
 359          $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
 360          $values['candelete'] = calendar_delete_event_allowed($legacyevent);
 361  
 362          $deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
 363          $values['deleteurl'] = $deleteurl->out(false);
 364  
 365          $editurl = new moodle_url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
 366                  'course' => $courseid]);
 367          $values['editurl'] = $editurl->out(false);
 368          $viewurl = new moodle_url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
 369                  'time' => $timesort]);
 370          $viewurl->set_anchor('event_' . $event->get_id());
 371          $values['viewurl'] = $viewurl->out(false);
 372          $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
 373                  $timesort);
 374  
 375          if ($group = $event->get_group()) {
 376              $values['groupname'] = format_string($group->get('name'), true,
 377                  ['context' => \context_course::instance($event->get_course()->get('id'))]);
 378          }
 379  
 380          if ($event instanceof action_event_interface) {
 381              // Export event action if applicable.
 382              $actionrelated = [
 383                  'context' => $this->related['context'],
 384                  'event' => $event
 385              ];
 386              $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
 387              $values['action'] = $actionexporter->export($output);
 388          }
 389  
 390          return $values;
 391      }
 392  
 393      /**
 394       * Returns a list of objects that are related.
 395       *
 396       * @return array
 397       */
 398      protected static function define_related() {
 399          return [
 400              'context' => 'context',
 401              'course' => 'stdClass?',
 402          ];
 403      }
 404  }