Passed
Push — Mobile-API---Comments ( 439939 )
by
unknown
19:32
created

comment.php ➔ get_comments_all()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 48
nop 5
dl 0
loc 39
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
4
elgg_ws_expose_function(
5
	"get.commentsall",
6
	"get_comments_all",
7
	array(
8
		"user" => array('type' => 'string', 'required' => true),
9
		"guid" => array('type' => 'int', 'required' => true),
10
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
11
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
12
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
13
	),
14
	'Gets comments for given entity (guid), uses limit and offset.',
15
	'POST',
16
	true,
17
	false
18
);
19
20
21
22
function get_comments_all($user, $guid, $limit, $offset, $lang)
23
{
24
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
25
	if (!$user_entity) {
26
		return "User was not found. Please try a different GUID, username, or email address";
27
	}
28
	if (!$user_entity instanceof ElggUser) {
29
		return "Invalid user. Please try a different GUID, username, or email address";
30
	}
31
	if (!elgg_is_logged_in()) {
32
		login($user_entity);
33
	}
34
35
	$entity = get_entity($guid);
36
	$subtype = 'nothing';
37
	if (!$entity) {
38
		return "Discussion was not found. Please try a different GUID";
39
	} else if (elgg_instanceof($entity, "object", "groupforumtopic")){
40
		$subtype = 'discussion_reply';
41
	} else {
42
		$subtype = 'comment';
43
	}
44
45
	$all_replies = elgg_list_entities_from_metadata(array(
46
		'type' => 'object',
47
		'subtype' => $subtype,
48
		'container_guid' => $guid,
49
		'limit' => $limit,
50
		'offset' => $offset
51
	));
52
	$replies = json_decode($all_replies);
53
	$comments = array();
54
	foreach ($replies as $reply) {
55
		$reply->userDetails = get_user_block($reply->owner_guid, $lang);
56
		$comments[] = $reply;
57
	}
58
59
	return $comments;
60
}
61