require_once('../../config.php');
require_once('../lib.php');
+$success = true;
+
$courseid = required_param('courseid', PARAM_INT);
$groupingid = required_param('groupingid', PARAM_INT);
+// Get the course information so we can print the header and
+// check the course id is valid
+$course = groups_get_course_info($courseid);
+if (! $course) {
+ $success = false;
+ print_error('invalidcourse');
+}
+
-require_login($courseid);
+if ($success) {
+ // Make sure that the user has permissions to manage groups.
+ require_login($courseid);
-// confirm_sesskey checks that this is a POST request
-if (isteacheredit($courseid)) {
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
+ if (! has_capability('moodle/course:managegroups', $context)) {
+ redirect();
+ }
+
+ //( confirm_sesskey checks that this is a POST request.)
// Print the page and form
$strgroups = get_string('groups');
"-> <a href=\"$CFG->wwwroot/group/groupui/index.php?id=$courseid\">$strgroups</a>".
"-> Display grouping", "", "", true, '', user_login_string($course, $USER));
- $groupingsettings = groups_get_grouping_settings($groupingid);
-
- if (! isset($groupingsettings->name)) {
+ $groupingname = groups_get_grouping_name($groupingid);
+ if (! $groupingname) {
print_error('errorinvalidgrouping', 'group', groups_home_url($courseid));
} else {
// Print the name of the grouping
- $name = $groupingsettings->name;
- echo "<h1>$name</h1>\n";
+ echo "<h1>$groupingname</h1>\n";
}
- // Get the groups and group members for the grouping
- $groupids = groups_get_groups_in_grouping($groupingid);
-
- if ($groupids != false) {
-
- // Make sure the groups are in the right order
- foreach($groupids as $groupid) {
- $listgroups[$groupid] = groups_get_group_displayname($groupid);
- }
+ // Get the groups and group members for the grouping.
+ if (GROUP_NOT_IN_GROUPING == $groupingid) {
+ $groupids = groups_get_groups_not_in_any_grouping($courseid);
+ } else {
+ $groupids = groups_get_groups_in_grouping($groupingid);
+ }
- natcasesort($listgroups);
+ if ($groupids) {
+ // Make sure the groups are in the right order
+ $group_names = groups_groupids_to_group_names($groupids);
+
+ // Go through each group in turn and print the group name and then the members
+ foreach ($group_names as $group) {
- // Go through each group in turn and print the group name and then the members
- foreach($listgroups as $groupid=>$groupname) {
- echo "<h2>$groupname</h2>\n";
- $userids = groups_get_members($groupid);
+ echo "<h2>{$group->name}</h2>\n";
+ $userids = groups_get_members($group->id);
if ($userids != false) {
// Make sure the users are in the right order
- unset($listmembers);
- foreach($userids as $userid) {
- $listmembers[$userid] = groups_get_user_displayname($userid, $courseid);
- }
- natcasesort($listmembers);
+ $user_names = groups_userids_to_user_names($userids, $courseid);
echo "<ol>\n";
- foreach($listmembers as $userid=>$name) {
- echo "<li>$name</li>\n";
+ foreach ($user_names as $user) {
+
+ echo "<li>{$user->name}</li>\n";
}
echo "</ol>\n";
}
$success = true;
-
+
$courseid = required_param('id', PARAM_INT);
-$groupingid = optional_param('grouping', -1, PARAM_INT);
+$groupingid = optional_param('grouping', GROUP_NOT_IN_GROUPING, PARAM_INT);
$groupid = optional_param('group', false, PARAM_INT);
$userid = optional_param('user', false, PARAM_INT);
switch ($action) {
case 'ajax_getgroupsingrouping':
- $groups = groups_groupids_to_groups(groups_get_groups_in_grouping($groupingid));
+ if (GROUP_NOT_IN_GROUPING == $groupingid) {
+ $groupids = groups_get_groups_not_in_any_grouping($courseid);
+ } else {
+ $groupids = groups_get_groups_in_grouping($groupingid);
+ }
+ $group_names = groups_groupids_to_group_names($groupids);
$json = new Services_JSON();
- echo $json->encode($groups);
+ echo $json->encode($group_names);
die; // Client side JavaScript takes it from here.
case 'ajax_getmembersingroup':
$members = array();
if ($memberids = groups_get_members($groupid)) {
- foreach ($memberids as $memberid) {
- $member = groups_get_user($memberid);
- array_push($members, $member);
- }
+ $member_names = groups_userids_to_user_names($memberids, $courseid);
$json = new Services_JSON();
- echo $json->encode($members);
+ echo $json->encode($member_names);
}
die; // Client side JavaScript takes it from here.
if ($groupingids) {
// Put the groupings into a hash and sort them
foreach($groupingids as $id) {
- $listgroupings[$id] = groups_get_grouping_displayname($id);
+ $listgroupings[$id] = groups_get_grouping_displayname($id, $courseid);
}
natcasesort($listgroupings);
}
if ($groupids) {
// Put the groups into a hash and sort them
- foreach($groupids as $id) {
- $listgroups[$id] = groups_get_group_displayname($id);
- }
-
- natcasesort($listgroups);
+ $group_names = groups_groupids_to_group_names($groupids);
// Print out the HTML
$count = 1;
- foreach($listgroups as $id => $name) {
+ foreach ($group_names as $group) {
$select = '';
- if ($groupid == $id) { //|| $count <= 1) ??
+ if ($groupid == $group->id) { //|| $count <= 1) ??
$select = ' selected="selected"';
- $sel_groupid = $id;
+ $sel_groupid = $group->id;
}
- echo "<option value=\"$id\"$select >$name</option>\n";
+ echo "<option value=\"{$group->id}\"$select>{$group->name}</option>\n";
$count++;
}
}
if (isset($sel_groupid)) {
$userids = groups_get_members($sel_groupid);
}
- if (isset($userids) && is_array($userids)) {
+ if (isset($userids)) { //&& is_array($userids)
// Put the groupings into a hash and sort them
- foreach($userids as $id) {
- $listmembers[$id] = groups_get_user_displayname($id, $courseid);
- }
- natcasesort($listmembers);
+ $user_names = groups_userids_to_user_names($userids);
- foreach($listmembers as $id => $name) {
- echo "<option value=\"$id\">$name</option>\n";
+ foreach ($user_names as $user) {
+ echo "<option value=\"{$user->id}\">{$user->name}</option>\n";
}
}
?>
for (var i=0; i<members.length; i++) {
var optionEl = document.createElement("option");
optionEl.setAttribute("value", members[i].id);
- optionEl.innerHTML = members[i].firstname+" "+members[i].lastname;
+ optionEl.innerHTML = members[i].name;
selectEl.appendChild(optionEl);
}
}
require_once($CFG->dirroot.'/group/db/dbgroupinglib.php');
define('GROUP_NOT_IN_GROUPING', -1);
+define('GROUP_ANY_GROUPING', -2);
/*****************************
Access/List functions
return groups_db_set_grouping_settings($groupingid, $groupingsettings);
}
-// TO DO
+
+/**
+ * Gets the name of a grouping with a specified ID
+ * @param int $groupid The grouping ID.
+ * @return string The name of the grouping.
+ */
+function groups_get_grouping_name($groupingid) {
+ if (GROUP_NOT_IN_GROUPING == $groupingid) {
+ return get_string('notingrouping', 'group');
+ }
+ elseif (GROUP_ANY_GROUPING == $groupingid) {
+ return get_string('anygrouping', 'group');
+ }
+ $settings = groups_get_grouping_settings($groupingid);
+ if ($settings && isset($settings->name)) {
+ return $settings->name;
+ }
+ return false;
+}
+
+
+/**
+ * Get array of group IDs for the user in a grouping.
+ * @param int $userid
+ * @param int $groupingid
+ * @return array If the user has groups an array of group IDs, else false.
+ */
function groups_get_groups_for_user_in_grouping($userid, $groupingid) {
+ global $CFG;
+ $sql = "SELECT gg.groupid
+ FROM {$CFG->prefix}groups_groupings_groups gg
+ INNER JOIN {$CFG->prefix}groups_members gm ON gm.groupid = gg.groupid
+ WHERE gm.userid = '$userid'
+ AND gg.groupingid = '$groupingid'";
+ $records = get_records_sql($sql);
+
+//print_object($records);
+ return groups_groups_to_groupids($records); //TODO:check.
}
/**
/**
- * Gets the grouping to use for a particular instance of a module in a course
+ * Gets the grouping ID to use for a particular instance of a module in a course
* @param int $coursemoduleid The id of the instance of the module in the course
* @return int The id of the grouping or false if there is no such id recorded
* or if an error occurred.
*/
-function groups_get_grouping_for_coursemodule($coursemoduleid) {
- return groups_db_get_grouping_for_coursemodule($coursemoduleid);
+function groups_get_grouping_for_coursemodule($coursemodule) {
+ return groups_db_get_grouping_for_coursemodule($coursemodule);
}
/*****************************
/**
* Utility functions for groups.
*
- * Functions to get information about users and courses that we could do with
- * that don't use any of the groups and that I can't find anywhere else!
+ * Functions we need independent of groups about users and courses.
+ * And groups utility/ user-interface functions.
*
* @copyright © 2006 The Open University
+ * @author N.D.Freear AT open.ac.uk
* @author J.White AT open.ac.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package groups
* @param int $groupid The group specified
* @return int The number of members of the group
*/
-function groups_get_no_group_members($groupid) {
- $userids = groups_get_members($groupid);
- if (!$userids) {
- $nomembers = 0;
- } else {
- $nomembers = count($userids);
- }
-
- return $nomembers;
+function groups_count_group_members($groupid) {
+ return count_records('groups_members', 'groupid ', $groupid);
}
/**
* Gets the number of groups in a specified grouping
* @param int $groupingid The grouping specified
+ * @param int $courseid The related course.
* @return int The number of groups in the grouping
*/
-function groups_get_no_groups_in_grouping($groupingid) {
- $groupids = groups_get_groups_in_grouping($groupingid);
- if (!$groupids) {
- $nogroups = 0;
+function groups_count_groups_in_grouping($groupingid, $courseid) {
+ if (GROUP_NOT_IN_GROUPING == $groupingid) {
+ $groupids = groups_get_groups_not_in_any_grouping($courseid);
+ return count($groupids);
} else {
- $nogroups = count($groupids);
+ return count_records('groups_groupings_groups', 'groupingid ', $groupingid);
}
- return $nogroups;
}
/**
- * Returns the display name of a user. This is the full name which if the user
- * is a teacher, is prefixed by if the teacher has edit permission and -
- * otherwise.
- * @param int $userid The id of the user specified
- * @param boolean $teacher True if the user is a teacher, false otherwise
- * @return string The display name of the user
+ * Returns the display name of a user - the full name of the user
+ * prefixed by '#' for editing teachers and '-' for teachers.
+ * @param int $userid The ID of the user.
+ * @param int $courseid The ID of the related-course.
+ * @return string The display name of the user.
*/
function groups_get_user_displayname($userid, $courseid) {
if ($courseid == false) {
- $fullname = false;
+ $fullname = false;
} else {
$user = groups_get_user($userid);
$fullname = fullname($user, true);
+ //TODO: isteacher, isteacheredit.
if (isteacher($courseid, $userid)) {
if (isteacheredit($courseid, $userid)) {
$prefix = '# ';
} else {
$prefix = '- ';
}
-
$fullname = $prefix.$fullname;
}
}
-
return $fullname;
}
/**
- * Returns the display name of a group - this is the group name followed by the
- * number of group members in brackets
- * @param int $groupid The groupid
+ * Returns the display name of a group - the group name followed by
+ * the number of members in brackets.
+ * @param int $groupid The group ID.
* @return string The display name of the group
*/
function groups_get_group_displayname($groupid) {
- $groupsettings = groups_get_group_settings($groupid);
- if ($groupsettings) {
- $groupname = $groupsettings->name;
- $count = groups_get_no_group_members($groupid);
+ if ($groupname = groups_get_group_name($groupid)) {
+ $count = groups_count_group_members($groupid);
return "$groupname ($count)";
}
return false;
/**
- * Returns the display name of a grouping - this is the grouping name followed
- * by the number of groups in the
- * grouping in brackets
- * @param int $groupingid The grouping id
+ * Returns the display name of a grouping - the grouping name followed
+ * by the number of groups in the grouping in brackets.
+ * @param int $groupingid The grouping ID.
+ * @param int $courseid The related course.
* @return string The display name of the grouping
*/
-function groups_get_grouping_displayname($groupingid) {
- if (GROUP_NOT_IN_GROUPING == $groupingid) {
- return get_string('notingrouping', 'group');
- }
- $groupingsettings = groups_get_grouping_settings($groupingid);
- if ($groupingsettings) {
- $groupingname = $groupingsettings->name;
- $count = groups_get_no_groups_in_grouping($groupingid);
+function groups_get_grouping_displayname($groupingid, $courseid) {
+ if ($groupingname = groups_get_grouping_name($groupingid)) {
+ $count = groups_count_groups_in_grouping($groupingid, $courseid);
return "$groupingname ($count)";
}
return false;
/**
* Takes an array of users (i.e of objects) and converts it in the corresponding
- * array of userids.
+ * array of user IDs.
* @param $users array The array of users
- * @return array The array of user ids, or false if an error occurred
+ * @return array The array of user IDs, or false if an error occurred
*/
function groups_users_to_userids($users) {
if (! $users) {
return $userids;
}
+/**
+ * Get an sorted array of user-id/display-name objects.
+ */
+function groups_userids_to_user_names($userids, $courseid) {
+ if (! $userids) {
+ return array();
+ }
+ $member_names = array();
+ foreach ($userids as $id) {
+ $user = new object;
+ $user->id = $id;
+ $user->name = groups_get_user_displayname($id, $courseid);
+ $member_names[] = $user;
+ }
+ if (! usort($member_names, 'groups_compare_name')) {
+ debug('Error usort [groups_compare_name].');
+ }
+ return $member_names;
+}
+
+
/**
* Takes an array of groups (i.e of objects) and converts it to the
* corresponding array of group IDs.
return $groupids;
}
-// @@@ TO DO
-function groups_groupid_to_group($groupid) {
-}
/**
* Given an array of group IDs get an array of group objects.
* TODO: quick and dirty. Replace with SQL?
* @param $groupids Array of group IDs.
- * @param $courseid Default false, or Course ID.
+ * @param $courseid Default false, or the course ID for backwards compatibility.
* @param $alldata Default false, or get complete record for group.
- * @param array Array of group objects, with basic or all data.
+ * @return array Array of group objects, with basic or all data.
*/
function groups_groupids_to_groups($groupids, $courseid=false, $alldata=false) {
if (! $groupids) {
return $groups;
}
+
+/**
+ * Get a sorted array of group-id/display-name objects.
+ */
+function groups_groupids_to_group_names($groupids) {
+ if (! $groupids) {
+ return array();
+ }
+ $group_names = array();
+ foreach ($groupids as $id) {
+ $gname = new object;
+ $gname->id = $id;
+ $gname->name = groups_get_group_displayname($id);
+ $group_names[] = $gname;
+ }
+ if (! usort($group_names, 'groups_compare_name')) {
+ debug('Error usort [groups_compare_name].');
+ }
+ /*// Put the groups into a hash and sort them
+ foreach($groupids as $id) {
+ $listgroups[$id] = groups_get_group_displayname($id);
+ }
+ natcasesort($listgroups);
+
+ $group_names = array();
+ foreach ($listgroups as $id => $name) {
+ $gname = new object;
+ $gname->id = $id;
+ $gname->name = $name;
+ $group_names[] = $gname;
+ }*/
+ return $group_names;
+}
+
+
+/**
+ * Comparison function for 'usort' on objects with a name member.
+ * Equivalent to 'natcasesort'.
+ */
+function groups_compare_name($obj1, $obj2) {
+ if (!$obj1 || !$obj2 || !isset($obj1->name) || !isset($obj2->name)) {
+ debug('Error, groups_compare_name.');
+ }
+ return strcasecmp($obj1->name, $obj2->name);
+}
+
+
function groups_groupingids_to_groupings($groupingids) {
if (! $groupingids) {
return false;
/**
* Gets the user object for a given userid. Can't find a function anywhere to
- * do this and we need this
- * for fullname()
+ * do this and we need this for fullname()
*
* @param $userid int The userid
* @return object The corresponding user object, or false if an error occurred
* Gets the course information object for a given course id
* @param $courseid int The course id
* @return object The course info object, or false if an error occurred.
- * @@@ TO DO - need to put the database bit into a db file
+ * TODO: need to put the database bit into a db file
*/
function groups_get_course_info($courseid){
if (!$courseid) {