Completed
Push — api_opt_v2 ( b2849f...f4658c )
by
unknown
24:10
created

opportunity.php ➔ apply_post()   F

Complexity

Conditions 22
Paths 1064

Size

Total Lines 145
Code Lines 90

Duplication

Lines 55
Ratio 37.93 %

Importance

Changes 0
Metric Value
cc 22
eloc 90
nc 1064
nop 4
dl 55
loc 145
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
elgg_ws_expose_function(
37
	"apply.post",
38
	"apply_post",
39
	array(
40
		"user" => array('type' => 'string', 'required' => true),
41
		"message" => array('type' => 'string', 'required' => true, 'default' => ""),		
42
		"guid" => array('type' => 'int', 'required' => true),
43
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
44
	),
45
	'Retrieves a opportunity based on user id and opportunity id',
46
	'POST',
47
	true,
48
	false
49
);
50
51
elgg_ws_expose_function(
52
	"withdraw.post",
53
	"withdraw_post",
54
	array(
55
		"user" => array('type' => 'string', 'required' => true),
56
		"guid" => array('type' => 'int', 'required' => true),
57
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
58
	),
59
	'Retrieves a opportunity based on user id and opportunity id',
60
	'POST',
61
	true,
62
	false
63
);
64
65
elgg_ws_expose_function(
66
	"accept.post",
67
	"accept_post",
68
	array(
69
		"user" => array('type' => 'string', 'required' => true),
70
		"guid" => array('type' => 'int', 'required' => true),
71
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
72
	),
73
	'Retrieves a opportunity based on user id and opportunity id',
74
	'POST',
75
	true,
76
	false
77
);
78
79
function get_opportunity($user, $guid, $lang)
80
{
81
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
82
	if (!$user_entity) {
83
		return "User was not found. Please try a different GUID, username, or email address";
84
	}
85
	if (!$user_entity instanceof ElggUser) {
86
		return "Invalid user. Please try a different GUID, username, or email address";
87
	}
88
89
	if (!elgg_is_logged_in()) {
90
		login($user_entity);
91
	}
92
93
	$entity = get_entity($guid);
94
	if (!$entity) {
95
		return "Opportunity was not found. Please try a different GUID";
96
	}
97
	if (!elgg_instanceof($entity, 'object', 'mission')) {
98
		return "Invalid opportunity. Please try a different GUID";
99
	}
100
101
102
103
	$opportunities = elgg_list_entities(array(
104
		'type' => 'object',
105
		'subtype' => 'mission',
106
		'guid' => $guid
107
	));
108
	$opportunity = json_decode($opportunities)[0];
109
110
	$opportunity->title = gc_explode_translation($opportunity->title, $lang);
111
112
	$likes = elgg_get_annotations(array(
113
		'guid' => $opportunity->guid,
114
		'annotation_name' => 'likes'
115
	));
116
	$opportunity->likes = count($likes);
117
118
	$liked = elgg_get_annotations(array(
119
		'guid' => $opportunity->guid,
120
		'annotation_owner_guid' => $user_entity->guid,
121
		'annotation_name' => 'likes'
122
	));
123
	$opportunity->liked = count($liked) > 0;
124
125
	$opportunity->comments = get_entity_comments($opportunity->guid);
126
127
	$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
128
	$opportunity->description = gc_explode_translation($opportunity->description, $lang);
129
130
	$opportunityObj = get_entity($opportunity->guid);
131
	$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
132
	$opportunity->roletype = elgg_echo($opportunityObj->role_type);
133
	//$opportunity->programArea = elgg_echo('missions:program_area') . ": " . elgg_echo($opportunityObj->program_area); //This should work and translate to user lang but doesnt
134
	$opportunity->programArea = elgg_echo($opportunityObj->program_area);
135
	$opportunity->numOpportunities = $opportunityObj->number;
136
	$opportunity->idealStart = $opportunityObj->start_date;
137
	$opportunity->idealComplete = $opportunityObj->complete_date;
138
	$opportunity->deadline = $opportunityObj->deadline;
139
	$opportunity->oppVirtual = $opportunityObj->remotely;
140
	$opportunity->oppOnlyIn = $opportunityObj->openess;
141
	$opportunity->location = elgg_echo($opportunityObj->location);
142
	$opportunity->security = elgg_echo($opportunityObj->security);
143
	$opportunity->skills = $opportunityObj->key_skills;
144
	//$opportunity->participants = $opportunityObj->;
145
	//$opportunity->applicants = $opportunityObj->;
146
	$opportunity->timezone = elgg_echo($opportunityObj->timezone);
147
	$opportunity->timecommitment = $opportunityObj->time_commitment;
148
	$opportunity->department = $opportunityObj->department;
149
	$opportunity->state = $opportunityObj->state;
150
151
	//Language metadata
152
	$unpacked_array = mm_unpack_mission($opportunityObj);
153
	$unpacked_language = '';
154 View Code Duplication
	if (! empty($unpacked_array['lwc_english']) || ! empty($unpacked_array['lwc_french'])) {
155
  	$unpacked_language .= '<b>' . elgg_echo('missions:written_comprehension') . ': </b>';
156
  	if (! empty($unpacked_array['lwc_english'])) {
157
      $unpacked_language .= '<span name="mission-lwc-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwc_english'])) . '</span> ';
158
  	}
159
  	if (! empty($unpacked_array['lwc_french'])) {
160
      $unpacked_language .= '<span name="mission-lwc-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwc_french'])) . '</span>';
161
  	}
162
		$unpacked_language .= '<br>';
163
	}
164 View Code Duplication
	if (! empty($unpacked_array['lwe_english']) || ! empty($unpacked_array['lwe_french'])) {
165
		$unpacked_language .= '<b>' . elgg_echo('missions:written_expression') . ': </b>';
166
		if (! empty($unpacked_array['lwe_english'])) {
167
	  	$unpacked_language .= '<span name="mission-lwe-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwe_english'])) . '</span> ';
168
	 	}
169
	  if (! empty($unpacked_array['lwe_french'])) {
170
	  	$unpacked_language .= '<span name="mission-lwe-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwe_french'])) . '</span>';
171
	  }
172
	  	$unpacked_language .= '<br>';
173
	}
174 View Code Duplication
	if (! empty($unpacked_array['lop_english']) || ! empty($unpacked_array['lop_french'])) {
175
		$unpacked_language .= '<b>' . elgg_echo('missions:oral_proficiency') . ': </b>';
176
		if (! empty($unpacked_array['lop_english'])) {
177
			$unpacked_language .= '<span name="mission-lop-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lop_english'])) . '</span> ';
178
		}
179
		if (! empty($unpacked_array['lop_french'])) {
180
			$unpacked_language .= '<span name="mission-lop-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lop_french'])) . '</span>';
181
		}
182
		$unpacked_language .= '<br>';
183
	}
184
	if (empty($unpacked_language)) {
185
		$unpacked_language = '<span name="no-languages">' . elgg_echo('missions:none_required') . '</span>';
186
	}
187
	$opportunity->languageRequirements = $unpacked_language;
188
189
	//scheduling metadata
190
	$unpacked_time = '';
191
	if ($opportunityObj->mon_start) {
192
		$unpacked_time .= '<b>' . elgg_echo('missions:mon') . ': </b>';
193
	 	$unpacked_time .= $opportunityObj->mon_start . elgg_echo('missions:to') .  $opportunityObj->mon_duration . '<br>';
194
	}
195
	if ($opportunityObj->tue_start) {
196
		$unpacked_time .= '<b>' . elgg_echo('missions:tue') . ': </b>';
197
		$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>';
198
	}
199
	if ($opportunityObj->wed_start) {
200
		$unpacked_time .= '<b>' . elgg_echo('missions:wed') . ': </b>';
201
		$unpacked_time .=  $opportunityObj->wed_start  . elgg_echo('missions:to') . $opportunityObj->wed_duration . '<br>';
202
	}
203
	if ($opportunityObj->thu_start) {
204
		$unpacked_time .= '<b>' . elgg_echo('missions:thu') . ': </b>';
205
		$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>';
206
	}
207
	if ($opportunityObj->fri_start) {
208
	  $unpacked_time .= '<b>' . elgg_echo('missions:fri') . ': </b>';
209
	  $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>';
210
	}
211
	if ($opportunityObj->sat_start) {
212
	  $unpacked_time .= '<b>' . elgg_echo('missions:sat') . ': </b>';
213
	  $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>';
214
	}
215
	if ($opportunityObj->sun_start) {
216
	  $unpacked_time .= '<b>' . elgg_echo('missions:sun') . ': </b>';
217
	  $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>';
218
	}
219
	if (empty($unpacked_time)) {
220
	  $unpacked_time = '<span name="no-times">' . elgg_echo('missions:none_required') . '</span>';
221
	}
222
	$opportunity->schedulingRequirements = $unpacked_time;
223
224
	
225
	return $opportunity;
226
}
227
228
function get_opportunities($user, $limit, $offset, $filters, $lang)
229
{
230
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
231
	if (!$user_entity) {
232
		return "User was not found. Please try a different GUID, username, or email address";
233
	}
234
	if (!$user_entity instanceof ElggUser) {
235
		return "Invalid user. Please try a different GUID, username, or email address";
236
	}
237
238
	if (!elgg_is_logged_in()) {
239
		login($user_entity);
240
	}
241
242
	$filter_data = json_decode($filters);
243
	if (!empty($filter_data)) {
244
		$params = array(
245
			'type' => 'object',
246
			'subtype' => 'mission',
247
			'limit' => $limit,
248
			'offset' => $offset
249
		);
250
251
		if ($filter_data->type) {
252
			$params['metadata_name'] = 'job_type';
253
			$params['metadata_value'] = $filter_data->type;
254
		}
255
256 View Code Duplication
		if ($filter_data->name) {
257
			$db_prefix = elgg_get_config('dbprefix');
258
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
259
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
260
		}
261
262
		if ($filter_data->mine) {
263
			$all_opportunities = elgg_list_entities_from_relationship($params);
264
		} else {
265
			$all_opportunities = elgg_list_entities_from_metadata($params);
266
		}
267
	} else {
268
		$all_opportunities = elgg_list_entities(array(
269
			'type' => 'object',
270
			'subtype' => 'mission',
271
			'limit' => $limit,
272
			'offset' => $offset
273
		));
274
	}
275
276
	$opportunities = json_decode($all_opportunities);
277
278
	foreach ($opportunities as $opportunity) {
279
		$opportunityObj = get_entity($opportunity->guid);
280
		$opportunity->title = gc_explode_translation($opportunity->title, $lang);
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
			}else{
320
				$opportunity->apply = 'close';
321
			}
322
		}
323
			
324
		$opportunity->liked = count($liked) > 0;
325
		$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
326
		$opportunity->roletype = elgg_echo($opportunityObj->role_type);
327
		$opportunity->deadline = $opportunityObj->deadline;
328
		$opportunity->programArea = elgg_echo($opportunityObj->program_area);
329
		$opportunity->owner = ($opportunityObj->getOwnerEntity() == $user_entity);
330
		$opportunity->iconURL = $opportunityObj->getIconURL();
331
		$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
332
		$opportunity->description = clean_text(gc_explode_translation($opportunity->description, $lang));
333
		$opportunity->state = $opportunityObj->state;
334
335
	}
336
337
	return $opportunities;
338
}
339
340
341
342
function apply_post($user,$message,$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_info = elgg_echo('missions:you_have_applied_to_mission', array($entity->job_title, $entity->name));
368
369
		$title_applicant_en = elgg_echo('missions:application_notice_sentence_title', array($user_entity->name, $entity->job_title),'en') ;
370
		$title_applicant_fr = elgg_echo('missions:application_notice_sentence_title', array($user_entity->name, $entity->job_title),'fr') ;
371
		
372
		$content_applicant_en = elgg_echo('missions:application_notice_sentence', 'en') . '<br>';
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
373
		$content_applicant_fr = elgg_echo('missions:application_notice_sentence', 'fr') . '<br>';
0 ignored issues
show
Documentation introduced by
'fr' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
374
		$content_applicant_en .= '<br>'.elgg_echo('missions:see_full_profile','en') . ': '; 
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
375
		$content_applicant_fr .= '<br>'.elgg_echo('missions:see_full_profile','fr') . ': '; 
0 ignored issues
show
Documentation introduced by
'fr' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
376
	
377
		$content_applicant_en .= '<a href="'.$user_entity->getURL().'">'.$user_entity->username.'<a/><br>';
378
		$content_applicant_fr .= '<a href="'.$user_entity->getURL().'">'.$user_entity->username.'<a/><br>';
379
		
380
		$content_applicant_en .= elgg_echo('missions:see_full_mission','en') . ': '; 
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
381
		$content_applicant_fr .= elgg_echo('missions:see_full_mission','fr') . ': '; 
0 ignored issues
show
Documentation introduced by
'fr' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
382
	
383
		$content_applicant_en .= '<a href="'.$entity->getURL().'">'.$entity->job_title.'<a/><br>';
384
		$content_applicant_fr .= '<a href="'.$entity->getURL().'">'.$entity->job_title.'<a/><br>';
385
		
386
		$content_applicant_en .=  '<br>'.elgg_echo('missions:from_message',array($user_entity->username),'en').'<br><span style="font-style: italic;">'.$message . '</span><br>';
387
		$content_applicant_fr .=  '<br>'.elgg_echo('missions:from_message',array($user_entity->username),'fr').'<br><span style="font-style: italic;">'.$message . '</span><br>';
388
	
389
		// Lists all educations of the applicant.
390
		$education_list = $user_entity->education;
391
		if(!is_array($education_list)) {
392
			$education_list = array_filter(array($education_list));
393
		}
394 View Code Duplication
		if(!empty($education_list)) {
395
			foreach ($education_list as $education) {
396
				$education = get_entity($education);
397
				$education_ending_en = date("F", mktime(null, null, null, $education->enddate)) . ', ' . $education->endyear;
398
				$education_ending_fr = $cal_month[$education->enddate] . ', ' . $education->endyear;
0 ignored issues
show
Bug introduced by
The variable $cal_month does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
399
	
400
				if ($education->ongoing == 'true') {
401
					$education_ending_en = 'Present';
402
					$education_ending_fr = 'Présent';
403
				}
404
	
405
				$education_beginning_en = date("F", mktime(null, null, null, $education->startdate)) . ', ' . $education->startyear;
406
				$education_beginning_fr = $cal_month[$education->startdate] . ', ' . $education->startyear;
407
	 
408
				// TODO: This section should be in the language files eventually.
409
				$content_applicant_en .= '<br><b><font size="4">' . $education->school . ':</font></b>' . "<br>";
410
				$content_applicant_en .=  $education_beginning_en . ' - ' . $education_ending_en . "<br>";
411
				$content_applicant_en .= '<b>' . $education->degree . ':</b> ' . $education->field . "<br>";
412
	
413
				$content_applicant_fr .= '<br><b><font size="4">' . $education->school . ':</font></b>' . "<br>";
414
				$content_applicant_fr .=  $education_beginning_fr . ' - ' . $education_ending_fr . "<br>";
415
				$content_applicant_fr .= '<b>' . $education->degree . ':</b> ' . $education->field . "<br>";
416
			}
417
		}
418
		
419
		//Lists all work experiences of the applicant.
420
		$experience_list = $user_entity->work;
421
		if(!is_array($experience_list)) {
422
			$experience_list = array_filter(array($experience_list));
423
		}
424 View Code Duplication
		if(!empty($experience_list)) {
425
			foreach ($experience_list as $experience) {
426
				$experience = get_entity($experience);
427
				$experience_ending_en = date("F", mktime(null, null, null, $experience->enddate)) . ', ' . $experience->endyear;
428
				$experience_ending_fr = $cal_month[$experience->enddate] . ', ' . $experience->endyear;
429
				if ($experience->ongoing == 'true') {
430
					$experience_ending_en = 'Present';
431
					$experience_ending_fr = 'Présent';
432
				}
433
				$experience_beginning_en = date("F", mktime(null, null, null, $experience->startdate)) . ', ' . $experience->startyear;
434
				$experience_beginning_fr =  $cal_month[$experience->startdate] . ', ' . $experience->startyear;
435
				
436
				// TODO: This section should be in the language files eventually.
437
				$content_applicant_en .= '<br><b><font size="4">' . $experience->organization . ':</font></b>' . "<br>";
438
				$content_applicant_en .= $experience_beginning_en . ' - ' . $experience_ending_en . "<br>";
439
				$content_applicant_en .= '<b>' . $experience->title . '</b>' . "<br>";
440
				$content_applicant_en .= $experience->responsibilities . "<br><br>";
441
	
442
				$content_applicant_fr .= '<br><b><font size="4">' . $experience->organization . ':</font></b>' . "<br>";
443
				$content_applicant_fr .= $experience_beginning_fr . ' - ' . $experience_ending_fr . "<br>";
444
				$content_applicant_fr .= '<b>' . $experience->title . '</b>' . "<br>";
445
				$content_applicant_fr .= $experience->responsibilities . "<br><br>";
446
			}
447
		}
448
		
449
		// Lists all skills of the applicant.
450
		$skill_list = $user_entity->gc_skills;
451
		if(!is_array($skill_list)) {
452
			$skill_list = array_filter(array($skill_list));
453
		}
454 View Code Duplication
		if(!empty($skill_list)) {
455
			foreach ($skill_list as $skill) {
456
				$skill = get_entity($skill);
457
				$content_applicant_en .= '<b>' . $skill->title . '</b>' . "<br>";
458
				$content_applicant_fr .= '<b>' . $skill->title . '</b>' . "<br>";
459
			}
460
		}
461
		
462
		// Sends the application via email and notification.
463
		$content_applicant_en .= elgg_view('output/url', array(
464
				'href' => elgg_get_site_url() . 'missions/mission-offer/' . $entity->guid . '/' . $user_entity->guid,
465
				'text' => '<br>'.elgg_echo('missions:offer','en')
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
466
		));
467
	
468
		$content_applicant_fr .= elgg_view('output/url', array(
469
				'href' => elgg_get_site_url() . 'missions/mission-offer/' . $entity->guid . '/' . $user_entity->guid,
470
				'text' => '<br>'.elgg_echo('missions:offer','fr')
0 ignored issues
show
Documentation introduced by
'fr' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
471
		));
472
	
473
		$subject = elgg_echo('missions:application_to','en') . $entity->job_title.' | '.elgg_echo('missions:application_to','fr') . $entity->job_title;
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'fr' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
474
475
		mm_notify_user($entity->guid, $user_entity->guid, $subject,$title_applicant_en,$title_applicant_fr, $content_applicant_en, $content_applicant_fr);
476
		
477
	}
478
	
479
	// Opt in applicant if they are not opted in yet.
480
	if(!check_if_opted_in($user_entity)) {
481
		$user_entity->opt_in_missions = 'gcconnex_profile:opt:yes';
482
		$user_entity->save();
483
	}
484
	return $message_info;
0 ignored issues
show
Bug introduced by
The variable $message_info 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...
485
486
}
487
488
function withdraw_post($user, $guid, $lang)
489
{
490
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
491
	if (!$user_entity) {
492
		return "User was not found. Please try a different GUID, username, or email address";
493
	}
494
	if (!$user_entity instanceof ElggUser) {
495
		return "Invalid user. Please try a different GUID, username, or email address";
496
	}
497
498
	if (!elgg_is_logged_in()) {
499
		login($user_entity);
500
	}
501
502
	$entity = get_entity($guid);
503
	if (!$entity) {
504
		return "Opportunity was not found. Please try a different GUID";
505
	}
506
	if (!elgg_instanceof($entity, 'object', 'mission')) {
507
		return "Invalid opportunity. Please try a different GUID";
508
	}
509
510
	// Deletes the tentative relationship between mission and applicant.
511 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
512
	$message_return = 'missions:declination_has_been_sent';
513
	remove_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid);
514
}
515
if(check_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid)) {
516
	$message_return = 'missions:withdrawal_has_been_sent';
517
	remove_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
518
}
519 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid)) {
520
	$message_return = 'missions:declination_has_been_sent';
521
	remove_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid);
522
}
523 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid)) {
524
	$message_return = 'missions:withdrawal_has_been_sent';
525
	remove_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid);
526
  mm_complete_mission_inprogress_reports($entity, true);
527
}
528
	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...
529
	
530
531
}
532
533
function accept_post($user, $guid, $lang)
534
{
535
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
536
	if (!$user_entity) {
537
		return "User was not found. Please try a different GUID, username, or email address";
538
	}
539
	if (!$user_entity instanceof ElggUser) {
540
		return "Invalid user. Please try a different GUID, username, or email address";
541
	}
542
543
	if (!elgg_is_logged_in()) {
544
		login($user_entity);
545
	}
546
547
	$entity = get_entity($guid);
548
	if (!$entity) {
549
		return "Opportunity was not found. Please try a different GUID";
550
	}
551
	if (!elgg_instanceof($entity, 'object', 'mission')) {
552
		return "Invalid opportunity. Please try a different GUID";
553
	}
554
555 View Code Duplication
	if(check_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid)) {
556
557
		remove_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid);
558
		add_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid);
559
	}
560
	
561
	return elgg_echo('missions:now_participating_in_mission', array($entity->job_title));
562
}
563