Completed
Push — function_create_opt_api ( 13090a )
by
unknown
20:14
created

opportunity.php ➔ create_opportinities1()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 16
nop 3
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
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
		"message" => array('type' => 'string', 'required' => true, 'default' => ""),
57
		"guid" => array('type' => 'int', 'required' => true),
58
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
59
	),
60
	'Retrieves a opportunity based on user id and opportunity id',
61
	'POST',
62
	true,
63
	false
64
);
65
66
elgg_ws_expose_function(
67
	"accept.post",
68
	"accept_post",
69
	array(
70
		"user" => array('type' => 'string', 'required' => true),
71
		"guid" => array('type' => 'int', 'required' => true),
72
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
73
	),
74
	'Retrieves a opportunity based on user id and opportunity id',
75
	'POST',
76
	true,
77
	false
78
);
79
80
elgg_ws_expose_function(
81
	"accept.post",
82
	"accept_post",
83
	array(
84
		"user" => array('type' => 'string', 'required' => true),
85
		"guid" => array('type' => 'int', 'required' => true),
86
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
87
	),
88
	'Retrieves a opportunity based on user id and opportunity id',
89
	'POST',
90
	true,
91
	false
92
);
93
94
elgg_ws_expose_function(
95
	"create.opportinities1",
96
	"create_opportinities1",
97
	array(
98
		"user" => array('type' => 'string', 'required' => true),
99
		"message" => array('type' => 'string', 'required' => true),
100
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
101
	),
102
	'Retrieves a opportunity based on user id and opportunity id',
103
	'POST',
104
	true,
105
	false
106
);
107
108
function get_opportunity($user, $guid, $lang)
109
{
110
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
111
	if (!$user_entity) {
112
		return "User was not found. Please try a different GUID, username, or email address";
113
	}
114
	if (!$user_entity instanceof ElggUser) {
115
		return "Invalid user. Please try a different GUID, username, or email address";
116
	}
117
118
	if (!elgg_is_logged_in()) {
119
		login($user_entity);
120
	}
121
122
	$entity = get_entity($guid);
123
	if (!$entity) {
124
		return "Opportunity was not found. Please try a different GUID";
125
	}
126
	if (!elgg_instanceof($entity, 'object', 'mission')) {
127
		return "Invalid opportunity. Please try a different GUID";
128
	}
129
130
131
132
	$opportunities = elgg_list_entities(array(
133
		'type' => 'object',
134
		'subtype' => 'mission',
135
		'guid' => $guid
136
	));
137
	$opportunity = json_decode($opportunities)[0];
138
139
	$opportunity->title = gc_explode_translation($opportunity->title, $lang);
140
141
	$likes = elgg_get_annotations(array(
142
		'guid' => $opportunity->guid,
143
		'annotation_name' => 'likes'
144
	));
145
	$opportunity->likes = count($likes);
146
147
	$liked = elgg_get_annotations(array(
148
		'guid' => $opportunity->guid,
149
		'annotation_owner_guid' => $user_entity->guid,
150
		'annotation_name' => 'likes'
151
	));
152
	$opportunity->liked = count($liked) > 0;
153
154
	$opportunity->comments = get_entity_comments($opportunity->guid);
155
156
	$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
157
	$opportunity->description = gc_explode_translation($opportunity->description, $lang);
158
159
	$opportunityObj = get_entity($opportunity->guid);
160
	$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
161
	$opportunity->roletype = elgg_echo($opportunityObj->role_type);
162
	//$opportunity->programArea = elgg_echo('missions:program_area') . ": " . elgg_echo($opportunityObj->program_area); //This should work and translate to user lang but doesnt
163
	$opportunity->programArea = elgg_echo($opportunityObj->program_area);
164
	$opportunity->numOpportunities = $opportunityObj->number;
165
	$opportunity->idealStart = $opportunityObj->start_date;
166
	$opportunity->idealComplete = $opportunityObj->complete_date;
167
	$opportunity->deadline = $opportunityObj->deadline;
168
	$opportunity->oppVirtual = $opportunityObj->remotely;
169
	$opportunity->oppOnlyIn = $opportunityObj->openess;
170
	$opportunity->location = elgg_echo($opportunityObj->location);
171
	$opportunity->security = elgg_echo($opportunityObj->security);
172
	$opportunity->skills = $opportunityObj->key_skills;
173
	//$opportunity->participants = $opportunityObj->;
174
	//$opportunity->applicants = $opportunityObj->;
175
	$opportunity->timezone = elgg_echo($opportunityObj->timezone);
176
	$opportunity->timecommitment = $opportunityObj->time_commitment;
177
	$opportunity->department = $opportunityObj->department;
178
	$opportunity->state = $opportunityObj->state;
179
180
	//Language metadata
181
	$unpacked_array = mm_unpack_mission($opportunityObj);
182
	$unpacked_language = '';
183 View Code Duplication
	if (! empty($unpacked_array['lwc_english']) || ! empty($unpacked_array['lwc_french'])) {
184
  	$unpacked_language .= '<b>' . elgg_echo('missions:written_comprehension') . ': </b>';
185
  	if (! empty($unpacked_array['lwc_english'])) {
186
      $unpacked_language .= '<span name="mission-lwc-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwc_english'])) . '</span> ';
187
  	}
188
  	if (! empty($unpacked_array['lwc_french'])) {
189
      $unpacked_language .= '<span name="mission-lwc-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwc_french'])) . '</span>';
190
  	}
191
		$unpacked_language .= '<br>';
192
	}
193 View Code Duplication
	if (! empty($unpacked_array['lwe_english']) || ! empty($unpacked_array['lwe_french'])) {
194
		$unpacked_language .= '<b>' . elgg_echo('missions:written_expression') . ': </b>';
195
		if (! empty($unpacked_array['lwe_english'])) {
196
	  	$unpacked_language .= '<span name="mission-lwe-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwe_english'])) . '</span> ';
197
	 	}
198
	  if (! empty($unpacked_array['lwe_french'])) {
199
	  	$unpacked_language .= '<span name="mission-lwe-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwe_french'])) . '</span>';
200
	  }
201
	  	$unpacked_language .= '<br>';
202
	}
203 View Code Duplication
	if (! empty($unpacked_array['lop_english']) || ! empty($unpacked_array['lop_french'])) {
204
		$unpacked_language .= '<b>' . elgg_echo('missions:oral_proficiency') . ': </b>';
205
		if (! empty($unpacked_array['lop_english'])) {
206
			$unpacked_language .= '<span name="mission-lop-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lop_english'])) . '</span> ';
207
		}
208
		if (! empty($unpacked_array['lop_french'])) {
209
			$unpacked_language .= '<span name="mission-lop-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lop_french'])) . '</span>';
210
		}
211
		$unpacked_language .= '<br>';
212
	}
213
	if (empty($unpacked_language)) {
214
		$unpacked_language = '<span name="no-languages">' . elgg_echo('missions:none_required') . '</span>';
215
	}
216
	$opportunity->languageRequirements = $unpacked_language;
217
218
	//scheduling metadata
219
	$unpacked_time = '';
220
	if ($opportunityObj->mon_start) {
221
		$unpacked_time .= '<b>' . elgg_echo('missions:mon') . ': </b>';
222
	 	$unpacked_time .= $opportunityObj->mon_start . elgg_echo('missions:to') .  $opportunityObj->mon_duration . '<br>';
223
	}
224
	if ($opportunityObj->tue_start) {
225
		$unpacked_time .= '<b>' . elgg_echo('missions:tue') . ': </b>';
226
		$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>';
227
	}
228
	if ($opportunityObj->wed_start) {
229
		$unpacked_time .= '<b>' . elgg_echo('missions:wed') . ': </b>';
230
		$unpacked_time .=  $opportunityObj->wed_start  . elgg_echo('missions:to') . $opportunityObj->wed_duration . '<br>';
231
	}
232
	if ($opportunityObj->thu_start) {
233
		$unpacked_time .= '<b>' . elgg_echo('missions:thu') . ': </b>';
234
		$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>';
235
	}
236
	if ($opportunityObj->fri_start) {
237
	  $unpacked_time .= '<b>' . elgg_echo('missions:fri') . ': </b>';
238
	  $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>';
239
	}
240
	if ($opportunityObj->sat_start) {
241
	  $unpacked_time .= '<b>' . elgg_echo('missions:sat') . ': </b>';
242
	  $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>';
243
	}
244
	if ($opportunityObj->sun_start) {
245
	  $unpacked_time .= '<b>' . elgg_echo('missions:sun') . ': </b>';
246
	  $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>';
247
	}
248
	if (empty($unpacked_time)) {
249
	  $unpacked_time = '<span name="no-times">' . elgg_echo('missions:none_required') . '</span>';
250
	}
251
	$opportunity->schedulingRequirements = $unpacked_time;
252
253
	
254
	return $opportunity;
255
}
256
257
function get_opportunities($user, $limit, $offset, $filters, $lang)
258
{
259
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
260
	if (!$user_entity) {
261
		return "User was not found. Please try a different GUID, username, or email address";
262
	}
263
	if (!$user_entity instanceof ElggUser) {
264
		return "Invalid user. Please try a different GUID, username, or email address";
265
	}
266
267
	if (!elgg_is_logged_in()) {
268
		login($user_entity);
269
	}
270
271
	$filter_data = json_decode($filters);
272
	if (!empty($filter_data)) {
273
		$params = array(
274
			'type' => 'object',
275
			'subtype' => 'mission',
276
			'limit' => $limit,
277
			'offset' => $offset
278
		);
279
280
		if ($filter_data->type) {
281
			$params['metadata_name'] = 'job_type';
282
			$params['metadata_value'] = $filter_data->type;
283
		}
284
285 View Code Duplication
		if ($filter_data->name) {
286
			$db_prefix = elgg_get_config('dbprefix');
287
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
288
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
289
		}
290
291
		if ($filter_data->mine) {
292
			$all_opportunities = elgg_list_entities_from_relationship($params);
293
		} else {
294
			$all_opportunities = elgg_list_entities_from_metadata($params);
295
		}
296
	} else {
297
		$all_opportunities = elgg_list_entities(array(
298
			'type' => 'object',
299
			'subtype' => 'mission',
300
			'limit' => $limit,
301
			'offset' => $offset
302
		));
303
	}
304
305
	$opportunities = json_decode($all_opportunities);
306
307
	foreach ($opportunities as $opportunity) {
308
		$opportunityObj = get_entity($opportunity->guid);
309
		$opportunity->title = gc_explode_translation($opportunity->title, $lang);
310
	
311
		$likes = elgg_get_annotations(array(
312
			'guid' => $opportunity->guid,
313
			'annotation_name' => 'likes'
314
		));
315
		$opportunity->likes = count($likes);
316
317
		$liked = elgg_get_annotations(array(
318
			'guid' => $opportunity->guid,
319
			'annotation_owner_guid' => $user_entity->guid,
320
			'annotation_name' => 'likes'
321
		));
322
		if($opportunity->owner_guid != $user_entity->guid){
323
			
324
			if($opportunityObj->state != 'completed' && $opportunityObj->state != 'cancelled'){
325
				$relationship_count = elgg_get_entities_from_relationship(array(
326
					'relationship' => 'mission_accepted',
327
					'relationship_guid' => $opportunity->guid,
328
					'count' => true
329
				));	
330
				if($relationship_count < $opportunityObj->number) {
331
				
332
					$opportunity->apply = 'mission_apply'; // user can apply
333
				}
334
					
335
				if(check_entity_relationship($opportunity->guid, 'mission_tentative', $user_entity->guid)) {
336
					//console.log($opportunity->title);
337
					$opportunity->apply = 'tentative'; // user can accecpt offer
338
				}
339
				if(check_entity_relationship($opportunity->guid, 'mission_offered', $user_entity->guid)) {
340
					$opportunity->apply = 'offered'; // user can accecpt offer
341
					
342
				}
343
				if(check_entity_relationship($opportunity->guid, 'mission_accepted', $user_entity->guid) ||
344
				check_entity_relationship($opportunity->guid, 'mission_applied', $user_entity->guid)) {
345
					$opportunity->apply = 'withdraw'; // user can accecpt offer
346
				
347
				}
348
			}else{
349
				$opportunity->apply = 'close';
350
			}
351
		}
352
			
353
		$opportunity->liked = count($liked) > 0;
354
		$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
355
		$opportunity->roletype = elgg_echo($opportunityObj->role_type);
356
		$opportunity->deadline = $opportunityObj->deadline;
357
		$opportunity->programArea = elgg_echo($opportunityObj->program_area);
358
		$opportunity->owner = ($opportunityObj->getOwnerEntity() == $user_entity);
359
		$opportunity->iconURL = $opportunityObj->getIconURL();
360
		$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
361
		$opportunity->description = clean_text(gc_explode_translation($opportunity->description, $lang));
362
		$opportunity->state = $opportunityObj->state;
363
364
	}
365
366
	return $opportunities;
367
}
368
369
370
371
function apply_post($user,$message,$guid, $lang)
372
{
373
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
374
	if (!$user_entity) {
375
		return "User was not found. Please try a different GUID, username, or email address";
376
	}
377
	if (!$user_entity instanceof ElggUser) {
378
		return "Invalid user. Please try a different GUID, username, or email address";
379
	}
380
381
	if (!elgg_is_logged_in()) {
382
		login($user_entity);
383
	}
384
385
	$entity = get_entity($guid);
386
	if (!$entity) {
387
		return "Opportunity was not found. Please try a different GUID";
388
	}
389
	if (!elgg_instanceof($entity, 'object', 'mission')) {
390
		return "Invalid opportunity. Please try a different GUID";
391
	}
392
393
	// Creates an applied relationship between user and mission if there is no relationship there already.
394
	if(!check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid) && !check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
395
		add_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
396
		$message_info = elgg_echo('missions:you_have_applied_to_mission', array($entity->job_title, $entity->name));
397
398
		$title_applicant_en = elgg_echo('missions:application_notice_sentence_title', array($user_entity->name, $entity->job_title),'en') ;
399
		$title_applicant_fr = elgg_echo('missions:application_notice_sentence_title', array($user_entity->name, $entity->job_title),'fr') ;
400
		
401
		$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...
402
		$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...
403
		$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...
404
		$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...
405
	
406
		$content_applicant_en .= '<a href="'.$user_entity->getURL().'">'.$user_entity->username.'<a/><br>';
407
		$content_applicant_fr .= '<a href="'.$user_entity->getURL().'">'.$user_entity->username.'<a/><br>';
408
		
409
		$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...
410
		$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...
411
	
412
		$content_applicant_en .= '<a href="'.$entity->getURL().'">'.$entity->job_title.'<a/><br>';
413
		$content_applicant_fr .= '<a href="'.$entity->getURL().'">'.$entity->job_title.'<a/><br>';
414
		
415
		$content_applicant_en .=  '<br>'.elgg_echo('missions:from_message',array($user_entity->username),'en').'<br><span style="font-style: italic;">'.$message . '</span><br>';
416
		$content_applicant_fr .=  '<br>'.elgg_echo('missions:from_message',array($user_entity->username),'fr').'<br><span style="font-style: italic;">'.$message . '</span><br>';
417
	
418
		// Lists all educations of the applicant.
419
		$education_list = $user_entity->education;
420
		if(!is_array($education_list)) {
421
			$education_list = array_filter(array($education_list));
422
		}
423 View Code Duplication
		if(!empty($education_list)) {
424
			foreach ($education_list as $education) {
425
				$education = get_entity($education);
426
				$education_ending_en = date("F", mktime(null, null, null, $education->enddate)) . ', ' . $education->endyear;
427
				$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...
428
	
429
				if ($education->ongoing == 'true') {
430
					$education_ending_en = 'Present';
431
					$education_ending_fr = 'Présent';
432
				}
433
	
434
				$education_beginning_en = date("F", mktime(null, null, null, $education->startdate)) . ', ' . $education->startyear;
435
				$education_beginning_fr = $cal_month[$education->startdate] . ', ' . $education->startyear;
436
	 
437
				// TODO: This section should be in the language files eventually.
438
				$content_applicant_en .= '<br><b><font size="4">' . $education->school . ':</font></b>' . "<br>";
439
				$content_applicant_en .=  $education_beginning_en . ' - ' . $education_ending_en . "<br>";
440
				$content_applicant_en .= '<b>' . $education->degree . ':</b> ' . $education->field . "<br>";
441
	
442
				$content_applicant_fr .= '<br><b><font size="4">' . $education->school . ':</font></b>' . "<br>";
443
				$content_applicant_fr .=  $education_beginning_fr . ' - ' . $education_ending_fr . "<br>";
444
				$content_applicant_fr .= '<b>' . $education->degree . ':</b> ' . $education->field . "<br>";
445
			}
446
		}
447
		
448
		//Lists all work experiences of the applicant.
449
		$experience_list = $user_entity->work;
450
		if(!is_array($experience_list)) {
451
			$experience_list = array_filter(array($experience_list));
452
		}
453 View Code Duplication
		if(!empty($experience_list)) {
454
			foreach ($experience_list as $experience) {
455
				$experience = get_entity($experience);
456
				$experience_ending_en = date("F", mktime(null, null, null, $experience->enddate)) . ', ' . $experience->endyear;
457
				$experience_ending_fr = $cal_month[$experience->enddate] . ', ' . $experience->endyear;
458
				if ($experience->ongoing == 'true') {
459
					$experience_ending_en = 'Present';
460
					$experience_ending_fr = 'Présent';
461
				}
462
				$experience_beginning_en = date("F", mktime(null, null, null, $experience->startdate)) . ', ' . $experience->startyear;
463
				$experience_beginning_fr =  $cal_month[$experience->startdate] . ', ' . $experience->startyear;
464
				
465
				// TODO: This section should be in the language files eventually.
466
				$content_applicant_en .= '<br><b><font size="4">' . $experience->organization . ':</font></b>' . "<br>";
467
				$content_applicant_en .= $experience_beginning_en . ' - ' . $experience_ending_en . "<br>";
468
				$content_applicant_en .= '<b>' . $experience->title . '</b>' . "<br>";
469
				$content_applicant_en .= $experience->responsibilities . "<br><br>";
470
	
471
				$content_applicant_fr .= '<br><b><font size="4">' . $experience->organization . ':</font></b>' . "<br>";
472
				$content_applicant_fr .= $experience_beginning_fr . ' - ' . $experience_ending_fr . "<br>";
473
				$content_applicant_fr .= '<b>' . $experience->title . '</b>' . "<br>";
474
				$content_applicant_fr .= $experience->responsibilities . "<br><br>";
475
			}
476
		}
477
		
478
		// Lists all skills of the applicant.
479
		$skill_list = $user_entity->gc_skills;
480
		if(!is_array($skill_list)) {
481
			$skill_list = array_filter(array($skill_list));
482
		}
483 View Code Duplication
		if(!empty($skill_list)) {
484
			foreach ($skill_list as $skill) {
485
				$skill = get_entity($skill);
486
				$content_applicant_en .= '<b>' . $skill->title . '</b>' . "<br>";
487
				$content_applicant_fr .= '<b>' . $skill->title . '</b>' . "<br>";
488
			}
489
		}
490
		
491
		// Sends the application via email and notification.
492
		$content_applicant_en .= elgg_view('output/url', array(
493
				'href' => elgg_get_site_url() . 'missions/mission-offer/' . $entity->guid . '/' . $user_entity->guid,
494
				'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...
495
		));
496
	
497
		$content_applicant_fr .= elgg_view('output/url', array(
498
				'href' => elgg_get_site_url() . 'missions/mission-offer/' . $entity->guid . '/' . $user_entity->guid,
499
				'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...
500
		));
501
	
502
		$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...
503
504
		mm_notify_user($entity->guid, $user_entity->guid, $subject,$title_applicant_en,$title_applicant_fr, $content_applicant_en, $content_applicant_fr);
505
		
506
	}
507
	
508
	// Opt in applicant if they are not opted in yet.
509
	if(!check_if_opted_in($user_entity)) {
510
		$user_entity->opt_in_missions = 'gcconnex_profile:opt:yes';
511
		$user_entity->save();
512
	}
513
	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...
514
515
}
516
517
function withdraw_post($user,$message,  $guid, $lang)
518
{
519
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
520
	if (!$user_entity) {
521
		return "User was not found. Please try a different GUID, username, or email address";
522
	}
523
	if (!$user_entity instanceof ElggUser) {
524
		return "Invalid user. Please try a different GUID, username, or email address";
525
	}
526
527
	if (!elgg_is_logged_in()) {
528
		login($user_entity);
529
	}
530
531
	$entity = get_entity($guid);
532
	if (!$entity) {
533
		return "Opportunity was not found. Please try a different GUID";
534
	}
535
	if (!elgg_instanceof($entity, 'object', 'mission')) {
536
		return "Invalid opportunity. Please try a different GUID";
537
	}
538
539
	// Deletes the tentative relationship between mission and applicant.
540 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
541
	$message_return = 'missions:declination_has_been_sent';
542
	remove_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid);
543
}
544
if(check_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid)) {
545
	$message_return = 'missions:withdrawal_has_been_sent';
546
	remove_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
547
}
548 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid)) {
549
	$message_return = 'missions:declination_has_been_sent';
550
	remove_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid);
551
}
552 View Code Duplication
if(check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid)) {
553
	$message_return = 'missions:withdrawal_has_been_sent';
554
	remove_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid);
555
  mm_complete_mission_inprogress_reports($entity, true);
556
}
557
558
$reason = array('workload', 'interest', 'engagement', 'approval');
559
if (in_array($message,$reason)){
560
	$reasonEn = elgg_echo('missions:decline:'.$message,'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...
561
	$reasonFr = elgg_echo('missions:decline:'.$message,'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...
562
}else{
563
	$reasonEn = $message;
564
	$reasonFr = $message;
565
}
566
567
$mission_link = '<a href="'.$entity->getURL().'" >'.elgg_get_excerpt($entity->job_title, elgg_get_plugin_setting('mission_job_title_card_cutoff', 'missions')).' </a>';
568
$subject = elgg_echo('missions:applicant_leaves', array($user_entity->name),'en')." | ". elgg_echo('missions:applicant_leaves', array($user_entity->name),'fr');
569
570
$withdrawnEn .= elgg_echo('missions:applicant_leaves_more', array($user_entity->name),'en') . $mission_link . '.' . "\n";
0 ignored issues
show
Bug introduced by
The variable $withdrawnEn 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...
571
$withdrawnReasonEn .= elgg_echo('missions:reason_given', array($reasonEn),'en');
0 ignored issues
show
Bug introduced by
The variable $withdrawnReasonEn does not exist. Did you mean $reason?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
572
573
$withdrawnFr .= elgg_echo('missions:applicant_leaves_more', array($user_entity->name),'fr') . $mission_link . '.' . "\n";
0 ignored issues
show
Bug introduced by
The variable $withdrawnFr 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...
574
$withdrawnReasonFr .= elgg_echo('missions:reason_given', array($reasonFr),'fr');
0 ignored issues
show
Bug introduced by
The variable $withdrawnReasonFr does not exist. Did you mean $reason?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
575
576
mm_notify_user($entity->guid, $user_entity->guid, $subject, $withdrawnEn, $withdrawnFr,$withdrawnReasonEn ,$withdrawnReasonFr );
577
578
579
	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...
580
	
581
582
}
583
584
function accept_post($user, $guid, $lang)
585
{
586
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
587
	if (!$user_entity) {
588
		return "User was not found. Please try a different GUID, username, or email address";
589
	}
590
	if (!$user_entity instanceof ElggUser) {
591
		return "Invalid user. Please try a different GUID, username, or email address";
592
	}
593
594
	if (!elgg_is_logged_in()) {
595
		login($user_entity);
596
	}
597
598
	$entity = get_entity($guid);
599
	if (!$entity) {
600
		return "Opportunity was not found. Please try a different GUID";
601
	}
602
	if (!elgg_instanceof($entity, 'object', 'mission')) {
603
		return "Invalid opportunity. Please try a different GUID";
604
	}
605
606 View Code Duplication
	if(check_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid)) {
607
608
		remove_entity_relationship($entity->guid, 'mission_offered', $user_entity->guid);
609
		add_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid);
610
	}
611
	$mission_link = '<a href="'.$entity->getURL().'">'.$entity->title.'</a>';
612
613
		// notify participant
614
		$subject = elgg_echo('missions:participating_in', array(elgg_get_excerpt($entity->job_title, elgg_get_plugin_setting('mission_job_title_card_cutoff', 'missions'))),'en').' | '.elgg_echo('missions:participating_in', array(elgg_get_excerpt($entity->job_title, elgg_get_plugin_setting('mission_job_title_card_cutoff', 'missions'))),'fr');
615
		$message_en = elgg_echo('missions:participating_in_more', array($user_entity->name),'en') . $mission_link . '.';
616
		$message_fr = elgg_echo('missions:participating_in_more', array($user_entity->name),'fr') . $mission_link . '.';
617
618
		mm_notify_user($user_entity->guid, $entity->guid, $subject, '','',$message_en,$message_fr);
619
620
		// notify owner
621
		$subject = elgg_echo( 'missions:participating_in2', array($user_entity->name),'en').' | '.elgg_echo( 'missions:participating_in2', array($user_entity->name),'fr');
622
		$message_en = elgg_echo('missions:participating_in2_more', array($user_entity->name),'en') . $mission_link . '.';
623
		$message_fr = elgg_echo('missions:participating_in2_more', array($user_entity->name),'fr') . $mission_link . '.';
624
625
		mm_notify_user($entity->guid, $user_entity->guid, $subject, '','',$message_en,$message_fr);
626
	
627
	return elgg_echo('missions:now_participating_in_mission', array($entity->job_title));
628
}
629
630
function create_opportinities1($user, $message, $lang)
631
{
632
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
633
	if (!$user_entity) {
634
		return "User was not found. Please try a different GUID, username, or email address";
635
	}
636
	if (!$user_entity instanceof ElggUser) {
637
		return "Invalid user. Please try a different GUID, username, or email address";
638
	}
639
640
	if (!elgg_is_logged_in()) {
641
		login($user_entity);
642
	}
643
	
644
error_log('message :'.$message .'user: '.$user. 'lang: '.$lang);
645
646
	return $message;
647
}
648