Commit 08a79155 authored by Ryan Wyllie's avatar Ryan Wyllie

MDL-60964 calendar: lazy load context for calendar_event

Stop loading the context in the constructor if it hasn't been provided.
There is no need to load it by default. We should lazily load it when it
is required.
parent 5f54a876
......@@ -249,10 +249,6 @@ class calendar_event {
}
$this->properties = $data;
if (empty($data->context)) {
$this->properties->context = $this->calculate_context();
}
}
/**
......@@ -343,6 +339,24 @@ class calendar_event {
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.
*
......@@ -367,7 +381,7 @@ class calendar_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);
}
......@@ -433,7 +447,7 @@ class calendar_event {
// 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,
......@@ -485,7 +499,7 @@ class calendar_event {
// 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;
......@@ -512,7 +526,7 @@ class calendar_event {
// 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();
......@@ -681,7 +695,7 @@ class calendar_event {
// 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,
......@@ -715,7 +729,7 @@ class calendar_event {
// 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.
......@@ -774,10 +788,10 @@ class calendar_event {
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));
......@@ -785,7 +799,7 @@ class calendar_event {
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;
......@@ -793,7 +807,7 @@ class calendar_event {
// 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;
......@@ -869,7 +883,7 @@ class calendar_event {
// 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,
......@@ -939,7 +953,7 @@ class calendar_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)) {
// We don't have a context here, do a normal format_text.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment