3 * Extra library for groups and groupings.
5 * @copyright © 2006 The Open University
6 * @author J.White AT open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * INTERNAL FUNCTIONS - to be used by moodle core only
13 * require_once $CFG->dirroot.'/group/lib.php' must be used
18 * @param object $data group properties (with magic quotes);
19 * @param object $um upload manager with group picture
20 * @return id of group or false if error
22 function groups_create_group($data, $um=false) {
24 require_once("$CFG->libdir/gdlib.php");
26 $data->timecreated
= time();
27 $data->timemodified
= $data->timecreated
;
28 $id = insert_record('groups', $data);
32 if (save_profile_image($id, $um, 'groups')) {
33 set_field('groups', 'picture', 1, 'id', $id);
42 * @param object $data group properties (with magic quotes);
43 * @param object $um upload manager with group picture
44 * @return boolean success
46 function groups_update_group($data, $um=false) {
48 require_once("$CFG->libdir/gdlib.php");
50 $data->timemodified
= time();
51 $result = update_record('groups', $data);
53 if ($result and $um) {
55 if (save_profile_image($data->id
, $um, 'groups')) {
56 set_field('groups', 'picture', 1, 'id', $data->id
);
64 * Delete a group best effort, first removing members and links with courses and groupings.
65 * Removes group avatar too.
66 * @param int $groupid The group to delete
67 * @return boolean True if deletion was successful, false otherwise
69 function groups_delete_group($groupid) {
71 require_once($CFG->libdir
.'/gdlib.php');
73 if (empty($groupid)) {
77 //first delete usage in groupings_groups
78 delete_records('groupings_groups', 'groupid', $groupid);
80 delete_records('groups_members', 'groupid', $groupid);
82 delete_profile_image($groupid, 'groups');
84 return delete_records('groups', 'id', $groupid);
87 function groups_delete_grouping($groupingid) {
88 if (empty($groupingid)) {
93 //first delete usage in groupings_groups
94 delete_records('groupings_groups', 'groupingid', $groupingid);
95 // remove the default groupingid from course
96 set_field('course', 'defaultgroupingid', 0, 'defaultgroupingid', $groupingid);
97 // remove the groupingid from all course modules
98 set_field('course_modules', 'groupingid', 0, 'groupingid', $groupingid);
100 return delete_records('groupings', 'id', $groupingid);
103 function groups_delete_group_members($courseid, $showfeedback=false) {
106 $sql = "DELETE FROM {$CFG->prefix}groups_members
107 WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)";
109 execute_sql($sql, false);
111 notify(get_string('deleted').' groups_members');
117 function groups_delete_groups($courseid, $showfeedback=false) {
119 require_once($CFG->libdir
.'/gdlib.php');
121 // delete any uses of groups
122 $sql = "DELETE FROM {$CFG->prefix}groupings_groups
123 WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)";
124 execute_sql($sql, false);
126 groups_delete_group_members($courseid, false);
128 // delete group pictures
129 if ($groups = get_records('groups', 'courseid', $courseid)) {
130 foreach($groups as $group) {
131 delete_profile_image($group->id
, 'groups');
135 delete_records('groups', 'courseid', $courseid);
137 notify(get_string('deleted').' groups');
143 function groups_delete_groupings($courseid, $showfeedback=false) {
146 // delete any uses of groupings
147 $sql = "DELETE FROM {$CFG->prefix}groupings_groups
148 WHERE groupingid in (SELECT id FROM {$CFG->prefix}groupings g WHERE g.courseid = $courseid)";
149 execute_sql($sql, false);
151 // remove the default groupingid from course
152 set_field('course', 'defaultgroupingid', 0, 'id', $courseid);
153 // remove the groupingid from all course modules
154 set_field('course_modules', 'groupingid', 0, 'courseid', $courseid);
156 delete_records('groupings', 'courseid', $courseid);
158 notify(get_string('deleted').' groupings');
164 /* =================================== */
165 /* various functions used by groups UI */
166 /* =================================== */
169 * Gets the users for a course who are not in a specified group
170 * @param int $groupid The id of the group
171 * @param string searchtext similar to searchtext in role assign, search
172 * @return array An array of the userids of the non-group members, or false if
174 * This function was changed to get_users_by_capability style
175 * mostly because of the searchtext requirement
177 function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') {
181 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
183 if ($searchtext !== '') { // Search for a subset of remaining users
185 $FULLNAME = sql_fullname();
186 $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE $FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%' )";
191 $capability = 'moodle/course:view';
194 // find all possible "student" roles
195 if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW
, $context)) {
197 if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM
)) {
198 return false; // Something is seriously wrong
200 $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW
, $sitecontext);
203 $validroleids = array();
204 foreach ($possibleroles as $possiblerole) {
206 if (isset($doanythingroles[$possiblerole->id
])) { // We don't want these included
210 if ($caps = role_context_capabilities($possiblerole->id
, $context, $capability)) { // resolved list
211 if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0
212 $validroleids[] = $possiblerole->id
;
216 if (empty($validroleids)) {
219 $roleids = '('.implode(',', $validroleids).')';
221 return false; // No need to continue, since no roles have this capability set
224 /// Construct the main SQL
225 $select = " SELECT u.id, u.firstname, u.lastname";
226 $from = " FROM {$CFG->prefix}user u
227 INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
228 INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid";
229 $where = " WHERE ra.contextid ".get_related_contexts_string($context)."
231 AND ra.roleid in $roleids
232 AND u.id NOT IN (SELECT userid
233 FROM {$CFG->prefix}groups_members
234 WHERE groupid = $groupid)
237 return get_records_sql($select.$from.$where);;