require_once('../config.php');
require_once('lib.php');
-$groupid = required_param('group', PARAM_INT);
+define("MAX_USERS_PER_PAGE", 5000);
+
+$groupid = required_param('group', PARAM_INT);
+$searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
+$showall = optional_param('showall', 0, PARAM_BOOL);
+
+if ($showall) {
+ $searchtext = '';
+}
+
require_login();
if (!$group = get_record('groups', 'id', $groupid)) {
require_login($course);
$courseid = $course->id;
+$strsearch = get_string('search');
+$strshowall = get_string('showall');
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$groupid;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$potentialmembers = array();
$potentialmembersoptions = '';
$potentialmemberscount = 0;
- $potentialmembers = groups_get_users_not_in_group($courseid, $groupid);
- if ($potentialmembers != false) {
- // Put the groupings into a hash and sorts them
- foreach ($potentialmembers as $userid) {
- $nonmembers[$userid] = groups_get_user_displayname($userid, $courseid);
- $potentialmemberscount++;
- }
- natcasesort($nonmembers);
+ $potentialmembers = groups_get_users_not_in_group($courseid, $groupid, $searchtext);
+ if (!empty($potentialmembers)) {
+ $potentialmemberscount = count($potentialmembers);
+ } else {
+ $potentialmemberscount = 0;
+ }
+ if ($potentialmemberscount <= MAX_USERS_PER_PAGE) {
- // Print out the HTML
- foreach($nonmembers as $id => $name) {
- $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
+ if ($potentialmembers != false) {
+ // Put the groupings into a hash and sorts them
+ foreach ($potentialmembers as $userid => $user) {
+ $nonmembers[$userid] = fullname($user);
+ //$nonmembers[$userid] = groups_get_user_displayname($userid, $courseid);
+ }
+ natcasesort($nonmembers);
+
+ // Print out the HTML
+ foreach($nonmembers as $id => $name) {
+ $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
+ }
+ } else {
+ $potentialmembersoptions .= '<option> </option>';
}
- } else {
- $potentialmembersoptions .= '<option> </option>';
}
// Print the page and form
onfocus="document.getElementById('assignform').add.disabled=false;
document.getElementById('assignform').remove.disabled=true;
document.getElementById('assignform').removeselect.selectedIndex=-1;">
- <?php echo $potentialmembersoptions ?>
+ <?php
+ if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
+ echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
+ .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
+ } else {
+ echo $potentialmembersoptions;
+ }
+ ?>
</select>
<br />
- <?php //TODO: Search box?
-
- /*if (!empty($searchtext)) {
- echo '<input name="showall" type="submit" value="'.get_string('showall').'" />'."\n";
- }*/
+ <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
+ <input type="text" name="searchtext" id="searchtext" size="30" value="<?php p($searchtext, true) ?>"
+ onfocus ="getElementById('assignform').add.disabled=true;
+ getElementById('assignform').remove.disabled=true;
+ getElementById('assignform').removeselect.selectedIndex=-1;
+ getElementById('assignform').addselect.selectedIndex=-1;"
+ onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
+ if (keyCode == 13) {
+ getElementById('assignform').previoussearch.value=1;
+ getElementById('assignform').submit();
+ } " />
+ <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
+ <?php
+ if (!empty($searchtext)) {
+ echo '<input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
+ }
?>
</td>
</tr>
<?php
print_footer($course);
-
-
?>
/**
* Gets the users for a course who are not in a specified group
* @param int $groupid The id of the group
+ * @param string searchtext similar to searchtext in role assign, search
* @return array An array of the userids of the non-group members, or false if
- * an error occurred.
+ * an error occurred.
+ * This function was changed to get_users_by_capability style
+ * mostly because of the searchtext requirement
*/
-function groups_get_users_not_in_group($courseid, $groupid) {
- $users = get_course_users($courseid);
- $userids = groups_users_to_userids($users);
- $nongroupmembers = array();
+function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') {
- foreach($userids as $userid) {
- if (!groups_is_member($groupid, $userid)) {
- array_push($nongroupmembers, $userid);
+ global $CFG;
+
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
+
+ if ($searchtext !== '') { // Search for a subset of remaining users
+ $LIKE = sql_ilike();
+ $FULLNAME = sql_fullname();
+ $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE $FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%' )";
+ } else {
+ $wheresearch = '';
+ }
+
+ $capability = 'moodle/course:view';
+ $doanything = false;
+
+ // find all possible "student" roles
+ if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) {
+ if (!$doanything) {
+ if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
+ return false; // Something is seriously wrong
+ }
+ $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
}
+
+ $validroleids = array();
+ foreach ($possibleroles as $possiblerole) {
+ if (!$doanything) {
+ if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included
+ continue;
+ }
+ }
+ if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list
+ if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0
+ $validroleids[] = $possiblerole->id;
+ }
+ }
+ }
+ if (empty($validroleids)) {
+ return false;
+ }
+ $roleids = '('.implode(',', $validroleids).')';
+ } else {
+ return false; // No need to continue, since no roles have this capability set
}
- return $nongroupmembers;
+/// Construct the main SQL
+ $select = " SELECT u.id, u.firstname, u.lastname";
+ $from = " FROM {$CFG->prefix}user u
+ INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
+ INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid";
+ $where = " WHERE ra.contextid ".get_related_contexts_string($context)."
+ AND u.deleted = 0
+ AND ra.roleid in $roleids
+ AND u.id NOT IN (SELECT userid
+ FROM {$CFG->prefix}groups_members
+ WHERE groupid = $groupid)
+ $wheresearch";
+
+ return get_records_sql($select.$from.$where);;
}
/**