Completed
Push — api_opt_action ( 0e883e )
by
unknown
31:33 queued 13:35
created

opportunity.php ➔ get_opportunities()   F

Complexity

Conditions 19
Paths 1376

Size

Total Lines 109
Code Lines 71

Duplication

Lines 5
Ratio 4.59 %

Importance

Changes 0
Metric Value
cc 19
eloc 71
nc 1376
nop 5
dl 5
loc 109
rs 2
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 Opportunity entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.opportunity",
8
	"get_opportunity",
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 opportunity based on user id and opportunity id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"get.opportunities",
22
	"get_opportunities",
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 opportunities based on user id',
31
	'POST',
32
	true,
33
	false
34
);
35
36
function get_opportunity($user, $guid, $lang)
37
{
38
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
39
	if (!$user_entity) {
40
		return "User was not found. Please try a different GUID, username, or email address";
41
	}
42
	if (!$user_entity instanceof ElggUser) {
43
		return "Invalid user. Please try a different GUID, username, or email address";
44
	}
45
46
	if (!elgg_is_logged_in()) {
47
		login($user_entity);
48
	}
49
50
	$entity = get_entity($guid);
51
	if (!$entity) {
52
		return "Opportunity was not found. Please try a different GUID";
53
	}
54
	if (!elgg_instanceof($entity, 'object', 'mission')) {
55
		return "Invalid opportunity. Please try a different GUID";
56
	}
57
58
59
60
	$opportunities = elgg_list_entities(array(
61
		'type' => 'object',
62
		'subtype' => 'mission',
63
		'guid' => $guid
64
	));
65
	$opportunity = json_decode($opportunities)[0];
66
67
	$opportunity->title = gc_explode_translation($opportunity->title, $lang);
68
69
	$likes = elgg_get_annotations(array(
70
		'guid' => $opportunity->guid,
71
		'annotation_name' => 'likes'
72
	));
73
	$opportunity->likes = count($likes);
74
75
	$liked = elgg_get_annotations(array(
76
		'guid' => $opportunity->guid,
77
		'annotation_owner_guid' => $user_entity->guid,
78
		'annotation_name' => 'likes'
79
	));
80
	$opportunity->liked = count($liked) > 0;
81
82
	$opportunity->comments = get_entity_comments($opportunity->guid);
83
84
	$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
85
	$opportunity->description = gc_explode_translation($opportunity->description, $lang);
86
87
	$opportunityObj = get_entity($opportunity->guid);
88
	$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
89
	$opportunity->roletype = elgg_echo($opportunityObj->role_type);
90
	//$opportunity->programArea = elgg_echo('missions:program_area') . ": " . elgg_echo($opportunityObj->program_area); //This should work and translate to user lang but doesnt
91
	$opportunity->programArea = elgg_echo($opportunityObj->program_area);
92
	$opportunity->numOpportunities = $opportunityObj->number;
93
	$opportunity->idealStart = $opportunityObj->start_date;
94
	$opportunity->idealComplete = $opportunityObj->complete_date;
95
	$opportunity->deadline = $opportunityObj->deadline;
96
	$opportunity->oppVirtual = $opportunityObj->remotely;
97
	$opportunity->oppOnlyIn = $opportunityObj->openess;
98
	$opportunity->location = elgg_echo($opportunityObj->location);
99
	$opportunity->security = elgg_echo($opportunityObj->security);
100
	$opportunity->skills = $opportunityObj->key_skills;
101
	//$opportunity->participants = $opportunityObj->;
102
	//$opportunity->applicants = $opportunityObj->;
103
	$opportunity->timezone = elgg_echo($opportunityObj->timezone);
104
	$opportunity->timecommitment = $opportunityObj->time_commitment;
105
	$opportunity->department = $opportunityObj->department;
106
	$opportunity->state = $opportunityObj->state;
107
108
	//Language metadata
109
	$unpacked_array = mm_unpack_mission($opportunityObj);
110
	$unpacked_language = '';
111 View Code Duplication
	if (! empty($unpacked_array['lwc_english']) || ! empty($unpacked_array['lwc_french'])) {
112
  	$unpacked_language .= '<b>' . elgg_echo('missions:written_comprehension') . ': </b>';
113
  	if (! empty($unpacked_array['lwc_english'])) {
114
      $unpacked_language .= '<span name="mission-lwc-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwc_english'])) . '</span> ';
115
  	}
116
  	if (! empty($unpacked_array['lwc_french'])) {
117
      $unpacked_language .= '<span name="mission-lwc-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwc_french'])) . '</span>';
118
  	}
119
		$unpacked_language .= '<br>';
120
	}
121 View Code Duplication
	if (! empty($unpacked_array['lwe_english']) || ! empty($unpacked_array['lwe_french'])) {
122
		$unpacked_language .= '<b>' . elgg_echo('missions:written_expression') . ': </b>';
123
		if (! empty($unpacked_array['lwe_english'])) {
124
	  	$unpacked_language .= '<span name="mission-lwe-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwe_english'])) . '</span> ';
125
	 	}
126
	  if (! empty($unpacked_array['lwe_french'])) {
127
	  	$unpacked_language .= '<span name="mission-lwe-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwe_french'])) . '</span>';
128
	  }
129
	  	$unpacked_language .= '<br>';
130
	}
131 View Code Duplication
	if (! empty($unpacked_array['lop_english']) || ! empty($unpacked_array['lop_french'])) {
132
		$unpacked_language .= '<b>' . elgg_echo('missions:oral_proficiency') . ': </b>';
133
		if (! empty($unpacked_array['lop_english'])) {
134
			$unpacked_language .= '<span name="mission-lop-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lop_english'])) . '</span> ';
135
		}
136
		if (! empty($unpacked_array['lop_french'])) {
137
			$unpacked_language .= '<span name="mission-lop-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lop_french'])) . '</span>';
138
		}
139
		$unpacked_language .= '<br>';
140
	}
141
	if (empty($unpacked_language)) {
142
		$unpacked_language = '<span name="no-languages">' . elgg_echo('missions:none_required') . '</span>';
143
	}
144
	$opportunity->languageRequirements = $unpacked_language;
145
146
	//scheduling metadata
147
	$unpacked_time = '';
148
	if ($opportunityObj->mon_start) {
149
		$unpacked_time .= '<b>' . elgg_echo('missions:mon') . ': </b>';
150
	 	$unpacked_time .= $opportunityObj->mon_start . elgg_echo('missions:to') .  $opportunityObj->mon_duration . '<br>';
151
	}
152
	if ($opportunityObj->tue_start) {
153
		$unpacked_time .= '<b>' . elgg_echo('missions:tue') . ': </b>';
154
		$unpacked_time .= '<span name="mission-tue-start">' . $opportunityObj->tue_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-tue-duration">' . $opportunityObj->tue_duration . '</span><br>';
155
	}
156
	if ($opportunityObj->wed_start) {
157
		$unpacked_time .= '<b>' . elgg_echo('missions:wed') . ': </b>';
158
		$unpacked_time .=  $opportunityObj->wed_start  . elgg_echo('missions:to') . $opportunityObj->wed_duration . '<br>';
159
	}
160
	if ($opportunityObj->thu_start) {
161
		$unpacked_time .= '<b>' . elgg_echo('missions:thu') . ': </b>';
162
		$unpacked_time .= '<span name="mission-thu-start">' . $opportunityObj->thu_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-thu-duration">' . $opportunityObj->thu_duration . '</span><br>';
163
	}
164
	if ($opportunityObj->fri_start) {
165
	  $unpacked_time .= '<b>' . elgg_echo('missions:fri') . ': </b>';
166
	  $unpacked_time .= '<span name="mission-fri-start">' . $opportunityObj->fri_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-fri-duration">' . $opportunityObj->fri_duration . '</span><br>';
167
	}
168
	if ($opportunityObj->sat_start) {
169
	  $unpacked_time .= '<b>' . elgg_echo('missions:sat') . ': </b>';
170
	  $unpacked_time .= '<span name="mission-sat-start">' . $opportunityObj->sat_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-sat-duration">' . $opportunityObj->sat_duration . '</span><br>';
171
	}
172
	if ($opportunityObj->sun_start) {
173
	  $unpacked_time .= '<b>' . elgg_echo('missions:sun') . ': </b>';
174
	  $unpacked_time .= '<span name="mission-sun-start">' . $opportunityObj->sun_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-sun-duration">' . $opportunityObj->sun_duration . '</span><br>';
175
	}
176
	if (empty($unpacked_time)) {
177
	  $unpacked_time = '<span name="no-times">' . elgg_echo('missions:none_required') . '</span>';
178
	}
179
	$opportunity->schedulingRequirements = $unpacked_time;
180
181
	
182
	return $opportunity;
183
}
184
185
function get_opportunities($user, $limit, $offset, $filters, $lang)
186
{
187
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
188
	if (!$user_entity) {
189
		return "User was not found. Please try a different GUID, username, or email address";
190
	}
191
	if (!$user_entity instanceof ElggUser) {
192
		return "Invalid user. Please try a different GUID, username, or email address";
193
	}
194
195
	if (!elgg_is_logged_in()) {
196
		login($user_entity);
197
	}
198
199
	$filter_data = json_decode($filters);
200
	if (!empty($filter_data)) {
201
		$params = array(
202
			'type' => 'object',
203
			'subtype' => 'mission',
204
			'limit' => $limit,
205
			'offset' => $offset
206
		);
207
208
		if ($filter_data->type) {
209
			$params['metadata_name'] = 'job_type';
210
			$params['metadata_value'] = $filter_data->type;
211
		}
212
213 View Code Duplication
		if ($filter_data->name) {
214
			$db_prefix = elgg_get_config('dbprefix');
215
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
216
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
217
		}
218
219
		if ($filter_data->mine) {
220
			$all_opportunities = elgg_list_entities_from_relationship($params);
221
		} else {
222
			$all_opportunities = elgg_list_entities_from_metadata($params);
223
		}
224
	} else {
225
		$all_opportunities = elgg_list_entities(array(
226
			'type' => 'object',
227
			'subtype' => 'mission',
228
			'limit' => $limit,
229
			'offset' => $offset
230
		));
231
	}
232
233
	$opportunities = json_decode($all_opportunities);
234
235
	foreach ($opportunities as $opportunity) {
236
		$opportunity->title = gc_explode_translation($opportunity->title, $lang);
237
		$opportunityObj = get_entity($opportunity->guid);
238
239
		$likes = elgg_get_annotations(array(
240
			'guid' => $opportunity->guid,
241
			'annotation_name' => 'likes'
242
		));
243
		$opportunity->likes = count($likes);
244
245
		$liked = elgg_get_annotations(array(
246
			'guid' => $opportunity->guid,
247
			'annotation_owner_guid' => $user_entity->guid,
248
			'annotation_name' => 'likes'
249
		));
250
		if($opportunity->owner_guid != $user_entity->guid){
251
			error_log('opt owner: '.$opportunity->owner_guid.'opt account: '.$opportunity->account."user guid :".$user_entity->guid);
252
			if($opportunityObj->state != 'completed' && $opportunityObj->state != 'cancelled'){
253
				$relationship_count = elgg_get_entities_from_relationship(array(
254
					'relationship' => 'mission_accepted',
255
					'relationship_guid' => $opportunity->guid,
256
					'count' => true
257
				));	
258
				if($relationship_count < $opportunityObj->number) {
259
				
260
					$opportunity->apply = 'mission_apply'; // user can apply
261
				}
262
					
263
				if(check_entity_relationship($opportunity->guid, 'mission_tentative', $user_entity->guid)) {
264
					//console.log($opportunity->title);
265
					$opportunity->apply = 'tentative'; // user can accecpt offer
266
				}
267
				if(check_entity_relationship($opportunity->guid, 'mission_offered', $user_entity->guid)) {
268
					$opportunity->apply = 'offered'; // user can accecpt offer
269
					
270
				}
271
				if(check_entity_relationship($opportunity->guid, 'mission_accepted', $user_entity->guid) ||
272
				check_entity_relationship($opportunity->guid, 'mission_applied', $user_entity->guid)) {
273
					$opportunity->apply = 'withdraw'; // user can accecpt offer
274
				
275
				}
276
			}
277
		}
278
			
279
		$opportunity->liked = count($liked) > 0;
280
		$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
281
		$opportunity->roletype = elgg_echo($opportunityObj->role_type);
282
		$opportunity->deadline = $opportunityObj->deadline;
283
		$opportunity->programArea = elgg_echo($opportunityObj->program_area);
284
		$opportunity->owner = ($opportunityObj->getOwnerEntity() == $user_entity);
285
		$opportunity->iconURL = $opportunityObj->getIconURL();
286
		$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
287
		$opportunity->description = clean_text(gc_explode_translation($opportunity->description, $lang));
288
		$opportunity->state = $opportunityObj->state;
289
290
	}
291
292
	return $opportunities;
293
}