Completed
Push — api_opt_action ( 0e883e...ff0d21 )
by
unknown
41:25 queued 18:04
created

opportunity.php ➔ apply_post()   C

Complexity

Conditions 11
Paths 56

Size

Total Lines 37
Code Lines 20

Duplication

Lines 4
Ratio 10.81 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 56
nop 3
dl 4
loc 37
rs 5.2653
c 0
b 0
f 0

How to fix   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
elgg_ws_expose_function(
37
	"apply.post",
38
	"apply_post",
39
	array(
40
		"user" => array('type' => 'string', 'required' => true),
41
		"guid" => array('type' => 'int', 'required' => true),
42
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
43
	),
44
	'Retrieves a opportunity based on user id and opportunity id',
45
	'POST',
46
	true,
47
	false
48
);
49
50
function get_opportunity($user, $guid, $lang)
51
{
52
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
53
	if (!$user_entity) {
54
		return "User was not found. Please try a different GUID, username, or email address";
55
	}
56
	if (!$user_entity instanceof ElggUser) {
57
		return "Invalid user. Please try a different GUID, username, or email address";
58
	}
59
60
	if (!elgg_is_logged_in()) {
61
		login($user_entity);
62
	}
63
64
	$entity = get_entity($guid);
65
	if (!$entity) {
66
		return "Opportunity was not found. Please try a different GUID";
67
	}
68
	if (!elgg_instanceof($entity, 'object', 'mission')) {
69
		return "Invalid opportunity. Please try a different GUID";
70
	}
71
72
73
74
	$opportunities = elgg_list_entities(array(
75
		'type' => 'object',
76
		'subtype' => 'mission',
77
		'guid' => $guid
78
	));
79
	$opportunity = json_decode($opportunities)[0];
80
81
	$opportunity->title = gc_explode_translation($opportunity->title, $lang);
82
83
	$likes = elgg_get_annotations(array(
84
		'guid' => $opportunity->guid,
85
		'annotation_name' => 'likes'
86
	));
87
	$opportunity->likes = count($likes);
88
89
	$liked = elgg_get_annotations(array(
90
		'guid' => $opportunity->guid,
91
		'annotation_owner_guid' => $user_entity->guid,
92
		'annotation_name' => 'likes'
93
	));
94
	$opportunity->liked = count($liked) > 0;
95
96
	$opportunity->comments = get_entity_comments($opportunity->guid);
97
98
	$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
99
	$opportunity->description = gc_explode_translation($opportunity->description, $lang);
100
101
	$opportunityObj = get_entity($opportunity->guid);
102
	$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
103
	$opportunity->roletype = elgg_echo($opportunityObj->role_type);
104
	//$opportunity->programArea = elgg_echo('missions:program_area') . ": " . elgg_echo($opportunityObj->program_area); //This should work and translate to user lang but doesnt
105
	$opportunity->programArea = elgg_echo($opportunityObj->program_area);
106
	$opportunity->numOpportunities = $opportunityObj->number;
107
	$opportunity->idealStart = $opportunityObj->start_date;
108
	$opportunity->idealComplete = $opportunityObj->complete_date;
109
	$opportunity->deadline = $opportunityObj->deadline;
110
	$opportunity->oppVirtual = $opportunityObj->remotely;
111
	$opportunity->oppOnlyIn = $opportunityObj->openess;
112
	$opportunity->location = elgg_echo($opportunityObj->location);
113
	$opportunity->security = elgg_echo($opportunityObj->security);
114
	$opportunity->skills = $opportunityObj->key_skills;
115
	//$opportunity->participants = $opportunityObj->;
116
	//$opportunity->applicants = $opportunityObj->;
117
	$opportunity->timezone = elgg_echo($opportunityObj->timezone);
118
	$opportunity->timecommitment = $opportunityObj->time_commitment;
119
	$opportunity->department = $opportunityObj->department;
120
	$opportunity->state = $opportunityObj->state;
121
122
	//Language metadata
123
	$unpacked_array = mm_unpack_mission($opportunityObj);
124
	$unpacked_language = '';
125 View Code Duplication
	if (! empty($unpacked_array['lwc_english']) || ! empty($unpacked_array['lwc_french'])) {
126
  	$unpacked_language .= '<b>' . elgg_echo('missions:written_comprehension') . ': </b>';
127
  	if (! empty($unpacked_array['lwc_english'])) {
128
      $unpacked_language .= '<span name="mission-lwc-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwc_english'])) . '</span> ';
129
  	}
130
  	if (! empty($unpacked_array['lwc_french'])) {
131
      $unpacked_language .= '<span name="mission-lwc-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwc_french'])) . '</span>';
132
  	}
133
		$unpacked_language .= '<br>';
134
	}
135 View Code Duplication
	if (! empty($unpacked_array['lwe_english']) || ! empty($unpacked_array['lwe_french'])) {
136
		$unpacked_language .= '<b>' . elgg_echo('missions:written_expression') . ': </b>';
137
		if (! empty($unpacked_array['lwe_english'])) {
138
	  	$unpacked_language .= '<span name="mission-lwe-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwe_english'])) . '</span> ';
139
	 	}
140
	  if (! empty($unpacked_array['lwe_french'])) {
141
	  	$unpacked_language .= '<span name="mission-lwe-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwe_french'])) . '</span>';
142
	  }
143
	  	$unpacked_language .= '<br>';
144
	}
145 View Code Duplication
	if (! empty($unpacked_array['lop_english']) || ! empty($unpacked_array['lop_french'])) {
146
		$unpacked_language .= '<b>' . elgg_echo('missions:oral_proficiency') . ': </b>';
147
		if (! empty($unpacked_array['lop_english'])) {
148
			$unpacked_language .= '<span name="mission-lop-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lop_english'])) . '</span> ';
149
		}
150
		if (! empty($unpacked_array['lop_french'])) {
151
			$unpacked_language .= '<span name="mission-lop-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lop_french'])) . '</span>';
152
		}
153
		$unpacked_language .= '<br>';
154
	}
155
	if (empty($unpacked_language)) {
156
		$unpacked_language = '<span name="no-languages">' . elgg_echo('missions:none_required') . '</span>';
157
	}
158
	$opportunity->languageRequirements = $unpacked_language;
159
160
	//scheduling metadata
161
	$unpacked_time = '';
162
	if ($opportunityObj->mon_start) {
163
		$unpacked_time .= '<b>' . elgg_echo('missions:mon') . ': </b>';
164
	 	$unpacked_time .= $opportunityObj->mon_start . elgg_echo('missions:to') .  $opportunityObj->mon_duration . '<br>';
165
	}
166
	if ($opportunityObj->tue_start) {
167
		$unpacked_time .= '<b>' . elgg_echo('missions:tue') . ': </b>';
168
		$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>';
169
	}
170
	if ($opportunityObj->wed_start) {
171
		$unpacked_time .= '<b>' . elgg_echo('missions:wed') . ': </b>';
172
		$unpacked_time .=  $opportunityObj->wed_start  . elgg_echo('missions:to') . $opportunityObj->wed_duration . '<br>';
173
	}
174
	if ($opportunityObj->thu_start) {
175
		$unpacked_time .= '<b>' . elgg_echo('missions:thu') . ': </b>';
176
		$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>';
177
	}
178
	if ($opportunityObj->fri_start) {
179
	  $unpacked_time .= '<b>' . elgg_echo('missions:fri') . ': </b>';
180
	  $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>';
181
	}
182
	if ($opportunityObj->sat_start) {
183
	  $unpacked_time .= '<b>' . elgg_echo('missions:sat') . ': </b>';
184
	  $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>';
185
	}
186
	if ($opportunityObj->sun_start) {
187
	  $unpacked_time .= '<b>' . elgg_echo('missions:sun') . ': </b>';
188
	  $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>';
189
	}
190
	if (empty($unpacked_time)) {
191
	  $unpacked_time = '<span name="no-times">' . elgg_echo('missions:none_required') . '</span>';
192
	}
193
	$opportunity->schedulingRequirements = $unpacked_time;
194
195
	
196
	return $opportunity;
197
}
198
199
function get_opportunities($user, $limit, $offset, $filters, $lang)
200
{
201
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
202
	if (!$user_entity) {
203
		return "User was not found. Please try a different GUID, username, or email address";
204
	}
205
	if (!$user_entity instanceof ElggUser) {
206
		return "Invalid user. Please try a different GUID, username, or email address";
207
	}
208
209
	if (!elgg_is_logged_in()) {
210
		login($user_entity);
211
	}
212
213
	$filter_data = json_decode($filters);
214
	if (!empty($filter_data)) {
215
		$params = array(
216
			'type' => 'object',
217
			'subtype' => 'mission',
218
			'limit' => $limit,
219
			'offset' => $offset
220
		);
221
222
		if ($filter_data->type) {
223
			$params['metadata_name'] = 'job_type';
224
			$params['metadata_value'] = $filter_data->type;
225
		}
226
227 View Code Duplication
		if ($filter_data->name) {
228
			$db_prefix = elgg_get_config('dbprefix');
229
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
230
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
231
		}
232
233
		if ($filter_data->mine) {
234
			$all_opportunities = elgg_list_entities_from_relationship($params);
235
		} else {
236
			$all_opportunities = elgg_list_entities_from_metadata($params);
237
		}
238
	} else {
239
		$all_opportunities = elgg_list_entities(array(
240
			'type' => 'object',
241
			'subtype' => 'mission',
242
			'limit' => $limit,
243
			'offset' => $offset
244
		));
245
	}
246
247
	$opportunities = json_decode($all_opportunities);
248
249
	foreach ($opportunities as $opportunity) {
250
		$opportunity->title = gc_explode_translation($opportunity->title, $lang);
251
		$opportunityObj = get_entity($opportunity->guid);
252
253
		$likes = elgg_get_annotations(array(
254
			'guid' => $opportunity->guid,
255
			'annotation_name' => 'likes'
256
		));
257
		$opportunity->likes = count($likes);
258
259
		$liked = elgg_get_annotations(array(
260
			'guid' => $opportunity->guid,
261
			'annotation_owner_guid' => $user_entity->guid,
262
			'annotation_name' => 'likes'
263
		));
264
		if($opportunity->owner_guid != $user_entity->guid){
265
			error_log('opt owner: '.$opportunity->owner_guid.'opt account: '.$opportunity->account."user guid :".$user_entity->guid);
266
			if($opportunityObj->state != 'completed' && $opportunityObj->state != 'cancelled'){
267
				$relationship_count = elgg_get_entities_from_relationship(array(
268
					'relationship' => 'mission_accepted',
269
					'relationship_guid' => $opportunity->guid,
270
					'count' => true
271
				));	
272
				if($relationship_count < $opportunityObj->number) {
273
				
274
					$opportunity->apply = 'mission_apply'; // user can apply
275
				}
276
					
277
				if(check_entity_relationship($opportunity->guid, 'mission_tentative', $user_entity->guid)) {
278
					//console.log($opportunity->title);
279
					$opportunity->apply = 'tentative'; // user can accecpt offer
280
				}
281
				if(check_entity_relationship($opportunity->guid, 'mission_offered', $user_entity->guid)) {
282
					$opportunity->apply = 'offered'; // user can accecpt offer
283
					
284
				}
285
				if(check_entity_relationship($opportunity->guid, 'mission_accepted', $user_entity->guid) ||
286
				check_entity_relationship($opportunity->guid, 'mission_applied', $user_entity->guid)) {
287
					$opportunity->apply = 'withdraw'; // user can accecpt offer
288
				
289
				}
290
			}
291
		}
292
			
293
		$opportunity->liked = count($liked) > 0;
294
		$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
295
		$opportunity->roletype = elgg_echo($opportunityObj->role_type);
296
		$opportunity->deadline = $opportunityObj->deadline;
297
		$opportunity->programArea = elgg_echo($opportunityObj->program_area);
298
		$opportunity->owner = ($opportunityObj->getOwnerEntity() == $user_entity);
299
		$opportunity->iconURL = $opportunityObj->getIconURL();
300
		$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
301
		$opportunity->description = clean_text(gc_explode_translation($opportunity->description, $lang));
302
		$opportunity->state = $opportunityObj->state;
303
304
	}
305
306
	return $opportunities;
307
}
308
309
310
311
function apply_post($user, $guid, $lang)
312
{
313
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
314
	if (!$user_entity) {
315
		return "User was not found. Please try a different GUID, username, or email address";
316
	}
317
	if (!$user_entity instanceof ElggUser) {
318
		return "Invalid user. Please try a different GUID, username, or email address";
319
	}
320
321
	if (!elgg_is_logged_in()) {
322
		login($user_entity);
323
	}
324
325
	$entity = get_entity($guid);
326
	if (!$entity) {
327
		return "Opportunity was not found. Please try a different GUID";
328
	}
329
	if (!elgg_instanceof($entity, 'object', 'mission')) {
330
		return "Invalid opportunity. Please try a different GUID";
331
	}
332
333
	// Creates an applied relationship between user and mission if there is no relationship there already.
334 View Code Duplication
	if(!check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid) && !check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
335
		add_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
336
		$message = 'Apply';
337
	}
338
		
339
	// Opt in applicant if they are not opted in yet.
340
	if(!check_if_opted_in($user_entity)) {
341
		$user_entity->opt_in_missions = 'gcconnex_profile:opt:yes';
342
		$user_entity->save();
343
	}
344
345
	return 'You '.$message;
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
346
347
}