]> git.mjollnir.org Git - moodle.git/commitdiff
Fixes for Bug MDL-8617 "Implement groupings & course modules..."
authornfreear <nfreear>
Thu, 22 Feb 2007 15:27:30 +0000 (15:27 +0000)
committernfreear <nfreear>
Thu, 22 Feb 2007 15:27:30 +0000 (15:27 +0000)
group/db/dbmodulelib.php
group/lib.php
group/lib/groupinglib.php
group/lib/legacylib.php
group/lib/modulelib.php
group/lib/utillib.php

index 5ad604f87d7086f84eab6c347a1002196b24652e..2a97a7eeb84a03dd0ba88b646bdd3de377389346 100644 (file)
@@ -18,7 +18,7 @@
  * @param int  $cmid The id of the module instance.
  * @return int The grouping id (or false if it is null or an error occurred)
  */
-function groups_db_m_get_groupingid($cmid) {
+function groups___db_m_get_groupingid($cmid) {
        // @@@ Check nulls are turned into false
        $query = get_record('course_modules', 'groupingid', $userid);
        return $query;
index d01b0217db38b65547c7c6795d7132f7b78714d8..9cd645db465029c5e485cb3c6822ba73c5c4bc80 100644 (file)
@@ -14,8 +14,8 @@ require_once($CFG->dirroot.'/group/lib/groupinglib.php');
 
 require_once($CFG->dirroot.'/group/lib/utillib.php');
 
-require_once($CFG->dirroot.'/group/lib/automaticgroupinglib.php');
+require_once($CFG->dirroot.'/group/lib/modulelib.php');
 
 require_once($CFG->dirroot.'/group/lib/legacylib.php');
 
-?>
+?>
\ No newline at end of file
index f735ec164079b998133c14c38ab042e7382b1fc6..13cf0e93f78cde6bf3a85470fb3c3bc279cab37a 100644 (file)
@@ -136,7 +136,7 @@ function groups_get_groups_for_user_in_grouping($userid, $groupingid) {
     $records = get_records_sql($sql);
 
 //print_object($records);
-    return groups_groups_to_groupids($records); //TODO:check.
+    return groups_groups_to_groupids($records);
 }
 
 /**
index 514c453e95e504711a3245ac20e104107d2ae882..5bdaec48892d757fb1075edbe89b921a0a6fe341 100644 (file)
@@ -156,7 +156,7 @@ function mygroupid($courseid) {
  */
 function groupmode($course, $cm=null) {
 
-    if ($cm and !$course->groupmodeforce) {
+    if (is_object($cm) && isset($cm->groupmode) && !isset($course->groupmodeforce)) {
         return $cm->groupmode;
     }
     return $course->groupmode;
index 22ec458748282ae276c7d647db9400a5b2ca91fc..1634a2b0bc1e9ba1d232ecadc5d048e0c1d90341 100644 (file)
@@ -1,9 +1,5 @@
 <?php
-
-// @@@ TO DO Still tons to do here! Most of this isn't done or just copied
-// and pasted so far and needs to be sorted out. 
-
-/*******************************************************************************
+/**
  * modulelib.php
  * 
  * This file contains functions to be used by modules to support groups. More
  * 
  * For queries, suggestions for improvements etc. please post on the Groups 
  * forum on the moodle.org site.
- ******************************************************************************/
+ *
+ * @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
+ */ 
 
-/*******************************************************************************
- * Permission types
+/*
+ * (OLD) Permission types
  * 
  * There are six types of permission that a user can hold for a particular
  * group - 'student view', 'student contribute', 'teacher view', 
@@ -50,7 +51,7 @@
  * @name GROUPS_STUDENT_CONTRIBUTE 'student contribute' permission
  * @name GROUPS_TEACHER_VIEW 'teacher view' permission
  * @name GROUPS_TEACHER_CONTRIBUTE 'teacher contribute' permission
- ******************************************************************************/
+ */
 define('GROUPS_STUDENT', 1);
 define('GROUPS_TEACHER', 2);
 define('GROUPS_VIEW', 4);
@@ -147,31 +148,44 @@ function groups_m_get_selected_group($cmid, $permissiontype, $userid) {
 }
         
 /**
- * Gets an array of the groupids of all the groups that the specified user has
- * particular permission for in this particular instance of the module
+ * Gets an array of the group IDs of all groups for the user in this course module.
  * @uses $USER     
- * @param int $cmid The id of the module instance
- * @param int $permissiontype The permission type - see note on permission types 
- * above
- * @param int $userid The id of the user, defaults to the current user 
- * @return array An array of the group ids 
- */    
-function groups_m_get_groups_for_user($cmid, $permissiontype, $userid) {
-       $groupingid = groups_db_get_groupingid($cmid);
-       $groupids = groups_get_groups_in_grouping($groupingid);
-       if (!$groupids) {
-               $groupidsforuser = false;
-       } else {
-               $groupidsforuser = array();
-               foreach($groupids as $groupid) {
-                       if (groups_m_has_permission($cmid, $groupid, $permissiontype, $userid) ) {
-                               array_push($groupidsforuser, $groupid);
-                       }
-               }
-       }
-       
-       return $groupidsforuser;
-}  
+ * @param object $cm The course-module object.
+ * @param int $userid The ID of the user.
+ * @return array An array of group IDs, or false. 
+ */
+function groups_m_get_groups_for_user($cm, $userid) {
+//echo 'User'; print_object($cm);
+    $groupingid = groups_get_grouping_for_coursemodule($cm);
+    if (!$groupingid) {
+        return false;
+    }
+    if (!isset($cm->course) || !groupmode($cm->course, $cm)) {
+        return false;
+    }
+    elseif (GROUP_ANY_GROUPING == $groupingid) {
+        return groups_get_groups_for_user($userid, $cm->course);
+    }
+    return groups_get_groups_for_user_in_grouping($userid, $groupingid);
+} 
+
+
+/**
+ * Get the ID of the first group for the global $USER in the course module.
+ * Replaces legacylib function 'mygroupid'.
+ * @uses $USER
+ * @param $cm A course module object.
+ * @return int A single group ID for this user.
+ */ 
+function groups_m_get_my_group($cm) {
+    global $USER;
+    $groupids = groups_m_get_groups_for_user($cm, $USER->id);
+    if (!$groupids || count($groupids) == 0) {
+        return 0;
+    }
+    return array_shift($groupids);
+}
+
 
 /**
  * Indicates if a specified user has a particular type of permission for a 
@@ -187,9 +201,17 @@ function groups_m_get_groups_for_user($cmid, $permissiontype, $userid) {
  * @return boolean True if the user has the specified permission type, false 
  * otherwise or if an error occurred. 
  */
- function groups_m_has_permission($cmid, $groupid, $permissiontype, $userid) {
-       $groupingid = groups_get_grouping_for_coursemodule($coursemoduleid);
-       $isstudent = isstudent($courseid, $userid);
+ function groups_m_has_permission($cm, $groupid, $permissiontype, $userid = null) {
+    if (!$userid) {
+        global $USER;
+        $userid = $USER->id;
+    }
+       $groupingid = groups_get_grouping_for_coursemodule($cm);
+       if (!$groupingid || !is_object($cm) || !isset($cm->course)) {
+        return false;
+    }
+    $courseid = $cm->course;
+    $isstudent = isstudent($courseid, $userid);
        $isteacher = isteacher($courseid, $userid);
        $groupmember = groups_is_member($groupid, $userid);
        $memberofsomegroup = groups_is_member_of_some_group_in_grouping($userid, $groupingid);
@@ -264,7 +286,7 @@ function groups_m_get_members_with_permission($cmid, $groupid,
                                               $permissiontype) {
        // Get all the users as $userid
        $validuserids = array();        
-       foreach($userids as $userid) {
+       foreach($validuserids as $userid) {
                $haspermission = groups_m_has_permission($cmid, $groupid, 
                                                                                $permissiontype, $userid);
                if ($haspermission) {
@@ -313,88 +335,7 @@ function groups_m_get_members($cmid, $groupid) {
        } else {
                // Check if each user is enrolled on the course @@@ TO DO 
        }
-}
-
-/**
- * Indicates if a user is a member of a particular group. In general you should 
- * use groups_m_has_permission, however this function is provided for 
- * circumstances where this function isn't sufficient for some reason. 
- * @param int $groupid The id of the group
- * @param int $userid The id of the user 
- * @return boolean True if the user is a member of the group, false otherwise 
- */
-function groups_m_is_member($groupid, $userid) {
-       return groups_is_member($groupid, $userid);
-}
-
-/**
- * Determines if a user has a particular permission type for a particular group. Permissions
- * are set at grouping level and are set via the grouping settings. This is the function
- * to check, if somebody can't access something that you think they ought to be able to!
- * This is for the current user. What about for general user?
- * @param int $courseid The id of the course
- * @param int $groupid The id of the group
- * @param string $permissiontype The permission type: 'view', 'studentcontribute', 
- * 'teacherview', 'teachercontribute', 'viewmembers', 'contribute', 'all'
- */
-function groups_m_has_permission($courseid, $groupid, $permissiontype) {
-       $userid = $USER->id;
-       $groupingid = groups_get_grouping_for_coursemodule($coursemoduleid);
-       $isstudent = isstudent($courseid, $userid);
-       $isteacher = isteacher($courseid, $userid);
-       $groupmember = groups_is_member($groupid, $userid);
-       $memberofsomegroup = groups_is_member_of_some_group_in_grouping($userid, $groupingid);
-       
-       $groupingsettings = groups_get_grouping_settings($groupingid);
-       $viewowngroup = $groupingsettings->viewowngroup;
-       $viewallgroupsmembers = $groupingsettings->viewallgroupmembers;
-       $viewallgroupsactivities = $groupingsettings->viewallgroupsactivities;
-       $teachersgroupsmark = $groupingsettings->teachersgroupsmark;
-       $teachersgroupsview = $groupingsettings->teachersgroupsview;
-       $teachersgroupmark = $groupingsettings->teachersgroupmark;
-       $teachersgroupview = $groupingsettings->teachersgroupview;
-       $teachersoverride = $groupingsettings->teachersoverride;
-               
-       $permission = false;
-       
-       switch ($permissiontype) {
-               case 'view':
-                       if (($isstudent and $groupmember) or 
-                           ($isteacher and $groupmember) or 
-                           ($isstudent and $viewallgroupsactivities) or 
-                           ($isteacher and !$teachersgroupview) or 
-                           ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-                               $permission = true;
-                       } 
-                       break;
-                       
-               case 'studentcontribute':
-                       if (($isstudent and $groupmember) or 
-                           ($isteacher and $groupmember) or 
-                           ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-                               $permission = true;
-                       } 
-                       break;
-               case 'teachermark':
-                       if (($isteacher and $groupmember) or 
-                               ($isteacher and !$teachersgroupmark) or
-                           ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-                               $permission = true;
-                       }  
-                       break;
-               
-               case 'viewmembers':     
-                       if (($isstudent and $groupmember and $viewowngroup) or 
-                           ($isstudent and $viewallgroupsmembers) or 
-                               ($isteacher and $groupmember) or 
-                           ($isteacher and !$teachersgroupview) or 
-                           ($isteacher and !$memberofsomegroup and $teachersoverride) or 
-                           $isteacheredit) {
-                               $permission = true;
-                       }  
-                       break;
-       }
-       return $permission;     
+    return $memberids;
 }
 
 ?>
\ No newline at end of file
index 300dfa052804de664cc0c6b1828612c918b87315..19dc2c3c0a2dcd00b395e57464f63a46dab24529 100644 (file)
@@ -40,6 +40,8 @@ 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);
+    } elseif (GROUP_ANY_GROUPING == $groupingid) {
+        return count_records('groups_courses_groups', 'courseid', $courseid);
     } else {
         return count_records('groups_groupings_groups', 'groupingid ', $groupingid);
     }
@@ -154,7 +156,12 @@ function groups_groups_to_groupids($groups) {
     }
     $groupids = array();
        foreach ($groups as $group) {
-               array_push($groupids, $group->id);
+        if (isset($group->id)) {
+                   array_push($groupids, $group->id);
+        } else {
+            //Warn if there's no "groupid" member.
+            array_push($groupids, $group->groupid);
+        }
        }
        return $groupids;
 }