Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  
  95          if ($repeats = $event->get_repeats()) {
  96              $data->repeatid = $repeats->get_id();
  97              $data->eventcount = $repeats->get_num() + 1;
  98          }
  99  
 100          if ($cm = $event->get_course_module()) {
 101              $data->modulename = $cm->get('modname');
 102              $data->instance = $cm->get('id');
 103          }
 104  
 105          parent::__construct($data, $related);
 106      }
 107  
 108      /**
 109       * Return the list of properties.
 110       *
 111       * @return array
 112       */
 113      protected static function define_properties() {
 114          return [
 115              'id' => ['type' => PARAM_INT],
 116              'name' => ['type' => PARAM_TEXT],
 117              'description' => [
 118                  'type' => PARAM_RAW,
 119                  'optional' => true,
 120                  'default' => null,
 121                  'null' => NULL_ALLOWED
 122              ],
 123              'descriptionformat' => [
 124                  'type' => PARAM_INT,
 125                  'optional' => true,
 126                  'default' => null,
 127                  'null' => NULL_ALLOWED
 128              ],
 129              'location' => [
 130                  'type' => PARAM_RAW,
 131                  'optional' => true,
 132                  'default' => null,
 133                  'null' => NULL_ALLOWED
 134              ],
 135              'categoryid' => [
 136                  'type' => PARAM_INT,
 137                  'optional' => true,
 138                  'default' => null,
 139                  'null' => NULL_ALLOWED
 140              ],
 141              'groupid' => [
 142                  'type' => PARAM_INT,
 143                  'optional' => true,
 144                  'default' => null,
 145                  'null' => NULL_ALLOWED
 146              ],
 147              'userid' => [
 148                  'type' => PARAM_INT,
 149                  'optional' => true,
 150                  'default' => null,
 151                  'null' => NULL_ALLOWED
 152              ],
 153              'repeatid' => [
 154                  'type' => PARAM_INT,
 155                  'optional' => true,
 156                  'default' => null,
 157                  'null' => NULL_ALLOWED
 158              ],
 159              'eventcount' => [
 160                  'type' => PARAM_INT,
 161                  'optional' => true,
 162                  'default' => null,
 163                  'null' => NULL_ALLOWED
 164              ],
 165              'component' => [
 166                  'type' => PARAM_COMPONENT,
 167                  'optional' => true,
 168                  'default' => null,
 169                  'null' => NULL_ALLOWED
 170              ],
 171              'modulename' => [
 172                  'type' => PARAM_TEXT,
 173                  'optional' => true,
 174                  'default' => null,
 175                  'null' => NULL_ALLOWED
 176              ],
 177              'instance' => [
 178                  'type' => PARAM_INT,
 179                  'optional' => true,
 180                  'default' => null,
 181                  'null' => NULL_ALLOWED
 182              ],
 183              'eventtype' => ['type' => PARAM_TEXT],
 184              'timestart' => ['type' => PARAM_INT],
 185              'timeduration' => ['type' => PARAM_INT],
 186              'timesort' => ['type' => PARAM_INT],
 187              'timeusermidnight' => ['type' => PARAM_INT],
 188              'visible' => ['type' => PARAM_INT],
 189              'timemodified' => ['type' => PARAM_INT],
 190          ];
 191      }
 192  
 193      /**
 194       * Return the list of additional properties.
 195       *
 196       * @return array
 197       */
 198      protected static function define_other_properties() {
 199          return [
 200              'icon' => [
 201                  'type' => event_icon_exporter::read_properties_definition(),
 202              ],
 203              'category' => [
 204                  'type' => coursecat_summary_exporter::read_properties_definition(),
 205                  'optional' => true,
 206              ],
 207              'course' => [
 208                  'type' => course_summary_exporter::read_properties_definition(),
 209                  'optional' => true,
 210              ],
 211              'subscription' => [
 212                  'type' => event_subscription_exporter::read_properties_definition(),
 213                  'optional' => true,
 214              ],
 215              'canedit' => [
 216                  'type' => PARAM_BOOL
 217              ],
 218              'candelete' => [
 219                  'type' => PARAM_BOOL
 220              ],
 221              'deleteurl' => [
 222                  'type' => PARAM_URL
 223              ],
 224              'editurl' => [
 225                  'type' => PARAM_URL
 226              ],
 227              'viewurl' => [
 228                  'type' => PARAM_URL
 229              ],
 230              'formattedtime' => [
 231                  'type' => PARAM_RAW,
 232              ],
 233              'isactionevent' => [
 234                  'type' => PARAM_BOOL
 235              ],
 236              'iscourseevent' => [
 237                  'type' => PARAM_BOOL
 238              ],
 239              'iscategoryevent' => [
 240                  'type' => PARAM_BOOL
 241              ],
 242              'groupname' => [
 243                  'type' => PARAM_RAW,
 244                  'optional' => true,
 245                  'default' => null,
 246                  'null' => NULL_ALLOWED
 247              ],
 248              'normalisedeventtype' => [
 249                  'type' => PARAM_TEXT
 250              ],
 251              'normalisedeventtypetext' => [
 252                  'type' => PARAM_TEXT
 253              ],
 254              'action' => [
 255                  'type' => event_action_exporter::read_properties_definition(),
 256                  'optional' => true,
 257              ],
 258          ];
 259      }
 260  
 261      /**
 262       * Get the additional values to inject while exporting.
 263       *
 264       * @param renderer_base $output The renderer.
 265       * @return array Keys are the property names, values are their values.
 266       */
 267      protected function get_other_values(renderer_base $output) {
 268          $values = [];
 269          $event = $this->event;
 270          $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
 271          $context = $this->related['context'];
 272          $course = $this->related['course'];
 273          $values['isactionevent'] = false;
 274          $values['iscourseevent'] = false;
 275          $values['iscategoryevent'] = false;
 276          $values['normalisedeventtype'] = $event->get_type();
 277          if ($moduleproxy = $event->get_course_module()) {
 278              // We need a separate property to flag if an event is action event.
 279              // That's required because canedit return true but action action events cannot be edited on the calendar UI.
 280              // But they are considered editable because you can drag and drop the event on the month view.
 281              $values['isactionevent'] = true;
 282              // Activity events are normalised to "look" like course events.
 283              $values['normalisedeventtype'] = 'course';
 284          } else if ($event->get_type() == 'course') {
 285              $values['iscourseevent'] = true;
 286          } else if ($event->get_type() == 'category') {
 287              $values['iscategoryevent'] = true;
 288          }
 289          $timesort = $event->get_times()->get_sort_time()->getTimestamp();
 290          $iconexporter = new event_icon_exporter($event, ['context' => $context]);
 291          $identifier = 'type' . $values['normalisedeventtype'];
 292          $stringexists = get_string_manager()->string_exists($identifier, 'calendar');
 293          if (!$stringexists) {
 294              // Property normalisedeventtype is used to build the name of the CSS class for the events.
 295              $values['normalisedeventtype'] = 'other';
 296          }
 297          $values['normalisedeventtypetext'] = $stringexists ? get_string($identifier, 'calendar') : '';
 298  
 299          $values['icon'] = $iconexporter->export($output);
 300  
 301          $subscriptionexporter = new event_subscription_exporter($event);
 302          $values['subscription'] = $subscriptionexporter->export($output);
 303  
 304          $proxy = $this->event->get_category();
 305          if ($proxy && $proxy->get('id')) {
 306              $category = $proxy->get_proxied_instance();
 307              $categorysummaryexporter = new coursecat_summary_exporter($category, ['context' => $context]);
 308              $values['category'] = $categorysummaryexporter->export($output);
 309          }
 310  
 311          if ($course && $course->id != SITEID) {
 312              $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
 313              $values['course'] = $coursesummaryexporter->export($output);
 314          }
 315  
 316          $courseid = (!$course) ? SITEID : $course->id;
 317  
 318          $values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
 319          $values['candelete'] = calendar_delete_event_allowed($legacyevent);
 320  
 321          $deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
 322          $values['deleteurl'] = $deleteurl->out(false);
 323  
 324          $editurl = new moodle_url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
 325                  'course' => $courseid]);
 326          $values['editurl'] = $editurl->out(false);
 327          $viewurl = new moodle_url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
 328                  'time' => $timesort]);
 329          $viewurl->set_anchor('event_' . $event->get_id());
 330          $values['viewurl'] = $viewurl->out(false);
 331          $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
 332                  $timesort);
 333  
 334          if ($group = $event->get_group()) {
 335              $values['groupname'] = format_string($group->get('name'), true,
 336                  ['context' => \context_course::instance($event->get_course()->get('id'))]);
 337          }
 338  
 339          if ($event instanceof action_event_interface) {
 340              // Export event action if applicable.
 341              $actionrelated = [
 342                  'context' => $this->related['context'],
 343                  'event' => $event
 344              ];
 345              $actionexporter = new event_action_exporter($event->get_action(), $actionrelated);
 346              $values['action'] = $actionexporter->export($output);
 347          }
 348  
 349          return $values;
 350      }
 351  
 352      /**
 353       * Returns a list of objects that are related.
 354       *
 355       * @return array
 356       */
 357      protected static function define_related() {
 358          return [
 359              'context' => 'context',
 360              'course' => 'stdClass?',
 361          ];
 362      }
 363  }