Completed
Push — api_opt_action ( ff0d21...126323 )
by
unknown
41:58 queued 23:17
created

opportunity.php ➔ withdraw_post()   D

Complexity

Conditions 12
Paths 152

Size

Total Lines 44
Code Lines 27

Duplication

Lines 13
Ratio 29.55 %

Importance

Changes 0
Metric Value
cc 12
eloc 27
nc 152
nop 3
dl 13
loc 44
rs 4.8484
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
elgg_ws_expose_function(
51
	"withdraw.post",
52
	"withdraw_post",
53
	array(
54
		"user" => array('type' => 'string', 'required' => true),
55
		"guid" => array('type' => 'int', 'required' => true),
56
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
57
	),
58
	'Retrieves a opportunity based on user id and opportunity id',
59
	'POST',
60
	true,
61
	false
62
);
63
64
elgg_ws_expose_function(
65
	"accept.post",
66
	"accept_post",
67
	array(
68
		"user" => array('type' => 'string', 'required' => true),
69
		"guid" => array('type' => 'int', 'required' => true),
70
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
71
	),
72
	'Retrieves a opportunity based on user id and opportunity id',
73
	'POST',
74
	true,
75
	false
76
);
77
78
function get_opportunity($user, $guid, $lang)
79
{
80
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
81
	if (!$user_entity) {
82
		return "User was not found. Please try a different GUID, username, or email address";
83
	}
84
	if (!$user_entity instanceof ElggUser) {
85
		return "Invalid user. Please try a different GUID, username, or email address";
86
	}
87
88
	if (!elgg_is_logged_in()) {
89
		login($user_entity);
90
	}
91
92
	$entity = get_entity($guid);
93
	if (!$entity) {
94
		return "Opportunity was not found. Please try a different GUID";
95
	}
96
	if (!elgg_instanceof($entity, 'object', 'mission')) {
97
		return "Invalid opportunity. Please try a different GUID";
98
	}
99
100
101
102
	$opportunities = elgg_list_entities(array(
103
		'type' => 'object',
104
		'subtype' => 'mission',
105
		'guid' => $guid
106
	));
107
	$opportunity = json_decode($opportunities)[0];
108
109
	$opportunity->title = gc_explode_translation($opportunity->title, $lang);
110
111
	$likes = elgg_get_annotations(array(
112
		'guid' => $opportunity->guid,
113
		'annotation_name' => 'likes'
114
	));
115
	$opportunity->likes = count($likes);
116
117
	$liked = elgg_get_annotations(array(
118
		'guid' => $opportunity->guid,
119
		'annotation_owner_guid' => $user_entity->guid,
120
		'annotation_name' => 'likes'
121
	));
122
	$opportunity->liked = count($liked) > 0;
123
124
	$opportunity->comments = get_entity_comments($opportunity->guid);
125
126
	$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
127
	$opportunity->description = gc_explode_translation($opportunity->description, $lang);
128
129
	$opportunityObj = get_entity($opportunity->guid);
130
	$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
131
	$opportunity->roletype = elgg_echo($opportunityObj->role_type);
132
	//$opportunity->programArea = elgg_echo('missions:program_area') . ": " . elgg_echo($opportunityObj->program_area); //This should work and translate to user lang but doesnt
133
	$opportunity->programArea = elgg_echo($opportunityObj->program_area);
134
	$opportunity->numOpportunities = $opportunityObj->number;
135
	$opportunity->idealStart = $opportunityObj->start_date;
136
	$opportunity->idealComplete = $opportunityObj->complete_date;
137
	$opportunity->deadline = $opportunityObj->deadline;
138
	$opportunity->oppVirtual = $opportunityObj->remotely;
139
	$opportunity->oppOnlyIn = $opportunityObj->openess;
140
	$opportunity->location = elgg_echo($opportunityObj->location);
141
	$opportunity->security = elgg_echo($opportunityObj->security);
142
	$opportunity->skills = $opportunityObj->key_skills;
143
	//$opportunity->participants = $opportunityObj->;
144
	//$opportunity->applicants = $opportunityObj->;
145
	$opportunity->timezone = elgg_echo($opportunityObj->timezone);
146
	$opportunity->timecommitment = $opportunityObj->time_commitment;
147
	$opportunity->department = $opportunityObj->department;
148
	$opportunity->state = $opportunityObj->state;
149
150
	//Language metadata
151
	$unpacked_array = mm_unpack_mission($opportunityObj);
152
	$unpacked_language = '';
153 View Code Duplication
	if (! empty($unpacked_array['lwc_english']) || ! empty($unpacked_array['lwc_french'])) {
154
  	$unpacked_language .= '<b>' . elgg_echo('missions:written_comprehension') . ': </b>';
155
  	if (! empty($unpacked_array['lwc_english'])) {
156
      $unpacked_language .= '<span name="mission-lwc-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwc_english'])) . '</span> ';
157
  	}
158
  	if (! empty($unpacked_array['lwc_french'])) {
159
      $unpacked_language .= '<span name="mission-lwc-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwc_french'])) . '</span>';
160
  	}
161
		$unpacked_language .= '<br>';
162
	}
163 View Code Duplication
	if (! empty($unpacked_array['lwe_english']) || ! empty($unpacked_array['lwe_french'])) {
164
		$unpacked_language .= '<b>' . elgg_echo('missions:written_expression') . ': </b>';
165
		if (! empty($unpacked_array['lwe_english'])) {
166
	  	$unpacked_language .= '<span name="mission-lwe-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwe_english'])) . '</span> ';
167
	 	}
168
	  if (! empty($unpacked_array['lwe_french'])) {
169
	  	$unpacked_language .= '<span name="mission-lwe-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwe_french'])) . '</span>';
170
	  }
171
	  	$unpacked_language .= '<br>';
172
	}
173 View Code Duplication
	if (! empty($unpacked_array['lop_english']) || ! empty($unpacked_array['lop_french'])) {
174
		$unpacked_language .= '<b>' . elgg_echo('missions:oral_proficiency') . ': </b>';
175
		if (! empty($unpacked_array['lop_english'])) {
176
			$unpacked_language .= '<span name="mission-lop-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lop_english'])) . '</span> ';
177
		}
178
		if (! empty($unpacked_array['lop_french'])) {
179
			$unpacked_language .= '<span name="mission-lop-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lop_french'])) . '</span>';
180
		}
181
		$unpacked_language .= '<br>';
182
	}
183
	if (empty($unpacked_language)) {
184
		$unpacked_language = '<span name="no-languages">' . elgg_echo('missions:none_required') . '</span>';
185
	}
186
	$opportunity->languageRequirements = $unpacked_language;
187
188
	//scheduling metadata
189
	$unpacked_time = '';
190
	if ($opportunityObj->mon_start) {
191
		$unpacked_time .= '<b>' . elgg_echo('missions:mon') . ': </b>';
192
	 	$unpacked_time .= $opportunityObj->mon_start . elgg_echo('missions:to') .  $opportunityObj->mon_duration . '<br>';
193
	}
194
	if ($opportunityObj->tue_start) {
195
		$unpacked_time .= '<b>' . elgg_echo('missions:tue') . ': </b>';
196
		$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>';
197
	}
198
	if ($opportunityObj->wed_start) {
199
		$unpacked_time .= '<b>' . elgg_echo('missions:wed') . ': </b>';
200
		$unpacked_time .=  $opportunityObj->wed_start  . elgg_echo('missions:to') . $opportunityObj->wed_duration . '<br>';
201
	}
202
	if ($opportunityObj->thu_start) {
203
		$unpacked_time .= '<b>' . elgg_echo('missions:thu') . ': </b>';
204
		$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>';
205
	}
206
	if ($opportunityObj->fri_start) {
207
	  $unpacked_time .= '<b>' . elgg_echo('missions:fri') . ': </b>';
208
	  $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>';
209
	}
210
	if ($opportunityObj->sat_start) {
211
	  $unpacked_time .= '<b>' . elgg_echo('missions:sat') . ': </b>';
212
	  $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>';
213
	}
214
	if ($opportunityObj->sun_start) {
215
	  $unpacked_time .= '<b>' . elgg_echo('missions:sun') . ': </b>';
216
	  $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>';
217
	}
218
	if (empty($unpacked_time)) {
219
	  $unpacked_time = '<span name="no-times">' . elgg_echo('missions:none_required') . '</span>';
220
	}
221
	$opportunity->schedulingRequirements = $unpacked_time;
222
223
	
224
	return $opportunity;
225
}
226
227
function get_opportunities($user, $limit, $offset, $filters, $lang)
228
{
229
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
230
	if (!$user_entity) {
231
		return "User was not found. Please try a different GUID, username, or email address";
232
	}
233
	if (!$user_entity instanceof ElggUser) {
234
		return "Invalid user. Please try a different GUID, username, or email address";
235
	}
236
237
	if (!elgg_is_logged_in()) {
238
		login($user_entity);
239
	}
240
241
	$filter_data = json_decode($filters);
242
	if (!empty($filter_data)) {
243
		$params = array(
244
			'type' => 'object',
245
			'subtype' => 'mission',
246
			'limit' => $limit,
247
			'offset' => $offset
248
		);
249
250
		if ($filter_data->type) {
251
			$params['metadata_name'] = 'job_type';
252
			$params['metadata_value'] = $filter_data->type;
253
		}
254
255 View Code Duplication
		if ($filter_data->name) {
256
			$db_prefix = elgg_get_config('dbprefix');
257
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
258
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
259
		}
260
261
		if ($filter_data->mine) {
262
			$all_opportunities = elgg_list_entities_from_relationship($params);
263
		} else {
264
			$all_opportunities = elgg_list_entities_from_metadata($params);
265
		}
266
	} else {
267
		$all_opportunities = elgg_list_entities(array(
268
			'type' => 'object',
269
			'subtype' => 'mission',
270
			'limit' => $limit,
271
			'offset' => $offset
272
		));
273
	}
274
275
	$opportunities = json_decode($all_opportunities);
276
277
	foreach ($opportunities as $opportunity) {
278
	
279
		$opportunity->title = gc_explode_translation($opportunity->title, $lang);
280
		$opportunityObj = get_entity($opportunity->guid);
281
	
282
		$likes = elgg_get_annotations(array(
283
			'guid' => $opportunity->guid,
284
			'annotation_name' => 'likes'
285
		));
286
		$opportunity->likes = count($likes);
287
288
		$liked = elgg_get_annotations(array(
289
			'guid' => $opportunity->guid,
290
			'annotation_owner_guid' => $user_entity->guid,
291
			'annotation_name' => 'likes'
292
		));
293
		if($opportunity->owner_guid != $user_entity->guid){
294
			
295
			if($opportunityObj->state != 'completed' && $opportunityObj->state != 'cancelled'){
296
				$relationship_count = elgg_get_entities_from_relationship(array(
297
					'relationship' => 'mission_accepted',
298
					'relationship_guid' => $opportunity->guid,
299
					'count' => true
300
				));	
301
				if($relationship_count < $opportunityObj->number) {
302
				
303
					$opportunity->apply = 'mission_apply'; // user can apply
304
				}
305
					
306
				if(check_entity_relationship($opportunity->guid, 'mission_tentative', $user_entity->guid)) {
307
					//console.log($opportunity->title);
308
					$opportunity->apply = 'tentative'; // user can accecpt offer
309
				}
310
				if(check_entity_relationship($opportunity->guid, 'mission_offered', $user_entity->guid)) {
311
					$opportunity->apply = 'offered'; // user can accecpt offer
312
					
313
				}
314
				if(check_entity_relationship($opportunity->guid, 'mission_accepted', $user_entity->guid) ||
315
				check_entity_relationship($opportunity->guid, 'mission_applied', $user_entity->guid)) {
316
					$opportunity->apply = 'withdraw'; // user can accecpt offer
317
				
318
				}
319
			}
320
		}
321
			
322
		$opportunity->liked = count($liked) > 0;
323
		$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
324
		$opportunity->roletype = elgg_echo($opportunityObj->role_type);
325
		$opportunity->deadline = $opportunityObj->deadline;
326
		$opportunity->programArea = elgg_echo($opportunityObj->program_area);
327
		$opportunity->owner = ($opportunityObj->getOwnerEntity() == $user_entity);
328
		$opportunity->iconURL = $opportunityObj->getIconURL();
329
		$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
330
		$opportunity->description = clean_text(gc_explode_translation($opportunity->description, $lang));
331
		$opportunity->state = $opportunityObj->state;
332
		error_log('opportunity '.print_r($opportunity,true));
333
	
334
335
	}
336
337
	return $opportunities;
338
}
339
340
341
342
function apply_post($user, $guid, $lang)
343
{
344
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
345
	if (!$user_entity) {
346
		return "User was not found. Please try a different GUID, username, or email address";
347
	}
348
	if (!$user_entity instanceof ElggUser) {
349
		return "Invalid user. Please try a different GUID, username, or email address";
350
	}
351
352
	if (!elgg_is_logged_in()) {
353
		login($user_entity);
354
	}
355
356
	$entity = get_entity($guid);
357
	if (!$entity) {
358
		return "Opportunity was not found. Please try a different GUID";
359
	}
360
	if (!elgg_instanceof($entity, 'object', 'mission')) {
361
		return "Invalid opportunity. Please try a different GUID";
362
	}
363
364
	// Creates an applied relationship between user and mission if there is no relationship there already.
365
	if(!check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid) && !check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
366
		add_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
367
		$message = elgg_echo('missions:you_have_applied_to_mission', array($entity->job_title, $entity->name));
368
		
369
	}
370
	
371
	// Opt in applicant if they are not opted in yet.
372
	if(!check_if_opted_in($user_entity)) {
373
		$user_entity->opt_in_missions = 'gcconnex_profile:opt:yes';
374
		$user_entity->save();
375
	}
376
	return $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...
377
378
}
379
380
function withdraw_post($user, $guid, $lang)
381
{
382
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
383
	if (!$user_entity) {
384
		return "User was not found. Please try a different GUID, username, or email address";
385
	}
386
	if (!$user_entity instanceof ElggUser) {
387
		return "Invalid user. Please try a different GUID, username, or email address";
388
	}
389
390
	if (!elgg_is_logged_in()) {
391
		login($user_entity);
392
	}
393
394
	$entity = get_entity($guid);
395
	if (!$entity) {
396
		return "Opportunity was not found. Please try a different GUID";
397
	}
398
	if (!elgg_instanceof($entity, 'object', 'mission')) {
399
		return "Invalid opportunity. Please try a different GUID";
400
	}
401
402
	// Deletes the tentative relationship between mission and applicant.
403 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
404
	$message_return = 'missions:declination_has_been_sent';
405
	remove_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid);
406
}
407
if(check_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid)) {
408
	$message_return = 'missions:withdrawal_has_been_sent';
409
	remove_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
410
}
411 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid)) {
412
	$message_return = 'missions:declination_has_been_sent';
413
	remove_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid);
414
}
415 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid)) {
416
	$message_return = 'missions:withdrawal_has_been_sent';
417
	remove_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid);
418
  mm_complete_mission_inprogress_reports($entity, true);
419
}
420
	return elgg_echo($message_return, array($entity->job_title));
0 ignored issues
show
Bug introduced by
The variable $message_return 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...
421
	
422
423
}
424
425
function accept_post($user, $guid, $lang)
426
{
427
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
428
	if (!$user_entity) {
429
		return "User was not found. Please try a different GUID, username, or email address";
430
	}
431
	if (!$user_entity instanceof ElggUser) {
432
		return "Invalid user. Please try a different GUID, username, or email address";
433
	}
434
435
	if (!elgg_is_logged_in()) {
436
		login($user_entity);
437
	}
438
439
	$entity = get_entity($guid);
440
	if (!$entity) {
441
		return "Opportunity was not found. Please try a different GUID";
442
	}
443
	if (!elgg_instanceof($entity, 'object', 'mission')) {
444
		return "Invalid opportunity. Please try a different GUID";
445
	}
446
447 View Code Duplication
	if(check_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid)) {
448
449
		remove_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid);
450
		add_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid);
451
	}
452
	
453
	return elgg_echo('missions:now_participating_in_mission', array($entity->job_title));
454
}
455