Skip to content

Commit

Permalink
[document_repository] Data framework (aces#8906)
Browse files Browse the repository at this point in the history
Fixes aces#8894
  • Loading branch information
kongtiaowang authored Oct 29, 2024
1 parent 43108f9 commit b08cb72
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 44 deletions.
81 changes: 81 additions & 0 deletions modules/document_repository/php/docrepoprovisioner.class.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php declare(strict_types=1);

/**
* This file implements a data provisioner to get all modules
* for the Module Manager menu page.
*
* PHP Version 7
*
* @category Core
* @package Main
* @subpackage Core
* @author Shen Wang <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/

namespace LORIS\document_repository;

/**
* This class implements a data provisioner to get all modules
* for the module manager menu page.
*
* PHP Version 7
*
* @category Core
* @package Main
* @subpackage Core
* @author Shen Wang <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
class DocRepoProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
{
/**
* Create a ModuleManagerProvisioner, which gets modules for the
* module manager menu table.
*/
function __construct()
{
parent::__construct(
"
SELECT
v.File_name,
v.version,
v.File_type,
v.Instrument,
v.uploaded_by,
(SELECT name FROM psc WHERE CenterID = v.For_site) as site,
v.comments,
v.Date_uploaded,
v.record_id as Edit,
v.record_id as Delete_File,
v.File_category as category,
v.Data_dir,
v.For_site
FROM
document_repository v
WHERE
COALESCE(v.hide_video, false) = false
ORDER BY
v.File_name;
",
[],
);
}

/**
* Returns an instance of a HelpRow object for a given
* table row.
*
* @param array $row The database row from the LORIS Database class.
*
* @return \LORIS\Data\DataInstance An instance representing this row.
*/
public function getInstance($row) : \LORIS\Data\DataInstance
{
$cid = \CenterID::singleton(intval($row['For_site']));
return new DocRepoRow($row, $cid);
}

}
70 changes: 70 additions & 0 deletions modules/document_repository/php/docreporow.class.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php declare(strict_types=1);

/**
* This class implements a data Instance which represents a single
* module in the acknowledgements menu table.
*
* PHP Version 7
*
* @category Core
* @package Main
* @subpackage Core
* @author Dave MacFarlane <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/

namespace LORIS\document_repository;

/**
* A ModuleRow represents a row in the acknowledgements menu table.
*
* The acknowledgements requires a specific "row" concept because
* the \Module class does not have any concept of the Active flag
* for the module in order to serialize it as JSON for the data
* table.
*
* @category Core
* @package Main
* @subpackage Core
* @author Dave MacFarlane <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
class DocRepoRow implements
\LORIS\Data\DataInstance,
\LORIS\StudyEntities\SiteHaver
{
protected $DBRow;

private \CenterID $CenterID;
/**
* Create a new HelpRow
*
* @param array $row The row
* @param \CenterID $cid The CenterID
*/
public function __construct(array $row, \CenterID $cid)
{
$this->DBRow = $row;
$this->CenterID = $cid;
}
/**
* Returns the CenterID for this row
*
* @return \CenterID
*/
public function getCenterID(): \CenterID
{
return $this->CenterID;
}
/**
* Implements \LORIS\Data\DataInstance interface for this row.
*
* @return array which can be serialized by json_encode()
*/
public function jsonSerialize() : array
{
return $this->DBRow;
}
}
93 changes: 49 additions & 44 deletions modules/document_repository/php/document_repository.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace LORIS\document_repository;
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris
*/
class Document_Repository extends \NDB_Menu_Filter_Form
class Document_Repository extends \DataFrameworkMenu
{
private const PERMISSIONS = [
'document_repository_view',
Expand All @@ -46,13 +46,12 @@ class Document_Repository extends \NDB_Menu_Filter_Form
return $user->hasAnyPermission(self::PERMISSIONS);
}
/**
* Function _setupVariables
* Function getFieldOptions
*
* @return void
* @return array
*/
function setup()
protected function getFieldOptions() : array
{
parent::setup();
$user = \User::singleton();
$db = $this->loris->getDatabaseConnection();
$query = $db->pselect(
Expand Down Expand Up @@ -102,14 +101,13 @@ class Document_Repository extends \NDB_Menu_Filter_Form
foreach ($fileTypeQuery as $filetype) {
$fileTypeList[$filetype['File_type']] = $filetype['File_type'];
}
$this->fieldOptions = [
return [
'fileCategories' => $categoriesList,
'sites' => $siteList,
'instruments' => $instrumentsList,
'fileTypes' => $fileTypeList,
'hiddenFile' => ['yes' => "Yes", 'no' => "No"],
];
return;
}
/**
* Handler of parsing category
Expand Down Expand Up @@ -143,54 +141,61 @@ class Document_Repository extends \NDB_Menu_Filter_Form
"id" => $id,
];
}
/**
* Converts the results of this menu filter to a JSON format to be retrieved
* with ?format=json
*
* @return string a json encoded string of the headers and data from this table
*/
public function toJSON(): string
{
$table = (new \LORIS\Data\Table())
->withDataFrom($this->getDataProvisionerWithFilters());
$arr = array_map(
function ($row) {
return array_values($row);
},
json_decode($table->toJSON(\NDB_Factory::singleton()->user()), true)
);

return json_encode(
[
'Data' => $arr,
'fieldOptions' => $this->getFieldOptions(),
'maxUploadSize' => \Utility::getMaxUploadSize(),
]
);
}
/**
* Build a list of media to display in Data Table
* {@inheritDoc}
*
* @return void
* @throws \DatabaseException
* @return bool
*/
function _setupVariables()
public function useProjectFilter() : bool
{
$user = \User::singleton();
// the base query
$query = " FROM document_repository v";
$query .= " WHERE COALESCE(v.hide_video, false)=false";
if (!$user->hasPermission('document_repository_hidden')) {
$query .= " AND (hidden_file='no' OR hidden_file IS NULL)";
}
// set the class variables
$this->columns = [
'v.File_name',
'v.version',
'v.File_type',
'v.Instrument',
'v.uploaded_by',
'(SELECT name FROM psc WHERE CenterID=v.For_site) as site',
'v.comments',
'v.Date_uploaded',
'v.record_id as Edit',
'v.record_id as Delete_File',
'v.File_category as category',
'v.Data_dir',
];
$this->query = $query;
$this->group_by = '';
$this->order_by = 'v.File_name';
return false;
}

/**
* Converts the results of this menu filter to a JSON format to be retrieved
* with ?format=json
* {@inheritDoc}
*
* @return string a json encoded string of the headers and data from this table
* @return \Loris\Data\Provisioner
*/
function toJSON(): string
public function getBaseDataProvisioner(): \LORIS\Data\Provisioner
{
$result = $this->toArray();
$result['maxUploadSize'] = \Utility::getMaxUploadSize();
return json_encode($result);
return new DocRepoProvisioner();
}
/**
* Tells the base class that this page's provisioner can support the
* HasAnyPermissionOrUserSiteMatch filter.
*
* @return ?array of site permissions or null
*/
public function allSitePermissionNames() : ?array
{
return ['access_all_profiles'];

}


/**
* Include additional CSS files:
Expand Down

0 comments on commit b08cb72

Please sign in to comment.