Skip to content

Commit

Permalink
[Core] Update NDB_Page constructor to include LorisInstance object (#…
Browse files Browse the repository at this point in the history
…7414)

This updates the NDB_Page constructor to include a LorisInstance object. The LorisInstance object represents the properties of the LorisInstance itself (such as the database connection) and can replace many factory/singleton calls, but is seldom used in LORIS because it's difficult to get a copy of the object. (It needs to be indirectly accessed from a PSR7 request attribute). After moving it to the constructor, it can now be accessed directly from any NDB_Page class or subclass in a simple class property.
  • Loading branch information
driusan authored and laemtl committed Jun 7, 2021
1 parent 623ed31 commit e4df353
Show file tree
Hide file tree
Showing 32 changed files with 299 additions and 110 deletions.
9 changes: 9 additions & 0 deletions htdocs/survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class DirectDataEntryMainPage
var $tpl_data;
var $caller;

private $loris;

/**
* Initialize all of the class variables and things required from the
* REQUEST.
Expand All @@ -78,6 +80,12 @@ function initialize()
$this->key = $_REQUEST['key'];

$DB = Database::singleton();

$this->loris = new \LORIS\LorisInstance(
$DB,
$config,
[]
);
$this->TestName = $DB->pselectOne(
"SELECT Test_name FROM participant_accounts
WHERE OneTimePassword=:key AND Status <> 'Complete'",
Expand Down Expand Up @@ -346,6 +354,7 @@ function display()
}

$workspace = $this->caller->load(
$this->loris,
$this->TestName,
$this->Subtest ?? '',
$this->CommentID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class Flags extends Endpoint implements \LORIS\Middleware\ETagCalculator
);
}

$this->_instrumentStatus = new \NDB_BVL_InstrumentStatus();
$loris = $request->getAttribute("loris");
$this->_instrumentStatus = new \NDB_BVL_InstrumentStatus($loris);
$this->_instrumentStatus->select($this->_instrument->commentID);

$data = json_decode((string) $request->getBody(), true);
Expand Down Expand Up @@ -213,7 +214,8 @@ class Flags extends Endpoint implements \LORIS\Middleware\ETagCalculator
);
}

$this->_instrumentStatus = new \NDB_BVL_InstrumentStatus();
$loris = $request->getAttribute("loris");
$this->_instrumentStatus = new \NDB_BVL_InstrumentStatus($loris);
$this->_instrumentStatus->select($this->_instrument->commentID);

if (!$this->_instrument->determineDataEntryAllowed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$pathparts = $request->getAttribute('pathparts');
$loris = $request->getAttribute("loris");
if (count($pathparts) === 0) {
switch ($request->getMethod()) {
case 'GET':
Expand Down Expand Up @@ -124,6 +125,7 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator

try {
$instrument = \NDB_BVL_Instrument::factory(
$loris,
$instrumentname,
$commentid,
'',
Expand Down
2 changes: 2 additions & 0 deletions modules/api/php/endpoints/project/instruments.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ class Instruments extends Endpoint implements \LORIS\Middleware\ETagCalculator
// Delegate to instrument specific endpoint.
$instrumentname = array_shift($pathparts);
try {
$loris = $request->getAttribute("loris");
$instrument = \NDB_BVL_Instrument::factory(
$loris,
$instrumentname,
'',
'',
Expand Down
4 changes: 3 additions & 1 deletion modules/api/php/endpoints/projects.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class Projects extends Endpoint implements \LORIS\Middleware\ETagCalculator

// Delegate to project specific endpoint.
try {
$project_name = $pathparts[1] ?? '';
$project_name = isset($pathparts[1]) ?
urldecode($pathparts[1]) :
'';
$project = \NDB_Factory::singleton()
->project($project_name);
} catch (\NotFound $e) {
Expand Down
4 changes: 3 additions & 1 deletion modules/candidate_profile/php/module.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class Module extends \Module
$candidate = \Candidate::singleton($candID);

$request = $request->withAttribute('candidate', $candidate);
$page = $this->loadPage("candidate_profile");
$loris = $request->getAttribute("loris");
$page = $this->loadPage($loris, "candidate_profile");

return $page->process($request, $page);
} catch (\DomainException | \LORISException $e) {
// A LORISException means the \Candidate::singleton couldn't
Expand Down
2 changes: 2 additions & 0 deletions modules/conflict_resolver/php/conflict_resolver.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class Conflict_Resolver extends \NDB_Menu_Filter_Form
$TableName = $row['TableName'];

$Instrument = \NDB_BVL_Instrument::factory(
$this->loris,
$TableName,
$row['CommentId1'],
'',
Expand All @@ -154,6 +155,7 @@ class Conflict_Resolver extends \NDB_Menu_Filter_Form
}

$Instrument = \NDB_BVL_Instrument::factory(
$this->loris,
$TableName,
$row['CommentId2'],
'',
Expand Down
24 changes: 18 additions & 6 deletions modules/dashboard/php/dashboard.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,32 @@ class Dashboard extends \NDB_Form
/**
* Page constructor for the dashboard main page.
*
* @param \Module $module The test name being accessed
* @param string $page The subtest being accessed (may be an empty string)
* @param string $identifier The identifier for the data to load on this page
* @param string $commentID The CommentID to load the data for
* @param string $formname The name to give this form
* @param \LORIS\LorisInstance $loris The LORIS instance the page is being
* constructed for
* @param \Module $module The test name being accessed
* @param string $page The subtest being accessed
* (may be an empty string)
* @param string $identifier The identifier for the data to
* load on this page
* @param string $commentID The CommentID to load the data for
* @param string $formname The name to give this form
*/
function __construct(
\LORIS\LorisInstance $loris,
\Module $module,
string $page,
string $identifier,
string $commentID,
string $formname
) {
parent::__construct($module, $page, $identifier, $commentID, $formname);
parent::__construct(
$loris,
$module,
$page,
$identifier,
$commentID,
$formname
);

// This needs to be done before setup so that the widget specific CSS and
// javascript are available for getCSSDependencies and getJSDependencies,
Expand Down
3 changes: 2 additions & 1 deletion modules/data_release/php/permissions.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Permissions extends \NDB_Page
$loris = $request->getAttribute("loris");
$DB = $loris->getDatabaseConnection();
$releasePage = $this->Module->loadPage(
$loris,
'data_release',
);
assert($releasePage instanceof Data_Release);
Expand Down Expand Up @@ -206,7 +207,7 @@ class Permissions extends \NDB_Page
$DB = $loris->getDatabaseConnection();

// Get current user version files and list of files for each version.
$dataRelease = $this->Module->loadPage('data_release');
$dataRelease = $this->Module->loadPage($loris, 'data_release');
assert($dataRelease instanceof Data_Release);
$userVersionFiles = $dataRelease->getUserVersionFiles($DB);
$versionFiles = $dataRelease->getVersionFiles($DB);
Expand Down
11 changes: 7 additions & 4 deletions modules/dictionary/php/module.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,18 @@ class Module extends \Module
* by the page, so we arbitrarily map "module" URLs to the "moduledict"
* class name instead.
*
* @param string $page the Page name being accessed.
* @param \LORIS\LorisInstance $loris The LORIS instance that the module
* is serving.
* @param string $page The name of the page within the
* module to load.
*
* @return \NDB_Page
*/
public function loadPage(string $page)
public function loadPage(\Loris\LORISInstance $loris, string $page)
{
if ($page === 'module') {
return $this->loadPage('moduledict');
return $this->loadPage($loris, 'moduledict');
}
return parent::loadPage($page);
return parent::loadPage($loris, $page);
}
}
5 changes: 3 additions & 2 deletions modules/instrument_list/php/instrument_list.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class Instrument_List extends \NDB_Menu_Filter
function getControlPanel()
{
$controlPanel = new Instrument_List_ControlPanel(
$this->loris,
$this->timePoint->getSessionID()
);
// save possible changes from the control panel...
Expand Down Expand Up @@ -226,10 +227,10 @@ class Instrument_List extends \NDB_Menu_Filter
$prevSubgroup = $instrument['Sub_group'];

// make an instrument status object
$status = new \NDB_BVL_InstrumentStatus;
$status = new \NDB_BVL_InstrumentStatus($this->loris);
$status->select($instrument['CommentID']);

$ddeStatus = new \NDB_BVL_InstrumentStatus;
$ddeStatus = new \NDB_BVL_InstrumentStatus($this->loris);
$ddeStatus->select($instrument['DDECommentID']);

$Ins = "instruments";
Expand Down
28 changes: 20 additions & 8 deletions modules/instrument_list/php/instrument_list_controlpanel.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,27 @@ class Instrument_List_ControlPanel extends \TimePoint
{
protected array $tpl_data;

/**
* The LORIS Instance that this timepoint exists on
*
* @var \LORIS\LorisInstance $loris
*/
protected $loris;

/**
* Construct function
*
* @param \SessionID $sessionID the value of sessionID
* @param \LORIS\LorisInstance $loris The LORIS Instance.
* @param \SessionID $sessionID SessionID that this control
* panel is controlling.
*
* @return void
*/
function __construct($sessionID)
function __construct(\LORIS\LorisInstance $loris, $sessionID)
{
$this->tpl_data = [];
$this->loris = $loris;

$this->select($sessionID);
}

Expand Down Expand Up @@ -226,6 +237,7 @@ class Instrument_List_ControlPanel extends \TimePoint
$instrument['CommentID']
);
$diff =\ConflictDetector::detectConflictsForCommentIds(
$this->loris,
$instrument['Test_name'],
$instrument['CommentID'],
$instrument['DDECommentID']
Expand Down Expand Up @@ -351,7 +363,7 @@ class Instrument_List_ControlPanel extends \TimePoint
$ddeConflictDetected = false;
$dataEntry = '';
foreach ($batteryList as $instrument) {
$status = new \NDB_BVL_InstrumentStatus();
$status = new \NDB_BVL_InstrumentStatus($this->loris);
$status->select($instrument['CommentID']);
$dataEntry = $status->getDataEntryStatus();
if ($dataEntry != 'Complete') {
Expand All @@ -361,7 +373,7 @@ class Instrument_List_ControlPanel extends \TimePoint
// If the instrument requires double data entry,
//check that DDE is also done
if (in_array($instrument['Test_name'], $ddeInstruments)) {
$status = new \NDB_BVL_InstrumentStatus();
$status = new \NDB_BVL_InstrumentStatus($this->loris);
$status->select($instrument['DDECommentID']);
$dataEntry = $status->getDataEntryStatus();
if ($dataEntry != 'Complete') {
Expand Down Expand Up @@ -567,7 +579,7 @@ class Instrument_List_ControlPanel extends \TimePoint
$allDataEntryComplete =true;
foreach ($batteryList as $instrument) {

$status = new \NDB_BVL_InstrumentStatus();
$status = new \NDB_BVL_InstrumentStatus($this->loris);
$status->select($instrument['CommentID']);
$dataEntry = $status->getDataEntryStatus();
if ($dataEntry != 'Complete') {
Expand All @@ -578,7 +590,7 @@ class Instrument_List_ControlPanel extends \TimePoint
// If the instrument requires double data entry,
// check that DDE is also done
if (in_array($instrument['Test_name'], $ddeInstruments)) {
$status = new \NDB_BVL_InstrumentStatus();
$status = new \NDB_BVL_InstrumentStatus($this->loris);
$status->select($instrument['DDECommentID']);
$dataEntry = $status->getDataEntryStatus();
if ($dataEntry != 'Complete') {
Expand Down Expand Up @@ -663,7 +675,7 @@ class Instrument_List_ControlPanel extends \TimePoint
$allDataEntryComplete =true;
foreach ($batteryList as $instrument) {

$status = new \NDB_BVL_InstrumentStatus();
$status = new \NDB_BVL_InstrumentStatus($this->loris);
$status->select($instrument['CommentID']);
$dataEntry = $status->getDataEntryStatus();
if ($dataEntry != 'Complete') {
Expand All @@ -674,7 +686,7 @@ class Instrument_List_ControlPanel extends \TimePoint
// If the instrument requires double data entry,
// check that DDE is also done
if (in_array($instrument['Test_name'], $ddeInstruments)) {
$status = new \NDB_BVL_InstrumentStatus();
$status = new \NDB_BVL_InstrumentStatus($this->loris);
$status->select($instrument['DDECommentID']);
$dataEntry = $status->getDataEntryStatus();
if ($dataEntry != 'Complete') {
Expand Down
3 changes: 2 additions & 1 deletion modules/instrument_list/php/module.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class Module extends \Module
// Falling back to instrument_list, ensure that the CandID
// and SessionID are valid, and if so attach the models to
// the request.
$page = $this->loadPage("instrument_list");
$loris = $request->getAttribute("loris");
$page = $this->loadPage($loris, "instrument_list");
'@phan-var Instrument_List $page';

$gets = $request->getQueryParams();
Expand Down
33 changes: 24 additions & 9 deletions modules/instrument_manager/php/instrument_manager.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,30 @@ class Instrument_Manager extends \NDB_Menu_Filter
* that are common to every type of page. May be overridden by a specific
* page or specific page type.
*
* @param Module $module The test name being accessed
* @param string $page The subtest being accessed (may be an empty string)
* @param string $id The identifier for the data to load on this page
* @param string $commentID The CommentID to load the data for
* @param string $formname The name to give this form
* @param \LORIS\LorisInstance $loris The LORIS Instance that is serving
* the page
* @param Module $module The test name being accessed
* @param string $page The subtest being accessed (may be
* an empty string)
* @param string $id The identifier for the data to load
* on this page
* @param string $commentID The CommentID to load the data for
* @param string $formname The name to give this form
*/
public function __construct($module, $page, $id, $commentID, $formname)
{
public function __construct(
\LORIS\LorisInstance $loris,
$module,
$page,
$id,
$commentID,
$formname
) {
$this->AjaxModule = true;
$this->skipTemplate = true;
$this->_factory = \NDB_Factory::singleton();
$this->_path = $this->_factory->config()->getSetting("base");

parent::__construct($module, $page, $id, $commentID);
parent::__construct($loris, $module, $page, $id, $commentID);
}

/**
Expand Down Expand Up @@ -184,7 +194,12 @@ class Instrument_Manager extends \NDB_Menu_Filter
// messsage from MySQL. This will be stored in $result and
// logged via LorisException.
try {
$table_name = \NDB_BVL_Instrument::factory($instrument, '', '')->table;
$table_name = \NDB_BVL_Instrument::factory(
$this->loris,
$instrument,
'',
'',
)->table;
} catch (\NotFound $e) {
return (new \LORIS\Http\Response\JSON\NotFound(
$e->getMessage()
Expand Down
5 changes: 4 additions & 1 deletion modules/instruments/php/module.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ class Module extends \Module
->withStatus(400)
->withBody(new \LORIS\Http\StringStream("Missing CommentID"));
}

$loris = $request->getAttribute("loris");
$instrument = \NDB_BVL_Instrument::factory(
$loris,
$instrumentName,
$commentID,
$page
Expand Down Expand Up @@ -163,7 +166,7 @@ class Module extends \Module
$dict = [];
foreach ($tests as $testname) {
try {
$inst = \NDB_BVL_Instrument::factory($testname, "", "");
$inst = \NDB_BVL_Instrument::factory($loris, $testname, "", "");
$cat = new \LORIS\Data\Dictionary\Category(
$testname,
$inst->getFullName()
Expand Down
1 change: 1 addition & 0 deletions modules/instruments/php/visitsummary.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class VisitSummary extends \NDB_Page
$commentID = $row['CommentID'];

$instrument = \NDB_BVL_Instrument::factory(
$this->loris,
$testName,
$commentID,
'',
Expand Down
Loading

0 comments on commit e4df353

Please sign in to comment.