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