-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodifyHonorMetadata.php
60 lines (56 loc) · 2.45 KB
/
modifyHonorMetadata.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
<?php
# for use with hooks/honorHook.php
use \Vanderbilt\CareerDevLibrary\Sanitizer;
use \Vanderbilt\CareerDevLibrary\Download;
use \Vanderbilt\CareerDevLibrary\DataDictionaryManagement;
use \Vanderbilt\CareerDevLibrary\Upload;
require_once(dirname(__FILE__)."/small_base.php");
require_once(dirname(__FILE__)."/classes/Autoload.php");
$affectedFields = DataDictionaryManagement::HONORACTIVITY_SPECIAL_FIELDS;
$records = Download::recordIdsByPid($pid);
$field = Sanitizer::sanitize($_POST['field'] ?? "");
$recordId = Sanitizer::getSanitizedRecord($_POST['record'] ?? "", $records);
$label = Sanitizer::sanitize($_POST['label'] ?? "");
$destFields = $affectedFields[$field] ?? [];
if ($field && $recordId && $label && !empty($destFields)) {
$metadata = Download::metadataByPid($pid);
$numModifiedFields = 0;
$fieldChoices = [];
$newIndex = 0;
foreach ($metadata as $i => $row) {
if (in_array($row['field_name'], $destFields)) {
if (empty($fieldChoices)) {
$fieldChoices = DataDictionaryManagement::getRowChoices($row['select_choices_or_calculations']);
$otherString = $fieldChoices[DataDictionaryManagement::HONORACTIVITY_OTHER_VALUE] ?? "Other/Not Listed";
unset($fieldChoices[DataDictionaryManagement::HONORACTIVITY_OTHER_VALUE]); // reset last below
$maxIndex = 0;
foreach (array_keys($fieldChoices) as $index) {
$isInt = is_int($index) || (ctype_digit($index));
if ($isInt && ($index > $maxIndex)) {
$maxIndex = $index;
}
}
$newIndex = $maxIndex + 1;
$fieldChoices[$newIndex] = $label;
$fieldChoices[DataDictionaryManagement::HONORACTIVITY_OTHER_VALUE] = $otherString;
}
$metadata[$i]["select_choices_or_calculations"] = DataDictionaryManagement::makeChoiceStr($fieldChoices);
$numModifiedFields++;
}
}
if ($newIndex == 0) {
$data = ["error" => "No new index generated!"];
} else if ($numModifiedFields > 0) {
Upload::metadataNoAPI($metadata, $pid);
$data = [
"result" => $numModifiedFields." fields modified.",
"index" => $newIndex,
"label" => $label,
];
} else {
$data = ["error" => "No fields modified!"];
}
} else {
$data = ["error" => "Missing input data!"];
}
echo json_encode($data);