error_log($error);
         header("HTTP/1.0 503 Service Unavailable. " . $error);
         return;
     }
     break;
 case 'recording_import':
     if ($bbbsession['managerecordings']) {
         $importrecordings = $SESSION->bigbluebuttonbn_importrecordings;
         if (isset($importrecordings[$params['id']])) {
             $importrecordings[$params['id']]['imported'] = true;
             $overrides['meetingid'] = $importrecordings[$params['id']]['meetingID'];
             $meta = '{"recording":' . json_encode($importrecordings[$params['id']]) . '}';
             bigbluebuttonbn_logs($bbbsession, BIGBLUEBUTTONBN_LOG_EVENT_IMPORT, $overrides, $meta);
             // Moodle event logger: Create an event for recording imported
             if (isset($bigbluebuttonbn)) {
                 bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_RECORDING_IMPORTED, $bigbluebuttonbn, $context, $cm);
             }
             $callback_response['status'] = "true";
             $callback_response_data = json_encode($callback_response);
             echo "{$params['callback']}({$callback_response_data});";
         } else {
             $error = "Recording {$params['id']} could not be found. It can not be imported";
             error_log($error);
             header("HTTP/1.0 404 Not found. " . $error);
             return;
         }
     }
     break;
 case 'moodle_notify':
     break;
 case 'moodle_event':
    //
    if (!($bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $a), '*', MUST_EXIST))) {
        print_error("BigBlueButton ID {$a} is incorrect");
    }
    $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
    $cm = get_coursemodule_from_instance('bigbluebuttonbn', $bigbluebuttonbn->id, $course->id, false, MUST_EXIST);
    //User roles
    if ($bigbluebuttonbn->participants == null || $bigbluebuttonbn->participants == "" || $bigbluebuttonbn->participants == "[]") {
        //The room that is being used comes from a previous version
        $moderator = has_capability('mod/bigbluebuttonbn:moderate', $context);
    } else {
        $moderator = bigbluebuttonbn_is_moderator($USER->id, get_user_roles($context, $USER->id, true), $bigbluebuttonbn->participants);
    }
    $administrator = has_capability('moodle/category:manage', $context);
    if ($moderator || $administrator) {
        bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_MEETING_ENDED, $bigbluebuttonbn, $context, $cm);
        echo get_string('index_ending', 'bigbluebuttonbn');
        $meetingID = $bigbluebuttonbn->meetingid . '-' . $course->id . '-' . $bigbluebuttonbn->id;
        $modPW = $bigbluebuttonbn->moderatorpass;
        if ($g != '0') {
            $getArray = bigbluebuttonbn_wrap_xml_load_file(bigbluebuttonbn_getEndMeetingURL($meetingID . '[' . $g . ']', $modPW, $endpoint, $shared_secret));
        } else {
            $getArray = bigbluebuttonbn_wrap_xml_load_file(bigbluebuttonbn_getEndMeetingURL($meetingID, $modPW, $endpoint, $shared_secret));
        }
        redirect('index.php?id=' . $id);
    }
}
foreach ($bigbluebuttonbns as $bigbluebuttonbn) {
    $cm = get_coursemodule_from_id('bigbluebuttonbn', $bigbluebuttonbn->coursemodule, 0, false, MUST_EXIST);
    //User roles
    if ($bigbluebuttonbn->participants == null || $bigbluebuttonbn->participants == "" || $bigbluebuttonbn->participants == "[]") {
    $cm = get_coursemodule_from_instance('bigbluebuttonbn', $bigbluebuttonbn->id, $course->id, false, MUST_EXIST);
} else {
    print_error(get_string('view_error_url_missing_parameters', 'bigbluebuttonbn'));
}
require_login($course, true, $cm);
$version_major = bigbluebuttonbn_get_moodle_version_major();
if ($version_major < '2013111800') {
    //This is valid before v2.6
    $module = $DB->get_record('modules', array('name' => 'bigbluebuttonbn'));
    $module_version = $module->version;
} else {
    //This is valid after v2.6
    $module_version = get_config('mod_bigbluebuttonbn', 'version');
}
$context = bigbluebuttonbn_get_context_module($cm->id);
bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_ACTIVITY_VIEWED, $bigbluebuttonbn, $context, $cm);
////////////////////////////////////////////////
/////  BigBlueButton Session Setup Starts  /////
////////////////////////////////////////////////
// BigBluebuttonBN activity data
$bbbsession['bigbluebuttonbn'] = $bigbluebuttonbn;
// 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);
function bigbluebutton_bbb_view_execute_join($bbbsession, $cm, $context, $bigbluebuttonbn)
{
    //// Update the cache
    $meeting_info = bigbluebuttonbn_bbb_broker_get_meeting_info($bbbsession['meetingid'], $bbbsession['modPW'], true);
    if ($bbbsession['userlimit'] == 0 || intval($meeting_info['participantCount']) < $bbbsession['userlimit']) {
        //// Build the URL
        if ($bbbsession['administrator'] || $bbbsession['moderator']) {
            $password = $bbbsession['modPW'];
        } else {
            $password = $bbbsession['viewerPW'];
        }
        $join_url = bigbluebuttonbn_getJoinURL($bbbsession['meetingid'], $bbbsession['username'], $password, $bbbsession['shared_secret'], $bbbsession['endpoint'], $bbbsession['logoutURL']);
        //// Moodle event logger: Create an event for meeting joined
        bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_MEETING_JOINED, $bigbluebuttonbn, $context, $cm);
        /// Internal logger: Instert a record with the meeting created
        bigbluebuttonbn_logs($bbbsession, BIGBLUEBUTTONBN_LOG_EVENT_JOIN);
        //// Before executing the redirect, increment the number of participants
        bigbluebuttonbn_bbb_broker_participant_joined($bbbsession['meetingid'], $bbbsession['administrator'] || $bbbsession['moderator']);
        //// Execute the redirect
        header('Location: ' . $join_url);
    } else {
        header('Location: ' . $bbbsession['logoutURL']);
    }
}