diff --git a/mod/bigbluebuttonbn/classes/task/base_send_notification.php b/mod/bigbluebuttonbn/classes/task/base_send_notification.php new file mode 100644 index 00000000000..b6db7ed115d --- /dev/null +++ b/mod/bigbluebuttonbn/classes/task/base_send_notification.php @@ -0,0 +1,224 @@ +. + +namespace mod_bigbluebuttonbn\task; + +use core\message\message; +use core\task\adhoc_task; +use mod_bigbluebuttonbn\instance; +use moodle_exception; +use stdClass; + +/** + * Class containing the abstract class for notification processes in BBB. + * + * @package mod_bigbluebuttonbn + * @copyright 2021 Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class base_send_notification extends adhoc_task { + + /** @var instance */ + protected $instance = null; + + /** @var object */ + protected $coursecontact = null; + + /** + * Execute the task. + */ + public function execute() { + $this->send_all_notifications(); + } + + /** + * Append additional elements of custom data + * + * @param array $newdata + */ + protected function append_custom_data(array $newdata): void { + if ($currentdata = (array) $this->get_custom_data()) { + $newdata = array_merge($currentdata, $newdata); + } + + $this->set_custom_data($newdata); + } + + /** + * Set the instanceid in the custom data. + * + * @param int $instanceid + */ + public function set_instance_id(int $instanceid): void { + $this->append_custom_data(['instanceid' => $instanceid]); + } + + /** + * Get the bigbluebutton instance that this notification is for. + * + * @return instance + */ + protected function get_instance(): instance { + if ($this->instance === null) { + $this->instance = instance::get_from_instanceid($this->get_custom_data()->instanceid); + } + + return $this->instance; + } + + /** + * Get the preferred course contact for this notification. + * + * @return stdClass + */ + protected function get_course_contact(): stdClass { + global $DB; + + if ($this->coursecontact === null) { + // Get course managers so they can be highlighted in the list. + $coursecontext = $this->get_instance()->get_course_context(); + + if ($managerroles = get_config('', 'coursecontact')) { + $coursecontactroles = explode(',', $managerroles); + foreach ($coursecontactroles as $roleid) { + $contacts = get_role_users($roleid, $coursecontext, true, 'u.id', 'u.id ASC'); + foreach ($contacts as $contact) { + $this->coursecontact = $contact; + break; + } + } + } + + if ($this->coursecontact === null) { + $this->coursecontact = \core_user::get_noreply_user(); + } + } + + return $this->coursecontact; + } + + /** + * Get the list of recipients for the notification. + * + * @return stdClass[] + */ + protected function get_recipients(): array { + // Potential users should be active users only. + return get_enrolled_users( + $this->get_instance()->get_course_context(), + 'mod/bigbluebuttonbn:view', + 0, + 'u.*', + null, + 0, + 0, + true + ); + } + + /** + * Get the HTML message content. + * + * @return string + */ + abstract protected function get_html_message(): string; + + /** + * Get the plain text message content. + * + * @return string + */ + protected function get_message(): string { + return html_to_text($this->get_html_message()); + } + + /** + * Get the short summary message. + * + * @return string + */ + abstract protected function get_small_message(): string; + + /** + * Get the preferred message format + * + * @return string + */ + protected function get_message_format(): string { + return FORMAT_HTML; + } + + /** + * Get the notification type. + * + * @return string + */ + abstract protected function get_notification_type(): string; + + /** + * Get the subject of the notification. + * + * @return string + */ + abstract protected function get_subject(): string; + + /** + * Send all of the notifications + */ + protected function send_all_notifications(): void { + $instance = $this->get_instance(); + foreach ($this->get_recipients() as $recipient) { + try { + \core_user::require_active_user($recipient, true, true); + cron_setup_user($recipient); + } catch (moodle_exception $e) { + // Skip sending. + continue; + } + + $this->send_notification_to_current_user(); + } + + cron_setup_user(); + } + + /** + * Send the notificiation to the current user. + */ + protected function send_notification_to_current_user(): void { + global $USER; + + $instance = $this->get_instance(); + + $eventdata = new message(); + $eventdata->courseid = $instance->get_course_id(); + $eventdata->component = 'mod_bigbluebuttonbn'; + $eventdata->name = $this->get_notification_type(); + $eventdata->userfrom = $this->get_course_contact(); + $eventdata->userto = $USER; + + $eventdata->subject = $this->get_subject(); + $eventdata->smallmessage = $this->get_small_message(); + $eventdata->fullmessage = $this->get_message(); + $eventdata->fullmessageformat = $this->get_message_format(); + $eventdata->fullmessagehtml = $this->get_html_message(); + $eventdata->notification = 1; + $eventdata->contexturl = $this->get_instance()->get_view_url(); + $eventdata->contexturlname = $this->get_instance()->get_meeting_name(); + + message_send($eventdata); + } +} diff --git a/mod/bigbluebuttonbn/classes/task/send_guest_emails.php b/mod/bigbluebuttonbn/classes/task/send_guest_emails.php index fadbc004ef4..67da1e23d9a 100644 --- a/mod/bigbluebuttonbn/classes/task/send_guest_emails.php +++ b/mod/bigbluebuttonbn/classes/task/send_guest_emails.php @@ -25,7 +25,7 @@ use html_writer; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Laurent David (laurent [at] call-learning [dt] fr) */ -class send_guest_emails extends send_notification { +class send_guest_emails extends base_send_notification { /** * Get the notification type. diff --git a/mod/bigbluebuttonbn/classes/task/send_notification.php b/mod/bigbluebuttonbn/classes/task/send_notification.php index c2110b1b90c..bc2db0850ef 100644 --- a/mod/bigbluebuttonbn/classes/task/send_notification.php +++ b/mod/bigbluebuttonbn/classes/task/send_notification.php @@ -15,210 +15,31 @@ // along with Moodle. If not, see . namespace mod_bigbluebuttonbn\task; - -use core\message\message; use core\task\adhoc_task; -use mod_bigbluebuttonbn\instance; -use moodle_exception; -use stdClass; /** - * Class containing the abstract class for notification processes in BBB. + * Class containing the deprecated class for send_notification event in BBB. * * @package mod_bigbluebuttonbn * @copyright 2021 Andrew Lyons * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -abstract class send_notification extends adhoc_task { - - /** @var instance */ - protected $instance = null; - - /** @var object */ - protected $coursecontact = null; - +class send_notification extends adhoc_task { /** * Execute the task. */ public function execute() { - $this->send_all_notifications(); + // Log the debug message. + $message = $this->generate_message(); + debugging($message, DEBUG_DEVELOPER); } /** - * Append additional elements of custom data + * Output the debug log message. * - * @param array $newdata + * @return string The debug log message. */ - protected function append_custom_data(array $newdata): void { - if ($currentdata = (array) $this->get_custom_data()) { - $newdata = array_merge($currentdata, $newdata); - } - - $this->set_custom_data($newdata); - } - - /** - * Set the instanceid in the custom data. - * - * @param int $instanceid - */ - public function set_instance_id(int $instanceid): void { - $this->append_custom_data(['instanceid' => $instanceid]); - } - - /** - * Get the bigbluebutton instance that this notification is for. - * - * @return instance - */ - protected function get_instance(): instance { - if ($this->instance === null) { - $this->instance = instance::get_from_instanceid($this->get_custom_data()->instanceid); - } - - return $this->instance; - } - - /** - * Get the preferred course contact for this notification. - * - * @return stdClass - */ - protected function get_course_contact(): stdClass { - global $DB; - - if ($this->coursecontact === null) { - // Get course managers so they can be highlighted in the list. - $coursecontext = $this->get_instance()->get_course_context(); - - if ($managerroles = get_config('', 'coursecontact')) { - $coursecontactroles = explode(',', $managerroles); - foreach ($coursecontactroles as $roleid) { - $contacts = get_role_users($roleid, $coursecontext, true, 'u.id', 'u.id ASC'); - foreach ($contacts as $contact) { - $this->coursecontact = $contact; - break; - } - } - } - - if ($this->coursecontact === null) { - $this->coursecontact = \core_user::get_noreply_user(); - } - } - - return $this->coursecontact; - } - - /** - * Get the list of recipients for the notification. - * - * @return stdClass[] - */ - protected function get_recipients(): array { - // Potential users should be active users only. - return get_enrolled_users( - $this->get_instance()->get_course_context(), - 'mod/bigbluebuttonbn:view', - 0, - 'u.*', - null, - 0, - 0, - true - ); - } - - /** - * Get the HTML message content. - * - * @return string - */ - abstract protected function get_html_message(): string; - - /** - * Get the plain text message content. - * - * @return string - */ - protected function get_message(): string { - return html_to_text($this->get_html_message()); - } - - /** - * Get the short summary message. - * - * @return string - */ - abstract protected function get_small_message(): string; - - /** - * Get the preferred message format - * - * @return string - */ - protected function get_message_format(): string { - return FORMAT_HTML; - } - - /** - * Get the notification type. - * - * @return string - */ - abstract protected function get_notification_type(): string; - - /** - * Get the subject of the notification. - * - * @return string - */ - abstract protected function get_subject(): string; - - /** - * Send all of the notifications - */ - protected function send_all_notifications(): void { - $instance = $this->get_instance(); - foreach ($this->get_recipients() as $recipient) { - try { - \core_user::require_active_user($recipient, true, true); - cron_setup_user($recipient); - } catch (moodle_exception $e) { - // Skip sending. - continue; - } - - $this->send_notification_to_current_user(); - } - - cron_setup_user(); - } - - /** - * Send the notificiation to the current user. - */ - protected function send_notification_to_current_user(): void { - global $USER; - - $instance = $this->get_instance(); - - $eventdata = new message(); - $eventdata->courseid = $instance->get_course_id(); - $eventdata->component = 'mod_bigbluebuttonbn'; - $eventdata->name = $this->get_notification_type(); - $eventdata->userfrom = $this->get_course_contact(); - $eventdata->userto = $USER; - - $eventdata->subject = $this->get_subject(); - $eventdata->smallmessage = $this->get_small_message(); - $eventdata->fullmessage = $this->get_message(); - $eventdata->fullmessageformat = $this->get_message_format(); - $eventdata->fullmessagehtml = $this->get_html_message(); - $eventdata->notification = 1; - $eventdata->contexturl = $this->get_instance()->get_view_url(); - $eventdata->contexturlname = $this->get_instance()->get_meeting_name(); - - message_send($eventdata); + public function generate_message() { + return "Attempted to run deprecated implementation of send_notification task."; } } diff --git a/mod/bigbluebuttonbn/classes/task/send_recording_ready_notification.php b/mod/bigbluebuttonbn/classes/task/send_recording_ready_notification.php index adbda589bab..d65944fd79b 100644 --- a/mod/bigbluebuttonbn/classes/task/send_recording_ready_notification.php +++ b/mod/bigbluebuttonbn/classes/task/send_recording_ready_notification.php @@ -25,7 +25,7 @@ use html_writer; * @copyright 2021 Andrew Lyons * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class send_recording_ready_notification extends send_notification { +class send_recording_ready_notification extends base_send_notification { /** * Get the notification type. * diff --git a/mod/bigbluebuttonbn/tests/task/base_send_notification_test.php b/mod/bigbluebuttonbn/tests/task/base_send_notification_test.php new file mode 100644 index 00000000000..e70c740888a --- /dev/null +++ b/mod/bigbluebuttonbn/tests/task/base_send_notification_test.php @@ -0,0 +1,62 @@ +. + +namespace mod_bigbluebuttonbn\task; + +use advanced_testcase; + +/** + * Class containing the scheduled task for lti module. + * + * @package mod_bigbluebuttonbn + * @copyright 2019 onwards, Blindside Networks Inc + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \mod_bigbluebuttonbn\task\base_send_notification + * @coversDefaultClass \mod_bigbluebuttonbn\task\base_send_notification + */ +class base_send_notification_test extends advanced_testcase { + /** + * Check if set instance ID works correctly + * + */ + public function test_set_instance_id(): void { + $this->resetAfterTest(); + $stub = $this->getMockForAbstractClass( + base_send_notification::class, + [], + '', + true, + true, + true, + [] + ); + + $generator = $this->getDataGenerator(); + $course = $generator->create_course(); + $instancedata = $generator->create_module('bigbluebuttonbn', [ + 'course' => $course->id, + ]); + + $stub->set_instance_id($instancedata->id); + + $rc = new \ReflectionClass(base_send_notification::class); + $rcm = $rc->getMethod('get_instance'); + $rcm->setAccessible(true); + $instance = $rcm->invoke($stub); + + $this->assertEquals($instancedata->id, $instance->get_instance_id()); + } +} diff --git a/mod/bigbluebuttonbn/tests/task/send_notification_test.php b/mod/bigbluebuttonbn/tests/task/send_notification_test.php index 915e3077b34..9aa003f8675 100644 --- a/mod/bigbluebuttonbn/tests/task/send_notification_test.php +++ b/mod/bigbluebuttonbn/tests/task/send_notification_test.php @@ -29,34 +29,13 @@ use advanced_testcase; */ class send_notification_test extends advanced_testcase { /** - * Check if set instance ID works correctly + * Test that the debug message is correctly output. * */ - public function test_set_instance_id(): void { + public function test_generate_message() { $this->resetAfterTest(); - $stub = $this->getMockForAbstractClass( - send_notification::class, - [], - '', - true, - true, - true, - [] - ); - - $generator = $this->getDataGenerator(); - $course = $generator->create_course(); - $instancedata = $generator->create_module('bigbluebuttonbn', [ - 'course' => $course->id, - ]); - - $stub->set_instance_id($instancedata->id); - - $rc = new \ReflectionClass(send_notification::class); - $rcm = $rc->getMethod('get_instance'); - $rcm->setAccessible(true); - $instance = $rcm->invoke($stub); - - $this->assertEquals($instancedata->id, $instance->get_instance_id()); + $task = new send_notification(); + $message = $task->generate_message(); + $this->assertEquals("Attempted to run deprecated implementation of send_notification task.", $message); } } diff --git a/mod/bigbluebuttonbn/tests/task/send_recording_ready_notification_test.php b/mod/bigbluebuttonbn/tests/task/send_recording_ready_notification_test.php index 7c9e497ebb8..2675a6cd6c3 100644 --- a/mod/bigbluebuttonbn/tests/task/send_recording_ready_notification_test.php +++ b/mod/bigbluebuttonbn/tests/task/send_recording_ready_notification_test.php @@ -24,8 +24,8 @@ use advanced_testcase; * @package mod_bigbluebuttonbn * @copyright 2019 onwards, Blindside Networks Inc * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @coversDefaultClass \mod_bigbluebuttonbn\task\send_notification - * @covers \mod_bigbluebuttonbn\task\send_notification + * @coversDefaultClass \mod_bigbluebuttonbn\task\base_send_notification + * @covers \mod_bigbluebuttonbn\task\base_send_notification * @covers \mod_bigbluebuttonbn\task\send_recording_ready_notification */ class send_recording_ready_notification_test extends advanced_testcase {