Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.7.x will end 11 May 2020 (12 months).
  • Bug fixes for security issues in 3.7.x will end 9 November 2020 (18 months) - Support has ended.
  • minimum PHP 7.1.0 Note: minimum PHP version has increased since Moodle 3.6. PHP 7.2.x and 7.3.x are supported too. PHP 7.x could have some engine limitations.
  • Differences Between: [Versions 35 and 37] [Versions 37 and 310] [Versions 37 and 311] [Versions 37 and 38] [Versions 37 and 39]

       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   * This file contains helper classes and functions for testing.
      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  defined('MOODLE_INTERNAL') || die();
      26  
      27  global $CFG;
      28  
      29  require_once($CFG->dirroot . '/calendar/lib.php');
      30  
      31  use core_calendar\local\event\entities\action_event;
      32  use core_calendar\local\event\entities\event;
      33  use core_calendar\local\event\entities\repeat_event_collection;
      34  use core_calendar\local\event\proxies\std_proxy;
      35  use core_calendar\local\event\proxies\coursecat_proxy;
      36  use core_calendar\local\event\proxies\cm_info_proxy;
      37  use core_calendar\local\event\value_objects\action;
      38  use core_calendar\local\event\value_objects\event_description;
      39  use core_calendar\local\event\value_objects\event_times;
      40  use core_calendar\local\event\factories\event_factory_interface;
      41  
      42  /**
      43   * Create a calendar event with the given properties.
      44   *
      45   * @param array $properties The properties to set on the event
      46   * @return \calendar_event
      47   */
      48  function create_event($properties) {
      49      $record = new \stdClass();
      50      $record->name = 'event name';
      51      $record->eventtype = 'global';
      52      $record->repeat = 0;
      53      $record->repeats = 0;
      54      $record->timestart = time();
      55      $record->timeduration = 0;
      56      $record->timesort = 0;
      57      $record->type = CALENDAR_EVENT_TYPE_STANDARD;
      58      $record->courseid = 0;
      59      $record->categoryid = 0;
      60  
      61      foreach ($properties as $name => $value) {
      62          $record->$name = $value;
      63      }
      64  
      65      $event = new \calendar_event($record);
      66      return $event->create($record);
      67  }
      68  
      69  /**
      70   * A test factory that will create action events.
      71   *
      72   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
      73   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
      74   */
      75  class action_event_test_factory implements event_factory_interface {
      76  
      77      /**
      78       * @var callable $callback.
      79       */
      80      private $callback;
      81  
      82      /**
      83       * A test factory that will create action events. The factory accepts a callback
      84       * that will be used to determine if the event should be returned or not.
      85       *
      86       * The callback will be given the event and should return true if the event
      87       * should be returned and false otherwise.
      88       *
      89       * @param callable $callback The callback.
      90       */
      91      public function __construct($callback = null) {
      92          $this->callback = $callback;
      93      }
      94  
      95      public function create_instance(\stdClass $record) {
      96          $module = null;
      97          $subscription = null;
      98  
      99          if ($record->instance && $record->modulename) {
     100              $module = new cm_info_proxy($record->instance, $record->modulename, $record->courseid);
     101          }
     102  
     103          if ($record->subscriptionid) {
     104              $subscription = new std_proxy($record->subscriptionid, function($id) {
     105                  return (object)['id' => $id];
     106              });
     107          }
     108  
     109          $event = new event(
     110              $record->id,
     111              $record->name,
     112              new event_description($record->description, $record->format),
     113              new coursecat_proxy($record->categoryid),
     114              new std_proxy($record->courseid, function($id) {
     115                  $course = new \stdClass();
     116                  $course->id = $id;
     117                  return $course;
     118              }),
     119              new std_proxy($record->groupid, function($id) {
     120                  $group = new \stdClass();
     121                  $group->id = $id;
     122                  return $group;
     123              }),
     124              new std_proxy($record->userid, function($id) {
     125                  $user = new \stdClass();
     126                  $user->id = $id;
     127                  return $user;
     128              }),
     129              !empty($record->repeatid) ? new repeat_event_collection($record, $this) : null,
     130              $module,
     131              $record->eventtype,
     132              new event_times(
     133                  (new \DateTimeImmutable())->setTimestamp($record->timestart),
     134                  (new \DateTimeImmutable())->setTimestamp($record->timestart + $record->timeduration),
     135                  (new \DateTimeImmutable())->setTimestamp($record->timesort ? $record->timesort : $record->timestart),
     136                  (new \DateTimeImmutable())->setTimestamp($record->timemodified)
     137              ),
     138              !empty($record->visible),
     139              $subscription,
     140              $record->location
     141          );
     142  
     143          $action = new action(
     144              'Test action',
     145              new \moodle_url('/'),
     146              1,
     147              true
     148          );
     149  
     150          $actionevent = new action_event($event, $action);
     151  
     152          if ($callback = $this->callback) {
     153              return $callback($actionevent) ? $actionevent : false;
     154          } else {
     155              return $actionevent;
     156          }
     157      }
     158  }