Skip to content

Commit

Permalink
MDL-82872 mod_bigbluebuttonbn Hook for extending broker_meeting_events
Browse files Browse the repository at this point in the history
  • Loading branch information
ssj365 committed Aug 23, 2024
1 parent e0a9cf2 commit 7a8efb0
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 10 deletions.
4 changes: 1 addition & 3 deletions mod/bigbluebuttonbn/bbb_broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@

$params = $_REQUEST;

$broker = new broker();
$error = $broker->validate_parameters($params);
$error = broker::validate_parameters($params);
if (!empty($error)) {
header('HTTP/1.0 400 Bad Request. ' . $error);
return;
}

$action = $params['action'];

$instance = instance::get_from_instanceid($params['bigbluebuttonbn']);
Expand Down
22 changes: 16 additions & 6 deletions mod/bigbluebuttonbn/classes/broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use mod_bigbluebuttonbn\local\config;
use mod_bigbluebuttonbn\extension;

/**
* The broker routines
Expand All @@ -32,7 +33,7 @@
class broker {

/** @var array List of required params */
protected $requiredparams = [
protected static $requiredparams = [
'recording_ready' => [
'bigbluebuttonbn' => 'The BigBlueButtonBN instance ID must be specified.',
'signed_parameters' => 'A JWT encoded string must be included as [signed_parameters].'
Expand All @@ -48,16 +49,16 @@ class broker {
* @param array $params
* @return null|string
*/
public function validate_parameters(array $params): ?string {
public static function validate_parameters(array $params): ?string {
if (!isset($params['action']) || empty($params['action']) ) {
return 'Parameter ['.$params['action'].'] was not included';
}

$action = strtolower($params['action']);
if (!array_key_exists($action, $this->requiredparams)) {
if (!array_key_exists($action, self::$requiredparams)) {
return "Action {$params['action']} can not be performed.";
}
return $this->validate_parameters_message($params, $this->requiredparams[$action]);
return self::validate_parameters_message($params, self::$requiredparams[$action]);
}

/**
Expand Down Expand Up @@ -153,15 +154,24 @@ public static function process_meeting_events(instance $instance) {

// Get JSON string from the body.
$jsonstr = file_get_contents('php://input');
debugging($jsonstr, DEBUG_DEVELOPER);

// Convert JSON string to a JSON object.
$jsonobj = json_decode($jsonstr);
$headermsg = meeting::meeting_events($instance, $jsonobj);
header($headermsg);

// Hooks for extensions.
$extensions = extension::broker_meeting_events_addons_instances($instance, $jsonstr);
foreach ($extensions as $extension) {
$extension->process_action();
}
} catch (Exception $e) {
$msg = 'Caught exception: ' . $e->getMessage();
header('HTTP/1.0 400 Bad Request. ' . $msg);
debugging($msg, DEBUG_DEVELOPER);
$headermsg = 'HTTP/1.0 400 Bad Request. ' . $msg;
}

header($headermsg);
}


Expand Down
21 changes: 21 additions & 0 deletions mod/bigbluebuttonbn/classes/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use cache;
use cm_info;
use mod_bigbluebuttonbn\local\extension\action_url_addons;
use mod_bigbluebuttonbn\local\extension\broker_meeting_events_addons;
use mod_bigbluebuttonbn\local\extension\custom_completion_addons;
use mod_bigbluebuttonbn\local\extension\mod_form_addons;
use mod_bigbluebuttonbn\local\extension\mod_instance_helper;
Expand Down Expand Up @@ -217,4 +218,24 @@ public static function delete_instance(int $id): void {
$fmclass->delete_instance($id);
}
}

/**
* Get all broker_meeting_events addons classes.
*
* @return array of broker_meeting_events addon classes.
*/
public static function broker_meeting_events_addons_classes(): array {
return self::get_classes_implementing(broker_meeting_events_addons::class);
}

/**
* Get all broker_meeting_events addons classes instances
*
* @param instance|null $instance
* @param string|null $data
* @return array of custom completion addon classes instances
*/
public static function broker_meeting_events_addons_instances(instance $instance, string $data): array {
return self::get_instances_implementing(broker_meeting_events_addons::class, [$instance, $data]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_bigbluebuttonbn\local\extension;

use stdClass;
use Firebase\JWT\Key;
use mod_bigbluebuttonbn\broker;
use mod_bigbluebuttonbn\instance;
use mod_bigbluebuttonbn\meeting;

/**
* A class to deal with broker addons in a subplugin
*
* @package mod_bigbluebuttonbn
* @copyright 2024 onwards, Blindside Networks Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Jesus Federico (jesus [at] blindsidenetworks [dt] com)
*/
abstract class broker_meeting_events_addons {

/**
* @var instance|null $instance BigBlueButton instance if any
*/
protected $instance = null;

/**
* @var string|null $data data to be processed
*/
protected $data = null;

/**
* Constructor
*
* @param instance|null $instance BigBlueButton instance if any
* @param string|null $data data to be processed
*/
public function __construct(?instance $instance = null, ?string $data = null) {
$this->instance = $instance;
$this->data = $data;
}

/**
* Data processing action
*/
abstract public function process_action();
}
1 change: 1 addition & 0 deletions mod/bigbluebuttonbn/classes/meeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ public static function meeting_events(instance $instance, object $data): string
$meta['internalmeetingid'] = $data->{'internal_meeting_id'};
$meta['callback'] = 'meeting_events';
$meta['meetingid'] = $data->{'meeting_id'};
$meta['data'] = $data->{'data'};

$eventcount = logger::log_event_callback($instance, $overrides, $meta);
if ($eventcount === 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function define_bigbluebuttonbn_subplugin_structure() {
$subpluginelement = new backup_nested_element(
'bbbext_simple',
null,
['newfield', 'completionextraisehandtwice']
['newfield', 'completionextraisehandtwice', 'meetingevents']
);

// Connect XML elements into the tree.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace bbbext_simple\bigbluebuttonbn;

use mod_bigbluebuttonbn\broker;
use mod_bigbluebuttonbn\instance;

/**
* When meeting_events callback is implemented by BigBlueButton, Moodle receives a POST request
* which is processed in the function using super globals.
*
* @package bbbext_b3dummy_broker_meeting_events
* @copyright 2024 onwards, Blindside Networks Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Jesus Federico (jesus [at] blindsidenetworks [dt] com)
*/
class broker_meeting_events_addons extends \mod_bigbluebuttonbn\local\extension\broker_meeting_events_addons {

/**
* Constructor
*
* @param instance|null $instance BigBlueButton instance if any
* @param string|null $data data to be processed
*/
public function __construct(?instance $instance = null, ?string $data = null) {
parent::__construct($instance, $data);
}

/**
* Data processing action
*/
public function process_action() {
global $DB;
if ($this->instance) {
$bigbluebuttonbnid = $this->instance->get_instance_id();
$record = $DB->get_record('bbbext_simple', [
'bigbluebuttonbnid' => $bigbluebuttonbnid,
]);
if ($record) {
$record->meetingevents = $this->data;
return $DB->update_record('bbbext_simple', $record);
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function add_instance(stdClass $bigbluebuttonbn) {
'bigbluebuttonbnid' => $bigbluebuttonbn->id,
'newfield' => $bigbluebuttonbn->newfield ?? '',
'completionextraisehandtwice' => $bigbluebuttonbn->completionextraisehandtwice ?? '',
'meetingevents' => $bigbluebuttonbn->meetingevents ?? '',
]);
}

Expand All @@ -56,10 +57,12 @@ public function update_instance(stdClass $bigbluebuttonbn): void {
$record->bigbluebuttonbnid = $bigbluebuttonbn->id;
$record->newfield = $bigbluebuttonbn->newfield ?? '';
$record->completionextraisehandtwice = $bigbluebuttonbn->completionextraisehandtwice ?? 0;
$record->meetingevents = $bigbluebuttonbn->meetingevents ?? '';
$DB->insert_record('bbbext_simple', $record);
} else {
$record->newfield = $bigbluebuttonbn->newfield ?? '';
$record->completionextraisehandtwice = $bigbluebuttonbn->completionextraisehandtwice ?? 0;
$record->meetingevents = $bigbluebuttonbn->meetingevents ?? '';
$DB->update_record('bbbext_simple', $record);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<FIELD NAME="bigbluebuttonbnid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="newfield" TYPE="int" LENGTH="2" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="completionextraisehandtwice" TYPE="int" LENGTH="9" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="meetingevents" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
51 changes: 51 additions & 0 deletions mod/bigbluebuttonbn/tests/local/extension_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use mod_bigbluebuttonbn\completion\custom_completion;
use mod_bigbluebuttonbn\extension;
use mod_bigbluebuttonbn\instance;
use mod_bigbluebuttonbn\local\extension\broker_meeting_events_addons;
use mod_bigbluebuttonbn\local\extension\mod_instance_helper;
use mod_bigbluebuttonbn\meeting;
use mod_bigbluebuttonbn\test\subplugins_test_helper_trait;
Expand Down Expand Up @@ -369,6 +370,56 @@ public static function custom_completion_data_provider(): array {
];
}

/**
* Test broker meeting_events with and without addons.
* @return void
* @covers \mod_bigbluebuttonbn\local\extension\broker_meeting_events_addons
*/
public function test_broker_meeting_events_addons(): void {
$this->resetAfterTest();
global $DB;
// Enable plugin.
$this->enable_plugins(true);
$this->initialise_mock_server();
list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance(
$this->get_course());
$plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

// Now create a couple of events.
$instance = instance::get_from_instanceid($bbactivity->id);
set_config('bigbluebuttonbn_meetingevents_enabled', true);
$meeting = $plugingenerator->create_meeting([
'instanceid' => $instance->get_instance_id(),
'groupid' => $instance->get_group_id(),
'participants' => json_encode([$user->id]),
]);

$events = [
(object) ['name' => 'talks'],
(object) ['name' => 'raisehand'],
(object) ['name' => 'raisehand'],
];
foreach ($events as $edesc) {
$plugingenerator->add_meeting_event($user, $instance, $edesc->name, $edesc->data ?? '');
}
$result = $plugingenerator->send_all_events($instance);
$this->assertNotEmpty($result->data);
$resultstring = json_encode($result->data);
$data = json_decode($resultstring);

meeting::meeting_events($instance, $data);
$extensions = extension::broker_meeting_events_addons_instances($instance, json_encode($data));
foreach ($extensions as $extension) {
$extension->process_action();
}
$addondata = $DB->get_field('bbbext_simple', 'meetingevents', ['bigbluebuttonbnid' => $bbactivity->id]);
$addondata = json_decode($addondata);
// Check that the data is received.
$this->assertEquals(json_encode($addondata), $resultstring);
}

/**
* Data provider for testing get_class_implementing
*
Expand Down

0 comments on commit 7a8efb0

Please sign in to comment.