Skip to content
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

[Core] Remove duplicated getActiveModules function #7446

Merged
merged 9 commits into from Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion htdocs/AjaxHelper.php
Expand Up @@ -89,7 +89,14 @@

$public = false;
try {
$m = Module::factory($Module);
// Anything that's still in an ajax directory isn't using the lorisinstance
// object, so for now just make a fake one to pass to the factory.
$loris = new \LORIS\LorisInstance(
new \Database(),
new \NDB_Config(),
[]
);
$m = Module::factory($loris, $Module);

$public = $m->isPublicModule();
} catch (LorisModuleMissingException $e) {
Expand Down
2 changes: 1 addition & 1 deletion modules/candidate_list/php/candidate_list.class.inc
Expand Up @@ -71,7 +71,7 @@ class Candidate_List extends \DataFrameworkMenu
{
// Relying on a side-effect of the the server process module to autoload
// its namespace.
\Module::factory('candidate_parameters');
\Module::factory($this->lorisinstance, 'candidate_parameters');

// create user object
$factory = \NDB_Factory::singleton();
Expand Down
12 changes: 11 additions & 1 deletion modules/candidate_parameters/ajax/getData.php
Expand Up @@ -302,7 +302,17 @@ function getFamilyInfoFields()
*/
function getParticipantStatusFields()
{
\Module::factory('candidate_parameters');
// All we care about is the namespace loading, so we just need to ensure
// that the directory path has this module in it
$loris = new \LORIS\LorisInstance(
new Database(),
new NDB_Config(),
[
__DIR__ . '../'
]
);

\Module::factory($loris, 'candidate_parameters');
$candID = new CandID($_GET['candID']);

$db = \NDB_Factory::singleton()->database();
Expand Down
3 changes: 1 addition & 2 deletions modules/candidate_profile/php/candidate_profile.class.inc
Expand Up @@ -72,10 +72,9 @@ class Candidate_Profile extends \NDB_Page
}

$factory = \NDB_Factory::singleton();
$DB = $factory->database();
$user = $factory->user();

$modules = \Module::getActiveModules($DB);
$modules = $this->lorisinstance->getActiveModules();

$widgets = [];
foreach ($modules as $module) {
Expand Down
4 changes: 1 addition & 3 deletions modules/candidate_profile/php/module.class.inc
Expand Up @@ -70,9 +70,7 @@ class Module extends \Module
case 'candidate':
$factory = \NDB_Factory::singleton();
$baseurl = $factory->settings()->getBaseURL();

$DB = $factory->database();
$modules = \Module::getActiveModules($DB);
$modules = $this->lorisinstance->getActiveModules();

$params = [];
foreach ($modules as $module) {
Expand Down
1 change: 0 additions & 1 deletion modules/dashboard/php/dashboard.class.inc
Expand Up @@ -79,7 +79,6 @@ class Dashboard extends \NDB_Form
// calculating the body.
$factory = \NDB_Factory::singleton();
$user = $factory->user();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should discuss this change

$modules = $loris->getActiveModules();

$this->_smallwidgets = [];
Expand Down
3 changes: 1 addition & 2 deletions modules/dashboard/php/module.class.inc
Expand Up @@ -136,8 +136,7 @@ class Module extends \Module
private function _getTasksWidget()
{
$factory = \NDB_Factory::singleton();
$DB = $factory->database();
$modules = \Module::getActiveModules($DB);
$modules = $this->lorisinstance->getActiveModules();
$user = $factory->user();

$widgets = [];
Expand Down
16 changes: 14 additions & 2 deletions modules/help_editor/ajax/help.php
Expand Up @@ -13,9 +13,19 @@
*/

try {
$factory = \NDB_Factory::singleton();
$loris = new \LORIS\LorisInstance(
$factory->database(),
$factory->config(),
[
__DIR__ . "/../../",
__DIR__ . "/../../../project/modules"
],
);

$moduleName = $_REQUEST['testName'] ?? null;
$subpageName = $_REQUEST['subtest'] ?? null;
$m = Module::factory($moduleName);
$m = Module::factory($loris, $moduleName);
// Load help data. Try to load subpage first as its more specific and
// will only be present some of the time. Fallback to the module name if
// no subpage present.
Expand All @@ -24,7 +34,9 @@
'format' => 'markdown',
];
print json_encode($help);
ob_end_flush();
if (ob_get_level() > 0) {
ob_end_flush();
}
exit;
} catch (Exception $e) {

Expand Down
4 changes: 2 additions & 2 deletions modules/issue_tracker/php/edit.class.inc
Expand Up @@ -162,7 +162,7 @@ class Edit extends \NDB_Page implements ETagCalculator
}
}

$allModules = \Module::getActiveModulesIndexed($db);
$allModules = \Module::getActiveModulesIndexed($this->loris);

$modules = [];
foreach ($allModules as $key => $m) {
Expand Down Expand Up @@ -306,7 +306,7 @@ class Edit extends \NDB_Page implements ETagCalculator
);

// looping by reference so can edit in place
$modules = \Module::getActiveModulesIndexed($db);
$modules = \Module::getActiveModulesIndexed($this->loris);
foreach ($unformattedComments as &$comment) {
if ($comment['fieldChanged'] === 'module') {
$mid = $comment['newValue'];
Expand Down
4 changes: 2 additions & 2 deletions modules/issue_tracker/php/issue_tracker.class.inc
Expand Up @@ -54,7 +54,7 @@ class Issue_Tracker extends \NDB_Menu_Filter_Form
*/
function getDataProvisioner() : \LORIS\Data\Provisioner
{
$provisioner = new IssueRowProvisioner($this->loris);
$provisioner = new IssueRowProvisioner($this->lorisinstance);

$factory = \NDB_Factory::singleton();
$user = $factory->user();
Expand Down Expand Up @@ -141,7 +141,7 @@ class Issue_Tracker extends \NDB_Menu_Filter_Form
}

$modules = array_reduce(
\Module::getActiveModules($db),
$this->lorisinstance->getActiveModules(),
function (
$result,
$module
Expand Down
10 changes: 7 additions & 3 deletions modules/issue_tracker/php/issuerowprovisioner.class.inc
Expand Up @@ -31,11 +31,12 @@ namespace LORIS\issue_tracker;
class IssueRowProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
{
private array $allCenterIDs;
private \LORIS\LorisInstance $lorisinstance;

/**
* Create a IssueRowProvisioner, which gets rows for the issues menu table.
*
* @param \LORIS\LorisInstance $loris The LORIS instance with the issues
* @param \LORIS\LorisInstance $loris the LORIS instance with the issues
*/
function __construct(\LORIS\LorisInstance $loris)
{
Expand All @@ -48,6 +49,9 @@ class IssueRowProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
$loris->getAllSites()
);
$userID = $user->getUsername();

$this->lorisinstance = $loris;

//note that this needs to be in the same order as the headers array
parent::__construct(
"SELECT i.issueID,
Expand Down Expand Up @@ -99,10 +103,10 @@ class IssueRowProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
static $modules = [];

$mname = $row['module'];
$module = new \NullModule();
$module = new \NullModule($this->lorisinstance);
if (!empty($mname)) {
if (!isset($modules[$mname])) {
$modules[$mname] = \Module::factory($mname);
$modules[$mname] = \Module::factory($this->lorisinstance, $mname);
}
$module = &$modules[$mname];
}
Expand Down
2 changes: 1 addition & 1 deletion modules/module_manager/php/module_manager.class.inc
Expand Up @@ -64,7 +64,7 @@ class Module_Manager extends \DataFrameworkMenu
*/
public function getBaseDataProvisioner(): \LORIS\Data\Provisioner
{
return new ModuleManagerProvisioner();
return new ModuleManagerProvisioner($this->lorisinstance);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions modules/module_manager/php/modulemanagerprovisioner.class.inc
Expand Up @@ -30,12 +30,17 @@ namespace LORIS\module_manager;
*/
class ModuleManagerProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
{
protected \LORIS\LorisInstance $lorisinstance;

/**
* Create a ModuleManagerProvisioner, which gets modules for the
* module manager menu table.
*
* @param \LORIS\LorisInstance $loris The LORIS instance
*/
function __construct()
function __construct(\LORIS\LorisInstance $loris)
{
$this->lorisinstance = $loris;
parent::__construct(
"SELECT name, active FROM modules",
[]
Expand All @@ -51,6 +56,6 @@ class ModuleManagerProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
*/
public function getInstance($row) : \LORIS\Data\DataInstance
{
return new ModuleRow($row);
return new ModuleRow($this->lorisinstance, $row);
}
}
11 changes: 7 additions & 4 deletions modules/module_manager/php/modulerow.class.inc
Expand Up @@ -35,16 +35,19 @@ class ModuleRow implements \LORIS\Data\DataInstance
protected $Module;
protected $DBRow;
protected $Active;
protected $lorisinstance;

/**
* Create a new ModuleRow Instance.
*
* @param array $row The ModuleRow Instance
* @param \LORIS\LorisInstance $loris The LORIS instance with the module
* @param array $row The ModuleRow Instance
*/
public function __construct(array $row)
public function __construct(\LORIS\LorisInstance $loris, array $row)
{
$this->Module = \Module::factory($row['name']);
$this->Active = $row['active'];
$this->lorisinstance = $loris;
$this->Module = \Module::factory($loris, $row['name']);
$this->Active = $row['active'];
}

/**
Expand Down
3 changes: 1 addition & 2 deletions modules/user_accounts/php/edit_user.class.inc
Expand Up @@ -935,8 +935,7 @@ class Edit_User extends \NDB_Form
= self::canRejectAccount(\User::factory($this->identifier));
}

// get the editor's permissions
$perms = $editor->getPermissionsVerbose();
$perms = $editor->getPermissionsVerbose($this->lorisinstance);

$lastRole = '';
$group = [];
Expand Down
71 changes: 30 additions & 41 deletions php/libraries/Module.class.inc
Expand Up @@ -40,53 +40,29 @@ abstract class Module extends \LORIS\Router\PrefixRouter
. "your project administrator and/or verify your LORIS configuration.";
protected $name;
protected $dir;
protected $lorisinstance;

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

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

/**
* Retrieve all active module descriptors from the given database, indexed
* by their primary key in the database.
*
* @param \Database $db an open connection to a database containing a 'modules'
* table
* @param \LORIS\LorisInstance $loris The LORIS instance with the modules
*
* @return \Module[]
*/
static public function getActiveModulesIndexed(\Database $db): array
{
static public function getActiveModulesIndexed(
\LORIS\LorisInstance $loris
) : array {
$db = $loris->getDatabaseConnection();
$mnames = $db->pselect(
"SELECT ID, Name FROM modules WHERE Active='Y'",
[]
);

$rv = [];
foreach ($mnames as $row) {
$rv[$row['ID']] = self::factory($row['Name']);
$rv[$row['ID']] = self::factory($loris, $row['Name']);
}
return $rv;
}
Expand All @@ -100,16 +76,19 @@ abstract class Module extends \LORIS\Router\PrefixRouter
* This also sets up PHP class autoloading for the module that is loaded,
* such that files in the module/php directory can be autoloaded.
*
* @param string $name The module name we'd like information about
* @param \Loris\LorisInstance $loris The LORIS instance containing the module
* @param string $name The module name we'd like information about
*
* @throws \LorisNoSuchModuleException
* @throws \LorisModuleMissingException
* @throws \NotFound
*
* @return \Module object
*/
static public function factory(string $name): \Module
{
static public function factory(
\Loris\LorisInstance $loris,
string $name
) : \Module {
$factory = NDB_Factory::singleton();
$config = $factory->config();
$base = $config->getSetting("base");
Expand Down Expand Up @@ -157,22 +136,29 @@ abstract class Module extends \LORIS\Router\PrefixRouter
// namespacing
//require_once $mpath . "/php/Module.class.inc";
$className = "\LORIS\\$name\Module";
$cls = new $className($name, $mpath);
$cls = new $className($loris, $name, $mpath);
return $cls;
}

/**
* Creates a new module instance
*
* @param string $name The name of the module
* @param string $moduledir The directory on the filesystem containing the module
* @param \LORIS\LorisInstance $loris The LORIS instance for the module
* @param string $name The name of the module
* @param string $moduledir The directory on the filesystem
* containing the module
*/
public function __construct(string $name, string $moduledir)
{
$config = \NDB_Factory::singleton()->config();
$loglevel = $config->getLogSettings()->getRequestLogLevel();
public function __construct(
\LORIS\LorisInstance $loris,
string $name,
string $moduledir
) {
$loglevel = $loris->getConfiguration()
->getLogSettings()
->getRequestLogLevel();

$this->logger = new \LORIS\Log\ErrorLogLogger($loglevel);

parent::__construct(
new \ArrayIterator(
[
Expand All @@ -197,6 +183,9 @@ abstract class Module extends \LORIS\Router\PrefixRouter
]
)
);

$this->lorisinstance = $loris;

$this->name = $name;
$this->dir = $moduledir;
}
Expand Down