}
$this->properties = $data;
-
- if (empty($data->context)) {
- $this->properties->context = $this->calculate_context();
- }
}
/**
return $context;
}
+ /**
+ * Returns the context for this event. The context is calculated
+ * the first time is is requested and then stored in a member
+ * variable to be returned each subsequent time.
+ *
+ * This is a magical getter function that will be called when
+ * ever the context property is accessed, e.g. $event->context.
+ *
+ * @return context
+ */
+ protected function get_context() {
+ if (!isset($this->properties->context)) {
+ $this->properties->context = $this->calculate_context();
+ }
+
+ return $this->properties->context;
+ }
+
/**
* Returns an array of editoroptions for this event.
*
// Check if we have already resolved the context for this event.
if ($this->editorcontext === null) {
// Switch on the event type to decide upon the appropriate context to use for this event.
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
if (!calendar_is_valid_eventtype($this->properties->eventtype)) {
return clean_text($this->properties->description, $this->properties->format);
}
// Prepare event data.
$eventargs = array(
- 'context' => $this->properties->context,
+ 'context' => $this->get_context(),
'objectid' => $this->properties->id,
'other' => array(
'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
// were set when calculate_context() was called from the constructor.
if ($usingeditor) {
$this->properties->context = $this->calculate_context();
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
}
$editor = $this->properties->description;
// Log the event entry.
$eventargs['objectid'] = $this->properties->id;
- $eventargs['context'] = $this->properties->context;
+ $eventargs['context'] = $this->get_context();
$event = \core\event\calendar_event_created::create($eventargs);
$event->trigger();
// Trigger an event for the delete action.
$eventargs = array(
- 'context' => $this->properties->context,
+ 'context' => $this->get_context(),
'objectid' => $this->properties->id,
'other' => array(
'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
// If the editor context hasn't already been set then set it now.
if ($this->editorcontext === null) {
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
}
// If the context has been set delete all associated files.
if ($properties->eventtype === 'site') {
// Site context.
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
} else if ($properties->eventtype === 'user') {
// User context.
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
} else if ($properties->eventtype === 'group' || $properties->eventtype === 'course') {
// First check the course is valid.
$course = $DB->get_record('course', array('id' => $properties->courseid));
print_error('invalidcourse');
}
// Course context.
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
// We have a course and are within the course context so we had
// better use the courses max bytes value.
$this->editoroptions['maxbytes'] = $course->maxbytes;
// First check the course is valid.
\coursecat::get($properties->categoryid, MUST_EXIST, true);
// Course context.
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
// We have a course and are within the course context so we had
// better use the courses max bytes value.
$this->editoroptions['maxbytes'] = $course->maxbytes;
// Prepare event data.
$eventargs = array(
- 'context' => $this->properties->context,
+ 'context' => $this->get_context(),
'objectid' => $this->properties->id,
'other' => array(
'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
if ($this->editorcontext === null) {
// Switch on the event type to decide upon the appropriate context to use for this event.
- $this->editorcontext = $this->properties->context;
+ $this->editorcontext = $this->get_context();
if (!calendar_is_valid_eventtype($this->properties->eventtype)) {
// We don't have a context here, do a normal format_text.