MDL-79581 communication: Context is a mandatory field
The API was incorrectly assuming that all uses of the API were for a course, and that the instanceid of the communication instance was a course id. These assumptions are both entirely wrong. The API is intended to support a range of uses including use at the site, user, and activity levels. Furthermore, if a group were to be used, then the instanceid should be of that group's id, and therefore the contextid would need to be fetched or that group's course instead. The only solution here is to add a new contextid field to the table, and implement it all parts of the API.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
namespace core_communication;
|
||||
|
||||
use core\context;
|
||||
use stdClass;
|
||||
use stored_file;
|
||||
|
||||
@@ -68,6 +69,7 @@ class processor {
|
||||
/**
|
||||
* Create communication instance.
|
||||
*
|
||||
* @param context $context The context of the item for the instance
|
||||
* @param string $provider The communication provider
|
||||
* @param int $instanceid The instance id
|
||||
* @param string $component The component name
|
||||
@@ -76,6 +78,7 @@ class processor {
|
||||
* @return processor|null
|
||||
*/
|
||||
public static function create_instance(
|
||||
context $context,
|
||||
string $provider,
|
||||
int $instanceid,
|
||||
string $component,
|
||||
@@ -88,6 +91,7 @@ class processor {
|
||||
return null;
|
||||
}
|
||||
$record = (object) [
|
||||
'contextid' => $context->id,
|
||||
'provider' => $provider,
|
||||
'instanceid' => $instanceid,
|
||||
'component' => $component,
|
||||
@@ -357,12 +361,14 @@ class processor {
|
||||
/**
|
||||
* Load communication instance by instance id.
|
||||
*
|
||||
* @param context $context The context of the item for the instance
|
||||
* @param string $component The component name
|
||||
* @param string $instancetype The instance type
|
||||
* @param int $instanceid The instance id
|
||||
* @return processor|null
|
||||
*/
|
||||
public static function load_by_instance(
|
||||
context $context,
|
||||
string $component,
|
||||
string $instancetype,
|
||||
int $instanceid
|
||||
@@ -371,6 +377,7 @@ class processor {
|
||||
global $DB;
|
||||
|
||||
$record = $DB->get_record('communication', [
|
||||
'contextid' => $context->id,
|
||||
'instanceid' => $instanceid,
|
||||
'component' => $component,
|
||||
'instancetype' => $instancetype,
|
||||
@@ -411,6 +418,33 @@ class processor {
|
||||
return $this->instancedata->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the context of the communication instance.
|
||||
*
|
||||
* @return context
|
||||
*/
|
||||
public function get_context(): context {
|
||||
return context::instance_by_id($this->get_context_id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the context id of the communication instance.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_context_id(): int {
|
||||
return $this->instancedata->contextid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get communication instance type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_instance_type(): string {
|
||||
return $this->instancedata->instancetype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get communication instance id.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user