}
}
$aconnect = aconnect_login();
// Get the Meeting details.
$cond = array('instanceid' => $adobeconnect->id, 'groupid' => $groupid);
$scoid = $DB->get_field('adobeconnect_meeting_groups', 'meetingscoid', $cond);
$meetfldscoid = aconnect_get_folder($aconnect, 'meetings');
$filter = array('filter-sco-id' => $scoid);
if ($meeting = aconnect_meeting_exists($aconnect, $meetfldscoid, $filter)) {
    $meeting = current($meeting);
} else {
    /* First check if the module instance has a user associated with it
       if so, then check the user's adobe connect folder for existince of the meeting */
    if (!empty($adobeconnect->userid)) {
        $username = get_connect_username($adobeconnect->userid);
        $meetfldscoid = aconnect_get_user_folder_sco_id($aconnect, $username);
        $meeting = aconnect_meeting_exists($aconnect, $meetfldscoid, $filter);
        if (!empty($meeting)) {
            $meeting = current($meeting);
        }
    }
    // If meeting does not exist then display an error message.
    if (empty($meeting)) {
        $message = get_string('nomeeting', 'adobeconnect');
        echo $OUTPUT->notification($message);
        aconnect_logout($aconnect);
        die;
    }
}
aconnect_logout($aconnect);
$sesskey = !empty($usrobj->sesskey) ? $usrobj->sesskey : '';
/**
 * Given an object containing all the necessary data,
 * (defined by the form in mod_form.php) this function
 * will update an existing instance with new data.
 *
 * @param object $adobeconnect An object from the form in mod_form.php
 * @return boolean Success/Fail
 */
function adobeconnect_update_instance($adobeconnect)
{
    global $DB;
    $adobeconnect->timemodified = time();
    $adobeconnect->id = $adobeconnect->instance;
    $aconnect = aconnect_login();
    $meetfldscoid = aconnect_get_folder($aconnect, 'meetings');
    // Look for meetings whose names are similar.
    $filter = array('filter-like-name' => $adobeconnect->name);
    $namematches = aconnect_meeting_exists($aconnect, $meetfldscoid, $filter);
    if (empty($namematches)) {
        $namematches = array();
    }
    // Find meeting URLs that are similar.
    $url = $adobeconnect->meeturl;
    $filter = array('filter-like-url-path' => $url);
    $urlmatches = aconnect_meeting_exists($aconnect, $meetfldscoid, $filter);
    if (empty($urlmatches)) {
        $urlmatches = array();
    } else {
        // Format url for comparison.
        if (false === strpos($url, '/') or 0 != strpos($url, '/')) {
            $url = '/' . $url;
        }
    }
    $url = adobeconnect_clean_meet_url($url);
    // Get all instances of the activity meetings.
    $param = array('instanceid' => $adobeconnect->instance);
    $grpmeetings = $DB->get_records('adobeconnect_meeting_groups', $param);
    if (empty($grpmeetings)) {
        $grpmeetings = array();
    }
    // If no errors then check to see if the updated name and URL are actually different.
    // If true, then update the meeting names and URLs now.
    $namechange = true;
    $urlchange = true;
    $timechange = true;
    // Look for meeting name change.
    foreach ($namematches as $match) {
        if (array_key_exists($match->scoid, $grpmeetings)) {
            if (0 == substr_compare($match->name, $adobeconnect->name . '_', 0, strlen($adobeconnect->name . '_'), false)) {
                // Break out of loop and change all referenced meetings.
                $namechange = false;
                break;
            } else {
                if (date('c', $adobeconnect->starttime) == $match->starttime) {
                    $timechange = false;
                    break;
                } else {
                    if (date('c', $adobeconnect->endtime) == $match->endtime) {
                        $timechange = false;
                        break;
                    }
                }
            }
        }
    }
    // Look for URL change.
    foreach ($urlmatches as $match) {
        if (array_key_exists($match->scoid, $grpmeetings)) {
            if (0 == substr_compare($match->url, $url . '_', 0, strlen($url . '_'), false)) {
                // Break out of loop and change all referenced meetings.
                $urlchange = false;
                break;
            } else {
                if (date('c', $adobeconnect->starttime) == $match->starttime) {
                    $timechange = false;
                    break;
                } else {
                    if (date('c', $adobeconnect->endtime) == $match->endtime) {
                        $timechange = false;
                        break;
                    }
                }
            }
        }
    }
    if ($timechange or $urlchange or $namechange) {
        $meetingobj = new stdClass();
        foreach ($grpmeetings as $grpmeeting) {
            $group = '';
            if ($adobeconnect->groupmode) {
                $group = groups_get_group($grpmeeting->groupid);
                $group = '_' . $group->name;
            }
            $meetingobj->scoid = $grpmeeting->meetingscoid;
            $meetingobj->name = $adobeconnect->name . $group;
            $meetingobj->starttime = date('c', $adobeconnect->starttime);
            $meetingobj->endtime = date('c', $adobeconnect->endtime);
            /* If the userid is not empty then set the meeting folder sco id to
               the user's connect folder.  If this line of code is not executed
               then user's meetings that were previously in the user's connect folder
               would be moved into the shared folder */
            if (!empty($adobeconnect->userid)) {
                $username = get_connect_username($adobeconnect->userid);
                $userfolder = aconnect_get_user_folder_sco_id($aconnect, $username);
                if (!empty($userfolder)) {
                    $meetfldscoid = $userfolder;
                }
            }
            // Update each meeting instance.
            if (!aconnect_update_meeting($aconnect, $meetingobj, $meetfldscoid)) {
                debugging('error updating meeting', DEBUG_DEVELOPER);
            }
            if (empty($adobeconnect->meetingpublic)) {
                aconnect_update_meeting_perm($aconnect, $grpmeeting->meetingscoid, ADOBE_MEETPERM_PRIVATE);
            } else {
                aconnect_update_meeting_perm($aconnect, $grpmeeting->meetingscoid, ADOBE_MEETPERM_PUBLIC);
            }
            // Update calendar event.
            $param = array('courseid' => $adobeconnect->course, 'instance' => $adobeconnect->id, 'groupid' => $grpmeeting->groupid, 'modulename' => 'adobeconnect');
            $eventid = $DB->get_field('event', 'id', $param);
            if (!empty($eventid)) {
                $event = new stdClass();
                $event->id = $eventid;
                $event->name = $meetingobj->name;
                $event->description = format_module_intro('adobeconnect', $adobeconnect, $adobeconnect->coursemodule);
                $event->courseid = $adobeconnect->course;
                $event->groupid = $grpmeeting->groupid;
                $event->userid = 0;
                $event->instance = $adobeconnect->id;
                $event->eventtype = 0 == $grpmeeting->groupid ? 'course' : 'group';
                $event->timestart = $adobeconnect->starttime;
                $event->timeduration = $adobeconnect->endtime - $adobeconnect->starttime;
                $event->visible = 1;
                $event->modulename = 'adobeconnect';
                $calendarevent = calendar_event::load($eventid);
                $calendarevent->update($event);
            }
        }
    }
    aconnect_logout($aconnect);
    return $DB->update_record('adobeconnect', $adobeconnect);
}
 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;
 }