-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudyInfoGenerator.php
345 lines (307 loc) · 15.2 KB
/
StudyInfoGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
namespace Drupal\csis_helpers\Utils;
use Drupal\taxonomy\Entity\Term;
use Drupal\group\Entity\GroupContent;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Class StudyInfoGenerator.
*/
class StudyInfoGenerator {
/**
* Extract information from STUDY's GL_STEP **node** entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @return array with Study informations
*/
public function generateStudyInfoNode(\Drupal\Core\Entity\EntityInterface $entity) {
$groupid = array();
$groupuuid = array();
$groupEmikatID = null;
$datapackageUUID = null;
$groupArea = null;
$groupName = null;
$groupEeaName = null;
$groupCityCode = null;
$calculationStatus = null;
$relations = GroupContent::loadByEntity($entity);
$studyPreset = array();
$studyPresets = array(); // stores ONLY 1 preset, will be replaced by $studyScenarios
$studyScenarios = array(); // replacing $studyPresets in the future
$adaptedScenario = False; // declares whether or not an Adaptation Strategy has been defined by the user
$stepName = null;
// write-permissions inside a Study need to be checked upon a group-member level and not on user level
$userAccount = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userAccount->id());
$has_user_special_roles = false;
$isAnonymous = true;
$isMember = false;
if (!$userAccount->isAnonymous()) {
$isAnonymous = false;
}
foreach ($relations as $relation) {
$groupid[] = $relation->getGroup()->id();
$groupuuid[] = $relation->getGroup()->uuid();
$groupEmikatID = $relation->getGroup()->get('field_emikat_id')->value;
$groupEmikatID = empty($groupEmikatID) ? -1 : intval($groupEmikatID);
$calculationStatus = $relation->getGroup()->get('field_calculation_status')->value;
$groupName = $relation->getGroup()->get('field_name')->value;
$groupDatapackageID = $relation->getGroup()->get('field_data_package')->target_id;
$groupArea = $relation->getGroup()->get('field_area')->value;
$groupCityTerm = (!$relation->getGroup()->get('field_city_region')->isEmpty() ? Term::load($relation->getGroup()->get('field_city_region')->target_id) : false);
$member = $relation->getGroup()->getMember($userAccount);
if ($member) {
$isMember = true;
$memberRoles = $member->getRoles();
foreach ($memberRoles as $role) {
// GL-steps should be writable for owners and team members, but not observers
if ($role->id() == "study-owner" || $role->id() == "study-team") {
$has_user_special_roles = true;
break;
}
}
}
// bypass member roles permissions for admins
if ($user->hasRole("administrator")) {
$has_user_special_roles = true;
$isMember = true;
}
// get Study presets (combination of time-period, emission scenario and event frequency) from Paragraph reference
foreach ($relation->getGroup()->get('field_study_presets') as $paraReference) {
$paragraph = Paragraph::load($paraReference->target_id);
if ($paragraph && $paragraph->getType() == "variable_set") {
// load all the necessary taxonomy terms
$termEmScenario = Term::load($paragraph->get('field_emission_scenario')->target_id);
$termEventFreq = Term::load($paragraph->get('field_event_frequency')->target_id);
$termTimePeriod = Term::load($paragraph->get('field_time_period')->target_id);
//$termStudyVariant = Term::load($paragraph->get('field_study_variant')->target_id);
if (!$termTimePeriod || !$termEmScenario || !$termEventFreq) {
// although in the Study preset (aka Study scenario) all fields are required
// and therefore must exist if the paragraph exists, it could happen that
// the related taxonomy term has been removed from the system
// -> in such a case break for-loop and don't add an incomplete preset
// should be resolved once Drupal introduces a possiblity to unpublish taxonomy terms
break;
}
// extract preset label and all needed values from those terms
$studyPreset['label'] = $paragraph->get('field_label')->value;
$studyPreset['time_period'] = $termTimePeriod->get('field_var_meaning')->value;
$studyPreset['emission_scenario'] = $termEmScenario->get('field_var_meaning')->value;
$studyPreset['event_frequency'] = $termEventFreq->get('field_var_meaning')->value;
//$studyPreset['study_variant'] = $termStudyVariant->get('field_var_meaning')->value;
array_push($studyScenarios, $studyPreset);
// this can later be removed, once we switch to the $studyScenarios object
$studyPresets['time_period'] = $termTimePeriod->get('field_var_meaning')->value;
$studyPresets['emission_scenario'] = $termEmScenario->get('field_var_meaning')->value;
$studyPresets['event_frequency'] = $termEventFreq->get('field_var_meaning')->value;
//$studyPresets['study_variant'] = $termStudyVariant->get('field_var_meaning')->value;
}
}
$activeScenario = $relation->getGroup()->get('field_active_scenario')->value;
if ($activeScenario) {
$paragraph = Paragraph::load($activeScenario);
$termEmScenario = Term::load($paragraph->get('field_emission_scenario')->target_id);
$termEventFreq = Term::load($paragraph->get('field_event_frequency')->target_id);
$termTimePeriod = Term::load($paragraph->get('field_time_period')->target_id);
//$termStudyVariant = Term::load($paragraph->get('field_study_variant')->target_id);
$studyPresets['time_period'] = $termTimePeriod->get('field_var_meaning')->value;
$studyPresets['emission_scenario'] = $termEmScenario->get('field_var_meaning')->value;
$studyPresets['event_frequency'] = $termEventFreq->get('field_var_meaning')->value;
//$studyPresets['study_variant'] = $termStudyVariant->get('field_var_meaning')->value;
}
// check if Study has an Adaptation Project (ap) set
$ap = $relation->getGroup()->getContentEntities("group_node:adaptation_project");
if ($ap) {
$adaptedScenario = True;
}
$euTerm = (!$entity->get('field_field_eu_gl_methodology')->isEmpty() ? Term::load($entity->get('field_field_eu_gl_methodology')->target_id) : false);
if ($euTerm) {
$termID = $euTerm->get('field_eu_gl_taxonomy_id')->value;
switch ($termID) {
case "eu-gl:hazard-characterization":
$stepName = "hazard";
break;
case "eu-gl:hazard-characterization:local-effects":
$stepName = "hazard_local";
break;
case "eu-gl:exposure-evaluation":
$stepName = "exposition";
break;
case "eu-gl:vulnerability-analysis":
$stepName = "vulnerability";
break;
case "eu-gl:risk-and-impact-assessment":
$stepName = "risk";
break;
case "eu-gl:adaptation-options:identification":
$stepName = "adaptation_identification";
break;
case "eu-gl:adaptation-options:appraisal":
$stepName = "adaptation_appraisal";
break;
case "eu-gl:adaptation-action-plans:implementation":
$stepName = "adaptation_implementation";
break;
default:
$stepName = "unknown_EU_GL_ID";
}
}
if ($groupDatapackageID) {
$datapackageNode = \Drupal\node\Entity\Node::load($groupDatapackageID);
if ($datapackageNode) {
$datapackageUUID = $datapackageNode->get('uuid')->value;
}
}
if ($groupCityTerm) {
$groupEeaName = $groupCityTerm->get('field_eea_city_profile_name')->value;
$groupCityCode = $groupCityTerm->get('field_city_code')->value;
}
}
$studyEntityInfo = array(
'name' => $groupName,
'step' => $entity->id(),
'step_uuid' => $entity->uuid(),
'step_name' => $stepName,
'study' => (empty($groupid) ? -1 : $groupid[0]), //deprecated -> use entitiyinfo.study.id
'study_uuid' => (empty($groupuuid) ? -1 : $groupuuid[0]), //deprecated -> use entitiyinfo.study.uuid
'id' => (empty($groupid) ? -1 : $groupid[0]), //overwrites $entityinfo.id from csis_helpers_node_entity_info
'uuid' => (empty($groupuuid) ? -1 : $groupuuid[0]), //overwrites $entityinfo.uuid from csis_helpers_node_entity_info
'study_emikat_id' => $groupEmikatID,
'calculation_status' => $calculationStatus,
'study_datapackage_uuid' => $datapackageUUID,
'study_area' => $groupArea,
'eea_city_name' => $groupEeaName,
'city_code' => $groupCityCode,
'study_presets' => $studyPresets,
'study_scenarios' => $studyScenarios,
'has_adapted_scenario' => $adaptedScenario,
'is_anonymous' => $isAnonymous,
'is_member' => $isMember,
'write_permissions' => ($has_user_special_roles ? 1 : 0),
);
return $studyEntityInfo;
}
/**
* sets for each Study group the $entityInfo variable
* Extract information from STUDY **GROUP** entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @return array with Study informations
*/
public function generateStudyInfoGroup(\Drupal\Core\Entity\EntityInterface $entity) {
$datapackageUUID = null;
$eeaName = null;
$cityCode = null;
$studyPreset = array();
$studyPresets = array(); // stores ONLY 1 preset, will be replaced by $studyScenarios
$studyScenarios = array(); // replacing $studyPresets in the future
$adaptedScenario = False; // declares whether or not an Adaptation Strategy has been defined by the user
// Load the data package
$groupDatapackageID = $entity->get('field_data_package')->target_id;
if ($groupDatapackageID) {
$datapackageNode = \Drupal\node\Entity\Node::load($groupDatapackageID);
$datapackageUUID = $datapackageNode->uuid();
}
// load city term, if it has already been set in the Study
$cityTerm = (!$entity->get('field_city_region')->isEmpty() ? Term::load($entity->get('field_city_region')->target_id) : false);
if ($cityTerm) {
$eeaName = $cityTerm->get('field_eea_city_profile_name')->value;
$cityCode = $cityTerm->get('field_city_code')->value;
}
// get Study presets (combination of time-period, emission scenario and event frequency) from Paragraph reference
if (!$entity->get('field_study_presets')->isEmpty()) {
foreach ($entity->get('field_study_presets') as $paraReference) {
// apparently when adding a new paragraph instance, it is first generated empty, which means that
// at first the new study scenario will have no reference and therefore generate a warning here after
if (!$paraReference->target_id) {
continue;
}
$paragraph = Paragraph::load($paraReference->target_id);
if ($paragraph && $paragraph->getType() == "variable_set") {
// load all the necessary taxonomy terms
$termTimePeriod = Term::load($paragraph->get('field_time_period')->target_id);
$termEmScenario = Term::load($paragraph->get('field_emission_scenario')->target_id);
$termEventFreq = Term::load($paragraph->get('field_event_frequency')->target_id);
//$termStudyVariant = Term::load($paragraph->get('field_study_variant')->target_id);
if (!$termTimePeriod || !$termEmScenario || !$termEventFreq) {
// although in the Study preset (aka Study scenario) all fields are required
// and therefore must exist if the paragraph exists, it could happen that
// the related taxonomy term has been removed from the system
// -> in such a case break for-loop and don't add an incomplete preset
// should be resolved once Drupal introduces a possiblity to unpublish taxonomy terms
break;
}
// extract preset label and all needed values from those terms
$studyPreset['label'] = $paragraph->get('field_label')->value;
$studyPreset['time_period'] = $termTimePeriod->get('field_var_meaning')->value;
$studyPreset['emission_scenario'] = $termEmScenario->get('field_var_meaning')->value;
$studyPreset['event_frequency'] = $termEventFreq->get('field_var_meaning')->value;
//$studyPreset['study_variant'] = $termStudyVariant->get('field_var_meaning')->value;
array_push($studyScenarios, $studyPreset);
// this can later be removed, once we switch to the $studyScenarios object
$studyPresets['time_period'] = $termTimePeriod->get('field_var_meaning')->value;
$studyPresets['emission_scenario'] = $termEmScenario->get('field_var_meaning')->value;
$studyPresets['event_frequency'] = $termEventFreq->get('field_var_meaning')->value;
//$studyPresets['study_variant'] = $termStudyVariant->get('field_var_meaning')->value;
}
}
}
// check if Study has an Adaptation Project (ap) set
$ap = $entity->getContentEntities("group_node:adaptation_project");
if ($ap) {
$adaptedScenario = True;
}
// write-permissions inside a Study need to be checked upon a group-member level and not a user level
$userAccount = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userAccount->id());
$has_user_special_roles = false;
$isAnonymous = true;
$isMember = false;
if (!$userAccount->isAnonymous()) {
$isAnonymous = false;
}
$member = $entity->getMember($userAccount);
if ($member) {
$isMember = true;
$memberRoles = $member->getRoles();
foreach ($memberRoles as $role) {
// Study group itself should be writable for owners only
if ($role->id() == "study-owner") {
$has_user_special_roles = true;
break;
}
}
}
// bypass member roles permissions for admins
if ($user->hasRole("administrator")) {
$has_user_special_roles = true;
$isMember = true;
}
$emikatId = $entity->get('field_emikat_id')->value;
$emikatId = empty($emikatId) ? -1 : intval($emikatId);
$calculationStatus = $entity->get('field_calculation_status')->value;
$groupEntityInfo = array(
//'title' => $entity->get('title')->value, // inconsistency: no title field ins study group!
'step' => -1,
'step_uuid' => -1,
'step_name' => 'none',
'id' => $entity->id(),
'uuid' => $entity->uuid(),
'study' => $entity->id(), //deprecated -> use entitiyinfo.study.id
'study_uuid' => $entity->uuid(), //deprecated -> use entitiyinfo.study.uuid
'study_emikat_id' => $emikatId,
'calculation_status' => $calculationStatus,
'study_datapackage_uuid' => $datapackageUUID,
'study_area' => $entity->get('field_area')->value,
'eea_city_name' => $eeaName,
'city_code' => $cityCode,
'study_presets' => $studyPresets,
'study_scenarios' => $studyScenarios,
'has_adapted_scenario' => $adaptedScenario,
'is_anonymous' => $isAnonymous,
'is_member' => $isMember,
'write_permissions' => ($has_user_special_roles ? 1 : 0),
'trigger_permissions' => ($has_user_special_roles ? 1 : 0),
);
return $groupEntityInfo;
}
}