<script type="text/javascript">
//<![CDATA[
<?php 
    $roleoptions = array(0 => get_string('all'));
    $roles = get_all_roles();
    foreach ($roles as $rolex) {
        $roleoptions[$rolex->id] = $rolex->name;
    }

    foreach ($listmembers as $groupid => $listmember) {
        echo "group$groupid = new Object();\n";
        $useridstring = "group$groupid.userid = new Array(";
        $usernamestring = "group$groupid.username = new Array(";
        $max = count($listmember);
        $count = 0;
        foreach ($listmember as $userid => $username) {
            $count++;
            $useridstring .= "\"$userid\"";
            $usernamestring .= '"'.addslashes($username).'"';
            if ($count < $max) {
                $useridstring .= ', ';
                $usernamestring .= ', ';
            }
        }
        $useridstring .= ");\n";
        $usernamestring .= ");\n";

        echo $useridstring;
        echo $usernamestring;
    }
?>

function updateGroup() {
    document.getElementById("form1").groupid.value = document.getElementById("form2").groups.value;
    document.getElementById("form3").groupid.value = document.getElementById("form2").groups.value;
}


function updateMembers(selectgroup) {
    eval('group=group'+selectgroup.value); 

    username = group.username;
    userid = group.userid;

    document.getElementById("form3")['members[]'].length = username.length;

    for (i=0;i<username.length;i++) {
        document.getElementById("form3")['members[]'].options[i].value = userid[i];
        document.getElementById("form3")['members[]'].options[i].text  = username[i];
    }

    updateGroup();
}

function userWindow(selectuser) {
    num = 0;
    for (var i=0; i<selectuser.options.length; i++) {
        if (selectuser.options[i].selected) {
            num++;
            user = selectuser.options[i].value;
            openpopup('/user/view.php?id='+user+'&course=<?php echo $courseid ?>','userinfo'+num,'','');
        }
    }
    return false;
}

function groupWindow(selectgroup) {
    num = 0;
    for (var i=0; i<selectgroup.options.length; i++) {
        if (selectgroup.options[i].selected) {
            num++;
            group = selectgroup.options[i].value;
            openpopup('/course/group.php?id=<?php echo $courseid ?>&group='+group,'groupinfo'+num,'menubar=0,directory=0,location=0,scrollbars,resizable,width=600,height=480','');
        }
    }
    return false;
}


//]]>
</script>

        <form id="rolesform1" action="groups.php" method="get">
        <input type="hidden" name="id" value="<?php echo $courseid ?>" />
        <div align="center"><?php echo get_string('currentrole', 'role') ?>: 
            <?php choose_from_menu ($roleoptions, 'roleid', $roleid, '', 'document.getElementById(\'rolesform1\').submit()') ?>
        </div></form>

  <table cellspacing="0" cellpadding="10" align="center" class="generaltable generalbox">
    <tr align="center" valign="top">
      <td class="generalboxcontent">
        <label for="nonmembers"><?php p($strmemberincourse) ?></label>
        <form id="form1" method="post" action="groups.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="groupid" value="<?php p($selectedgroup) ?>" />
          <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>" />
          <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
          
          <select name="nonmembers[]" size="15" id="nonmembers" multiple="multiple">
            <?php 
                if (!empty($nonmembers)) {
                    foreach ($nonmembers as $id => $nonmembername) {
                        if (!is_array($ugroups = user_group($course->id, $id))) {
                            $numgroups = 0;
                        }
                        else {
                            $numgroups = count($ugroups);
                        }
                        echo "<option value=\"$id\" title=\"$nonmembername is in $numgroups groups\">$nonmembername ($numgroups)</option>\n";
                    }
                }
            ?>
          </select>
          <br />
            <input type="submit" name="nonmembersadd" value="<?php p($strgroupaddusers) ?> -&gt;" 
                   onclick="updateGroup()" />
          <br />
          <!--
            <input type="submit" name="nonmembersrandom" value="<?php p($strgrouprandomassign) ?> -&gt;" />
          <br /> -->
            <input type="submit" name="nonmembersinfo" value="<?php p($strgroupinfopeople) ?>" 
                   onclick="return userWindow(document.getElementById('form1')['nonmembers[]']);" />
        </form>
      </td>
      <td class="generalboxcontent">
        <label for="groups"><?php p($strgroups) ?></label>
        <form id="form2" method="post" action="groups.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>" />
          <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
          <select name="groups" size="15" id="groups" onchange="updateMembers(this)">
            <?php 
                if (!empty($listgroups)) {
                    foreach ($listgroups as $id => $listgroup) {
                        $selected = '';
                        if ($id == $selectedgroup) {
                            $selected = 'selected="selected"';
                        }
                        echo "<option $selected value=\"$id\">$listgroup</option>";
                    }
                }
            ?>
          </select>
        <br />
          <input type="submit" name="groupsinfo" value="<?php p($strgroupinfoedit) ?>" 
                 onclick="return groupWindow(document.getElementById('form2').groups);" />
        <br />
          <input type="submit" name="groupsremove" value="<?php p($strgroupremove) ?>" />
        <br />
          <label for="groupadd" class="accesshide"><?php p($strgroupadd) ?></label>
          <input name="newgroupname" id="groupadd" type="text" size="10" />
          <input type="submit" name="groupsadd" value="<?php p($strgroupadd) ?>" />
        </form>
      </td>


      <td class="generalboxcontent">
        <label for="members"><?php p($strgroupmembersselected) ?></label>
        <form id="form3" method="post" action="groups.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="groupid" value="<?php p($selectedgroup) ?>" />
          <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>" />
          <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
          <select name="members[]" size="15" id="members" multiple="multiple">
            <?php 
                if (!empty($members)) {
                    foreach ($members as $id => $membername) {
                        echo "<option value=\"$id\">$membername</option>";
                    }
                }
            ?>
          </select>
        <br />
          <input type="submit" name="membersinfo" value="<?php p($strgroupinfomembers) ?>"
                 onclick="return userWindow(document.getElementById('form3')['members[]']);" />
        <br />
          <input type="submit" name="membersremove" value="<?php p($strgroupremovemembers) ?>" 
                 onclick="updateGroup()" />
        </form>
      </td>
    </tr>
  </table>
