function bigbluebuttonbn_server_offers($capability_name)
{
    global $CFG;
    $capability_offered = null;
    $endpoint = bigbluebuttonbn_get_cfg_server_url();
    $shared_secret = bigbluebuttonbn_get_cfg_shared_secret();
    //Validates if the server may have extended capabilities
    $parse = parse_url($endpoint);
    $host = $parse['host'];
    $host_ends = explode(".", $host);
    $host_ends_length = count($host_ends);
    if ($host_ends_length > 0 && $host_ends[$host_ends_length - 1] == 'com' && $host_ends[$host_ends_length - 2] == 'blindsidenetworks') {
        //Validate the capabilities offered
        $capabilities = bigbluebuttonbn_getCapabilitiesArray($endpoint, $shared_secret);
        if ($capabilities) {
            foreach ($capabilities as $capability) {
                if ($capability["name"] == $capability_name) {
                    $capability_offered = $capability;
                }
            }
        }
    }
    return $capability_offered;
}
// User data
$bbbsession['username'] = fullname($USER);
$bbbsession['userID'] = $USER->id;
$bbbsession['roles'] = get_user_roles($context, $USER->id, true);
// User roles
if ($bigbluebuttonbn->participants == null || $bigbluebuttonbn->participants == "" || $bigbluebuttonbn->participants == "[]") {
    //The room that is being used comes from a previous version
    $bbbsession['moderator'] = has_capability('mod/bigbluebuttonbn:moderate', $context);
} else {
    $bbbsession['moderator'] = bigbluebuttonbn_is_moderator($bbbsession['userID'], $bbbsession['roles'], $bigbluebuttonbn->participants);
}
$bbbsession['administrator'] = has_capability('moodle/category:manage', $context);
$bbbsession['managerecordings'] = $bbbsession['administrator'] || has_capability('mod/bigbluebuttonbn:managerecordings', $context);
// BigBlueButton server data
$bbbsession['endpoint'] = bigbluebuttonbn_get_cfg_server_url();
$bbbsession['shared_secret'] = bigbluebuttonbn_get_cfg_shared_secret();
// Server data
$bbbsession['modPW'] = $bigbluebuttonbn->moderatorpass;
$bbbsession['viewerPW'] = $bigbluebuttonbn->viewerpass;
// Database info related to the activity
$bbbsession['meetingdescription'] = $bigbluebuttonbn->intro;
$bbbsession['welcome'] = $bigbluebuttonbn->welcome;
if (!isset($bbbsession['welcome']) || $bbbsession['welcome'] == '') {
    $bbbsession['welcome'] = get_string('mod_form_field_welcome_default', 'bigbluebuttonbn');
}
$bbbsession['userlimit'] = bigbluebuttonbn_get_cfg_userlimit_editable() ? intval($bigbluebuttonbn->userlimit) : intval(bigbluebuttonbn_get_cfg_userlimit_default());
$bbbsession['voicebridge'] = $bigbluebuttonbn->voicebridge > 0 ? 70000 + $bigbluebuttonbn->voicebridge : $bigbluebuttonbn->voicebridge;
$bbbsession['wait'] = $bigbluebuttonbn->wait;
$bbbsession['record'] = $bigbluebuttonbn->record;
if ($bigbluebuttonbn->record) {
    $bbbsession['welcome'] .= '<br><br>' . get_string('bbbrecordwarning', 'bigbluebuttonbn');
 * @copyright 2015 Blindside Networks Inc.
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
 */
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
require_once dirname(__FILE__) . '/locallib.php';
global $PAGE, $USER, $CFG, $SESSION, $DB;
$params['action'] = optional_param('action', '', PARAM_TEXT);
$params['callback'] = optional_param('callback', '', PARAM_TEXT);
$params['id'] = optional_param('id', '', PARAM_TEXT);
//recordID, the BBB recordID
$params['idx'] = optional_param('idx', '', PARAM_TEXT);
//meetingID, the BBB meetingID
$params['bigbluebuttonbn'] = optional_param('bigbluebuttonbn', 0, PARAM_INT);
$params['signed_parameters'] = optional_param('signed_parameters', '', PARAM_TEXT);
$endpoint = bigbluebuttonbn_get_cfg_server_url();
$shared_secret = bigbluebuttonbn_get_cfg_shared_secret();
$error = '';
if (empty($params['action'])) {
    $error = bigbluebuttonbn_bbb_broker_add_error($error, "Parameter [action] was not included");
} else {
    $error = bigbluebuttonbn_bbb_broker_validate_parameters($params);
    if (empty($error) && $params['action'] != "recording_ready") {
        if ($params['bigbluebuttonbn'] != 0) {
            $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $params['bigbluebuttonbn']), '*', MUST_EXIST);
            $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
            $cm = get_coursemodule_from_instance('bigbluebuttonbn', $bigbluebuttonbn->id, $course->id, false, MUST_EXIST);
            $context = bigbluebuttonbn_get_context_module($cm->id);
        }
        if (isset($SESSION->bigbluebuttonbn_bbbsession) && !is_null($SESSION->bigbluebuttonbn_bbbsession)) {
            $bbbsession = $SESSION->bigbluebuttonbn_bbbsession;
        } else {
/**
 * Runs any processes that must be run
 * after a bigbluebuttonbn insert/update
 *
 * @global object
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
 * @return void
 **/
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn)
{
    global $DB, $CFG, $USER;
    // Now that an id was assigned, generate and set the meetingid property based on
    // [Moodle Instance + Activity ID + BBB Secret] (but only for new activities)
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
        $bigbluebuttonbn_meetingid = sha1($CFG->wwwroot . $bigbluebuttonbn->id . bigbluebuttonbn_get_cfg_shared_secret());
        $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn_meetingid, array('id' => $bigbluebuttonbn->id));
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
    } else {
        $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
    }
    // Add evento to the calendar when if openingtime is set
    if (isset($bigbluebuttonbn->openingtime) && $bigbluebuttonbn->openingtime) {
        $event = new stdClass();
        $event->name = $bigbluebuttonbn->name;
        $event->courseid = $bigbluebuttonbn->course;
        $event->groupid = 0;
        $event->userid = 0;
        $event->modulename = 'bigbluebuttonbn';
        $event->instance = $bigbluebuttonbn->id;
        $event->timestart = $bigbluebuttonbn->openingtime;
        if ($bigbluebuttonbn->closingtime) {
            $event->durationtime = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
        } else {
            $event->durationtime = 0;
        }
        if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
            $calendarevent = calendar_event::load($event->id);
            $calendarevent->update($event);
        } else {
            calendar_event::create($event);
        }
    } else {
        $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id));
    }
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
        // Prepare message
        $msg = new stdClass();
        /// Build the message_body
        $msg->action = $action;
        $msg->activity_type = "";
        $msg->activity_title = $bigbluebuttonbn->name;
        /// Add the meeting details to the message_body
        $msg->action = ucfirst($action);
        $msg->activity_description = "";
        if (!empty($bigbluebuttonbn->intro)) {
            $msg->activity_description = trim($bigbluebuttonbn->intro);
        }
        $msg->activity_openingtime = "";
        if ($bigbluebuttonbn->openingtime) {
            $date = new stdClass();
            $date->day = calendar_day_representation($bigbluebuttonbn->openingtime);
            $date->time = calendar_time_representation($bigbluebuttonbn->openingtime);
            $msg->activity_openingtime = get_string('email_date', 'bigbluebuttonbn', $date);
        }
        $msg->activity_closingtime = "";
        if ($bigbluebuttonbn->closingtime) {
            $date = new stdClass();
            $date->day = calendar_day_representation($bigbluebuttonbn->closingtime);
            $date->time = calendar_time_representation($bigbluebuttonbn->closingtime);
            $msg->activity_closingtime = get_string('email_date', 'bigbluebuttonbn', $date);
        }
        $msg->activity_owner = fullname($USER);
        $message_text = get_string('email_body_notification', 'bigbluebuttonbn', $msg);
        // Send notification to all users enrolled
        bigbluebuttonbn_send_notification($USER, $bigbluebuttonbn, $message_text);
    }
}