@@ -836,7 +836,7 @@ public function join($GroupID, $UserID, $watched = true, $followed = true ){
836
836
}
837
837
$ this ->followGroup ($ GroupID , $ UserID , $ followed );
838
838
$ this ->watchGroup ($ GroupID , $ UserID , $ watched );
839
-
839
+ self :: clearUserGroupCache ( $ UserID );
840
840
}
841
841
842
842
/**
@@ -860,7 +860,7 @@ public function accept($groupID, $userID){
860
860
}
861
861
862
862
/**
863
- * Returntur if user is a member of the group
863
+ * Return true if user is a member of the group
864
864
*
865
865
*/
866
866
public function isMemberOfGroup ($ userID , $ groupID ) {
@@ -882,6 +882,7 @@ public function isMemberOfGroup($userID, $groupID) {
882
882
* @throws Exception
883
883
*/
884
884
public function setRole ($ GroupID , $ MemberID , $ Role ){
885
+ self ::clearUserGroupCache ($ MemberID );
885
886
return $ this ->SQL ->update ('UserGroup ' )
886
887
->set ('Role ' , $ Role )
887
888
->where ('GroupID ' , $ GroupID )
@@ -897,10 +898,12 @@ public function setRole($GroupID, $MemberID, $Role){
897
898
* @return bool|Gdn_DataSet|object|string|void
898
899
*/
899
900
public function removeMember ($ GroupID , $ MemberID ){
900
- $ result = $ this ->unbookmarkGroupDiscussions ($ GroupID , $ MemberID );
901
+ $ this ->unbookmarkGroupDiscussions ($ GroupID , $ MemberID );
901
902
$ this ->unwatchGroup ($ GroupID , $ MemberID );
902
903
$ this ->unfollowGroup ($ GroupID , $ MemberID );
903
- return $ this ->SQL ->delete ('UserGroup ' , ['GroupID ' => $ GroupID , 'UserID ' => $ MemberID ]);
904
+ $ result = $ this ->SQL ->delete ('UserGroup ' , ['GroupID ' => $ GroupID , 'UserID ' => $ MemberID ]);
905
+ self ::clearUserGroupCache ($ MemberID );
906
+ return $ result ;
904
907
905
908
}
906
909
@@ -1124,25 +1127,33 @@ public function countOfMembers($groupId, $role = null){
1124
1127
* @return array|mixed|null
1125
1128
*/
1126
1129
public function memberOf ($ userID ){
1127
- $ sql = $ this ->SQL ;
1128
- $ result = $ sql ->select ('ug.Role, ug.GroupID ' )
1129
- ->from ('UserGroup ug ' )
1130
- ->where ('UserID ' , $ userID )
1131
- ->get ();
1132
- return $ result ->result ();
1130
+ $ key = 'UserGroup_ ' .$ userID ;
1131
+ $ result = Gdn::cache ()->get ($ key );
1132
+ if ($ result === Gdn_Cache::CACHEOP_FAILURE ) {
1133
+ $ sql = clone $ this ->SQL ;
1134
+ $ sql ->reset ();
1135
+ $ result = $ sql ->select ('ug.Role, ug.GroupID ' )
1136
+ ->from ('UserGroup ug ' )
1137
+ ->where ('UserID ' , $ userID )
1138
+ ->get ()->result ();
1139
+ Gdn::cache ()->store ($ key , $ result );
1140
+ return $ result ;
1141
+ } else {
1142
+ return $ result ;
1143
+ }
1133
1144
}
1134
1145
1135
1146
/**
1136
1147
* Get a group role
1137
1148
*/
1138
1149
public function getGroupRoleFor ($ userID , $ groupID ) {
1139
- $ sql = $ this ->SQL ;
1140
- $ result = $ sql -> select ( ' ug.Role ' )
1141
- -> from ( ' UserGroup ug ' )
1142
- -> where ( ' UserID ' , $ userID )
1143
- -> where ( ' GroupID ' , $ groupID )
1144
- -> get ()-> firstRow ();
1145
- return $ result ;
1150
+ $ groups = $ this ->memberOf ( $ userID ) ;
1151
+ foreach ( $ groups as $ group ) {
1152
+ if ( $ group -> GroupID == $ groupID ) {
1153
+ return $ group ;
1154
+ }
1155
+ }
1156
+ return false ;
1146
1157
}
1147
1158
1148
1159
/**
@@ -1857,4 +1868,18 @@ public function getRootGroupCategory($group){
1857
1868
1858
1869
return -1 ; //return Vanilla root
1859
1870
}
1871
+
1872
+ /**
1873
+ * Clear the cached UserGroup data for a specific user.
1874
+ *
1875
+ * @param int|null $userID The user to clear. Use `null` for the current user.
1876
+ */
1877
+ public static function clearUserGroupCache ($ userID = null ) {
1878
+ if ($ userID === null ) {
1879
+ $ userID = Gdn::session ()->UserID ;
1880
+ }
1881
+
1882
+ $ key = 'UserGroup_ ' .$ userID ;
1883
+ Gdn::cache ()->remove ($ key );
1884
+ }
1860
1885
}
0 commit comments