From cb7d2bf2f182023e64afb84b20a4df4c14dc3eb0 Mon Sep 17 00:00:00 2001 From: Laurent David Date: Thu, 23 Jun 2022 16:51:48 +0200 Subject: [PATCH] MDL-73228 mod_bigbluebuttonbn: Close all meetings when deleting * When a BigblueButtonBN activity is deleted, we need to also close all running meetings (including group meetings) --- mod/bigbluebuttonbn/bbb_view.php | 4 +- mod/bigbluebuttonbn/classes/instance.php | 2 + .../lang/en/bigbluebuttonbn.php | 2 +- mod/bigbluebuttonbn/lib.php | 28 +++++-- mod/bigbluebuttonbn/tests/lib_test.php | 73 +++++++++++++++++++ 5 files changed, 99 insertions(+), 10 deletions(-) diff --git a/mod/bigbluebuttonbn/bbb_view.php b/mod/bigbluebuttonbn/bbb_view.php index e0a506c4cd1..3662701f391 100644 --- a/mod/bigbluebuttonbn/bbb_view.php +++ b/mod/bigbluebuttonbn/bbb_view.php @@ -56,7 +56,9 @@ if ($id) { } if (!$instance) { - throw new moodle_exception('view_error_url_missing_parameters', plugin::COMPONENT); + $courseid = optional_param('courseid', 1, PARAM_INT); + \core\notification::error(get_string('general_error_not_found', 'mod_bigbluebuttonbn', $id)); + redirect(new moodle_url('/course/view.php', ['id' => $courseid])); } $cm = $instance->get_cm(); diff --git a/mod/bigbluebuttonbn/classes/instance.php b/mod/bigbluebuttonbn/classes/instance.php index 51efd4d0e1c..92a27e9806a 100644 --- a/mod/bigbluebuttonbn/classes/instance.php +++ b/mod/bigbluebuttonbn/classes/instance.php @@ -999,6 +999,8 @@ EOF; return new moodle_url('/mod/bigbluebuttonbn/bbb_view.php', [ 'action' => 'logout', 'id' => $this->cm->id, + 'courseid' => $this->cm->course // Used to find the course if ever the activity is deleted + // while the meeting is running. ]); } diff --git a/mod/bigbluebuttonbn/lang/en/bigbluebuttonbn.php b/mod/bigbluebuttonbn/lang/en/bigbluebuttonbn.php index 02cf6e4033e..54694a61651 100644 --- a/mod/bigbluebuttonbn/lang/en/bigbluebuttonbn.php +++ b/mod/bigbluebuttonbn/lang/en/bigbluebuttonbn.php @@ -298,7 +298,7 @@ $string['general_error_unable_connect'] = 'Unable to connect. Please check the u Details : {$a}'; $string['general_error_no_answer'] = 'Empty response. Please check the url of the BigBlueButton server AND check to see if the BigBlueButton server is running.'; $string['general_error_not_allowed_to_create_instances'] = 'User is not allowed to create any type of instance.'; -$string['general_error_not_found'] = 'Entity not found : {$a}.'; +$string['general_error_not_found'] = 'Cannot find the BigBlueButton activity ({$a}).'; $string['general_error_cannot_create_meeting'] = 'Cannot create session.'; $string['general_error_cannot_get_recordings'] = 'Cannot get recordings.'; $string['index_confirm_end'] = 'Do you want to end the session?'; diff --git a/mod/bigbluebuttonbn/lib.php b/mod/bigbluebuttonbn/lib.php index 93394deec05..5a9493e6de1 100644 --- a/mod/bigbluebuttonbn/lib.php +++ b/mod/bigbluebuttonbn/lib.php @@ -159,20 +159,32 @@ function bigbluebuttonbn_delete_instance($id) { try { $meeting = new meeting($instance); $meeting->end_meeting(); - $groups = groups_get_course_group($instance->get_course()); - if ($groups) { - foreach ($groups as $group) { - $instance->set_group_id($group->id); - $meeting = new meeting($instance); - $meeting->end_meeting(); - } - } } catch (moodle_exception $e) { // Do not log any issue when testing. if (!(defined('PHPUNIT_TEST') && PHPUNIT_TEST) && !defined('BEHAT_SITE_RUNNING')) { debugging($e->getMessage(), DEBUG_NORMAL, $e->getTrace()); } } + // Get all possible groups (course and course module). + $groupids = []; + if (groups_get_activity_groupmode($instance->get_cm())) { + $coursegroups = groups_get_activity_allowed_groups($instance->get_cm()); + $groupids = array_map( + function($gp) { + return $gp->id; + }, + $coursegroups); + } + // End all meetings for all groups. + foreach ($groupids as $groupid) { + try { + $instance->set_group_id($groupid); + $meeting = new meeting($instance); + $meeting->end_meeting(); + } catch (moodle_exception $e) { + debugging($e->getMessage() . ' for group ' . $groupid, DEBUG_NORMAL, $e->getTrace()); + } + } $result = true; diff --git a/mod/bigbluebuttonbn/tests/lib_test.php b/mod/bigbluebuttonbn/tests/lib_test.php index 4bd63f765cb..100e5c6d179 100644 --- a/mod/bigbluebuttonbn/tests/lib_test.php +++ b/mod/bigbluebuttonbn/tests/lib_test.php @@ -100,6 +100,79 @@ class lib_test extends \advanced_testcase { $this->assertTrue($result); } + /** + * Check delete instance + * + * @covers ::bigbluebuttonbn_delete_instance + */ + public function test_bigbluebuttonbn_delete_instance_with_running_meeting() { + $this->resetAfterTest(); + $this->initialise_mock_server(); + list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance(); + $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn'); + $instance = instance::get_from_instanceid($bbactivity->id); + // Run the meeting. + $bbbgenerator->create_meeting([ + 'instanceid' => $instance->get_instance_id(), + 'groupid' => $instance->get_group_id(), + ]); + $meeting = new meeting($instance); + $meeting->update_cache(); + $this->assertTrue($meeting->is_running()); + $result = bigbluebuttonbn_delete_instance($bbactivity->id); + $this->assertTrue($result); + $meeting->update_cache(); + $this->assertFalse($meeting->is_running()); + } + + /** + * Check delete instance + * + * @covers ::bigbluebuttonbn_delete_instance + */ + public function test_bigbluebuttonbn_delete_instance_with_running_group_meetings() { + $this->resetAfterTest(); + $this->initialise_mock_server(); + $datagenerator = $this->getDataGenerator(); + list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance(); + $course = $this->get_course(); + set_coursemodule_groupmode($bbactivitycm->id, VISIBLEGROUPS); + + $groups = []; + foreach (['G1', 'G2'] as $gname) { + $groups[] = $datagenerator->create_group(['courseid' => $course->id, 'name' => $gname]); + } + // Just create a user in one of the group so we check we don't just end meetings for this user... + $user = $datagenerator->create_and_enrol($this->get_course()); + $groupids = array_map(function($g) { + return $g->id; + }, $groups); + $datagenerator->create_group_member(['userid' => $user->id, 'groupid' => $groupids[0]]); + $this->setUser($user); + + $groupids[] = 0; // Add no group (id=0) - as an item so it is covered in the loop. + $bbbgenerator = $datagenerator->get_plugin_generator('mod_bigbluebuttonbn'); + $globalinstance = instance::get_from_instanceid($bbactivity->id); + $meetings = []; + foreach ($groupids as $groupid) { + $instance = instance::get_group_instance_from_instance($globalinstance, $groupid); + // Run the meetings. + $bbbgenerator->create_meeting([ + 'instanceid' => $instance->get_instance_id(), + 'groupid' => $instance->get_group_id(), + ]); + $meeting = new meeting($instance); + $meeting->update_cache(); + $this->assertTrue($meeting->is_running()); + $meetings[] = $meeting; + } + $result = bigbluebuttonbn_delete_instance($bbactivity->id); + $this->assertTrue($result); + foreach ($meetings as $meeting) { + $meeting->update_cache(); + $this->assertFalse($meeting->is_running()); + } + } /** * Check user outline page *