$meetscoid = $DB->get_record_sql($sql, $params);
// Get the Meeting recording details.
$recording = array();
$aconnect = aconnect_login();
$fldid = aconnect_get_folder($aconnect, 'content');
$usrcanjoin = false;
$usrprincipal = 0;
if (!($usrprincipal = aconnect_user_exists($aconnect, $usrobj))) {
    if (!($usrprincipal = aconnect_create_user($aconnect, $usrobj))) {
        debugging(get_string('erroruser', 'adobeconnect'), DEBUG_DEVELOPER);
    }
}
// Check the user's capabilities and assign them the Adobe Role.
if (has_capability('mod/adobeconnect:meetinghost', $context, $usrobj->id, false)) {
    // Add the host user to the host group if they aren't already.
    $group_principal_id = aconnect_get_host_group($aconnect);
    aconnect_add_user_group($aconnect, $group_principal_id, $usrprincipal);
    aconnect_check_user_perm($aconnect, $usrprincipal, $meetscoid->meetingscoid, ADOBE_HOST, true);
} else {
    if (has_capability('mod/adobeconnect:meetingpresenter', $context, $usrobj->id, false)) {
        aconnect_check_user_perm($aconnect, $usrprincipal, $meetscoid->meetingscoid, ADOBE_PRESENTER, true);
    } else {
        if (has_capability('mod/adobeconnect:meetingparticipant', $context, $usrobj->id, false)) {
            aconnect_check_user_perm($aconnect, $usrprincipal, $meetscoid->meetingscoid, ADOBE_PARTICIPANT, true);
        } else {
            // Check if meeting is public and allow them to join.
            if ($adobeconnect->meetingpublic) {
                // If for a public meeting the user does not not have either of presenter or participant capabilities
                // then give the user the participant role for the meeting.
                aconnect_check_user_perm($aconnect, $usrprincipal, $meetscoid->meetingscoid, ADOBE_PARTICIPANT, true);
            }
function connectsync_add_user_to_host_group($userid)
{
    global $DB;
    static $groupid;
    if ($aconnect = aconnect_login()) {
        if (!isset($groupid)) {
            $groupid = aconnect_get_host_group($aconnect);
        }
        aconnect_add_user_group($aconnect, $groupid, $userid);
        echo "Added user: "******" to Host Group in Adobe Connect\n";
    }
}
 public function validation($data, $files)
 {
     global $DB, $USER, $COURSE;
     $errors = parent::validation($data, $files);
     $usrobj = clone $USER;
     $name = empty($usrobj->idnumber) ? $usrobj->username : $usrobj->idnumber;
     $usrobj->username = set_username($name, $usrobj->email);
     $aconnect = aconnect_login();
     $groupprincipalid = aconnect_get_host_group($aconnect);
     // Check if the user exists and if not create the new user.
     if (!($usrprincipal = aconnect_user_exists($aconnect, $usrobj))) {
         if (!($usrprincipal = aconnect_create_user($aconnect, $usrobj))) {
             debugging("error creating user", DEBUG_DEVELOPER);
         }
     }
     // Add the user to the host group if they aren't already.
     aconnect_add_user_group($aconnect, $groupprincipalid, $usrprincipal);
     // Search for a Meeting with the same starting name.  It will cause a duplicate
     // meeting name (and error) when the user begins to add participants to the meeting.
     $meetfldscoid = aconnect_get_folder($aconnect, 'meetings');
     $filter = array('filter-like-name' => $data['name']);
     $namematches = aconnect_meeting_exists($aconnect, $meetfldscoid, $filter);
     // Search the user's adobe connect folder.
     $usrfldscoid = aconnect_get_user_folder_sco_id($aconnect, $usrobj->username);
     if (!empty($usrfldscoid)) {
         $namematches = $namematches + aconnect_meeting_exists($aconnect, $usrfldscoid, $filter);
     }
     if (empty($namematches)) {
         $namematches = array();
     }
     // Now search for existing meeting room URLs.
     $url = $data['meeturl'] = adobeconnect_clean_meet_url($data['meeturl']);
     // Check to see if there are any trailing slashes or additional parts to the url
     // ex. mymeeting/mysecondmeeting/  Only the 'mymeeting' part is valid.
     if (0 != substr_count($url, '/') and false !== strpos($url, '/', 1)) {
         $errors['meeturl'] = get_string('invalidadobemeeturl', 'adobeconnect');
     }
     $filter = array('filter-like-url-path' => $url);
     $urlmatches = aconnect_meeting_exists($aconnect, $meetfldscoid, $filter);
     // Search the user's adobe connect folder.
     if (!empty($usrfldscoid)) {
         $urlmatches = $urlmatches + aconnect_meeting_exists($aconnect, $usrfldscoid, $filter);
     }
     if (empty($urlmatches)) {
         $urlmatches = array();
     } else {
         // Format url for comparison.
         if (false === strpos($url, '/') or 0 != strpos($url, '/')) {
             $url = '/' . $url;
         }
     }
     // Check URL for correct length and format.
     if (!empty($data['meeturl'])) {
         if (strlen($data['meeturl']) > 60) {
             $errors['meeturl'] = get_string('longurl', 'adobeconnect');
         } else {
             if (!preg_match('/^[a-z][a-z\\-]*/i', $data['meeturl'])) {
                 $errors['meeturl'] = get_string('invalidurl', 'adobeconnect');
             }
         }
     }
     // Check for available groups if groupmode is selected.
     if ($data['groupmode'] > 0) {
         $crsgroups = groups_get_all_groups($COURSE->id);
         if (empty($crsgroups)) {
             $errors['groupmode'] = get_string('missingexpectedgroups', 'adobeconnect');
         }
     }
     // Adding activity.
     if (empty($data['update'])) {
         if ($data['starttime'] == $data['endtime']) {
             $errors['starttime'] = get_string('samemeettime', 'adobeconnect');
             $errors['endtime'] = get_string('samemeettime', 'adobeconnect');
         } else {
             if ($data['endtime'] < $data['starttime']) {
                 $errors['starttime'] = get_string('greaterstarttime', 'adobeconnect');
             }
         }
         // Check for local activities with the same name.
         $params = array('name' => $data['name']);
         if ($DB->record_exists('adobeconnect', $params)) {
             $errors['name'] = get_string('duplicatemeetingname', 'adobeconnect');
             return $errors;
         }
         // Check Adobe connect server for duplicated names.
         foreach ($namematches as $match) {
             if (0 == substr_compare($match->name, $data['name'] . '_', 0, strlen($data['name'] . '_'), false)) {
                 $errors['name'] = get_string('duplicatemeetingname', 'adobeconnect');
             }
         }
         foreach ($urlmatches as $match) {
             $matchurl = rtrim($match->url, '/');
             if (0 == substr_compare($matchurl, $url . '_', 0, strlen($url . '_'), false)) {
                 $errors['meeturl'] = get_string('duplicateurl', 'adobeconnect');
             }
         }
     } else {
         // Updating activity
         // Look for existing meeting names, excluding this activity's group meeting(s).
         $grpmeetings = $DB->get_records('adobeconnect_meeting_groups', array('instanceid' => $data['instance']), null, 'meetingscoid, groupid');
         if (empty($grpmeetings)) {
             $grpmeetings = array();
         }
         foreach ($namematches as $match) {
             if (!array_key_exists($match->scoid, $grpmeetings)) {
                 if (0 == substr_compare($match->name, $data['name'] . '_', 0, strlen($data['name'] . '_'), false)) {
                     $errors['name'] = get_string('duplicatemeetingname', 'adobeconnect');
                 }
             }
         }
         foreach ($urlmatches as $match) {
             if (!array_key_exists($match->scoid, $grpmeetings)) {
                 if (0 == substr_compare($match->url, $url . '_', 0, strlen($url . '_'), false)) {
                     $errors['meeturl'] = get_string('duplicateurl', 'adobeconnect');
                 }
             }
         }
         // Validate start and end times.
         if ($data['starttime'] == $data['endtime']) {
             $errors['starttime'] = get_string('samemeettime', 'adobeconnect');
             $errors['endtime'] = get_string('samemeettime', 'adobeconnect');
         } else {
             if ($data['endtime'] < $data['starttime']) {
                 $errors['starttime'] = get_string('greaterstarttime', 'adobeconnect');
             }
         }
     }
     aconnect_logout($aconnect);
     if ($aconnect->timeout()) {
         $errors = array('name' => get_string('errortimeout', 'adobeconnect'));
     }
     return $errors;
 }