Skip to content

Commit d908264

Browse files
authored
[dataquery] Add instrument flags to instruments query engine (#9529)
This adds instrument flags to the data that is bulk loaded by bulkLoadInstanceData and uses that to address the low hanging fruit of #9372 of including administration flags in the data query tool. It does not address the DDE and conflict related fields that are in the CouchDB import script because we should investigate if those make sense in the instruments query engine or if it's possible to have those provided by the conflict resolver module rather than the instruments module, as they are more related to that module.
1 parent 9f9efc0 commit d908264

File tree

2 files changed

+143
-29
lines changed

2 files changed

+143
-29
lines changed

modules/instruments/php/instrumentqueryengine.class.inc

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php declare(strict_types=1);
22

33
namespace LORIS\instruments;
4+
use \LORIS\Data\Scope;
5+
use \LORIS\Data\Cardinality;
6+
7+
use \LORIS\Data\Types\Enumeration;
48

59
/**
610
* InstrumentQueryEngine implements a LORIS QueryEngine for querying
@@ -41,19 +45,57 @@ class InstrumentQueryEngine implements \LORIS\Data\Query\QueryEngine
4145
$rows = $DB->pselectCol("SELECT Test_name FROM test_names", []);
4246

4347
$dict = [];
48+
49+
// Use the same option enums for all instruments
50+
$adminoptions = new Enumeration('None', 'Partial', 'All');
51+
$dataentryoptions = new Enumeration('In Progress', 'Complete');
52+
$validityoptions = new Enumeration('Questionable', 'Invalid', 'Valid');
53+
54+
$cardinality = new Cardinality(Cardinality::SINGLE);
55+
$scope = new Scope(Scope::SESSION);
56+
4457
foreach ($rows as $testname) {
4558
try {
46-
$inst = \NDB_BVL_Instrument::factory(
59+
$inst = \NDB_BVL_Instrument::factory(
4760
$this->loris,
4861
$testname,
4962
"",
5063
"",
5164
);
52-
$cat = new \LORIS\Data\Dictionary\Category(
65+
$cat = new \LORIS\Data\Dictionary\Category(
5366
$testname,
5467
$inst->getFullName()
5568
);
56-
$fields = $inst->getDataDictionary();
69+
70+
$fields = [
71+
new DictionaryItem(
72+
$inst->testName.'_Administration',
73+
'Administration flag for ' . $inst->getFullName(),
74+
$scope,
75+
$adminoptions,
76+
$cardinality,
77+
'Administration',
78+
),
79+
new DictionaryItem(
80+
$inst->testName.'_Data_entry',
81+
'Data entry status for ' . $inst->getFullName(),
82+
$scope,
83+
$dataentryoptions,
84+
$cardinality,
85+
'Data_entry',
86+
),
87+
];
88+
if ($inst->ValidityEnabled) {
89+
$fields[] = new DictionaryItem(
90+
$inst->testName.'_Validity',
91+
'Test validity for ' . $inst->getFullName(),
92+
$scope,
93+
$validityoptions,
94+
$cardinality,
95+
'Validity',
96+
);
97+
}
98+
$fields = array_merge($fields, $inst->getDataDictionary());
5799
$dict[] = $cat->withItems($fields);
58100
} catch (\LorisException $e) {
59101
error_log($e);
@@ -329,11 +371,25 @@ class InstrumentQueryEngine implements \LORIS\Data\Query\QueryEngine
329371
$candData[$dict->getName()] = [];
330372
}
331373
$sid = $loadedInstrument->getSessionID();
332-
$candData[$dict->getName()][$sid->__toString()] = [
333-
'VisitLabel' => $loadedInstrument->getVisitLabel(),
334-
'SessionID' => $sid->__toString(),
335-
'value' => $loadedInstrument->getDictionaryValue($dict),
336-
];
374+
switch ($dict->fieldname) {
375+
case 'Administration':
376+
case 'Data_entry':
377+
case 'Validity':
378+
$flags = $loadedInstrument->getFlags()->toArray();
379+
$candData[$dict->getName()][$sid->__toString()] = [
380+
'VisitLabel' => $loadedInstrument->getVisitLabel(),
381+
'SessionID' => $sid->__toString(),
382+
'value' => $flags[$dict->fieldname],
383+
];
384+
break;
385+
default:
386+
$candData[$dict->getName()][$sid->__toString()] = [
387+
'VisitLabel' => $loadedInstrument->getVisitLabel(),
388+
'SessionID' => $sid->__toString(),
389+
'value' => $loadedInstrument
390+
->getDictionaryValue($dict),
391+
];
392+
}
337393
}
338394
}
339395
yield "$iCandID" => $candData;

php/libraries/NDB_BVL_Instrument.class.inc

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,26 +2275,52 @@ abstract class NDB_BVL_Instrument extends NDB_Page
22752275
(SELECT CommentID FROM load_comment_ids)";
22762276
}
22772277
if ($condition !== null) {
2278-
$conditions[] = 'JSON_VALUE(Data, "$.'
2279-
. substr(
2278+
$fname = substr(
22802279
$condition->dictionary->getName(),
22812280
strlen($this->testName)+1
2282-
)
2283-
. '") '
2284-
. \LORIS\Data\Query\SQLQueryEngine::sqlOperator($condition->criteria)
2285-
. ' '
2286-
. \LORIS\Data\Query\SQLQueryEngine::sqlValue(
2287-
$condition->dictionary,
2288-
$condition->criteria,
2289-
$params
2290-
)
2291-
. ' ';
2281+
);
2282+
if ($fname == 'Administration'
2283+
|| $fname == 'Validity'
2284+
|| $fname == 'Data_entry'
2285+
) {
2286+
$conditions[] = "flag.$fname"
2287+
. \LORIS\Data\Query\SQLQueryEngine::sqlOperator(
2288+
$condition->criteria
2289+
)
2290+
. \LORIS\Data\Query\SQLQueryEngine::sqlValue(
2291+
$condition->dictionary,
2292+
$condition->criteria,
2293+
$params
2294+
)
2295+
. ' ';
2296+
} else {
2297+
2298+
$conditions[] = 'JSON_VALUE(Data, "$.'
2299+
. substr(
2300+
$condition->dictionary->getName(),
2301+
strlen($this->testName)+1
2302+
)
2303+
. '")'
2304+
. \LORIS\Data\Query\SQLQueryEngine::sqlOperator(
2305+
$condition->criteria
2306+
)
2307+
. \LORIS\Data\Query\SQLQueryEngine::sqlValue(
2308+
$condition->dictionary,
2309+
$condition->criteria,
2310+
$params
2311+
)
2312+
. ' ';
2313+
}
2314+
$conditions[] ="t.CommentID NOT LIKE 'DDE_%'";
22922315
}
22932316
$where = 'WHERE ' . join(' AND ', $conditions);
22942317
$query = "SELECT CandID,
22952318
SessionID,
22962319
CommentID,
22972320
session.Visit_Label as VisitLabel,
2321+
flag.Administration,
2322+
flag.Data_entry as DataEntry,
2323+
flag.Validity,
22982324
Data
22992325
FROM flag
23002326
JOIN session ON (session.ID=flag.SessionID)
@@ -2306,12 +2332,18 @@ abstract class NDB_BVL_Instrument extends NDB_Page
23062332
foreach ($jsondata as $row) {
23072333
$newinst = clone $this;
23082334

2309-
$newinst->candID = new CandID(strval($row['CandID']));
2310-
$newinst->commentID = $row['CommentID'];
2311-
$newinst->visitLabel = $row['VisitLabel'];
2312-
$newinst->sessionID = new SessionID(
2313-
strval($row['SessionID'])
2335+
$newinst->candID = new CandID(strval($row['CandID']));
2336+
$newinst->commentID = $row['CommentID'];
2337+
$newinst->visitLabel = $row['VisitLabel'];
2338+
$newinst->sessionID = new SessionID(
2339+
strval($row['SessionID'])
2340+
);
2341+
$newinst->flags = new \InstrumentFlags(
2342+
$row['Data_entry'],
2343+
$row['Administration'],
2344+
$row['Validity']
23142345
);
2346+
unset($row['Data_entry'], $row['Administration'], $row['Validity']);
23152347
$newinst->instanceData = json_decode(
23162348
$row['Data'] ?? '{}',
23172349
true,
@@ -2327,11 +2359,21 @@ abstract class NDB_BVL_Instrument extends NDB_Page
23272359
(SELECT CommentID FROM load_comment_ids)";
23282360
}
23292361
if ($condition !== null) {
2330-
$conditions[] = 't.'
2331-
. substr(
2362+
$fname = substr(
23322363
$condition->dictionary->getName(),
23332364
strlen($this->testName)+1
2334-
)
2365+
);
2366+
if ($fname == 'Administration'
2367+
|| $fname == 'Validity'
2368+
|| $fname == 'Data_entry'
2369+
) {
2370+
$tname = 'f.';
2371+
} else {
2372+
$tname = 't.';
2373+
}
2374+
2375+
$conditions[] = $tname
2376+
. $fname
23352377
. ' '
23362378
. \LORIS\Data\Query\SQLQueryEngine::sqlOperator(
23372379
$condition->criteria
@@ -2343,12 +2385,16 @@ abstract class NDB_BVL_Instrument extends NDB_Page
23432385
$params
23442386
)
23452387
. ' ';
2388+
$conditions[] ="t.CommentID NOT LIKE 'DDE_%'";
23462389
}
23472390
$where = 'WHERE ' . join(' AND ', $conditions);
23482391
$query = "SELECT
23492392
session.CandID as CandID,
23502393
t.CommentID as CommentID,
23512394
session.Visit_Label as VisitLabel,
2395+
f.Administration,
2396+
f.Data_entry,
2397+
f.Validity,
23522398
session.ID as SessionID, t.*
23532399
FROM $this->table t
23542400
JOIN flag f ON (t.CommentID=f.CommentID)
@@ -2365,6 +2411,13 @@ abstract class NDB_BVL_Instrument extends NDB_Page
23652411
$newinst->sessionID = new SessionID(strval($row['SessionID']));
23662412
unset($row['CommentID'], $row['VisitLabel'], $row['SessionID']);
23672413

2414+
$newinst->flags = new \InstrumentFlags(
2415+
$row['Data_entry'],
2416+
$row['Administration'],
2417+
$row['Validity']
2418+
);
2419+
unset($row['Data_entry'], $row['Administration'], $row['Validity']);
2420+
23682421
$newinst->instanceData = $row;
23692422

23702423
yield $newinst;
@@ -3071,13 +3124,17 @@ abstract class NDB_BVL_Instrument extends NDB_Page
30713124
return $this->determineDataEntryAllowed();
30723125
}
30733126

3127+
private ?\InstrumentFlags $flags = null;
30743128
/**
30753129
* Gets the flags of that instrument
30763130
*
30773131
* @return \InstrumentFlags
30783132
*/
30793133
public function getFlags(): \InstrumentFlags
30803134
{
3135+
if ($this->flags !== null) {
3136+
return $this->flags;
3137+
}
30813138
$row = \NDB_Factory::singleton()->database()->pselectRow(
30823139
'SELECT
30833140
Data_entry as dataentry,
@@ -3093,11 +3150,12 @@ abstract class NDB_BVL_Instrument extends NDB_Page
30933150
$administration = $row['administration'] ?? null;
30943151
$validity = $row['validity'] ?? null;
30953152

3096-
return new \InstrumentFlags(
3153+
$this->flags = new \InstrumentFlags(
30973154
$dataentry,
30983155
$administration,
30993156
$validity
31003157
);
3158+
return $this->flags;
31013159
}
31023160

31033161
/**

0 commit comments

Comments
 (0)