MDL-78267 core: Move shared MoodleNet code to abstract

This commit is contained in:
Huong Nguyen 2023-07-21 14:51:41 +07:00
parent 9cd44cbf4c
commit b3ffe8432b
No known key found for this signature in database
GPG Key ID: 40D88AB693A3E72A
3 changed files with 11 additions and 29 deletions

View File

@ -54,6 +54,7 @@ class activity_sender extends resource_sender {
) {
parent::__construct($cmid, $userid, $moodlenetclient, $oauthclient, $shareformat);
[$this->course, $this->cminfo] = get_course_and_cm_from_cmid($cmid);
$this->packager = new activity_packager($this->cminfo, $this->userid);
}
/**
@ -137,21 +138,6 @@ class activity_sender extends resource_sender {
];
}
/**
* Prepare the data for sharing, in the format specified.
*
* @return stored_file
*/
protected function prepare_share_contents(): stored_file {
switch ($this->shareformat) {
case self::SHARE_FORMAT_BACKUP:
// If sharing the activity as a backup, prepare the packaged backup.
return (new activity_packager($this->cminfo, $this->userid))->get_package();
default:
throw new \coding_exception("Unknown share format: {$this->shareformat}'");
}
}
/**
* Log an event to the admin logs for an outbound share attempt.
*

View File

@ -54,6 +54,7 @@ class course_sender extends resource_sender {
parent::__construct($courseid, $userid, $moodlenetclient, $oauthclient, $shareformat);
$this->course = get_course($courseid);
$this->coursecontext = \core\context\course::instance($courseid);
$this->packager = new course_packager($this->course, $this->userid);
}
/**
@ -120,19 +121,6 @@ class course_sender extends resource_sender {
];
}
/**
* Prepare the data for sharing, in the format specified.
*
* @return stored_file
*/
protected function prepare_share_contents(): stored_file {
$packager = new course_packager($this->course, $this->userid);
return match ($this->shareformat) {
self::SHARE_FORMAT_BACKUP => $packager->get_package(),
default => throw new \coding_exception("Unknown share format: {$this->shareformat}'"),
};
}
/**
* Log an event to the admin logs for an outbound share attempt.
*

View File

@ -45,6 +45,9 @@ abstract class resource_sender {
*/
protected stdClass $course;
/** @var resource_packager Resource packager. */
protected resource_packager $packager;
/**
* Class constructor.
*
@ -91,5 +94,10 @@ abstract class resource_sender {
*
* @return stored_file
*/
abstract protected function prepare_share_contents(): stored_file;
protected function prepare_share_contents(): stored_file {
return match ($this->shareformat) {
self::SHARE_FORMAT_BACKUP => $this->packager->get_package(),
default => throw new \coding_exception("Unknown share format: {$this->shareformat}'"),
};
}
}