]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10383 - groups/groupings refactoring nearly finished ;-)
authorskodak <skodak>
Thu, 16 Aug 2007 11:06:48 +0000 (11:06 +0000)
committerskodak <skodak>
Thu, 16 Aug 2007 11:06:48 +0000 (11:06 +0000)
19 files changed:
admin/uploaduser.php
blocks/quiz_results/block_quiz_results.php
course/lib.php
enrol/imsenterprise/enrol.php
enrol/manual/enrol.php
group/clientlib.js [moved from group/lib/clientlib.js with 100% similarity]
group/db/dbbasicgrouplib.php [deleted file]
group/index.php
group/lib/basicgrouplib.php [deleted file]
group/lib/groupinglib.php [deleted file]
group/lib/legacylib.php [deleted file]
group/lib/utillib.php [deleted file]
lib/accesslib.php
lib/deprecatedlib.php
lib/grouplib.php
lib/moodlelib.php
mod/hotpot/report.php
mod/quiz/locallib.php
user/view.php

index ce86f5da9d18620c1d50f847104170c1c0422673..8a2c91af3e7684eeee067ab222df865f516724dc 100755 (executable)
@@ -339,7 +339,7 @@ if ( $formdata = $mform->get_data() ) {
                         if ($course[$i] && $groupid[$i]) {
                             $coursecontext = get_context_instance(CONTEXT_COURSE, $course[$i]->id);
                             if (count(get_user_roles($coursecontext, $user->id))) {
-                                if (add_user_to_group($groupid[$i], $user->id)) {
+                                if (groups_add_member($groupid[$i], $user->id)) {
                                     notify('-->' . get_string('addedtogroup','',$addgroup[$i]));
                                 } else {
                                     notify('-->' . get_string('addedtogroupnot','',$addgroup[$i]));
index d3f968a77758cef60d0f5d47294d6efc32d9a2c6..cd072cc26ec9f9e64d63be093acb6001fc770358 100644 (file)
@@ -127,10 +127,10 @@ class block_quiz_results extends block_base {
             }
 
             // Now find which groups these users belong in
-            $groupofuser = groups_get_groups_users($userids, $courseid); /*TODO: get_records_sql(
+            $groupofuser = get_records_sql(
             'SELECT m.userid, m.groupid, g.name FROM '.$CFG->prefix.'groups g LEFT JOIN '.$CFG->prefix.'groups_members m ON g.id = m.groupid '.
             'WHERE g.courseid = '.$courseid.' AND m.userid IN ('.implode(',', $userids).')'
-            );*/
+            );
 
             $groupgrades = array();
 
index 72f6ffe017c1bb771087327332bcff505ab95b1b..e6114ae310e1df663bddf430424619ca8106d899 100644 (file)
@@ -177,15 +177,19 @@ function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0,
         $groupmode =  groupmode($course);
 
         if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id)))) {
-            if ($groups_names = groups_get_groups_names($course->id)) { //TODO:check.
-            echo '<td><b>';
+            if ($groups = groups_get_all_groups($course->id)) {
+                $group_names = array();
+                foreach($groups as $group) {
+                    $group_names[$group->id] = format_string($group->name);
+                }
+                echo '<td><b>';
                 if ($groupmode == VISIBLEGROUPS) {
                     print_string('groupsvisible');
                 } else {
                     print_string('groupsseparate');
                 }
                 echo ':</b></td><td>';
-                choose_from_menu($groups_names, "selectedgroup", $selectedgroup, get_string("allgroups"), "", "");
+                choose_from_menu($group_names, "selectedgroup", $selectedgroup, get_string("allgroups"), "", "");
                 echo '</td>';
             }
         }
index ebc4530f28ec404f2f3a428144505861a80927c0..96c7fac5a43cc5ee0b297aa9464702ae2653b736 100644 (file)
@@ -795,7 +795,7 @@ function process_membership_tag($tagcontents){
                             }
                             // Add the user-to-group association if it doesn't already exist
                             if($member->groupid) {
-                                add_user_to_group ($member->groupid, $memberstoreobj->userid);
+                                groups_add_member($member->groupid, $memberstoreobj->userid);
                             }
                         } // End of group-enrolment (from member.role.extension.cohort tag)
                         
index 7a31408d4c33e60e48da4a48e8f07c1a1c9aa3ca..347e7e610ef6b1526e687de440c33797302d7eff 100644 (file)
@@ -147,7 +147,7 @@ function check_entry($form, $course) {
         } else {  /// Update or add new enrolment
             if (enrol_into_course($course, $USER, 'manual')) {
                 if ($groupid !== false) {
-                    if (!add_user_to_group($groupid, $USER->id)) {
+                    if (!groups_add_member($groupid, $USER->id)) {
                         print_error('couldnotassigngroup');
                     }
                 }
similarity index 100%
rename from group/lib/clientlib.js
rename to group/clientlib.js
diff --git a/group/db/dbbasicgrouplib.php b/group/db/dbbasicgrouplib.php
deleted file mode 100644 (file)
index 40e3e9a..0000000
+++ /dev/null
@@ -1,325 +0,0 @@
-<?php 
-/**
- * Functions to make changes to groups in the database i.e. functions that 
- * access tables:
- *     groups and groups_members.
- *
- * @copyright &copy; 2006 The Open University
- * @author J.White AT open.ac.uk
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- */
-
-/*******************************************************************************
- * Utility functions
- ******************************************************************************/
-
-/**
- * Returns the user record for a given userid - I can't seem to find a function 
- * anywhere else to do this
- * (and we need it to use the fullname() function). 
- * @param int $userid The id of the user to get the record for
- * @return object The user record
- */
-function groups_db_get_user($userid) {
-    $query = get_record('user', 'id', $userid);
-    return $query;
-}
-
-
-/******************************************************************************* 
-    List functions  
- ******************************************************************************/
-
-
-/**
- * Returns all the ids of all the groups for the specified course.
- * @uses $CFG
- * @param int $courseid The courseid to get the groups for.
- * @return array|false Returns an array of the group ids or false if no groups
- * or an error returned
- */
- function groups_db_get_groups($courseid) {
-    if (! $courseid) {
-        return false;
-    }
-    $records = get_records('groups', 'courseid', $courseid, 
-                           '', $fields='id');
-    if (! $records) {
-        return false;
-    }
-    // Put the results into an array, note these are NOT 'group' objects.
-    $groupids = array();
-    foreach ($records as $record) {
-        array_push($groupids, $record->id);
-    }
-
-    return $groupids;
-}
-
-
-/**
- * Returns the ids of the users in the specified group.
- * @param int $groupid The groupid to get the users for
- * @return array| false Returns an array of the user ids for the specified
- * group or false if no users or an error returned.
- */
-function groups_db_get_members($groupid) {
-    if (!$groupid) {
-        $userids = false;
-    } else {
-        $users = get_records('groups_members', 'groupid ', $groupid, '', 
-                             $fields='id, userid');
-        if (!$users) {
-            $userids = false;
-        } else {
-            $userids = array();
-            foreach ($users as $user) {
-                array_push($userids, $user->userid);
-            }
-        }
-    }
-    return $userids;
-}
-
-
-/**
- * Gets the groups to which a user belongs for a specified course. 
- * @uses $CFG
- * @param int $userid The id of the specified user
- * @param int $courseid The id of the course. 
- * @return array | false An array of the group ids of the groups to which the
- * user belongs or false if there are no groups or an error occurred.
- */
-function groups_db_get_groups_for_user($userid, $courseid) {
-    if (!$userid or !$courseid) {
-        $groupids = false;
-    } else {  
-        global $CFG;
-        $sql = "SELECT g.id, gm.userid 
-                FROM {$CFG->prefix}groups_members gm 
-                INNER JOIN {$CFG->prefix}groups g
-                ON gm.groupid = g.id
-                WHERE g.courseid  = '$courseid' AND gm.userid = '$userid'";
-                
-        $groups = get_records_sql($sql);
-        $groupids = groups_groups_to_groupids($groups);
-    }
-
-    return $groupids;
-}
-
-/**
- * Get the group settings object for a group - this contains the following 
- * properties:
- * name, description, picture, hidepicture
- * @param int $groupid The id of the group
- * @param $courseid Optionally add the course ID, for backwards compatibility.
- * @return object The group settings object 
- */
-function groups_db_get_group_settings($groupid, $courseid=false, $alldata=false) {
-   if (!$groupid) {
-        $groupsettings = false;
-    } else {
-        global $CFG;
-        $select = ($alldata) ? '*' : 'id, name, description, picture, hidepicture';
-        $sql = "SELECT $select
-                FROM {$CFG->prefix}groups
-                WHERE id = $groupid";
-        $groupsettings = get_record_sql($sql);
-        if ($courseid && $groupsettings) {
-            $groupsettings->courseid = $courseid;
-        }
-    }
-
-    return $groupsettings;
-
-}
-
-/******************************************************************************* 
-   Membership functions  
- ******************************************************************************/
-
-
-/**
- * Determines if a group with a given groupid exists. 
- * @param int $groupid The groupid to check for
- * @return boolean True if the group exists, false otherwise or if an error 
- * occurred. 
- */
-function groups_db_group_exists($groupid) {
-    if (!$groupid) {
-        $exists = false;
-    } else {
-        $exists = record_exists($table = 'groups', 'id', $groupid);
-    }
-
-    return $exists;
-}
-
-/**
- * Determines if a specified group is a group for a specified course
- * @param int $groupid The group about which the request has been made
- * @param int $courseid The course for which the request has been made
- * @return boolean True if the group belongs to the course, false otherwise
- */
-function groups_db_group_belongs_to_course($groupid, $courseid) {   
-    if (!$groupid or !$courseid) {
-        $ismember = false;
-    } else {
-        $ismember = record_exists($table = 'groups', 
-                                  'id', $groupid, 
-                                  'courseid', $courseid);
-    }
-
-    return $ismember;
-}
-
-
-/*******************************************************************************
-   Creation functions  
- ******************************************************************************/
-
-
-/** 
- * Creates a group for a specified course
- * @param int $courseid The course to create the group for
- * @return int The id of the group created or false if the create failed.
- */
-function groups_db_create_group($courseid, $groupsettings=false, $copytime=false) {
-    // Check we have a valid course id
-    if (!$courseid) {
-        $groupid = false; 
-    } else {      
-        $groupsettings = groups_set_default_group_settings($groupsettings);
-
-        $record = $groupsettings;
-        if (! $copytime) {
-            $now = time();
-            $record->timecreated = $now;
-            $record->timemodified = $now;
-        }
-        //print_r($record);
-        $groupid = insert_record('groups', $record);
-        
-    }
-    return $groupid;
-}
-
-/** 
- * Upgrades a group for a specified course. To preserve the group ID we do a raw insert.
- * @param int $courseid The course to create the group for
- * @return int The id of the group created or false if the insert failed.
- */
-function groups_db_upgrade_group($courseid, $group) {
-    global $CFG;
-    // Check we have a valid course id
-    if (!$courseid || !$group || !isset($group->id)) {
-        return false; 
-    }
-
-    $r = addslashes_object($group);
-    $sql = "INSERT INTO {$CFG->prefix}groups
-        (id,courseid,name,description, enrolmentkey,picture,hidepicture, timecreated,timemodified)
-        VALUES ('$r->id','$r->courseid','$r->name','$r->description', '$r->enrolmentkey','$r->picture',
-                '$r->hidepicture', '$r->timecreated','$r->timemodified')";
-
-    $result = execute_sql($sql);
-    return $group->id;
-}
-
-
-/**
- * Adds a specified user to a group
- * @param int $groupid  The group id
- * @param int $userid   The user id
- * @return boolean True if user added successfully, false otherwise. 
- */
-function groups_db_add_member($groupid, $userid, $copytime=false) {
-    // Check that the user and group are valid
-    if (!$userid or !$groupid or !groups_db_group_exists($groupid)) {
-        $useradded = false;
-        // If the user is already a member of the group, just return success
-    } elseif (groups_is_member($groupid, $userid)) {
-        $useradded = true;
-    } else {
-        // Add the user to the group
-        $record = new Object();
-        $record->groupid = $groupid;
-        $record->userid = $userid;
-        if ($copytime) {
-            $record->timeadded = $copytime;
-        } else {
-            $record->timeadded = time();
-        }
-        $useradded = insert_record($table = 'groups_members', $record);
-    }
-
-    return $useradded;
-}
-
-
-/**
- * Sets the information about a group
- * @param object $groupsettings An object containing some or all of the 
- * following properties:
- * name, description, picture, hidepicture
- * @return boolean True if info was added successfully, false otherwise. 
- */
-function groups_db_set_group_settings($groupid, $groupsettings) {
-    $success = true;
-    if (!$groupid or !$groupsettings or !groups_db_group_exists($groupid)) {
-        $success = false; 
-    } else {
-        $record = $groupsettings;
-        $record->id = $groupid;
-        $record->timemodified = time();
-        $result = update_record('groups', $record);
-        if (!$result) {
-            $success = false;
-        }
-    }
-
-    return $success;
-}
-
-/******************************************************************************* 
-    Deletion functions  
- ******************************************************************************/
-
-
-/**
- * Deletes the specified user from the specified group
- * @param int $groupid The group to delete the user from
- * @param int $userid The user to delete
- * @return boolean True if deletion was successful, false otherwise
- */
-function groups_db_remove_member($groupid, $userid) {
-    if (!$userid or !$groupid) {
-        $success = false;
-    } else {
-        $results = delete_records('groups_members', 
-                                  'groupid', $groupid, 'userid', $userid);
-        // delete_records returns an array of the results from the sql call, 
-        // not a boolean, so we have to set our return variable
-        if ($results == false) {
-            $success = false;
-        } else {
-            $success = true;
-        }
-    }
-
-    return $success;
-}
-
-
-/**
- * Internal function to set the time a group was modified.
- */
-function groups_db_set_group_modified($groupid) {
-    return set_field('groups', 'timemodified', time(), 'id', $groupid);
-}
-
-
-?>
index f81944083bf8a8c6063049e0d9895b85e31da7ca..a12281f1e5ca88978148ab0cb3030b57292c4afc 100644 (file)
@@ -16,7 +16,7 @@ require_js('yui_yahoo');
 require_js('yui_dom');
 require_js('yui_utilities');
 require_js('yui_connection');
-require_js($CFG->wwwroot.'/group/lib/clientlib.js');
+require_js($CFG->wwwroot.'/group/clientlib.js');
 
 $courseid = required_param('id', PARAM_INT);
 $groupid  = optional_param('group', false, PARAM_INT);
@@ -134,28 +134,22 @@ if (!$course = get_record('course', 'id',$courseid)) {
     echo '<select name="group" id="groups" size="15" class="select" onchange="membersCombo.refreshMembers(this.options[this.selectedIndex].value);"'."\n";
     echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
 
-    if ($groups = groups_get_all_groups($courseid)) {
-        $groupids = array_keys($groups);
-    } else {
-        $groupids = false;
-    }
+    $groups = groups_get_all_groups($courseid);
 
     $sel_groupid = 0;
 
-    if ($groupids) {
-        // Put the groups into a hash and sort them
-        $group_names = groups_groupids_to_group_names($groupids);
-
+    if ($groups) {
         // Print out the HTML
-        $count = 1;
-        foreach ($group_names as $group) {
+        foreach ($groups as $group) {
             $select = '';
-            if ($groupid == $group->id) { //|| $count <= 1) ??
+            if ($groupid == $group->id) {
                 $select = ' selected="selected"';
                 $sel_groupid = $group->id;
             }
-            echo "<option value=\"{$group->id}\"$select title=\"{$group->name}\">{$group->name}</option>\n";
-            $count++;
+            $usercount = (int)count_records('groups_members', 'groupid', $group->id);
+            $groupname = format_string($group->name).' ('.$usercount.')';
+
+            echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
         }
     } else {
         // Print an empty option to avoid the XHTML error of having an empty select element
diff --git a/group/lib/basicgrouplib.php b/group/lib/basicgrouplib.php
deleted file mode 100644 (file)
index 3c7f07c..0000000
+++ /dev/null
@@ -1,240 +0,0 @@
-<?php
-/**
- * Library of basic group functions.
- *
- * These functions are essentially just wrappers for the equivalent database
- * functions in db/dbgrouplib.php
- * 
- * It is advised that you do not create groups that do not belong to a 
- * grouping, although to allow maximum flexibility, functions are 
- * provided that allow you to do this. 
- * Note that groups (and groupings - see groupinglib.php) must belong to a 
- * course. There is no reason why a group cannot belong to more than one
- * course, although this might cause problems when group members are not
- * users of one of the courses.
- * At the moment, there are no checks that group members are also users of a 
- * course.
- * 
- * @copyright &copy; 2006 The Open University
- * @author J.White AT open.ac.uk
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- */
-require_once($CFG->dirroot.'/group/db/dbbasicgrouplib.php');
-
-
-/***************************** 
-    List functions  
- *****************************/
-
-/**
- * Get the user ID and time added for each member of a group, for backup4.
- * @return array An array of member records.
- */
-function groups_get_member_records($groupid) {
-    if (!$groupid) {
-        return false;
-    }
-    $members = get_records('groups_members', 'groupid ', $groupid, '', 
-                          $fields='id, userid, timeadded');
-
-    return $members;
-}
-
-
-/**
- * Gets the groups to which a user belongs for a specified course. 
- * @param int $userid The id of the specified user
- * @param int $courseid The id of the course.
- * @param boolean $usedatabase. If true, gets the information from  
- * @return array | false An array of the group ids of the groups to which the
- * user belongs or false if there are no groups or an error occurred.
- */
-function groups_get_groups_for_user($userid, $courseid) {  
-    $groupids = groups_db_get_groups_for_user($userid, $courseid);
-    return $groupids;
-}
-
-/**
- * Get the groups to which a user belongs in any course on site.
- * @return array | false An array of the group IDs, or false on error.
- */
-function groups_get_all_groups_for_user($userid) {
-    $groups = get_records('groups_members', 'userid', $userid);
-    if (! $groups) {
-        return false;
-    }
-    // Put the results into an array. TODO: check.
-    $groupids = array();
-    foreach ($groups as $group) {
-        array_push($groupids, $group->id);    
-    }
-    return $groupids;
-}
-
-/**
- * Gets the groups for the current user and specified course 
- * @param int $courseid The id of the course
- * @param int $usedatabase Set to true if the information is to be obtained 
- * directly
- * from the database, false if it is to be obtained from the $USER object. 
- * @return array An array of the groupids. 
- */
-function groups_get_groups_for_current_user($courseid) {
-    global $USER;
-    $groupids = groups_get_groups_for_user($USER->id, $courseid);
-    return $groupids;
-}
-
-
-/**
- * Get the group settings object for a group - this contains the following 
- * properties:
- * name, description, picture, hidepicture
- * @param int $groupid The group ID.
- * @return object The group settings object 
- */
-function groups_get_group_settings($groupid, $courseid=false, $alldata=false) {
-    return groups_db_get_group_settings($groupid, $courseid, $alldata);
-}
-
-/**
- * Gets the path where the image for a particular group can be found (if it 
- * exists)
- * @param int $groupid The id of the group
- * @return string The path of the image for the group
- */
-function groups_get_group_image_path($groupid) {
-    //TODO: groupid=1, /user/pixgroup.php/1/f1.jpg ??
-    return $CFG->wwwroot.'/pixgroup.php/'.$groupid.'/f1.jpg';
-}
-
-/**
- * Gets the name of a group with a specified id
- * @param int $groupid The id of the group
- * @return string The name of the group
- */
-function groups_get_group_name($groupid) {
-    $settings = groups_get_group_settings($groupid);
-    if ($settings) {
-        return $settings->name;
-    }
-    return false;
-}
-
-/*****************************
-   Membership functions 
- *****************************/
-
-
-/**
- * Determines if a group with a given groupid exists. 
- * @param int $groupid The groupid to check for
- * @return boolean True if the group exists, false otherwise or if an error 
- * occurred. 
- */
-function groups_group_exists($groupid) {
-    return groups_db_group_exists($groupid);
-}
-
-/**
- * Determines if a specified group is a group for a specified course
- * @param int $groupid The group about which the request has been made
- * @param int $courseid The course for which the request has been made
- * @return boolean True if the group belongs to the course, false otherwise
- */
-function groups_group_belongs_to_course($groupid, $courseid) {
-    $belongstocourse = groups_db_group_belongs_to_course($groupid, $courseid);
-    return $belongstocourse;
-}
-
-/**
- * Returns an object with the default group info values - these can of course be 
- * overridden if desired.
- * Can also be used to set the default for any values not set
- * @return object The group info object. 
- */
-function groups_set_default_group_settings($groupinfo = null) {
-        
-    if (!isset($groupinfo->name)) {
-        $groupinfo->name = 'Temporary Group Name';
-    }
-
-    if (!isset($groupinfo->description)) {
-        $groupinfo->description = '';
-    }
-
-    if (!isset($groupinfo->picture)) {
-        $groupinfo->picture = 0;
-    }
-
-    if (!isset($groupinfo->hidepicture)) {
-        $groupinfo->hidepicture = 1;
-    }
-
-    if (isset($groupinfo->hidepicture)) {
-        if ($groupinfo->hidepicture != 0 and $groupinfo->hidepicture != 1) {
-            $groupinfo->hidepicture = 1;
-        }
-    }
-
-    return $groupinfo;
-}
-
-/***************************** 
-      Creation functions  
- *****************************/
-
-/**
- * Adds a specified user to a group
- * @param int $userid   The user id
- * @param int $groupid  The group id
- * @return boolean True if user added successfully or the user is already a 
- * member of the group, false otherwise. 
- * See comment above on web service autoupdating. 
- */
-function groups_add_member($groupid, $userid) {
-    $useradded = false;
-
-    $alreadymember = groups_is_member($groupid, $userid);
-    if (!groups_group_exists($groupid)) {
-        $useradded = false;
-    } elseif ($alreadymember) {
-        $useradded = true;
-    } else {
-        $useradded = groups_db_add_member($groupid, $userid);
-    }
-    if ($useradded) {
-      
-        // MDL-9983
-        $eventdata = new object();
-        $eventdata -> groupid = $groupid;
-        $eventdata -> userid = $userid;
-        events_trigger('group_user_added', $eventdata);      
-        $useradded = groups_db_set_group_modified($groupid);
-    }
-    return $useradded;
-}
-
-
-/*****************************
-        Deletion functions  
- *****************************/
-
-
-/**
- * Deletes the link between the specified user and group.
- * @param int $groupid The group to delete the user from
- * @param int $userid The user to delete
- * @return boolean True if deletion was successful, false otherwise
- * See comment above on web service autoupdating. 
- */
-function groups_remove_member($groupid, $userid) {
-    $success = groups_db_remove_member($groupid, $userid);    
-    if ($success) {
-        $success = groups_db_set_group_modified($groupid);
-    }
-    return $success;
-}
-
-?>
diff --git a/group/lib/groupinglib.php b/group/lib/groupinglib.php
deleted file mode 100644 (file)
index 6a6b57d..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-/**
- * A grouping is a set of groups that belong to a course. 
- * There may be any number of groupings for a course and a group may
- * belong to more than one grouping.
- *
- * @copyright &copy; 2006 The Open University
- * @author J.White AT open.ac.uk
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- */
-require_once($CFG->dirroot.'/group/lib/basicgrouplib.php');
-require_once($CFG->dirroot.'/group/db/dbgroupinglib.php');
-
-define('GROUP_NOT_IN_GROUPING', -1);
-define('GROUP_ANY_GROUPING',     0);
-
-
-/**
- * Gets the information about a specified grouping
- * @param int $groupingid
- * @return object The grouping settings object - properties are name and 
- * description. 
- */
-function groups_get_grouping_settings($groupingid) {
-    error('missing');
-}
-
-/**
- * Gets a list of the groups not in any grouping, but in this course.
- * TODO: move to dbgroupinglib.php
- * @param $courseid If null or false, returns groupids 'not in a grouping sitewide'.
- * @return array An array of group IDs.
- */
-function groups_get_groups_not_in_any_grouping($courseid) {
-    global $CFG;
-
-    $join = '';
-    $where= '';
-    if ($courseid) {
-        $where= "AND g.courseid = '$courseid'";
-    }
-    $sql = "SELECT g.id
-        FROM {$CFG->prefix}groups g
-        $join
-        WHERE g.id NOT IN 
-        (SELECT groupid FROM {$CFG->prefix}groupings_groups)
-        $where";
-
-    $records = get_records_sql($sql);
-    $groupids = groups_groups_to_groupids($records, $courseid);
-
-    return $groupids;
-}
-
-
-?>
diff --git a/group/lib/legacylib.php b/group/lib/legacylib.php
deleted file mode 100644 (file)
index 79b2ca8..0000000
+++ /dev/null
@@ -1,318 +0,0 @@
-<?php
-/**
- * Legacy groups functions - these were in moodlelib.php, datalib.php, weblib.php
- *
- * @@@ Don't look at this file - still tons to do! 
- *
- * TODO: For the moment these functions are in /lib/deprecatedlib.php
- *   get_group_students
- *   get_group_teachers
- *
- * @copyright &copy; 2006 The Open University
- * @author J.White AT open.ac.uk and others
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package groups
- */
-
-
-
-/**
- * Returns an array of user objects
- *
- * @uses $CFG
- * @param int $groupid The group in question.
- * @param string $sort ?
- * @param string $exceptions ?
- * @return object
- * @todo Finish documenting this function
- */
-function get_group_users($groupid, $sort='u.lastaccess DESC', $exceptions='', 
-                         $fields='u.*') {
-    global $CFG;
-    if (!empty($exceptions)) {
-        $except = ' AND u.id NOT IN ('. $exceptions .') ';
-    } else {
-        $except = '';
-    }
-    // in postgres, you can't have things in sort that aren't in the select, so...
-    $extrafield = str_replace('ASC','',$sort);
-    $extrafield = str_replace('DESC','',$extrafield);
-    $extrafield = trim($extrafield);
-    if (!empty($extrafield)) {
-        $extrafield = ','.$extrafield;
-    }
-    return get_records_sql("SELECT DISTINCT $fields $extrafield
-                              FROM {$CFG->prefix}user u,
-                                   {$CFG->prefix}groups_members m
-                             WHERE m.groupid = '$groupid'
-                               AND m.userid = u.id $except
-                          ORDER BY $sort");
-}
-
-
-
-
-/**
- * Add a user to a group, return true upon success or if user already a group 
- * member
- *
- * @param int $groupid  The group id to add user to
- * @param int $userid   The user id to add to the group
- * @return bool
- */
-function add_user_to_group($groupid, $userid) {
-    return groups_add_member($groupid, $userid);
-}
-
-
-/**
- * Get the IDs for the user's groups in the given course.
- *
- * @uses $USER
- * @param int $courseid The course being examined - the 'course' table id field.
- * @return array An _array_ of groupids.
- * (Was return $groupids[0] - consequences!)
- */
-function mygroupid($courseid) {
-    global $USER;
-    $groupids = groups_get_groups_for_user($USER->id, $courseid);
-    return $groupids;
-}
-
-/**
- * Returns the current group mode for a given course or activity module
- * 
- * Could be false, SEPARATEGROUPS or VISIBLEGROUPS    (<-- Martin)
- */
-function groupmode($course, $cm=null) {
-
-    if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
-        return $cm->groupmode;
-    }
-    return $course->groupmode;
-}
-
-
-/**
- * Sets the current group in the session variable
- * When $SESSION->currentgroup[$courseid] is set to 0 it means, show all groups. 
- * Sets currentgroup[$courseid] in the session variable appropriately.
- * Does not do any permission checking. 
- * @uses $SESSION
- * @param int $courseid The course being examined - relates to id field in 
- * 'course' table.
- * @param int $groupid The group being examined.
- * @return int Current group id which was set by this function
- */
-function set_current_group($courseid, $groupid) {
-    global $SESSION;
-    return $SESSION->currentgroup[$courseid] = $groupid;
-}
-
-
-/**
- * Gets the current group - either from the session variable or from the database. 
- *
- * @uses $USER
- * @uses $SESSION
- * @param int $courseid The course being examined - relates to id field in 
- * 'course' table.
- * @param bool $full If true, the return value is a full record object. 
- * If false, just the id of the record.
- */
-function get_current_group($courseid, $full = false) {
-    global $SESSION;
-
-    if (isset($SESSION->currentgroup[$courseid])) {
-        if ($full) {
-            return groups_get_group($SESSION->currentgroup[$courseid], false);
-        } else {
-            return $SESSION->currentgroup[$courseid];
-        }
-    }
-
-    $mygroupid = mygroupid($courseid);
-    if (is_array($mygroupid)) {
-        $mygroupid = array_shift($mygroupid);
-        set_current_group($courseid, $mygroupid);
-        if ($full) {
-            return groups_get_group($mygroupid, false);
-        } else {
-            return $mygroupid;
-        }
-    }
-
-    if ($full) {
-        return false;
-    } else {
-        return 0;
-    }
-}
-
-
-/**
- * A combination function to make it easier for modules
- * to set up groups.
- *
- * It will use a given "groupid" parameter and try to use
- * that to reset the current group for the user.
- *
- * @uses VISIBLEGROUPS
- * @param course $course A {@link $COURSE} object
- * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
- * @param int $groupid Will try to use this optional parameter to
- *            reset the current group for the user
- * @return int|false Returns the current group id or false if error.
- */
-function get_and_set_current_group($course, $groupmode, $groupid=-1) {
-    //TODO: ?? groups_has_permission($userid, $groupingid, $courseid, $groupid, $permissiontype);
-
-    // Sets to the specified group, provided the current user has view permission 
-    if (!$groupmode) {   // Groups don't even apply
-        return false;
-    }
-
-    $currentgroupid = get_current_group($course->id);
-
-    if ($groupid < 0) {  // No change was specified
-        return $currentgroupid;
-    }
-
-    $context = get_context_instance(CONTEXT_COURSE, $course->id);
-    if ($groupid) {      // Try to change the current group to this groupid
-        if (groups_group_belongs_to_course($groupid, $course->id)) { // Exists  TODO:check.
-            if (has_capability('moodle/site:accessallgroups', $context)) {  // Sets current default group
-                $currentgroupid = set_current_group($course->id, $groupid);
-
-            } elseif ($groupmode == VISIBLEGROUPS) {
-                  // All groups are visible
-                //if (groups_is_member($group->id)){
-                    $currentgroupid = set_current_group($course->id, $groupid); //set this since he might post
-                /*)}else {
-                    $currentgroupid = $group->id;*/
-            } elseif ($groupmode == SEPARATEGROUPS) { // student in separate groups switching
-                if (groups_is_member($groupid)) { //check if is a member
-                    $currentgroupid = set_current_group($course->id, $groupid); //might need to set_current_group?
-                }
-                else {
-                    notify('You do not belong to this group! ('.$groupid.')', 'error');
-                }
-            }
-        }
-    } else { // When groupid = 0 it means show ALL groups
-        // this is changed, non editting teacher needs access to group 0 as well,
-        // for viewing work in visible groups (need to set current group for multiple pages)
-        if (has_capability('moodle/site:accessallgroups', $context)) { // Sets current default group
-            $currentgroupid = set_current_group($course->id, 0);
-
-        } else if ($groupmode == VISIBLEGROUPS) {  // All groups are visible
-            $currentgroupid = set_current_group($course->id, 0);
-        }
-    }
-
-    return $currentgroupid;
-}
-
-
-/**
- * A big combination function to make it easier for modules
- * to set up groups.
- *
- * Terminates if the current user shouldn't be looking at this group
- * Otherwise returns the current group if there is one
- * Otherwise returns false if groups aren't relevant
- *
- * @uses SEPARATEGROUPS
- * @uses VISIBLEGROUPS
- * @param course $course A {@link $COURSE} object
- * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
- * @param string $urlroot ?
- * @return int|false
- */
-function setup_and_print_groups($course, $groupmode, $urlroot) {
-
-    global $USER, $SESSION; //needs his id, need to hack his groups in session
-
-    $changegroup = optional_param('group', -1, PARAM_INT);
-
-    $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
-    if ($currentgroup === false) {
-        return false;
-    }
-
-    $context = get_context_instance(CONTEXT_COURSE, $course->id);
-
-    if ($groupmode == SEPARATEGROUPS and !$currentgroup and !has_capability('moodle/site:accessallgroups', $context)) {
-        //we are in separate groups and the current group is group 0, as last set.
-        //this can mean that either, this guy has no group
-        //or, this guy just came from a visible all forum, and he left when he set his current group to 0 (show all)
-
-        if ($usergroups = groups_get_all_groups($course->id, $USER->id)){
-            //for the second situation, we need to perform the trick and get him a group.
-            $first = reset($usergroups);
-            $currentgroup = get_and_set_current_group($course, $groupmode, $first->id);
-
-        } else {
-            //else he has no group in this course
-            print_heading(get_string('notingroup'));
-            print_footer($course);
-            exit;
-        }
-    }
-
-    if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
-
-        if ($groups = groups_get_all_groups($course->id)) {
-
-            echo '<div class="groupselector">';
-            print_group_menu($groups, $groupmode, $currentgroup, $urlroot, 1);
-            echo '</div>';
-        }
-
-    } else if ($groupmode == SEPARATEGROUPS and has_capability('moodle/course:view', $context)) {
-        //get all the groups this guy is in in this course
-        if ($usergroups = groups_get_all_groups($course->id, $USER->id)){
-            echo '<div class="groupselector">';
-            //print them in the menu
-            print_group_menu($usergroups, $groupmode, $currentgroup, $urlroot, 0);
-            echo '</div>';
-        }
-    }
-
-    return $currentgroup;
-
-}
-
-
-/**
- * Get an array of groups, as id => name.
- * Replaces, get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")
- * (For /user/index.php)
- */
-function groups_get_groups_names($courseid) {
-    $groupids = groups_db_get_groups($courseid);
-    if (! $groupids) {
-        return false;
-    }
-    $groups_names = array();
-    foreach ($groupids as $id) {
-        $groups_names[$id] = groups_get_group_name($id);
-    }
-//TODO: sort. SQL?
-    return $groups_names;
-}
-
-/**
- * Get the groups that a number of users are in.
- * (For block_quiz_results.php)
- */
-function groups_get_groups_users($userids, $courseid) {
-    global $CFG;
-    $groups_users = get_records_sql(
-        'SELECT gm.userid, gm.groupid, g.name FROM '.$CFG->prefix.'groups g LEFT JOIN '.$CFG->prefix.'groups_members gm ON g.id = gm.groupid '.
-        'WHERE g.courseid = '.$courseid.' AND gm.userid IN ('.implode(',', $userids).')'
-        );
-    return $groups_users;
-}
-
-?>
diff --git a/group/lib/utillib.php b/group/lib/utillib.php
deleted file mode 100644 (file)
index ee4826a..0000000
+++ /dev/null
@@ -1,256 +0,0 @@
-<?php
-/**
- * Utility functions for groups.
- *
- * Functions we need independent of groups about users and courses.
- * And groups utility/ user-interface functions.
- *
- * @copyright &copy; 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
- */
-require_once($CFG->libdir.'/moodlelib.php');
-
-
-/**********************************
- * Functions to get display names
- **********************************
- */
-
-
-/**
- * Gets the number of members of a group
- * @param int $groupid The group specified
- * @return int The number of members of the group
- */
-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_count_groups_in_grouping($groupingid, $courseid) {
-    if (GROUP_NOT_IN_GROUPING == $groupingid) {
-        $groupids = groups_get_groups_not_in_any_grouping($courseid);
-        
-        if ($groupids === false) {
-            return false;
-        }
-        
-        return count($groupids);
-    } elseif (GROUP_ANY_GROUPING == $groupingid) {
-        return count_records('groups', 'courseid', $courseid);
-    } else {
-        return count_records('groupings_groups', 'groupingid ', $groupingid);
-    }
-}
-
-
-/**
- * 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;
-    } 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 - 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) {
-    if ($groupname = groups_get_group_name($groupid)) {
-        $count = groups_count_group_members($groupid);
-        return "$groupname ($count)"; 
-    }
-    return false;
-}
-
-/**
- * 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. 
- * @param $groups array The array of group-like objects, only the $group->id member is required. 
- * @return array The array of group IDs, or false if an error occurred 
- */
-function groups_groups_to_groupids($groups) {
-    if (! $groups) {
-        return false;
-    }
-    $groupids = array();
-    foreach ($groups as $group) {
-        if (isset($group->id)) {
-            array_push($groupids, $group->id);
-        } else {
-            //Warn if there's no "groupid" member.
-            array_push($groupids, $group->groupid);
-        }
-    }
-    return $groupids;
-}
-
-
-/**
- * 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 the course ID for backwards compatibility.
- * @param $alldata Default false, or get complete record for group.
- * @return array Array of group objects INDEXED by group ID, with basic or all data.
- */
-function groups_groupids_to_groups($groupids, $courseid=false, $alldata=false) {
-    if (! $groupids) {
-        return false;
-    }
-    $groups = array();
-    foreach ($groupids as $id) {
-        $groups[$id] = groups_get_group_settings($id, $courseid, $alldata);
-    }
-    return $groups;
-}
-
-
-/**
- * Get a sorted array of group-id/display-name objects.
- * @param array $groupids Array of group IDs
- * @param bool $justnames Return names only as values, not objects. Needed
- *   for print_group_menu in weblib
- * @return array If $justnames is set, returns an array of id=>name. Otherwise
- *   returns an array without specific keys of objects containing id, name
- */
-function groups_groupids_to_group_names($groupids, $justnames=false) {
-    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;
-    }*/
-
-    if ($justnames) {
-        $namesonly = array();
-        foreach ($group_names as $id => $object) {
-            $namesonly[$object->id] = $object->name;
-        }
-        return $namesonly;
-    }
-
-    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;
-    }
-    $groupings = array();
-    foreach ($groupingids as $id) {
-        $groupings[] = groups_get_grouping_settings($id);
-    }
-    return $groupings;
-}
-
-/**
- * Gets the user object for a given userid. Can't find a function anywhere to 
- * 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
- */
-function groups_get_user($userid) {
-    return groups_db_get_user($userid);
-}
-
-
-/**
- * Gets the course ID for a given group.
- */
-function groups_get_course($groupid) {
-    $course_group = get_record('groups', 'id', $groupid);
-    if ($course_group) {
-        return $course_group->courseid;
-    }
-    return false;
-}
-
-
-?>
index b73521f701c93172ed12d7675b7db97e5f69398f..7bc3f9ad1d23f86da649a8e80f5ab7be168fbb30 100755 (executable)
@@ -549,7 +549,7 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
 
             case CONTEXT_GROUP:
                 // Find course.
-                $courseid = groups_get_course($context->instanceid);
+                $courseid = get_field('groups', 'courseid', 'id', $context->instanceid);
                 $courseinstance = get_context_instance(CONTEXT_COURSE, $courseid);
 
                 $parentcats = get_parent_cats($courseinstance, CONTEXT_COURSE);
@@ -708,7 +708,7 @@ function capability_search($capability, $context, $capabilities, $switchroleacti
         break;
 
         case CONTEXT_GROUP: // 1 to 1 to course
-            $courseid = groups_get_course($context->instanceid);
+            $courseid = get_field('groups', 'courseid', 'id', $context->instanceid);
             $parentcontext = get_context_instance(CONTEXT_COURSE, $courseid);
             $permission = capability_search($capability, $parentcontext, $capabilities, $switchroleactive);
         break;
@@ -1340,7 +1340,7 @@ function capability_prohibits($capability, $context, $sum='', $array='') {
 
         case CONTEXT_GROUP:
             // 1 to 1 to course.
-            if (!$courseid = groups_get_course($context->instanceid)) {
+            if (!$courseid = get_field('groups', 'courseid', 'id', $context->instanceid)) {
                 $prohibits[$capability][$context->id] = false;
                 return false;
             }
index e153f7f1b2f3f21e3a06ca549afee7087a0bb369..e2f34d1351b7a8e67bdaf59e2dc3baed87283839 100644 (file)
@@ -1144,4 +1144,269 @@ function ismember($groupid, $userid = null) {
     return groups_is_member($groupid, $userid);
 }
 
+/**
+ * Get the IDs for the user's groups in the given course.
+ *
+ * @uses $USER
+ * @param int $courseid The course being examined - the 'course' table id field.
+ * @return array An _array_ of groupids.
+ * (Was return $groupids[0] - consequences!)
+ */
+function mygroupid($courseid) {
+    global $USER;
+    if ($groups = groups_get_all_groups($courseid, $USER->id)) {
+        return array_keys($groups);
+    } else {
+        return false;
+    }
+}
+
+/**
+ * Add a user to a group, return true upon success or if user already a group 
+ * member
+ *
+ * @param int $groupid  The group id to add user to
+ * @param int $userid   The user id to add to the group
+ * @return bool
+ */
+function add_user_to_group($groupid, $userid) {
+    return groups_add_member($groupid, $userid);
+}
+
+
+/**
+ * Returns an array of user objects
+ *
+ * @uses $CFG
+ * @param int $groupid The group in question.
+ * @param string $sort ?
+ * @param string $exceptions ?
+ * @return object
+ * @todo Finish documenting this function
+ */
+function get_group_users($groupid, $sort='u.lastaccess DESC', $exceptions='', 
+                         $fields='u.*') {
+    global $CFG;
+    if (!empty($exceptions)) {
+        $except = ' AND u.id NOT IN ('. $exceptions .') ';
+    } else {
+        $except = '';
+    }
+    // in postgres, you can't have things in sort that aren't in the select, so...
+    $extrafield = str_replace('ASC','',$sort);
+    $extrafield = str_replace('DESC','',$extrafield);
+    $extrafield = trim($extrafield);
+    if (!empty($extrafield)) {
+        $extrafield = ','.$extrafield;
+    }
+    return get_records_sql("SELECT DISTINCT $fields $extrafield
+                              FROM {$CFG->prefix}user u,
+                                   {$CFG->prefix}groups_members m
+                             WHERE m.groupid = '$groupid'
+                               AND m.userid = u.id $except
+                          ORDER BY $sort");
+}
+
+/**
+ * Returns the current group mode for a given course or activity module
+ * 
+ * Could be false, SEPARATEGROUPS or VISIBLEGROUPS    (<-- Martin)
+ */
+function groupmode($course, $cm=null) {
+
+    if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
+        return $cm->groupmode;
+    }
+    return $course->groupmode;
+}
+
+
+/**
+ * Sets the current group in the session variable
+ * When $SESSION->currentgroup[$courseid] is set to 0 it means, show all groups. 
+ * Sets currentgroup[$courseid] in the session variable appropriately.
+ * Does not do any permission checking. 
+ * @uses $SESSION
+ * @param int $courseid The course being examined - relates to id field in 
+ * 'course' table.
+ * @param int $groupid The group being examined.
+ * @return int Current group id which was set by this function
+ */
+function set_current_group($courseid, $groupid) {
+    global $SESSION;
+    return $SESSION->currentgroup[$courseid] = $groupid;
+}
+
+
+/**
+ * Gets the current group - either from the session variable or from the database. 
+ *
+ * @uses $USER
+ * @uses $SESSION
+ * @param int $courseid The course being examined - relates to id field in 
+ * 'course' table.
+ * @param bool $full If true, the return value is a full record object. 
+ * If false, just the id of the record.
+ */
+function get_current_group($courseid, $full = false) {
+    global $SESSION;
+
+    if (isset($SESSION->currentgroup[$courseid])) {
+        if ($full) {
+            return groups_get_group($SESSION->currentgroup[$courseid]);
+        } else {
+            return $SESSION->currentgroup[$courseid];
+        }
+    }
+
+    $mygroupid = mygroupid($courseid);
+    if (is_array($mygroupid)) {
+        $mygroupid = array_shift($mygroupid);
+        set_current_group($courseid, $mygroupid);
+        if ($full) {
+            return groups_get_group($mygroupid);
+        } else {
+            return $mygroupid;
+        }
+    }
+
+    if ($full) {
+        return false;
+    } else {
+        return 0;
+    }
+}
+
+
+/**
+ * A combination function to make it easier for modules
+ * to set up groups.
+ *
+ * It will use a given "groupid" parameter and try to use
+ * that to reset the current group for the user.
+ *
+ * @uses VISIBLEGROUPS
+ * @param course $course A {@link $COURSE} object
+ * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
+ * @param int $groupid Will try to use this optional parameter to
+ *            reset the current group for the user
+ * @return int|false Returns the current group id or false if error.
+ */
+function get_and_set_current_group($course, $groupmode, $groupid=-1) {
+
+    // Sets to the specified group, provided the current user has view permission 
+    if (!$groupmode) {   // Groups don't even apply
+        return false;
+    }
+
+    $currentgroupid = get_current_group($course->id);
+
+    if ($groupid < 0) {  // No change was specified
+        return $currentgroupid;
+    }
+
+    $context = get_context_instance(CONTEXT_COURSE, $course->id);
+    if ($groupid and $group = get_record('groups', 'id', $groupid)) {      // Try to change the current group to this groupid
+        if ($group->courseid == $course->id) {
+            if (has_capability('moodle/site:accessallgroups', $context)) {  // Sets current default group
+                $currentgroupid = set_current_group($course->id, $groupid);
+
+            } elseif ($groupmode == VISIBLEGROUPS) {
+                  // All groups are visible
+                //if (groups_is_member($group->id)){
+                    $currentgroupid = set_current_group($course->id, $groupid); //set this since he might post
+                /*)}else {
+                    $currentgroupid = $group->id;*/
+            } elseif ($groupmode == SEPARATEGROUPS) { // student in separate groups switching
+                if (groups_is_member($groupid)) { //check if is a member
+                    $currentgroupid = set_current_group($course->id, $groupid); //might need to set_current_group?
+                }
+                else {
+                    notify('You do not belong to this group! ('.$groupid.')', 'error');
+                }
+            }
+        }
+    } else { // When groupid = 0 it means show ALL groups
+        // this is changed, non editting teacher needs access to group 0 as well,
+        // for viewing work in visible groups (need to set current group for multiple pages)
+        if (has_capability('moodle/site:accessallgroups', $context)) { // Sets current default group
+            $currentgroupid = set_current_group($course->id, 0);
+
+        } else if ($groupmode == VISIBLEGROUPS) {  // All groups are visible
+            $currentgroupid = set_current_group($course->id, 0);
+        }
+    }
+
+    return $currentgroupid;
+}
+
+
+/**
+ * A big combination function to make it easier for modules
+ * to set up groups.
+ *
+ * Terminates if the current user shouldn't be looking at this group
+ * Otherwise returns the current group if there is one
+ * Otherwise returns false if groups aren't relevant
+ *
+ * @uses SEPARATEGROUPS
+ * @uses VISIBLEGROUPS
+ * @param course $course A {@link $COURSE} object
+ * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
+ * @param string $urlroot ?
+ * @return int|false
+ */
+function setup_and_print_groups($course, $groupmode, $urlroot) {
+
+    global $USER, $SESSION; //needs his id, need to hack his groups in session
+
+    $changegroup = optional_param('group', -1, PARAM_INT);
+
+    $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
+    if ($currentgroup === false) {
+        return false;
+    }
+
+    $context = get_context_instance(CONTEXT_COURSE, $course->id);
+
+    if ($groupmode == SEPARATEGROUPS and !$currentgroup and !has_capability('moodle/site:accessallgroups', $context)) {
+        //we are in separate groups and the current group is group 0, as last set.
+        //this can mean that either, this guy has no group
+        //or, this guy just came from a visible all forum, and he left when he set his current group to 0 (show all)
+
+        if ($usergroups = groups_get_all_groups($course->id, $USER->id)){
+            //for the second situation, we need to perform the trick and get him a group.
+            $first = reset($usergroups);
+            $currentgroup = get_and_set_current_group($course, $groupmode, $first->id);
+
+        } else {
+            //else he has no group in this course
+            print_heading(get_string('notingroup'));
+            print_footer($course);
+            exit;
+        }
+    }
+
+    if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
+
+        if ($groups = groups_get_all_groups($course->id)) {
+
+            echo '<div class="groupselector">';
+            print_group_menu($groups, $groupmode, $currentgroup, $urlroot, 1);
+            echo '</div>';
+        }
+
+    } else if ($groupmode == SEPARATEGROUPS and has_capability('moodle/course:view', $context)) {
+        //get all the groups this guy is in in this course
+        if ($usergroups = groups_get_all_groups($course->id, $USER->id)){
+            echo '<div class="groupselector">';
+            //print them in the menu
+            print_group_menu($usergroups, $groupmode, $currentgroup, $urlroot, 0);
+            echo '</div>';
+        }
+    }
+
+    return $currentgroup;
+}
+
 ?>
index 243416b6c3fbc5c546e414d0e9273446aadb8c81..dc6fdc6f9e3fff5bd6814e8ca4053817f2c27aaa 100644 (file)
@@ -1,9 +1,86 @@
 <?php  //$Id$
 
-// folowing files will be removed soon
-require_once($CFG->dirroot.'/group/lib/basicgrouplib.php');
-require_once($CFG->dirroot.'/group/lib/utillib.php');
-require_once($CFG->dirroot.'/group/lib/legacylib.php');
+
+/**
+ * Adds a specified user to a group
+ * @param int $userid   The user id
+ * @param int $groupid  The group id
+ * @return boolean True if user added successfully or the user is already a 
+ * member of the group, false otherwise. 
+ */
+function groups_add_member($groupid, $userid) {
+    if (!groups_group_exists($groupid)) {
+        return false;
+    }
+
+    if (groups_is_member($groupid, $userid)) {
+        return true;
+    }
+
+    $member = new object();
+    $member->groupid   = $groupid;
+    $member->userid    = $userid;
+    $member->timeadded = time();
+
+    if (!insert_record('groups_members', $member)) {
+        return false;
+    }
+
+    //update group info
+    set_field('groups', 'timemodified', $member->timeadded, 'id', $groupid);
+
+    // MDL-9983
+    $eventdata = new object();
+    $eventdata -> groupid = $groupid;
+    $eventdata -> userid = $userid;
+    events_trigger('group_user_added', $eventdata);      
+
+    return true;
+}
+
+/**
+ * Deletes the link between the specified user and group.
+ * @param int $groupid The group to delete the user from
+ * @param int $userid The user to delete
+ * @return boolean True if deletion was successful, false otherwise
+ */
+function groups_remove_member($groupid, $userid) {
+    if (!groups_group_exists($groupid)) {
+        return false;
+    }
+
+    if (!groups_is_member($groupid, $userid)) {
+        return true;
+    }
+
+    if (!delete_records('groups_members', 'groupid', $groupid, 'userid', $userid)) {
+        return false;
+    }    
+    //update group info
+    set_field('groups', 'timemodified', time(), 'id', $groupid);
+
+    return true;
+}
+
+
+/**
+ * Determines if a group with a given groupid exists. 
+ * @param int $groupid The groupid to check for
+ * @return boolean True if the group exists, false otherwise or if an error 
+ * occurred. 
+ */
+function groups_group_exists($groupid) {
+    return record_exists('groups', 'id', $groupid);
+}
+
+/**
+ * Gets the name of a group with a specified id
+ * @param int $groupid The id of the group
+ * @return string The name of the group
+ */
+function groups_get_group_name($groupid) {
+    return get_field('groups', 'name', 'id', $groupid);
+}
 
 /**
  * Returns the groupid of a group with the name specified for the course.
index cc71ef4f55ceea46af8af3dce05adbd5bb422a14..34cee17a19d5431d6cb82cd289e9fae0465c72de 100644 (file)
@@ -2871,11 +2871,18 @@ function get_complete_user_data($field, $value, $mnethostid=null) {
         }
     }
 
-    if ($groupids = groups_get_all_groups_for_user($user->id)) { //TODO:check.
-        foreach ($groupids as $groupid) {
-            $courseid = groups_get_course($groupid);
-            //change this to 2D array so we can put multiple groups in a course
-            $user->groupmember[$courseid][] = $groupid;
+    $sql = "SELECT g.id, g.courseid
+              FROM {$CFG->prefix}groups g, {$CFG->prefix}groups_members gm
+             WHERE gm.groupid=g.id AND gm.userid={$user->id}";
+
+    // this is a special hack to speedup calendar display
+    $user->groupmember = array();
+    if ($groups = get_records_sql($sql)) {
+        foreach ($groups as $group) {
+            if (!array_key_exists($group->courseid, $user->groupmember)) {
+                $user->groupmember[$group->courseid] = array();
+            }
+            $user->groupmember[$group->courseid][$group->id] = $group->id;
         }
     }
 
@@ -3119,33 +3126,9 @@ function remove_course_contents($courseid, $showfeedback=true) {
     }
 
 /// Delete any groups, removing members and grouping/course links first.
-    //TODO: If groups or groupings are to be shared between courses, think again!
-    if ($groupids = groups_get_groups($course->id)) {
-        foreach ($groupids as $groupid) {
-            if (groups_remove_all_members($groupid)) {
-                if ($showfeedback) {
-                    notify($strdeleted .' groups_members');
-                }
-            } else {
-                $result = false;
-            }
-            /// Delete any associated context for this group ??
-            delete_context(CONTEXT_GROUP, $groupid);
-
-            if (groups_delete_group($groupid)) {
-                if ($showfeedback) {
-                    notify($strdeleted .' groups');
-                }
-            } else {
-                $result = false;
-            }
-        }
-    }
-/// Delete any groupings.
-    $result = groups_delete_all_groupings($course->id);
-    if ($result && $showfeedback) {
-        notify($strdeleted .' groupings');
-    }
+    require_once($CFG->dirroot.'/group/lib.php');
+    groups_delete_groupings($courseid, true);
+    groups_delete_groups($courseid, true);
 
 /// Delete all related records in other tables that may have a courseid
 /// This array stores the tables that need to be cleared, as
@@ -3222,6 +3205,7 @@ function remove_course_contents($courseid, $showfeedback=true) {
 function reset_course_userdata($data, $showfeedback=true) {
 
     global $CFG, $USER, $SESSION;
+    require_once($CFG->dirroot.'/group/lib.php');
 
     $result = true;
 
@@ -3261,18 +3245,8 @@ function reset_course_userdata($data, $showfeedback=true) {
                 notify($strdeleted .' '.get_string('students'), 'notifysuccess');
             }
 
-            /// Delete group members (but keep the groups) TODO:check.
-            if ($groupids = groups_get_groups($data->courseid)) {
-                foreach ($groupids as $groupid) {
-                    if (groups_remove_all_group_members($groupid)) {
-                        if ($showfeedback) {
-                            notify($strdeleted .' groups_members', 'notifysuccess');
-                        }
-                    } else {
-                        $result = false;
-                    }
-                }
-            }
+            /// Delete group members (but keep the groups)
+            $result = groups_delete_group_members($data->courseid, $showfeedback) && $result;
         }
 
         if (!empty($data->reset_teachers)) {
@@ -3286,17 +3260,8 @@ function reset_course_userdata($data, $showfeedback=true) {
     }
 
     if (!empty($data->reset_groups)) {
-        if ($groupids = groups_get_groups($data->courseid)) {
-            foreach ($groupids as $groupid) {
-                if (groups_delete_group($groupid)) {
-                    if ($showfeedback) {
-                        notify($strdeleted .' groups', 'notifysuccess');
-                    }
-                } else {
-                    $result = false;
-                }
-            }
-        }
+            $result = groups_delete_groupings($data->courseid, $showfeedback) && $result;
+            $result = groups_delete_groups($data->courseid, $showfeedback) && $result;
     }
 
     if (!empty($data->reset_events)) {
@@ -3326,18 +3291,6 @@ function reset_course_userdata($data, $showfeedback=true) {
     return $result;
 }
 
-
-require_once($CFG->dirroot.'/group/lib.php');
-/*TODO: functions moved to /group/lib/legacylib.php
-
-ismember
-add_user_to_group
-mygroupid
-groupmode
-set_current_group
-... */
-
-
 function generate_email_processing_address($modid,$modargs) {
     global $CFG;
 
index 3581353d28a9f5d3c6fc9b79863b9ee7dbc692be..39a602a7cf3417983280b6b3906f8d078661b4a4 100644 (file)
@@ -86,7 +86,7 @@
         $formdata['reportusers'] = 'group';
         $formdata['reportgroupid'] = 0;
         // validate groupid
-        if ($groups = groups_get_groups_names($course->id)) {
+        if ($groups = groups_get_all_groups($course->id)) {
             if (isset($groups[$matches[1]])) {
                 $formdata['reportgroupid'] = $matches[1];
             }
@@ -460,9 +460,9 @@ function hotpot_print_report_selector(&$course, &$hotpot, &$formdata) {
     );
 
     // groups
-    if ($groups = groups_get_groups_names($course->id)) { //TODO:check.
-        foreach ($groups as $gid => $gname) {
-            $menus['reportusers']["group$gid"] = get_string('group').': '.$gname;
+    if ($groups = groups_get_all_groups($course->id)) {
+        foreach ($groups as $gid => $group) {
+            $menus['reportusers']["group$gid"] = get_string('group').': '.format_string($group->name);
         }
     }
 
index daed4ac411b5ec7775baf55e9151b893da376a78..69eabbae7747f2331fd3ca89685ee8e5d3894998 100644 (file)
@@ -881,7 +881,7 @@ function quiz_send_notification_emails($course, $quiz, $attempt, $context, $cm)
     // check for notifications required
     $notifyfields = 'u.id, u.username, u.firstname, u.lastname, u.email, u.emailstop, u.lang, u.timezone, u.mailformat, u.maildisplay';
     $userstonotify = get_users_by_capability($context, 'mod/quiz:emailnotifysubmission',
-            $notifyfields, '', '', '', groups_m_get_groups_for_user($cm, $USER->id),
+            $notifyfields, '', '', '', groups_get_all_groups($course->id, $USER->id),
             $notifyexcludeusers, false, false, true);
 
     // if something to send, then build $a
index a9dfe031c1257c293d84afe36cf823f6ae7716df..ddf427e281dcede83c1b336c1a229a2493ea9b1c 100644 (file)
             require_login();
     
             ///this is changed because of mygroupid
-            $gtrue = (bool)groups_get_groups_for_user($user->id, $course->id);
-            /*TODO: $gtrue = false;
-            if ($mygroups = mygroupid($course->id)){
-                foreach ($mygroups as $group){
-                    if (groups_is_member($group, $user->id)){
-                        $gtrue = true;
-                    }
-                }
-            }*/
+            $gtrue = (bool)groups_get_all_groups($course->id, $user->id);
             if (!$gtrue) {
                 print_header("$strpersonalprofile: ", "$strpersonalprofile: ",
                              "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->