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