/**
 * Remove a list of users from a given meeting.  Also handles removing the
 * calendar event and any assosciated reminders for the user.  This action
 * can only occur with a private meeting.
 *
 * If $moderators is not set to true, the users will be removed from the
 *
 * @param object  $meeting    The Blackboard Collaborate activity database record.
 * @param array   $userids    A list of Moodle user IDs to remove from the meeting.
 * @param int     $groupid    The group ID this is specifically for.
 * @param boolean $moderators True if the users being removed are moderators.
 * @param boolean $force      Whether to force an override of the behviour for not deleting the meeting creator.
 * @return boolean True on success, False otherwise.
 */
function elluminate_del_users($meeting, $userids, $groupid = 0, $moderators = false)
{
    global $DB;
    $elluminate = $DB->get_record('elluminate', array('id' => $meeting->id));
    if ($meeting->groupmode == 0) {
        $cm = get_coursemodule_from_instance('elluminate', $meeting->id, $meeting->course);
    } else {
        if ($meeting->groupparentid != 0) {
            $cm = get_coursemodule_from_instance('elluminate', $meeting->groupparentid, $meeting->course);
        } else {
            $cm = get_coursemodule_from_instance('elluminate', $meeting->id, $meeting->course);
        }
    }
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    $timenow = time();
    $muserids = array();
    /// Remove each user from the meeting on the Blackboard Collaborate server.
    foreach ($userids as $userid) {
        if ($userid != $meeting->creator) {
            $muserids[] = $userid;
            if (!elluminate_delete_participant($elluminate->id, $userid)) {
                return false;
            }
        }
    }
    if (!empty($elluminate->meetingid)) {
        $args[0]['name'] = 'meetingId';
        $args[0]['value'] = $elluminate->meetingid;
        $args[0]['type'] = 'xsd:int';
        $args[1]['name'] = 'chairList';
        $args[1]['value'] = $elluminate->chairlist;
        $args[1]['type'] = 'xsd:string';
        $args[2]['name'] = 'nonChairList';
        $args[2]['value'] = $elluminate->nonchairlist;
        $args[2]['type'] = 'xsd:string';
        $result = elluminate_send_command('setMeeting', $args);
    }
    if (empty($muserids)) {
        return true;
    }
    /// Delete user events.
    if (count($muserids) > 1) {
        $select = "modulename = 'elluminate' AND instance = " . $meeting->id . " AND userid IN (" . implode(',', $muserids) . ");";
        return $DB->delete_records_select('event', $select);
    } else {
        return $DB->delete_records('event', array('modulename' => 'elluminate', 'instance' => $meeting->id, 'userid' => $userid));
    }
}
/**
 * Remove a list of users from a given meeting.  Also handles removing the
 * calendar event and any assosciated reminders for the user.  This action
 * can only occur with a private meeting.
 *
 * If $moderators is not set to true, the users will be removed from the
 *
 * @param object  $meeting    The Elluminate Live! activity database record.
 * @param array   $userids    A list of Moodle user IDs to remove from the meeting.
 * @param int     $groupid    The group ID this is specifically for.
 * @param boolean $moderators True if the users being removed are moderators.
 * @param boolean $force      Whether to force an override of the behviour for not deleting the meeting creator.
 * @return boolean True on success, False otherwise.
 */
function elluminate_del_users($meeting, $userids, $groupid = 0, $moderators = false)
{
    $elluminate = get_record('elluminate', 'id', $meeting->id);
    if ($meeting->groupmode == 0) {
        $cm = get_coursemodule_from_instance('elluminate', $meeting->id, $meeting->course);
    } else {
        if ($meeting->groupparentid != 0) {
            $cm = get_coursemodule_from_instance('elluminate', $meeting->groupparentid, $meeting->course);
        } else {
            $cm = get_coursemodule_from_instance('elluminate', $meeting->id, $meeting->course);
        }
    }
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    //$session = get_record('elluminate_session', 'elluminate', $meeting->id);
    $timenow = time();
    $muserids = array();
    if ($moderators) {
        $role = get_record('role', 'shortname', 'elluminatemoderator');
    } else {
        $role = get_record('role', 'shortname', 'elluminateparticipant');
    }
    /// Remove each user from the meeting on the Elluminate Live! server.
    foreach ($userids as $userid) {
        if ($userid != $meeting->creator) {
            if (!role_unassign($role->id, $userid, $groupid, $context->id)) {
                continue;
            }
            $muserids[] = $userid;
            if (!elluminate_delete_participant($elluminate->id, $userid)) {
                return false;
            }
        }
    }
    if (!empty($elluminate->meetingid)) {
        $args[0]['name'] = 'meetingId';
        $args[0]['value'] = $elluminate->meetingid;
        $args[0]['type'] = 'xsd:int';
        $args[1]['name'] = 'chairList';
        $args[1]['value'] = $elluminate->chairlist;
        $args[1]['type'] = 'xsd:string';
        $args[2]['name'] = 'nonChairList';
        $args[2]['value'] = $elluminate->nonchairlist;
        $args[2]['type'] = 'xsd:string';
        $result = elluminate_send_command('setMeeting', $args);
    }
    if (empty($muserids)) {
        return true;
    }
    /// Delete user events.
    if (count($muserids) > 1) {
        $select = "modulename = 'elluminate' AND instance = {$meeting->id} AND " . "userid IN (" . implode(', ', $muserids) . ")";
        return delete_records_select('event', $select);
    } else {
        return delete_records('event', 'modulename', 'elluminate', 'instance', $meeting->id, 'userid', $userid);
    }
}