-
Notifications
You must be signed in to change notification settings - Fork 177
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
[instruments] Save value into UserID field #7252
Merged
driusan
merged 13 commits into
aces:23.0-release
from
zaliqarosli:2020-12-18-SaveInstrumentUserID
Mar 30, 2021
Merged
Changes from all 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 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 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,147 @@ | ||
<?php | ||
/** | ||
* RUN-TIME WARNING FOR PROJECTS: It can take up to a whole day to run this script. It took 14 hours to run the script on the CCNA DB during testing. | ||
* | ||
* This script is intended for a one-time use only to restore the value of the | ||
* `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 <zaliqa.rosli@mcin.ca> | ||
* @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\n"; | ||
|
||
// Get history DB | ||
$DBInfo = $config->getSetting('database'); | ||
$hDB = isset($DBInfo['historydb']) ? $DBInfo['historydb'] : $DBInfo['database']; | ||
|
||
$result_count = 1; | ||
zaliqarosli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Query entries in the history table for the latest change | ||
// made to the flag table, for every CommentID. | ||
$history = $DB->pselect( | ||
"SELECT f.Test_name, f.CommentID, h1.userID | ||
FROM flag f | ||
JOIN " . $DB->escape($hDB) . ".history h1 on (f.CommentID=h1.primaryVals) | ||
JOIN | ||
( | ||
SELECT primaryVals, MAX(changeDate) as changeDate | ||
FROM " . $DB->escape($hDB) . ".history | ||
WHERE userID <> 'unknown' | ||
AND type='U' | ||
AND NOT ( | ||
col='Data_entry' AND old IS NULL | ||
) | ||
GROUP BY primaryVals | ||
) h2 USING (primaryVals, changeDate) | ||
GROUP BY CommentID, h1.userID", | ||
zaliqarosli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
array() | ||
); | ||
// Loop and index results from history by testname | ||
$idxHist = []; | ||
foreach ($history as $entry) { | ||
$idxHist[$entry['Test_name']][] = $entry; | ||
} | ||
|
||
foreach(\Utility::getAllInstruments() as $testname => $fullName) { | ||
// Instantiate instrument object to get information | ||
try { | ||
$instrument = \NDB_BVL_Instrument::factory($testname, '', ''); | ||
} catch (Exception $e) { | ||
echo "$testname does not seem to be a valid instrument.\n"; | ||
continue; | ||
} | ||
|
||
// Check if instrument saves data in JSON format i.e. no-SQL table | ||
$JSONData = $instrument->usesJSONData(); | ||
$table = null; | ||
if ($JSONData === false) { | ||
$table = $instrument->table; | ||
if (!$table) { | ||
echo "\n\nERROR: The table name for instrument $testname cannot be found.\n"; | ||
continue; | ||
} else if (!$DB->tableExists($table)) { | ||
echo "Table $table for instrument $testname does not exist in the Database.\n"; | ||
continue; | ||
} else if (!$DB->columnExists($table, 'UserID')) { | ||
echo "Column 'UserID' does not exist in the $table table.\n"; | ||
continue; | ||
} | ||
} | ||
|
||
if (empty($idxHist[$testname])) { | ||
echo "\n\nThere is no data to import into $testname.\n"; | ||
continue; | ||
} else { | ||
echo "\n\nThe following data can be imported into $testname:\n\n"; | ||
foreach ($idxHist[$testname] as $row) { | ||
$commentID = $row['CommentID']; | ||
$userID = $row['userID']; | ||
echo "\nResult $result_count: $commentID userID: $userID\n"; | ||
$result_count++; | ||
} | ||
} | ||
|
||
// Update the instrument table | ||
if ($confirm) { | ||
echo "\nImporting data into $testname...\n\n"; | ||
|
||
// Instantiate instrument at the session level in order to | ||
// use _save() function | ||
foreach ($idxHist[$testname] as $row) { | ||
$commentID = $row['CommentID']; | ||
$userID = $row['userID']; | ||
|
||
try { | ||
$sessionInst = \NDB_BVL_Instrument::factory( | ||
driusan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$testname, | ||
$commentID, | ||
'' | ||
); | ||
} catch (Exception $e) { | ||
ridz1208 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
echo "\t$testname instrument row with CommentID: $commentID was ". | ||
" Ignored for one of the following reasons:\n". | ||
" - The candidate is inactive.\n". | ||
" - The session is inactive.\n\n"; | ||
continue; | ||
} | ||
|
||
if (!$sessionInst) { | ||
// instrument does not exist | ||
echo "\t$testname for CommentID: $CommentID could not be instantiated.\n"; | ||
continue; | ||
} | ||
|
||
// Save userID | ||
echo "\tSaving userID $userID for CommentID: $commentID\n\n"; | ||
$sessionInst->_save( | ||
array( | ||
'UserID' => $userID | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
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