Passed
Push — gcconnex ( a660c9...3f6bf9 )
by
unknown
46s
created

group.php ➔ get_discussions()   C

Complexity

Conditions 11
Paths 152

Size

Total Lines 65
Code Lines 44

Duplication

Lines 31
Ratio 47.69 %

Importance

Changes 0
Metric Value
cc 11
eloc 44
nc 152
nop 5
dl 31
loc 65
rs 5.5833
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Exposes API endpoints for Group entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.group",
8
	"get_group",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
13
	),
14
	'Retrieves a group based on user id and group id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"get.groups",
22
	"get_groups",
23
	array(
24
		"user" => array('type' => 'string', 'required' => true),
25
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
26
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
27
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
28
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
29
	),
30
	'Retrieves groups based on user id',
31
	'POST',
32
	true,
33
	false
34
);
35
36
elgg_ws_expose_function(
37
	"get.discussion",
38
	"get_discussion_post",
39
	array(
40
		"user" => array('type' => 'string', 'required' => true),
41
		"guid" => array('type' => 'int', 'required' => true),
42
		"thread" => array('type' => 'int', 'required' => false, 'default' => 0),
43
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
44
	),
45
	'Retrieves a discussion based on user id and discussion id',
46
	'POST',
47
	true,
48
	false
49
);
50
51
elgg_ws_expose_function(
52
	"get.discussions",
53
	"get_discussions",
54
	array(
55
		"user" => array('type' => 'string', 'required' => true),
56
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
57
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
58
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
59
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
60
	),
61
	'Retrieves discussions based on user id',
62
	'POST',
63
	true,
64
	false
65
);
66
67
function get_group( $user, $guid, $lang ){
68
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
69
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
70
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
71
72
	$entity = get_entity( $guid );
73
	if( !$entity ) return "Group was not found. Please try a different GUID";
74
	if( !$entity instanceof ElggGroup ) return "Invalid group. Please try a different GUID";
75
76
	if( !elgg_is_logged_in() )
77
		login($user_entity);
78
	
79
	$groups = elgg_list_entities(array(
80
		'type' => 'group',
81
		'guid' => $guid
82
	));
83
	$group = json_decode($groups)[0];
84
	
85
	$group->name = gc_explode_translation($group->name, $lang);
86
87
	$likes = elgg_get_annotations(array(
88
		'guid' => $group->guid,
89
		'annotation_name' => 'likes'
90
	));
91
	$group->likes = count($likes);
92
93
	$liked = elgg_get_annotations(array(
94
		'guid' => $group->guid,
95
		'annotation_owner_guid' => $user_entity->guid,
96
		'annotation_name' => 'likes'
97
	));
98
	$group->liked = count($liked) > 0;
99
100
	$group->comments = get_entity_comments($group->guid);
101
	
102
	$group->userDetails = get_user_block($group->owner_guid, $lang);
103
	$group->description = clean_text(gc_explode_translation($group->description, $lang));
104
105
	return $group;
106
}
107
108
function get_groups( $user, $limit, $offset, $filters, $lang ){
109
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
110
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
111
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
112
113
	if( !elgg_is_logged_in() )
114
		login($user_entity);
115
116
	$filter_data = json_decode($filters);
117
	if( !empty($filter_data) ){
118
		$params = array(
119
	        'type' => 'group',
120
			'limit' => $limit,
121
	        'offset' => $offset
122
		);
123
124
		if( $filter_data->mine ){
125
			$params['relationship'] = 'member';
126
			$params['relationship_guid'] = $user_entity->guid;
127
			$params['inverse_relationship'] = FALSE;
128
		}
129
130
		if( $filter_data->name ){
131
			$db_prefix = elgg_get_config('dbprefix');
132
        	$params['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid");
133
			$params['wheres'] = array("(ge.name LIKE '%" . $filter_data->name . "%' OR ge.description LIKE '%" . $filter_data->name . "%')");
134
        }
135
136
        if( $filter_data->mine ){
137
	    	$all_groups = elgg_list_entities_from_relationship($params);
138
        } else {
139
	    	$all_groups = elgg_list_entities_from_metadata($params);
140
        }
141
	} else {
142
		$all_groups = elgg_list_entities(array(
143
	        'type' => 'group',
144
	        'limit' => $limit,
145
	        'offset' => $offset
146
	    ));
147
	}
148
	
149
	$groups = json_decode($all_groups);
150
151
	foreach($groups as $group){
152
		$group->name = gc_explode_translation($group->name, $lang);
153
154
		$likes = elgg_get_annotations(array(
155
			'guid' => $group->guid,
156
			'annotation_name' => 'likes'
157
		));
158
		$group->likes = count($likes);
159
160
		$liked = elgg_get_annotations(array(
161
			'guid' => $group->guid,
162
			'annotation_owner_guid' => $user_entity->guid,
163
			'annotation_name' => 'likes'
164
		));
165
		$group->liked = count($liked) > 0;
166
167
		$groupObj = get_entity($group->guid);
168
		$group->member = $groupObj->isMember($user_entity);
169
		$group->owner = ($groupObj->getOwnerEntity() == $user_entity);
170
		$group->iconURL = $groupObj->geticon();
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::getIcon() has been deprecated with message: 1.8 Use getIconURL()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
171
		$group->count = $groupObj->getMembers(array('count' => true));
172
173
		$group->userDetails = get_user_block($group->owner_guid, $lang);
174
		$group->description = clean_text(gc_explode_translation($group->description, $lang));
175
	}
176
177
	return $groups;
178
}
179
180
function get_discussion_post( $user, $guid, $thread, $lang ){
181
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
182
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
183
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
184
185
	$entity = get_entity( $guid );
186
	if( !$entity ) return "Discussion was not found. Please try a different GUID";
187
	if( !elgg_instanceof($entity, "object", "groupforumtopic") ) return "Invalid discussion. Please try a different GUID";
188
189
	if( !elgg_is_logged_in() )
190
		login($user_entity);
191
	
192
	$discussions = elgg_list_entities(array(
193
		'type' => 'object',
194
	    'subtype' => 'groupforumtopic',
195
		'guid' => $guid
196
	));
197
	$discussion = json_decode($discussions)[0];
198
	
199
	$discussion->name = gc_explode_translation($discussion->name, $lang);
200
201
	$likes = elgg_get_annotations(array(
202
		'guid' => $discussion->guid,
203
		'annotation_name' => 'likes'
204
	));
205
	$discussion->likes = count($likes);
206
207
	$liked = elgg_get_annotations(array(
208
		'guid' => $discussion->guid,
209
		'annotation_owner_guid' => $user_entity->guid,
210
		'annotation_name' => 'likes'
211
	));
212
	$discussion->liked = count($liked) > 0;
213
214
	$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
215
	$discussion->description = clean_text(gc_explode_translation($discussion->description, $lang));
216
217
	$discussionsArray = array();
218
	$discussionsArray[] = $discussion;
219
220
	if( $thread ){
221
		$all_replies = elgg_list_entities_from_metadata(array(
222
			'type' => 'object',
223
		    'subtype' => 'discussion_reply',
224
			'container_guid' => $guid
225
		));
226
		$replies = json_decode($all_replies);
227
		$replies = array_reverse($replies);
228
229
		foreach($replies as $reply){
230
			$discussionsArray[] = $reply;
231
		}
232
	}
233
234
	return $discussionsArray;
235
}
236
237
function get_discussions( $user, $limit, $offset, $filters, $lang ){
238
	$user_entity = is_numeric($user) ? get_user($user) : ( strpos($user, '@') !== FALSE ? get_user_by_email($user)[0] : get_user_by_username($user) );
239
 	if( !$user_entity ) return "User was not found. Please try a different GUID, username, or email address";
240
	if( !$user_entity instanceof ElggUser ) return "Invalid user. Please try a different GUID, username, or email address";
241
242
	if( !elgg_is_logged_in() )
243
		login($user_entity);
244
245
	$filter_data = json_decode($filters);
246 View Code Duplication
	if( !empty($filter_data) ){
247
		$params = array(
248
	        'type' => 'object',
249
	        'subtype' => 'groupforumtopic',
250
			'limit' => $limit,
251
	        'offset' => $offset
252
		);
253
254
		if( $filter_data->mine ){
255
			$params['owner_guid'] = $user_entity->guid;
256
		}
257
258
		if( $filter_data->name ){
259
			$db_prefix = elgg_get_config('dbprefix');
260
        	$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
261
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
262
        }
263
264
        if( $filter_data->mine ){
265
	    	$all_discussions = elgg_list_entities_from_relationship($params);
266
        } else {
267
	    	$all_discussions = elgg_list_entities_from_metadata($params);
268
        }
269
	} else {
270
		$all_discussions = elgg_list_entities(array(
271
	        'type' => 'object',
272
	        'subtype' => 'groupforumtopic',
273
	        'limit' => $limit,
274
	        'offset' => $offset
275
	    ));
276
	}
277
	
278
	$discussions = json_decode($all_discussions);
279
280
	foreach($discussions as $discussion){
281
		$discussion->name = gc_explode_translation($discussion->name, $lang);
282
283
		$likes = elgg_get_annotations(array(
284
			'guid' => $discussion->guid,
285
			'annotation_name' => 'likes'
286
		));
287
		$discussion->likes = count($likes);
288
289
		$liked = elgg_get_annotations(array(
290
			'guid' => $discussion->guid,
291
			'annotation_owner_guid' => $user_entity->guid,
292
			'annotation_name' => 'likes'
293
		));
294
		$discussion->liked = count($liked) > 0;
295
296
		$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
297
		$discussion->description = clean_text(gc_explode_translation($discussion->description, $lang));
298
	}
299
300
	return $discussions;
301
}
302