@@ -861,7 +861,9 @@ public function accept($groupID, $userID){
861
861
862
862
/**
863
863
* Return true if user is a member of the group
864
- *
864
+ * @param $userID
865
+ * @param $groupID
866
+ * @return bool
865
867
*/
866
868
public function isMemberOfGroup ($ userID , $ groupID ) {
867
869
$ groups = $ this ->memberOf ($ userID );
@@ -1188,32 +1190,22 @@ public function canFollowGroup($group) {
1188
1190
}
1189
1191
1190
1192
/**
1191
- * Check if the current user has followed at least one group category
1192
- *
1193
+ * Check if a user has followed at least one group category
1194
+ * @param $group
1195
+ * @param null $userID if userID is not set, the current user is used
1196
+ * @return bool
1193
1197
*/
1194
- public function hasFollowedGroup ($ group ) {
1195
- if ($ group ->ChallengeID ) {
1196
- $ categoryModel = new CategoryModel ();
1197
- $ groupCategory = $ categoryModel ->getByCode ($ group ->ChallengeID );
1198
-
1199
- if ($ groupCategory ->DisplayAs !== 'Discussions ' ) {
1200
- $ categories = CategoryModel::getSubtree ($ groupCategory ->CategoryID , false );
1201
- $ categoryIDs = array_column ($ categories , 'CategoryID ' );
1202
- } else {
1203
- $ categoryIDs = [$ groupCategory ->CategoryID ];
1204
- }
1205
-
1206
- $ where ['Followed ' ] = true ;
1207
- $ where ['CategoryID ' ] = $ categoryIDs ;
1208
-
1209
- $ result = $ categoryModel
1210
- ->getWhere ($ where )
1211
- ->resultArray ();
1212
-
1213
- return count ($ result ) > 0 ;
1198
+ public function hasFollowedGroup ($ group , $ userID = null ) {
1199
+ if (!$ userID ) {
1200
+ $ userID = Gdn::session ()->UserID ;
1214
1201
}
1215
1202
1216
- return false ;
1203
+ $ categories = Gdn::sql ()->getWhere ('Category ' , ['GroupID ' => $ group ->GroupID , 'DisplayAs ' => 'Discussions ' ])->resultArray ();
1204
+ $ categoryIDs = array_column ($ categories , 'CategoryID ' );
1205
+ $ result = Gdn::sql ()->getWhere ('UserCategory ' , ['UserID ' => $ userID , 'Followed ' => 1 , 'CategoryID ' => $ categoryIDs ])
1206
+ ->resultArray ();
1207
+
1208
+ return count ($ result ) > 0 ;
1217
1209
}
1218
1210
1219
1211
/**
@@ -1230,26 +1222,19 @@ public function canWatchGroup($group) {
1230
1222
}
1231
1223
1232
1224
/**
1233
- * Check if the current user has watched at least one group category
1225
+ * Check if an user has watched at least one group category
1234
1226
* @param $group
1227
+ * @param null $userID if userID is not set then the current user is used
1235
1228
* @return bool User has watched at least one group category
1236
1229
*/
1237
- public function hasWatchedGroup ($ group ) {
1238
- if ($ group ->ChallengeID ) {
1239
- $ categoryModel = new CategoryModel ();
1240
- $ groupCategory = $ categoryModel ->getByCode ($ group ->ChallengeID );
1241
-
1242
- if ($ groupCategory ->DisplayAs !== 'Discussions ' ) {
1243
- $ categories = CategoryModel::getSubtree ($ groupCategory ->CategoryID , false );
1244
- $ categoryIDs = array_column ($ categories , 'CategoryID ' );
1245
- } else {
1246
- $ categoryIDs = [$ groupCategory ->CategoryID ];
1247
- }
1248
-
1249
- return $ categoryModel ->hasWatched ($ categoryIDs ,Gdn::session ()->UserID );
1230
+ public function hasWatchedGroup ($ group , $ userID = null ) {
1231
+ if (!$ userID ) {
1232
+ $ userID = Gdn::session ()->UserID ;
1250
1233
}
1251
-
1252
- return false ;
1234
+ $ categoryModel = new CategoryModel ();
1235
+ $ categories = Gdn::sql ()->getWhere ('Category ' , ['GroupID ' => $ group ->GroupID , 'DisplayAs ' => 'Discussions ' ])->resultArray ();
1236
+ $ categoryIDs = array_column ($ categories , 'CategoryID ' );
1237
+ return $ categoryModel ->hasWatched ($ categoryIDs ,$ userID );
1253
1238
}
1254
1239
1255
1240
/**
@@ -1262,20 +1247,19 @@ public function followGroup($group, $userID, $followed = true ) {
1262
1247
if (is_numeric ($ group ) && $ group > 0 ) {
1263
1248
$ group = $ this ->getByGroupID ($ group );
1264
1249
}
1265
- if ($ group ->ChallengeID ) {
1266
- $ categories = Gdn::sql ()->getWhere ('Category ' , ['GroupID ' => $ group ->GroupID , 'DisplayAs ' => 'Discussions ' ])->resultArray ();
1267
- $ categoryIDs = array_column ($ categories , 'CategoryID ' );
1268
1250
1269
- foreach ( $ categoryIDs as $ categoryID ) {
1270
- $ this -> SQL -> replace (
1271
- ' UserCategory ' ,
1272
- [ ' Followed ' => ( int ) $ followed ],
1273
- [ ' UserID ' => $ userID , ' CategoryID ' => $ categoryID ]
1274
- );
1275
- }
1276
- CategoryModel:: clearUserCache ( $ userID);
1277
- Gdn:: cache ()-> remove ( " Follow_ { $ userID }" );
1251
+ $ categories = Gdn:: sql ()-> getWhere ( ' Category ' , [ ' GroupID ' => $ group -> GroupID , ' DisplayAs ' => ' Discussions ' ])-> resultArray ();
1252
+ $ categoryIDs = array_column ( $ categories , ' CategoryID ' );
1253
+
1254
+ foreach ( $ categoryIDs as $ categoryID ) {
1255
+ $ this -> SQL -> replace (
1256
+ ' UserCategory ' ,
1257
+ [ ' Followed ' => ( int ) $ followed ],
1258
+ [ ' UserID ' => $ userID, ' CategoryID ' => $ categoryID ]
1259
+ );
1278
1260
}
1261
+ CategoryModel::clearUserCache ($ userID );
1262
+ Gdn::cache ()->remove ("Follow_ {$ userID }" );
1279
1263
}
1280
1264
1281
1265
/**
@@ -1298,33 +1282,32 @@ public function watchGroup($group, $userID, $watched = true) {
1298
1282
$ group = $ this ->getByGroupID ($ group );
1299
1283
}
1300
1284
1301
- if ($ group ->ChallengeID ) {
1302
- $ categories = Gdn::sql ()->getWhere ('Category ' , ['GroupID ' => $ group ->GroupID , 'DisplayAs ' => 'Discussions ' ])->resultArray ();
1303
- $ categoryIDs = array_column ($ categories , 'CategoryID ' );
1304
- // Don't use setCategoryMetaData due to cache
1305
- $ metaKeys = ['Preferences.Email.NewComment. ' ,
1306
- 'Preferences.Email.NewDiscussion. ' ,
1307
- 'Preferences.Popup.NewComment. ' ,
1308
- 'Preferences.Popup.NewDiscussion. ' ];
1309
- foreach ($ categoryIDs as $ categoryID ) {
1310
- foreach ($ metaKeys as $ metaKey ) {
1311
- if ($ watched ) {
1312
- $ this ->SQL ->replace (
1313
- 'UserMeta ' ,
1314
- [ 'Value ' => (int )$ watched ],
1315
- [ 'UserID ' => $ userID , 'Name ' => $ metaKey . $ categoryID ,]
1316
- );
1317
- } else {
1318
- Gdn::sql ()->delete ('UserMeta ' , [
1319
- 'UserID ' => $ userID ,
1320
- 'Name ' => $ metaKey . $ categoryID
1321
- ]);
1322
- }
1285
+ $ categories = Gdn::sql ()->getWhere ('Category ' , ['GroupID ' => $ group ->GroupID , 'DisplayAs ' => 'Discussions ' ])->resultArray ();
1286
+ $ categoryIDs = array_column ($ categories , 'CategoryID ' );
1287
+ // Don't use setCategoryMetaData due to cache
1288
+ $ metaKeys = ['Preferences.Email.NewComment. ' ,
1289
+ 'Preferences.Email.NewDiscussion. ' ,
1290
+ 'Preferences.Popup.NewComment. ' ,
1291
+ 'Preferences.Popup.NewDiscussion. ' ];
1292
+ foreach ($ categoryIDs as $ categoryID ) {
1293
+ foreach ($ metaKeys as $ metaKey ) {
1294
+ if ($ watched ) {
1295
+ $ this ->SQL ->replace (
1296
+ 'UserMeta ' ,
1297
+ [ 'Value ' => (int )$ watched ],
1298
+ [ 'UserID ' => $ userID , 'Name ' => $ metaKey . $ categoryID ,]
1299
+ );
1300
+ } else {
1301
+ Gdn::sql ()->delete ('UserMeta ' , [
1302
+ 'UserID ' => $ userID ,
1303
+ 'Name ' => $ metaKey . $ categoryID
1304
+ ]);
1323
1305
}
1324
1306
}
1325
- CategoryModel::clearUserCache ($ userID );
1326
- Gdn::cache ()->remove ("UserMeta_ {$ userID }" );
1327
1307
}
1308
+ CategoryModel::clearUserCache ($ userID );
1309
+ Gdn::cache ()->remove ("UserMeta_ {$ userID }" );
1310
+
1328
1311
}
1329
1312
1330
1313
/**
0 commit comments