Skip to content

Commit

Permalink
[Core] Add LorisInstance class (aces#6118)
Browse files Browse the repository at this point in the history
This adds a class to LORIS to represent an installed LORIS instance.

It is primarily a stub that can be expanded in the future.

Resolves aces#6022
  • Loading branch information
driusan authored and laemtl committed Jun 16, 2020
1 parent 7655efe commit e9919c1
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
2 changes: 2 additions & 0 deletions modules/dashboard/php/dashboard.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class Dashboard extends \NDB_Form
$DB = \NDB_Factory::singleton()->database();
$user = \User::singleton();

// FIXME: This should use \LORIS\LorisInstance->getActiveModules(), but there
// is no access to a LORISInstance object at this point in the code.
$this->modules = \Module::getActiveModules($DB);

$this->smallwidgets = [];
Expand Down
107 changes: 107 additions & 0 deletions src/LorisInstance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php declare(strict_types=1);

namespace LORIS;

/**
* A LorisInstance represents an installed instance of a LORIS
* project.
*
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
*/
class LorisInstance
{
protected $modulesDirs;
private $DB;

/**
* Construct a LORISInstance for the install connected to $db
* which uses modules from $moduleDirs.
*
* @param \Database $db A database connection to this
* instance.
* @param string[] $moduleDirs A list of directories that may
* contain modules for this instance.
*/
public function __construct(\Database $db, array $modulesDirs)
{
$this->DB = $db;
$this->modulesDirs = $modulesDirs;
}

/**
* Return an active database connection to this instance.
*
* @return \Database
*/
public function getDatabaseConnection() : \Database
{
return $this->DB;
}

/**
* Return a list of directories on the filesystem which
* may contain modules.
*
* @return string[]
*/
private function getModulesDirs() : array
{
return $this->modulesDirs;
}

/**
* Retrieve all active module descriptors from the given database.
*
* @param \Database $db an open connection to a database containing a 'modules'
* table
*
* @return \Module[]
*/
public function getActiveModules(): array
{
$mnames = $this->getDatabaseConnection()->pselectCol(
"SELECT Name FROM modules WHERE Active='Y'",
[]
);

$modules = [];
foreach ($mnames as $name) {
try {
$modules[] = \Module::factory($name);
} catch (\LorisModuleMissingException $e) {
error_log($e->getMessage() . " " . $e->getTraceAsString());
}
}
return $modules;
}

/**
* Return true if the LORISInstance has a module named
* $name. To be installed it must be both available on
* the filesystem and active in the modules table.
*
* @return bool
*/
public function hasModule(string $name) : bool
{
$dirs = $this->getModulesDirs();
$found = false;
foreach ($dirs as $subdir) {
if (is_dir($subdir . "/" . $name)) {
$found = true;
break;
}
}

if ($found === false) {
return false;
}

foreach ($this->getActiveModules() as $module) {
if ($module->getName() == $name) {
return true;
}
}
return false;
}
}
11 changes: 5 additions & 6 deletions src/Middleware/UserPageDecorationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function __construct(
string $pagename,
\NDB_Config $config,
array $JS,
array $CSS,
\Database $DB
array $CSS
) {

$this->JSFiles = $JS;
Expand All @@ -32,7 +31,6 @@ public function __construct(
$this->BaseURL = $baseurl;
$this->PageName = $pagename;
$this->user = $user;
$this->DB = $DB;
}

/**
Expand All @@ -48,14 +46,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
ob_start();
// Set the page template variables
// $user is set by the page base router
// $user and $loris is set by the page base router
$user = $request->getAttribute("user");
$loris = $request->getAttribute("loris");
$tpl_data = array(
'test_name' => $this->PageName,
);
$menu = [];

$menu = [];
$modules = \Module::getActiveModules($this->DB);
$modules = $loris->getActiveModules();
foreach ($modules as $module) {
if (!$module->hasAccess($user)) {
continue;
Expand Down

0 comments on commit e9919c1

Please sign in to comment.