Skip to content

Commit

Permalink
Add Analytics Callback URL global setting
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentdavid committed Nov 29, 2023
1 parent 700a785 commit 187245d
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 34 deletions.
2 changes: 1 addition & 1 deletion backup/moodle2/backup_bbbext_flexurl_subplugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function define_bigbluebuttonbn_subplugin_structure() {

// Set source to populate the data.
$subpluginelement->set_source_table('bbbext_flexurl',
array('bigbluebuttonbnid' => backup::VAR_PARENTID));
['bigbluebuttonbnid' => backup::VAR_PARENTID]);

return $subplugin;
}
Expand Down
2 changes: 1 addition & 1 deletion backup/moodle2/restore_bbbext_flexurl_subplugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class restore_bbbext_flexurl_subplugin extends restore_subplugin {
*/
protected function define_bigbluebuttonbn_subplugin_structure() {

$paths = array();
$paths = [];

$elename = $this->get_namefor('bigbluebuttonbn');
// We used get_recommended_name() so this works.
Expand Down
6 changes: 6 additions & 0 deletions classes/bigbluebuttonbn/action_url_addons.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class action_url_addons extends \mod_bigbluebuttonbn\local\extension\action_url_
* 'metadata' keys)
*/
public function execute(string $action = '', array $data = [], array $metadata = [], ?int $instanceid = null): array {
if ($action == 'create') {
$analyticcburl = get_config('bbbext_flexurl', 'analytics_callback_url');
if ($analyticcburl) {
$metadata['analytics-callback-url'] = $analyticcburl;
}
}
if ($action == 'create' || $action == 'join') {
if (empty($instanceid)) {
if (!(defined('PHPUNIT_TEST') && PHPUNIT_TEST) && !defined('BEHAT_SITE_RUNNING')) {
Expand Down
104 changes: 97 additions & 7 deletions classes/bigbluebuttonbn/mod_form_addons.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace bbbext_flexurl\bigbluebuttonbn;

use bbbext_flexurl\utils;
use stdClass;

/**
Expand All @@ -39,7 +40,7 @@ public function data_postprocessing(\stdClass &$data): void {
}

/**
* Allow module to modify the data at the pre-processing stage.
* Allow module to modify the data at the pre-processing stage.
*
* This method is also called in the bulk activity completion form.
*
Expand All @@ -49,11 +50,19 @@ public function data_preprocessing(?array &$defaultvalues): void {
// This is where we can add the data from the flexurl table to the data provided.
if (!empty($defaultvalues['id'])) {
global $DB;
$flexurlrecord = $DB->get_record(mod_instance_helper::SUBPLUGIN_TABLE, [
$flexurlrecords = $DB->get_records(mod_instance_helper::SUBPLUGIN_TABLE, [
'bigbluebuttonbnid' => $defaultvalues['id'],
]);
if ($flexurlrecord) {
$defaultvalues['additionalparams'] = $flexurlrecord->additionalparams;
if ($flexurlrecords) {
$flexurlrecords = array_values($flexurlrecords);
foreach($flexurlrecords as $flexurlrecord) {
foreach (utils::PARAM_TYPES as $paramtype => $paramtypevalue) {
if (!isset($defaultvalues["flexurl_{$paramtype}"])) {
$defaultvalues["flexurl_{$paramtype}"] = [];
}
$defaultvalues["flexurl_{$paramtype}"][] = $flexurlrecord->{$paramtype} ?? '';
}
}
}
}
}
Expand Down Expand Up @@ -90,16 +99,97 @@ public function completion_rule_enabled(array $data): bool {
* @return void
*/
public function definition_after_data() {
// Nothing for now.
// After data.
$isdeleting = optional_param_array('flexurl_paramdelete', [], PARAM_RAW);
//// Get the index of the delete button that was pressed.
if (!empty($isdeleting)) {
$firstindex = array_key_first($isdeleting);
// Then reassign values from the deleted group to the previous group.
$paramcount = optional_param('flexurl_paramcount', 0, PARAM_INT);
for ($index = $firstindex; $index < $paramcount; $index++) {
$nextindex = $index + 1;
if ($this->mform->elementExists("flexurl_paramgroup[{$nextindex}]")) {
$nextgroupelement = $this->mform->getElement("flexurl_paramgroup[{$nextindex}]");
if (!empty($nextgroupelement)) {
$nextgroupvalue = $nextgroupelement->getValue();
$currentgroupelement = $this->mform->getElement("flexurl_paramgroup[{$index}]");
$value = [
"flexurl_paramname[{$index}]" => $nextgroupvalue["flexurl_paramname[{$nextindex}]"],
"flexurl_paramvalue[{$index}]" => $nextgroupvalue["flexurl_paramvalue[{$nextindex}]"],
];
$currentgroupelement->setValue($value);
}
}
}
$newparamcount = $paramcount - 1;
$this->mform->removeElement("flexurl_paramgroup[{$newparamcount}]");
$this->mform->getElement('flexurl_paramcount')->setValue($newparamcount);
}
}

/**
* Add new form field definition
*/
public function add_fields(): void {
$this->mform->addElement('header', 'flexurl', get_string('pluginname', 'bbbext_flexurl'));
$this->mform->addElement('text', 'additionalparams', get_string('additionalparams', 'bbbext_flexurl'));
$this->mform->setType('additionalparams', PARAM_TEXT);

$paramcount = optional_param('flexurl_paramcount', $this->bigbluebuttonbndata->paramcount ?? 0, PARAM_RAW);
$paramcount += optional_param('flexurl_addparamgroup', 0, PARAM_RAW) ? 1 : 0;
$isdeleting = optional_param_array('flexurl_paramdelete', [], PARAM_RAW);
foreach ($isdeleting as $index => $value) {
// This prevents the last delete button from submitting the form.
$this->mform->registerNoSubmitButton("flexurl_paramdelete[$index]");
}
for ($index = 0; $index < $paramcount; $index++) {
$paramtype = $this->mform->createElement(
'select',
"flexurl_eventtype[$index]",
get_string('param_eventtype', 'bbbext_flexurl'),
utils::get_option_for_eventtype(),
['multiple' => true, 'size' => '2']
);
$paramname = $this->mform->createElement(
'text',
"flexurl_paramname[$index]",
get_string('param_name', 'bbbext_flexurl'),
['size' => '8']
);
$paramvalue = $this->mform->createElement(
'selectgroups',
"flexurl_paramvalue[$index]",
get_string('param_value', 'bbbext_flexurl'),
utils::get_options_for_parameters(),
['size' => '1']
);

$paramdelete = $this->mform->createElement(
'submit',
"flexurl_paramdelete[$index]",
get_string('delete'),
);

$this->mform->addGroup(
[
$paramtype, $paramname, $paramvalue, $paramdelete,
],
"flexurl_paramgroup[$index]",
get_string('paramgroup', 'bbbext_flexurl'),
[' '],
false
);
$this->mform->setType("flexurl_paramname[$index]", PARAM_TEXT);
$this->mform->setType("flexurl_paramvalue[$index]", PARAM_RAW);
$this->mform->setType("flexurl_eventtype[$index]", PARAM_RAW);
$this->mform->setType("flexurl_paramdelete[$index]", PARAM_RAW);
$this->mform->registerNoSubmitButton("flexurl_paramdelete[$index]");
}
// Add a button to add new param groups.
$this->mform->addElement('submit', 'flexurl_addparamgroup', get_string('addparamgroup', 'bbbext_flexurl'));
$this->mform->setType('flexurl_addparamgroup', PARAM_TEXT);
$this->mform->registerNoSubmitButton('flexurl_addparamgroup');
$this->mform->addElement('hidden', 'flexurl_paramcount');
$this->mform->setType('flexurl_paramcount', PARAM_INT);
$this->mform->setConstants(['flexurl_paramcount' => $paramcount]);
}

/**
Expand Down
55 changes: 36 additions & 19 deletions classes/bigbluebuttonbn/mod_instance_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace bbbext_flexurl\bigbluebuttonbn;

use bbbext_flexurl\utils;
use stdClass;

/**
Expand All @@ -35,11 +36,7 @@ class mod_instance_helper extends \mod_bigbluebuttonbn\local\extension\mod_insta
* @param stdClass $bigbluebuttonbn BigBlueButtonBN form data
**/
public function add_instance(stdClass $bigbluebuttonbn) {
global $DB;
$DB->insert_record(self::SUBPLUGIN_TABLE, (object) [
'bigbluebuttonbnid' => $bigbluebuttonbn->id,
'additionalparams' => $bigbluebuttonbn->additionalparams ?? ''
]);
$this->sync_additional_params($bigbluebuttonbn);
}

/**
Expand All @@ -48,20 +45,7 @@ public function add_instance(stdClass $bigbluebuttonbn) {
* @param stdClass $bigbluebuttonbn BigBlueButtonBN form data
**/
public function update_instance(stdClass $bigbluebuttonbn): void {
global $DB;
$record = $DB->get_record(self::SUBPLUGIN_TABLE, [
'bigbluebuttonbnid' => $bigbluebuttonbn->id,
]);
// Just in case the instance was created before the extension was installed.
if (empty($record)) {
$record = new stdClass();
$record->bigbluebuttonbnid = $bigbluebuttonbn->id;
$record->additionalparams = $bigbluebuttonbn->additionalparams ?? '';
$DB->insert_record(self::SUBPLUGIN_TABLE, $record);
} else {
$record->additionalparams = $bigbluebuttonbn->additionalparams ?? '';
$DB->update_record(self::SUBPLUGIN_TABLE, $record);
}
$this->sync_additional_params($bigbluebuttonbn);
}

/**
Expand All @@ -83,4 +67,37 @@ public function delete_instance(int $id): void {
public function get_join_tables(): array {
return [self::SUBPLUGIN_TABLE];
}

/**
* Make sure that the bbbext_flexurl has the right parameters (and not more)
* @param stdClass $bigbluebuttonbn
* @return void
*/
private function sync_additional_params(stdClass $bigbluebuttonbn): void {
global $DB;
// Checks first.
$count = $bigbluebuttonbn->flexurl_paramcount ?? 0;
foreach(utils::PARAM_TYPES as $type =>$paramtype) {
if ($count != count($bigbluebuttonbn->{'flexurl_' . $type})) {
debugging('FlexURL : The number of ' . $type . ' does not match the number of parameters.');
return;
}
if (clean_param_array($bigbluebuttonbn->{'flexurl_' . $type}, $paramtype, true) != $bigbluebuttonbn->{'flexurl_' . $type}) {
debugging('FlexURL : The ' . $type . ' contains invalid value.');
return;
}
}
// Then sync.
// First delete everything related to this module.
$DB->delete_records(self::SUBPLUGIN_TABLE, ['bigbluebuttonbnid' => $bigbluebuttonbn->id]);

for($index = 0; $index < $count; $index++) {
$queryfields = [];
foreach(array_keys(utils::PARAM_TYPES) as $type) {
$queryfields[$type] = $bigbluebuttonbn->{'flexurl_' . $type}[$index];
}
$queryfields['bigbluebuttonbnid'] = $bigbluebuttonbn->id;
$DB->insert_record(self::SUBPLUGIN_TABLE, (object) $queryfields);
}
}
}
133 changes: 133 additions & 0 deletions classes/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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_flexurl;

use core_course\external\course_module_summary_exporter;
use core_course\external\course_summary_exporter;

/**
* Utility class
*
* @package bbbext_flexurl
* @copyright 2023 onwards, Blindside Networks Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Laurent David ([email protected])
*/
class utils {
/**
* Types of additional parameters
*/
public const PARAM_TYPES = [
'eventtype' => PARAM_ALPHA,
'paramname' => PARAM_ALPHA,
'paramvalue' => PARAM_RAW,
];

/**
* Get option group for parameters.
*
* @return array
*/
public static function get_options_for_parameters(): array {
$parametertypes = self::get_parameter_types();
$options = [];
$selectedptypes = explode(',', get_config('bbbext_flexurl', 'available_info'));
foreach ($parametertypes as $key => $value) {
if (in_array($key, $selectedptypes)) {
$options[$value] = self::get_fields_for_parameter($key);
}
}
return $options;
}

/**
* Get parameter types
*
* @return array
* @throws \coding_exception
*/
public static function get_parameter_types(): array {
return [
'activityinfo' => get_string('activity_info', 'bbbext_flexurl'),
'courseinfo' => get_string('course_info', 'bbbext_flexurl'),
'userinfo' => get_string('user_info', 'bbbext_flexurl'),
];
}

public static function get_fields_for_parameter(string $key): array {
if (method_exists(self::class, 'get_' . $key . '_fields')) {
return call_user_func(self::class . '::get_' . $key . '_fields');
} else {
return [];
}
}

/**
* Get user fields prefixed by user.
* @return string[]
*/
public static function get_userinfo_fields() {
$userfields = \core_user\fields::get_identity_fields(\context_system::instance());
$userfields = array_merge($userfields, \core_user\fields::get_name_fields());
sort($userfields);
return array_map(
function($field) {
return "user.{$field}";
},
$userfields
);
}

/**
* Course information
*
* @return string[]
*/
public static function get_courseinfo_fields() {
return array_map(
function($field) {
return "courseinfo.{$field}";
},
array_keys(course_summary_exporter::read_properties_definition())
);
}

/**
* Activity information
*
* @return string[]
*/
public static function get_activityinfo_fields() {
return array_map(
function($field) {
return "activityinfo.{$field}";
},
array_keys(course_module_summary_exporter::read_properties_definition())
);
}

/**
* Type of event : create or join
*
* @return array
*/
public static function get_option_for_eventtype() {
return [
1 => get_string('event_join', 'bbbext_flexurl'),
2 => get_string('event_create', 'bbbext_flexurl'),
];
}
}
Loading

0 comments on commit 187245d

Please sign in to comment.