function bigbluebuttonbn_event_log_standard($event_type, $bigbluebuttonbn, $context, $cm)
{
    $context = context_module::instance($cm->id);
    $event_properties = array('context' => $context, 'objectid' => $bigbluebuttonbn->id);
    switch ($event_type) {
        case BIGBLUEBUTTON_EVENT_MEETING_JOINED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_meeting_joined::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_MEETING_CREATED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_meeting_created::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_MEETING_ENDED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_meeting_ended::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_MEETING_LEFT:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_meeting_left::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_RECORDING_PUBLISHED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_recording_published::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_RECORDING_UNPUBLISHED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_recording_unpublished::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_RECORDING_DELETED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_recording_deleted::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_ACTIVITY_VIEWED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_activity_viewed::create($event_properties);
            break;
        case BIGBLUEBUTTON_EVENT_ACTIVITY_MANAGEMENT_VIEWED:
            $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_activity_management_viewed::create($event_properties);
            break;
    }
    $event->trigger();
}
function bigbluebuttonbn_view_joining($bbbsession, $context, $bigbluebuttonbn)
{
    global $CFG, $DB;
    $joining = false;
    // If user is administrator, moderator or if is viewer and no waiting is required
    if ($bbbsession['flag']['administrator'] || $bbbsession['flag']['moderator'] || !$bbbsession['flag']['wait']) {
        //
        // Join directly
        //
        $metadata = array("meta_origin" => $bbbsession['origin'], "meta_originVersion" => $bbbsession['originVersion'], "meta_originServerName" => $bbbsession['originServerName'], "meta_originServerCommonName" => $bbbsession['originServerCommonName'], "meta_originTag" => $bbbsession['originTag'], "meta_context" => $bbbsession['context'], "meta_contextActivity" => $bbbsession['contextActivity'], "meta_contextActivityDescription" => $bbbsession['contextActivityDescription'], "meta_recording" => $bbbsession['textflag']['record']);
        $response = bigbluebuttonbn_getCreateMeetingArray($bbbsession['meetingname'], $bbbsession['meetingid'], $bbbsession['welcome'], $bbbsession['modPW'], $bbbsession['viewerPW'], $bbbsession['salt'], $bbbsession['url'], $bbbsession['logoutURL'], $bbbsession['textflag']['record'], $bbbsession['timeduration'], $bbbsession['voicebridge'], $metadata);
        if (!$response) {
            // If the server is unreachable, then prompts the user of the necessary action
            if ($bbbsession['flag']['administrator']) {
                print_error('view_error_unable_join', 'bigbluebuttonbn', $CFG->wwwroot . '/admin/settings.php?section=modsettingbigbluebuttonbn');
            } else {
                if ($bbbsession['flag']['moderator']) {
                    print_error('view_error_unable_join_teacher', 'bigbluebuttonbn', $CFG->wwwroot . '/admin/settings.php?section=modsettingbigbluebuttonbn');
                } else {
                    print_error('view_error_unable_join_student', 'bigbluebuttonbn', $CFG->wwwroot . '/admin/settings.php?section=modsettingbigbluebuttonbn');
                }
            }
        } else {
            if ($response['returncode'] == "FAILED") {
                // The meeting was not created
                $error_key = bigbluebuttonbn_get_error_key($response['messageKey'], 'view_error_create');
                if (!$error_key) {
                    print_error($response['message'], 'bigbluebuttonbn');
                } else {
                    print_error($error_key, 'bigbluebuttonbn');
                }
            } else {
                if ($response['hasBeenForciblyEnded'] == "true") {
                    print_error(get_string('index_error_forciblyended', 'bigbluebuttonbn'));
                } else {
                    ///////////////Everything is ok /////////////////////
                    /// Moodle event logger: Create an event for meeting created
                    if ($CFG->version < '2014051200') {
                        //This is valid before v2.7
                        add_to_log($bbbsession['courseid'], 'bigbluebuttonbn', 'meeting created', '', $bigbluebuttonbn->name, $bbbsession['cm']->id);
                    } else {
                        //This is valid after v2.7
                        $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_meeting_created::create(array('context' => $context, 'objectid' => $bigbluebuttonbn->id));
                        $event->trigger();
                    }
                    /// Internal logger: Instert a record with the meeting created
                    bigbluebuttonbn_log($bbbsession, 'Create');
                    if (groups_get_activity_groupmode($bbbsession['cm']) > 0 && count(groups_get_activity_allowed_groups($bbbsession['cm'])) > 1) {
                        print "<br>" . get_string('view_groups_selection', 'bigbluebuttonbn') . "&nbsp;&nbsp;<input type='button' onClick='M.mod_bigbluebuttonbn.joinURL()' value='" . get_string('view_groups_selection_join', 'bigbluebuttonbn') . "'>";
                    } else {
                        $joining = true;
                        if ($bbbsession['flag']['administrator'] || $bbbsession['flag']['moderator']) {
                            print "<br />" . get_string('view_login_moderator', 'bigbluebuttonbn') . "<br /><br />";
                        } else {
                            print "<br />" . get_string('view_login_viewer', 'bigbluebuttonbn') . "<br /><br />";
                        }
                        print "<center><img src='pix/loading.gif' /></center>";
                    }
                    /// Moodle event logger: Create an event for meeting joined
                    if ($CFG->version < '2014051200') {
                        //This is valid before v2.7
                        add_to_log($bbbsession['courseid'], 'bigbluebuttonbn', 'meeting joined', '', $bigbluebuttonbn->name, $bbbsession['cm']->id);
                    } else {
                        //This is valid after v2.7
                        $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_meeting_joined::create(array('context' => $context, 'objectid' => $bigbluebuttonbn->id));
                        $event->trigger();
                    }
                }
            }
        }
    } else {
        //
        // "Viewer" && Waiting for moderator is required;
        //
        $joining = true;
        print "<div align='center'>";
        if (bigbluebuttonbn_wrap_simplexml_load_file(bigbluebuttonbn_getIsMeetingRunningURL($bbbsession['meetingid'], $bbbsession['url'], $bbbsession['salt'])) == "true") {
            /// Since the meeting is already running, we just join the session
            print "<br />" . get_string('view_login_viewer', 'bigbluebuttonbn') . "<br /><br />";
            print "<center><img src='pix/loading.gif' /></center>";
            /// Moodle event logger: Create an event for meeting joined
            if ($CFG->version < '2014051200') {
                //This is valid before v2.7
                add_to_log($bbbsession['courseid'], 'bigbluebuttonbn', 'meeting joined', '', $bigbluebuttonbn->name, $bbbsession['cm']->id);
            } else {
                //This is valid after v2.7
                $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_meeting_joined::create(array('context' => $context, 'objectid' => $bigbluebuttonbn->id));
                $event->trigger();
            }
        } else {
            /// Since the meeting is not running, the spining wheel is shown
            print "<br />" . get_string('view_wait', 'bigbluebuttonbn') . "<br /><br />";
            print '<center><img src="pix/polling.gif"></center>';
        }
        print "</div>";
    }
    return $joining;
}