-
Notifications
You must be signed in to change notification settings - Fork 181
[instruments] Save value into UserID field #7252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
driusan
merged 13 commits into
aces:23.0-release
from
zaliqarosli:2020-12-18-SaveInstrumentUserID
Mar 30, 2021
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
354fc4d
save UserID to db for instruments
zaliqarosli 8b6d8c7
add tool to backpopulate data
zaliqarosli dad61c2
update as suggested by xavier
zaliqarosli d807b69
execute flag table
zaliqarosli 96e86ee
count total number of resutls
zaliqarosli fab7cc5
update script to incorporate ridas suggestions
zaliqarosli 71171c0
bugfix
zaliqarosli 27c190f
cleanup
zaliqarosli 63c7389
add use case where Data entry in progress on entering inst without sa…
zaliqarosli 472c9ed
use _save instead so UserID field doesnt get overriden is lorisadmin …
zaliqarosli ca9c078
add column exists check
zaliqarosli 14bffcc
Update tools/single_use/SaveUserIDToInstrumentData.php
zaliqarosli d975e27
escape hDB variable
zaliqarosli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* This script is intended for a one-time use only to restore the value of the | ||
zaliqarosli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* `UserID` column of instrument tables and the `UserID` key of the instrument | ||
* JSON `Data` in the flag table. | ||
* | ||
* This tool queries data from the `history` table and meaningfully imports | ||
* data from its `userID` column back into the instrument and `flag` tables. | ||
* | ||
* Example use: | ||
* $ php SaveUserIDToInstrumentData.php [confirm] | ||
|
||
* PHP Version 7 | ||
* | ||
* @category Tools | ||
* @package Loris | ||
* @author Zaliqa Rosli <[email protected]> | ||
* @licence LORIS License | ||
* @link https://www.github.com/aces/Loris | ||
*/ | ||
require_once __DIR__ . '/../generic_includes.php'; | ||
|
||
$confirm = false; | ||
if (isset($argv[1]) && $argv[1] === 'confirm') { | ||
$confirm = true; | ||
} | ||
|
||
echo "\n\nQuerying data...\n"; | ||
$db = \NDB_Factory::singleton()->database(); | ||
// Query CommentIDs with DECS 'Complete', and | ||
// userID for latest changes made to the instrument data | ||
$history = $db->pselect( | ||
zaliqarosli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"SELECT h1.tbl, h1.primaryVals AS CommentID, h1.userID | ||
FROM history h1 | ||
INNER JOIN | ||
( | ||
SELECT primaryVals, MAX(changeDate) AS max_date | ||
FROM history | ||
WHERE primaryVals IN ( | ||
SELECT primaryVals | ||
FROM history | ||
WHERE col = 'Data_entry_completion_status' | ||
AND primaryCols = 'CommentID' | ||
AND new = 'Complete' | ||
) | ||
GROUP BY primaryVals | ||
) h2 | ||
ON h1.primaryVals = h2.primaryVals | ||
AND h1.changeDate = h2.max_date | ||
WHERE userID <> 'unknown' | ||
AND tbl <> 'flag' | ||
AND col <> 'CommentID' | ||
GROUP BY h1.primaryVals, h1.changeDate, h1.userID", | ||
array() | ||
); | ||
if (empty($history)) { | ||
echo "\n\nThere is no data to import.\n"; | ||
die(); | ||
} | ||
echo "\n\nThe following data can be imported into the instrument tables:\n"; | ||
foreach ($history as $index => $row) { | ||
$result = $index + 1; | ||
echo "\n\nResult $result:\n"; | ||
print_r($row); | ||
|
||
$table = $row['tbl']; | ||
$commentID = $row['CommentID']; | ||
$userID = $row['userID']; | ||
|
||
// Extract old data from flag so we can update it with merge so that we | ||
// don't overwrite it | ||
$oldData = $db->pselectOne( | ||
"SELECT Data FROM flag WHERE CommentID=:cid", | ||
array('cid' => $commentID) | ||
); | ||
echo "\n\nOld Data: \n"; | ||
print_r($oldData); | ||
|
||
// (The PDO driver seems to return null as "null" for JSON column types) | ||
if (!empty($oldData) && $oldData !== "null") { | ||
$oldData = json_decode($oldData, true); | ||
} else { | ||
$oldData = array(); | ||
} | ||
$newData = array_merge( | ||
$oldData ?? array(), | ||
array('UserID' => $userID) | ||
); | ||
echo "\n\nNew Data: \n"; | ||
print_r(json_encode($newData)); | ||
|
||
if ($confirm) { | ||
echo "\n\nImporting data into respective instrument tables and commentID flag entries...\n"; | ||
// Update instrument table | ||
$db->update( | ||
$table, | ||
array('UserID' => $userID), | ||
array('CommentID' => $commentID) | ||
); | ||
|
||
// Save the JSON to the flag.Data column. | ||
// | ||
// json_encode ensures that this is safe. If we use the safe wrapper, | ||
// HTML encoding the quotation marks will make it invalid JSON. | ||
$db->unsafeUpdate( | ||
"flag", | ||
array("Data" => json_encode($newData)), | ||
array('CommentID' => $commentID) | ||
); | ||
echo "\n\nDone. $userID saved as UserID in $table and flag tables for CommentID $commentID.\n"; | ||
} | ||
} | ||
|
||
if (!$confirm) { | ||
echo "\n\nRun this tool again with the argument 'confirm' to ". | ||
"perform the changes.\n"; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before SQL